|
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\EventSubscriber; |
|
12
|
|
|
|
|
13
|
|
|
use JMS\Serializer\EventDispatcher\Events; |
|
14
|
|
|
use JMS\Serializer\EventDispatcher\ObjectEvent; |
|
15
|
|
|
use JMS\Serializer\GenericSerializationVisitor; |
|
16
|
|
|
use LoginCidadao\APIBundle\Service\VersionService; |
|
17
|
|
|
use LoginCidadao\CoreBundle\Model\PersonInterface; |
|
18
|
|
|
use LoginCidadao\OAuthBundle\Model\AccessTokenManager; |
|
19
|
|
|
use LoginCidadao\OAuthBundle\Model\ClientInterface; |
|
20
|
|
|
use LoginCidadao\RemoteClaimsBundle\Entity\RemoteClaim; |
|
21
|
|
|
use LoginCidadao\RemoteClaimsBundle\Entity\RemoteClaimAuthorization; |
|
22
|
|
|
use LoginCidadao\RemoteClaimsBundle\EventSubscriber\PersonSerializeEventSubscriber; |
|
23
|
|
|
use LoginCidadao\RemoteClaimsBundle\Exception\ClaimUriUnavailableException; |
|
24
|
|
|
use LoginCidadao\RemoteClaimsBundle\Model\ClaimProviderInterface; |
|
25
|
|
|
use LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimFetcherInterface; |
|
26
|
|
|
use LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimManagerInterface; |
|
27
|
|
|
use LoginCidadao\RemoteClaimsBundle\Model\TagUri; |
|
28
|
|
|
|
|
29
|
|
|
class PersonSerializeEventSubscriberTest extends \PHPUnit_Framework_TestCase |
|
30
|
|
|
{ |
|
31
|
|
|
// PersonSerializeEventSubscriber |
|
32
|
|
|
|
|
33
|
|
|
public function testGetSubscribedEvents() |
|
34
|
|
|
{ |
|
35
|
|
|
$this->assertEquals([ |
|
36
|
|
|
[ |
|
37
|
|
|
'event' => Events::POST_SERIALIZE, |
|
38
|
|
|
'method' => 'onPostSerialize', |
|
39
|
|
|
'class' => 'LoginCidadao\CoreBundle\Model\PersonInterface', |
|
40
|
|
|
], |
|
41
|
|
|
], PersonSerializeEventSubscriber::getSubscribedEvents()); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testRunsOnlyForPersonObjects() |
|
45
|
|
|
{ |
|
46
|
|
|
$event = $this->getEvent(); |
|
47
|
|
|
$event->expects($this->once())->method('getObject')->willReturn(new \stdClass()); |
|
48
|
|
|
|
|
49
|
|
|
$subscriber = $this->getSubscriber(); |
|
50
|
|
|
$subscriber->onPostSerialize($event); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function testWontRunForVersion1_2_3() |
|
54
|
|
|
{ |
|
55
|
|
|
$this->checkVersion([1, 2, 3], false); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function testWillRunForVersion2_3_4() |
|
59
|
|
|
{ |
|
60
|
|
|
$this->checkVersion([2, 3, 4], true); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function testWillRunForVersion3_4_5() |
|
64
|
|
|
{ |
|
65
|
|
|
$this->checkVersion([3, 4, 5], true); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function testAddRemoteClaim() |
|
69
|
|
|
{ |
|
70
|
|
|
$dummyJson = []; |
|
71
|
|
|
$endpoint = 'https://dummy/endpoint'; |
|
72
|
|
|
$fetcher = $this->getFetcher(); |
|
73
|
|
|
$fetcher->expects($this->once())->method('discoverClaimUri')->willReturn($endpoint); |
|
74
|
|
|
|
|
75
|
|
|
$this->runAddDistributedAndAggregatedClaimsTest($fetcher, $dummyJson); |
|
76
|
|
|
|
|
77
|
|
|
$tag = 'tag:example.com,2018-01:example'; |
|
78
|
|
|
$accessToken = 'my_secret_access_token'; |
|
79
|
|
|
$this->assertEquals([ |
|
80
|
|
|
$tag => [ |
|
81
|
|
|
'endpoint' => $endpoint, |
|
82
|
|
|
'access_token' => $accessToken, |
|
83
|
|
|
], |
|
84
|
|
|
], $dummyJson['_claim_sources']); |
|
85
|
|
|
|
|
86
|
|
|
$this->assertEquals([$tag => $tag], $dummyJson['_claim_names']); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function testUnknownEndpoint() |
|
90
|
|
|
{ |
|
91
|
|
|
$dummyJson = []; |
|
92
|
|
|
$fetcher = $this->getFetcher(); |
|
93
|
|
|
$fetcher->expects($this->once())->method('discoverClaimUri') |
|
94
|
|
|
->willThrowException(new ClaimUriUnavailableException()); |
|
95
|
|
|
|
|
96
|
|
|
$this->runAddDistributedAndAggregatedClaimsTest($fetcher, $dummyJson); |
|
97
|
|
|
|
|
98
|
|
|
$this->assertEmpty($dummyJson['_claim_names']); |
|
99
|
|
|
$this->assertEmpty($dummyJson['_claim_sources']); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function runAddDistributedAndAggregatedClaimsTest($fetcher, &$dummyJson) |
|
103
|
|
|
{ |
|
104
|
|
|
$person = $this->getPerson(); |
|
105
|
|
|
$client = $this->getClient(); |
|
106
|
|
|
$tag = 'tag:example.com,2018-01:example'; |
|
107
|
|
|
$claimName = TagUri::createFromString($tag); |
|
108
|
|
|
$accessToken = 'my_secret_access_token'; |
|
109
|
|
|
|
|
110
|
|
|
$remoteClaims = [ |
|
111
|
|
|
[ |
|
112
|
|
|
'remoteClaim' => (new RemoteClaim()) |
|
113
|
|
|
->setProvider($client) |
|
114
|
|
|
->setName($claimName), |
|
115
|
|
|
'authorization' => (new RemoteClaimAuthorization()) |
|
116
|
|
|
->setAccessToken($accessToken), |
|
117
|
|
|
], |
|
118
|
|
|
]; |
|
119
|
|
|
|
|
120
|
|
|
$visitor = $this->getVisitor(); |
|
121
|
|
|
$visitor->expects($this->exactly(2)) |
|
122
|
|
|
->method('addData')->willReturnCallback(function ($key, $value) use (&$dummyJson) { |
|
123
|
|
|
$this->assertContains($key, ['_claim_names', '_claim_sources']); |
|
124
|
|
|
$dummyJson[$key] = $value; |
|
125
|
|
|
}); |
|
126
|
|
|
|
|
127
|
|
|
$event = $this->getEvent(); |
|
128
|
|
|
$event->expects($this->once())->method('getVisitor')->willReturn($visitor); |
|
129
|
|
|
$event->expects($this->exactly(2))->method('getObject')->willReturn($person); |
|
130
|
|
|
|
|
131
|
|
|
$accessTokenManager = $this->getAccessTokenManager(); |
|
132
|
|
|
$accessTokenManager->expects($this->once())->method('getTokenClient')->willReturn($client); |
|
133
|
|
|
|
|
134
|
|
|
$remoteClaimManager = $this->getRemoteClaimManager(); |
|
135
|
|
|
$remoteClaimManager->expects($this->once()) |
|
136
|
|
|
->method('getRemoteClaimsWithTokens')->with($client, $person) |
|
137
|
|
|
->willReturn($remoteClaims); |
|
138
|
|
|
|
|
139
|
|
|
$versionService = $this->getVersionService([2, 0, 0]); |
|
140
|
|
|
|
|
141
|
|
|
$subscriber = $this->getSubscriber($accessTokenManager, $remoteClaimManager, $fetcher, $versionService); |
|
142
|
|
|
$subscriber->onPostSerialize($event); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param array $version |
|
147
|
|
|
* @param bool $shouldRun |
|
148
|
|
|
*/ |
|
149
|
|
|
private function checkVersion($version, $shouldRun) |
|
150
|
|
|
{ |
|
151
|
|
|
$event = $this->getEvent(); |
|
152
|
|
|
$event->expects($this->once())->method('getObject')->willReturn($this->getPerson()); |
|
153
|
|
|
if ($shouldRun) { |
|
154
|
|
|
$event->expects($this->once())->method('getVisitor')->willReturn(new \stdClass()); |
|
155
|
|
|
} else { |
|
156
|
|
|
$event->expects($this->never())->method('getVisitor'); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
$versionService = $this->getVersionService($version); |
|
160
|
|
|
|
|
161
|
|
|
$subscriber = $this->getSubscriber(null, null, null, $versionService); |
|
162
|
|
|
$subscriber->onPostSerialize($event); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @return ObjectEvent|\PHPUnit_Framework_MockObject_MockObject |
|
167
|
|
|
*/ |
|
168
|
|
|
private function getEvent() |
|
169
|
|
|
{ |
|
170
|
|
|
return $this->getMockBuilder('JMS\Serializer\EventDispatcher\ObjectEvent') |
|
171
|
|
|
->disableOriginalConstructor()->getMock(); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @return PersonInterface|\PHPUnit_Framework_MockObject_MockObject |
|
176
|
|
|
*/ |
|
177
|
|
|
private function getPerson() |
|
178
|
|
|
{ |
|
179
|
|
|
return $this->getMock('LoginCidadao\CoreBundle\Model\PersonInterface'); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @return ClaimProviderInterface|ClientInterface|\PHPUnit_Framework_MockObject_MockObject |
|
184
|
|
|
*/ |
|
185
|
|
|
private function getClient() |
|
186
|
|
|
{ |
|
187
|
|
|
return $this->getMock('LoginCidadao\OAuthBundle\Entity\Client'); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @return AccessTokenManager|\PHPUnit_Framework_MockObject_MockObject |
|
192
|
|
|
*/ |
|
193
|
|
|
private function getAccessTokenManager() |
|
194
|
|
|
{ |
|
195
|
|
|
return $this->getMockBuilder('LoginCidadao\OAuthBundle\Model\AccessTokenManager') |
|
196
|
|
|
->disableOriginalConstructor()->getMock(); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @return RemoteClaimManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
|
201
|
|
|
*/ |
|
202
|
|
|
private function getRemoteClaimManager() |
|
203
|
|
|
{ |
|
204
|
|
|
return $this->getMock('LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimManagerInterface'); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @return RemoteClaimFetcherInterface|\PHPUnit_Framework_MockObject_MockObject |
|
209
|
|
|
*/ |
|
210
|
|
|
private function getFetcher() |
|
211
|
|
|
{ |
|
212
|
|
|
return $this->getMock('LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimFetcherInterface'); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* @return GenericSerializationVisitor|\PHPUnit_Framework_MockObject_MockObject |
|
217
|
|
|
*/ |
|
218
|
|
|
private function getVisitor() |
|
219
|
|
|
{ |
|
220
|
|
|
return $this->getMockBuilder('JMS\Serializer\GenericSerializationVisitor') |
|
221
|
|
|
->disableOriginalConstructor()->getMock(); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @param array|null $version |
|
226
|
|
|
* @return VersionService|\PHPUnit_Framework_MockObject_MockObject |
|
227
|
|
|
*/ |
|
228
|
|
|
private function getVersionService($version = null) |
|
229
|
|
|
{ |
|
230
|
|
|
$versionService = $this->getMockBuilder('LoginCidadao\APIBundle\Service\VersionService') |
|
231
|
|
|
->disableOriginalConstructor()->getMock(); |
|
232
|
|
|
|
|
233
|
|
|
if ($version !== null) { |
|
234
|
|
|
$versionString = implode('.', $version); |
|
235
|
|
|
$version = [ |
|
236
|
|
|
'major' => $version[0], |
|
237
|
|
|
'minor' => $version[1], |
|
238
|
|
|
'patch' => $version[2], |
|
239
|
|
|
]; |
|
240
|
|
|
|
|
241
|
|
|
$versionService->expects($this->once())->method('getVersionFromRequest')->willReturn($version); |
|
242
|
|
|
$versionService->expects($this->once())->method('getString')->with($version)->willReturn($versionString); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
return $versionService; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
private function getSubscriber( |
|
249
|
|
|
$accessTokenManager = null, |
|
250
|
|
|
$remoteClaimManager = null, |
|
251
|
|
|
$fetcher = null, |
|
252
|
|
|
$versionService = null |
|
253
|
|
|
) { |
|
254
|
|
|
if ($accessTokenManager === null) { |
|
255
|
|
|
$accessTokenManager = $this->getAccessTokenManager(); |
|
256
|
|
|
} |
|
257
|
|
|
if ($remoteClaimManager === null) { |
|
258
|
|
|
$remoteClaimManager = $this->getRemoteClaimManager(); |
|
259
|
|
|
} |
|
260
|
|
|
if ($fetcher === null) { |
|
261
|
|
|
$fetcher = $this->getFetcher(); |
|
262
|
|
|
} |
|
263
|
|
|
if ($versionService === null) { |
|
264
|
|
|
$versionService = $this->getVersionService(); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
return new PersonSerializeEventSubscriber($accessTokenManager, $remoteClaimManager, $fetcher, $versionService); |
|
268
|
|
|
} |
|
269
|
|
|
} |
|
270
|
|
|
|