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