22
22
use Symfony \Component \Security \Core \Authorization \AccessDecisionManagerInterface ;
23
23
use Symfony \Component \Security \Core \Exception \AccessDeniedException ;
24
24
use Symfony \Component \Security \Core \Exception \AuthenticationCredentialsNotFoundException ;
25
- use Symfony \Component \Security \Core \Exception \ UsernameNotFoundException ;
25
+ use Symfony \Component \Security \Core \User \ InMemoryUserProvider ;
26
26
use Symfony \Component \Security \Core \User \User ;
27
27
use Symfony \Component \Security \Core \User \UserCheckerInterface ;
28
- use Symfony \Component \Security \Core \User \UserProviderInterface ;
29
28
use Symfony \Component \Security \Http \Event \SwitchUserEvent ;
30
29
use Symfony \Component \Security \Http \Firewall \SwitchUserListener ;
31
30
use Symfony \Component \Security \Http \SecurityEvents ;
@@ -48,7 +47,7 @@ class SwitchUserListenerTest extends TestCase
48
47
protected function setUp (): void
49
48
{
50
49
$ this ->tokenStorage = new TokenStorage ();
51
- $ this ->userProvider = $ this -> createMock (UserProviderInterface::class );
50
+ $ this ->userProvider = new InMemoryUserProvider ([ ' kuba ' => []] );
52
51
$ this ->userChecker = $ this ->createMock (UserCheckerInterface::class);
53
52
$ this ->accessDecisionManager = $ this ->createMock (AccessDecisionManagerInterface::class);
54
53
$ this ->request = new Request ();
@@ -113,8 +112,8 @@ public function testExitUserDispatchesEventWithRefreshedUser()
113
112
{
114
113
$ originalUser = new User ('username ' , null );
115
114
$ refreshedUser = new User ('username ' , null );
116
- $ this
117
- -> userProvider
115
+ $ userProvider = $ this -> createMock (InMemoryUserProvider::class);
116
+ $ userProvider
118
117
->expects ($ this ->any ())
119
118
->method ('refreshUser ' )
120
119
->with ($ this ->identicalTo ($ originalUser ))
@@ -135,15 +134,15 @@ public function testExitUserDispatchesEventWithRefreshedUser()
135
134
)
136
135
;
137
136
138
- $ listener = new SwitchUserListener ($ this ->tokenStorage , $ this -> userProvider , $ this ->userChecker , 'provider123 ' , $ this ->accessDecisionManager , null , '_switch_user ' , 'ROLE_ALLOWED_TO_SWITCH ' , $ dispatcher );
137
+ $ listener = new SwitchUserListener ($ this ->tokenStorage , $ userProvider , $ this ->userChecker , 'provider123 ' , $ this ->accessDecisionManager , null , '_switch_user ' , 'ROLE_ALLOWED_TO_SWITCH ' , $ dispatcher );
139
138
$ listener ($ this ->event );
140
139
}
141
140
142
141
public function testExitUserDoesNotDispatchEventWithStringUser ()
143
142
{
144
143
$ originalUser = 'anon. ' ;
145
- $ this
146
- -> userProvider
144
+ $ userProvider = $ this -> createMock (InMemoryUserProvider::class);
145
+ $ userProvider
147
146
->expects ($ this ->never ())
148
147
->method ('refreshUser ' );
149
148
$ originalToken = new UsernamePasswordToken ($ originalUser , '' , 'key ' );
@@ -156,7 +155,7 @@ public function testExitUserDoesNotDispatchEventWithStringUser()
156
155
->method ('dispatch ' )
157
156
;
158
157
159
- $ listener = new SwitchUserListener ($ this ->tokenStorage , $ this -> userProvider , $ this ->userChecker , 'provider123 ' , $ this ->accessDecisionManager , null , '_switch_user ' , 'ROLE_ALLOWED_TO_SWITCH ' , $ dispatcher );
158
+ $ listener = new SwitchUserListener ($ this ->tokenStorage , $ userProvider , $ this ->userChecker , 'provider123 ' , $ this ->accessDecisionManager , null , '_switch_user ' , 'ROLE_ALLOWED_TO_SWITCH ' , $ dispatcher );
160
159
$ listener ($ this ->event );
161
160
}
162
161
@@ -173,11 +172,6 @@ public function testSwitchUserIsDisallowed()
173
172
->method ('decide ' )->with ($ token , ['ROLE_ALLOWED_TO_SWITCH ' ])
174
173
->willReturn (false );
175
174
176
- $ this ->userProvider ->expects ($ this ->exactly (2 ))
177
- ->method ('loadUserByUsername ' )
178
- ->withConsecutive (['kuba ' ])
179
- ->will ($ this ->onConsecutiveCalls ($ user , $ this ->throwException (new UsernameNotFoundException ())));
180
-
181
175
$ listener = new SwitchUserListener ($ this ->tokenStorage , $ this ->userProvider , $ this ->userChecker , 'provider123 ' , $ this ->accessDecisionManager );
182
176
$ listener ($ this ->event );
183
177
}
@@ -188,38 +182,28 @@ public function testSwitchUserTurnsAuthenticationExceptionTo403()
188
182
$ token = new UsernamePasswordToken ('username ' , '' , 'key ' , ['ROLE_ALLOWED_TO_SWITCH ' ]);
189
183
190
184
$ this ->tokenStorage ->setToken ($ token );
191
- $ this ->request ->query ->set ('_switch_user ' , 'kuba ' );
185
+ $ this ->request ->query ->set ('_switch_user ' , 'not-existing ' );
192
186
193
187
$ this ->accessDecisionManager ->expects ($ this ->never ())
194
188
->method ('decide ' );
195
189
196
- $ this ->userProvider ->expects ($ this ->exactly (2 ))
197
- ->method ('loadUserByUsername ' )
198
- ->withConsecutive (['kuba ' ], ['username ' ])
199
- ->will ($ this ->onConsecutiveCalls ($ this ->throwException (new UsernameNotFoundException ())));
200
-
201
190
$ listener = new SwitchUserListener ($ this ->tokenStorage , $ this ->userProvider , $ this ->userChecker , 'provider123 ' , $ this ->accessDecisionManager );
202
191
$ listener ($ this ->event );
203
192
}
204
193
205
194
public function testSwitchUser ()
206
195
{
207
196
$ token = new UsernamePasswordToken ('username ' , '' , 'key ' , ['ROLE_FOO ' ]);
208
- $ user = new User ('username ' , 'password ' , []);
209
197
210
198
$ this ->tokenStorage ->setToken ($ token );
211
199
$ this ->request ->query ->set ('_switch_user ' , 'kuba ' );
212
200
213
201
$ this ->accessDecisionManager ->expects ($ this ->once ())
214
- ->method ('decide ' )->with ($ token , ['ROLE_ALLOWED_TO_SWITCH ' ], $ user )
202
+ ->method ('decide ' )->with ($ token , ['ROLE_ALLOWED_TO_SWITCH ' ], $ this -> callback ( function ( $ user) { return ' kuba ' === $ user -> getUsername (); }) )
215
203
->willReturn (true );
216
204
217
- $ this ->userProvider ->expects ($ this ->exactly (2 ))
218
- ->method ('loadUserByUsername ' )
219
- ->withConsecutive (['kuba ' ])
220
- ->will ($ this ->onConsecutiveCalls ($ user , $ this ->throwException (new UsernameNotFoundException ())));
221
205
$ this ->userChecker ->expects ($ this ->once ())
222
- ->method ('checkPostAuth ' )->with ($ user );
206
+ ->method ('checkPostAuth ' )->with ($ this -> callback ( function ( $ user) { return ' kuba ' === $ user -> getUsername (); }) );
223
207
224
208
$ listener = new SwitchUserListener ($ this ->tokenStorage , $ this ->userProvider , $ this ->userChecker , 'provider123 ' , $ this ->accessDecisionManager );
225
209
$ listener ($ this ->event );
@@ -237,20 +221,15 @@ public function testSwitchUserAlreadySwitched()
237
221
$ tokenStorage = new TokenStorage ();
238
222
$ tokenStorage ->setToken ($ alreadySwitchedToken );
239
223
240
- $ targetUser = new User ('kuba ' , 'password ' , ['ROLE_FOO ' , 'ROLE_BAR ' ]);
241
-
242
224
$ this ->request ->query ->set ('_switch_user ' , 'kuba ' );
243
225
226
+ $ targetsUser = $ this ->callback (function ($ user ) { return 'kuba ' === $ user ->getUsername (); });
244
227
$ this ->accessDecisionManager ->expects ($ this ->once ())
245
- ->method ('decide ' )->with ($ originalToken , ['ROLE_ALLOWED_TO_SWITCH ' ], $ targetUser )
228
+ ->method ('decide ' )->with ($ originalToken , ['ROLE_ALLOWED_TO_SWITCH ' ], $ targetsUser )
246
229
->willReturn (true );
247
230
248
- $ this ->userProvider ->expects ($ this ->exactly (2 ))
249
- ->method ('loadUserByUsername ' )
250
- ->withConsecutive (['kuba ' ])
251
- ->will ($ this ->onConsecutiveCalls ($ targetUser , $ this ->throwException (new UsernameNotFoundException ())));
252
231
$ this ->userChecker ->expects ($ this ->once ())
253
- ->method ('checkPostAuth ' )->with ($ targetUser );
232
+ ->method ('checkPostAuth ' )->with ($ targetsUser );
254
233
255
234
$ listener = new SwitchUserListener ($ tokenStorage , $ this ->userProvider , $ this ->userChecker , 'provider123 ' , $ this ->accessDecisionManager , null , '_switch_user ' , 'ROLE_ALLOWED_TO_SWITCH ' , null , false );
256
235
$ listener ($ this ->event );
@@ -264,22 +243,19 @@ public function testSwitchUserAlreadySwitched()
264
243
265
244
public function testSwitchUserWorksWithFalsyUsernames ()
266
245
{
267
- $ token = new UsernamePasswordToken ('username ' , '' , 'key ' , ['ROLE_FOO ' ]);
268
- $ user = new User ('username ' , 'password ' , []);
246
+ $ token = new UsernamePasswordToken ('kuba ' , '' , 'key ' , ['ROLE_FOO ' ]);
269
247
270
248
$ this ->tokenStorage ->setToken ($ token );
271
249
$ this ->request ->query ->set ('_switch_user ' , '0 ' );
272
250
251
+ $ this ->userProvider ->createUser ($ user = new User ('0 ' , null ));
252
+
273
253
$ this ->accessDecisionManager ->expects ($ this ->once ())
274
254
->method ('decide ' )->with ($ token , ['ROLE_ALLOWED_TO_SWITCH ' ])
275
255
->willReturn (true );
276
256
277
- $ this ->userProvider ->expects ($ this ->exactly (2 ))
278
- ->method ('loadUserByUsername ' )
279
- ->withConsecutive (['0 ' ])
280
- ->will ($ this ->onConsecutiveCalls ($ user , $ this ->throwException (new UsernameNotFoundException ())));
281
257
$ this ->userChecker ->expects ($ this ->once ())
282
- ->method ('checkPostAuth ' )->with ($ user );
258
+ ->method ('checkPostAuth ' )->with ($ this -> callback ( function ( $ argUser ) use ( $ user) { return $ user -> isEqualTo ( $ argUser ); }) );
283
259
284
260
$ listener = new SwitchUserListener ($ this ->tokenStorage , $ this ->userProvider , $ this ->userChecker , 'provider123 ' , $ this ->accessDecisionManager );
285
261
$ listener ($ this ->event );
@@ -292,7 +268,6 @@ public function testSwitchUserWorksWithFalsyUsernames()
292
268
public function testSwitchUserKeepsOtherQueryStringParameters ()
293
269
{
294
270
$ token = new UsernamePasswordToken ('username ' , '' , 'key ' , ['ROLE_FOO ' ]);
295
- $ user = new User ('username ' , 'password ' , []);
296
271
297
272
$ this ->tokenStorage ->setToken ($ token );
298
273
$ this ->request ->query ->replace ([
@@ -301,16 +276,13 @@ public function testSwitchUserKeepsOtherQueryStringParameters()
301
276
'section ' => 2 ,
302
277
]);
303
278
279
+ $ targetsUser = $ this ->callback (function ($ user ) { return 'kuba ' === $ user ->getUsername (); });
304
280
$ this ->accessDecisionManager ->expects ($ this ->once ())
305
- ->method ('decide ' )->with ($ token , ['ROLE_ALLOWED_TO_SWITCH ' ], $ user )
281
+ ->method ('decide ' )->with ($ token , ['ROLE_ALLOWED_TO_SWITCH ' ], $ targetsUser )
306
282
->willReturn (true );
307
283
308
- $ this ->userProvider ->expects ($ this ->exactly (2 ))
309
- ->method ('loadUserByUsername ' )
310
- ->withConsecutive (['kuba ' ])
311
- ->will ($ this ->onConsecutiveCalls ($ user , $ this ->throwException (new UsernameNotFoundException ())));
312
284
$ this ->userChecker ->expects ($ this ->once ())
313
- ->method ('checkPostAuth ' )->with ($ user );
285
+ ->method ('checkPostAuth ' )->with ($ targetsUser );
314
286
315
287
$ listener = new SwitchUserListener ($ this ->tokenStorage , $ this ->userProvider , $ this ->userChecker , 'provider123 ' , $ this ->accessDecisionManager );
316
288
$ listener ($ this ->event );
@@ -331,21 +303,16 @@ public function testSwitchUserWithReplacedToken()
331
303
$ this ->request ->query ->set ('_switch_user ' , 'kuba ' );
332
304
333
305
$ this ->accessDecisionManager ->expects ($ this ->any ())
334
- ->method ('decide ' )->with ($ token , ['ROLE_ALLOWED_TO_SWITCH ' ], $ user )
306
+ ->method ('decide ' )->with ($ token , ['ROLE_ALLOWED_TO_SWITCH ' ], $ this -> callback ( function ( $ user) { return ' kuba ' === $ user -> getUsername (); }) )
335
307
->willReturn (true );
336
308
337
- $ this ->userProvider ->expects ($ this ->exactly (2 ))
338
- ->method ('loadUserByUsername ' )
339
- ->withConsecutive (['kuba ' ])
340
- ->will ($ this ->onConsecutiveCalls ($ user , $ this ->throwException (new UsernameNotFoundException ())));
341
-
342
309
$ dispatcher = $ this ->createMock (EventDispatcherInterface::class);
343
310
$ dispatcher
344
311
->expects ($ this ->once ())
345
312
->method ('dispatch ' )
346
313
->with (
347
- $ this ->callback (function (SwitchUserEvent $ event ) use ($ replacedToken, $ user ) {
348
- if ($ user !== $ event ->getTargetUser ()) {
314
+ $ this ->callback (function (SwitchUserEvent $ event ) use ($ replacedToken ) {
315
+ if (' kuba ' !== $ event ->getTargetUser ()-> getUsername ()) {
349
316
return false ;
350
317
}
351
318
$ event ->setToken ($ replacedToken );
@@ -373,21 +340,17 @@ public function testSwitchUserThrowsAuthenticationExceptionIfNoCurrentToken()
373
340
public function testSwitchUserStateless ()
374
341
{
375
342
$ token = new UsernamePasswordToken ('username ' , '' , 'key ' , ['ROLE_FOO ' ]);
376
- $ user = new User ('username ' , 'password ' , []);
377
343
378
344
$ this ->tokenStorage ->setToken ($ token );
379
345
$ this ->request ->query ->set ('_switch_user ' , 'kuba ' );
380
346
347
+ $ targetsUser = $ this ->callback (function ($ user ) { return 'kuba ' === $ user ->getUsername (); });
381
348
$ this ->accessDecisionManager ->expects ($ this ->once ())
382
- ->method ('decide ' )->with ($ token , ['ROLE_ALLOWED_TO_SWITCH ' ], $ user )
349
+ ->method ('decide ' )->with ($ token , ['ROLE_ALLOWED_TO_SWITCH ' ], $ targetsUser )
383
350
->willReturn (true );
384
351
385
- $ this ->userProvider ->expects ($ this ->exactly (2 ))
386
- ->method ('loadUserByUsername ' )
387
- ->withConsecutive (['kuba ' ])
388
- ->will ($ this ->onConsecutiveCalls ($ user , $ this ->throwException (new UsernameNotFoundException ())));
389
352
$ this ->userChecker ->expects ($ this ->once ())
390
- ->method ('checkPostAuth ' )->with ($ user );
353
+ ->method ('checkPostAuth ' )->with ($ targetsUser );
391
354
392
355
$ listener = new SwitchUserListener ($ this ->tokenStorage , $ this ->userProvider , $ this ->userChecker , 'provider123 ' , $ this ->accessDecisionManager , null , '_switch_user ' , 'ROLE_ALLOWED_TO_SWITCH ' , null , true );
393
356
$ listener ($ this ->event );
@@ -400,8 +363,8 @@ public function testSwitchUserRefreshesOriginalToken()
400
363
{
401
364
$ originalUser = new User ('username ' , null );
402
365
$ refreshedOriginalUser = new User ('username ' , null );
403
- $ this
404
- -> userProvider
366
+ $ userProvider = $ this -> createMock (InMemoryUserProvider::class);
367
+ $ userProvider
405
368
->expects ($ this ->any ())
406
369
->method ('refreshUser ' )
407
370
->with ($ this ->identicalTo ($ originalUser ))
@@ -422,7 +385,7 @@ public function testSwitchUserRefreshesOriginalToken()
422
385
)
423
386
;
424
387
425
- $ listener = new SwitchUserListener ($ this ->tokenStorage , $ this -> userProvider , $ this ->userChecker , 'provider123 ' , $ this ->accessDecisionManager , null , '_switch_user ' , 'ROLE_ALLOWED_TO_SWITCH ' , $ dispatcher );
388
+ $ listener = new SwitchUserListener ($ this ->tokenStorage , $ userProvider , $ this ->userChecker , 'provider123 ' , $ this ->accessDecisionManager , null , '_switch_user ' , 'ROLE_ALLOWED_TO_SWITCH ' , $ dispatcher );
426
389
$ listener ($ this ->event );
427
390
}
428
391
}
0 commit comments