Passed
Push — master ( f75ef5...782b7a )
by Matthieu
05:01
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\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 testItSkipsWhenTheRouteIsNullAndRouteRequiresNoLicense(): 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 testLicenseIsNotActiveOrDevelopment(): void
88
    {
89
        $request1 = new Request(
90
            ['lic' => 'active'],
91
            [],
92
            [
93
                '_route' => 'route',
94
                'requires_license' => true,
95
            ]
96
        );
97
98
        $request2 = new Request(
99
            ['lic' => 'notactive'],
100
            [],
101
            [
102
                '_route' => 'route',
103
                'requires_license' => true,
104
            ]
105
        );
106
107
        $this->tokenStorage
108
            ->expects($this->never())
109
            ->method('getToken');
110
111
        $event1 = $this->getEvent(
112
            $this->kernel,
113
            $request1,
114
            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

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

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

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

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

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

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

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

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