1818use Symfony \Component \Security \Core \Authentication \Token \Storage \TokenStorage ;
1919use Symfony \Component \Security \Core \Authentication \Token \SwitchUserToken ;
2020use Symfony \Component \Security \Core \Authentication \Token \UsernamePasswordToken ;
21+ use Symfony \Component \Security \Core \Exception \UsernameNotFoundException ;
2122use Symfony \Component \Security \Core \Role \SwitchUserRole ;
2223use Symfony \Component \Security \Core \User \User ;
2324use Symfony \Component \Security \Http \Event \SwitchUserEvent ;
@@ -174,6 +175,7 @@ public function testSwitchUserIsDisallowed()
174175 {
175176 $ this ->expectException ('Symfony\Component\Security\Core\Exception\AccessDeniedException ' );
176177 $ token = new UsernamePasswordToken ('username ' , '' , 'key ' , ['ROLE_FOO ' ]);
178+ $ user = new User ('username ' , 'password ' , []);
177179
178180 $ this ->tokenStorage ->setToken ($ token );
179181 $ this ->request ->query ->set ('_switch_user ' , 'kuba ' );
@@ -182,6 +184,31 @@ public function testSwitchUserIsDisallowed()
182184 ->method ('decide ' )->with ($ token , ['ROLE_ALLOWED_TO_SWITCH ' ])
183185 ->willReturn (false );
184186
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+
185212 $ listener = new SwitchUserListener ($ this ->tokenStorage , $ this ->userProvider , $ this ->userChecker , 'provider123 ' , $ this ->accessDecisionManager );
186213 $ listener ($ this ->event );
187214 }
@@ -198,9 +225,10 @@ public function testSwitchUser()
198225 ->method ('decide ' )->with ($ token , ['ROLE_ALLOWED_TO_SWITCH ' ], $ user )
199226 ->willReturn (true );
200227
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 ())));
204232 $ this ->userChecker ->expects ($ this ->once ())
205233 ->method ('checkPostAuth ' )->with ($ user );
206234
@@ -224,9 +252,10 @@ public function testSwitchUserWorksWithFalsyUsernames()
224252 ->method ('decide ' )->with ($ token , ['ROLE_ALLOWED_TO_SWITCH ' ])
225253 ->willReturn (true );
226254
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 ())));
230259 $ this ->userChecker ->expects ($ this ->once ())
231260 ->method ('checkPostAuth ' )->with ($ user );
232261
@@ -254,9 +283,10 @@ public function testSwitchUserKeepsOtherQueryStringParameters()
254283 ->method ('decide ' )->with ($ token , ['ROLE_ALLOWED_TO_SWITCH ' ], $ user )
255284 ->willReturn (true );
256285
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 ())));
260290 $ this ->userChecker ->expects ($ this ->once ())
261291 ->method ('checkPostAuth ' )->with ($ user );
262292
@@ -282,9 +312,10 @@ public function testSwitchUserWithReplacedToken()
282312 ->method ('decide ' )->with ($ token , ['ROLE_ALLOWED_TO_SWITCH ' ], $ user )
283313 ->willReturn (true );
284314
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 ())));
288319
289320 $ dispatcher = $ this ->getMockBuilder (EventDispatcherInterface::class)->getMock ();
290321 $ dispatcher
@@ -329,9 +360,10 @@ public function testSwitchUserStateless()
329360 ->method ('decide ' )->with ($ token , ['ROLE_ALLOWED_TO_SWITCH ' ], $ user )
330361 ->willReturn (true );
331362
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 ())));
335367 $ this ->userChecker ->expects ($ this ->once ())
336368 ->method ('checkPostAuth ' )->with ($ user );
337369
0 commit comments