|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace AtlassianConnectBundle\Tests\Security; |
|
4
|
|
|
|
|
5
|
|
|
use AtlassianConnectBundle\Entity\Tenant; |
|
6
|
|
|
use AtlassianConnectBundle\Security\JWTAuthenticator; |
|
7
|
|
|
use AtlassianConnectBundle\Security\JWTUserProvider; |
|
8
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
|
9
|
|
|
use Doctrine\Common\Persistence\ObjectRepository; |
|
10
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
11
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
12
|
|
|
use PHPUnit\Framework\TestCase; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
14
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
|
15
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class JWTAuthenticatorTest |
|
19
|
|
|
*/ |
|
20
|
|
|
final class JWTAuthenticatorTest extends TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var JWTUserProvider|MockObject |
|
24
|
|
|
*/ |
|
25
|
|
|
private $userProvider; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var KernelInterface|MockObject |
|
29
|
|
|
*/ |
|
30
|
|
|
private $kernel; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var ManagerRegistry|MockObject |
|
34
|
|
|
*/ |
|
35
|
|
|
private $managerRegistry; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var EntityManagerInterface|MockObject |
|
39
|
|
|
*/ |
|
40
|
|
|
private $em; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
private $tenantEntityClass; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var int |
|
49
|
|
|
*/ |
|
50
|
|
|
private $devTenant; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var JWTAuthenticator |
|
54
|
|
|
*/ |
|
55
|
|
|
private $jwtAuthenticator; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Setup method |
|
59
|
|
|
*/ |
|
60
|
|
|
public function setUp(): void |
|
61
|
|
|
{ |
|
62
|
|
|
$this->userProvider = $this->createMock(JWTUserProvider::class); |
|
63
|
|
|
$this->kernel = $this->createMock(KernelInterface::class); |
|
64
|
|
|
$this->managerRegistry = $this->createMock(ManagerRegistry::class); |
|
65
|
|
|
$this->em = $this->createMock(EntityManagerInterface::class); |
|
66
|
|
|
$this->tenantEntityClass = Tenant::class; |
|
67
|
|
|
$this->devTenant = 1; |
|
68
|
|
|
|
|
69
|
|
|
$this->managerRegistry |
|
70
|
|
|
->method('getManager') |
|
|
|
|
|
|
71
|
|
|
->willReturn($this->em); |
|
72
|
|
|
|
|
73
|
|
|
$this->jwtAuthenticator = new JWTAuthenticator( |
|
74
|
|
|
$this->userProvider, |
|
75
|
|
|
$this->kernel, |
|
76
|
|
|
$this->managerRegistry, |
|
77
|
|
|
$this->tenantEntityClass, |
|
78
|
|
|
$this->devTenant |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Tests if the request is supported |
|
84
|
|
|
*/ |
|
85
|
|
|
public function testSupportsRequest(): void |
|
86
|
|
|
{ |
|
87
|
|
|
$request = new Request(['jwt' => 'token']); |
|
88
|
|
|
$this->assertTrue($this->jwtAuthenticator->supports($request)); |
|
89
|
|
|
|
|
90
|
|
|
$request = new Request(); |
|
91
|
|
|
$request->headers->set('authorization', 'jwt token'); |
|
92
|
|
|
$this->assertTrue($this->jwtAuthenticator->supports($request)); |
|
93
|
|
|
|
|
94
|
|
|
$request = new Request(); |
|
95
|
|
|
|
|
96
|
|
|
$this->kernel |
|
97
|
|
|
->expects($this->once()) |
|
|
|
|
|
|
98
|
|
|
->method('getEnvironment') |
|
99
|
|
|
->willReturn('dev'); |
|
100
|
|
|
|
|
101
|
|
|
$this->assertTrue($this->jwtAuthenticator->supports($request)); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Tests if the request is not supportd |
|
106
|
|
|
*/ |
|
107
|
|
|
public function testDoesNotSupportRequest(): void |
|
108
|
|
|
{ |
|
109
|
|
|
$request = new Request(); |
|
110
|
|
|
$this->assertFalse($this->jwtAuthenticator->supports($request)); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Test if the getCredentials method returns a valid array |
|
115
|
|
|
*/ |
|
116
|
|
|
public function testGetsCredentials(): void |
|
117
|
|
|
{ |
|
118
|
|
|
$request = new Request(['jwt' => 'token']); |
|
119
|
|
|
$credentials = $this->jwtAuthenticator->getCredentials($request); |
|
120
|
|
|
$this->assertIsArray($credentials); |
|
121
|
|
|
$this->assertArrayHasKey('jwt', $credentials); |
|
122
|
|
|
$this->assertEquals('token', $credentials['jwt']); |
|
123
|
|
|
|
|
124
|
|
|
$request = new Request(); |
|
125
|
|
|
$request->headers->set('authorization', 'jwt token'); |
|
126
|
|
|
$credentials = $this->jwtAuthenticator->getCredentials($request); |
|
127
|
|
|
$this->assertIsArray($credentials); |
|
128
|
|
|
$this->assertArrayHasKey('jwt', $credentials); |
|
129
|
|
|
$this->assertEquals('token', $credentials['jwt']); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Test if the getCredentials method returns null when no jwt token is passed |
|
134
|
|
|
*/ |
|
135
|
|
|
public function testGetsCredentialsTokenDoesNotExist(): void |
|
136
|
|
|
{ |
|
137
|
|
|
$this->kernel |
|
138
|
|
|
->expects($this->once()) |
|
139
|
|
|
->method('getEnvironment') |
|
140
|
|
|
->willReturn('prod'); |
|
141
|
|
|
|
|
142
|
|
|
$request = new Request(); |
|
143
|
|
|
$credentials = $this->jwtAuthenticator->getCredentials($request); |
|
144
|
|
|
$this->assertNull($credentials); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Test if the getCredentials method can get the credentials in dev mode |
|
149
|
|
|
*/ |
|
150
|
|
|
public function testGetsCredentialsOnDevTenant(): void |
|
151
|
|
|
{ |
|
152
|
|
|
$tenant = new Tenant(); |
|
153
|
|
|
$tenant->setClientKey('client_key'); |
|
154
|
|
|
$tenant->setSharedSecret('shared_secret'); |
|
155
|
|
|
|
|
156
|
|
|
$repository = $this->createMock(ObjectRepository::class); |
|
157
|
|
|
$repository |
|
158
|
|
|
->expects($this->once()) |
|
159
|
|
|
->method('find') |
|
160
|
|
|
->with(1) |
|
161
|
|
|
->willReturn($tenant); |
|
162
|
|
|
|
|
163
|
|
|
$this->em |
|
164
|
|
|
->expects($this->once()) |
|
|
|
|
|
|
165
|
|
|
->method('getRepository') |
|
166
|
|
|
->willReturn($repository); |
|
167
|
|
|
|
|
168
|
|
|
$this->kernel |
|
169
|
|
|
->expects($this->once()) |
|
170
|
|
|
->method('getEnvironment') |
|
171
|
|
|
->willReturn('dev'); |
|
172
|
|
|
|
|
173
|
|
|
$request = new Request(); |
|
174
|
|
|
$credentials = $this->jwtAuthenticator->getCredentials($request); |
|
175
|
|
|
$this->assertIsArray($credentials); |
|
176
|
|
|
$this->assertArrayHasKey('jwt', $credentials); |
|
177
|
|
|
$this->assertIsString($credentials['jwt']); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Test if a user gets fetched |
|
182
|
|
|
*/ |
|
183
|
|
|
public function testGetsUser(): void |
|
184
|
|
|
{ |
|
185
|
|
|
$token = [ |
|
186
|
|
|
'iss' => 'iss', |
|
187
|
|
|
'sub' => 'username', |
|
188
|
|
|
]; |
|
189
|
|
|
|
|
190
|
|
|
$tenant = new Tenant(); |
|
191
|
|
|
|
|
192
|
|
|
$this->userProvider |
|
193
|
|
|
->expects($this->once()) |
|
|
|
|
|
|
194
|
|
|
->method('getDecodedToken') |
|
195
|
|
|
->willReturn((object) $token); |
|
196
|
|
|
|
|
197
|
|
|
$this->userProvider |
|
198
|
|
|
->expects($this->once()) |
|
199
|
|
|
->method('loadUserByUsername') |
|
200
|
|
|
->with('iss') |
|
201
|
|
|
->willReturn($tenant); |
|
202
|
|
|
|
|
203
|
|
|
$user = $this->jwtAuthenticator->getUser(['jwt' => 'token'], $this->createMock(UserProviderInterface::class)); |
|
204
|
|
|
|
|
205
|
|
|
$this->assertInstanceOf(Tenant::class, $user); |
|
206
|
|
|
$this->assertEquals('username', $tenant->getUsername()); |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.