|
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\OpenIDBundle\Tests\Service; |
|
12
|
|
|
|
|
13
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
14
|
|
|
use LoginCidadao\CoreBundle\Entity\Person; |
|
15
|
|
|
use LoginCidadao\CoreBundle\Model\PersonInterface; |
|
16
|
|
|
use LoginCidadao\OAuthBundle\Entity\Client; |
|
17
|
|
|
use LoginCidadao\OAuthBundle\Model\ClientInterface; |
|
18
|
|
|
use LoginCidadao\OpenIDBundle\Entity\ClientMetadata; |
|
19
|
|
|
use LoginCidadao\OpenIDBundle\Entity\SubjectIdentifier; |
|
20
|
|
|
use LoginCidadao\OpenIDBundle\Entity\SubjectIdentifierRepository; |
|
21
|
|
|
use LoginCidadao\OpenIDBundle\Service\SubjectIdentifierService; |
|
22
|
|
|
use PHPUnit\Framework\TestCase; |
|
23
|
|
|
|
|
24
|
|
|
class SubjectIdentifierServiceTest extends TestCase |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @return EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
|
28
|
|
|
*/ |
|
29
|
|
|
private function getEntityManager() |
|
30
|
|
|
{ |
|
31
|
|
|
return $this->createMock('Doctrine\ORM\EntityManagerInterface'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return ClientInterface|\PHPUnit_Framework_MockObject_MockObject |
|
36
|
|
|
*/ |
|
37
|
|
|
private function getClient() |
|
38
|
|
|
{ |
|
39
|
|
|
/** @var ClientInterface|\PHPUnit_Framework_MockObject_MockObject $client */ |
|
40
|
|
|
$client = $this->createMock('LoginCidadao\OAuthBundle\Model\ClientInterface'); |
|
41
|
|
|
|
|
42
|
|
|
return $client; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return SubjectIdentifierRepository|\PHPUnit_Framework_MockObject_MockObject |
|
47
|
|
|
*/ |
|
48
|
|
|
private function getRepo() |
|
49
|
|
|
{ |
|
50
|
|
|
$repo = $this->getMockBuilder('LoginCidadao\OpenIDBundle\Entity\SubjectIdentifierRepository') |
|
51
|
|
|
->disableOriginalConstructor() |
|
52
|
|
|
->getMock(); |
|
53
|
|
|
|
|
54
|
|
|
return $repo; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param mixed|null $id |
|
59
|
|
|
* @return PersonInterface|\PHPUnit_Framework_MockObject_MockObject |
|
60
|
|
|
*/ |
|
61
|
|
|
private function getPerson($id = null) |
|
62
|
|
|
{ |
|
63
|
|
|
$person = $this->createMock('LoginCidadao\CoreBundle\Model\PersonInterface'); |
|
64
|
|
|
if ($id) { |
|
65
|
|
|
$person->expects($this->once())->method('getId')->willReturn($id); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $person; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param null|string $type |
|
73
|
|
|
* @param ClientInterface|null $client |
|
74
|
|
|
* @return ClientMetadata|\PHPUnit_Framework_MockObject_MockObject |
|
75
|
|
|
*/ |
|
76
|
|
|
private function getClientMetadata($type = null, ClientInterface $client = null) |
|
77
|
|
|
{ |
|
78
|
|
|
$metadata = $this->createMock('LoginCidadao\OpenIDBundle\Entity\ClientMetadata'); |
|
79
|
|
|
|
|
80
|
|
|
if ($client) { |
|
81
|
|
|
$metadata->expects($this->any())->method('getClient')->willReturn($client); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if (!$type) { |
|
85
|
|
|
return $metadata; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$metadata->expects($this->once())->method('getSubjectType')->willReturn($type); |
|
89
|
|
|
if ($type === 'pairwise') { |
|
90
|
|
|
$metadata->expects($this->once())->method('getSectorIdentifier') |
|
91
|
|
|
->willReturn('https://example.com/sector.identifier'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $metadata; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function testPairwiseSubjectIdentifier() |
|
98
|
|
|
{ |
|
99
|
|
|
$id = 123456; |
|
100
|
|
|
$secret = 'my.secret'; |
|
101
|
|
|
|
|
102
|
|
|
$repo = $this->getRepo(); |
|
103
|
|
|
$person = $this->getPerson($id); |
|
104
|
|
|
$metadata = $this->getClientMetadata('pairwise'); |
|
105
|
|
|
|
|
106
|
|
|
$service = new SubjectIdentifierService($this->getEntityManager(), $repo, $secret); |
|
107
|
|
|
$sub = $service->getSubjectIdentifier($person, $metadata); |
|
108
|
|
|
|
|
109
|
|
|
$this->assertNotNull($sub); |
|
110
|
|
|
$this->assertNotEquals($id, $sub); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function testPublicSubjectIdentifier() |
|
114
|
|
|
{ |
|
115
|
|
|
$id = 654321; |
|
116
|
|
|
$secret = 'my.secret'; |
|
117
|
|
|
|
|
118
|
|
|
$repo = $this->getRepo(); |
|
119
|
|
|
$person = $this->getPerson($id); |
|
120
|
|
|
$metadata = $this->getClientMetadata('public'); |
|
121
|
|
|
|
|
122
|
|
|
$service = new SubjectIdentifierService($this->getEntityManager(), $repo, $secret); |
|
123
|
|
|
$sub = $service->getSubjectIdentifier($person, $metadata); |
|
124
|
|
|
|
|
125
|
|
|
$this->assertNotNull($sub); |
|
126
|
|
|
$this->assertEquals($id, $sub); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function testPublicSubjectIdentifierNullMetadata() |
|
130
|
|
|
{ |
|
131
|
|
|
$id = 654321; |
|
132
|
|
|
$secret = 'my.secret'; |
|
133
|
|
|
|
|
134
|
|
|
$repo = $this->getRepo(); |
|
135
|
|
|
$person = $this->getPerson($id); |
|
136
|
|
|
|
|
137
|
|
|
$service = new SubjectIdentifierService($this->getEntityManager(), $repo, $secret); |
|
138
|
|
|
$sub = $service->getSubjectIdentifier($person, null); |
|
139
|
|
|
|
|
140
|
|
|
$this->assertNotNull($sub); |
|
141
|
|
|
$this->assertEquals($id, $sub); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public function testFetchSubjectIdentifier() |
|
145
|
|
|
{ |
|
146
|
|
|
$id = 123456; |
|
147
|
|
|
$secret = 'my.secret'; |
|
148
|
|
|
$expectedSub = 'my_sub_id'; |
|
149
|
|
|
|
|
150
|
|
|
$subId = $this->createMock('LoginCidadao\OpenIDBundle\Entity\SubjectIdentifier'); |
|
151
|
|
|
$subId->expects($this->once())->method('getSubjectIdentifier')->willReturn($expectedSub); |
|
152
|
|
|
|
|
153
|
|
|
$repo = $this->getRepo(); |
|
154
|
|
|
$repo->expects($this->once())->method('findOneBy')->willReturn($subId); |
|
155
|
|
|
|
|
156
|
|
|
$person = $this->getPerson(); |
|
157
|
|
|
$metadata = $this->getClientMetadata(); |
|
158
|
|
|
|
|
159
|
|
|
$service = new SubjectIdentifierService($this->getEntityManager(), $repo, $secret); |
|
160
|
|
|
$sub = $service->getSubjectIdentifier($person, $metadata); |
|
161
|
|
|
|
|
162
|
|
|
$this->assertNotNull($sub); |
|
163
|
|
|
$this->assertNotEquals($id, $sub); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function testIsSubjectIdentifierPersisted() |
|
167
|
|
|
{ |
|
168
|
|
|
$secret = 'my.secret'; |
|
169
|
|
|
|
|
170
|
|
|
$subId = $this->createMock('LoginCidadao\OpenIDBundle\Entity\SubjectIdentifier'); |
|
171
|
|
|
/** @var ClientInterface $client */ |
|
172
|
|
|
$client = $this->createMock('LoginCidadao\OAuthBundle\Model\ClientInterface'); |
|
173
|
|
|
|
|
174
|
|
|
$repo = $this->getRepo(); |
|
175
|
|
|
$repo->expects($this->once())->method('findOneBy')->willReturn($subId); |
|
176
|
|
|
|
|
177
|
|
|
$service = new SubjectIdentifierService($this->getEntityManager(), $repo, $secret); |
|
178
|
|
|
|
|
179
|
|
|
$this->assertTrue($service->isSubjectIdentifierPersisted($this->getPerson(), $client)); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
public function testCreateOnEnforceSubjectIdentifier() |
|
183
|
|
|
{ |
|
184
|
|
|
$id = 123456; |
|
185
|
|
|
$secret = 'my.secret'; |
|
186
|
|
|
|
|
187
|
|
|
$em = $this->getEntityManager(); |
|
188
|
|
|
$em->expects($this->once())->method('persist') |
|
189
|
|
|
->with($this->isInstanceOf('LoginCidadao\OpenIDBundle\Entity\SubjectIdentifier')); |
|
190
|
|
|
|
|
191
|
|
|
$repo = $this->getRepo(); |
|
192
|
|
|
$repo->expects($this->once())->method('findOneBy')->willReturn(null); |
|
193
|
|
|
|
|
194
|
|
|
$person = $this->getPerson($id); |
|
195
|
|
|
$client = $this->getClient(); |
|
196
|
|
|
$metadata = $this->getClientMetadata(null, $client); |
|
197
|
|
|
|
|
198
|
|
|
$service = new SubjectIdentifierService($em, $repo, $secret); |
|
199
|
|
|
$sub = $service->enforceSubjectIdentifier($person, $metadata); |
|
200
|
|
|
|
|
201
|
|
|
$this->assertInstanceOf('LoginCidadao\OpenIDBundle\Entity\SubjectIdentifier', $sub); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
public function testFetchOnEnforceSubjectIdentifier() |
|
205
|
|
|
{ |
|
206
|
|
|
$secret = 'my.secret'; |
|
207
|
|
|
|
|
208
|
|
|
$subId = $this->createMock('LoginCidadao\OpenIDBundle\Entity\SubjectIdentifier'); |
|
209
|
|
|
|
|
210
|
|
|
$repo = $this->getRepo(); |
|
211
|
|
|
$repo->expects($this->once())->method('findOneBy')->willReturn($subId); |
|
212
|
|
|
|
|
213
|
|
|
$person = $this->getPerson(); |
|
214
|
|
|
$client = $this->getClient(); |
|
215
|
|
|
$metadata = $this->getClientMetadata(null, $client); |
|
216
|
|
|
|
|
217
|
|
|
$service = new SubjectIdentifierService($this->getEntityManager(), $repo, $secret); |
|
218
|
|
|
$sub = $service->enforceSubjectIdentifier($person, $metadata); |
|
219
|
|
|
|
|
220
|
|
|
$this->assertInstanceOf('LoginCidadao\OpenIDBundle\Entity\SubjectIdentifier', $sub); |
|
221
|
|
|
$this->assertSame($subId, $sub); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
public function testGetPerson() |
|
225
|
|
|
{ |
|
226
|
|
|
$person = new Person(); |
|
227
|
|
|
$subId = 'my_sub_id'; |
|
228
|
|
|
$client = new Client(); |
|
229
|
|
|
|
|
230
|
|
|
$repo = $this->getRepo(); |
|
231
|
|
|
$repo->expects($this->once()) |
|
232
|
|
|
->method('findOneBy')->with(['subjectIdentifier' => $subId, 'client' => $client]) |
|
233
|
|
|
->willReturn($person); |
|
234
|
|
|
|
|
235
|
|
|
$service = new SubjectIdentifierService($this->getEntityManager(), $repo, 'my.secret'); |
|
236
|
|
|
$this->assertSame($person, $service->getPerson($subId, $client)); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
public function testConvertSubjectIdentifier() |
|
240
|
|
|
{ |
|
241
|
|
|
$oldSub = '123'; |
|
242
|
|
|
|
|
243
|
|
|
/** @var PersonInterface|\PHPUnit_Framework_MockObject_MockObject $person */ |
|
244
|
|
|
$person = $this->createMock('LoginCidadao\CoreBundle\Model\PersonInterface'); |
|
245
|
|
|
$person->expects($this->once())->method('getId')->willReturn($oldSub); |
|
246
|
|
|
|
|
247
|
|
|
$client = new Client(); |
|
248
|
|
|
$metadata = new ClientMetadata(); |
|
249
|
|
|
$metadata->setClient($client); |
|
250
|
|
|
|
|
251
|
|
|
$subjectIdentifier = (new SubjectIdentifier()) |
|
252
|
|
|
->setClient($client) |
|
253
|
|
|
->setPerson($person) |
|
254
|
|
|
->setSubjectIdentifier($oldSub); |
|
255
|
|
|
|
|
256
|
|
|
$repo = $this->getRepo(); |
|
257
|
|
|
$repo->expects($this->once()) |
|
258
|
|
|
->method('findOneBy')->with(['person' => $person, 'client' => $client]) |
|
259
|
|
|
->willReturn($subjectIdentifier); |
|
260
|
|
|
|
|
261
|
|
|
$service = new SubjectIdentifierService($this->getEntityManager(), $repo, 'my.secret'); |
|
262
|
|
|
$newSub = $service->convertSubjectIdentifier($person, $metadata); |
|
263
|
|
|
|
|
264
|
|
|
$this->assertSame($person, $newSub->getPerson()); |
|
265
|
|
|
$this->assertSame($client, $newSub->getClient()); |
|
266
|
|
|
$this->assertNotEquals($oldSub, $newSub->getSubjectIdentifier()); |
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
|
|
|