exframe-middleware 1.3.0 → 1.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.json +1 -1
- package/index.js +2 -1
- package/lib/authorizeUser.js +6 -2
- package/package.json +2 -2
- package/test/authorizeUser.test.js +52 -17
package/.eslintrc.json
CHANGED
package/index.js
CHANGED
package/lib/authorizeUser.js
CHANGED
|
@@ -2,7 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
const errors = require('exframe-errors');
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
/**
|
|
6
|
+
* @param {Object} context
|
|
7
|
+
* @param {Object} context.user
|
|
8
|
+
*/
|
|
9
|
+
module.exports = ({ user }) => {
|
|
10
|
+
if (!user)
|
|
7
11
|
throw new errors.Unauthorized('Could not authorize user');
|
|
8
12
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "exframe-middleware",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"config": {
|
|
@@ -33,5 +33,5 @@
|
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"exframe-errors": "^1.2.0"
|
|
35
35
|
},
|
|
36
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "6c4adb8a4ad4a5a60f1b98fb94dc4a13026eb796"
|
|
37
37
|
}
|
|
@@ -6,26 +6,61 @@ const errors = require('exframe-errors');
|
|
|
6
6
|
const { authorizeUser } = require('../index');
|
|
7
7
|
|
|
8
8
|
context('authorizeUser', () => {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
9
|
+
describe('mq', () => {
|
|
10
|
+
it('throws unauthorized error when no user', () => {
|
|
11
|
+
try { authorizeUser.mq({}); }
|
|
12
|
+
catch (e) {
|
|
13
|
+
expect(e).to.be.an.instanceOf(errors.Unauthorized);
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
expect.fail('authorizeUser should have thrown an Unauthorized error');
|
|
18
|
+
});
|
|
18
19
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
it('does not throw an error when there is a user', () => {
|
|
21
|
+
try { authorizeUser.mq({ user: 'something' }); }
|
|
22
|
+
catch (e) { expect.fail('authorizeUser should not have thrown an error'); }
|
|
23
|
+
});
|
|
22
24
|
});
|
|
23
25
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
describe('express - user exists directly on the request object', () => {
|
|
27
|
+
it('calls the next function when there is a user', () => {
|
|
28
|
+
let called = 0;
|
|
29
|
+
const next = () => ++called;
|
|
30
|
+
authorizeUser.express({ user: {} }, {}, next);
|
|
31
|
+
|
|
32
|
+
expect(called).to.equal(1);
|
|
33
|
+
});
|
|
34
|
+
it('throws unauthorized error when there is no user', () => {
|
|
35
|
+
let called = 0;
|
|
36
|
+
const next = () => ++called;
|
|
37
|
+
try { authorizeUser.express({ }, {}, next); }
|
|
38
|
+
catch (e) {
|
|
39
|
+
expect(e).to.be.an.instanceOf(errors.Unauthorized);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
expect.fail('authorizeUser should have thrown an Unauthorized error');
|
|
43
|
+
expect(called).to.equal(0);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
28
46
|
|
|
29
|
-
|
|
47
|
+
describe('express - user exists within the context on the request object', () => {
|
|
48
|
+
it('calls the next function when there is a user', () => {
|
|
49
|
+
let called = 0;
|
|
50
|
+
const next = () => ++called;
|
|
51
|
+
authorizeUser.express({ context: { user: {} } }, {}, next);
|
|
52
|
+
expect(called).to.equal(1);
|
|
53
|
+
});
|
|
54
|
+
it('throws unauthorized error when there is no user', () => {
|
|
55
|
+
let called = 0;
|
|
56
|
+
const next = () => ++called;
|
|
57
|
+
try { authorizeUser.express({ context: {} }, {}, next); }
|
|
58
|
+
catch (e) {
|
|
59
|
+
expect(e).to.be.an.instanceOf(errors.Unauthorized);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
expect.fail('authorizeUser should have thrown an Unauthorized error');
|
|
63
|
+
expect(called).to.equal(0);
|
|
64
|
+
});
|
|
30
65
|
});
|
|
31
66
|
});
|