|
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\Tests\OpenIDBundle\Storage; |
|
12
|
|
|
|
|
13
|
|
|
use Doctrine\ORM\EntityManager; |
|
14
|
|
|
use LoginCidadao\CoreBundle\Entity\Person; |
|
15
|
|
|
use LoginCidadao\CoreBundle\Entity\PersonRepository; |
|
16
|
|
|
use LoginCidadao\OAuthBundle\Entity\Client; |
|
17
|
|
|
use LoginCidadao\OpenIDBundle\Entity\ClientMetadata; |
|
18
|
|
|
use LoginCidadao\OpenIDBundle\Manager\ClientManager; |
|
19
|
|
|
use LoginCidadao\OpenIDBundle\Service\SubjectIdentifierService; |
|
20
|
|
|
use LoginCidadao\OpenIDBundle\Storage\AccessToken; |
|
21
|
|
|
|
|
22
|
|
|
class AccessTokenTest extends \PHPUnit_Framework_TestCase |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
public function testGetAccessToken() |
|
26
|
|
|
{ |
|
27
|
|
|
$clientId = 'client_id'; |
|
28
|
|
|
$token = 'my.access.token'; |
|
29
|
|
|
$expires = time(); |
|
30
|
|
|
$scope = 'scope1 scope2'; |
|
31
|
|
|
$idToken = 'id-token-here'; |
|
32
|
|
|
|
|
33
|
|
|
$clientMetadata = new ClientMetadata(); |
|
34
|
|
|
|
|
35
|
|
|
$client = new Client(); |
|
36
|
|
|
$client->setId('client'); |
|
37
|
|
|
$client->setRandomId('id'); |
|
38
|
|
|
$client->setMetadata($clientMetadata); |
|
39
|
|
|
|
|
40
|
|
|
$person = new Person(); |
|
41
|
|
|
$accessToken = new \LoginCidadao\OAuthBundle\Entity\AccessToken(); |
|
42
|
|
|
$accessToken->setClient($client); |
|
43
|
|
|
$accessToken->setUser($person); |
|
44
|
|
|
$accessToken->setExpiresAt($expires); |
|
45
|
|
|
$accessToken->setScope($scope); |
|
46
|
|
|
$accessToken->setIdToken($idToken); |
|
47
|
|
|
|
|
48
|
|
|
$repo = $this->getMockBuilder('Doctrine\ORM\EntityRepository')->disableOriginalConstructor()->getMock(); |
|
49
|
|
|
$repo->expects($this->once()) |
|
50
|
|
|
->method('findOneBy')->with(['token' => $token]) |
|
51
|
|
|
->willReturn($accessToken); |
|
52
|
|
|
|
|
53
|
|
|
$em = $this->getEntityManager(); |
|
54
|
|
|
$em->expects($this->once()) |
|
55
|
|
|
->method('getRepository')->with('LoginCidadaoOAuthBundle:AccessToken') |
|
56
|
|
|
->willReturn($repo); |
|
57
|
|
|
|
|
58
|
|
|
$subIdService = $this->getSubjectIdentifierService(); |
|
59
|
|
|
$subIdService->expects($this->once()) |
|
60
|
|
|
->method('getSubjectIdentifier')->with($person, $clientMetadata) |
|
61
|
|
|
->willReturn('subId'); |
|
62
|
|
|
|
|
63
|
|
|
$accessTokenStorage = new AccessToken($em); |
|
64
|
|
|
$accessTokenStorage->setSubjectIdentifierService($subIdService); |
|
65
|
|
|
|
|
66
|
|
|
$this->assertSame([ |
|
67
|
|
|
'client_id' => $clientId, |
|
68
|
|
|
'user_id' => 'subId', |
|
69
|
|
|
'expires' => $expires, |
|
70
|
|
|
'scope' => $scope, |
|
71
|
|
|
'id_token' => $idToken, |
|
72
|
|
|
], $accessTokenStorage->getAccessToken($token)); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function testGetAccessTokenNotFound() |
|
76
|
|
|
{ |
|
77
|
|
|
$token = 'my.access.token'; |
|
78
|
|
|
$repo = $this->getMockBuilder('Doctrine\ORM\EntityRepository')->disableOriginalConstructor()->getMock(); |
|
79
|
|
|
$repo->expects($this->once()) |
|
80
|
|
|
->method('findOneBy')->with(['token' => $token]) |
|
81
|
|
|
->willReturn(null); |
|
82
|
|
|
|
|
83
|
|
|
$em = $this->getEntityManager(); |
|
84
|
|
|
$em->expects($this->once()) |
|
85
|
|
|
->method('getRepository')->with('LoginCidadaoOAuthBundle:AccessToken') |
|
86
|
|
|
->willReturn($repo); |
|
87
|
|
|
|
|
88
|
|
|
$accessTokenStorage = new AccessToken($em); |
|
89
|
|
|
$this->assertNull($accessTokenStorage->getAccessToken($token)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
|
94
|
|
|
*/ |
|
95
|
|
|
public function testSetAccessToken() |
|
96
|
|
|
{ |
|
97
|
|
|
$clientId = 'client_id'; |
|
98
|
|
|
$userId = 'subId'; |
|
99
|
|
|
$token = 'my.access.token'; |
|
100
|
|
|
$expires = time(); |
|
101
|
|
|
$scope = 'scope1 scope2'; |
|
102
|
|
|
$idToken = 'id-token-here'; |
|
103
|
|
|
$client = new Client(); |
|
104
|
|
|
|
|
105
|
|
|
$person = new Person(); |
|
106
|
|
|
$personRepo = $this->getPersonRepository(); |
|
107
|
|
|
$personRepo->expects($this->once()) |
|
108
|
|
|
->method('find')->with($userId) |
|
109
|
|
|
->willReturn($person); |
|
110
|
|
|
|
|
111
|
|
|
$subIdService = $this->getSubjectIdentifierService(); |
|
112
|
|
|
$subIdService->expects($this->once()) |
|
113
|
|
|
->method('getPerson')->with($userId, $client) |
|
114
|
|
|
->willReturn(null); |
|
115
|
|
|
|
|
116
|
|
|
$em = $this->getEntityManager(); |
|
117
|
|
|
$em->expects($this->once())->method('flush'); |
|
118
|
|
|
$em->expects($this->once())->method('persist') |
|
119
|
|
|
->with($this->isInstanceOf('LoginCidadao\OAuthBundle\Entity\AccessToken')); |
|
120
|
|
|
$em->expects($this->once()) |
|
121
|
|
|
->method('getRepository')->with('LoginCidadaoCoreBundle:Person') |
|
122
|
|
|
->willReturn($personRepo); |
|
123
|
|
|
|
|
124
|
|
|
/** @var ClientManager|\PHPUnit_Framework_MockObject_MockObject $clientManager */ |
|
125
|
|
|
$clientManager = $this->getMockBuilder('LoginCidadao\OpenIDBundle\Manager\ClientManager') |
|
126
|
|
|
->disableOriginalConstructor()->getMock(); |
|
127
|
|
|
$clientManager->expects($this->once()) |
|
128
|
|
|
->method('getClientById')->with($clientId) |
|
129
|
|
|
->willReturn($client); |
|
130
|
|
|
|
|
131
|
|
|
$accessTokenStorage = new AccessToken($em); |
|
132
|
|
|
$accessTokenStorage->setSubjectIdentifierService($subIdService); |
|
133
|
|
|
$accessTokenStorage->setClientManager($clientManager); |
|
134
|
|
|
$accessTokenStorage->setAccessToken($token, $clientId, $userId, $expires, $scope, $idToken); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
|
139
|
|
|
*/ |
|
140
|
|
|
public function testSetAccessTokenNoUser() |
|
141
|
|
|
{ |
|
142
|
|
|
$clientId = 'client_id'; |
|
143
|
|
|
$userId = null; |
|
144
|
|
|
$token = 'my.access.token'; |
|
145
|
|
|
$expires = time(); |
|
146
|
|
|
$scope = 'scope1 scope2'; |
|
147
|
|
|
$idToken = 'id-token-here'; |
|
148
|
|
|
|
|
149
|
|
|
$accessTokenStorage = new AccessToken($this->getEntityManager()); |
|
150
|
|
|
$this->assertNull($accessTokenStorage->setAccessToken($token, $clientId, $userId, $expires, $scope, $idToken)); |
|
|
|
|
|
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @return EntityManager|\PHPUnit_Framework_MockObject_MockObject |
|
155
|
|
|
*/ |
|
156
|
|
|
private function getEntityManager() |
|
157
|
|
|
{ |
|
158
|
|
|
return $this->getMockBuilder('Doctrine\ORM\EntityManager')->disableOriginalConstructor()->getMock(); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @return PersonRepository|\PHPUnit_Framework_MockObject_MockObject |
|
163
|
|
|
*/ |
|
164
|
|
|
private function getPersonRepository() |
|
165
|
|
|
{ |
|
166
|
|
|
return $this->getMockBuilder('LoginCidadao\CoreBundle\Entity\PersonRepository') |
|
167
|
|
|
->disableOriginalConstructor()->getMock(); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @return SubjectIdentifierService|\PHPUnit_Framework_MockObject_MockObject |
|
172
|
|
|
*/ |
|
173
|
|
|
private function getSubjectIdentifierService() |
|
174
|
|
|
{ |
|
175
|
|
|
return $this->getMockBuilder('LoginCidadao\OpenIDBundle\Service\SubjectIdentifierService') |
|
176
|
|
|
->disableOriginalConstructor()->getMock(); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.