1
- /* eslint brace-style:0, max-statements-per-line:0 */
2
-
3
1
/**
4
2
* Test Suite: AccessControl (Core)
5
- * @author Onur Yıldırım ( onur@cutepilot.com)
3
+ * @author Onur Yıldırım < onur@cutepilot.com>
6
4
*/
7
5
8
- var AccessControl = require ( '../lib' ) . AccessControl ;
6
+ const AccessControl = require ( '../lib' ) . AccessControl ;
7
+
8
+ function type ( o ) {
9
+ return Object . prototype . toString . call ( o ) . match ( / \s ( \w + ) / i) [ 1 ] . toLowerCase ( ) ;
10
+ }
9
11
10
12
describe ( 'Test Suite: Access Control' , function ( ) {
11
13
'use strict' ;
12
14
13
15
// grant list fetched from DB (to be converted to a valid grants object)
14
- var grantList = [
16
+ let grantList = [
15
17
{ role : 'admin' , resource : 'video' , action : 'create:any' , attributes : [ '*' ] } ,
16
18
{ role : 'admin' , resource : 'video' , action : 'read:any' , attributes : [ '*' ] } ,
17
19
{ role : 'admin' , resource : 'video' , action : 'update:any' , attributes : [ '*' ] } ,
@@ -24,7 +26,7 @@ describe('Test Suite: Access Control', function () {
24
26
] ;
25
27
26
28
// valid grants object
27
- var grantsObject = {
29
+ let grantsObject = {
28
30
admin : {
29
31
video : {
30
32
'create:any' : [ '*' ] ,
@@ -51,8 +53,24 @@ describe('Test Suite: Access Control', function () {
51
53
// TESTS
52
54
//----------------------------
53
55
56
+ it ( 'should construct with grants array or object, output a grants object' , function ( ) {
57
+ let ac = new AccessControl ( grantList ) ;
58
+ let grants = ac . getGrants ( ) ;
59
+ expect ( type ( grants ) ) . toEqual ( 'object' ) ;
60
+ expect ( type ( grants . admin ) ) . toEqual ( 'object' ) ;
61
+ expect ( grants . admin . video [ 'create:any' ] ) . toEqual ( jasmine . any ( Array ) ) ;
62
+ // console.log(grants);
63
+
64
+ ac = new AccessControl ( grantsObject ) ;
65
+ grants = ac . getGrants ( ) ;
66
+ expect ( type ( grants ) ) . toEqual ( 'object' ) ;
67
+ expect ( type ( grants . admin ) ) . toEqual ( 'object' ) ;
68
+ expect ( grants . admin . video [ 'create:any' ] ) . toEqual ( jasmine . any ( Array ) ) ;
69
+ } ) ;
70
+
71
+
54
72
it ( 'should add grants from flat list (db), check/remove roles and resources' , function ( ) {
55
- var ac = this . ac ;
73
+ let ac = this . ac ;
56
74
ac . setGrants ( grantList ) ;
57
75
// console.log('grants', ac.getGrants());
58
76
// console.log('resources', ac.getResources());
@@ -78,7 +96,7 @@ describe('Test Suite: Access Control', function () {
78
96
} ) ;
79
97
80
98
it ( 'should grant/deny access and check permissions' , function ( ) {
81
- var ac = this . ac ,
99
+ let ac = this . ac ,
82
100
attrs = [ '*' , '!size' ] ;
83
101
84
102
ac . grant ( 'user' ) . createAny ( 'photo' , attrs ) ;
@@ -121,7 +139,7 @@ describe('Test Suite: Access Control', function () {
121
139
} ) ;
122
140
123
141
it ( 'should chain grant methods and check permissions' , function ( ) {
124
- var ac = this . ac ,
142
+ let ac = this . ac ,
125
143
attrs = [ '*' ] ;
126
144
127
145
ac . grant ( 'superadmin' )
@@ -137,23 +155,23 @@ describe('Test Suite: Access Control', function () {
137
155
} ) ;
138
156
139
157
it ( 'should grant/deny access via object and check permissions' , function ( ) {
140
- var ac = this . ac ,
158
+ let ac = this . ac ,
141
159
attrs = [ '*' ] ;
142
160
143
- var o1 = {
161
+ let o1 = {
144
162
role : 'moderator' ,
145
163
resource : 'post' ,
146
164
action : 'create:any' , // action:possession
147
165
attributes : [ '*' ] // grant only
148
166
} ;
149
- var o2 = {
167
+ let o2 = {
150
168
role : 'moderator' ,
151
169
resource : 'news' ,
152
170
action : 'read' , // separate action
153
171
possession : 'own' , // separate possession
154
172
attributes : [ '*' ] // grant only
155
173
} ;
156
- var o3 = {
174
+ let o3 = {
157
175
role : 'moderator' ,
158
176
resource : 'book' ,
159
177
// no action/possession set
@@ -187,7 +205,7 @@ describe('Test Suite: Access Control', function () {
187
205
} ) ;
188
206
189
207
it ( 'should grant/deny access (variation, chained)' , function ( ) {
190
- var ac = this . ac ;
208
+ let ac = this . ac ;
191
209
ac . setGrants ( grantsObject ) ;
192
210
193
211
expect ( ac . can ( 'admin' ) . createAny ( 'video' ) . granted ) . toEqual ( true ) ;
@@ -254,7 +272,7 @@ describe('Test Suite: Access Control', function () {
254
272
} ) ;
255
273
256
274
it ( 'should switch-chain grant/deny roles' , function ( ) {
257
- var ac = this . ac ;
275
+ let ac = this . ac ;
258
276
ac . grant ( 'r1' )
259
277
. createOwn ( 'a' )
260
278
. grant ( 'r2' )
@@ -277,13 +295,13 @@ describe('Test Suite: Access Control', function () {
277
295
} ) ;
278
296
279
297
it ( 'deny should auto-set attributes to []' , function ( ) {
280
- var ac = this . ac ;
298
+ let ac = this . ac ;
281
299
ac . deny ( 'user' ) . createAny ( 'book' , [ '*' ] ) ;
282
300
expect ( ac . getGrants ( ) . user . book [ 'create:any' ] ) . toEqual ( [ ] ) ;
283
301
} ) ;
284
302
285
303
it ( 'should grant comma/semi-colon separated roles' , function ( ) {
286
- var ac = this . ac ;
304
+ let ac = this . ac ;
287
305
// also supporting comma/semi-colon separated roles
288
306
ac . grant ( 'role2; role3, editor; viewer, agent' ) . createOwn ( 'book' ) ;
289
307
expect ( ac . hasRole ( 'role3' ) ) . toEqual ( true ) ;
@@ -292,7 +310,7 @@ describe('Test Suite: Access Control', function () {
292
310
} ) ;
293
311
294
312
it ( 'permission should also return queried role(s) and resource' , function ( ) {
295
- var ac = this . ac ;
313
+ let ac = this . ac ;
296
314
// also supporting comma/semi-colon separated roles
297
315
ac . grant ( 'foo, bar' ) . createOwn ( 'baz' ) ;
298
316
expect ( ac . can ( 'bar' ) . createAny ( 'baz' ) . granted ) . toEqual ( false ) ;
@@ -305,7 +323,7 @@ describe('Test Suite: Access Control', function () {
305
323
} ) ;
306
324
307
325
it ( 'should extend / remove roles' , function ( ) {
308
- var ac = this . ac ;
326
+ let ac = this . ac ;
309
327
310
328
ac . grant ( 'admin' ) . createOwn ( 'book' ) ;
311
329
ac . extendRole ( 'onur' , 'admin' ) ;
@@ -333,14 +351,14 @@ describe('Test Suite: Access Control', function () {
333
351
expect ( ac . getGrants ( ) . admin . $extend ) . not . toContain ( 'editor' ) ;
334
352
expect ( ac . getGrants ( ) . admin . $extend ) . not . toContain ( 'agent' ) ;
335
353
336
- expect ( function ( ) { ac . grant ( 'roleX' ) . extend ( 'roleX' ) ; } ) . toThrow ( ) ;
337
- expect ( function ( ) { ac . grant ( [ 'admin2' , 'roleX' ] ) . extend ( [ 'roleX' , 'admin3' ] ) ; } ) . toThrow ( ) ;
354
+ expect ( ( ) => ac . grant ( 'roleX' ) . extend ( 'roleX' ) ) . toThrow ( ) ;
355
+ expect ( ( ) => ac . grant ( [ 'admin2' , 'roleX' ] ) . extend ( [ 'roleX' , 'admin3' ] ) ) . toThrow ( ) ;
338
356
339
357
// console.log(JSON.stringify(ac.getGrants(), null, ' '));
340
358
} ) ;
341
359
342
360
it ( 'should throw if grant or deny objects are invalid' , function ( ) {
343
- var o ,
361
+ let o ,
344
362
ac = this . ac ;
345
363
346
364
o = {
@@ -349,61 +367,61 @@ describe('Test Suite: Access Control', function () {
349
367
action : 'create:any' ,
350
368
attributes : [ '*' ] // grant only
351
369
} ;
352
- expect ( function ( ) { ac . grant ( o ) ; } ) . toThrow ( ) ;
353
- expect ( function ( ) { ac . deny ( o ) ; } ) . toThrow ( ) ;
370
+ expect ( ( ) => ac . grant ( o ) ) . toThrow ( ) ;
371
+ expect ( ( ) => ac . deny ( o ) ) . toThrow ( ) ;
354
372
355
373
o = {
356
374
role : 'moderator' ,
357
375
resource : null , // invalid resource, should be non-empty string
358
376
action : 'create:any' ,
359
377
attributes : [ '*' ] // grant only
360
378
} ;
361
- expect ( function ( ) { ac . grant ( o ) ; } ) . toThrow ( ) ;
362
- expect ( function ( ) { ac . deny ( o ) ; } ) . toThrow ( ) ;
379
+ expect ( ( ) => ac . grant ( o ) ) . toThrow ( ) ;
380
+ expect ( ( ) => ac . deny ( o ) ) . toThrow ( ) ;
363
381
364
382
o = {
365
383
role : 'admin' ,
366
384
resource : 'post' ,
367
385
action : 'put:any' , // invalid action, should be create|read|update|delete
368
386
attributes : [ '*' ] // grant only
369
387
} ;
370
- expect ( function ( ) { ac . grant ( o ) ; } ) . toThrow ( ) ;
371
- expect ( function ( ) { ac . deny ( o ) ; } ) . toThrow ( ) ;
388
+ expect ( ( ) => ac . grant ( o ) ) . toThrow ( ) ;
389
+ expect ( ( ) => ac . deny ( o ) ) . toThrow ( ) ;
372
390
373
391
o = {
374
392
role : 'admin' ,
375
393
resource : 'post' ,
376
394
action : null , // invalid action, should be create|read|update|delete
377
395
attributes : [ '*' ] // grant only
378
396
} ;
379
- expect ( function ( ) { ac . grant ( o ) ; } ) . toThrow ( ) ;
380
- expect ( function ( ) { ac . deny ( o ) ; } ) . toThrow ( ) ;
397
+ expect ( ( ) => ac . grant ( o ) ) . toThrow ( ) ;
398
+ expect ( ( ) => ac . deny ( o ) ) . toThrow ( ) ;
381
399
382
400
o = {
383
401
role : 'admin' ,
384
402
resource : 'post' ,
385
403
action : 'create:all' , // invalid possession, should be any|own or omitted
386
404
attributes : [ '*' ] // grant only
387
405
} ;
388
- expect ( function ( ) { ac . grant ( o ) ; } ) . toThrow ( ) ;
389
- expect ( function ( ) { ac . deny ( o ) ; } ) . toThrow ( ) ;
406
+ expect ( ( ) => ac . grant ( o ) ) . toThrow ( ) ;
407
+ expect ( ( ) => ac . deny ( o ) ) . toThrow ( ) ;
390
408
391
409
o = {
392
410
role : 'admin2' ,
393
411
resource : 'post' ,
394
412
action : 'create' , // possession omitted, will be set to any
395
413
attributes : [ '*' ] // grant only
396
414
} ;
397
- expect ( function ( ) { ac . grant ( o ) ; } ) . not . toThrow ( ) ;
415
+ expect ( ( ) => ac . grant ( o ) ) . not . toThrow ( ) ;
398
416
expect ( ac . can ( 'admin2' ) . createAny ( 'post' ) . granted ) . toEqual ( true ) ;
399
417
// possession "any" will also return granted=true for "own"
400
418
expect ( ac . can ( 'admin2' ) . createOwn ( 'post' ) . granted ) . toEqual ( true ) ;
401
- expect ( function ( ) { ac . deny ( o ) ; } ) . not . toThrow ( ) ;
419
+ expect ( ( ) => ac . deny ( o ) ) . not . toThrow ( ) ;
402
420
403
421
} ) ;
404
422
405
423
it ( 'should throw `AccessControlError`' , function ( ) {
406
- var ac = this . ac ;
424
+ let ac = this . ac ;
407
425
function grant ( ) {
408
426
ac . grant ( ) . createOwn ( ) ;
409
427
}
@@ -417,7 +435,7 @@ describe('Test Suite: Access Control', function () {
417
435
} ) ;
418
436
419
437
it ( 'should filter granted attributes' , function ( ) {
420
- var ac = this . ac ,
438
+ let ac = this . ac ,
421
439
attrs = [ '*' , '!account.balance.credit' , '!account.id' , '!secret' ] ,
422
440
data = {
423
441
name : 'Company, LTD.' ,
@@ -438,9 +456,9 @@ describe('Test Suite: Access Control', function () {
438
456
}
439
457
} ;
440
458
ac . grant ( 'user' ) . createOwn ( 'company' , attrs ) ;
441
- var permission = ac . can ( 'user' ) . createOwn ( 'company' ) ;
459
+ let permission = ac . can ( 'user' ) . createOwn ( 'company' ) ;
442
460
expect ( permission . granted ) . toEqual ( true ) ;
443
- var filtered = permission . filter ( data ) ;
461
+ let filtered = permission . filter ( data ) ;
444
462
expect ( filtered . name ) . toEqual ( jasmine . any ( String ) ) ;
445
463
expect ( filtered . address ) . toEqual ( jasmine . any ( Object ) ) ;
446
464
expect ( filtered . address . city ) . toEqual ( 'istanbul' ) ;
@@ -452,7 +470,7 @@ describe('Test Suite: Access Control', function () {
452
470
} ) ;
453
471
454
472
it ( 'Check with multiple roles changes grant list (issue #2)' , function ( ) {
455
- var ac = this . ac ;
473
+ let ac = this . ac ;
456
474
ac . grant ( 'admin' ) . updateAny ( 'video' )
457
475
. grant ( [ 'user' , 'admin' ] ) . updateOwn ( 'video' ) ;
458
476
@@ -472,7 +490,7 @@ describe('Test Suite: Access Control', function () {
472
490
} ) ;
473
491
474
492
it ( 'should grant/deny multiple roles and multiple resources' , function ( ) {
475
- var ac = this . ac ;
493
+ let ac = this . ac ;
476
494
477
495
ac . grant ( 'admin, user' ) . createAny ( 'profile, video' ) ;
478
496
expect ( ac . can ( 'admin' ) . createAny ( 'profile' ) . granted ) . toEqual ( true ) ;
0 commit comments