SimpleRbacAuthorizeTest::providerAuthorize()   B
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 407

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 0
dl 0
loc 407
rs 8
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com)
4
 *
5
 * Licensed under The MIT License
6
 * Redistributions of files must retain the above copyright notice.
7
 *
8
 * @copyright Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com)
9
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
10
 */
11
12
namespace CakeDC\Api\Test\TestCase\Auth\Authorize;
13
14
use CakeDC\Api\Service\Action\Action;
15
use CakeDC\Api\Service\Action\CrudIndexAction;
16
use CakeDC\Api\Service\Auth\Authorize\SimpleRbacAuthorize;
17
use CakeDC\Api\Service\FallbackService;
18
use CakeDC\Api\Service\Service;
19
use CakeDC\Api\TestSuite\TestCase;
20
use CakeDC\Users\Auth\Rules\Rule;
21
use Cake\Http\Response;
22
use Cake\Http\ServerRequest;
23
use ReflectionClass;
24
25
class SimpleRbacAuthorizeTest extends TestCase
26
{
27
28
    /**
29
     * @var Service
30
     */
31
    public $Service;
32
33
    /**
34
     * @var Action
35
     */
36
    public $Action;
37
38
    /**
39
     * @var SimpleRbacAuthorize
40
     */
41
    protected $simpleRbacAuthorize;
42
43
    protected $defaultPermissions = [
44
        [
45
            'role' => 'admin',
46
            'version' => '*',
47
            'service' => '*',
48
            'action' => '*',
49
        ],
50
    ];
51
52
    /**
53
     * Sets up the fixture, for example, opens a network connection.
54
     * This method is called before a test is executed.
55
     */
56
    public function setUp()
57
    {
58
        $request = new ServerRequest();
59
        $response = new Response();
60
61
        $this->Service = new FallbackService([
62
            'request' => $request,
63
            'response' => $response
64
        ]);
65
        $this->Action = new CrudIndexAction([
66
            'service' => $this->Service,
67
        ]);
68
    }
69
70
    /**
71
     * Tears down the fixture, for example, closes a network connection.
72
     * This method is called after a test is executed.
73
     */
74
    public function tearDown()
75
    {
76
        unset($this->simpleRbacAuthorize, $this->Service, $this->Action);
77
    }
78
79
    /**
80
     * @covers CakeDC\Api\Service\Auth\Authorize\SimpleRbacAuthorize::__construct
81
     */
82
    public function testConstruct()
83
    {
84
        //don't autoload config
85
        $this->simpleRbacAuthorize = new SimpleRbacAuthorize($this->Action, ['autoload_config' => false]);
86
        $this->assertEmpty($this->simpleRbacAuthorize->getConfig('permissions'));
87
    }
88
89
    /**
90
     * test
91
     *
92
     * @return void
93
     */
94
    public function testLoadPermissions()
95
    {
96
        $this->simpleRbacAuthorize = $this->getMockBuilder('CakeDC\Api\Service\Auth\Authorize\SimpleRbacAuthorize')
97
                ->disableOriginalConstructor()
98
                ->getMock();
99
        $reflectedClass = new ReflectionClass($this->simpleRbacAuthorize);
100
        $loadPermissions = $reflectedClass->getMethod('_loadPermissions');
101
        $loadPermissions->setAccessible(true);
102
        $permissions = $loadPermissions->invoke($this->simpleRbacAuthorize, 'missing');
103
        $this->assertEquals($this->defaultPermissions, $permissions);
104
    }
105
106
    /**
107
     * @covers CakeDC\Api\Service\Auth\Authorize\SimpleRbacAuthorize::__construct
108
     */
109
    public function testConstructMissingPermissionsFile()
110
    {
111
        $this->simpleRbacAuthorize = $this->getMockBuilder('CakeDC\Api\Service\Auth\Authorize\SimpleRbacAuthorize')
112
            ->setMethods(null)
113
            ->setConstructorArgs([$this->Action, ['autoload_config' => 'does-not-exist']])
114
            ->getMock();
115
        //we should have the default permissions
116
        $this->assertEquals($this->defaultPermissions, $this->simpleRbacAuthorize->getConfig('permissions'));
117
    }
118
119
    protected function assertConstructorPermissions($instance, $config, $permissions)
120
    {
121
        $reflectedClass = new ReflectionClass($instance);
122
        $constructor = $reflectedClass->getConstructor();
123
        $constructor->invoke($this->simpleRbacAuthorize, $this->Action, $config);
124
125
        //we should have the default permissions
126
        $resultPermissions = $this->simpleRbacAuthorize->getConfig('permissions');
127
        $this->assertEquals($permissions, $resultPermissions);
128
    }
129
130
    /**
131
     * @covers CakeDC\Api\Service\Auth\Authorize\SimpleRbacAuthorize::__construct
132
     */
133
    public function testConstructPermissionsFileHappy()
134
    {
135
        $permissions = [[
136
            'service' => 'Test',
137
            'action' => 'test'
138
        ]];
139
        $className = 'CakeDC\Api\Service\Auth\Authorize\SimpleRbacAuthorize';
140
        $this->simpleRbacAuthorize = $this->getMockBuilder($className)
141
                ->setMethods(['_loadPermissions'])
142
                ->disableOriginalConstructor()
143
                ->getMock();
144
        $this->simpleRbacAuthorize
145
                ->expects($this->once())
146
                ->method('_loadPermissions')
147
                ->with('permissions-happy')
148
                ->will($this->returnValue($permissions));
149
        $this->assertConstructorPermissions($className, ['autoload_config' => 'permissions-happy'], $permissions);
150
    }
151
152
    protected function preparePermissions($permissions)
153
    {
154
        $className = 'CakeDC\Api\Service\Auth\Authorize\SimpleRbacAuthorize';
155
        $simpleRbacAuthorize = $this->getMockBuilder($className)
156
                ->setMethods(['_loadPermissions'])
157
                ->disableOriginalConstructor()
158
                ->getMock();
159
        $simpleRbacAuthorize->setConfig('permissions', $permissions);
160
161
        return $simpleRbacAuthorize;
162
    }
163
164
    /**
165
     * @dataProvider providerAuthorize
166
     * @param $permissions
167
     * @param $user
168
     * @param $requestParams
169
     * @param $expected
170
     * @param null $msg
171
     */
172
    public function testAuthorize($permissions, $user, $requestParams, $expected, $msg = null)
173
    {
174
        $this->simpleRbacAuthorize = $this->preparePermissions($permissions);
175
        $request = new ServerRequest();
0 ignored issues
show
Unused Code introduced by
$request is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
176
177
        $request = new ServerRequest();
178
        $response = new Response();
179
        $service = new FallbackService([
180
            'request' => $request,
181
            'response' => $response,
182
            'service' => $requestParams['service'],
183
        ]);
184
        $action = new CrudIndexAction([
185
            'service' => $service,
186
            'name' => $requestParams['action'],
187
        ]);
188
189
        $this->simpleRbacAuthorize->setAction($action);
190
191
        $result = $this->simpleRbacAuthorize->authorize($user, $request);
192
        $this->assertSame($expected, $result, $msg);
193
    }
194
195
    public function providerAuthorize()
196
    {
197
        $trueRuleMock = $this->getMockBuilder(Rule::class)
198
            ->setMethods(['allowed'])
199
            ->getMock();
200
        $trueRuleMock->expects($this->any())
201
            ->method('allowed')
202
            ->willReturn(true);
203
204
        return [
205
            'happy-strict-all' => [
206
                //permissions
207
                [[
208
                    'role' => 'test',
209
                    'service' => 'tests',
210
                    'action' => 'test',
211
                    'allowed' => true,
212
                ]],
213
                //user
214
                [
215
                    'id' => 1,
216
                    'username' => 'luke',
217
                    'role' => 'test',
218
                ],
219
                //request
220
                [
221
                    'service' => 'tests',
222
                    'action' => 'test'
223
                ],
224
                //expected
225
                true
226
            ],
227
            'happy-strict-all-deny' => [
228
                //permissions
229
                [[
230
                    'role' => 'test',
231
                    'service' => 'tests',
232
                    'action' => 'test',
233
                    'allowed' => false,
234
                ]],
235
                //user
236
                [
237
                    'id' => 1,
238
                    'username' => 'luke',
239
                    'role' => 'test',
240
                ],
241
                //request
242
                [
243
                    'service' => 'tests',
244
                    'action' => 'test'
245
                ],
246
                //expected
247
                false
248
            ],
249
            'happy-pl-null-allowed-null' => [
250
                //permissions
251
                [[
252
                    'role' => 'test',
253
                    'service' => 'tests',
254
                    'action' => 'test',
255
                ]],
256
                //user
257
                [
258
                    'id' => 1,
259
                    'username' => 'luke',
260
                    'role' => 'test',
261
                ],
262
                //request
263
                [
264
                    'service' => 'tests',
265
                    'action' => 'test'
266
                ],
267
                //expected
268
                true
269
            ],
270
            'happy-asterisk' => [
271
                //permissions
272
                [[
273
                    'role' => 'test',
274
                    'service' => 'tests',
275
                    'action' => 'test',
276
                ]],
277
                //user
278
                [
279
                    'id' => 1,
280
                    'username' => 'luke',
281
                    'role' => 'test',
282
                ],
283
                //request
284
                [
285
                    'service' => 'tests',
286
                    'action' => 'test'
287
                ],
288
                //expected
289
                true
290
            ],
291
            'happy-asterisk-main-app' => [
292
                //permissions
293
                [[
294
                    'role' => 'test',
295
                    'service' => 'tests',
296
                    'action' => 'test',
297
                ]],
298
                //user
299
                [
300
                    'id' => 1,
301
                    'username' => 'luke',
302
                    'role' => 'test',
303
                ],
304
                //request
305
                [
306
                    'service' => 'tests',
307
                    'action' => 'test'
308
                ],
309
                //expected
310
                true
311
            ],
312
            'happy-role-asterisk' => [
313
                //permissions
314
                [[
315
                    'role' => '*',
316
                    'service' => 'tests',
317
                    'action' => 'test',
318
                ]],
319
                //user
320
                [
321
                    'id' => 1,
322
                    'username' => 'luke',
323
                    'role' => 'any-role',
324
                ],
325
                //request
326
                [
327
                    'service' => 'tests',
328
                    'action' => 'test'
329
                ],
330
                //expected
331
                true
332
            ],
333
            'happy-service-asterisk' => [
334
                //permissions
335
                [[
336
                    'role' => 'test',
337
                    'service' => '*',
338
                    'action' => 'test',
339
                ]],
340
                //user
341
                [
342
                    'id' => 1,
343
                    'username' => 'luke',
344
                    'role' => 'test',
345
                ],
346
                //request
347
                [
348
                    'service' => 'tests',
349
                    'action' => 'test'
350
                ],
351
                //expected
352
                true
353
            ],
354
            'happy-action-asterisk' => [
355
                //permissions
356
                [[
357
                    'role' => 'test',
358
                    'service' => 'tests',
359
                    'action' => '*',
360
                ]],
361
                //user
362
                [
363
                    'id' => 1,
364
                    'username' => 'luke',
365
                    'role' => 'test',
366
                ],
367
                //request
368
                [
369
                    'service' => 'tests',
370
                    'action' => 'any'
371
                ],
372
                //expected
373
                true
374
            ],
375
            'happy-some-asterisk-allowed' => [
376
                //permissions
377
                [[
378
                    'role' => 'test',
379
                    'service' => '*',
380
                    'action' => '*',
381
                ]],
382
                //user
383
                [
384
                    'id' => 1,
385
                    'username' => 'luke',
386
                    'role' => 'test',
387
                ],
388
                //request
389
                [
390
                    'service' => 'tests',
391
                    'action' => 'any'
392
                ],
393
                //expected
394
                true
395
            ],
396
            'happy-some-asterisk-deny' => [
397
                //permissions
398
                [[
399
                    'role' => 'test',
400
                    'service' => '*',
401
                    'action' => '*',
402
                    'allowed' => false,
403
                ]],
404
                //user
405
                [
406
                    'id' => 1,
407
                    'username' => 'luke',
408
                    'role' => 'test',
409
                ],
410
                //request
411
                [
412
                    'service' => 'tests',
413
                    'action' => 'any'
414
                ],
415
                //expected
416
                false
417
            ],
418
            'all-deny' => [
419
                //permissions
420
                [[
421
                    'role' => '*',
422
                    'service' => '*',
423
                    'action' => '*',
424
                    'allowed' => false,
425
                ]],
426
                //user
427
                [
428
                    'id' => 1,
429
                    'username' => 'luke',
430
                    'role' => 'test',
431
                ],
432
                //request
433
                [
434
                    'service' => 'Any',
435
                    'action' => 'any'
436
                ],
437
                //expected
438
                false
439
            ],
440
            'dasherized' => [
441
                //permissions
442
                [[
443
                    'role' => 'test',
444
                    'service' => 'TestTests',
445
                    'action' => 'TestAction',
446
                    'allowed' => true,
447
                ]],
448
                //user
449
                [
450
                    'id' => 1,
451
                    'username' => 'luke',
452
                    'role' => 'test',
453
                ],
454
                //request
455
                [
456
                    'service' => 'test-tests',
457
                    'action' => 'test-action'
458
                ],
459
                //expected
460
                true
461
            ],
462
            'happy-array' => [
463
                //permissions
464
                [[
465
                    'role' => ['test'],
466
                    'service' => ['tests'],
467
                    'action' => ['one', 'two'],
468
                ]],
469
                //user
470
                [
471
                    'id' => 1,
472
                    'username' => 'luke',
473
                    'role' => 'test',
474
                ],
475
                //request
476
                [
477
                    'service' => 'tests',
478
                    'action' => 'one'
479
                ],
480
                //expected
481
                true
482
            ],
483
            'happy-array' => [
484
                //permissions
485
                [[
486
                    'role' => ['test'],
487
                    'service' => ['tests'],
488
                    'action' => ['one', 'two'],
489
                ]],
490
                //user
491
                [
492
                    'id' => 1,
493
                    'username' => 'luke',
494
                    'role' => 'test',
495
                ],
496
                //request
497
                [
498
                    'service' => 'tests',
499
                    'action' => 'three'
500
                ],
501
                //expected
502
                false
503
            ],
504
            'happy-callback-check-params' => [
505
                //permissions
506
                [[
507
                    'role' => ['test'],
508
                    'service' => ['tests'],
509
                    'action' => ['one', 'two'],
510
                    'allowed' => function ($user, $role, $request) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
511
                        return $user['id'] === 1 && $role == 'test';
512
                    }
513
                ]],
514
                //user
515
                [
516
                    'id' => 1,
517
                    'username' => 'luke',
518
                    'role' => 'test',
519
                ],
520
                //request
521
                [
522
                    'service' => 'tests',
523
                    'action' => 'one'
524
                ],
525
                //expected
526
                true
527
            ],
528
            'happy-callback-deny' => [
529
                //permissions
530
                [[
531
                    'role' => ['test'],
532
                    'service' => ['tests'],
533
                    'action' => ['one', 'two'],
534
                    'allowed' => function ($user, $role, $request) {
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $role is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
535
                        return false;
536
                    }
537
                ]],
538
                //user
539
                [
540
                    'id' => 1,
541
                    'username' => 'luke',
542
                    'role' => 'test',
543
                ],
544
                //request
545
                [
546
                    'service' => 'tests',
547
                    'action' => 'one'
548
                ],
549
                //expected
550
                false
551
            ],
552
            'happy-prefix' => [
553
                //permissions
554
                [[
555
                    'role' => ['test'],
556
                    'prefix' => ['admin'],
557
                    'service' => ['tests'],
558
                    'action' => ['one', 'two'],
559
                ]],
560
                //user
561
                [
562
                    'id' => 1,
563
                    'username' => 'luke',
564
                    'role' => 'test',
565
                ],
566
                //request
567
                [
568
                    'prefix' => 'admin',
569
                    'service' => 'tests',
570
                    'action' => 'one'
571
                ],
572
                //expected
573
                true
574
            ],
575
576
            'rule-class' => [
577
                //permissions
578
                [
579
                    [
580
                        'role' => ['test'],
581
                        'service' => '*',
582
                        'action' => 'one',
583
                        'allowed' => $trueRuleMock,
584
                    ],
585
                ],
586
                //user
587
                [
588
                    'id' => 1,
589
                    'username' => 'luke',
590
                    'role' => 'test',
591
                ],
592
                //request
593
                [
594
                    'service' => 'tests',
595
                    'action' => 'one'
596
                ],
597
                //expected
598
                true
599
            ],
600
        ];
601
    }
602
}
603