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

LicenseListenerTest::getLicenseListener()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
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
    private LicenseListener $listener;
0 ignored issues
show
introduced by
The private property $listener is not used, and could be removed.
Loading history...
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;
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...
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
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

82
            /** @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...
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
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

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

123
            /** @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...
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
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

161
            /** @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...
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());
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

168
        $this->assertEquals('http://website.com', $response->/** @scrutinizer ignore-call */ getTargetUrl());
Loading history...
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
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

199
            /** @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...
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
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

236
            /** @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...
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
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

280
            /** @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...
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
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

318
            /** @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...
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