|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests\Unit; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\Query\QueryException; |
|
6
|
|
|
use GuzzleHttp\Psr7\Request; |
|
7
|
|
|
use GuzzleHttp\Psr7\Response; |
|
8
|
|
|
use GuzzleHttp\Psr7\ServerRequest; |
|
9
|
|
|
use Potievdev\SlimRbac\Component\RbacMiddleware; |
|
10
|
|
|
use Potievdev\SlimRbac\Exception\CyclicException; |
|
11
|
|
|
use Potievdev\SlimRbac\Exception\DatabaseException; |
|
12
|
|
|
use Potievdev\SlimRbac\Exception\InvalidArgumentException; |
|
13
|
|
|
use Potievdev\SlimRbac\Exception\NotUniqueException; |
|
14
|
|
|
use Potievdev\SlimRbac\Structure\RbacManagerOptions; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class for testing RbacMiddleware |
|
18
|
|
|
* Class RbacMiddlewareTest |
|
19
|
|
|
* @package Tests\Unit |
|
20
|
|
|
*/ |
|
21
|
|
|
class RbacMiddlewareTest extends BaseTestCase |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var callable $callable */ |
|
24
|
|
|
protected $callable; |
|
25
|
|
|
|
|
26
|
|
|
/** @var ServerRequest $request */ |
|
27
|
|
|
protected $request; |
|
28
|
|
|
|
|
29
|
|
|
/** @var Response $response */ |
|
30
|
|
|
protected $response; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @throws CyclicException |
|
34
|
|
|
* @throws DatabaseException |
|
35
|
|
|
* @throws NotUniqueException |
|
36
|
|
|
* @throws QueryException |
|
37
|
|
|
*/ |
|
38
|
|
|
public function setUp(): void |
|
39
|
|
|
{ |
|
40
|
|
|
parent::setUp(); |
|
41
|
|
|
|
|
42
|
|
|
$this->rbacManager->removeAll(); |
|
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->assign($moderator, self::MODERATOR_USER_ID); |
|
55
|
|
|
$this->rbacManager->assign($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 RbacMiddleware($this->rbacManagerOptions); |
|
71
|
|
|
$request = $this->request->withAttribute($this->rbacManagerOptions->getUserIdFieldName(), self::ADMIN_USER_ID); |
|
72
|
|
|
$response = $middleware->__invoke($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 RbacMiddleware($this->rbacManagerOptions); |
|
83
|
|
|
$request = $this->request->withAttribute($this->rbacManagerOptions->getUserIdFieldName(), self::MODERATOR_USER_ID); |
|
84
|
|
|
$response = $middleware->__invoke($request, $this->response, $this->callable); |
|
85
|
|
|
$this->assertEquals(403, $response->getStatusCode()); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @throws QueryException |
|
90
|
|
|
* @throws InvalidArgumentException |
|
91
|
|
|
*/ |
|
92
|
|
|
public function testCheckReadingUserIdFromHeader() |
|
93
|
|
|
{ |
|
94
|
|
|
$rbacManagerOptions = $this->rbacManagerOptions; |
|
95
|
|
|
$rbacManagerOptions->setUserIdStorageType(RbacManagerOptions::HEADER_STORAGE_TYPE); |
|
96
|
|
|
$middleware = new RbacMiddleware($rbacManagerOptions); |
|
97
|
|
|
$request = $this->request->withHeader($rbacManagerOptions->getUserIdFieldName(), self::ADMIN_USER_ID); |
|
98
|
|
|
$response = $middleware->__invoke($request, $this->response, $this->callable); |
|
99
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @throws QueryException |
|
104
|
|
|
* @throws InvalidArgumentException |
|
105
|
|
|
*/ |
|
106
|
|
|
public function testCheckReadingUserIdFromCookie() |
|
107
|
|
|
{ |
|
108
|
|
|
$rbacManagerOptions = $this->rbacManagerOptions; |
|
109
|
|
|
$rbacManagerOptions->setUserIdStorageType(RbacManagerOptions::COOKIE_STORAGE_TYPE); |
|
110
|
|
|
$middleware = new RbacMiddleware($rbacManagerOptions); |
|
111
|
|
|
$request = $this->request->withCookieParams([$rbacManagerOptions->getUserIdFieldName() => self::ADMIN_USER_ID]); |
|
112
|
|
|
$response = $middleware->__invoke($request, $this->response, $this->callable); |
|
113
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|