Test Failed
Pull Request — master (#52)
by Matthieu
17:46
created

JWTSecurityHelperTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AtlassianConnectBundle\Tests\Security;
6
7
use AtlassianConnectBundle\Entity\Tenant;
8
use AtlassianConnectBundle\Entity\TenantInterface;
9
use AtlassianConnectBundle\Security\JWTSecurityHelper;
10
use Doctrine\ORM\EntityManagerInterface;
11
use Doctrine\Persistence\ObjectRepository;
12
use PHPUnit\Framework\MockObject\MockObject;
13
use PHPUnit\Framework\TestCase;
14
use Symfony\Component\HttpFoundation\Request;
15
16
/**
17
 * Class JWTSecurityHelperTest
18
 */
19
final class JWTSecurityHelperTest extends TestCase
20
{
21
    /**
22
     * @var EntityManagerInterface|MockObject
23
     */
24
    private $em;
25
26
    /**
27
     * @var JWTSecurityHelper
28
     */
29
    private $helper;
30
31
    protected function setUp(): void
32
    {
33
        $this->em = $this->createMock(EntityManagerInterface::class);
34
        $this->helper = new JWTSecurityHelper($this->em, 1, 'dev', TenantInterface::class);
35
    }
36
37
    /**
38
     * @dataProvider supportsRequestProvider
39
     *
40
     * @param Request $request
41
     * @param bool $supportsRequest
42
     * @param int|null $devTenant
43
     * @param string $environment
44
     */
45
    public function testSupportsRequest(
46
        Request $request,
47
        bool $supportsRequest,
48
        ?int $devTenant,
49
        string $environment
50
    ): void
51
    {
52
        $helper = new JWTSecurityHelper($this->em, $devTenant, $environment, TenantInterface::class);
53
54
        $this->assertSame($supportsRequest, $helper->supportsRequest($request));
55
    }
56
57
    public function supportsRequestProvider(): \Generator
58
    {
59
        $request = new Request(['jwt' => 'token']);
60
        yield 'query_parameter' => [$request, true, null, 'prod'];
61
62
        $request = new Request();
63
        yield 'empty_request' => [$request, false, null, 'prod'];
64
65
        $request = new Request();
66
        $request->headers->set('authorization', 'Bearer token');
67
        yield 'header' => [$request, true, null, 'prod'];
68
69
        yield 'dev_tenant' => [new Request(), true, 1, 'dev'];
70
        yield 'no_dev_tenant' => [new Request(), false, null, 'dev'];
71
        yield 'dev_tenant_prod' => [new Request(), false, 1, 'prod'];
72
    }
73
74
    /**
75
     * Test if the jwt token can be fetched from the query parameters.
76
     */
77
    public function testGetJWTFromQueryParameter(): void
78
    {
79
        $request = new Request(['jwt' => 'token']);
80
81
        $this->assertSame('token', $this->helper->getJWTToken($request));
82
    }
83
84
    /**
85
     * Test if the jwt token can be fetched from the request headers.
86
     */
87
    public function testGetJWTFromAuthorizationHeader(): void
88
    {
89
        $request = new Request();
90
        $request->headers->set('authorization', 'Bearer token');
91
92
        $this->assertSame('token', $this->helper->getJWTToken($request));
93
    }
94
95
    /**
96
     * Test if the jwt token can be fetched from the dev tenant.
97
     */
98
    public function testGetJWTFromDevTenant(): void
99
    {
100
        $tenant = new Tenant();
101
        $tenant->setClientKey('client_key');
102
        $tenant->setSharedSecret('shared_secret');
103
104
        $repository = $this->createMock(ObjectRepository::class);
105
        $repository
106
            ->expects($this->once())
107
            ->method('find')
108
            ->with(1)
109
            ->willReturn($tenant);
110
111
        $this->em
112
            ->expects($this->once())
113
            ->method('getRepository')
114
            ->willReturn($repository);
115
116
        $jwt = $this->helper->getJWTToken(Request::create('/test'));
117
        $this->assertNotNull($jwt);
118
        $this->assertStringContainsString(
119
            'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.',
120
            $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

120
            /** @scrutinizer ignore-type */ $jwt
Loading history...
121
        );
122
    }
123
124
    public function testCannotFindTenant(): void
125
    {
126
        $this->expectException(\RuntimeException::class);
127
128
        $repository = $this->createMock(ObjectRepository::class);
129
        $repository
130
            ->expects($this->once())
131
            ->method('find')
132
            ->with(1)
133
            ->willReturn(null);
134
135
        $this->em
136
            ->expects($this->once())
137
            ->method('getRepository')
138
            ->willReturn($repository);
139
140
        $this->helper->getJWTToken(new Request());
141
    }
142
143
    public function testNoJWTToken(): void
144
    {
145
        $helper = new JWTSecurityHelper($this->em, 1, 'prod', TenantInterface::class);
146
147
        $this->em->expects($this->never())->method('getRepository');
148
        $this->assertNull($helper->getJWTToken(new Request()));
149
    }
150
}
151