17
17
use Symfony \Component \HttpKernel \HttpKernelInterface ;
18
18
use Symfony \Component \Security \Core \Authentication \Token \Storage \TokenStorage ;
19
19
use Symfony \Component \Security \Core \Authentication \Token \UsernamePasswordToken ;
20
+ use Symfony \Component \Security \Core \Exception \UsernameNotFoundException ;
20
21
use Symfony \Component \Security \Core \Role \SwitchUserRole ;
21
22
use Symfony \Component \Security \Core \User \User ;
22
23
use Symfony \Component \Security \Http \Event \SwitchUserEvent ;
@@ -161,6 +162,7 @@ public function testExitUserDoesNotDispatchEventWithStringUser()
161
162
public function testSwitchUserIsDisallowed ()
162
163
{
163
164
$ token = new UsernamePasswordToken ('username ' , '' , 'key ' , ['ROLE_FOO ' ]);
165
+ $ user = new User ('username ' , 'password ' , []);
164
166
165
167
$ this ->tokenStorage ->setToken ($ token );
166
168
$ this ->request ->query ->set ('_switch_user ' , 'kuba ' );
@@ -169,6 +171,33 @@ public function testSwitchUserIsDisallowed()
169
171
->method ('decide ' )->with ($ token , ['ROLE_ALLOWED_TO_SWITCH ' ])
170
172
->willReturn (false );
171
173
174
+ $ this ->userProvider ->expects ($ this ->exactly (2 ))
175
+ ->method ('loadUserByUsername ' )
176
+ ->withConsecutive (['kuba ' ])
177
+ ->will ($ this ->onConsecutiveCalls ($ user , $ this ->throwException (new UsernameNotFoundException ())));
178
+
179
+ $ listener = new SwitchUserListener ($ this ->tokenStorage , $ this ->userProvider , $ this ->userChecker , 'provider123 ' , $ this ->accessDecisionManager );
180
+ $ listener ->handle ($ this ->event );
181
+ }
182
+
183
+ /**
184
+ * @expectedException \Symfony\Component\Security\Core\Exception\AccessDeniedException
185
+ */
186
+ public function testSwitchUserTurnsAuthenticationExceptionTo403 ()
187
+ {
188
+ $ token = new UsernamePasswordToken ('username ' , '' , 'key ' , ['ROLE_ALLOWED_TO_SWITCH ' ]);
189
+
190
+ $ this ->tokenStorage ->setToken ($ token );
191
+ $ this ->request ->query ->set ('_switch_user ' , 'kuba ' );
192
+
193
+ $ this ->accessDecisionManager ->expects ($ this ->never ())
194
+ ->method ('decide ' );
195
+
196
+ $ this ->userProvider ->expects ($ this ->exactly (2 ))
197
+ ->method ('loadUserByUsername ' )
198
+ ->withConsecutive (['kuba ' ], ['username ' ])
199
+ ->will ($ this ->onConsecutiveCalls ($ this ->throwException (new UsernameNotFoundException ())));
200
+
172
201
$ listener = new SwitchUserListener ($ this ->tokenStorage , $ this ->userProvider , $ this ->userChecker , 'provider123 ' , $ this ->accessDecisionManager );
173
202
$ listener ->handle ($ this ->event );
174
203
}
@@ -185,9 +214,10 @@ public function testSwitchUser()
185
214
->method ('decide ' )->with ($ token , ['ROLE_ALLOWED_TO_SWITCH ' ], $ user )
186
215
->willReturn (true );
187
216
188
- $ this ->userProvider ->expects ($ this ->once ())
189
- ->method ('loadUserByUsername ' )->with ('kuba ' )
190
- ->willReturn ($ user );
217
+ $ this ->userProvider ->expects ($ this ->exactly (2 ))
218
+ ->method ('loadUserByUsername ' )
219
+ ->withConsecutive (['kuba ' ])
220
+ ->will ($ this ->onConsecutiveCalls ($ user , $ this ->throwException (new UsernameNotFoundException ())));
191
221
$ this ->userChecker ->expects ($ this ->once ())
192
222
->method ('checkPostAuth ' )->with ($ user );
193
223
@@ -215,9 +245,10 @@ public function testSwitchUserKeepsOtherQueryStringParameters()
215
245
->method ('decide ' )->with ($ token , ['ROLE_ALLOWED_TO_SWITCH ' ], $ user )
216
246
->willReturn (true );
217
247
218
- $ this ->userProvider ->expects ($ this ->once ())
219
- ->method ('loadUserByUsername ' )->with ('kuba ' )
220
- ->willReturn ($ user );
248
+ $ this ->userProvider ->expects ($ this ->exactly (2 ))
249
+ ->method ('loadUserByUsername ' )
250
+ ->withConsecutive (['kuba ' ])
251
+ ->will ($ this ->onConsecutiveCalls ($ user , $ this ->throwException (new UsernameNotFoundException ())));
221
252
$ this ->userChecker ->expects ($ this ->once ())
222
253
->method ('checkPostAuth ' )->with ($ user );
223
254
@@ -243,9 +274,10 @@ public function testSwitchUserWithReplacedToken()
243
274
->method ('decide ' )->with ($ token , ['ROLE_ALLOWED_TO_SWITCH ' ], $ user )
244
275
->willReturn (true );
245
276
246
- $ this ->userProvider ->expects ($ this ->any ())
247
- ->method ('loadUserByUsername ' )->with ('kuba ' )
248
- ->willReturn ($ user );
277
+ $ this ->userProvider ->expects ($ this ->exactly (2 ))
278
+ ->method ('loadUserByUsername ' )
279
+ ->withConsecutive (['kuba ' ])
280
+ ->will ($ this ->onConsecutiveCalls ($ user , $ this ->throwException (new UsernameNotFoundException ())));
249
281
250
282
$ dispatcher = $ this ->getMockBuilder ('Symfony\Component\EventDispatcher\EventDispatcherInterface ' )->getMock ();
251
283
$ dispatcher
@@ -290,9 +322,10 @@ public function testSwitchUserStateless()
290
322
->method ('decide ' )->with ($ token , ['ROLE_ALLOWED_TO_SWITCH ' ], $ user )
291
323
->willReturn (true );
292
324
293
- $ this ->userProvider ->expects ($ this ->once ())
294
- ->method ('loadUserByUsername ' )->with ('kuba ' )
295
- ->willReturn ($ user );
325
+ $ this ->userProvider ->expects ($ this ->exactly (2 ))
326
+ ->method ('loadUserByUsername ' )
327
+ ->withConsecutive (['kuba ' ])
328
+ ->will ($ this ->onConsecutiveCalls ($ user , $ this ->throwException (new UsernameNotFoundException ())));
296
329
$ this ->userChecker ->expects ($ this ->once ())
297
330
->method ('checkPostAuth ' )->with ($ user );
298
331
0 commit comments