18
18
use Symfony \Component \Security \Core \Authentication \Token \Storage \TokenStorage ;
19
19
use Symfony \Component \Security \Core \Authentication \Token \SwitchUserToken ;
20
20
use Symfony \Component \Security \Core \Authentication \Token \UsernamePasswordToken ;
21
+ use Symfony \Component \Security \Core \Exception \UsernameNotFoundException ;
21
22
use Symfony \Component \Security \Core \Role \SwitchUserRole ;
22
23
use Symfony \Component \Security \Core \User \User ;
23
24
use Symfony \Component \Security \Http \Event \SwitchUserEvent ;
@@ -174,6 +175,7 @@ public function testSwitchUserIsDisallowed()
174
175
{
175
176
$ this ->expectException ('Symfony\Component\Security\Core\Exception\AccessDeniedException ' );
176
177
$ token = new UsernamePasswordToken ('username ' , '' , 'key ' , ['ROLE_FOO ' ]);
178
+ $ user = new User ('username ' , 'password ' , []);
177
179
178
180
$ this ->tokenStorage ->setToken ($ token );
179
181
$ this ->request ->query ->set ('_switch_user ' , 'kuba ' );
@@ -182,6 +184,31 @@ public function testSwitchUserIsDisallowed()
182
184
->method ('decide ' )->with ($ token , ['ROLE_ALLOWED_TO_SWITCH ' ])
183
185
->willReturn (false );
184
186
187
+ $ this ->userProvider ->expects ($ this ->exactly (2 ))
188
+ ->method ('loadUserByUsername ' )
189
+ ->withConsecutive (['kuba ' ])
190
+ ->will ($ this ->onConsecutiveCalls ($ user , $ this ->throwException (new UsernameNotFoundException ())));
191
+
192
+ $ listener = new SwitchUserListener ($ this ->tokenStorage , $ this ->userProvider , $ this ->userChecker , 'provider123 ' , $ this ->accessDecisionManager );
193
+ $ listener ($ this ->event );
194
+ }
195
+
196
+ public function testSwitchUserTurnsAuthenticationExceptionTo403 ()
197
+ {
198
+ $ this ->expectException ('Symfony\Component\Security\Core\Exception\AccessDeniedException ' );
199
+ $ token = new UsernamePasswordToken ('username ' , '' , 'key ' , ['ROLE_ALLOWED_TO_SWITCH ' ]);
200
+
201
+ $ this ->tokenStorage ->setToken ($ token );
202
+ $ this ->request ->query ->set ('_switch_user ' , 'kuba ' );
203
+
204
+ $ this ->accessDecisionManager ->expects ($ this ->never ())
205
+ ->method ('decide ' );
206
+
207
+ $ this ->userProvider ->expects ($ this ->exactly (2 ))
208
+ ->method ('loadUserByUsername ' )
209
+ ->withConsecutive (['kuba ' ], ['username ' ])
210
+ ->will ($ this ->onConsecutiveCalls ($ this ->throwException (new UsernameNotFoundException ())));
211
+
185
212
$ listener = new SwitchUserListener ($ this ->tokenStorage , $ this ->userProvider , $ this ->userChecker , 'provider123 ' , $ this ->accessDecisionManager );
186
213
$ listener ($ this ->event );
187
214
}
@@ -198,9 +225,10 @@ public function testSwitchUser()
198
225
->method ('decide ' )->with ($ token , ['ROLE_ALLOWED_TO_SWITCH ' ], $ user )
199
226
->willReturn (true );
200
227
201
- $ this ->userProvider ->expects ($ this ->once ())
202
- ->method ('loadUserByUsername ' )->with ('kuba ' )
203
- ->willReturn ($ user );
228
+ $ this ->userProvider ->expects ($ this ->exactly (2 ))
229
+ ->method ('loadUserByUsername ' )
230
+ ->withConsecutive (['kuba ' ])
231
+ ->will ($ this ->onConsecutiveCalls ($ user , $ this ->throwException (new UsernameNotFoundException ())));
204
232
$ this ->userChecker ->expects ($ this ->once ())
205
233
->method ('checkPostAuth ' )->with ($ user );
206
234
@@ -224,9 +252,10 @@ public function testSwitchUserWorksWithFalsyUsernames()
224
252
->method ('decide ' )->with ($ token , ['ROLE_ALLOWED_TO_SWITCH ' ])
225
253
->willReturn (true );
226
254
227
- $ this ->userProvider ->expects ($ this ->once ())
228
- ->method ('loadUserByUsername ' )->with ('0 ' )
229
- ->willReturn ($ user );
255
+ $ this ->userProvider ->expects ($ this ->exactly (2 ))
256
+ ->method ('loadUserByUsername ' )
257
+ ->withConsecutive (['0 ' ])
258
+ ->will ($ this ->onConsecutiveCalls ($ user , $ this ->throwException (new UsernameNotFoundException ())));
230
259
$ this ->userChecker ->expects ($ this ->once ())
231
260
->method ('checkPostAuth ' )->with ($ user );
232
261
@@ -254,9 +283,10 @@ public function testSwitchUserKeepsOtherQueryStringParameters()
254
283
->method ('decide ' )->with ($ token , ['ROLE_ALLOWED_TO_SWITCH ' ], $ user )
255
284
->willReturn (true );
256
285
257
- $ this ->userProvider ->expects ($ this ->once ())
258
- ->method ('loadUserByUsername ' )->with ('kuba ' )
259
- ->willReturn ($ user );
286
+ $ this ->userProvider ->expects ($ this ->exactly (2 ))
287
+ ->method ('loadUserByUsername ' )
288
+ ->withConsecutive (['kuba ' ])
289
+ ->will ($ this ->onConsecutiveCalls ($ user , $ this ->throwException (new UsernameNotFoundException ())));
260
290
$ this ->userChecker ->expects ($ this ->once ())
261
291
->method ('checkPostAuth ' )->with ($ user );
262
292
@@ -282,9 +312,10 @@ public function testSwitchUserWithReplacedToken()
282
312
->method ('decide ' )->with ($ token , ['ROLE_ALLOWED_TO_SWITCH ' ], $ user )
283
313
->willReturn (true );
284
314
285
- $ this ->userProvider ->expects ($ this ->any ())
286
- ->method ('loadUserByUsername ' )->with ('kuba ' )
287
- ->willReturn ($ user );
315
+ $ this ->userProvider ->expects ($ this ->exactly (2 ))
316
+ ->method ('loadUserByUsername ' )
317
+ ->withConsecutive (['kuba ' ])
318
+ ->will ($ this ->onConsecutiveCalls ($ user , $ this ->throwException (new UsernameNotFoundException ())));
288
319
289
320
$ dispatcher = $ this ->getMockBuilder (EventDispatcherInterface::class)->getMock ();
290
321
$ dispatcher
@@ -329,9 +360,10 @@ public function testSwitchUserStateless()
329
360
->method ('decide ' )->with ($ token , ['ROLE_ALLOWED_TO_SWITCH ' ], $ user )
330
361
->willReturn (true );
331
362
332
- $ this ->userProvider ->expects ($ this ->once ())
333
- ->method ('loadUserByUsername ' )->with ('kuba ' )
334
- ->willReturn ($ user );
363
+ $ this ->userProvider ->expects ($ this ->exactly (2 ))
364
+ ->method ('loadUserByUsername ' )
365
+ ->withConsecutive (['kuba ' ])
366
+ ->will ($ this ->onConsecutiveCalls ($ user , $ this ->throwException (new UsernameNotFoundException ())));
335
367
$ this ->userChecker ->expects ($ this ->once ())
336
368
->method ('checkPostAuth ' )->with ($ user );
337
369
0 commit comments