12
12
namespace Symfony \Component \Security \Http \Tests \Firewall ;
13
13
14
14
use PHPUnit \Framework \TestCase ;
15
+ use Symfony \Component \Security \Core \Authenti
F438
cation \Token \Storage \TokenStorage ;
16
+ use Symfony \Component \Security \Core \Authentication \Token \TokenInterface ;
17
+ use Symfony \Component \Security \Core \Authentication \Token \UsernamePasswordToken ;
15
18
use Symfony \Component \Security \Core \Role \Role ;
19
+ use Symfony \Component \Security \Core \Role \SwitchUserRole ;
20
+ use Symfony \Component \Security \Core \User \UserInterface ;
16
21
use Symfony \Component \Security \Http \Event \SwitchUserEvent ;
17
22
use Symfony \Component \Security \Http \Firewall \SwitchUserListener ;
18
23
use Symfony \Component \Security \Http \SecurityEvents ;
@@ -33,7 +38,7 @@ class SwitchUserListenerTest extends TestCase
33
38
34
39
protected function setUp ()
35
40
{
36
- $ this ->tokenStorage = $ this -> getMockBuilder ( ' Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface ' )-> getMock ();
41
+ $ this ->tokenStorage = new TokenStorage ();
37
42
$ this ->userProvider = $ this ->getMockBuilder ('Symfony\Component\Security\Core\User\UserProviderInterface ' )->getMock ();
38
43
$ this ->userChecker = $ this ->getMockBuilder ('Symfony\Component\Security\Core\User\UserCheckerInterface ' )->getMock ();
39
44
$ this ->accessDecisionManager = $ this ->getMockBuilder ('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface ' )->getMock ();
@@ -57,20 +62,21 @@ public function testEventIsIgnoredIfUsernameIsNotPassedWithTheRequest()
57
62
$ this ->request ->expects ($ this ->any ())->method ('get ' )->with ('_switch_user ' )->will ($ this ->returnValue (null ));
58
63
59
64
$ this ->event ->expects ($ this ->never ())->method ('setResponse ' );
60
- $ this ->tokenStorage ->expects ($ this ->never ())->method ('setToken ' );
61
65
62
66
$ listener = new SwitchUserListener ($ this ->tokenStorage , $ this ->userProvider , $ this ->userChecker , 'provider123 ' , $ this ->accessDecisionManager );
63
67
$ listener ->handle ($ this ->event );
68
+
69
+ $ this ->assertNull ($ this ->tokenStorage ->getToken ());
64
70
}
65
71
66
72
/**
67
73
* @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException
68
74
*/
69
75
public function testExitUserThrowsAuthenticationExceptionIfOriginalTokenCannotBeFound ()
70
76
{
71
10000
- $ token = $ this ->getToken (array (new Role ('the role ' )));
77
+ $ token = $ this ->getToken ($ this -> getMockBuilder ( ' Symfony\Component\Security\Core\User\UserInterface ' )-> getMock (), array (new Role ('the role ' )));
72
78
73
- $ this ->tokenStorage ->expects ( $ this -> any ())-> method ( ' getToken ' )-> will ( $ this -> returnValue ( $ token) );
79
+ $ this ->tokenStorage ->setToken ( $ token );
74
80
$ this ->request ->expects ($ this ->any ())->method ('get ' )->with ('_switch_user ' )->will ($ this ->returnValue ('_exit ' ));
75
81
76
82
$ listener = new SwitchUserListener ($ this ->tokenStorage , $ this ->userProvider , $ this ->userChecker , 'provider123 ' , $ this ->accessDecisionManager );
@@ -79,29 +85,23 @@ public function testExitUserThrowsAuthenticationExceptionIfOriginalTokenCannotBe
79
85
80
86
public function testExitUserUpdatesToken ()
81
87
{
82
- $ originalToken = $ this ->getToken ();
83
- $ role = $ this ->getMockBuilder ('Symfony\Component\Security\Core\Role\SwitchUserRole ' )
84
- ->disableOriginalConstructor ()
85
- ->getMock ();
86
- $ role ->expects ($ this ->any ())->method ('getSource ' )->will ($ this ->returnValue ($ originalToken ));
88
+ $ originalToken = $ this ->getToken ($ this ->getMockBuilder ('Symfony\Component\Security\Core\User\UserInterface ' )->getMock ());
87
89
88
- $ this ->tokenStorage ->expects ($ this ->any ())
89
- ->method ('getToken ' )
90
- ->will ($ this ->returnValue ($ this ->getToken (array ($ role ))));
90
+ $ this ->tokenStorage ->setToken ($ this ->getToken ($ this ->getMockBuilder ('Symfony\Component\Security\Core\User\UserInterface ' )->getMock (), array ('ROLE_PREVIOUS_ADMIN ' ), $ originalToken ));
91
91
92
92
$ this ->request ->expects ($ this ->any ())->method ('get ' )->with ('_switch_user ' )->will ($ this ->returnValue ('_exit ' ));
93
93
$ this ->request ->expects ($ this ->any ())->method ('getUri ' )->will ($ this ->returnValue ('/ ' ));
94
94
$ this ->request ->query ->expects ($ this ->once ())->method ('remove ' , '_switch_user ' );
95
95
$ this ->request ->query ->expects ($ this ->any ())->method ('all ' )->will ($ this ->returnValue (array ()));
96
96
$ this ->request ->server ->expects ($ this ->once ())->method ('set ' )->with ('QUERY_STRING ' , '' );
97
97
98
- $ this ->tokenStorage ->expects ($ this ->once ())
99
- ->method ('setToken ' )->with ($ originalToken );
100
98
$ this ->event ->expects ($ this ->once ())
101
99
->method ('setResponse ' )->with ($ this ->isInstanceOf ('Symfony\Component\HttpFoundation\RedirectResponse ' ));
102
100
103
101
$ listener = new SwitchUserListener ($ this ->tokenStorage , $ this ->userProvider , $ this ->userChecker , 'provider123 ' , $ this ->accessDecisionManager );
104
102
$ listener ->handle ($ this ->event );
103
+
104
+ $ this ->assertSame ($ originalToken , $ this ->tokenStorage ->getToken ());
105
105
}
106
106
107
107
public function testExitUserDispatchesEventWithRefreshedUser ()
@@ -114,21 +114,9 @@ public function testExitUserDispatchesEventWithRefreshedUser()
114
114
->method ('refreshUser ' )
115
115
->with ($ originalUser )
116
116
->willReturn ($ refreshedUser );
117
- $ originalToken = $ this ->getToken ();
118
- $ originalToken
119
- ->expects ($ this ->any ())
120
- ->method ('getUser ' )
121
- ->willReturn ($ originalUser );
122
- $ role = $ this
123
- ->getMockBuilder ('Symfony\Component\Security\Core\Role\SwitchUserRole ' )
124
- ->disableOriginalConstructor ()
125
- ->getMock ();
126
- $ role ->expects ($ this ->any ())->method ('getSource ' )->willReturn ($ originalToken );
127
- $ this
128
- ->tokenStorage
129
- ->expects ($ this ->any ())
130
- ->method ('getToken ' )
131
- ->willReturn ($ this ->getToken (array ($ role )));
117
+ $ originalToken = $ this ->getToken ($ originalUser );
118
+ $ role = new SwitchUserRole ('ROLE_PREVIOUS_ADMIN ' , $ originalToken , false );
119
+ $ this ->tokenStorage ->setToken ($ this ->getToken ($ this ->getMockBuilder ('Symfony\Component\Security\Core\User\UserInterface ' )->getMock (), array ($ role ), $ originalToken ));
132
120
$ this
133
121
->request
134
122
->expects ($ this ->any ())
@@ -167,24 +155,8 @@ public function testExitUserDoesNotDispatchEventWithStringUser()
167
155
->userProvider
168
156
->expects ($ this ->never ())
169
157
->method ('refreshUser ' );
170
- $ originalToken = $ this ->getToken ();
171
- $ originalToken
172
- ->expects ($ this ->any ())
173
- ->method ('getUser ' )
174
- ->willReturn ($ originalUser );
175
- $ role = $ this
176
- ->getMockBuilder ('Symfony\Component\Security\Core\Role\SwitchUserRole ' )
177
- ->disableOriginalConstructor ()
178
- ->getMock ();
179
- $ role
180
- ->expects ($ this ->any ())
181
- ->method ('getSource ' )
182
- ->willReturn ($ originalToken );
183
- $ this
184
- ->tokenStorage
185
- ->expects ($ this ->any ())
186
- ->method ('getToken ' )
187
- ->willReturn ($ this ->getToken (array ($ role )));
158
+ $ originalToken = $ this ->getToken ($ originalUser );
159
+ $ this ->tokenStorage ->setToken ($ this ->getToken ($ this ->getMockBuilder ('Symfony\Component\Security\Core\User\UserInterface ' )->getMock (), array ('ROLE_PREVIOUS_ADMIN ' ), $ originalToken ));
188
160
$ this
189
161
->request
190
162
->expects ($ this ->any ())
@@ -218,9 +190,9 @@ public function testExitUserDoesNotDispatchEventWithStringUser()
218
190
*/
219
191
public function testSwitchUserIsDisallowed ()
220
192
{
221
- $ token = $ this ->getToken (array (new Role ('the role ' )));
193
+ $ token = $ this ->getToken ($ this -> getMockBuilder ( ' Symfony\Component\Security\Core\User\UserInterface ' )-> getMock (), array (new Role ('the role ' )));
222
194
223
- $ this ->tokenStorage ->expects ( $ this -> any ())-> method ( ' getToken ' )-> will ( $ this -> returnValue ( $ token) );
195
+ $ this ->tokenStorage ->setToken ( $ token );
224
196
$ this ->request ->expects ($ this ->any ())->method ('get ' )->with ('_switch_user ' )->will ($ this ->returnValue ('kuba ' ));
225
197
226
198
$ this ->accessDecisionManager ->expects ($ this ->once ())
@@ -233,11 +205,11 @@ public function testSwitchUserIsDisallowed()
233
205
234
206
public function testSwitchUser ()
235
207
{
236
- $ token = $ this ->getToken (array (new Role ('the role ' )));
208
+ $ token = $ this ->getToken (' username ' , array (new Role ('the role ' )));
237
209
$ user = $ this ->getMockBuilder ('Symfony\Component\Security\Core\User\UserInterface ' )->getMock ();
238
210
$ user ->expects ($ this ->any ())->method ('getRoles ' )->will ($ this ->returnValue (array ()));
239
211
240
- $ this ->tokenStorage ->expects ( $ this -> any ())-> method ( ' getToken ' )-> will ( $ this -> returnValue ( $ token) );
212
+ $ this ->tokenStorage ->setToken ( $ token );
241
213
$ this ->request ->expects ($ this ->any ())->method ('get ' )->with ('_switch_user ' )->will ($ this ->returnValue ('kuba ' ));
242
214
$ this ->request ->query ->expects ($ this ->once ())->method ('remove ' , '_switch_user ' );
243
215
$ this ->request ->query ->expects ($ this ->any ())->method ('all ' )->will ($ this ->returnValue (array ()));
@@ -254,20 +226,21 @@ public function testSwitchUser()
254
226
->will ($ this ->returnValue ($ user ));
255
227
$ this ->userChecker ->expects ($ this ->once ())
256
228
->method ('checkPostAuth ' )->with ($ user );
257
- $ this ->tokenStorage ->expects ($ this ->once ())
258
- ->method ('setToken ' )->with ($ this ->isInstanceOf ('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken ' ));
259
229
260
230
$ listener = new SwitchUserListener ($ this ->tokenStorage , $ this ->userProvider , $ this ->userChecker , 'provider123 ' , $ this ->accessDecisionManager );
261
231
$ listener ->handle ($ this ->event );
232
+
233
+ $ this ->assertInstanceOf ('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken ' , $ this ->tokenStorage ->getToken ());
234
+ $ this ->assertSame ($ token , $ this ->tokenStorage ->getToken ()->getPreviousToken ());
262
235
}
263
236
264
237
public function testSwitchUserKeepsOtherQueryStringParameters ()
265
238
{
266
- $ token = $ this ->getToken (array (new Role ('the role ' )));
239
+ $ token = $ this ->getToken ($ this -> getMockBuilder ( ' Symfony\Component\Security\Core\User\UserInterface ' )-> getMock (), array (new Role ('the role ' )));
267
240
$ user = $ this ->getMockBuilder ('Symfony\Component\Security\Core\User\UserInterface ' )->getMock ();
268
241
$ user ->expects ($ this ->any ())->method ('getRoles ' )->will ($ this ->returnValue (array ()));
269
242
270
- $ this ->tokenStorage ->expects ( $ this -> any ())-> method ( ' getToken ' )-> will ( $ this -> returnValue ( $ token) );
243
+ $ this ->tokenStorage ->setToken ( $ token );
271
244
$ this ->request ->expects ($ this ->any ())->method ('get ' )->with ('_switch_user ' )->will ($ this ->returnValue ('kuba ' ));
272
245
$ this ->request ->query ->expects ($ this ->once ())->method ('remove ' , '_switch_user ' );
273
246
$ this ->request ->query ->expects ($ this ->any ())->method ('all ' )->will ($ this ->returnValue (array ('page ' => 3 , 'section ' => 2 )));
@@ -283,11 +256,11 @@ public function testSwitchUserKeepsOtherQueryStringParameters()
283
256
->will ($ this ->returnValue ($ user ));
284
257
$ this ->userChecker ->expects ($ this ->once ())
285
258
->method ('checkPostAuth ' )->with ($ user );
286
- $ this ->tokenStorage ->expects ($ this ->once ())
287
- ->method ('setToken ' )->with ($ this ->isInstanceOf ('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken ' ));
288
259
289
260
$ listener = new SwitchUserListener ($ this ->tokenStorage , $ this ->userProvider , $ this ->userChecker , 'provider123 ' , $ this ->accessDecisionManager );
290
261
$ listener ->handle ($ this ->event );
262
+
263
+ $ this ->assertInstanceOf ('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken ' , $ this ->tokenStorage ->getToken ());
291
264
}
292
265
293
266
private function getEvent ($ request )
@@ -303,13 +276,8 @@ private function getEvent($request)
303
276
return $ event ;
304
277
}
305
278
306
- private function getToken (array $ roles = array ())
279
+ private function getToken ($ user , array $ roles = array (), TokenInterface $ previousToken = null )
307
280
{
308
- $ token = $ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\Token\TokenInterface ' )->getMock ();
309
- $ token ->expects ($ this ->any ())
310
- ->method ('getRoles ' )
311
- ->will ($ this ->returnValue ($ roles ));
312
-
313
- return $ token ;
281
+ return new UsernamePasswordToken ($ user , 'password ' , 'provider ' , $ roles , $ previousToken );
314
282
}
315
283
}
0 commit comments