|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AtlassianConnectBundle\Tests\Listener; |
|
6
|
|
|
|
|
7
|
|
|
use AtlassianConnectBundle\Entity\Tenant; |
|
8
|
|
|
use AtlassianConnectBundle\Listener\LicenseListener; |
|
9
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
10
|
|
|
use PHPUnit\Framework\TestCase; |
|
11
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
12
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
15
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
|
|
|
|
|
|
16
|
|
|
use Symfony\Component\HttpKernel\Event\RequestEvent; |
|
17
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
|
18
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
19
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
20
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
21
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
22
|
|
|
|
|
23
|
|
|
final class LicenseListenerTest extends TestCase |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var MockObject|KernelInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $kernel; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var MockObject|RouterInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $router; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var MockObject|TokenStorageInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
private $tokenStorage; |
|
39
|
|
|
|
|
40
|
|
|
private LicenseListener $listener; |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
protected function setUp(): void |
|
43
|
|
|
{ |
|
44
|
|
|
$this->router = $this->createMock(RouterInterface::class); |
|
45
|
|
|
$this->kernel = $this->createMock(KernelInterface::class); |
|
46
|
|
|
$this->tokenStorage = $this->createMock(TokenStorageInterface::class); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function testItSkipsOnASubRequest(): void |
|
50
|
|
|
{ |
|
51
|
|
|
$attributeParameterBag = $this->createMock(ParameterBagInterface::class); |
|
52
|
|
|
$attributeParameterBag |
|
53
|
|
|
->expects($this->never()) |
|
54
|
|
|
->method('get'); |
|
55
|
|
|
|
|
56
|
|
|
$request = new Request(); |
|
57
|
|
|
$request->attributes = $attributeParameterBag; |
|
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
$event = $this->getEvent( |
|
60
|
|
|
$this->kernel, |
|
61
|
|
|
$request, |
|
62
|
|
|
KernelInterface::SUB_REQUEST |
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
|
|
$this->getLicenseListener()->onKernelRequest($event); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function testItSkipsWhenTheRouteIsNullAndRouteRequiresNoLicense(): void |
|
69
|
|
|
{ |
|
70
|
|
|
$request = new Request( |
|
71
|
|
|
['lic' => 'test'], |
|
72
|
|
|
[], |
|
73
|
|
|
[ |
|
74
|
|
|
'_route' => 'route', |
|
75
|
|
|
'requires_license' => false, |
|
76
|
|
|
] |
|
77
|
|
|
); |
|
78
|
|
|
|
|
79
|
|
|
$event = $this->getEvent( |
|
80
|
|
|
$this->kernel, |
|
81
|
|
|
$request, |
|
82
|
|
|
KernelInterface::MASTER_REQUEST |
|
|
|
|
|
|
83
|
|
|
); |
|
84
|
|
|
|
|
85
|
|
|
$this->getLicenseListener('dev')->onKernelRequest($event); |
|
86
|
|
|
|
|
87
|
|
|
$this->assertNull($event->getResponse()); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function testLicenseIsNotActiveOrDevelopment(): void |
|
91
|
|
|
{ |
|
92
|
|
|
$request1 = new Request( |
|
93
|
|
|
['lic' => 'active'], |
|
94
|
|
|
[], |
|
95
|
|
|
[ |
|
96
|
|
|
'_route' => 'route', |
|
97
|
|
|
'requires_license' => true, |
|
98
|
|
|
] |
|
99
|
|
|
); |
|
100
|
|
|
|
|
101
|
|
|
$request2 = new Request( |
|
102
|
|
|
['lic' => 'notactive'], |
|
103
|
|
|
[], |
|
104
|
|
|
[ |
|
105
|
|
|
'_route' => 'route', |
|
106
|
|
|
'requires_license' => true, |
|
107
|
|
|
] |
|
108
|
|
|
); |
|
109
|
|
|
|
|
110
|
|
|
$this->tokenStorage |
|
111
|
|
|
->expects($this->never()) |
|
112
|
|
|
->method('getToken'); |
|
113
|
|
|
|
|
114
|
|
|
$event1 = $this->getEvent( |
|
115
|
|
|
$this->kernel, |
|
116
|
|
|
$request1, |
|
117
|
|
|
KernelInterface::MASTER_REQUEST |
|
|
|
|
|
|
118
|
|
|
); |
|
119
|
|
|
|
|
120
|
|
|
$event2 = $this->getEvent( |
|
121
|
|
|
$this->kernel, |
|
122
|
|
|
$request2, |
|
123
|
|
|
KernelInterface::MASTER_REQUEST |
|
|
|
|
|
|
124
|
|
|
); |
|
125
|
|
|
|
|
126
|
|
|
$this->getLicenseListener('dev')->onKernelRequest($event1); |
|
127
|
|
|
$this->getLicenseListener('dev')->onKernelRequest($event2); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function testUserIsNoTenant(): void |
|
131
|
|
|
{ |
|
132
|
|
|
$request = new Request( |
|
133
|
|
|
['lic' => 'not_active'], |
|
134
|
|
|
[], |
|
135
|
|
|
[ |
|
136
|
|
|
'_route' => 'route', |
|
137
|
|
|
'requires_license' => true, |
|
138
|
|
|
] |
|
139
|
|
|
); |
|
140
|
|
|
|
|
141
|
|
|
$token = $this->createMock(TokenInterface::class); |
|
142
|
|
|
$token |
|
143
|
|
|
->expects($this->once()) |
|
144
|
|
|
->method('getUser') |
|
145
|
|
|
->willReturn($this->createMock(UserInterface::class)); |
|
146
|
|
|
|
|
147
|
|
|
$this->tokenStorage |
|
148
|
|
|
->expects($this->once()) |
|
149
|
|
|
->method('getToken') |
|
150
|
|
|
->willReturn($token); |
|
151
|
|
|
|
|
152
|
|
|
$this->router |
|
153
|
|
|
->expects($this->once()) |
|
154
|
|
|
->method('generate') |
|
155
|
|
|
->with('atlassian_connect_unlicensed') |
|
156
|
|
|
->willReturn('http://website.com'); |
|
157
|
|
|
|
|
158
|
|
|
$event = $this->getEvent( |
|
159
|
|
|
$this->kernel, |
|
160
|
|
|
$request, |
|
161
|
|
|
KernelInterface::MASTER_REQUEST |
|
|
|
|
|
|
162
|
|
|
); |
|
163
|
|
|
|
|
164
|
|
|
$this->getLicenseListener()->onKernelRequest($event); |
|
165
|
|
|
|
|
166
|
|
|
$response = $event->getResponse(); |
|
167
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $response); |
|
168
|
|
|
$this->assertEquals('http://website.com', $response->getTargetUrl()); |
|
|
|
|
|
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
public function testTenantIsWhiteListed(): void |
|
172
|
|
|
{ |
|
173
|
|
|
$request = new Request( |
|
174
|
|
|
['lic' => 'not_active'], |
|
175
|
|
|
[], |
|
176
|
|
|
[ |
|
177
|
|
|
'_route' => 'route', |
|
178
|
|
|
'requires_license' => true, |
|
179
|
|
|
] |
|
180
|
|
|
); |
|
181
|
|
|
|
|
182
|
|
|
$user = new Tenant(); |
|
183
|
|
|
$user->setIsWhiteListed(true); |
|
184
|
|
|
|
|
185
|
|
|
$token = $this->createMock(TokenInterface::class); |
|
186
|
|
|
$token |
|
187
|
|
|
->expects($this->once()) |
|
188
|
|
|
->method('getUser') |
|
189
|
|
|
->willReturn($user); |
|
190
|
|
|
|
|
191
|
|
|
$this->tokenStorage |
|
192
|
|
|
->expects($this->once()) |
|
193
|
|
|
->method('getToken') |
|
194
|
|
|
->willReturn($token); |
|
195
|
|
|
|
|
196
|
|
|
$event = $this->getEvent( |
|
197
|
|
|
$this->kernel, |
|
198
|
|
|
$request, |
|
199
|
|
|
KernelInterface::MASTER_REQUEST |
|
|
|
|
|
|
200
|
|
|
); |
|
201
|
|
|
|
|
202
|
|
|
$this->getLicenseListener()->onKernelRequest($event); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
public function testIsValidByWhiteList(): void |
|
206
|
|
|
{ |
|
207
|
|
|
$request = new Request( |
|
208
|
|
|
['lic' => 'not_active'], |
|
209
|
|
|
[], |
|
210
|
|
|
[ |
|
211
|
|
|
'_route' => 'route', |
|
212
|
|
|
'requires_license' => true, |
|
213
|
|
|
] |
|
214
|
|
|
); |
|
215
|
|
|
|
|
216
|
|
|
$user = new Tenant(); |
|
217
|
|
|
$user->setClientKey('key'); |
|
218
|
|
|
|
|
219
|
|
|
$token = $this->createMock(TokenInterface::class); |
|
220
|
|
|
$token |
|
221
|
|
|
->expects($this->once()) |
|
222
|
|
|
->method('getUser') |
|
223
|
|
|
->willReturn($user); |
|
224
|
|
|
|
|
225
|
|
|
$this->tokenStorage |
|
226
|
|
|
->expects($this->once()) |
|
227
|
|
|
->method('getToken') |
|
228
|
|
|
->willReturn($token); |
|
229
|
|
|
|
|
230
|
|
|
$date = new \DateTime(); |
|
231
|
|
|
$date->modify('+1 day'); |
|
232
|
|
|
|
|
233
|
|
|
$event = $this->getEvent( |
|
234
|
|
|
$this->kernel, |
|
235
|
|
|
$request, |
|
236
|
|
|
KernelInterface::MASTER_REQUEST |
|
|
|
|
|
|
237
|
|
|
); |
|
238
|
|
|
|
|
239
|
|
|
$this->getLicenseListener('prod', [['valid_till' => $date->format('Y-m-d'), 'client_key' => 'key']])->onKernelRequest($event); |
|
240
|
|
|
$this->assertNull($event->getResponse()); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
public function testWhiteListIsExpired(): void |
|
244
|
|
|
{ |
|
245
|
|
|
$request = new Request( |
|
246
|
|
|
['lic' => 'not_active'], |
|
247
|
|
|
[], |
|
248
|
|
|
[ |
|
249
|
|
|
'_route' => 'route', |
|
250
|
|
|
'requires_license' => true, |
|
251
|
|
|
] |
|
252
|
|
|
); |
|
253
|
|
|
|
|
254
|
|
|
$user = new Tenant(); |
|
255
|
|
|
$user->setClientKey('key'); |
|
256
|
|
|
|
|
257
|
|
|
$token = $this->createMock(TokenInterface::class); |
|
258
|
|
|
$token |
|
259
|
|
|
->expects($this->once()) |
|
260
|
|
|
->method('getUser') |
|
261
|
|
|
->willReturn($user); |
|
262
|
|
|
|
|
263
|
|
|
$this->tokenStorage |
|
264
|
|
|
->expects($this->once()) |
|
265
|
|
|
->method('getToken') |
|
266
|
|
|
->willReturn($token); |
|
267
|
|
|
|
|
268
|
|
|
$date = new \DateTime(); |
|
269
|
|
|
$date->modify('-1 day'); |
|
270
|
|
|
|
|
271
|
|
|
$this->router |
|
272
|
|
|
->expects($this->once()) |
|
273
|
|
|
->method('generate') |
|
274
|
|
|
->with('atlassian_connect_unlicensed') |
|
275
|
|
|
->willReturn('http://website.com'); |
|
276
|
|
|
|
|
277
|
|
|
$event = $this->getEvent( |
|
278
|
|
|
$this->kernel, |
|
279
|
|
|
$request, |
|
280
|
|
|
KernelInterface::MASTER_REQUEST |
|
|
|
|
|
|
281
|
|
|
); |
|
282
|
|
|
|
|
283
|
|
|
$this->getLicenseListener()->onKernelRequest($event); |
|
284
|
|
|
$this->assertNotNull($event->getResponse()); |
|
285
|
|
|
$response = $event->getResponse(); |
|
286
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $response); |
|
287
|
|
|
$this->assertEquals('http://website.com', $response->getTargetUrl()); |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
public function testThrowsException(): void |
|
291
|
|
|
{ |
|
292
|
|
|
$request = new Request( |
|
293
|
|
|
['lic' => 'not_active'], |
|
294
|
|
|
[], |
|
295
|
|
|
[ |
|
296
|
|
|
'_route' => 'route', |
|
297
|
|
|
'requires_license' => true, |
|
298
|
|
|
] |
|
299
|
|
|
); |
|
300
|
|
|
|
|
301
|
|
|
$user = new Tenant(); |
|
302
|
|
|
$user->setClientKey('key'); |
|
303
|
|
|
|
|
304
|
|
|
$this->tokenStorage |
|
305
|
|
|
->expects($this->once()) |
|
306
|
|
|
->method('getToken') |
|
307
|
|
|
->willThrowException(new \Exception()); |
|
308
|
|
|
|
|
309
|
|
|
$this->router |
|
310
|
|
|
->expects($this->once()) |
|
311
|
|
|
->method('generate') |
|
312
|
|
|
->with('atlassian_connect_unlicensed') |
|
313
|
|
|
->willReturn('http://website.com'); |
|
314
|
|
|
|
|
315
|
|
|
$event = $this->getEvent( |
|
316
|
|
|
$this->kernel, |
|
317
|
|
|
$request, |
|
318
|
|
|
KernelInterface::MASTER_REQUEST |
|
|
|
|
|
|
319
|
|
|
); |
|
320
|
|
|
|
|
321
|
|
|
$this->getLicenseListener()->onKernelRequest($event); |
|
322
|
|
|
$this->assertNotNull($event->getResponse()); |
|
323
|
|
|
$response = $event->getResponse(); |
|
324
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $response); |
|
325
|
|
|
$this->assertEquals('http://website.com', $response->getTargetUrl()); |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
private function getEvent(KernelInterface $kernel, Request $request, int $type) |
|
329
|
|
|
{ |
|
330
|
|
|
if (class_exists(RequestEvent::class)) { |
|
331
|
|
|
return new RequestEvent($kernel, $request, $type); |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
return new GetResponseEvent($kernel, $request, $type); |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
private function getLicenseListener(string $environment = 'prod', array $license_allow_list = []) |
|
338
|
|
|
{ |
|
339
|
|
|
return new LicenseListener($this->router, $this->tokenStorage, $environment, $license_allow_list); |
|
340
|
|
|
} |
|
341
|
|
|
} |
|
342
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths