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 CHANGED
@@ -5,7 +5,7 @@
5
5
  },
6
6
  "extends": "airbnb-base",
7
7
  "parserOptions": {
8
- "ecmaVersion": 2018
8
+ "ecmaVersion": 2020
9
9
  },
10
10
  "rules": {
11
11
  "comma-dangle": [2, "never"],
package/index.js CHANGED
@@ -33,7 +33,8 @@ module.exports = {
33
33
  authorizeUser: {
34
34
  mq: authorizeUser,
35
35
  express: (req, res, next) => {
36
- authorizeUser(req);
36
+ const user = req?.user || req?.context?.user;
37
+ authorizeUser({ user });
37
38
  next();
38
39
  }
39
40
  },
@@ -2,7 +2,11 @@
2
2
 
3
3
  const errors = require('exframe-errors');
4
4
 
5
- module.exports = context => {
6
- if (!context.user)
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.0",
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": "1959247957f1a308c44d4c5210712125ab6e5573"
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
- it('throws unauthorized error when no user', () => {
10
- try { authorizeUser.mq({}); }
11
- catch (e) {
12
- expect(e).to.be.an.instanceOf(errors.Unauthorized);
13
- return;
14
- }
15
-
16
- expect.fail('authorizeUser should have thrown an Unauthorized error');
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
- it('does not throw an error when there is a user', () => {
20
- try { authorizeUser.mq({ user: 'something' }); }
21
- catch (e) { expect.fail('authorizeUser should not have thrown an error'); }
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
- it('works when there is a user and calls next when using express version', () => {
25
- let called = 0;
26
- const next = () => ++called;
27
- authorizeUser.express({ user: {} }, {}, next);
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
- expect(called).to.equal(1);
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
  });