|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +/** |
| 5 | + * Copyright 2016 - 2026, Cake Development Corporation (http://cakedc.com) |
| 6 | + * |
| 7 | + * Licensed under The MIT License |
| 8 | + * Redistributions of files must retain the above copyright notice. |
| 9 | + * |
| 10 | + * @copyright Copyright 2016 - 2026, Cake Development Corporation (http://cakedc.com) |
| 11 | + * @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CakeDC\Api\Test\TestCase\Rbac; |
| 15 | + |
| 16 | +use CakeDC\Api\Rbac\ApiRbac; |
| 17 | +use CakeDC\Api\Service\ServiceRegistry; |
| 18 | +use CakeDC\Api\Test\ConfigTrait; |
| 19 | +use CakeDC\Api\TestSuite\TestCase; |
| 20 | + |
| 21 | +/** |
| 22 | + * Unit tests for the RBAC permission matching logic. |
| 23 | + */ |
| 24 | +class ApiRbacTest extends TestCase |
| 25 | +{ |
| 26 | + use ConfigTrait; |
| 27 | + |
| 28 | + /** |
| 29 | + * tearDown |
| 30 | + * |
| 31 | + * @return void |
| 32 | + */ |
| 33 | + public function tearDown(): void |
| 34 | + { |
| 35 | + ServiceRegistry::getServiceLocator()->clear(); |
| 36 | + parent::tearDown(); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Build a service with a resolved action and attach it to the request. |
| 41 | + * |
| 42 | + * @param array $permissions RBAC permissions. |
| 43 | + * @param array $user User data. |
| 44 | + * @return array{rbac: \CakeDC\Api\Rbac\ApiRbac, request: \Cake\Http\ServerRequest, user: array} |
| 45 | + */ |
| 46 | + protected function buildRequest(array $permissions, array $user = ['role' => 1, 'id' => 1]): array |
| 47 | + { |
| 48 | + ServiceRegistry::getServiceLocator()->clear(); |
| 49 | + $this->_initializeRequest([ |
| 50 | + 'params' => ['service' => 'articles'], |
| 51 | + ], 'GET'); |
| 52 | + $service = ServiceRegistry::getServiceLocator()->get('articles', [ |
| 53 | + 'version' => null, |
| 54 | + 'service' => 'articles', |
| 55 | + 'request' => $this->request, |
| 56 | + 'response' => $this->response, |
| 57 | + 'baseUrl' => '/articles', |
| 58 | + ]); |
| 59 | + $service->dispatchPrepareAction(); |
| 60 | + |
| 61 | + $this->request = $this->request->withAttribute('service', $service); |
| 62 | + |
| 63 | + $rbac = new ApiRbac([ |
| 64 | + 'api_permissions' => $permissions, |
| 65 | + 'role_field' => 'role', |
| 66 | + 'default_role' => 'user', |
| 67 | + ]); |
| 68 | + |
| 69 | + return [$rbac, $this->request, $user]; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Apps commonly store the role as an integer group id. The role is passed to |
| 74 | + * the rules (typed `string`), so an integer role must be cast to string and |
| 75 | + * must not raise a TypeError. |
| 76 | + */ |
| 77 | + public function testIntegerRoleIsPassedAsStringToRules(): void |
| 78 | + { |
| 79 | + $rule = new RoleCapturingRule(); |
| 80 | + [$rbac, $request, $user] = $this->buildRequest([ |
| 81 | + ['role' => 1, 'service' => 'articles', 'action' => 'index', 'rule' => $rule], |
| 82 | + ]); |
| 83 | + |
| 84 | + $this->assertTrue($rbac->checkPermissions($user, $request)); |
| 85 | + $this->assertTrue($rule->called); |
| 86 | + $this->assertSame('1', $rule->receivedRole); |
| 87 | + } |
| 88 | +} |
0 commit comments