@unchainedshop/roles 3.0.0-alpha3 → 3.0.0-alpha4
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 +3 -3
- package/src/roles.test.ts +71 -107
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unchainedshop/roles",
|
|
3
|
-
"version": "3.0.0-
|
|
3
|
+
"version": "3.0.0-alpha4",
|
|
4
4
|
"description": "Roles package for unchained engine",
|
|
5
5
|
"main": "lib/roles-index.js",
|
|
6
6
|
"exports": {
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
"lodash.clone": "4.5.0"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@types/node": "^22.
|
|
37
|
+
"@types/node": "^22.7.8",
|
|
38
38
|
"jest": "^29.7.0",
|
|
39
|
-
"typescript": "^5.6.
|
|
39
|
+
"typescript": "^5.6.3"
|
|
40
40
|
}
|
|
41
41
|
}
|
package/src/roles.test.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { isFunction, permissions, has } from
|
|
2
|
-
import { Role, Roles } from
|
|
3
|
-
|
|
1
|
+
import { isFunction, permissions, has } from './roles-index.js';
|
|
2
|
+
import { Role, Roles } from './roles.js';
|
|
4
3
|
|
|
5
4
|
describe('Role', () => {
|
|
6
5
|
beforeEach(() => {
|
|
@@ -10,13 +9,11 @@ describe('Role', () => {
|
|
|
10
9
|
});
|
|
11
10
|
describe('constructor', () => {
|
|
12
11
|
it('should create a new role with the given name', () => {
|
|
13
|
-
|
|
14
|
-
|
|
15
12
|
expect(new Role('admin').name).toBe('admin');
|
|
16
13
|
});
|
|
17
14
|
|
|
18
15
|
it('should throw an error if a role with the same name already exists', () => {
|
|
19
|
-
new Role('admin')
|
|
16
|
+
new Role('admin');
|
|
20
17
|
expect(() => new Role('admin')).toThrowError('"admin" role is already defined');
|
|
21
18
|
});
|
|
22
19
|
});
|
|
@@ -37,9 +34,7 @@ describe('Role', () => {
|
|
|
37
34
|
it('should convert a non-function value to a function that returns that value', () => {
|
|
38
35
|
const role = new Role('admin');
|
|
39
36
|
role.helper('checkPermission', true);
|
|
40
|
-
expect(role.helpers.checkPermission).toEqual([
|
|
41
|
-
expect.any(Function),
|
|
42
|
-
]);
|
|
37
|
+
expect(role.helpers.checkPermission).toEqual([expect.any(Function)]);
|
|
43
38
|
expect(role.helpers.checkPermission[0]()).toBe(true);
|
|
44
39
|
});
|
|
45
40
|
});
|
|
@@ -65,14 +60,11 @@ describe('Role', () => {
|
|
|
65
60
|
it('should convert a non-function value to a function that returns that value', () => {
|
|
66
61
|
const role = new Role('admin');
|
|
67
62
|
role.allow('createUser', true);
|
|
68
|
-
expect(role.allowRules.createUser).toEqual([
|
|
69
|
-
expect.any(Function),
|
|
70
|
-
]);
|
|
63
|
+
expect(role.allowRules.createUser).toEqual([expect.any(Function)]);
|
|
71
64
|
expect(role.allowRules.createUser[0]()).toBe(true);
|
|
72
65
|
});
|
|
73
66
|
});
|
|
74
67
|
|
|
75
|
-
|
|
76
68
|
describe('Role utilities', () => {
|
|
77
69
|
const testRole = new Role('test_role');
|
|
78
70
|
const actionName = 'view_secret';
|
|
@@ -80,19 +72,14 @@ describe('Role', () => {
|
|
|
80
72
|
it('should register an action rule', () => {
|
|
81
73
|
const allowFn = () => true;
|
|
82
74
|
testRole.allow(actionName, allowFn);
|
|
83
|
-
expect(testRole.allowRules[actionName]).toEqual(
|
|
84
|
-
expect.arrayContaining([allowFn])
|
|
85
|
-
);
|
|
75
|
+
expect(testRole.allowRules[actionName]).toEqual(expect.arrayContaining([allowFn]));
|
|
86
76
|
});
|
|
87
77
|
|
|
88
78
|
it('should register a helper', () => {
|
|
89
79
|
const helperFn = () => true;
|
|
90
80
|
testRole.helper('test_helper', helperFn);
|
|
91
|
-
expect(testRole.helpers.test_helper).toEqual(
|
|
92
|
-
expect.arrayContaining([helperFn])
|
|
93
|
-
);
|
|
81
|
+
expect(testRole.helpers.test_helper).toEqual(expect.arrayContaining([helperFn]));
|
|
94
82
|
});
|
|
95
|
-
|
|
96
83
|
});
|
|
97
84
|
describe('Role Helper Registration', () => {
|
|
98
85
|
it('should add a helper', () => {
|
|
@@ -102,18 +89,14 @@ describe('Role', () => {
|
|
|
102
89
|
|
|
103
90
|
it('should add a helper attaching it to adminRole', () => {
|
|
104
91
|
Roles.registerHelper('test_admin_helper');
|
|
105
|
-
expect(Roles.helpers).toEqual(
|
|
106
|
-
|
|
107
|
-
);
|
|
108
|
-
});
|
|
92
|
+
expect(Roles.helpers).toEqual(expect.arrayContaining(['test_admin_helper']));
|
|
93
|
+
});
|
|
109
94
|
|
|
110
95
|
it('should skip adding helper if it already exists', () => {
|
|
111
96
|
Roles.registerHelper('test_helper');
|
|
112
97
|
Roles.registerHelper('test_admin_helper');
|
|
113
98
|
|
|
114
|
-
expect(Roles.helpers).toEqual(
|
|
115
|
-
expect.arrayContaining(['test_helper', 'test_admin_helper'])
|
|
116
|
-
);
|
|
99
|
+
expect(Roles.helpers).toEqual(expect.arrayContaining(['test_helper', 'test_admin_helper']));
|
|
117
100
|
});
|
|
118
101
|
});
|
|
119
102
|
|
|
@@ -146,7 +129,11 @@ describe('Role', () => {
|
|
|
146
129
|
|
|
147
130
|
describe('isFunction', () => {
|
|
148
131
|
test('it should return true give a function', () => {
|
|
149
|
-
expect(
|
|
132
|
+
expect(
|
|
133
|
+
isFunction(() => {
|
|
134
|
+
/**/
|
|
135
|
+
}),
|
|
136
|
+
).toBe(true);
|
|
150
137
|
});
|
|
151
138
|
|
|
152
139
|
test('it should return false given an improper function', () => {
|
|
@@ -185,10 +172,7 @@ describe('permissions', () => {
|
|
|
185
172
|
admin: {
|
|
186
173
|
name: 'admin',
|
|
187
174
|
allowRules: {
|
|
188
|
-
createUser: [
|
|
189
|
-
async () => true,
|
|
190
|
-
async () => false,
|
|
191
|
-
],
|
|
175
|
+
createUser: [async () => true, async () => false],
|
|
192
176
|
},
|
|
193
177
|
},
|
|
194
178
|
};
|
|
@@ -202,10 +186,7 @@ describe('permissions', () => {
|
|
|
202
186
|
admin: {
|
|
203
187
|
name: 'admin',
|
|
204
188
|
allowRules: {
|
|
205
|
-
createUser: [
|
|
206
|
-
async () => true,
|
|
207
|
-
async () => false,
|
|
208
|
-
],
|
|
189
|
+
createUser: [async () => true, async () => false],
|
|
209
190
|
},
|
|
210
191
|
},
|
|
211
192
|
};
|
|
@@ -219,19 +200,13 @@ describe('permissions', () => {
|
|
|
219
200
|
admin: {
|
|
220
201
|
name: 'admin',
|
|
221
202
|
allowRules: {
|
|
222
|
-
createUser: [
|
|
223
|
-
async () => true,
|
|
224
|
-
async () => false,
|
|
225
|
-
],
|
|
203
|
+
createUser: [async () => true, async () => false],
|
|
226
204
|
},
|
|
227
205
|
},
|
|
228
206
|
user: {
|
|
229
207
|
name: 'user',
|
|
230
208
|
allowRules: {
|
|
231
|
-
viewUser: [
|
|
232
|
-
async () => true,
|
|
233
|
-
async () => false,
|
|
234
|
-
],
|
|
209
|
+
viewUser: [async () => true, async () => false],
|
|
235
210
|
},
|
|
236
211
|
},
|
|
237
212
|
};
|
|
@@ -245,23 +220,14 @@ describe('permissions', () => {
|
|
|
245
220
|
admin: {
|
|
246
221
|
name: 'admin',
|
|
247
222
|
allowRules: {
|
|
248
|
-
createUser: [
|
|
249
|
-
|
|
250
|
-
async () => true,
|
|
251
|
-
],
|
|
252
|
-
viewUser: [
|
|
253
|
-
async () => true,
|
|
254
|
-
async () => false,
|
|
255
|
-
],
|
|
223
|
+
createUser: [async () => true, async () => true],
|
|
224
|
+
viewUser: [async () => true, async () => false],
|
|
256
225
|
},
|
|
257
226
|
},
|
|
258
227
|
user: {
|
|
259
228
|
name: 'user',
|
|
260
229
|
allowRules: {
|
|
261
|
-
viewUser: [
|
|
262
|
-
async () => true,
|
|
263
|
-
async () => true,
|
|
264
|
-
],
|
|
230
|
+
viewUser: [async () => true, async () => true],
|
|
265
231
|
},
|
|
266
232
|
},
|
|
267
233
|
};
|
|
@@ -270,63 +236,61 @@ describe('permissions', () => {
|
|
|
270
236
|
});
|
|
271
237
|
});
|
|
272
238
|
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
239
|
+
describe('registerAction', () => {
|
|
240
|
+
it('should add a new action to the list of actions if it does not already exist', () => {
|
|
241
|
+
const action = 'createUser';
|
|
242
|
+
Roles.registerAction(action);
|
|
243
|
+
expect(Roles.actions).toContain(action);
|
|
244
|
+
});
|
|
279
245
|
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
});
|
|
246
|
+
it('should not add the same action multiple times', () => {
|
|
247
|
+
const action = 'createUser';
|
|
248
|
+
Roles.registerAction(action);
|
|
249
|
+
Roles.registerAction(action);
|
|
250
|
+
expect(Roles.actions).toEqual([action]);
|
|
286
251
|
});
|
|
252
|
+
});
|
|
287
253
|
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
254
|
+
describe('registerHelper', () => {
|
|
255
|
+
it('should add a new helper to the list of helpers if it does not already exist', () => {
|
|
256
|
+
const helper = 'checkAdmin';
|
|
257
|
+
Roles.registerHelper(helper);
|
|
258
|
+
expect(Roles.helpers).toContain(helper);
|
|
259
|
+
});
|
|
294
260
|
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
});
|
|
261
|
+
it('should not add the same helper multiple times', () => {
|
|
262
|
+
const helper = 'checkAdmin';
|
|
263
|
+
Roles.registerHelper(helper);
|
|
264
|
+
Roles.registerHelper(helper);
|
|
265
|
+
expect(Roles.helpers).toEqual([helper]);
|
|
301
266
|
});
|
|
267
|
+
});
|
|
302
268
|
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
269
|
+
describe('getUserRoles', () => {
|
|
270
|
+
it('should return the correct roles for a logged-in user', () => {
|
|
271
|
+
const userId = 'user1';
|
|
272
|
+
const roles = ['admin'];
|
|
273
|
+
const includeSpecial = true;
|
|
274
|
+
const expected = ['admin', '__all__', '__loggedIn__'];
|
|
275
|
+
const result = Roles.getUserRoles(userId, roles, includeSpecial);
|
|
276
|
+
expect(result).toEqual(expected);
|
|
277
|
+
});
|
|
312
278
|
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
279
|
+
it('should return the correct roles for a logged-out user', () => {
|
|
280
|
+
const userId = null;
|
|
281
|
+
const roles = ['admin'];
|
|
282
|
+
const includeSpecial = true;
|
|
283
|
+
const expected = ['admin', '__all__', '__notLoggedIn__'];
|
|
284
|
+
const result = Roles.getUserRoles(userId as any, roles, includeSpecial);
|
|
285
|
+
expect(result).toEqual(expected);
|
|
286
|
+
});
|
|
321
287
|
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
288
|
+
it('should return the correct roles when includeSpecial is false', () => {
|
|
289
|
+
const userId = 'user1';
|
|
290
|
+
const roles = ['admin'];
|
|
291
|
+
const includeSpecial = false;
|
|
292
|
+
const expected = ['admin'];
|
|
293
|
+
const result = Roles.getUserRoles(userId, roles, includeSpecial);
|
|
294
|
+
expect(result).toEqual(expected);
|
|
295
|
+
});
|
|
332
296
|
});
|