1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace DataFlow\Tests\Unit\Authentication\Middleware; |
5
|
|
|
|
6
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
7
|
|
|
use Doctrine\Common\Collections\Selectable; |
8
|
|
|
use Prophecy\Argument; |
9
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
10
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
11
|
|
|
use SlayerBirden\DataFlowServer\Authentication\Entities\Grant; |
12
|
|
|
use SlayerBirden\DataFlowServer\Authentication\Entities\Token; |
13
|
|
|
use SlayerBirden\DataFlowServer\Authentication\Middleware\TokenMiddleware; |
14
|
|
|
use SlayerBirden\DataFlowServer\Domain\Entities\User; |
15
|
|
|
use SlayerBirden\DataFlowServer\Notification\MessageInterface; |
16
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
17
|
|
|
use Zend\Diactoros\ServerRequest; |
18
|
|
|
use Zend\Expressive\Router\RouteResult; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @codingStandardsIgnoreFile |
22
|
|
|
*/ |
23
|
|
|
class TokenMiddlewareTest extends \Codeception\Test\Unit |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var TokenMiddleware |
27
|
|
|
*/ |
28
|
|
|
private $tokenMiddleware; |
29
|
|
|
/** |
30
|
|
|
* @var ObjectProphecy |
31
|
|
|
*/ |
32
|
|
|
private $requestHandler; |
33
|
|
|
/** |
34
|
|
|
* @var ObjectProphecy |
35
|
|
|
*/ |
36
|
|
|
private $tokenRepository; |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
protected function setUp() |
40
|
|
|
{ |
41
|
|
|
$this->tokenRepository = $this->prophesize(Selectable::class); |
42
|
|
|
$this->tokenMiddleware = new TokenMiddleware($this->tokenRepository->reveal()); |
43
|
|
|
$this->requestHandler = $this->prophesize(RequestHandlerInterface::class); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function test_Success_Proxy() |
47
|
|
|
{ |
48
|
|
|
$request = new ServerRequest(); |
49
|
|
|
|
50
|
|
|
$token = new Token(); |
51
|
|
|
$token->setActive(true); |
52
|
|
|
$token->setDue(new \DateTime('+1 day')); |
53
|
|
|
$grant = new Grant(); |
54
|
|
|
$grant->setResource('test'); |
55
|
|
|
$token->setGrants(new ArrayCollection([$grant])); |
56
|
|
|
$token->setOwner(new User()); |
57
|
|
|
|
58
|
|
|
$collection = $this->prophesize(\Doctrine\Common\Collections\Collection::class); |
59
|
|
|
$this->tokenRepository->matching(Argument::any())->willReturn($collection->reveal()); |
60
|
|
|
$collection->count()->willReturn(1); |
61
|
|
|
$collection->first()->willReturn($token); |
62
|
|
|
|
63
|
|
|
$this->requestHandler->handle(Argument::type(ServerRequest::class))->shouldBeCalled(); |
64
|
|
|
|
65
|
|
|
$routeResult = $this->prophesize(RouteResult::class); |
66
|
|
|
$routeResult->getMatchedRouteName()->willReturn('test'); |
67
|
|
|
|
68
|
|
|
$this->tokenMiddleware->process( |
69
|
|
|
$request->withHeader('Authorization', 'Bearer XXX') |
70
|
|
|
->withAttribute(RouteResult::class, $routeResult->reveal()), |
71
|
|
|
$this->requestHandler->reveal() |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function test_Success_Proxy_No_ACL() |
76
|
|
|
{ |
77
|
|
|
$request = new ServerRequest(); |
78
|
|
|
|
79
|
|
|
$token = new Token(); |
80
|
|
|
$token->setActive(true); |
81
|
|
|
$token->setDue(new \DateTime('+1 day')); |
82
|
|
|
|
83
|
|
|
$collection = $this->prophesize(\Doctrine\Common\Collections\Collection::class); |
84
|
|
|
$this->tokenRepository->matching(Argument::any())->willReturn($collection->reveal()); |
85
|
|
|
$collection->count()->willReturn(1); |
86
|
|
|
$collection->first()->willReturn($token); |
87
|
|
|
|
88
|
|
|
$this->requestHandler->handle(Argument::type(ServerRequest::class))->shouldBeCalled(); |
89
|
|
|
|
90
|
|
|
$this->tokenMiddleware->process( |
91
|
|
|
$request->withHeader('Authorization', 'Bearer XXX'), |
92
|
|
|
$this->requestHandler->reveal() |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function test_Empty_Authorization_Header() |
97
|
|
|
{ |
98
|
|
|
$request = new ServerRequest(); |
99
|
|
|
|
100
|
|
|
/** @var JsonResponse $response */ |
101
|
|
|
$response = $this->tokenMiddleware->process( |
102
|
|
|
$request, |
103
|
|
|
$this->requestHandler->reveal() |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
/** @var MessageInterface $message */ |
107
|
|
|
$message = $response->getPayload()['msg']; |
108
|
|
|
$this->assertSame(401, $response->getStatusCode()); |
109
|
|
|
$this->assertSame('Empty Authorization header. Access denied.', $message->getMessage()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function test_Can_Not_Find_Token() |
113
|
|
|
{ |
114
|
|
|
$request = new ServerRequest(); |
115
|
|
|
|
116
|
|
|
$token = new Token(); |
117
|
|
|
$token->setActive(true); |
118
|
|
|
$token->setDue(new \DateTime('+1 day')); |
119
|
|
|
|
120
|
|
|
$collection = $this->prophesize(\Doctrine\Common\Collections\Collection::class); |
121
|
|
|
$this->tokenRepository->matching(Argument::any())->willReturn($collection->reveal()); |
122
|
|
|
$collection->count()->willReturn(0); |
123
|
|
|
|
124
|
|
|
/** @var JsonResponse $response */ |
125
|
|
|
$response = $this->tokenMiddleware->process( |
126
|
|
|
$request->withHeader('Authorization', 'Bearer XXX'), |
127
|
|
|
$this->requestHandler->reveal() |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
/** @var MessageInterface $message */ |
131
|
|
|
$message = $response->getPayload()['msg']; |
132
|
|
|
$this->assertSame(401, $response->getStatusCode()); |
133
|
|
|
$this->assertSame('Token is absent or invalid. Access denied.', $message->getMessage()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function test_Token_Inactive() |
137
|
|
|
{ |
138
|
|
|
$request = new ServerRequest(); |
139
|
|
|
|
140
|
|
|
$token = new Token(); |
141
|
|
|
$token->setActive(false); |
142
|
|
|
$token->setDue(new \DateTime('+1 day')); |
143
|
|
|
|
144
|
|
|
$collection = $this->prophesize(\Doctrine\Common\Collections\Collection::class); |
145
|
|
|
$this->tokenRepository->matching(Argument::any())->willReturn($collection->reveal()); |
146
|
|
|
$collection->count()->willReturn(1); |
147
|
|
|
$collection->first()->willReturn($token); |
148
|
|
|
|
149
|
|
|
/** @var JsonResponse $response */ |
150
|
|
|
$response = $this->tokenMiddleware->process( |
151
|
|
|
$request->withHeader('Authorization', 'Bearer XXX'), |
152
|
|
|
$this->requestHandler->reveal() |
153
|
|
|
); |
154
|
|
|
|
155
|
|
|
/** @var MessageInterface $message */ |
156
|
|
|
$message = $response->getPayload()['msg']; |
157
|
|
|
$this->assertSame(401, $response->getStatusCode()); |
158
|
|
|
$this->assertSame('Token is absent or invalid. Access denied.', $message->getMessage()); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function test_Token_Expired() |
162
|
|
|
{ |
163
|
|
|
$request = new ServerRequest(); |
164
|
|
|
|
165
|
|
|
$token = new Token(); |
166
|
|
|
$token->setActive(true); |
167
|
|
|
$token->setDue(new \DateTime('-1 day')); |
168
|
|
|
|
169
|
|
|
$collection = $this->prophesize(\Doctrine\Common\Collections\Collection::class); |
170
|
|
|
$this->tokenRepository->matching(Argument::any())->willReturn($collection->reveal()); |
171
|
|
|
$collection->count()->willReturn(1); |
172
|
|
|
$collection->first()->willReturn($token); |
173
|
|
|
|
174
|
|
|
/** @var JsonResponse $response */ |
175
|
|
|
$response = $this->tokenMiddleware->process( |
176
|
|
|
$request->withHeader('Authorization', 'Bearer XXX'), |
177
|
|
|
$this->requestHandler->reveal() |
178
|
|
|
); |
179
|
|
|
|
180
|
|
|
/** @var MessageInterface $message */ |
181
|
|
|
$message = $response->getPayload()['msg']; |
182
|
|
|
$this->assertSame(401, $response->getStatusCode()); |
183
|
|
|
$this->assertSame('Token is absent or invalid. Access denied.', $message->getMessage()); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function test_Access_Not_Granted() |
187
|
|
|
{ |
188
|
|
|
$request = new ServerRequest(); |
189
|
|
|
|
190
|
|
|
$token = new Token(); |
191
|
|
|
$token->setActive(true); |
192
|
|
|
$token->setDue(new \DateTime('+1 day')); |
193
|
|
|
$grant = new Grant(); |
194
|
|
|
$grant->setResource('test2'); |
195
|
|
|
$token->setGrants(new ArrayCollection([$grant])); |
196
|
|
|
|
197
|
|
|
$collection = $this->prophesize(\Doctrine\Common\Collections\Collection::class); |
198
|
|
|
$this->tokenRepository->matching(Argument::any())->willReturn($collection->reveal()); |
199
|
|
|
$collection->count()->willReturn(1); |
200
|
|
|
$collection->first()->willReturn($token); |
201
|
|
|
|
202
|
|
|
$routeResult = $this->prophesize(RouteResult::class); |
203
|
|
|
$routeResult->getMatchedRouteName()->willReturn('test'); |
204
|
|
|
|
205
|
|
|
/** @var JsonResponse $response */ |
206
|
|
|
$response = $this->tokenMiddleware->process( |
207
|
|
|
$request->withHeader('Authorization', 'Bearer XXX') |
208
|
|
|
->withAttribute(RouteResult::class, $routeResult->reveal()), |
209
|
|
|
$this->requestHandler->reveal() |
210
|
|
|
); |
211
|
|
|
|
212
|
|
|
/** @var MessageInterface $message */ |
213
|
|
|
$message = $response->getPayload()['msg']; |
214
|
|
|
$this->assertSame(403, $response->getStatusCode()); |
215
|
|
|
$this->assertSame('The permission to resource is not granted.', $message->getMessage()); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|