Passed
Push — master ( a52600...776408 )
by Matthieu
33:31 queued 28:50
created

LicenseListenerTest::testIsValidByWhiteList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 23
nc 1
nop 0
dl 0
loc 36
rs 9.552
c 1
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\GetResponseEvent;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpKe...\Event\GetResponseEvent was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Symfony\Component\HttpKernel\Event\RequestEvent;
16
use Symfony\Component\HttpKernel\KernelInterface;
17
use Symfony\Component\Routing\RouterInterface;
18
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
19
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
20
use Symfony\Component\Security\Core\User\UserInterface;
21
22
final class LicenseListenerTest extends TestCase
23
{
24
    /**
25
     * @var MockObject|KernelInterface
26
     */
27
    private $kernel;
28
29
    /**
30
     * @var MockObject|RouterInterface
31
     */
32
    private $router;
33
34
    /**
35
     * @var MockObject|TokenStorageInterface
36
     */
37
    private $tokenStorage;
38
39
    protected function setUp(): void
40
    {
41
        $this->router = $this->createMock(RouterInterface::class);
42
        $this->kernel = $this->createMock(KernelInterface::class);
43
        $this->tokenStorage = $this->createMock(TokenStorageInterface::class);
44
    }
45
46
    public function testItSkipsOnASubRequest(): void
47
    {
48
        $attributeParameterBag = $this->createMock(ParameterBagInterface::class);
49
        $attributeParameterBag
50
            ->expects($this->never())
51
            ->method('get');
52
53
        $request = new Request();
54
        $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...
55
56
        $event = $this->getEvent(
57
            $this->kernel,
58
            $request,
59
            KernelInterface::SUB_REQUEST
60
        );
61
62
        $this->getLicenseListener()->onKernelRequest($event);
63
    }
64
65
    public function testItSkipsWhenTheRouteIsNotNullAndRouteRequiresNoLicense(): void
66
    {
67
        $request = new Request(
68
            ['lic' => 'test'],
69
            [],
70
            [
71
                '_route' => 'route',
72
                'requires_license' => false,
73
            ]
74
        );
75
76
        $event = $this->getEvent(
77
            $this->kernel,
78
            $request,
79
            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

79
            /** @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...
80
        );
81
82
        $this->getLicenseListener('dev')->onKernelRequest($event);
83
84
        $this->assertNull($event->getResponse());
85
    }
86
87
    public function testItSkipsWhenTheRouteIsNotNullAndRouteHasNoRequiresLicenseAttribute(): void
88
    {
89
        $request = new Request(
90
            ['lic' => 'test'],
91
            [],
92
            [
93
                '_route' => 'route',
94
            ]
95
        );
96
97
        $event = $this->getEvent(
98
            $this->kernel,
99
            $request,
100
            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

100
            /** @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...
101
        );
102
103
        $this->getLicenseListener()->onKernelRequest($event);
104
105
        $this->assertNull($event->getResponse());
106
    }
107
108
    public function testLicenseIsNotActiveOrDevelopment(): void
109
    {
110
        $request1 = new Request(
111
            ['lic' => 'active'],
112
            [],
113
            [
114
                '_route' => 'route',
115
                'requires_license' => true,
116
            ]
117
        );
118
119
        $request2 = new Request(
120
            ['lic' => 'notactive'],
121
            [],
122
            [
123
                '_route' => 'route',
124
                'requires_license' => true,
125
            ]
126
        );
127
128
        $this->tokenStorage
129
            ->expects($this->never())
130
            ->method('getToken');
131
132
        $event1 = $this->getEvent(
133
            $this->kernel,
134
            $request1,
135
            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

135
            /** @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...
136
        );
137
138
        $event2 = $this->getEvent(
139
            $this->kernel,
140
            $request2,
141
            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

141
            /** @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...
142
        );
143
144
        $this->getLicenseListener('dev')->onKernelRequest($event1);
145
        $this->getLicenseListener('dev')->onKernelRequest($event2);
146
    }
147
148
    public function testUserIsNoTenant(): void
149
    {
150
        $request = new Request(
151
            ['lic' => 'not_active'],
152
            [],
153
            [
154
                '_route' => 'route',
155
                'requires_license' => true,
156
            ]
157
        );
158
159
        $token = $this->createMock(TokenInterface::class);
160
        $token
161
            ->expects($this->once())
162
            ->method('getUser')
163
            ->willReturn($this->createMock(UserInterface::class));
164
165
        $this->tokenStorage
166
            ->expects($this->once())
167
            ->method('getToken')
168
            ->willReturn($token);
169
170
        $this->router
171
            ->expects($this->once())
172
            ->method('generate')
173
            ->with('atlassian_connect_unlicensed')
174
            ->willReturn('http://website.com');
175
176
        $event = $this->getEvent(
177
            $this->kernel,
178
            $request,
179
            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

179
            /** @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...
180
        );
181
182
        $this->getLicenseListener()->onKernelRequest($event);
183
184
        $response = $event->getResponse();
185
        $this->assertInstanceOf(RedirectResponse::class, $response);
186
        $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

186
        $this->assertEquals('http://website.com', $response->/** @scrutinizer ignore-call */ getTargetUrl());
Loading history...
187
    }
188
189
    public function testTenantIsWhiteListed(): void
190
    {
191
        $request = new Request(
192
            ['lic' => 'not_active'],
193
            [],
194
            [
195
                '_route' => 'route',
196
                'requires_license' => true,
197
            ]
198
        );
199
200
        $user = new Tenant();
201
        $user->setIsWhiteListed(true);
202
203
        $token = $this->createMock(TokenInterface::class);
204
        $token
205
            ->expects($this->once())
206
            ->method('getUser')
207
            ->willReturn($user);
208
209
        $this->tokenStorage
210
            ->expects($this->once())
211
            ->method('getToken')
212
            ->willReturn($token);
213
214
        $event = $this->getEvent(
215
            $this->kernel,
216
            $request,
217
            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

217
            /** @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...
218
        );
219
220
        $this->getLicenseListener()->onKernelRequest($event);
221
    }
222
223
    public function testIsValidByWhiteList(): void
224
    {
225
        $request = new Request(
226
            ['lic' => 'not_active'],
227
            [],
228
            [
229
                '_route' => 'route',
230
                'requires_license' => true,
231
            ]
232
        );
233
234
        $user = new Tenant();
235
        $user->setClientKey('key');
236
237
        $token = $this->createMock(TokenInterface::class);
238
        $token
239
            ->expects($this->once())
240
            ->method('getUser')
241
            ->willReturn($user);
242
243
        $this->tokenStorage
244
            ->expects($this->once())
245
            ->method('getToken')
246
            ->willReturn($token);
247
248
        $date = new \DateTime();
249
        $date->modify('+1 day');
250
251
        $event = $this->getEvent(
252
            $this->kernel,
253
            $request,
254
            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

254
            /** @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...
255
        );
256
257
        $this->getLicenseListener('prod', [['valid_till' => $date->format('Y-m-d'), 'client_key' => 'key']])->onKernelRequest($event);
258
        $this->assertNull($event->getResponse());
259
    }
260
261
    public function testWhiteListIsExpired(): void
262
    {
263
        $request = new Request(
264
            ['lic' => 'not_active'],
265
            [],
266
            [
267
                '_route' => 'route',
268
                'requires_license' => true,
269
            ]
270
        );
271
272
        $user = new Tenant();
273
        $user->setClientKey('key');
274
275
        $token = $this->createMock(TokenInterface::class);
276
        $token
277
            ->expects($this->once())
278
            ->method('getUser')
279
            ->willReturn($user);
280
281
        $this->tokenStorage
282
            ->expects($this->once())
283
            ->method('getToken')
284
            ->willReturn($token);
285
286
        $date = new \DateTime();
287
        $date->modify('-1 day');
288
289
        $this->router
290
            ->expects($this->once())
291
            ->method('generate')
292
            ->with('atlassian_connect_unlicensed')
293
            ->willReturn('http://website.com');
294
295
        $event = $this->getEvent(
296
            $this->kernel,
297
            $request,
298
            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

298
            /** @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...
299
        );
300
301
        $this->getLicenseListener()->onKernelRequest($event);
302
        $this->assertNotNull($event->getResponse());
303
        $response = $event->getResponse();
304
        $this->assertInstanceOf(RedirectResponse::class, $response);
305
        $this->assertEquals('http://website.com', $response->getTargetUrl());
306
    }
307
308
    public function testThrowsException(): void
309
    {
310
        $request = new Request(
311
            ['lic' => 'not_active'],
312
            [],
313
            [
314
                '_route' => 'route',
315
                'requires_license' => true,
316
            ]
317
        );
318
319
        $user = new Tenant();
320
        $user->setClientKey('key');
321
322
        $this->tokenStorage
323
            ->expects($this->once())
324
            ->method('getToken')
325
            ->willThrowException(new \Exception());
326
327
        $this->router
328
            ->expects($this->once())
329
            ->method('generate')
330
            ->with('atlassian_connect_unlicensed')
331
            ->willReturn('http://website.com');
332
333
        $event = $this->getEvent(
334
            $this->kernel,
335
            $request,
336
            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

336
            /** @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...
337
        );
338
339
        $this->getLicenseListener()->onKernelRequest($event);
340
        $this->assertNotNull($event->getResponse());
341
        $response = $event->getResponse();
342
        $this->assertInstanceOf(RedirectResponse::class, $response);
343
        $this->assertEquals('http://website.com', $response->getTargetUrl());
344
    }
345
346
    private function getEvent(KernelInterface $kernel, Request $request, int $type)
347
    {
348
        if (class_exists(RequestEvent::class)) {
349
            return new RequestEvent($kernel, $request, $type);
350
        }
351
352
        return new GetResponseEvent($kernel, $request, $type);
353
    }
354
355
    private function getLicenseListener(string $environment = 'prod', array $license_allow_list = [])
356
    {
357
        return new LicenseListener($this->router, $this->tokenStorage, $environment, $license_allow_list);
358
    }
359
}
360