|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* File containing the RepositoryAuthenticationProviderTest class. |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
|
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace eZ\Publish\Core\MVC\Symfony\Security\Tests\Authentication; |
|
10
|
|
|
|
|
11
|
|
|
use eZ\Publish\Core\Base\Exceptions\NotFoundException; |
|
12
|
|
|
use eZ\Publish\Core\MVC\Symfony\Security\Authentication\RepositoryAuthenticationProvider; |
|
13
|
|
|
use PHPUnit_Framework_TestCase; |
|
14
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; |
|
15
|
|
|
use eZ\Publish\Core\MVC\Symfony\Security\User; |
|
16
|
|
|
|
|
17
|
|
|
class RepositoryAuthenticationProviderTest extends PHPUnit_Framework_TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|\Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
private $encoderFactory; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var RepositoryAuthenticationProvider |
|
26
|
|
|
*/ |
|
27
|
|
|
private $authProvider; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|\eZ\Publish\API\Repository\Repository |
|
31
|
|
|
*/ |
|
32
|
|
|
private $repository; |
|
33
|
|
|
|
|
34
|
|
|
protected function setUp() |
|
35
|
|
|
{ |
|
36
|
|
|
parent::setUp(); |
|
37
|
|
|
$this->encoderFactory = $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface'); |
|
38
|
|
|
$repository = $this->repository = $this->getMock('eZ\Publish\API\Repository\Repository'); |
|
39
|
|
|
$this->authProvider = new RepositoryAuthenticationProvider( |
|
40
|
|
|
$this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface'), |
|
41
|
|
|
$this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface'), |
|
42
|
|
|
'foo', |
|
43
|
|
|
$this->encoderFactory |
|
44
|
|
|
); |
|
45
|
|
|
$this->authProvider->setRepository($repository); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function testAuthenticationNotEzUser() |
|
49
|
|
|
{ |
|
50
|
|
|
$password = 'some_encoded_password'; |
|
51
|
|
|
$user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface'); |
|
52
|
|
|
$user |
|
53
|
|
|
->expects($this->any()) |
|
54
|
|
|
->method('getPassword') |
|
55
|
|
|
->will($this->returnValue($password)); |
|
56
|
|
|
|
|
57
|
|
|
$tokenUser = $this->getMock('Symfony\Component\Security\Core\User\UserInterface'); |
|
58
|
|
|
$tokenUser |
|
59
|
|
|
->expects($this->any()) |
|
60
|
|
|
->method('getPassword') |
|
61
|
|
|
->will($this->returnValue($password)); |
|
62
|
|
|
$token = new UsernamePasswordToken($tokenUser, 'foo', 'bar'); |
|
63
|
|
|
|
|
64
|
|
|
$method = new \ReflectionMethod($this->authProvider, 'checkAuthentication'); |
|
65
|
|
|
$method->setAccessible(true); |
|
66
|
|
|
$method->invoke($this->authProvider, $user, $token); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException |
|
71
|
|
|
*/ |
|
72
|
|
|
public function testCheckAuthenticationCredentialsChanged() |
|
73
|
|
|
{ |
|
74
|
|
|
$apiUser = $this->getMockBuilder('eZ\Publish\API\Repository\Values\User\User') |
|
75
|
|
|
->setConstructorArgs([['passwordHash' => 'some_encoded_password']]) |
|
76
|
|
|
->setMethods(['getUserId']) |
|
77
|
|
|
->getMockForAbstractClass(); |
|
78
|
|
|
$apiUser |
|
79
|
|
|
->expects($this->once()) |
|
80
|
|
|
->method('getUserId') |
|
81
|
|
|
->will($this->returnValue(456)); |
|
82
|
|
|
$tokenUser = new User($apiUser); |
|
83
|
|
|
$token = new UsernamePasswordToken($tokenUser, 'foo', 'bar'); |
|
84
|
|
|
|
|
85
|
|
|
$renewedApiUser = $this->getMockBuilder('eZ\Publish\API\Repository\Values\User\User') |
|
86
|
|
|
->setConstructorArgs(array(array('passwordHash' => 'renewed_encoded_password'))) |
|
87
|
|
|
->getMockForAbstractClass(); |
|
88
|
|
|
|
|
89
|
|
|
$user = $this->getMock('eZ\Publish\Core\MVC\Symfony\Security\User'); |
|
90
|
|
|
$user |
|
91
|
|
|
->expects($this->any()) |
|
92
|
|
|
->method('getAPIUser') |
|
93
|
|
|
->will($this->returnValue($renewedApiUser)); |
|
94
|
|
|
|
|
95
|
|
|
$method = new \ReflectionMethod($this->authProvider, 'checkAuthentication'); |
|
96
|
|
|
$method->setAccessible(true); |
|
97
|
|
|
$method->invoke($this->authProvider, $user, $token); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function testCheckAuthenticationAlreadyLoggedIn() |
|
101
|
|
|
{ |
|
102
|
|
|
$password = 'encoded_password'; |
|
103
|
|
|
|
|
104
|
|
|
$apiUser = $this->getMockBuilder('eZ\Publish\API\Repository\Values\User\User') |
|
105
|
|
|
->setConstructorArgs(array(array('passwordHash' => $password))) |
|
106
|
|
|
->setMethods(['getUserId']) |
|
107
|
|
|
->getMockForAbstractClass(); |
|
108
|
|
|
$tokenUser = new User($apiUser); |
|
109
|
|
|
$token = new UsernamePasswordToken($tokenUser, 'foo', 'bar'); |
|
110
|
|
|
|
|
111
|
|
|
$user = $this->getMock('eZ\Publish\Core\MVC\Symfony\Security\User'); |
|
112
|
|
|
$user |
|
113
|
|
|
->expects($this->once()) |
|
114
|
|
|
->method('getAPIUser') |
|
115
|
|
|
->will($this->returnValue($apiUser)); |
|
116
|
|
|
|
|
117
|
|
|
$this->repository |
|
118
|
|
|
->expects($this->once()) |
|
119
|
|
|
->method('setCurrentUser') |
|
120
|
|
|
->with($apiUser); |
|
121
|
|
|
|
|
122
|
|
|
$method = new \ReflectionMethod($this->authProvider, 'checkAuthentication'); |
|
123
|
|
|
$method->setAccessible(true); |
|
124
|
|
|
$method->invoke($this->authProvider, $user, $token); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException |
|
129
|
|
|
*/ |
|
130
|
|
|
public function testCheckAuthenticationFailed() |
|
131
|
|
|
{ |
|
132
|
|
|
$user = $this->getMock('eZ\Publish\Core\MVC\Symfony\Security\User'); |
|
133
|
|
|
$userName = 'my_username'; |
|
134
|
|
|
$password = 'foo'; |
|
135
|
|
|
$token = new UsernamePasswordToken($userName, $password, 'bar'); |
|
136
|
|
|
|
|
137
|
|
|
$userService = $this->getMock('eZ\Publish\API\Repository\UserService'); |
|
138
|
|
|
$userService |
|
139
|
|
|
->expects($this->once()) |
|
140
|
|
|
->method('loadUserByCredentials') |
|
141
|
|
|
->with($userName, $password) |
|
142
|
|
|
->will($this->throwException(new NotFoundException('what', 'identifier'))); |
|
143
|
|
|
$this->repository |
|
144
|
|
|
->expects($this->once()) |
|
145
|
|
|
->method('getUserService') |
|
146
|
|
|
->will($this->returnValue($userService)); |
|
147
|
|
|
|
|
148
|
|
|
$method = new \ReflectionMethod($this->authProvider, 'checkAuthentication'); |
|
149
|
|
|
$method->setAccessible(true); |
|
150
|
|
|
$method->invoke($this->authProvider, $user, $token); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function testCheckAuthentication() |
|
154
|
|
|
{ |
|
155
|
|
|
$user = $this->getMock('eZ\Publish\Core\MVC\Symfony\Security\User'); |
|
156
|
|
|
$userName = 'my_username'; |
|
157
|
|
|
$password = 'foo'; |
|
158
|
|
|
$token = new UsernamePasswordToken($userName, $password, 'bar'); |
|
159
|
|
|
|
|
160
|
|
|
$apiUser = $this->getMockForAbstractClass('eZ\Publish\API\Repository\Values\User\User'); |
|
161
|
|
|
$userService = $this->getMock('eZ\Publish\API\Repository\UserService'); |
|
162
|
|
|
$userService |
|
163
|
|
|
->expects($this->once()) |
|
164
|
|
|
->method('loadUserByCredentials') |
|
165
|
|
|
->with($userName, $password) |
|
166
|
|
|
->will($this->returnValue($apiUser)); |
|
167
|
|
|
$this->repository |
|
168
|
|
|
->expects($this->once()) |
|
169
|
|
|
->method('getUserService') |
|
170
|
|
|
->will($this->returnValue($userService)); |
|
171
|
|
|
$this->repository |
|
172
|
|
|
->expects($this->once()) |
|
173
|
|
|
->method('setCurrentUser') |
|
174
|
|
|
->with($apiUser); |
|
175
|
|
|
|
|
176
|
|
|
$method = new \ReflectionMethod($this->authProvider, 'checkAuthentication'); |
|
177
|
|
|
$method->setAccessible(true); |
|
178
|
|
|
$method->invoke($this->authProvider, $user, $token); |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|