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
![]() |
|||||
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
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
![]() |
|||||
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 |