Passed
Pull Request — master (#67)
by Matthieu
04:19
created

LicenseListenerTest::testWhiteListIsExpired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 45
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 31
nc 1
nop 0
dl 0
loc 45
rs 9.424
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\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;
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...
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
    protected function setUp(): void
41
    {
42
        $this->router = $this->createMock(RouterInterface::class);
43
        $this->kernel = $this->createMock(KernelInterface::class);
44
        $this->tokenStorage = $this->createMock(TokenStorageInterface::class);
45
    }
46
47
    public function testItSkipsOnASubRequest(): void
48
    {
49
        $attributeParameterBag = $this->createMock(ParameterBagInterface::class);
50
        $attributeParameterBag
51
            ->expects($this->never())
52
            ->method('get');
53
54
        $request = new Request();
55
        $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...
56
57
        $event = $this->getEvent(
58
            $this->kernel,
59
            $request,
60
            KernelInterface::SUB_REQUEST
61
        );
62
63
        $this->getLicenseListener()->onKernelRequest($event);
64
    }
65
66
    public function testItSkipsWhenTheRouteIsNullAndRouteRequiresNoLicense(): void
67
    {
68
        $request = new Request(
69
            ['lic' => 'test'],
70
            [],
71
            [
72
                '_route' => 'route',
73
                'requires_license' => false,
74
            ]
75
        );
76
77
        $event = $this->getEvent(
78
            $this->kernel,
79
            $request,
80
            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

80
            /** @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...
81
        );
82
83
        $this->getLicenseListener('dev')->onKernelRequest($event);
84
85
        $this->assertNull($event->getResponse());
86
    }
87
88
    public function testLicenseIsNotActiveOrDevelopment(): void
89
    {
90
        $request1 = new Request(
91
            ['lic' => 'active'],
92
            [],
93
            [
94
                '_route' => 'route',
95
                'requires_license' => true,
96
            ]
97
        );
98
99
        $request2 = new Request(
100
            ['lic' => 'notactive'],
101
            [],
102
            [
103
                '_route' => 'route',
104
                'requires_license' => true,
105
            ]
106
        );
107
108
        $this->tokenStorage
109
            ->expects($this->never())
110
            ->method('getToken');
111
112
        $event1 = $this->getEvent(
113
            $this->kernel,
114
            $request1,
115
            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

115
            /** @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...
116
        );
117
118
        $event2 = $this->getEvent(
119
            $this->kernel,
120
            $request2,
121
            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

121
            /** @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...
122
        );
123
124
        $this->getLicenseListener('dev')->onKernelRequest($event1);
125
        $this->getLicenseListener('dev')->onKernelRequest($event2);
126
    }
127
128
    public function testUserIsNoTenant(): void
129
    {
130
        $request = new Request(
131
            ['lic' => 'not_active'],
132
            [],
133
            [
134
                '_route' => 'route',
135
                'requires_license' => true,
136
            ]
137
        );
138
139
        $token = $this->createMock(TokenInterface::class);
140
        $token
141
            ->expects($this->once())
142
            ->method('getUser')
143
            ->willReturn($this->createMock(UserInterface::class));
144
145
        $this->tokenStorage
146
            ->expects($this->once())
147
            ->method('getToken')
148
            ->willReturn($token);
149
150
        $this->router
151
            ->expects($this->once())
152
            ->method('generate')
153
            ->with('atlassian_connect_unlicensed')
154
            ->willReturn('http://website.com');
155
156
        $event = $this->getEvent(
157
            $this->kernel,
158
            $request,
159
            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

159
            /** @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...
160
        );
161
162
        $this->getLicenseListener()->onKernelRequest($event);
163
164
        $response = $event->getResponse();
165
        $this->assertInstanceOf(RedirectResponse::class, $response);
166
        $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

166
        $this->assertEquals('http://website.com', $response->/** @scrutinizer ignore-call */ getTargetUrl());
Loading history...
167
    }
168
169
    public function testTenantIsWhiteListed(): void
170
    {
171
        $request = new Request(
172
            ['lic' => 'not_active'],
173
            [],
174
            [
175
                '_route' => 'route',
176
                'requires_license' => true,
177
            ]
178
        );
179
180
        $user = new Tenant();
181
        $user->setIsWhiteListed(true);
182
183
        $token = $this->createMock(TokenInterface::class);
184
        $token
185
            ->expects($this->once())
186
            ->method('getUser')
187
            ->willReturn($user);
188
189
        $this->tokenStorage
190
            ->expects($this->once())
191
            ->method('getToken')
192
            ->willReturn($token);
193
194
        $event = $this->getEvent(
195
            $this->kernel,
196
            $request,
197
            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

197
            /** @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...
198
        );
199
200
        $this->getLicenseListener()->onKernelRequest($event);
201
    }
202
203
    public function testIsValidByWhiteList(): void
204
    {
205
        $request = new Request(
206
            ['lic' => 'not_active'],
207
            [],
208
            [
209
                '_route' => 'route',
210
                'requires_license' => true,
211
            ]
212
        );
213
214
        $user = new Tenant();
215
        $user->setClientKey('key');
216
217
        $token = $this->createMock(TokenInterface::class);
218
        $token
219
            ->expects($this->once())
220
            ->method('getUser')
221
            ->willReturn($user);
222
223
        $this->tokenStorage
224
            ->expects($this->once())
225
            ->method('getToken')
226
            ->willReturn($token);
227
228
        $date = new \DateTime();
229
        $date->modify('+1 day');
230
231
        $event = $this->getEvent(
232
            $this->kernel,
233
            $request,
234
            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

234
            /** @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...
235
        );
236
237
        $this->getLicenseListener('prod', [['valid_till' => $date->format('Y-m-d'), 'client_key' => 'key']])->onKernelRequest($event);
238
        $this->assertNull($event->getResponse());
239
    }
240
241
    public function testWhiteListIsExpired(): void
242
    {
243
        $request = new Request(
244
            ['lic' => 'not_active'],
245
            [],
246
            [
247
                '_route' => 'route',
248
                'requires_license' => true,
249
            ]
250
        );
251
252
        $user = new Tenant();
253
        $user->setClientKey('key');
254
255
        $token = $this->createMock(TokenInterface::class);
256
        $token
257
            ->expects($this->once())
258
            ->method('getUser')
259
            ->willReturn($user);
260
261
        $this->tokenStorage
262
            ->expects($this->once())
263
            ->method('getToken')
264
            ->willReturn($token);
265
266
        $date = new \DateTime();
267
        $date->modify('-1 day');
268
269
        $this->router
270
            ->expects($this->once())
271
            ->method('generate')
272
            ->with('atlassian_connect_unlicensed')
273
            ->willReturn('http://website.com');
274
275
        $event = $this->getEvent(
276
            $this->kernel,
277
            $request,
278
            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

278
            /** @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...
279
        );
280
281
        $this->getLicenseListener()->onKernelRequest($event);
282
        $this->assertNotNull($event->getResponse());
283
        $response = $event->getResponse();
284
        $this->assertInstanceOf(RedirectResponse::class, $response);
285
        $this->assertEquals('http://website.com', $response->getTargetUrl());
286
    }
287
288
    public function testThrowsException(): void
289
    {
290
        $request = new Request(
291
            ['lic' => 'not_active'],
292
            [],
293
            [
294
                '_route' => 'route',
295
                'requires_license' => true,
296
            ]
297
        );
298
299
        $user = new Tenant();
300
        $user->setClientKey('key');
301
302
        $this->tokenStorage
303
            ->expects($this->once())
304
            ->method('getToken')
305
            ->willThrowException(new \Exception());
306
307
        $this->router
308
            ->expects($this->once())
309
            ->method('generate')
310
            ->with('atlassian_connect_unlicensed')
311
            ->willReturn('http://website.com');
312
313
        $event = $this->getEvent(
314
            $this->kernel,
315
            $request,
316
            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

316
            /** @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...
317
        );
318
319
        $this->getLicenseListener()->onKernelRequest($event);
320
        $this->assertNotNull($event->getResponse());
321
        $response = $event->getResponse();
322
        $this->assertInstanceOf(RedirectResponse::class, $response);
323
        $this->assertEquals('http://website.com', $response->getTargetUrl());
324
    }
325
326
    private function getEvent(KernelInterface $kernel, Request $request, int $type)
327
    {
328
        if (class_exists(RequestEvent::class)) {
329
            return new RequestEvent($kernel, $request, $type);
330
        }
331
332
        return new GetResponseEvent($kernel, $request, $type);
333
    }
334
335
    private function getLicenseListener(string $environment = 'prod', array $license_allow_list = [])
336
    {
337
        return new LicenseListener($this->router, $this->tokenStorage, $environment, $license_allow_list);
338
    }
339
}
340