1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Unit; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\ORMException; |
6
|
|
|
use Doctrine\ORM\Query\QueryException; |
7
|
|
|
use GuzzleHttp\Psr7\Request; |
8
|
|
|
use GuzzleHttp\Psr7\Response; |
9
|
|
|
use GuzzleHttp\Psr7\ServerRequest; |
10
|
|
|
use Potievdev\SlimRbac\Component\Config\RbacConfig; |
11
|
|
|
use Potievdev\SlimRbac\Component\RbacContainer; |
12
|
|
|
use Potievdev\SlimRbac\Exception\ConfigNotFoundException; |
13
|
|
|
use Potievdev\SlimRbac\Exception\CyclicException; |
14
|
|
|
use Potievdev\SlimRbac\Exception\DatabaseException; |
15
|
|
|
use Potievdev\SlimRbac\Exception\InvalidArgumentException; |
16
|
|
|
use Potievdev\SlimRbac\Exception\NotUniqueException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class for testing RbacMiddleware |
20
|
|
|
* Class RbacMiddlewareTest |
21
|
|
|
* @package Tests\Unit |
22
|
|
|
*/ |
23
|
|
|
class RbacMiddlewareTest extends BaseTestCase |
24
|
|
|
{ |
25
|
|
|
/** @var callable $callable */ |
26
|
|
|
protected $callable; |
27
|
|
|
|
28
|
|
|
/** @var ServerRequest $request */ |
29
|
|
|
protected $request; |
30
|
|
|
|
31
|
|
|
/** @var Response $response */ |
32
|
|
|
protected $response; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @throws CyclicException |
36
|
|
|
* @throws DatabaseException |
37
|
|
|
* @throws NotUniqueException |
38
|
|
|
* @throws QueryException|ORMException |
39
|
|
|
*/ |
40
|
|
|
public function setUp(): void |
41
|
|
|
{ |
42
|
|
|
parent::setUp(); |
43
|
|
|
|
44
|
|
|
$edit = $this->rbacManager->createPermission('edit', 'Edit permission'); |
45
|
|
|
$write = $this->rbacManager->createPermission('write', 'Write permission'); |
46
|
|
|
|
47
|
|
|
$moderator = $this->rbacManager->createRole('moderator', 'Moderator role'); |
48
|
|
|
$admin = $this->rbacManager->createRole('admin', 'Admin role'); |
49
|
|
|
|
50
|
|
|
$this->rbacManager->attachPermission($moderator, $edit); |
51
|
|
|
$this->rbacManager->attachPermission($admin, $write); |
52
|
|
|
$this->rbacManager->attachChildRole($admin, $moderator); |
53
|
|
|
|
54
|
|
|
$this->rbacManager->assignRoleToUser($moderator, self::MODERATOR_USER_ID); |
55
|
|
|
$this->rbacManager->assignRoleToUser($admin, self::ADMIN_USER_ID); |
56
|
|
|
|
57
|
|
|
$this->callable = function (Request $request, Response $response) { |
58
|
|
|
return $response; |
59
|
|
|
}; |
60
|
|
|
$this->request = new ServerRequest('GET', 'write'); |
61
|
|
|
$this->response = new Response(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @throws QueryException |
66
|
|
|
* @throws InvalidArgumentException |
67
|
|
|
*/ |
68
|
|
|
public function testCheckAccessSuccessCase() |
69
|
|
|
{ |
70
|
|
|
$middleware = (new RbacContainer())->getRbacMiddleware(); |
71
|
|
|
$request = $this->request->withAttribute('userId', self::ADMIN_USER_ID); |
72
|
|
|
$response = $middleware($request, $this->response, $this->callable); |
73
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @throws QueryException |
78
|
|
|
* @throws InvalidArgumentException |
79
|
|
|
*/ |
80
|
|
|
public function testCheckAccessDeniedCase() |
81
|
|
|
{ |
82
|
|
|
$middleware = (new RbacContainer())->getRbacMiddleware(); |
83
|
|
|
$request = $this->request->withAttribute('userId', self::MODERATOR_USER_ID); |
84
|
|
|
$response = $middleware($request, $this->response, $this->callable); |
85
|
|
|
$this->assertEquals(403, $response->getStatusCode()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @throws QueryException |
90
|
|
|
* @throws InvalidArgumentException |
91
|
|
|
* @throws ConfigNotFoundException |
92
|
|
|
*/ |
93
|
|
|
public function testCheckReadingUserIdFromHeader() |
94
|
|
|
{ |
95
|
|
|
$middleware = (new RbacContainer($this->createRbacConfig(RbacConfig::HEADER_RESOURCE_TYPE))) |
96
|
|
|
->getRbacMiddleware(); |
97
|
|
|
$request = $this->request->withHeader('userId', self::ADMIN_USER_ID); |
98
|
|
|
$response = $middleware($request, $this->response, $this->callable); |
99
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @throws QueryException |
104
|
|
|
* @throws InvalidArgumentException |
105
|
|
|
* @throws ConfigNotFoundException |
106
|
|
|
*/ |
107
|
|
|
public function testCheckReadingUserIdFromCookie() |
108
|
|
|
{ |
109
|
|
|
$middleware = (new RbacContainer($this->createRbacConfig(RbacConfig::COOKIE_RESOURCE_TYPE))) |
110
|
|
|
->getRbacMiddleware(); |
111
|
|
|
$request = $this->request->withCookieParams(['userId' => self::ADMIN_USER_ID]); |
112
|
|
|
$response = $middleware($request, $this->response, $this->callable); |
113
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @throws ConfigNotFoundException |
118
|
|
|
*/ |
119
|
|
|
private function createRbacConfig(?string $resourceTypeId): RbacConfig |
120
|
|
|
{ |
121
|
|
|
$rbacConfig = RbacConfig::createFromConfigFile(); |
122
|
|
|
|
123
|
|
|
return new RbacConfig( |
124
|
|
|
$rbacConfig->getDatabaseDriver(), |
125
|
|
|
$rbacConfig->getDatabaseHost(), |
126
|
|
|
$rbacConfig->getDatabaseUser(), |
127
|
|
|
$rbacConfig->getDatabasePassword(), |
128
|
|
|
$rbacConfig->getDatabasePort(), |
129
|
|
|
$rbacConfig->getDatabaseName(), |
130
|
|
|
$rbacConfig->getDatabaseCharset(), |
131
|
|
|
$rbacConfig->getUserIdFieldName(), |
132
|
|
|
$resourceTypeId ?? $rbacConfig->getUserIdResourceType() |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
} |
137
|
|
|
|