-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathauthorize-hook.test.js
61 lines (51 loc) · 1.79 KB
/
authorize-hook.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright IBM Corp. 2015,2018. All Rights Reserved.
// Node module: strong-remoting
// This file is licensed under the Artistic License 2.0.
// License text available at https://opensource.org/licenses/Artistic-2.0
'use strict';
const expect = require('./helpers/expect');
const express = require('express');
const RemoteObjects = require('../');
const User = require('./e2e/fixtures/user');
const fmt = require('util').format;
describe('authorization hook', function() {
let server, remotes;
before(function setupServer(done) {
const app = express();
remotes = RemoteObjects.create();
remotes.exports.User = User;
app.use(remotes.handler('rest'));
server = app.listen(0, '127.0.0.1', done);
});
after(function teardownServer(done) {
server.close(done);
});
describe('given a remotes object with an authorization hook', function() {
it('should be called when a remote method is invoked', function(done) {
const callStack = [];
remotes.authorization = function(ctx, next) {
callStack.push('authorization');
next();
};
remotes.before('User.login', function(ctx, next) {
callStack.push('before');
next();
});
invokeRemote(server.address().port,
function(err, session) {
expect(err).to.not.exist();
expect(session.userId).to.equal(123);
// vvvvvvvv - local before hook
expect(callStack).to.eql(['before', 'authorization', 'before']);
done();
});
});
});
function invokeRemote(port, callback) {
const url = 'http://127.0.0.1:' + port;
const method = 'User.login';
const args = [{username: 'joe', password: 'secret'}];
remotes.connect(url, 'rest');
remotes.invoke(method, args, callback);
}
});