12
12
namespace Symfony \Component \Security \Http \Tests \Firewall ;
13
13
14
14
use PHPUnit \Framework \TestCase ;
15
+ use Symfony \Component \HttpFoundation \Request ;
16
+ use Symfony \Component \HttpKernel \Event \GetResponseEvent ;
17
+ use Symfony \Component \HttpKernel \HttpKernelInterface ;
18
+ use Symfony \Component \Security \Core \Authentication \Token \Storage \TokenStorage ;
19
+ use Symfony \Component \Security \Core \Authentication \Token \UsernamePasswordToken ;
20
+ use Symfony \Component \Security \Core \Role \SwitchUserRole ;
21
+ use Symfony \Component \Security \Core \User \User ;
15
22
use Symfony \Component \Security \Http \Event \SwitchUserEvent ;
16
23
use Symfony \Component \Security \Http \Firewall \SwitchUserListener ;
17
24
use Symfony \Component \Security \Http \SecurityEvents ;
@@ -32,14 +39,12 @@ class SwitchUserListenerTest extends TestCase
32
39
33
40
protected function setUp ()
34
41
{
35
- $ this ->tokenStorage = $ this -> getMockBuilder ( ' Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface ' )-> getMock ();
42
+ $ this ->tokenStorage = new TokenStorage ();
36
43
$ this ->userProvider = $ this ->getMockBuilder ('Symfony\Component\Security\Core\User\UserProviderInterface ' )->getMock ();
37
44
$ this ->userChecker = $ this ->getMockBuilder ('Symfony\Component\Security\Core\User\UserCheckerInterface ' )->getMock ();
38
45
$ this ->accessDecisionManager = $ this ->getMockBuilder ('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface ' )->getMock ();
39
- $ this ->request = $ this ->getMockBuilder ('Symfony\Component\HttpFoundation\Request ' )->getMock ();
40
- $ this ->request ->query = $ this ->getMockBuilder ('Symfony\Component\HttpFoundation\ParameterBag ' )->getMock ();
41
- $ this ->request ->server = $ this ->getMockBuilder ('Symfony\Component\HttpFoundation\ServerBag ' )->getMock ();
42
- $ this ->event = $ this ->getEvent ($ this ->request );
46
+ $ this ->request = new Request ();
47
+ $ this ->event = new GetResponseEvent ($ this ->getMockBuilder ('Symfony\Component\HttpKernel\HttpKernelInterface ' )->getMock (), $ this ->request , HttpKernelInterface::MASTER_REQUEST );
43
48
}
44
49
45
50
/**
@@ -53,54 +58,42 @@ public function testProviderKeyIsRequired()
53
58
54
59
public function testEventIsIgnoredIfUsernameIsNotPassedWithTheRequest ()
55
60
{
56
- $ this ->request ->expects ($ this ->any ())->method ('get ' )->with ('_switch_user ' )->will ($ this ->returnValue (null ));
57
-
58
- $ this ->event ->expects ($ this ->never ())->method ('setResponse ' );
59
- $ this ->tokenStorage ->expects ($ this ->never ())->method ('setToken ' );
60
-
61
61
$ listener = new SwitchUserListener ($ this ->tokenStorage , $ this ->userProvider , $ this ->userChecker , 'provider123 ' , $ this ->accessDecisionManager );
62
62
$ listener ->handle ($ this ->event );
63
+
64
+ $ this ->assertNull ($ this ->event ->getResponse ());
65
+ $ this ->assertNull ($ this ->tokenStorage ->getToken ());
63
66
}
64
67
65
68
/**
66
69
* @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException
67
70
*/
68
71
public function testExitUserThrowsAuthenticationExceptionIfOriginalTokenCannotBeFound ()
69
72
{
70
- $ token = $ this -> getToken ( array ($ this -> getMockBuilder ( ' Symfony\Component\Security\Core\Role\RoleInterface ' )-> getMock () ));
73
+ $ token = new UsernamePasswordToken ( ' username ' , '' , ' key ' , array (' ROLE_FOO ' ));
71
74
72
- $ this ->tokenStorage ->expects ( $ this -> any ())-> method ( ' getToken ' )-> will ( $ this -> returnValue ( $ token) );
73
- $ this ->request ->expects ( $ this -> any ())-> method ( ' get ' )-> with ( ' _switch_user ')-> will ( $ this -> returnValue ( '_exit ' ) );
75
+ $ this ->tokenStorage ->setToken ( $ token );
76
+ $ this ->request ->query -> set ( ' _switch_user ', '_exit ' );
74
77
75
78
$ listener = new SwitchUserListener ($ this ->tokenStorage , $ this ->userProvider , $ this ->userChecker , 'provider123 ' , $ this ->accessDecisionManager );
76
79
$ listener ->handle ($ this ->event );
77
80
}
78
81
79
82
public function testExitUserUpdatesToken ()
80
83
{
81
- $ originalToken = $ this ->getToken ();
82
- $ role = $ this ->getMockBuilder ('Symfony\Component\Security\Core\Role\SwitchUserRole ' )
83
- ->disableOriginalConstructor ()
84
- ->getMock ();
85
- $ role ->expects ($ this ->any ())->method ('getSource ' )->will ($ this ->returnValue ($ originalToken ));
86
-
87
- $ this ->tokenStorage ->expects ($ this ->any ())
88
- ->method ('getToken ' )
89
- ->will ($ this ->returnValue ($ this ->getToken (array ($ role ))));
90
-
91
- $ this ->request ->expects ($ this ->any ())->method ('get ' )->with ('_switch_user ' )->will ($ this ->returnValue ('_exit ' ));
92
- $ this ->request ->expects ($ this ->any ())->method ('getUri ' )->will ($ this ->returnValue ('/ ' ));
93
- $ this ->request ->query ->expects ($ this ->once ())->method ('remove ' , '_switch_user ' );
94
- $ this ->request ->query ->expects ($ this ->any ())->method ('all ' )->will ($ this ->returnValue (array ()));
95
- $ this ->request ->server ->expects ($ this ->once ())->method ('set ' )->with ('QUERY_STRING ' , '' );
96
-
97
- $ this ->tokenStorage ->expects ($ this ->once ())
98
- ->method ('setToken ' )->with ($ originalToken );
99
- $ this ->event ->expects ($ this ->once ())
100
- ->method ('setResponse ' )->with ($ this ->isInstanceOf ('Symfony\Component\HttpFoundation\RedirectResponse ' ));
84
+ $ originalToken = new UsernamePasswordToken ('username ' , '' , 'key ' , array ());
85
+ $ this ->tokenStorage ->setToken (new UsernamePasswordToken ('username ' , '' , 'key ' , array (new SwitchUserRole ('ROLE_PREVIOUS ' , $ originalToken ))));
86
+
87
+ $ this ->request ->query ->set ('_switch_user ' , '_exit ' );
101
88
102
89
$ listener = new SwitchUserListener ($ this ->tokenStorage , $ this ->userProvider , $ this ->userChecker , 'provider123 ' , $ this ->accessDecisionManager );
103
90
$ listener ->handle ($ this ->event );
91
+
92
+ $ this ->assertSame (array (), $ this ->request ->query ->all ());
93
+ $ this ->assertSame ('' , $ this ->request ->server ->get ('QUERY_STRING ' ));
94
+ $ this ->assertInstanceOf ('Symfony\Component\HttpFoundation\RedirectResponse ' , $ this ->event ->getResponse ());
95
+ $ this ->assertSame ($ this ->request ->getUri (), $ this ->event ->getResponse ()->getTargetUrl ());
96
+ $ this ->assertSame ($ originalToken , $ this ->tokenStorage ->getToken ());
104
97
}
105
98
106
99
public function testExitUserDispatchesEventWithRefreshedUser ()
@@ -113,38 +106,9 @@ public function testExitUserDispatchesEventWithRefreshedUser()
113
106
->method ('refreshUser ' )
114
107
->with ($ originalUser )
115
108
->willReturn ($ refreshedUser );
116
- $ originalToken = $ this ->getToken ();
117
- $ originalToken
118
- ->expects ($ this ->any ())
119
- ->method ('getUser ' )
120
- ->willReturn ($ originalUser );
121
- $ role = $ this
122
- ->getMockBuilder ('Symfony\Component\Security\Core\Role\SwitchUserRole ' )
123
- ->disableOriginalConstructor ()
124
- ->getMock ();
125
- $ role ->expects ($ this ->any ())->method ('getSource ' )->willReturn ($ originalToken );
126
- $ this
127
- ->tokenStorage
128
- ->expects ($ this ->any ())
129
- ->method ('getToken ' )
130
- ->willReturn ($ this ->getToken (array ($ role )));
131
- $ this
132
- ->request
133
- ->expects ($ this ->any ())
134
- ->method ('get ' )
135
- ->with ('_switch_user ' )
136
- ->willReturn ('_exit ' );
137
- $ this
138
- ->request
139
- ->expects ($ this ->any ())
140
- ->method ('getUri ' )
141
- ->willReturn ('/ ' );
142
- $ this
143
- ->request
144
- ->query
145
- ->expects ($ this ->any ())
146
- ->method ('all ' )
147
- ->will ($ this ->returnValue (array ()));
109
+ $ originalToken = new UsernamePasswordToken ($ originalUser , '' , 'key ' );
110
+ $ this ->tokenStorage ->setToken (new UsernamePasswordToken ('username ' , '' , 'key ' , array (new SwitchUserRole ('ROLE_PREVIOUS ' , $ originalToken ))));
111
+ $ this ->request ->query ->set ('_switch_user ' , '_exit ' );
148
112
149
113
$ dispatcher = $ this ->getMockBuilder ('Symfony\Component\EventDispatcher\EventDispatcherInterface ' )->getMock ();
150
114
$ dispatcher
@@ -166,41 +130,9 @@ public function testExitUserDoesNotDispatchEventWithStringUser()
166
130
->userProvider
167
131
->expects ($ this ->never ())
168
132
->method ('refreshUser ' );
169
- $ originalToken = $ this ->getToken ();
170
- $ originalToken
171
- ->expects ($ this ->any ())
172
- ->method ('getUser ' )
173
- ->willReturn ($ originalUser );
174
- $ role = $ this
175
- ->getMockBuilder ('Symfony\Component\Security\Core\Role\SwitchUserRole ' )
176
- ->disableOriginalConstructor ()
177
- ->getMock ();
178
- $ role
179
- ->expects ($ this ->any ())
180
- ->method ('getSource ' )
181
- ->willReturn ($ originalToken );
182
- $ this
183
- ->tokenStorage
184
- ->expects ($ this ->any ())
185
- ->method ('getToken ' )
186
- ->willReturn ($ this ->getToken (array ($ role )));
187
- $ this
188
- ->request
189
- ->expects ($ this ->any ())
190
- ->method ('get ' )
191
- ->with ('_switch_user ' )
192
- ->willReturn ('_exit ' );
193
- $ this
194
- ->request
195
- ->query
196
- ->expects ($ this ->any ())
197
- ->method ('all ' )
198
- ->will ($ this ->returnValue (array ()));
199
- $ this
200
- ->request
201
- ->expects ($ this ->any ())
202
- ->method ('getUri ' )
203
- ->willReturn ('/ ' );
133
+ $ originalToken = new UsernamePasswordToken ($ originalUser , '' , 'key ' );
134
+ $ this ->tokenStorage ->setToken (new UsernamePasswordToken ('username ' , '' , 'key ' , array (new SwitchUserRole ('ROLE_PREVIOUS ' , $ originalToken ))));
135
+ $ this ->request ->query ->set ('_switch_user ' , '_exit ' );
204
136
205
137
$ dispatcher = $ this ->getMockBuilder ('Symfony\Component\EventDispatcher\EventDispatcherInterface ' )->getMock ();
206
138
$ dispatcher
@@ -217,10 +149,10 @@ public function testExitUserDoesNotDispatchEventWithStringUser()
217
149
*/
218
150
public function testSwitchUserIsDisallowed ()
219
151
{
220
- $ token = $ this -> getToken ( array ($ this -> getMockBuilder ( ' Symfony\Component\Security\Core\Role\RoleInterface ' )-> getMock () ));
152
+ $ token = new UsernamePasswordToken ( ' username ' , '' , ' key ' , array (' ROLE_FOO ' ));
221
153
222
- $ this ->tokenStorage ->expects ( $ this -> any ())-> method ( ' getToken ' )-> will ( $ this -> returnValue ( $ token) );
223
- $ this ->request ->expects ( $ this -> any ())-> method ( ' get ' )-> with ( ' _switch_user ')-> will ( $ this -> returnValue ( 'kuba ' ) );
154
+ $ this ->tokenStorage ->setToken ( $ token );
155
+ $ this ->request ->query -> set ( ' _switch_user ', 'kuba ' );
224
156
225
157
$ this ->accessDecisionManager ->expects ($ this ->once ())
226
158
->method ('decide ' )->with ($ token , array ('ROLE_ALLOWED_TO_SWITCH ' ))
@@ -232,17 +164,11 @@ public function testSwitchUserIsDisallowed()
232
164
233
165
public function testSwitchUser ()
234
166
{
235
- $ token = $ this ->getToken (array ($ this ->getMockBuilder ('Symfony\Component\Security\Core\Role\RoleInterface ' )->getMock ()));
236
- $ user = $ this ->getMockBuilder ('Symfony\Component\Security\Core\User\UserInterface ' )->getMock ();
237
- $ user ->expects ($ this ->any ())->method ('getRoles ' )->will ($ this ->returnValue (array ()));
167
+ $ token = new UsernamePasswordToken ('username ' , '' , 'key ' , array ('ROLE_FOO ' ));
168
+ $ user = new User ('username ' , 'password ' , array ());
238
169
239
- $ this ->tokenStorage ->expects ($ this ->any ())->method ('getToken ' )->will ($ this ->returnValue ($ token ));
240
- $ this ->request ->expects ($ this ->any ())->method ('get ' )->with ('_switch_user ' )->will ($ this ->returnValue ('kuba ' ));
241
- $ this ->request ->query ->expects ($ this ->once ())->method ('remove ' , '_switch_user ' );
242
- $ this ->request ->query ->expects ($ this ->any ())->method ('all ' )->will ($ this ->returnValue (array ()));
243
-
244
- $ this ->request ->expects ($ this ->any ())->method ('getUri ' )->will ($ this ->returnValue ('/ ' ));
245
- $ this ->request ->server ->expects ($ this ->once ())->method ('set ' )->with ('QUERY_STRING ' , '' );
170
+ $ this ->tokenStorage ->setToken ($ token );
171
+ $ this ->request ->query ->set ('_switch_user ' , 'kuba ' );
246
172
247
173
$ this ->accessDecisionManager ->expects ($ this ->once ())
248
174
->method ('decide ' )->with ($ token , array ('ROLE_ALLOWED_TO_SWITCH ' ))
@@ -253,25 +179,26 @@ public function testSwitchUser()
253
179
->will ($ this ->returnValue ($ user ));
254
180
$ this ->userChecker ->expects ($ this ->once ())
255
181
->method ('checkPostAuth ' )->with ($ user );
256
- $ this ->tokenStorage ->expects ($ this ->once ())
257
- ->method ('setToken ' )->with ($ this ->isInstanceOf ('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken ' ));
258
182
259
183
$ listener = new SwitchUserListener ($ this ->tokenStorage , $ this ->userProvider , $ this ->userChecker , 'provider123 ' , $ this ->accessDecisionManager );
260
184
$ listener ->handle ($ this ->event );
185
+
186
+ $ this ->assertSame (array (), $ this ->request ->query ->all ());
187
+ $ this ->assertSame ('' , $ this ->request ->server ->get ('QUERY_STRING ' ));
188
+ $ this ->assertInstanceOf ('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken ' , $ this ->tokenStorage ->getToken ());
261
189
}
262
190
263
191
public function testSwitchUserKeepsOtherQueryStringParameters ()
264
192
{
265
- $ token = $ this ->getToken (array ($ this ->getMockBuilder ('Symfony\Component\Security\Core\Role\RoleInterface ' )->getMock ()));
266
- $ user = $ this ->getMockBuilder ('Symfony\Component\Security\Core\User\UserInterface ' )->getMock ();
267
- $ user ->expects ($ this ->any ())->method ('getRoles ' )->will ($ this ->returnValue (array ()));
193
+ $ token = new UsernamePasswordToken ('username ' , '' , 'key ' , array ('ROLE_FOO ' ));
194
+ $ user = new User ('username ' , 'password ' , array ());
268
195
269
- $ this ->tokenStorage ->expects ( $ this -> any ())-> method ( ' getToken ' )-> will ( $ this -> returnValue ( $ token) );
270
- $ this ->request ->expects ( $ this -> any ())-> method ( ' get ' )-> with ( ' _switch_user ' )-> will ( $ this -> returnValue ( ' kuba ' ));
271
- $ this -> request -> query -> expects ( $ this -> once ())-> method ( ' remove ' , ' _switch_user ' );
272
- $ this -> request -> query -> expects ( $ this -> any ())-> method ( ' all ' )-> will ( $ this -> returnValue ( array ( ' page ' => 3 , ' section ' => 2 )));
273
- $ this -> request -> expects ( $ this -> any ())-> method ( ' getUri ' )-> will ( $ this -> returnValue ( ' / ' ));
274
- $ this -> request -> server -> expects ( $ this -> once ())-> method ( ' set ' )-> with ( ' QUERY_STRING ' , ' page=3§ion=2 ' );
196
+ $ this ->tokenStorage ->setToken ( $ token );
197
+ $ this ->request ->query -> replace ( array (
198
+ ' _switch_user ' => ' kuba ' ,
199
+ ' page ' => 3 ,
200
+ ' section ' => 2 ,
201
+ ) );
275
202
276
203
$ this ->accessDecisionManager ->expects ($ this ->once ())
277
204
->method ('decide ' )->with ($ token , array ('ROLE_ALLOWED_TO_SWITCH ' ))
@@ -282,33 +209,11 @@ public function testSwitchUserKeepsOtherQueryStringParameters()
282
209
->will ($ this ->returnValue ($ user ));
283
210
$ this ->userChecker ->expects ($ this ->once ())
284
211
->method ('checkPostAuth ' )->with ($ user );
285
- $ this ->tokenStorage ->expects ($ this ->once ())
286
- ->method ('setToken ' )->with ($ this ->isInstanceOf ('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken ' ));
287
212
288
213
$ listener = new SwitchUserListener ($ this ->tokenStorage , $ this ->userProvider , $ this ->userChecker , 'provider123 ' , $ this ->accessDecisionManager );
289
214
$ listener ->handle ($ this ->event );
290
- }
291
-
292
- private function getEvent ($ request )
293
- {
294
- $ event = $ this ->getMockBuilder ('Symfony\Component\HttpKernel\Event\GetResponseEvent ' )
295
- ->disableOriginalConstructor ()
296
- ->getMock ();
297
-
298
- $ event ->expects ($ this ->any ())
299
- ->method ('getRequest ' )
300
- ->will ($ this ->returnValue ($ request ));
301
-
302
- return $ event ;
303
- }
304
-
305
- private function getToken (array $ roles = array ())
306
- {
307
- $ token = $ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\Token\TokenInterface ' )->getMock ();
308
- $ token ->expects ($ this ->any ())
309
- ->method ('getRoles ' )
310
- ->will ($ this ->returnValue ($ roles ));
311
215
312
- return $ token ;
216
+ $ this ->assertSame ('page=3§ion=2 ' , $ this ->request ->server ->get ('QUERY_STRING ' ));
217
+ $ this ->assertInstanceOf ('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken ' , $ this ->tokenStorage ->getToken ());
313
218
}
314
219
}
0 commit comments