@unchainedshop/roles 2.0.0-beta8 → 2.0.0-rc.1
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/jest.config.js +9 -0
- package/lib/roles-index.d.ts +1 -0
- package/lib/roles-index.js +1 -0
- package/lib/roles-index.js.map +1 -1
- package/lib/roles.js +3 -0
- package/lib/roles.js.map +1 -1
- package/lib/utils/permissions.d.ts +1 -0
- package/lib/utils/permissions.js +25 -0
- package/lib/utils/permissions.js.map +1 -0
- package/package.json +6 -6
- package/src/roles-index.ts +1 -0
- package/src/roles.ts +3 -0
- package/src/utils/permissions.ts +21 -0
- package/tests/roles.test.ts +331 -0
- package/index.ts +0 -2
- package/test/roles.test.js +0 -212
package/jest.config.js
CHANGED
package/lib/roles-index.d.ts
CHANGED
package/lib/roles-index.js
CHANGED
package/lib/roles-index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"roles-index.js","sourceRoot":"","sources":["../src/roles-index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAEtC,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAClC,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC"}
|
|
1
|
+
{"version":3,"file":"roles-index.js","sourceRoot":"","sources":["../src/roles-index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAEtC,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAClC,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC"}
|
package/lib/roles.js
CHANGED
package/lib/roles.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"roles.js","sourceRoot":"","sources":["../src/roles.ts"],"names":[],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,CAAC,MAAM,CAAC,EAAE;YACzB,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC;SAC3B;QAED,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IACtC,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,MAAM,EAAE,KAAK;QACjB,IAAI,CAAC,MAAM,EAAE;YACX,MAAM,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC;SACzC;QACD,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE;YACnC,KAAK,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;SAC9B;QACD,IAAI,OAAO,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE;YACxB,MAAM,WAAW,GAAG,KAAK,CAAC,OAAO,CAAC,CAAC;YACnC,OAAO,GAAG,GAAG,EAAE;gBACb,OAAO,WAAW,CAAC;YACrB,CAAC,CAAC;SACH;QACD,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;QACxD,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IACxC,CAAC;CACF;AAED;;GAEG;AACH,KAAK,CAAC,SAAS,GAAG,IAAI,IAAI,CAAC,OAAO,CAAyB,CAAC;AAC5D;;GAEG;AACH,KAAK,CAAC,YAAY,GAAG,IAAI,IAAI,CAAC,cAAc,CAAyB,CAAC;AACtE;;GAEG;AACH,KAAK,CAAC,OAAO,GAAG,IAAI,IAAI,CAAC,SAAS,CAAyB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const permissions: (userRoles: any, allRoles: any) => Promise<any[]>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export const permissions = async (userRoles, allRoles) => {
|
|
2
|
+
const actions = userRoles?.flatMap((role) => {
|
|
3
|
+
const foundRole = Object.values(allRoles).find((r) => r.name === role);
|
|
4
|
+
if (!foundRole)
|
|
5
|
+
return [];
|
|
6
|
+
return Object.entries(foundRole.allowRules || {}).flatMap(([roleName, funcs]) => {
|
|
7
|
+
return funcs.map(async (f) => {
|
|
8
|
+
try {
|
|
9
|
+
// return permissions that are by default activated for that role,
|
|
10
|
+
// accidentally don't check context
|
|
11
|
+
const r = await f(null, null, null);
|
|
12
|
+
if (r)
|
|
13
|
+
return roleName;
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
catch (e) {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
const resolvedActions = await Promise.all(actions);
|
|
23
|
+
return [...new Set(resolvedActions)].filter(Boolean).sort((a, b) => a.localeCompare(b));
|
|
24
|
+
};
|
|
25
|
+
//# sourceMappingURL=permissions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"permissions.js","sourceRoot":"","sources":["../../src/utils/permissions.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,WAAW,GAAG,KAAK,EAAE,SAAc,EAAE,QAAa,EAAE,EAAE;IACjE,MAAM,OAAO,GAAG,SAAS,EAAE,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;QAC1C,MAAM,SAAS,GAAQ,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,CAAC,CAAM,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC;QACjF,IAAI,CAAC,SAAS;YAAE,OAAO,EAAE,CAAC;QAC1B,OAAO,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,UAAU,IAAI,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,QAAQ,EAAE,KAAK,CAAM,EAAE,EAAE;YACnF,OAAO,KAAK,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,EAAE,EAAE;gBAC3B,IAAI;oBACF,kEAAkE;oBAClE,mCAAmC;oBACnC,MAAM,CAAC,GAAG,MAAM,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;oBACpC,IAAI,CAAC;wBAAE,OAAO,QAAQ,CAAC;oBACvB,OAAO,IAAI,CAAC;iBACb;gBAAC,OAAO,CAAC,EAAE;oBACV,OAAO,IAAI,CAAC;iBACb;YACH,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IACH,MAAM,eAAe,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IACnD,OAAO,CAAC,GAAG,IAAI,GAAG,CAAC,eAAe,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAM,EAAE,CAAM,EAAE,EAAE,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC;AACpG,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unchainedshop/roles",
|
|
3
|
-
"version": "2.0.0-
|
|
3
|
+
"version": "2.0.0-rc.1",
|
|
4
4
|
"description": "Roles package for unchained engine",
|
|
5
5
|
"main": "lib/roles-index.js",
|
|
6
6
|
"exports": {
|
|
@@ -13,7 +13,8 @@
|
|
|
13
13
|
"clean": "rm -rf lib",
|
|
14
14
|
"build": "npm run clean && tsc",
|
|
15
15
|
"watch": "tsc -w",
|
|
16
|
-
"test": "jest --
|
|
16
|
+
"test": "NODE_OPTIONS=--experimental-vm-modules jest --detectOpenHandles --forceExit",
|
|
17
|
+
"test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch --detectOpenHandles --forceExit"
|
|
17
18
|
},
|
|
18
19
|
"repository": {
|
|
19
20
|
"type": "git",
|
|
@@ -33,10 +34,9 @@
|
|
|
33
34
|
"lodash.clone": "4.5.0"
|
|
34
35
|
},
|
|
35
36
|
"devDependencies": {
|
|
36
|
-
"@types/node": "^18.11.
|
|
37
|
-
"@unchainedshop/types": "^2.0.0-
|
|
38
|
-
"chai": "^4.3.7",
|
|
37
|
+
"@types/node": "^18.11.17",
|
|
38
|
+
"@unchainedshop/types": "^2.0.0-rc.1",
|
|
39
39
|
"jest": "^29.3.1",
|
|
40
|
-
"typescript": "^4.9.
|
|
40
|
+
"typescript": "^4.9.4"
|
|
41
41
|
}
|
|
42
42
|
}
|
package/src/roles-index.ts
CHANGED
package/src/roles.ts
CHANGED
|
@@ -139,6 +139,9 @@ export class Role implements RoleInterface {
|
|
|
139
139
|
* Adds allow properties to a role
|
|
140
140
|
*/
|
|
141
141
|
allow(action, allow) {
|
|
142
|
+
if (!action) {
|
|
143
|
+
throw new Error(`Action doesn't exist`);
|
|
144
|
+
}
|
|
142
145
|
if (!Roles.actions.includes(action)) {
|
|
143
146
|
Roles.registerAction(action);
|
|
144
147
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export const permissions = async (userRoles: any, allRoles: any) => {
|
|
2
|
+
const actions = userRoles?.flatMap((role) => {
|
|
3
|
+
const foundRole: any = Object.values(allRoles).find((r: any) => r.name === role);
|
|
4
|
+
if (!foundRole) return [];
|
|
5
|
+
return Object.entries(foundRole.allowRules || {}).flatMap(([roleName, funcs]: any) => {
|
|
6
|
+
return funcs.map(async (f) => {
|
|
7
|
+
try {
|
|
8
|
+
// return permissions that are by default activated for that role,
|
|
9
|
+
// accidentally don't check context
|
|
10
|
+
const r = await f(null, null, null);
|
|
11
|
+
if (r) return roleName;
|
|
12
|
+
return null;
|
|
13
|
+
} catch (e) {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
const resolvedActions = await Promise.all(actions);
|
|
20
|
+
return [...new Set(resolvedActions)].filter(Boolean).sort((a: any, b: any) => a.localeCompare(b));
|
|
21
|
+
};
|
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
import { Role, Roles } from "../src/roles";
|
|
2
|
+
import { has, isFunction, permissions } from "../src/roles-index";
|
|
3
|
+
|
|
4
|
+
describe('Role', () => {
|
|
5
|
+
beforeEach(() => {
|
|
6
|
+
Roles.roles = {};
|
|
7
|
+
Roles.actions = [];
|
|
8
|
+
Roles.helpers = [];
|
|
9
|
+
});
|
|
10
|
+
describe('constructor', () => {
|
|
11
|
+
it('should create a new role with the given name', () => {
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
expect(new Role('admin').name).toBe('admin');
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('should throw an error if a role with the same name already exists', () => {
|
|
18
|
+
new Role('admin')
|
|
19
|
+
expect(() => new Role('admin')).toThrowError('"admin" role is already defined');
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
describe('helper', () => {
|
|
24
|
+
it('should add the helper function to the role', () => {
|
|
25
|
+
const role = new Role('admin');
|
|
26
|
+
role.helper('checkPermission', () => true);
|
|
27
|
+
expect(role.helpers.checkPermission).toEqual([expect.any(Function)]);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('should add the helper to the list of helpers if it does not exist', () => {
|
|
31
|
+
const role = new Role('admin');
|
|
32
|
+
role.helper('checkPermission', () => true);
|
|
33
|
+
expect(Roles.helpers).toEqual(['checkPermission']);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('should convert a non-function value to a function that returns that value', () => {
|
|
37
|
+
const role = new Role('admin');
|
|
38
|
+
role.helper('checkPermission', true);
|
|
39
|
+
expect(role.helpers.checkPermission).toEqual([
|
|
40
|
+
expect.any(Function),
|
|
41
|
+
]);
|
|
42
|
+
expect(role.helpers.checkPermission[0]()).toBe(true);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
describe('allow', () => {
|
|
47
|
+
it('should add the allow function to the role', () => {
|
|
48
|
+
const role = new Role('admin');
|
|
49
|
+
role.allow('createUser', () => true);
|
|
50
|
+
expect(role.allowRules.createUser).toEqual([expect.any(Function)]);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('should add the action to the list of actions if it does not exist', () => {
|
|
54
|
+
const role = new Role('admin');
|
|
55
|
+
role.allow('createUser', () => true);
|
|
56
|
+
expect(Roles.actions).toEqual(['createUser']);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('should throw an error if the action does not exist', () => {
|
|
60
|
+
const role = new Role('admin');
|
|
61
|
+
expect(() => role.allow(null, () => true)).toThrowError("Action doesn't exist");
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('should convert a non-function value to a function that returns that value', () => {
|
|
65
|
+
const role = new Role('admin');
|
|
66
|
+
role.allow('createUser', true);
|
|
67
|
+
expect(role.allowRules.createUser).toEqual([
|
|
68
|
+
expect.any(Function),
|
|
69
|
+
]);
|
|
70
|
+
expect(role.allowRules.createUser[0]()).toBe(true);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
describe('Role utilities', () => {
|
|
76
|
+
const testRole = new Role('test_role');
|
|
77
|
+
const actionName = 'view_secret';
|
|
78
|
+
|
|
79
|
+
it('should register an action rule', () => {
|
|
80
|
+
const allowFn = () => true;
|
|
81
|
+
testRole.allow(actionName, allowFn);
|
|
82
|
+
expect(testRole.allowRules[actionName]).toEqual(
|
|
83
|
+
expect.arrayContaining([allowFn])
|
|
84
|
+
);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('should register a helper', () => {
|
|
88
|
+
const helperFn = () => true;
|
|
89
|
+
testRole.helper('test_helper', helperFn);
|
|
90
|
+
expect(testRole.helpers.test_helper).toEqual(
|
|
91
|
+
expect.arrayContaining([helperFn])
|
|
92
|
+
);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
});
|
|
96
|
+
describe('Role Helper Registration', () => {
|
|
97
|
+
it('should add a helper', () => {
|
|
98
|
+
Roles.registerHelper('test_helper');
|
|
99
|
+
expect(Roles.helpers).toEqual(['test_helper']);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('should add a helper attaching it to adminRole', () => {
|
|
103
|
+
Roles.registerHelper('test_admin_helper');
|
|
104
|
+
expect(Roles.helpers).toEqual(
|
|
105
|
+
expect.arrayContaining(['test_admin_helper'])
|
|
106
|
+
);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('should skip adding helper if it already exists', () => {
|
|
110
|
+
Roles.registerHelper('test_helper');
|
|
111
|
+
Roles.registerHelper('test_admin_helper');
|
|
112
|
+
|
|
113
|
+
expect(Roles.helpers).toEqual(
|
|
114
|
+
expect.arrayContaining(['test_helper', 'test_admin_helper'])
|
|
115
|
+
);
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
describe('Action registration', () => {
|
|
120
|
+
it('should add an action', () => {
|
|
121
|
+
Roles.registerAction('test_action');
|
|
122
|
+
expect(Roles.actions).toEqual(['test_action']);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('should skip adding action if it already exists', () => {
|
|
126
|
+
Roles.registerAction('test_action');
|
|
127
|
+
expect(Roles.actions).toEqual(['test_action']);
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
describe('Role contruction', () => {
|
|
132
|
+
it('should construct a new role', () => {
|
|
133
|
+
expect(new Role('test_role')).toMatchObject({
|
|
134
|
+
name: 'test_role',
|
|
135
|
+
allowRules: {},
|
|
136
|
+
helpers: {},
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it('should throw an error if given a role with similar name', () => {
|
|
141
|
+
new Role('test_role');
|
|
142
|
+
expect(() => new Role('test_role')).toThrow();
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
describe('isFunction', () => {
|
|
147
|
+
test('it should return true give a function', () => {
|
|
148
|
+
expect(isFunction(() => {})).toBe(true);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test('it should return false given an improper function', () => {
|
|
152
|
+
expect(isFunction('false' as any)).toBe(false);
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
describe('has', () => {
|
|
157
|
+
test('it should return true for existent key', () => {
|
|
158
|
+
const obj = {
|
|
159
|
+
foo: 'bar',
|
|
160
|
+
};
|
|
161
|
+
expect(has(obj, 'foo')).toBe(true);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
test('it should return true for existent nested key', () => {
|
|
165
|
+
const obj = {
|
|
166
|
+
foo: { bar: 'baz' },
|
|
167
|
+
};
|
|
168
|
+
expect(has(obj, 'foo.bar')).toBe(true);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
test('it should return false for non existent', () => {
|
|
172
|
+
const obj = {
|
|
173
|
+
foo: 'bar',
|
|
174
|
+
};
|
|
175
|
+
expect(has(obj, 'baz')).toBe(false);
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
describe('permissions', () => {
|
|
181
|
+
it('should return an empty array if no user roles are provided', async () => {
|
|
182
|
+
const userRoles = [];
|
|
183
|
+
const allRoles = {
|
|
184
|
+
admin: {
|
|
185
|
+
name: 'admin',
|
|
186
|
+
allowRules: {
|
|
187
|
+
createUser: [
|
|
188
|
+
async () => true,
|
|
189
|
+
async () => false,
|
|
190
|
+
],
|
|
191
|
+
},
|
|
192
|
+
},
|
|
193
|
+
};
|
|
194
|
+
const result = await permissions(userRoles, allRoles);
|
|
195
|
+
expect(result).toEqual([]);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it('should return the correct permissions for a single user role', async () => {
|
|
199
|
+
const userRoles = ['admin'];
|
|
200
|
+
const allRoles = {
|
|
201
|
+
admin: {
|
|
202
|
+
name: 'admin',
|
|
203
|
+
allowRules: {
|
|
204
|
+
createUser: [
|
|
205
|
+
async () => true,
|
|
206
|
+
async () => false,
|
|
207
|
+
],
|
|
208
|
+
},
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
const result = await permissions(userRoles, allRoles);
|
|
212
|
+
expect(result).toEqual(['createUser']);
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it('should return the correct permissions for multiple user roles', async () => {
|
|
216
|
+
const userRoles = ['admin', 'user'];
|
|
217
|
+
const allRoles = {
|
|
218
|
+
admin: {
|
|
219
|
+
name: 'admin',
|
|
220
|
+
allowRules: {
|
|
221
|
+
createUser: [
|
|
222
|
+
async () => true,
|
|
223
|
+
async () => false,
|
|
224
|
+
],
|
|
225
|
+
},
|
|
226
|
+
},
|
|
227
|
+
user: {
|
|
228
|
+
name: 'user',
|
|
229
|
+
allowRules: {
|
|
230
|
+
viewUser: [
|
|
231
|
+
async () => true,
|
|
232
|
+
async () => false,
|
|
233
|
+
],
|
|
234
|
+
},
|
|
235
|
+
},
|
|
236
|
+
};
|
|
237
|
+
const result = await permissions(userRoles, allRoles);
|
|
238
|
+
expect(result).toEqual(['createUser', 'viewUser']);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it('should return unique and sorted permissions', async () => {
|
|
242
|
+
const userRoles = ['admin', 'user'];
|
|
243
|
+
const allRoles = {
|
|
244
|
+
admin: {
|
|
245
|
+
name: 'admin',
|
|
246
|
+
allowRules: {
|
|
247
|
+
createUser: [
|
|
248
|
+
async () => true,
|
|
249
|
+
async () => true,
|
|
250
|
+
],
|
|
251
|
+
viewUser: [
|
|
252
|
+
async () => true,
|
|
253
|
+
async () => false,
|
|
254
|
+
],
|
|
255
|
+
},
|
|
256
|
+
},
|
|
257
|
+
user: {
|
|
258
|
+
name: 'user',
|
|
259
|
+
allowRules: {
|
|
260
|
+
viewUser: [
|
|
261
|
+
async () => true,
|
|
262
|
+
async () => true,
|
|
263
|
+
],
|
|
264
|
+
},
|
|
265
|
+
},
|
|
266
|
+
};
|
|
267
|
+
const result = await permissions(userRoles, allRoles);
|
|
268
|
+
expect(result).toEqual(['createUser', 'viewUser']);
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
describe('registerAction', () => {
|
|
273
|
+
it('should add a new action to the list of actions if it does not already exist', () => {
|
|
274
|
+
const action = 'createUser';
|
|
275
|
+
Roles.registerAction(action);
|
|
276
|
+
expect(Roles.actions).toContain(action);
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
it('should not add the same action multiple times', () => {
|
|
280
|
+
const action = 'createUser';
|
|
281
|
+
Roles.registerAction(action);
|
|
282
|
+
Roles.registerAction(action);
|
|
283
|
+
expect(Roles.actions).toEqual([action]);
|
|
284
|
+
});
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
describe('registerHelper', () => {
|
|
288
|
+
it('should add a new helper to the list of helpers if it does not already exist', () => {
|
|
289
|
+
const helper = 'checkAdmin';
|
|
290
|
+
Roles.registerHelper(helper);
|
|
291
|
+
expect(Roles.helpers).toContain(helper);
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
it('should not add the same helper multiple times', () => {
|
|
295
|
+
const helper = 'checkAdmin';
|
|
296
|
+
Roles.registerHelper(helper);
|
|
297
|
+
Roles.registerHelper(helper);
|
|
298
|
+
expect(Roles.helpers).toEqual([helper]);
|
|
299
|
+
});
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
describe('getUserRoles', () => {
|
|
303
|
+
it('should return the correct roles for a logged-in user', () => {
|
|
304
|
+
const userId = 'user1';
|
|
305
|
+
const roles = ['admin'];
|
|
306
|
+
const includeSpecial = true;
|
|
307
|
+
const expected = ['admin', '__all__', '__loggedIn__'];
|
|
308
|
+
const result = Roles.getUserRoles(userId, roles, includeSpecial);
|
|
309
|
+
expect(result).toEqual(expected);
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
it('should return the correct roles for a logged-out user', () => {
|
|
313
|
+
const userId = null;
|
|
314
|
+
const roles = ['admin'];
|
|
315
|
+
const includeSpecial = true;
|
|
316
|
+
const expected = ['admin', '__all__', '__notLoggedIn__'];
|
|
317
|
+
const result = Roles.getUserRoles(userId as any, roles, includeSpecial);
|
|
318
|
+
expect(result).toEqual(expected);
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
it('should return the correct roles when includeSpecial is false', () => {
|
|
322
|
+
const userId = 'user1';
|
|
323
|
+
const roles = ['admin'];
|
|
324
|
+
const includeSpecial = false;
|
|
325
|
+
const expected = ['admin'];
|
|
326
|
+
const result = Roles.getUserRoles(userId, roles, includeSpecial);
|
|
327
|
+
expect(result).toEqual(expected);
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
});
|
package/index.ts
DELETED
package/test/roles.test.js
DELETED
|
@@ -1,212 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
describe,
|
|
3
|
-
test,
|
|
4
|
-
expect,
|
|
5
|
-
it,
|
|
6
|
-
beforeAll,
|
|
7
|
-
afterEach,
|
|
8
|
-
jest,
|
|
9
|
-
} from '@jest/globals';
|
|
10
|
-
import { setupDatabase } from '../../tests/helpers';
|
|
11
|
-
import { User } from '../../tests/seeds/users';
|
|
12
|
-
import { isFunction, has } from './helpers';
|
|
13
|
-
import { Role, Roles } from './index';
|
|
14
|
-
|
|
15
|
-
describe('Roles', () => {
|
|
16
|
-
beforeAll(async () => {
|
|
17
|
-
// eslint-disable-next-line no-unused-vars
|
|
18
|
-
await setupDatabase();
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
afterEach(() => {
|
|
22
|
-
Roles.roles = {};
|
|
23
|
-
Roles.actions = [];
|
|
24
|
-
Roles.helpers = [];
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
describe('Role utilities', () => {
|
|
28
|
-
const testRole = new Role('test_role');
|
|
29
|
-
const actionName = 'view_secret';
|
|
30
|
-
|
|
31
|
-
it('should register an action rule', () => {
|
|
32
|
-
const allowFn = () => true;
|
|
33
|
-
testRole.allow(actionName, allowFn);
|
|
34
|
-
expect(testRole.allowRules[actionName]).toEqual(
|
|
35
|
-
expect.arrayContaining([allowFn])
|
|
36
|
-
);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it('should register a helper', () => {
|
|
40
|
-
const helperFn = () => true;
|
|
41
|
-
testRole.helper('test_helper', helperFn);
|
|
42
|
-
expect(testRole.helpers.test_helper).toEqual(
|
|
43
|
-
expect.arrayContaining([helperFn])
|
|
44
|
-
);
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it('should return true if the user passes the allow check', () => {
|
|
48
|
-
const permissionTestRole = new Role('permission_test_role');
|
|
49
|
-
permissionTestRole.allow('view_data', () => true);
|
|
50
|
-
|
|
51
|
-
expect(Roles.allow('permission_user', 'view_data')).toBe(true);
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
it("should return false if the user doesn't pass the allow check", () => {
|
|
55
|
-
const permissionTestRole = new Role('permission_test_role');
|
|
56
|
-
permissionTestRole.allow('view_data', () => false);
|
|
57
|
-
|
|
58
|
-
expect(Roles.allow('permission_user', 'view_data')).toBe(false);
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
it('should return false given a false user id', () => {
|
|
62
|
-
const permissionTestRole = new Role('permission_test_role');
|
|
63
|
-
permissionTestRole.allow('view_data', () => false);
|
|
64
|
-
|
|
65
|
-
expect(Roles.allow('not_found_user', 'view_data')).toBe(false);
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
it('should return false given a false role', () => {
|
|
69
|
-
expect(Roles.allow('permission_user', 'view_data')).toBe(false);
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
describe('Add allow to role', () => {
|
|
74
|
-
it('allow', async () => {
|
|
75
|
-
const permissionTestRole = new Role('permission_test_role');
|
|
76
|
-
permissionTestRole.allow('view_data', () => true);
|
|
77
|
-
|
|
78
|
-
expect(Roles.userHasPermission('permission_user', 'view_data')).toBe(
|
|
79
|
-
true
|
|
80
|
-
);
|
|
81
|
-
});
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
describe('Retrieve Roles', () => {
|
|
85
|
-
it('should add roles to user document', async () => {
|
|
86
|
-
const result = Roles.addUserToRoles(User._id, 'test_role');
|
|
87
|
-
expect(result).toMatchObject({ ok: 1, nModified: 1, n: 1 });
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
it('should get roles attach to user', () => {
|
|
91
|
-
const roles = Roles.getUserRoles(User._id);
|
|
92
|
-
expect(roles).toEqual(expect.arrayContaining(['test_role']));
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it('should get roles including special ones', () => {
|
|
96
|
-
const roles = Roles.getUserRoles(User._id, true);
|
|
97
|
-
expect(roles).toEqual(
|
|
98
|
-
expect.arrayContaining([
|
|
99
|
-
'test_role',
|
|
100
|
-
'__all__',
|
|
101
|
-
'__loggedIn__',
|
|
102
|
-
'__notAdmin__',
|
|
103
|
-
])
|
|
104
|
-
);
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
it('should get admin user roles including special ones', () => {
|
|
108
|
-
const roles = Roles.getUserRoles('admin', true);
|
|
109
|
-
expect(roles).toEqual(
|
|
110
|
-
expect.arrayContaining([
|
|
111
|
-
'test_role',
|
|
112
|
-
'admin',
|
|
113
|
-
'__all__',
|
|
114
|
-
'__loggedIn__',
|
|
115
|
-
])
|
|
116
|
-
);
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
it('should get roles including special ones as not logged in user', () => {
|
|
120
|
-
const roles = Roles.getUserRoles(null, true);
|
|
121
|
-
expect(roles).toEqual(
|
|
122
|
-
expect.arrayContaining(['__all__', '__notLoggedIn__'])
|
|
123
|
-
);
|
|
124
|
-
});
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
describe('Role Helper Registration', () => {
|
|
128
|
-
it('should add a helper', () => {
|
|
129
|
-
Roles.registerHelper('test_helper');
|
|
130
|
-
expect(Roles.helpers).toEqual(['test_helper']);
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
it('should add a helper attaching it to adminRole', () => {
|
|
134
|
-
Roles.registerHelper('test_admin_helper', jest.fn());
|
|
135
|
-
expect(Roles.helpers).toEqual(
|
|
136
|
-
expect.arrayContaining(['test_admin_helper'])
|
|
137
|
-
);
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
it('should skip adding helper if it already exists', () => {
|
|
141
|
-
Roles.registerHelper('test_helper');
|
|
142
|
-
Roles.registerHelper('test_admin_helper');
|
|
143
|
-
|
|
144
|
-
expect(Roles.helpers).toEqual(
|
|
145
|
-
expect.arrayContaining(['test_helper', 'test_admin_helper'])
|
|
146
|
-
);
|
|
147
|
-
});
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
// Roles.registerAction
|
|
151
|
-
describe('Action registration', () => {
|
|
152
|
-
it('should add an action', () => {
|
|
153
|
-
Roles.registerAction('test_action');
|
|
154
|
-
expect(Roles.actions).toEqual(['test_action']);
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
it('should skip adding action if it already exists', () => {
|
|
158
|
-
Roles.registerAction('test_action');
|
|
159
|
-
expect(Roles.actions).toEqual(['test_action']);
|
|
160
|
-
});
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
// Roles.Role
|
|
164
|
-
describe('Role contruction', () => {
|
|
165
|
-
it('should construct a new role', () => {
|
|
166
|
-
expect(new Role('test_role')).toMatchObject({
|
|
167
|
-
name: 'test_role',
|
|
168
|
-
allowRules: {},
|
|
169
|
-
helpers: {},
|
|
170
|
-
});
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
it('should throw an error if given a role with similar name', () => {
|
|
174
|
-
// eslint-disable-next-line no-new
|
|
175
|
-
new Role('test_role');
|
|
176
|
-
expect(() => new Role('test_role')).toThrow();
|
|
177
|
-
});
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
describe('isFunction', () => {
|
|
181
|
-
test('it should return true give a function', () => {
|
|
182
|
-
expect(isFunction(() => {})).toBe(true);
|
|
183
|
-
});
|
|
184
|
-
|
|
185
|
-
test('it should return false given an improper function', () => {
|
|
186
|
-
expect(isFunction('false')).toBe(false);
|
|
187
|
-
});
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
describe('has', () => {
|
|
191
|
-
test('it should return true for existent key', () => {
|
|
192
|
-
const obj = {
|
|
193
|
-
foo: 'bar',
|
|
194
|
-
};
|
|
195
|
-
expect(has(obj, 'foo')).toBe(true);
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
test('it should return true for existent nested key', () => {
|
|
199
|
-
const obj = {
|
|
200
|
-
foo: { bar: 'baz' },
|
|
201
|
-
};
|
|
202
|
-
expect(has(obj, 'foo.bar')).toBe(true);
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
test('it should return false for non existent', () => {
|
|
206
|
-
const obj = {
|
|
207
|
-
foo: 'bar',
|
|
208
|
-
};
|
|
209
|
-
expect(has(obj, 'baz')).toBe(false);
|
|
210
|
-
});
|
|
211
|
-
});
|
|
212
|
-
});
|