|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the login-cidadao project or it's bundles. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Guilherme Donato <guilhermednt on github> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace LoginCidadao\RemoteClaimsBundle\Tests\Model; |
|
12
|
|
|
|
|
13
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
14
|
|
|
use LoginCidadao\CoreBundle\Entity\Authorization; |
|
15
|
|
|
use LoginCidadao\OAuthBundle\Model\ClientInterface; |
|
16
|
|
|
use LoginCidadao\RemoteClaimsBundle\Entity\RemoteClaim; |
|
17
|
|
|
use LoginCidadao\RemoteClaimsBundle\Entity\RemoteClaimAuthorization; |
|
18
|
|
|
use LoginCidadao\RemoteClaimsBundle\Entity\RemoteClaimAuthorizationRepository; |
|
19
|
|
|
use LoginCidadao\RemoteClaimsBundle\Entity\RemoteClaimRepository; |
|
20
|
|
|
use LoginCidadao\RemoteClaimsBundle\Model\ClaimProviderInterface; |
|
21
|
|
|
use LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimAuthorizationInterface; |
|
22
|
|
|
use LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimManager; |
|
23
|
|
|
use LoginCidadao\CoreBundle\Model\PersonInterface; |
|
24
|
|
|
use LoginCidadao\RemoteClaimsBundle\Model\TagUri; |
|
25
|
|
|
|
|
26
|
|
|
class RemoteClaimManagerTest extends \PHPUnit_Framework_TestCase |
|
27
|
|
|
{ |
|
28
|
|
|
public function testEnforceNewAuthorization() |
|
29
|
|
|
{ |
|
30
|
|
|
$authorization = $this->getRemoteClaimAuthorization(); |
|
31
|
|
|
|
|
32
|
|
|
$em = $this->getEntityManager(); |
|
33
|
|
|
$em->expects($this->once())->method('persist')->with($authorization); |
|
34
|
|
|
|
|
35
|
|
|
$repo = $this->getRepo(); |
|
36
|
|
|
$repo->expects($this->once())->method('findAuthorization')->willReturn(null); |
|
37
|
|
|
|
|
38
|
|
|
$manager = new RemoteClaimManager($em, $repo, $this->getRemoteClaimRepo()); |
|
39
|
|
|
$this->assertSame($authorization, $manager->enforceAuthorization($authorization)); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function testEnforceExistingAuthorization() |
|
43
|
|
|
{ |
|
44
|
|
|
$authorization = $this->getRemoteClaimAuthorization(); |
|
45
|
|
|
$existingAuthorization = $this->getRemoteClaimAuthorization(); |
|
46
|
|
|
|
|
47
|
|
|
$em = $this->getEntityManager(); |
|
48
|
|
|
$em->expects($this->never())->method('persist'); |
|
49
|
|
|
|
|
50
|
|
|
$repo = $this->getRepo(); |
|
51
|
|
|
$repo->expects($this->once())->method('findAuthorization')->willReturn($existingAuthorization); |
|
52
|
|
|
|
|
53
|
|
|
$manager = new RemoteClaimManager($em, $repo, $this->getRemoteClaimRepo()); |
|
54
|
|
|
$this->assertSame($existingAuthorization, $manager->enforceAuthorization($authorization)); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function testIsAuthorizedClaimNameString() |
|
58
|
|
|
{ |
|
59
|
|
|
$claimName = 'tag:example.com,2017:my_claim'; |
|
60
|
|
|
$person = $this->getPerson(); |
|
61
|
|
|
$client = $this->getClient(); |
|
62
|
|
|
$authorization = $this->getRemoteClaimAuthorization(); |
|
63
|
|
|
|
|
64
|
|
|
$repo = $this->getRepo(); |
|
65
|
|
|
$repo->expects($this->once())->method('findAuthorization')->willReturn($authorization); |
|
66
|
|
|
|
|
67
|
|
|
$manager = new RemoteClaimManager($this->getEntityManager(), $repo, $this->getRemoteClaimRepo()); |
|
68
|
|
|
|
|
69
|
|
|
$this->assertTrue($manager->isAuthorized($claimName, $person, $client)); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function testIsAuthorizedTagUri() |
|
73
|
|
|
{ |
|
74
|
|
|
$claimName = new TagUri(); |
|
75
|
|
|
$person = $this->getPerson(); |
|
76
|
|
|
$client = $this->getClient(); |
|
77
|
|
|
$authorization = $this->getRemoteClaimAuthorization(); |
|
78
|
|
|
|
|
79
|
|
|
$repo = $this->getRepo(); |
|
80
|
|
|
$repo->expects($this->once())->method('findAuthorization')->willReturn($authorization); |
|
81
|
|
|
|
|
82
|
|
|
$manager = new RemoteClaimManager($this->getEntityManager(), $repo, $this->getRemoteClaimRepo()); |
|
83
|
|
|
|
|
84
|
|
|
$this->assertTrue($manager->isAuthorized($claimName, $person, $client)); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function testIsNotAuthorizedTagUri() |
|
88
|
|
|
{ |
|
89
|
|
|
$claimName = new TagUri(); |
|
90
|
|
|
$person = $this->getPerson(); |
|
91
|
|
|
$client = $this->getClient(); |
|
92
|
|
|
|
|
93
|
|
|
$repo = $this->getRepo(); |
|
94
|
|
|
$repo->expects($this->once())->method('findAuthorization')->willReturn(null); |
|
95
|
|
|
|
|
96
|
|
|
$manager = new RemoteClaimManager($this->getEntityManager(), $repo, $this->getRemoteClaimRepo()); |
|
97
|
|
|
|
|
98
|
|
|
$this->assertFalse($manager->isAuthorized($claimName, $person, $client)); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function testRevokeAllAuthorizations() |
|
102
|
|
|
{ |
|
103
|
|
|
$remoteClaimAuthorizations = [ |
|
104
|
|
|
$this->getRemoteClaimAuthorization(), |
|
105
|
|
|
$this->getRemoteClaimAuthorization(), |
|
106
|
|
|
]; |
|
107
|
|
|
|
|
108
|
|
|
$authorization = $this->getAuthorization(); |
|
109
|
|
|
$authorization->expects($this->once())->method('getPerson')->willReturn($this->getPerson()); |
|
110
|
|
|
$authorization->expects($this->once())->method('getClient')->willReturn($this->getClient()); |
|
111
|
|
|
|
|
112
|
|
|
$em = $this->getEntityManager(); |
|
113
|
|
|
$em->expects($this->exactly(count($remoteClaimAuthorizations)))->method('remove') |
|
114
|
|
|
->with($this->isInstanceOf('LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimAuthorizationInterface')); |
|
115
|
|
|
|
|
116
|
|
|
$repo = $this->getRepo(); |
|
117
|
|
|
$repo->expects($this->once())->method('findAllByClientAndPerson')->willReturn($remoteClaimAuthorizations); |
|
118
|
|
|
|
|
119
|
|
|
$manager = new RemoteClaimManager($em, $repo, $this->getRemoteClaimRepo()); |
|
120
|
|
|
$manager->revokeAllAuthorizations($authorization); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function testFilterRemoteClaimsString() |
|
124
|
|
|
{ |
|
125
|
|
|
$scopes = 'scope1 scope2 tag:example.com,2017:my_claim scope3'; |
|
126
|
|
|
$manager = new RemoteClaimManager($this->getEntityManager(), $this->getRepo(), $this->getRemoteClaimRepo()); |
|
127
|
|
|
$result = $manager->filterRemoteClaims($scopes); |
|
128
|
|
|
|
|
129
|
|
|
$this->assertEquals('scope1 scope2 scope3', $result); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function testFilterRemoteClaimsArray() |
|
133
|
|
|
{ |
|
134
|
|
|
$scopes = ['scope1', 'scope2', 'tag:example.com,2017:my_claim', 'scope3']; |
|
135
|
|
|
$manager = new RemoteClaimManager($this->getEntityManager(), $this->getRepo(), $this->getRemoteClaimRepo()); |
|
136
|
|
|
$result = $manager->filterRemoteClaims($scopes); |
|
137
|
|
|
|
|
138
|
|
|
$this->assertEquals(['scope1', 'scope2', 'scope3'], $result); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function testFilterRemoteClaimsNoAction() |
|
142
|
|
|
{ |
|
143
|
|
|
$scopes = 'scope1 scope2 scope3'; |
|
144
|
|
|
$manager = new RemoteClaimManager($this->getEntityManager(), $this->getRepo(), $this->getRemoteClaimRepo()); |
|
145
|
|
|
$result = $manager->filterRemoteClaims($scopes); |
|
146
|
|
|
|
|
147
|
|
|
$this->assertEquals('scope1 scope2 scope3', $result); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function testFilterRemoteClaimsEmptyString() |
|
151
|
|
|
{ |
|
152
|
|
|
$scopes = ''; |
|
153
|
|
|
$manager = new RemoteClaimManager($this->getEntityManager(), $this->getRepo(), $this->getRemoteClaimRepo()); |
|
154
|
|
|
$result = $manager->filterRemoteClaims($scopes); |
|
155
|
|
|
|
|
156
|
|
|
$this->assertEquals('', $result); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
public function testFilterRemoteClaimsEmptyArray() |
|
160
|
|
|
{ |
|
161
|
|
|
$scopes = []; |
|
162
|
|
|
$manager = new RemoteClaimManager($this->getEntityManager(), $this->getRepo(), $this->getRemoteClaimRepo()); |
|
163
|
|
|
$result = $manager->filterRemoteClaims($scopes); |
|
164
|
|
|
|
|
165
|
|
|
$this->assertEquals([], $result); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
public function testGetRemoteClaimsFromAuthorization() |
|
169
|
|
|
{ |
|
170
|
|
|
$client = $this->getClient(); |
|
171
|
|
|
$person = $this->getPerson(); |
|
172
|
|
|
|
|
173
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|Authorization $authorization */ |
|
174
|
|
|
$authorization = $this->getMock('LoginCidadao\CoreBundle\Entity\Authorization'); |
|
175
|
|
|
$authorization->expects($this->once())->method('getClient')->willReturn($client); |
|
176
|
|
|
$authorization->expects($this->once())->method('getPerson')->willReturn($person); |
|
177
|
|
|
|
|
178
|
|
|
$repo = $this->getRemoteClaimRepo(); |
|
179
|
|
|
$repo->expects($this->once())->method('findByClientAndPerson') |
|
180
|
|
|
->with($client, $person); |
|
181
|
|
|
|
|
182
|
|
|
$manager = new RemoteClaimManager($this->getEntityManager(), $this->getRepo(), $repo); |
|
183
|
|
|
$manager->getRemoteClaimsFromAuthorization($authorization); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function testGetRemoteClaimsAuthorizationsFromAuthorization() |
|
187
|
|
|
{ |
|
188
|
|
|
$client = $this->getClient(); |
|
189
|
|
|
$person = $this->getPerson(); |
|
190
|
|
|
|
|
191
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|Authorization $authorization */ |
|
192
|
|
|
$authorization = $this->getMock('LoginCidadao\CoreBundle\Entity\Authorization'); |
|
193
|
|
|
$authorization->expects($this->once())->method('getClient')->willReturn($client); |
|
194
|
|
|
$authorization->expects($this->once())->method('getPerson')->willReturn($person); |
|
195
|
|
|
|
|
196
|
|
|
$repo = $this->getRepo(); |
|
197
|
|
|
$repo->expects($this->once())->method('findAllByClientAndPerson') |
|
198
|
|
|
->with($client, $person); |
|
199
|
|
|
|
|
200
|
|
|
$manager = new RemoteClaimManager($this->getEntityManager(), $repo, $this->getRemoteClaimRepo()); |
|
201
|
|
|
$manager->getRemoteClaimsAuthorizationsFromAuthorization($authorization); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
public function testGetExistingRemoteClaim() |
|
205
|
|
|
{ |
|
206
|
|
|
$claimName = new TagUri(); |
|
207
|
|
|
|
|
208
|
|
|
$expected = new RemoteClaim(); |
|
209
|
|
|
$repo = $this->getRemoteClaimRepo(); |
|
210
|
|
|
$repo->expects($this->once())->method('findOneBy')->with(['name' => $claimName]) |
|
211
|
|
|
->willReturn($expected); |
|
212
|
|
|
|
|
213
|
|
|
$manager = new RemoteClaimManager($this->getEntityManager(), $this->getRepo(), $repo); |
|
214
|
|
|
|
|
215
|
|
|
$this->assertSame($expected, $manager->getExistingRemoteClaim($claimName)); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
public function testGetRemoteClaimsWithTokens() |
|
219
|
|
|
{ |
|
220
|
|
|
$client = $this->getClient(); |
|
221
|
|
|
$person = $this->getPerson(); |
|
222
|
|
|
$claimName = (new TagUri()) |
|
223
|
|
|
->setAuthorityName('example.com') |
|
224
|
|
|
->setDate('2018-01') |
|
225
|
|
|
->setSpecific('example'); |
|
226
|
|
|
|
|
227
|
|
|
$claimAuth = (new RemoteClaimAuthorization()) |
|
228
|
|
|
->setPerson($person) |
|
229
|
|
|
->setClient($client) |
|
230
|
|
|
->setClaimName($claimName); |
|
231
|
|
|
|
|
232
|
|
|
$authRepo = $this->getRepo(); |
|
233
|
|
|
$authRepo->expects($this->once())->method('findAllByClientAndPerson') |
|
234
|
|
|
->with($client, $person)->willReturn([$claimAuth]); |
|
235
|
|
|
|
|
236
|
|
|
$remoteClaim = (new RemoteClaim())->setName($claimName); |
|
237
|
|
|
$claimsRepo = $this->getRemoteClaimRepo(); |
|
238
|
|
|
$claimsRepo->expects($this->once())->method('findByClientAndPerson') |
|
239
|
|
|
->with($client, $person)->willReturn([$remoteClaim]); |
|
240
|
|
|
|
|
241
|
|
|
$manager = new RemoteClaimManager($this->getEntityManager(), $authRepo, $claimsRepo); |
|
242
|
|
|
$result = $manager->getRemoteClaimsWithTokens($client, $person); |
|
243
|
|
|
|
|
244
|
|
|
$this->assertEquals([ |
|
245
|
|
|
'tag:example.com,2018-01:example' => [ |
|
246
|
|
|
'authorization' => $claimAuth, |
|
247
|
|
|
'remoteClaim' => $remoteClaim, |
|
248
|
|
|
], |
|
249
|
|
|
], $result); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
public function testGetRemoteClaimAuthorizationByAccessToken() |
|
253
|
|
|
{ |
|
254
|
|
|
$claimName = TagUri::createFromString('tag:example.com,2017:my_claim'); |
|
255
|
|
|
$client = $this->getClient(); |
|
256
|
|
|
$person = $this->getPerson(); |
|
257
|
|
|
$provider = $this->getClaimProvider(); |
|
258
|
|
|
$token = 'my_access_token'; |
|
259
|
|
|
$claimAuth = (new RemoteClaimAuthorization()) |
|
260
|
|
|
->setPerson($person) |
|
261
|
|
|
->setClient($client) |
|
262
|
|
|
->setClaimName($claimName) |
|
263
|
|
|
->setAccessToken($token) |
|
264
|
|
|
->setClaimProvider($provider); |
|
265
|
|
|
|
|
266
|
|
|
$authRepo = $this->getRepo(); |
|
267
|
|
|
$authRepo->expects($this->once())->method('findOneBy') |
|
268
|
|
|
->with([ |
|
269
|
|
|
'claimProvider' => $provider, |
|
270
|
|
|
'accessToken' => $token, |
|
271
|
|
|
]) |
|
272
|
|
|
->willReturn($claimAuth); |
|
273
|
|
|
|
|
274
|
|
|
$manager = new RemoteClaimManager($this->getEntityManager(), $authRepo, $this->getRemoteClaimRepo()); |
|
275
|
|
|
|
|
276
|
|
|
$this->assertSame($claimAuth, $manager->getRemoteClaimAuthorizationByAccessToken($provider, $token)); |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
public function testUpdateRemoteClaimUri() |
|
280
|
|
|
{ |
|
281
|
|
|
$claimName = TagUri::createFromString('tag:example.com,2018:my_claim2'); |
|
282
|
|
|
$uri = 'https://new.uri'; |
|
283
|
|
|
|
|
284
|
|
|
$remoteClaim = (new RemoteClaim()) |
|
285
|
|
|
->setUri('https://old.uri/'); |
|
286
|
|
|
|
|
287
|
|
|
$claimRepo = $this->getRemoteClaimRepo(); |
|
288
|
|
|
$claimRepo->expects($this->once())->method('findOneBy') |
|
289
|
|
|
->willReturn($remoteClaim); |
|
290
|
|
|
$manager = new RemoteClaimManager($this->getEntityManager(), $this->getRepo(), $claimRepo); |
|
291
|
|
|
$manager->updateRemoteClaimUri($claimName, $uri); |
|
292
|
|
|
|
|
293
|
|
|
$this->assertEquals($uri, $remoteClaim->getUri()); |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
public function testUpdateRemoteClaimUriNotFoundClaim() |
|
297
|
|
|
{ |
|
298
|
|
|
$claimName = TagUri::createFromString('tag:example.com,2018:my_claim2'); |
|
299
|
|
|
$uri = 'https://new.uri'; |
|
300
|
|
|
|
|
301
|
|
|
$remoteClaim = null; |
|
302
|
|
|
|
|
303
|
|
|
$claimRepo = $this->getRemoteClaimRepo(); |
|
304
|
|
|
$claimRepo->expects($this->once())->method('findOneBy') |
|
305
|
|
|
->willReturn($remoteClaim); |
|
306
|
|
|
$manager = new RemoteClaimManager($this->getEntityManager(), $this->getRepo(), $claimRepo); |
|
307
|
|
|
|
|
308
|
|
|
$this->assertNull($manager->updateRemoteClaimUri($claimName, $uri)); |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|EntityManagerInterface |
|
313
|
|
|
*/ |
|
314
|
|
|
private function getEntityManager() |
|
315
|
|
|
{ |
|
316
|
|
|
return $this->getMock('Doctrine\ORM\EntityManagerInterface'); |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|RemoteClaimAuthorizationRepository |
|
321
|
|
|
*/ |
|
322
|
|
|
private function getRepo() |
|
323
|
|
|
{ |
|
324
|
|
|
return $this->getMockBuilder('LoginCidadao\RemoteClaimsBundle\Entity\RemoteClaimAuthorizationRepository') |
|
325
|
|
|
->disableOriginalConstructor()->getMock(); |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
/** |
|
329
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|RemoteClaimRepository |
|
330
|
|
|
*/ |
|
331
|
|
|
private function getRemoteClaimRepo() |
|
332
|
|
|
{ |
|
333
|
|
|
return $this->getMockBuilder('LoginCidadao\RemoteClaimsBundle\Entity\RemoteClaimRepository') |
|
334
|
|
|
->disableOriginalConstructor()->getMock(); |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
/** |
|
338
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|RemoteClaimAuthorizationInterface |
|
339
|
|
|
*/ |
|
340
|
|
|
private function getRemoteClaimAuthorization() |
|
341
|
|
|
{ |
|
342
|
|
|
return $this->getMock('LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimAuthorizationInterface'); |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
/** |
|
346
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|PersonInterface |
|
347
|
|
|
*/ |
|
348
|
|
|
private function getPerson() |
|
349
|
|
|
{ |
|
350
|
|
|
return $this->getMock('LoginCidadao\CoreBundle\Model\PersonInterface'); |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ClientInterface |
|
355
|
|
|
*/ |
|
356
|
|
|
private function getClient() |
|
357
|
|
|
{ |
|
358
|
|
|
return $this->getMock('LoginCidadao\OAuthBundle\Model\ClientInterface'); |
|
359
|
|
|
} |
|
360
|
|
|
|
|
361
|
|
|
/** |
|
362
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ClaimProviderInterface |
|
363
|
|
|
*/ |
|
364
|
|
|
private function getClaimProvider() |
|
365
|
|
|
{ |
|
366
|
|
|
return $this->getMock('LoginCidadao\RemoteClaimsBundle\Model\ClaimProviderInterface'); |
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
/** |
|
370
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|Authorization |
|
371
|
|
|
*/ |
|
372
|
|
|
private function getAuthorization() |
|
373
|
|
|
{ |
|
374
|
|
|
return $this->getMock('LoginCidadao\CoreBundle\Entity\Authorization'); |
|
375
|
|
|
} |
|
376
|
|
|
} |
|
377
|
|
|
|