Skip to content

Commit b774505

Browse files
committed
Merge branch '5.2' into 5.x
* 5.2: [Security] Use more concrete user classes in tests fix docblock
2 parents 9b0efa5 + a4b3d12 commit b774505

File tree

3 files changed

+32
-81
lines changed

3 files changed

+32
-81
lines changed

Tests/Firewall/SwitchUserListenerTest.php

Lines changed: 30 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
2525
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
2626
use Symfony\Component\Security\Core\User\InMemoryUser;
27+
use Symfony\Component\Security\Core\User\InMemoryUserProvider;
2728
use Symfony\Component\Security\Core\User\UserCheckerInterface;
28-
use Symfony\Component\Security\Core\User\UserProviderInterface;
2929
use Symfony\Component\Security\Http\Event\SwitchUserEvent;
3030
use Symfony\Component\Security\Http\Firewall\SwitchUserListener;
3131
use Symfony\Component\Security\Http\SecurityEvents;
@@ -48,7 +48,7 @@ class SwitchUserListenerTest extends TestCase
4848
protected function setUp(): void
4949
{
5050
$this->tokenStorage = new TokenStorage();
51-
$this->userProvider = $this->createMock(UserProviderInterface::class);
51+
$this->userProvider = new InMemoryUserProvider(['kuba' => []]);
5252
$this->userChecker = $this->createMock(UserCheckerInterface::class);
5353
$this->accessDecisionManager = $this->createMock(AccessDecisionManagerInterface::class);
5454
$this->request = new Request();
@@ -113,8 +113,8 @@ public function testExitUserDispatchesEventWithRefreshedUser()
113113
{
114114
$originalUser = new InMemoryUser('username', null);
115115
$refreshedUser = new InMemoryUser('username', null);
116-
$this
117-
->userProvider
116+
$userProvider = $this->createMock(InMemoryUserProvider::class);
117+
$userProvider
118118
->expects($this->any())
119119
->method('refreshUser')
120120
->with($this->identicalTo($originalUser))
@@ -135,15 +135,15 @@ public function testExitUserDispatchesEventWithRefreshedUser()
135135
)
136136
;
137137

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);
139139
$listener($this->event);
140140
}
141141

142142
public function testExitUserDoesNotDispatchEventWithStringUser()
143143
{
144144
$originalUser = 'anon.';
145-
$this
146-
->userProvider
145+
$userProvider = $this->createMock(InMemoryUserProvider::class);
146+
$userProvider
147147
->expects($this->never())
148148
->method('refreshUser');
149149
$originalToken = new UsernamePasswordToken($originalUser, '', 'key');
@@ -156,7 +156,7 @@ public function testExitUserDoesNotDispatchEventWithStringUser()
156156
->method('dispatch')
157157
;
158158

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);
160160
$listener($this->event);
161161
}
162162

@@ -173,11 +173,6 @@ public function testSwitchUserIsDisallowed()
173173
->method('decide')->with($token, ['ROLE_ALLOWED_TO_SWITCH'])
174174
->willReturn(false);
175175

176-
$this->userProvider->expects($this->exactly(2))
177-
->method('loadUserByUsername')
178-
->withConsecutive(['kuba'])
179-
->will($this->onConsecutiveCalls($user, $this->throwException(new UsernameNotFoundException())));
180-
181176
$listener = new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager);
182177
$listener($this->event);
183178
}
@@ -188,38 +183,28 @@ public function testSwitchUserTurnsAuthenticationExceptionTo403()
188183
$token = new UsernamePasswordToken('username', '', 'key', ['ROLE_ALLOWED_TO_SWITCH']);
189184

190185
$this->tokenStorage->setToken($token);
191-
$this->request->query->set('_switch_user', 'kuba');
186+
$this->request->query->set('_switch_user', 'not-existing');
192187

193188
$this->accessDecisionManager->expects($this->never())
194189
->method('decide');
195190

196-
$this->userProvider->expects($this->exactly(2))
197-
->method('loadUserByUsername')
198-
->withConsecutive(['kuba'], ['username'])
199-
->will($this->onConsecutiveCalls($this->throwException(new UsernameNotFoundException())));
200-
201191
$listener = new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager);
202192
$listener($this->event);
203193
}
204194

205195
public function testSwitchUser()
206196
{
207197
$token = new UsernamePasswordToken('username', '', 'key', ['ROLE_FOO']);
208-
$user = new InMemoryUser('username', 'password', []);
209198

210199
$this->tokenStorage->setToken($token);
211200
$this->request->query->set('_switch_user', 'kuba');
212201

213202
$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(); }))
215204
->willReturn(true);
216205

217-
$this->userProvider->expects($this->exactly(2))
218-
->method('loadUserByUsername')
219-
->withConsecutive(['kuba'])
220-
->will($this->onConsecutiveCalls($user, $this->throwException(new UsernameNotFoundException())));
221206
$this->userChecker->expects($this->once())
222-
->method('checkPostAuth')->with($user);
207+
->method('checkPostAuth')->with($this->callback(function ($user) { return 'kuba' === $user->getUsername(); }));
223208

224209
$listener = new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager);
225210
$listener($this->event);
@@ -241,16 +226,13 @@ public function testSwitchUserAlreadySwitched()
241226

242227
$this->request->query->set('_switch_user', 'kuba');
243228

229+
$targetsUser = $this->callback(function ($user) { return 'kuba' === $user->getUsername(); });
244230
$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)
246232
->willReturn(true);
247233

248-
$this->userProvider->expects($this->exactly(2))
249-
->method('loadUserByUsername')
250-
->withConsecutive(['kuba'])
251-
->will($this->onConsecutiveCalls($targetUser, $this->throwException(new UsernameNotFoundException())));
252234
$this->userChecker->expects($this->once())
253-
->method('checkPostAuth')->with($targetUser);
235+
->method('checkPostAuth')->with($targetsUser);
254236

255237
$listener = new SwitchUserListener($tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager, null, '_switch_user', 'ROLE_ALLOWED_TO_SWITCH', null, false);
256238
$listener($this->event);
@@ -264,22 +246,19 @@ public function testSwitchUserAlreadySwitched()
264246

265247
public function testSwitchUserWorksWithFalsyUsernames()
266248
{
267-
$token = new UsernamePasswordToken('username', '', 'key', ['ROLE_FOO']);
268-
$user = new InMemoryUser('username', 'password', []);
249+
$token = new UsernamePasswordToken('kuba', '', 'key', ['ROLE_FOO']);
269250

270251
$this->tokenStorage->setToken($token);
271252
$this->request->query->set('_switch_user', '0');
272253

254+
$this->userProvider->createUser($user = new InMemoryUser('0', null));
255+
273256
$this->accessDecisionManager->expects($this->once())
274257
->method('decide')->with($token, ['ROLE_ALLOWED_TO_SWITCH'])
275258
->willReturn(true);
276259

277-
$this->userProvider->expects($this->exactly(2))
278-
->method('loadUserByUsername')
279-
->withConsecutive(['0'])
280-
->will($this->onConsecutiveCalls($user, $this->throwException(new UsernameNotFoundException())));
281260
$this->userChecker->expects($this->once())
282-
->method('checkPostAuth')->with($user);
261+
->method('checkPostAuth')->with($this->callback(function ($argUser) use ($user) { return $user->isEqualTo($argUser); }));
283262

284263
$listener = new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager);
285264
$listener($this->event);
@@ -292,7 +271,6 @@ public function testSwitchUserWorksWithFalsyUsernames()
292271
public function testSwitchUserKeepsOtherQueryStringParameters()
293272
{
294273
$token = new UsernamePasswordToken('username', '', 'key', ['ROLE_FOO']);
295-
$user = new InMemoryUser('username', 'password', []);
296274

297275
$this->tokenStorage->setToken($token);
298276
$this->request->query->replace([
@@ -301,16 +279,13 @@ public function testSwitchUserKeepsOtherQueryStringParameters()
301279
'section' => 2,
302280
]);
303281

282+
$targetsUser = $this->callback(function ($user) { return 'kuba' === $user->getUsername(); });
304283
$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)
306285
->willReturn(true);
307286

308-
$this->userProvider->expects($this->exactly(2))
309-
->method('loadUserByUsername')
310-
->withConsecutive(['kuba'])
311-
->will($this->onConsecutiveCalls($user, $this->throwException(new UsernameNotFoundException())));
312287
$this->userChecker->expects($this->once())
313-
->method('checkPostAuth')->with($user);
288+
->method('checkPostAuth')->with($targetsUser);
314289

315290
$listener = new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager);
316291
$listener($this->event);
@@ -331,21 +306,16 @@ public function testSwitchUserWithReplacedToken()
331306
$this->request->query->set('_switch_user', 'kuba');
332307

333308
$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(); }))
335310
->willReturn(true);
336311

337-
$this->userProvider->expects($this->exactly(2))
338-
->method('loadUserByUsername')
339-
->withConsecutive(['kuba'])
340-
->will($this->onConsecutiveCalls($user, $this->throwException(new UsernameNotFoundException())));
341-
342312
$dispatcher = $this->createMock(EventDispatcherInterface::class);
343313
$dispatcher
344314
->expects($this->once())
345315
->method('dispatch')
346316
->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()) {
349319
return false;
350320
}
351321
$event->setToken($replacedToken);
@@ -378,16 +348,13 @@ public function testSwitchUserStateless()
378348
$this->tokenStorage->setToken($token);
379349
$this->request->query->set('_switch_user', 'kuba');
380350

351+
$targetsUser = $this->callback(function ($user) { return 'kuba' === $user->getUsername(); });
381352
$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)
383354
->willReturn(true);
384355

385-
$this->userProvider->expects($this->exactly(2))
386-
->method('loadUserByUsername')
387-
->withConsecutive(['kuba'])
388-
->will($this->onConsecutiveCalls($user, $this->throwException(new UsernameNotFoundException())));
389356
$this->userChecker->expects($this->once())
390-
->method('checkPostAuth')->with($user);
357+
->method('checkPostAuth')->with($targetsUser);
391358

392359
$listener = new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager, null, '_switch_user', 'ROLE_ALLOWED_TO_SWITCH', null, true);
393360
$listener($this->event);
@@ -400,8 +367,8 @@ public function testSwitchUserRefreshesOriginalToken()
400367
{
401368
$originalUser = new InMemoryUser('username', null);
402369
$refreshedOriginalUser = new InMemoryUser('username', null);
403-
$this
404-
->userProvider
370+
$userProvider = $this->createMock(InMemoryUserProvider::class);
371+
$userProvider
405372
->expects($this->any())
406373
->method('refreshUser')
407374
->with($this->identicalTo($originalUser))
@@ -422,7 +389,7 @@ public function testSwitchUserRefreshesOriginalToken()
422389
)
423390
;
424391

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);
426393
$listener($this->event);
427394
}
428395
}

Tests/RememberMe/PersistentTokenBasedRememberMeServicesTest.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,7 @@ public function testLoginSuccessSetsCookieWhenLoggedInWithNonRememberMeTokenInte
267267
$request = new Request();
268268
$response = new Response();
269269

270-
$account = $this->createMock(UserInterface::class);
271-
$account
272-
->expects($this->once())
273-
->method('getUsername')
274-
->willReturn('foo')
275-
;
270+
$account = new InMemoryUser('foo', null);
276271
$token = $this->createMock(TokenInterface::class);
277272
$token
278273
->expects($this->any())

Tests/RememberMe/TokenBasedRememberMeServicesTest.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
2121
use Symfony\Component\Security\Core\User\InMemoryUser;
2222
use Symfony\Component\Security\Core\User\InMemoryUserProvider;
23-
use Symfony\Component\Security\Core\User\UserInterface;
2423
use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
2524
use Symfony\Component\Security\Http\RememberMe\TokenBasedRememberMeServices;
2625

@@ -170,18 +169,8 @@ public function testLoginSuccess()
170169
$request = new Request();
171170
$response = new Response();
172171

172+
$user = new InMemoryUser('foouser', 'foopass');
173173
$token = $this->createMock(TokenInterface::class);
174-
$user = $this->createMock(UserInterface::class);
175-
$user
176-
->expects($this->once())
177-
->method('getPassword')
178-
->willReturn('foopass')
179-
;
180-
$user
181-
->expects($this->once())
182-
->method('getUsername')
183-
->willReturn('foouser')
184-
;
185174
$token
186175
->expects($this->atLeastOnce())
187176
->method('getUser')

0 commit comments

Comments
 (0)