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 \User \User ;
22
23
use Symfony \Component \Security \Http \Event \SwitchUserEvent ;
23
24
use Symfony \Component \Security \Http \Firewall \SwitchUserListener ;
@@ -157,6 +158,7 @@ public function testSwitchUserIsDisallowed()
157
158
{
158
159
$ this ->expectException ('Symfony\Component\Security\Core\Exception\AccessDeniedException ' );
159
160
$ token = new UsernamePasswordToken ('username ' , '' , 'key ' , ['ROLE_FOO ' ]);
161
+ $ user = new User ('username ' , 'password ' , []);
160
162
161
163
$ this ->tokenStorage ->setToken ($ token );
162
164
$ this ->request ->query ->set ('_switch_user ' , 'kuba ' );
@@ -165,6 +167,31 @@ public function testSwitchUserIsDisallowed()
165
167
->method ('decide ' )->with ($ token , ['ROLE_ALLOWED_TO_SWITCH ' ])
166
168
->willReturn (false );
167
169
170
+ $ this ->userProvider ->expects ($ this ->exactly (2 ))
171
+ ->method ('loadUserByUsername ' )
172
+ ->withConsecutive (['kuba ' ])
173
+ ->will ($ this ->onConsecutiveCalls ($ user , $ this ->throwException (new UsernameNotFoundException ())));
174
+
175
+ $ listener = new SwitchUserListener ($ this ->tokenStorage , $ this ->userProvider , $ this ->userChecker , 'provider123 ' , $ this ->accessDecisionManager );
176
+ $ listener ($ this ->event );
177
+ }
178
+
179
+ public function testSwitchUserTurnsAuthenticationExceptionTo403 ()
180
+ {
181
+ $ this ->expectException ('Symfony\Component\Security\Core\Exception\AccessDeniedException ' );
182
+ $ token = new UsernamePasswordToken ('username ' , '' , 'key ' , ['ROLE_ALLOWED_TO_SWITCH ' ]);
183
+
184
+ $ this ->tokenStorage ->setToken ($ token );
185
+ $ this ->request ->query ->set ('_switch_user ' , 'kuba ' );
186
+
187
+ $ this ->accessDecisionManager ->expects ($ this ->never ())
188
+ ->method ('decide ' );
189
+
190
+ $ this ->userProvider ->expects ($ this ->exactly (2 ))
191
+ ->method ('loadUserByUsername ' )
192
+ ->withConsecutive (['kuba ' ], ['username ' ])
193
+ ->will ($ this ->onConsecutiveCalls ($ this ->throwException (new UsernameNotFoundException ())));
194
+
168
195
$ listener = new SwitchUserListener ($ this ->tokenStorage , $ this ->userProvider , $ this ->userChecker , 'provider123 ' , $ this ->accessDecisionManager );
169
196
$ listener ($ this ->event );
170
197
}
@@ -181,9 +208,10 @@ public function testSwitchUser()
181
208
->method ('decide ' )->with ($ token , ['ROLE_ALLOWED_TO_SWITCH ' ], $ user )
182
209
->willReturn (true );
183
210
184
- $ this ->userProvider ->expects ($ this ->once ())
185
- ->method ('loadUserByUsername ' )->with ('kuba ' )
186
- ->willReturn ($ user );
211
+ $ this ->userProvider ->expects ($ this ->exactly (2 ))
212
+ ->method ('loadUserByUsername ' )
213
+ ->withConsecutive (['kuba ' ])
214
+ ->will ($ this ->onConsecutiveCalls ($ user , $ this ->throwException (new UsernameNotFoundException ())));
187
215
$ this ->userChecker ->expects ($ this ->once ())
188
216
->method ('checkPostAuth ' )->with ($ user );
189
217
@@ -207,9 +235,10 @@ public function testSwitchUserWorksWithFalsyUsernames()
207
235
->method ('decide ' )->with ($ token , ['ROLE_ALLOWED_TO_SWITCH ' ])
208
236
->willReturn (true );
209
237
210
- $ this ->userProvider ->expects ($ this ->once ())
211
- ->method ('loadUserByUsername ' )->with ('0 ' )
212
- ->willReturn ($ user );
238
+ $ this ->userProvider ->expects ($ this ->exactly (2 ))
239
+ ->method ('loadUserByUsername ' )
240
+ ->withConsecutive (['0 ' ])
241
+ ->will ($ this ->onConsecutiveCalls ($ user , $ this ->throwException (new UsernameNotFoundException ())));
213
242
$ this ->userChecker ->expects ($ this ->once ())
214
243
->method ('checkPostAuth ' )->with ($ user );
215
244
@@ -237,9 +266,10 @@ public function testSwitchUserKeepsOtherQueryStringParameters()
237
266
->method ('decide ' )->with ($ token , ['ROLE_ALLOWED_TO_SWITCH ' ], $ user )
238
267
->willReturn (true );
239
268
240
- $ this ->userProvider ->expects ($ this ->once ())
241
- ->method ('loadUserByUsername ' )->with ('kuba ' )
242
- ->willReturn ($ user );
269
+ $ this ->userProvider ->expects ($ this ->exactly (2 ))
270
+ ->method ('loadUserByUsername ' )
271
+ ->withConsecutive (['kuba ' ])
272
+ ->will ($ this ->onConsecutiveCalls ($ user , $ this ->throwException (new UsernameNotFoundException ())));
243
273
$ this ->userChecker ->expects ($ this ->once ())
244
274
->method ('checkPostAuth ' )->with ($ user );
245
275
@@ -265,9 +295,10 @@ public function testSwitchUserWithReplacedToken()
265
295
->method ('decide ' )->with ($ token , ['ROLE_ALLOWED_TO_SWITCH ' ], $ user )
266
296
->willReturn (true );
267
297
268
- $ this ->userProvider ->expects ($ this ->any ())
269
- ->method ('loadUserByUsername ' )->with ('kuba ' )
270
- ->willReturn ($ user );
298
+ $ this ->userProvider ->expects ($ this ->exactly (2 ))
299
+ ->method ('loadUserByUsername ' )
300
+ ->withConsecutive (['kuba ' ])
301
+ ->will ($ this ->onConsecutiveCalls ($ user , $ this ->throwException (new UsernameNotFoundException ())));
271
302
272
303
$ dispatcher = $ this ->getMockBuilder (EventDispatcherInterface::class)->getMock ();
273
304
$ dispatcher
@@ -312,9 +343,10 @@ public function testSwitchUserStateless()
312
343
->method ('decide ' )->with ($ token , ['ROLE_ALLOWED_TO_SWITCH ' ], $ user )
313
344
->willReturn (true );
314
345
315
- $ this ->userProvider ->expects ($ this ->once ())
316
- ->method ('loadUserByUsername ' )->with ('kuba ' )
317
- ->willReturn ($ user );
346
+ $ this ->userProvider ->expects ($ this ->exactly (2 ))
347
+ ->method ('loadUserByUsername ' )
348
+ ->withConsecutive (['kuba ' ])
349
+ ->will ($ this ->onConsecutiveCalls ($ user , $ this ->throwException (new UsernameNotFoundException ())));
318
350
$ this ->userChecker ->expects ($ this ->once ())
319
351
->method ('checkPostAuth ' )->with ($ user );
320
352
0 commit comments