@unchainedshop/api 3.1.1 → 3.1.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/package.json +5 -6
- package/tests/api-index.test.ts +18 -11
- package/jest.config.js +0 -17
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unchainedshop/api",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.2",
|
|
4
4
|
"main": "lib/api-index.js",
|
|
5
5
|
"types": "lib/api-index.d.ts",
|
|
6
6
|
"type": "module",
|
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
"build": "tsc -b",
|
|
10
10
|
"prepublishOnly": "npm run clean && npm run build",
|
|
11
11
|
"watch": "tsc -w",
|
|
12
|
-
"test": "
|
|
13
|
-
"test:watch": "
|
|
12
|
+
"test": "tsx --test",
|
|
13
|
+
"test:watch": "tsx --test --watch"
|
|
14
14
|
},
|
|
15
15
|
"repository": {
|
|
16
16
|
"type": "git",
|
|
@@ -37,8 +37,8 @@
|
|
|
37
37
|
"connect-mongo": ">= 5.1.0 < 6",
|
|
38
38
|
"express": ">= 5.0 < 6",
|
|
39
39
|
"express-session": ">= 1.18 < 2",
|
|
40
|
-
"graphql-yoga": ">= 5.9 < 6.0",
|
|
41
40
|
"fastify": ">= 5.2 < 6",
|
|
41
|
+
"graphql-yoga": ">= 5.9 < 6.0",
|
|
42
42
|
"passport": ">= 0.7 < 1"
|
|
43
43
|
},
|
|
44
44
|
"peerDependenciesMeta": {
|
|
@@ -92,9 +92,8 @@
|
|
|
92
92
|
"express": "^5.0.1",
|
|
93
93
|
"express-session": "^1.18.1",
|
|
94
94
|
"fastify": "^5.2.1",
|
|
95
|
-
"jest": "^29.7.0",
|
|
96
95
|
"passport": "^0.7.0",
|
|
97
|
-
"
|
|
96
|
+
"tsx": "^4.19.2",
|
|
98
97
|
"typescript": "^5.7.3"
|
|
99
98
|
}
|
|
100
99
|
}
|
package/tests/api-index.test.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
// Import the function to be tested.
|
|
2
|
+
import { describe, it, mock } from 'node:test';
|
|
3
|
+
import assert from 'node:assert';
|
|
2
4
|
import { admin } from '../src/roles/admin.js';
|
|
3
5
|
import { actions } from '../src/roles/index.js';
|
|
4
6
|
import { checkAction, ensureActionExists, ensureIsFunction } from '../src/acl.js';
|
|
@@ -8,24 +10,28 @@ import { Roles } from '@unchainedshop/roles';
|
|
|
8
10
|
describe('API', () => {
|
|
9
11
|
describe('roles', () => {
|
|
10
12
|
const role = {
|
|
11
|
-
allow:
|
|
13
|
+
allow: mock.fn(),
|
|
12
14
|
};
|
|
13
15
|
|
|
14
16
|
it('creates the admin role and grants permissions to all actions', () => {
|
|
15
17
|
admin(role, actions);
|
|
16
18
|
for (const actionName of Object.keys(actions)) {
|
|
17
|
-
|
|
19
|
+
assert.equal(
|
|
20
|
+
role.allow.mock.calls.some((call) => call.arguments[0] === actions[actionName]),
|
|
21
|
+
true,
|
|
22
|
+
"action wasn't granted",
|
|
23
|
+
);
|
|
18
24
|
}
|
|
19
25
|
});
|
|
20
26
|
});
|
|
21
27
|
|
|
22
28
|
describe('ensureActionExists', () => {
|
|
23
29
|
it('should throw a PermissionSystemError if the action is undefined', () => {
|
|
24
|
-
|
|
30
|
+
assert.throws(() => ensureActionExists(undefined, {}), PermissionSystemError);
|
|
25
31
|
});
|
|
26
32
|
|
|
27
33
|
it('should not throw an error if the action is defined', () => {
|
|
28
|
-
|
|
34
|
+
assert.doesNotThrow(() => ensureActionExists('some action', {}));
|
|
29
35
|
});
|
|
30
36
|
});
|
|
31
37
|
|
|
@@ -34,14 +40,14 @@ describe('API', () => {
|
|
|
34
40
|
const action = 'some action';
|
|
35
41
|
const options = { showKey: true };
|
|
36
42
|
const key = 'some key';
|
|
37
|
-
|
|
43
|
+
assert.throws(() => ensureIsFunction(null, action, options, key), PermissionSystemError);
|
|
38
44
|
});
|
|
39
45
|
|
|
40
46
|
it('should not throw an error if the provided value is a function', () => {
|
|
41
47
|
const action = 'some action';
|
|
42
48
|
const options = { showKey: true };
|
|
43
49
|
const key = 'some key';
|
|
44
|
-
|
|
50
|
+
assert.doesNotThrow(() =>
|
|
45
51
|
ensureIsFunction(
|
|
46
52
|
() => {
|
|
47
53
|
/**/
|
|
@@ -50,30 +56,31 @@ describe('API', () => {
|
|
|
50
56
|
options,
|
|
51
57
|
key,
|
|
52
58
|
),
|
|
53
|
-
)
|
|
59
|
+
);
|
|
54
60
|
});
|
|
55
61
|
});
|
|
56
62
|
|
|
57
63
|
describe('checkAction', () => {
|
|
58
64
|
it('should throw a NoPermissionError if the user does not have permission to perform the action', async () => {
|
|
59
|
-
Roles.userHasPermission =
|
|
65
|
+
Roles.userHasPermission = mock.fn(async () => false);
|
|
60
66
|
|
|
61
67
|
const context = { userId: '123' };
|
|
62
68
|
const action = 'some action';
|
|
63
69
|
const args: any = [];
|
|
64
70
|
const options = { key: 'some key' };
|
|
65
71
|
|
|
66
|
-
return
|
|
72
|
+
return assert.rejects(checkAction(context, action, args, options), NoPermissionError);
|
|
67
73
|
});
|
|
68
74
|
|
|
69
75
|
it('should not throw an error if the user has permission to perform the action', async () => {
|
|
70
|
-
Roles.userHasPermission =
|
|
76
|
+
Roles.userHasPermission = mock.fn(async () => true);
|
|
77
|
+
|
|
71
78
|
const context = { userId: '123' };
|
|
72
79
|
const action = 'some action';
|
|
73
80
|
const args: any = {};
|
|
74
81
|
const options = { key: 'some key' };
|
|
75
82
|
|
|
76
|
-
return
|
|
83
|
+
return assert.doesNotReject(checkAction(context, action, args, options));
|
|
77
84
|
});
|
|
78
85
|
});
|
|
79
86
|
});
|
package/jest.config.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
|
|
2
|
-
export default {
|
|
3
|
-
preset: 'ts-jest/presets/default-esm', // or other ESM presets
|
|
4
|
-
testEnvironment: 'node',
|
|
5
|
-
extensionsToTreatAsEsm: ['.ts'],
|
|
6
|
-
transform: {
|
|
7
|
-
'^.+\\.tsx?$': [
|
|
8
|
-
'ts-jest',
|
|
9
|
-
{
|
|
10
|
-
useESM: true,
|
|
11
|
-
},
|
|
12
|
-
],
|
|
13
|
-
},
|
|
14
|
-
moduleNameMapper: {
|
|
15
|
-
'^(\\.{1,2}/.*)\\.js$': '$1',
|
|
16
|
-
},
|
|
17
|
-
};
|