|
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\OAuthBundle\Tests\EventListener; |
|
12
|
|
|
|
|
13
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
14
|
|
|
use FOS\OAuthServerBundle\Event\OAuthEvent; |
|
15
|
|
|
use LoginCidadao\CoreBundle\Entity\Authorization; |
|
16
|
|
|
use LoginCidadao\CoreBundle\Entity\Person; |
|
17
|
|
|
use LoginCidadao\CoreBundle\Model\PersonInterface; |
|
18
|
|
|
use LoginCidadao\OAuthBundle\Entity\Client; |
|
19
|
|
|
use LoginCidadao\OAuthBundle\EventListener\OAuthEventListener; |
|
20
|
|
|
use LoginCidadao\OAuthBundle\Helper\ScopeFinderHelper; |
|
21
|
|
|
use LoginCidadao\OpenIDBundle\Service\SubjectIdentifierService; |
|
22
|
|
|
|
|
23
|
|
|
class OAuthEventListenerTest extends \PHPUnit_Framework_TestCase |
|
24
|
|
|
{ |
|
25
|
|
|
public function testOnPreAuthorizationProcessNotPreAuthorized() |
|
26
|
|
|
{ |
|
27
|
|
|
$person = $this->getPerson(); |
|
28
|
|
|
$person->expects($this->once())->method('isAuthorizedClient')->willReturn(false); |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
$personRepo = $this->getPersonRepository(); |
|
31
|
|
|
$personRepo->expects($this->once())->method('findOneBy')->willReturn($person); |
|
32
|
|
|
|
|
33
|
|
|
$em = $this->getEntityManager(['LoginCidadaoCoreBundle:Person' => $personRepo]); |
|
34
|
|
|
|
|
35
|
|
|
$subIdService = $this->getSubjectIdentifierService(); |
|
36
|
|
|
|
|
37
|
|
|
$listener = new OAuthEventListener($em, $this->getScopeFinder(['openid']), $subIdService); |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
$event = new OAuthEvent($person, new Client(), false); |
|
40
|
|
|
$listener->onPreAuthorizationProcess($event); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function testOnPreAuthorizationProcessPreAuthorizedSubNotPersisted() |
|
44
|
|
|
{ |
|
45
|
|
|
$sub = 'abc123'; |
|
46
|
|
|
|
|
47
|
|
|
$person = $this->getPerson(); |
|
48
|
|
|
$person->expects($this->once())->method('isAuthorizedClient')->willReturn(true); |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
$personRepo = $this->getPersonRepository(); |
|
51
|
|
|
$personRepo->expects($this->once())->method('findOneBy')->willReturn($person); |
|
52
|
|
|
|
|
53
|
|
|
$em = $this->getEntityManager(['LoginCidadaoCoreBundle:Person' => $personRepo]); |
|
54
|
|
|
$em->expects($this->once())->method('persist') |
|
|
|
|
|
|
55
|
|
|
->with($this->isInstanceOf('LoginCidadao\OpenIDBundle\Entity\SubjectIdentifier')); |
|
56
|
|
|
|
|
57
|
|
|
$subIdService = $this->getSubjectIdentifierService(); |
|
58
|
|
|
$subIdService->expects($this->once())->method('isSubjectIdentifierPersisted')->willReturn(false); |
|
|
|
|
|
|
59
|
|
|
$subIdService->expects($this->once())->method('getSubjectIdentifier')->willReturn($sub); |
|
60
|
|
|
|
|
61
|
|
|
$listener = new OAuthEventListener($em, $this->getScopeFinder(['openid']), $subIdService); |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
$event = new OAuthEvent($person, new Client(), false); |
|
64
|
|
|
$listener->onPreAuthorizationProcess($event); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function testOnPreAuthorizationProcessPreAuthorizedSubPersisted() |
|
68
|
|
|
{ |
|
69
|
|
|
$person = $this->getPerson(); |
|
70
|
|
|
$person->expects($this->once())->method('isAuthorizedClient')->willReturn(true); |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
$personRepo = $this->getPersonRepository(); |
|
73
|
|
|
$personRepo->expects($this->once())->method('findOneBy')->willReturn($person); |
|
74
|
|
|
|
|
75
|
|
|
$em = $this->getEntityManager(['LoginCidadaoCoreBundle:Person' => $personRepo]); |
|
76
|
|
|
|
|
77
|
|
|
$subIdService = $this->getSubjectIdentifierService(); |
|
78
|
|
|
$subIdService->expects($this->once())->method('isSubjectIdentifierPersisted')->willReturn(true); |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
$listener = new OAuthEventListener($em, $this->getScopeFinder(['openid']), $subIdService); |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
$event = new OAuthEvent($person, new Client(), false); |
|
83
|
|
|
$listener->onPreAuthorizationProcess($event); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function testOnPreAuthorizationProcessNoUser() |
|
87
|
|
|
{ |
|
88
|
|
|
$person = $this->getPerson(); |
|
89
|
|
|
$personRepo = $this->getPersonRepository(); |
|
90
|
|
|
$personRepo->expects($this->once())->method('findOneBy')->willReturn(null); |
|
91
|
|
|
|
|
92
|
|
|
$em = $this->getEntityManager(['LoginCidadaoCoreBundle:Person' => $personRepo]); |
|
93
|
|
|
|
|
94
|
|
|
$subIdService = $this->getSubjectIdentifierService(); |
|
95
|
|
|
|
|
96
|
|
|
$event = new OAuthEvent($person, new Client(), false); |
|
97
|
|
|
|
|
98
|
|
|
$listener = new OAuthEventListener($em, $this->getScopeFinder(['openid']), $subIdService); |
|
|
|
|
|
|
99
|
|
|
$listener->onPreAuthorizationProcess($event); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function testOnPostAuthorizationProcessNotAuthorized() |
|
103
|
|
|
{ |
|
104
|
|
|
$event = new OAuthEvent(new Person(), new Client(), false); |
|
105
|
|
|
$listener = new OAuthEventListener($this->getEntityManager(), $this->getScopeFinder(['openid']), |
|
|
|
|
|
|
106
|
|
|
$this->getSubjectIdentifierService()); |
|
|
|
|
|
|
107
|
|
|
$listener->onPostAuthorizationProcess($event); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function testOnPostAuthorizationProcessNewAuth() |
|
111
|
|
|
{ |
|
112
|
|
|
$sub = 'abc123'; |
|
113
|
|
|
|
|
114
|
|
|
$person = $this->getPerson(); |
|
115
|
|
|
$personRepo = $this->getPersonRepository(); |
|
116
|
|
|
$personRepo->expects($this->once())->method('findOneBy')->willReturn($person); |
|
117
|
|
|
|
|
118
|
|
|
$authRepo = $this->getAuthorizationRepository(); |
|
119
|
|
|
$em = $this->getEntityManager([ |
|
120
|
|
|
'LoginCidadaoCoreBundle:Person' => $personRepo, |
|
121
|
|
|
'LoginCidadaoCoreBundle:Authorization' => $authRepo, |
|
122
|
|
|
]); |
|
123
|
|
|
$em->expects($this->exactly(2))->method('persist'); |
|
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
$subIdService = $this->getSubjectIdentifierService(); |
|
126
|
|
|
$subIdService->expects($this->once())->method('getSubjectIdentifier')->willReturn($sub); |
|
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
$event = new OAuthEvent(new Person(), new Client(), true); |
|
129
|
|
|
$listener = new OAuthEventListener($em, $this->getScopeFinder(['openid']), $subIdService); |
|
|
|
|
|
|
130
|
|
|
$listener->onPostAuthorizationProcess($event); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function testOnPostAuthorizationProcessMergeAuth() |
|
134
|
|
|
{ |
|
135
|
|
|
$client = new Client(); |
|
136
|
|
|
$person = $this->getPerson(); |
|
137
|
|
|
$personRepo = $this->getPersonRepository(); |
|
138
|
|
|
$personRepo->expects($this->once())->method('findOneBy')->willReturn($person); |
|
139
|
|
|
|
|
140
|
|
|
$auth = new Authorization(); |
|
141
|
|
|
$auth->setPerson($person); |
|
|
|
|
|
|
142
|
|
|
$auth->setClient($client); |
|
143
|
|
|
$auth->setScope(['scope1', 'scope2']); |
|
144
|
|
|
|
|
145
|
|
|
$authRepo = $this->getAuthorizationRepository(); |
|
146
|
|
|
$authRepo->expects($this->once())->method('findOneBy')->willReturn($auth); |
|
147
|
|
|
|
|
148
|
|
|
$em = $this->getEntityManager([ |
|
149
|
|
|
'LoginCidadaoCoreBundle:Person' => $personRepo, |
|
150
|
|
|
'LoginCidadaoCoreBundle:Authorization' => $authRepo, |
|
151
|
|
|
]); |
|
152
|
|
|
|
|
153
|
|
|
$subIdService = $this->getSubjectIdentifierService(); |
|
154
|
|
|
$subIdService->expects($this->once())->method('isSubjectIdentifierPersisted')->willReturn(true); |
|
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
$event = new OAuthEvent($person, $client, true); |
|
157
|
|
|
$listener = new OAuthEventListener($em, $this->getScopeFinder(['scope1', 'scope2', 'scope3']), $subIdService); |
|
|
|
|
|
|
158
|
|
|
$listener->onPostAuthorizationProcess($event); |
|
159
|
|
|
|
|
160
|
|
|
$this->assertContains('scope3', $auth->getScope()); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @param array $repos |
|
165
|
|
|
* @return EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
|
166
|
|
|
*/ |
|
167
|
|
|
private function getEntityManager(array $repos = []) |
|
168
|
|
|
{ |
|
169
|
|
|
$em = $this->getMock('Doctrine\ORM\EntityManagerInterface'); |
|
170
|
|
|
|
|
171
|
|
|
if (count($repos) > 0) { |
|
172
|
|
|
$em->expects($this->atLeastOnce())->method('getRepository')->with($this->isType('string')) |
|
173
|
|
|
->willReturnCallback(function ($key) use ($repos) { |
|
174
|
|
|
return $repos[$key]; |
|
175
|
|
|
}); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return $em; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
private function getPersonRepository() |
|
|
|
|
|
|
182
|
|
|
{ |
|
183
|
|
|
return $this->getMockBuilder('LoginCidadao\CoreBundle\Entity\PersonRepository') |
|
184
|
|
|
->disableOriginalConstructor()->getMock(); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
private function getAuthorizationRepository() |
|
|
|
|
|
|
188
|
|
|
{ |
|
189
|
|
|
return $this->getMockBuilder('LoginCidadao\CoreBundle\Entity\AuthorizationRepository') |
|
190
|
|
|
->disableOriginalConstructor()->getMock(); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @return SubjectIdentifierService|\PHPUnit_Framework_MockObject_MockObject |
|
195
|
|
|
*/ |
|
196
|
|
|
private function getSubjectIdentifierService() |
|
197
|
|
|
{ |
|
198
|
|
|
return $this->getMockBuilder('LoginCidadao\OpenIDBundle\Service\SubjectIdentifierService') |
|
199
|
|
|
->disableOriginalConstructor()->getMock(); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @return PersonInterface|\PHPUnit_Framework_MockObject_MockObject |
|
204
|
|
|
*/ |
|
205
|
|
|
private function getPerson() |
|
206
|
|
|
{ |
|
207
|
|
|
return $this->getMock('LoginCidadao\CoreBundle\Model\PersonInterface'); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @param array|null $scope |
|
212
|
|
|
* @return ScopeFinderHelper|\PHPUnit_Framework_MockObject_MockObject |
|
213
|
|
|
*/ |
|
214
|
|
|
private function getScopeFinder(array $scope = null) |
|
215
|
|
|
{ |
|
216
|
|
|
$helper = $this->getMockBuilder('LoginCidadao\OAuthBundle\Helper\ScopeFinderHelper') |
|
217
|
|
|
->disableOriginalConstructor()->getMock(); |
|
218
|
|
|
|
|
219
|
|
|
if ($scope) { |
|
220
|
|
|
$helper->expects($this->any())->method('getScope')->willReturn($scope); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
return $helper; |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: