JWTSecurityHelperTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 45
c 1
b 0
f 0
dl 0
loc 102
rs 10
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsRequestProvider() 0 20 1
A testNoJWTToken() 0 7 1
A setUp() 0 4 1
A testSupportsRequest() 0 9 1
A testGetJWTFromDevTenant() 0 17 1
A testGetJWTFromAuthorizationHeader() 0 6 1
A testCannotFindTenant() 0 11 1
A testGetJWTFromQueryParameter() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AtlassianConnectBundle\Tests\Security;
6
7
use AtlassianConnectBundle\Entity\Tenant;
8
use AtlassianConnectBundle\Repository\TenantRepositoryInterface;
9
use AtlassianConnectBundle\Security\JWTSecurityHelper;
10
use PHPUnit\Framework\MockObject\MockObject;
11
use PHPUnit\Framework\TestCase;
12
use Symfony\Component\HttpFoundation\Request;
13
14
final class JWTSecurityHelperTest extends TestCase
15
{
16
    private TenantRepositoryInterface|MockObject $repository;
17
    private JWTSecurityHelper $helper;
18
19
    protected function setUp(): void
20
    {
21
        $this->repository = $this->createMock(TenantRepositoryInterface::class);
22
        $this->helper = new JWTSecurityHelper($this->repository, 1, 'dev');
23
    }
24
25
    /**
26
     * @dataProvider supportsRequestProvider
27
     */
28
    public function testSupportsRequest(
29
        Request $request,
30
        bool $supportsRequest,
31
        ?int $devTenant,
32
        string $environment
33
    ): void {
34
        $helper = new JWTSecurityHelper($this->repository, $devTenant, $environment);
35
36
        $this->assertSame($supportsRequest, $helper->supportsRequest($request));
37
    }
38
39
    public function supportsRequestProvider(): \Generator
40
    {
41
        $request = new Request(['jwt' => 'token']);
42
43
        yield 'query_parameter' => [$request, true, null, 'prod'];
44
45
        $request = new Request();
46
47
        yield 'empty_request' => [$request, false, null, 'prod'];
48
49
        $request = new Request();
50
        $request->headers->set('authorization', 'Bearer token');
51
52
        yield 'header' => [$request, true, null, 'prod'];
53
54
        yield 'dev_tenant' => [new Request(), true, 1, 'dev'];
55
56
        yield 'no_dev_tenant' => [new Request(), false, null, 'dev'];
57
58
        yield 'dev_tenant_prod' => [new Request(), false, 1, 'prod'];
59
    }
60
61
    public function testGetJWTFromQueryParameter(): void
62
    {
63
        $request = new Request(['jwt' => 'token']);
64
65
        $this->assertSame('token', $this->helper->getJWTToken($request));
66
    }
67
68
    public function testGetJWTFromAuthorizationHeader(): void
69
    {
70
        $request = new Request();
71
        $request->headers->set('authorization', 'Bearer token');
72
73
        $this->assertSame('token', $this->helper->getJWTToken($request));
74
    }
75
76
    public function testGetJWTFromDevTenant(): void
77
    {
78
        $tenant = new Tenant();
79
        $tenant->setClientKey('client_key');
80
        $tenant->setSharedSecret('shared_secret');
81
82
        $this->repository
83
            ->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on AtlassianConnectBundle\R...nantRepositoryInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to AtlassianConnectBundle\R...nantRepositoryInterface. ( Ignorable by Annotation )

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

83
            ->/** @scrutinizer ignore-call */ 
84
              expects($this->once())
Loading history...
84
            ->method('findById')
85
            ->with(1)
86
            ->willReturn($tenant);
87
88
        $jwt = $this->helper->getJWTToken(Request::create('/test'));
89
        $this->assertNotNull($jwt);
90
        $this->assertStringContainsString(
91
            'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.',
92
            $jwt
0 ignored issues
show
Bug introduced by
It seems like $jwt can also be of type null; however, parameter $haystack of PHPUnit\Framework\Assert...tStringContainsString() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

92
            /** @scrutinizer ignore-type */ $jwt
Loading history...
93
        );
94
    }
95
96
    public function testCannotFindTenant(): void
97
    {
98
        $this->expectException(\RuntimeException::class);
99
100
        $this->repository
101
            ->expects($this->once())
102
            ->method('findById')
103
            ->with(1)
104
            ->willReturn(null);
105
106
        $this->helper->getJWTToken(new Request());
107
    }
108
109
    public function testNoJWTToken(): void
110
    {
111
        $helper = new JWTSecurityHelper($this->repository, 1, 'prod');
112
113
        $this->repository->expects($this->never())->method('findById');
114
115
        $this->assertNull($helper->getJWTToken(new Request()));
116
    }
117
}
118