LicenseListenerTest::testThrowsException()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 24
nc 1
nop 0
dl 0
loc 36
rs 9.536
c 2
b 0
f 0
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\ParameterBag\ParameterBagInterface;
12
use Symfony\Component\HttpFoundation\RedirectResponse;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpKernel\Event\RequestEvent;
15
use Symfony\Component\HttpKernel\KernelInterface;
16
use Symfony\Component\Routing\RouterInterface;
17
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
18
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
19
use Symfony\Component\Security\Core\User\UserInterface;
20
21
final class LicenseListenerTest extends TestCase
22
{
23
    private KernelInterface|MockObject $kernel;
24
    private RouterInterface|MockObject $router;
25
    private TokenStorageInterface|MockObject $tokenStorage;
26
27
    protected function setUp(): void
28
    {
29
        $this->router = $this->createMock(RouterInterface::class);
30
        $this->kernel = $this->createMock(KernelInterface::class);
31
        $this->tokenStorage = $this->createMock(TokenStorageInterface::class);
32
    }
33
34
    public function testItSkipsOnASubRequest(): void
35
    {
36
        $attributeParameterBag = $this->createMock(ParameterBagInterface::class);
37
        $attributeParameterBag
38
            ->expects($this->never())
39
            ->method('get');
40
41
        $request = new Request();
42
        $request->attributes = $attributeParameterBag;
0 ignored issues
show
Documentation Bug introduced by
It seems like $attributeParameterBag of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Symfony\Component\HttpFoundation\ParameterBag of property $attributes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
43
44
        $event = $this->getEvent(
45
            $this->kernel,
46
            $request,
47
            KernelInterface::SUB_REQUEST
48
        );
49
50
        $this->getLicenseListener()->onKernelRequest($event);
51
    }
52
53
    public function testItSkipsWhenTheRouteIsNotNullAndRouteRequiresNoLicense(): void
54
    {
55
        $request = new Request(
56
            ['lic' => 'test'],
57
            [],
58
            [
59
                '_route' => 'route',
60
                'requires_license' => false,
61
            ]
62
        );
63
64
        $event = $this->getEvent(
65
            $this->kernel,
66
            $request,
67
            KernelInterface::MAIN_REQUEST
68
        );
69
70
        $this->getLicenseListener('dev')->onKernelRequest($event);
71
72
        $this->assertNull($event->getResponse());
73
    }
74
75
    public function testItSkipsWhenTheRouteIsNotNullAndRouteHasNoRequiresLicenseAttribute(): void
76
    {
77
        $request = new Request(
78
            ['lic' => 'test'],
79
            [],
80
            [
81
                '_route' => 'route',
82
            ]
83
        );
84
85
        $event = $this->getEvent(
86
            $this->kernel,
87
            $request,
88
            KernelInterface::MASTER_REQUEST
0 ignored issues
show
Deprecated Code introduced by
The constant Symfony\Component\HttpKe...terface::MASTER_REQUEST has been deprecated: since symfony/http-kernel 5.3, use MAIN_REQUEST instead. To ease the migration, this constant won't be removed until Symfony 7.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

88
            /** @scrutinizer ignore-deprecated */ KernelInterface::MASTER_REQUEST

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
89
        );
90
91
        $this->getLicenseListener()->onKernelRequest($event);
92
93
        $this->assertNull($event->getResponse());
94
    }
95
96
    public function testLicenseIsNotActiveOrDevelopment(): void
97
    {
98
        $request1 = new Request(
99
            ['lic' => 'active'],
100
            [],
101
            [
102
                '_route' => 'route',
103
                'requires_license' => true,
104
            ]
105
        );
106
107
        $request2 = new Request(
108
            ['lic' => 'notactive'],
109
            [],
110
            [
111
                '_route' => 'route',
112
                'requires_license' => true,
113
            ]
114
        );
115
116
        $this->tokenStorage
117
            ->expects($this->never())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Symfony\Component\Securi...e\TokenStorageInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

117
            ->/** @scrutinizer ignore-call */ 
118
              expects($this->never())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
118
            ->method('getToken');
119
120
        $event1 = $this->getEvent(
121
            $this->kernel,
122
            $request1,
123
            KernelInterface::MAIN_REQUEST
124
        );
125
126
        $event2 = $this->getEvent(
127
            $this->kernel,
128
            $request2,
129
            KernelInterface::MAIN_REQUEST
130
        );
131
132
        $this->getLicenseListener('dev')->onKernelRequest($event1);
133
        $this->getLicenseListener('dev')->onKernelRequest($event2);
134
    }
135
136
    public function testUserIsNoTenant(): void
137
    {
138
        $request = new Request(
139
            ['lic' => 'not_active'],
140
            [],
141
            [
142
                '_route' => 'route',
143
                'requires_license' => true,
144
            ]
145
        );
146
147
        $token = $this->createMock(TokenInterface::class);
148
        $token
149
            ->expects($this->once())
150
            ->method('getUser')
151
            ->willReturn($this->createMock(UserInterface::class));
152
153
        $this->tokenStorage
154
            ->expects($this->once())
155
            ->method('getToken')
156
            ->willReturn($token);
157
158
        $this->router
159
            ->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Symfony\Component\Routing\RouterInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

159
            ->/** @scrutinizer ignore-call */ 
160
              expects($this->once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
160
            ->method('generate')
161
            ->with('atlassian_connect_unlicensed')
162
            ->willReturn('http://website.com');
163
164
        $event = $this->getEvent(
165
            $this->kernel,
166
            $request,
167
            KernelInterface::MAIN_REQUEST
168
        );
169
170
        $this->getLicenseListener()->onKernelRequest($event);
171
172
        $response = $event->getResponse();
173
        $this->assertInstanceOf(RedirectResponse::class, $response);
174
        $this->assertEquals('http://website.com', $response->getTargetUrl());
0 ignored issues
show
Bug introduced by
The method getTargetUrl() does not exist on Symfony\Component\HttpFoundation\Response. It seems like you code against a sub-type of Symfony\Component\HttpFoundation\Response such as Symfony\Component\HttpFoundation\RedirectResponse. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

174
        $this->assertEquals('http://website.com', $response->/** @scrutinizer ignore-call */ getTargetUrl());
Loading history...
175
    }
176
177
    public function testTenantIsWhiteListed(): void
178
    {
179
        $request = new Request(
180
            ['lic' => 'not_active'],
181
            [],
182
            [
183
                '_route' => 'route',
184
                'requires_license' => true,
185
            ]
186
        );
187
188
        $user = new Tenant();
189
        $user->setIsWhiteListed(true);
190
191
        $token = $this->createMock(TokenInterface::class);
192
        $token
193
            ->expects($this->once())
194
            ->method('getUser')
195
            ->willReturn($user);
196
197
        $this->tokenStorage
198
            ->expects($this->once())
199
            ->method('getToken')
200
            ->willReturn($token);
201
202
        $event = $this->getEvent(
203
            $this->kernel,
204
            $request,
205
            KernelInterface::MAIN_REQUEST
206
        );
207
208
        $this->getLicenseListener()->onKernelRequest($event);
209
    }
210
211
    public function testIsValidByWhiteList(): void
212
    {
213
        $request = new Request(
214
            ['lic' => 'not_active'],
215
            [],
216
            [
217
                '_route' => 'route',
218
                'requires_license' => true,
219
            ]
220
        );
221
222
        $user = new Tenant();
223
        $user->setClientKey('key');
224
225
        $token = $this->createMock(TokenInterface::class);
226
        $token
227
            ->expects($this->once())
228
            ->method('getUser')
229
            ->willReturn($user);
230
231
        $this->tokenStorage
232
            ->expects($this->once())
233
            ->method('getToken')
234
            ->willReturn($token);
235
236
        $date = new \DateTime();
237
        $date->modify('+1 day');
238
239
        $event = $this->getEvent(
240
            $this->kernel,
241
            $request,
242
            KernelInterface::MAIN_REQUEST
243
        );
244
245
        $this->getLicenseListener('prod', [['valid_till' => $date->format('Y-m-d'), 'client_key' => 'key']])->onKernelRequest($event);
246
        $this->assertNull($event->getResponse());
247
    }
248
249
    public function testWhiteListIsExpired(): void
250
    {
251
        $request = new Request(
252
            ['lic' => 'not_active'],
253
            [],
254
            [
255
                '_route' => 'route',
256
                'requires_license' => true,
257
            ]
258
        );
259
260
        $user = new Tenant();
261
        $user->setClientKey('key');
262
263
        $token = $this->createMock(TokenInterface::class);
264
        $token
265
            ->expects($this->once())
266
            ->method('getUser')
267
            ->willReturn($user);
268
269
        $this->tokenStorage
270
            ->expects($this->once())
271
            ->method('getToken')
272
            ->willReturn($token);
273
274
        $date = new \DateTime();
275
        $date->modify('-1 day');
276
277
        $this->router
278
            ->expects($this->once())
279
            ->method('generate')
280
            ->with('atlassian_connect_unlicensed')
281
            ->willReturn('http://website.com');
282
283
        $event = $this->getEvent(
284
            $this->kernel,
285
            $request,
286
            KernelInterface::MAIN_REQUEST
287
        );
288
289
        $this->getLicenseListener()->onKernelRequest($event);
290
        $this->assertNotNull($event->getResponse());
291
        $response = $event->getResponse();
292
        $this->assertInstanceOf(RedirectResponse::class, $response);
293
        $this->assertEquals('http://website.com', $response->getTargetUrl());
294
    }
295
296
    public function testThrowsException(): void
297
    {
298
        $request = new Request(
299
            ['lic' => 'not_active'],
300
            [],
301
            [
302
                '_route' => 'route',
303
                'requires_license' => true,
304
            ]
305
        );
306
307
        $user = new Tenant();
308
        $user->setClientKey('key');
309
310
        $this->tokenStorage
311
            ->expects($this->once())
312
            ->method('getToken')
313
            ->willThrowException(new \Exception());
314
315
        $this->router
316
            ->expects($this->once())
317
            ->method('generate')
318
            ->with('atlassian_connect_unlicensed')
319
            ->willReturn('http://website.com');
320
321
        $event = $this->getEvent(
322
            $this->kernel,
323
            $request,
324
            KernelInterface::MAIN_REQUEST
325
        );
326
327
        $this->getLicenseListener()->onKernelRequest($event);
328
        $this->assertNotNull($event->getResponse());
329
        $response = $event->getResponse();
330
        $this->assertInstanceOf(RedirectResponse::class, $response);
331
        $this->assertEquals('http://website.com', $response->getTargetUrl());
332
    }
333
334
    private function getEvent(KernelInterface $kernel, Request $request, int $type)
335
    {
336
        return new RequestEvent($kernel, $request, $type);
337
    }
338
339
    private function getLicenseListener(string $environment = 'prod', array $license_allow_list = [])
340
    {
341
        return new LicenseListener($this->router, $this->tokenStorage, $environment, $license_allow_list);
342
    }
343
}
344