minor #40609 [Security] Use concrete UserInterface and UserProviderInterface classes in the tests (wouterj)
This PR was merged into the 5.2 branch.
Discussion
----------
[Security] Use concrete UserInterface and UserProviderInterface classes in the tests
| Q | A
| ------------- | ---
| Branch? | 5.2
| Bug fix? | no
| New feature? | no
| Deprecations? | no
| Tickets | -
| License | MIT
| Doc PR | n/a
Fixes failing deps=high builds in #40403
Commits
-------
89d9de2077
Use concrete user related classes in the tests
This commit is contained in:
commit
6e206bc8aa
@ -11,9 +11,14 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\Security\Http\Tests\Authenticator\Fixtures;
|
namespace Symfony\Component\Security\Http\Tests\Authenticator\Fixtures;
|
||||||
|
|
||||||
|
use Symfony\Component\Security\Core\User\InMemoryUserProvider;
|
||||||
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
|
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
|
||||||
|
use Symfony\Component\Security\Core\User\UserInterface;
|
||||||
use Symfony\Component\Security\Core\User\UserProviderInterface;
|
use Symfony\Component\Security\Core\User\UserProviderInterface;
|
||||||
|
|
||||||
abstract class PasswordUpgraderProvider implements UserProviderInterface, PasswordUpgraderInterface
|
class PasswordUpgraderProvider extends InMemoryUserProvider implements PasswordUpgraderInterface
|
||||||
{
|
{
|
||||||
|
public function upgradePassword(UserInterface $user, string $newEncodedPassword): void
|
||||||
|
{
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,8 +17,7 @@ use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
|||||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||||
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
|
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
|
||||||
use Symfony\Component\Security\Core\Security;
|
use Symfony\Component\Security\Core\Security;
|
||||||
use Symfony\Component\Security\Core\User\User;
|
use Symfony\Component\Security\Core\User\InMemoryUserProvider;
|
||||||
use Symfony\Component\Security\Core\User\UserProviderInterface;
|
|
||||||
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
|
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
|
||||||
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
|
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
|
||||||
use Symfony\Component\Security\Http\Authenticator\FormLoginAuthenticator;
|
use Symfony\Component\Security\Http\Authenticator\FormLoginAuthenticator;
|
||||||
@ -37,8 +36,7 @@ class FormLoginAuthenticatorTest extends TestCase
|
|||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
$this->userProvider = $this->createMock(UserProviderInterface::class);
|
$this->userProvider = new InMemoryUserProvider(['test' => ['password' => 's$cr$t']]);
|
||||||
$this->userProvider->expects($this->any())->method('loadUserByUsername')->willReturn(new User('test', 's$cr$t'));
|
|
||||||
$this->successHandler = $this->createMock(AuthenticationSuccessHandlerInterface::class);
|
$this->successHandler = $this->createMock(AuthenticationSuccessHandlerInterface::class);
|
||||||
$this->failureHandler = $this->createMock(AuthenticationFailureHandlerInterface::class);
|
$this->failureHandler = $this->createMock(AuthenticationFailureHandlerInterface::class);
|
||||||
}
|
}
|
||||||
@ -149,8 +147,7 @@ class FormLoginAuthenticatorTest extends TestCase
|
|||||||
$request = Request::create('/login_check', 'POST', ['_username' => 'wouter', '_password' => 's$cr$t']);
|
$request = Request::create('/login_check', 'POST', ['_username' => 'wouter', '_password' => 's$cr$t']);
|
||||||
$request->setSession($this->createSession());
|
$request->setSession($this->createSession());
|
||||||
|
|
||||||
$this->userProvider = $this->createMock(PasswordUpgraderProvider::class);
|
$this->userProvider = new PasswordUpgraderProvider(['test' => ['password' => 's$cr$t']]);
|
||||||
$this->userProvider->expects($this->any())->method('loadUserByUsername')->willReturn(new User('test', 's$cr$t'));
|
|
||||||
|
|
||||||
$this->setUpAuthenticator();
|
$this->setUpAuthenticator();
|
||||||
$passport = $this->authenticator->authenticate($request);
|
$passport = $this->authenticator->authenticate($request);
|
||||||
|
@ -6,8 +6,8 @@ use PHPUnit\Framework\TestCase;
|
|||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
|
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
|
||||||
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
|
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
|
||||||
|
use Symfony\Component\Security\Core\User\InMemoryUserProvider;
|
||||||
use Symfony\Component\Security\Core\User\User;
|
use Symfony\Component\Security\Core\User\User;
|
||||||
use Symfony\Component\Security\Core\User\UserProviderInterface;
|
|
||||||
use Symfony\Component\Security\Http\Authenticator\HttpBasicAuthenticator;
|
use Symfony\Component\Security\Http\Authenticator\HttpBasicAuthenticator;
|
||||||
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\PasswordUpgradeBadge;
|
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\PasswordUpgradeBadge;
|
||||||
use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
|
use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
|
||||||
@ -22,7 +22,7 @@ class HttpBasicAuthenticatorTest extends TestCase
|
|||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
$this->userProvider = $this->createMock(UserProviderInterface::class);
|
$this->userProvider = new InMemoryUserProvider();
|
||||||
$this->encoderFactory = $this->createMock(EncoderFactoryInterface::class);
|
$this->encoderFactory = $this->createMock(EncoderFactoryInterface::class);
|
||||||
$this->encoder = $this->createMock(PasswordEncoderInterface::class);
|
$this->encoder = $this->createMock(PasswordEncoderInterface::class);
|
||||||
$this->encoderFactory
|
$this->encoderFactory
|
||||||
@ -40,16 +40,12 @@ class HttpBasicAuthenticatorTest extends TestCase
|
|||||||
'PHP_AUTH_PW' => 'ThePassword',
|
'PHP_AUTH_PW' => 'ThePassword',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->userProvider
|
$this->userProvider->createUser($user = new User('TheUsername', 'ThePassword'));
|
||||||
->expects($this->any())
|
|
||||||
->method('loadUserByUsername')
|
|
||||||
->with('TheUsername')
|
|
||||||
->willReturn($user = new User('TheUsername', 'ThePassword'));
|
|
||||||
|
|
||||||
$passport = $this->authenticator->authenticate($request);
|
$passport = $this->authenticator->authenticate($request);
|
||||||
$this->assertEquals('ThePassword', $passport->getBadge(PasswordCredentials::class)->getPassword());
|
$this->assertEquals('ThePassword', $passport->getBadge(PasswordCredentials::class)->getPassword());
|
||||||
|
|
||||||
$this->assertSame($user, $passport->getUser());
|
$this->assertTrue($user->isEqualTo($passport->getUser()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -77,8 +73,7 @@ class HttpBasicAuthenticatorTest extends TestCase
|
|||||||
'PHP_AUTH_PW' => 'ThePassword',
|
'PHP_AUTH_PW' => 'ThePassword',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->userProvider = $this->createMock(PasswordUpgraderProvider::class);
|
$this->userProvider = new PasswordUpgraderProvider(['test' => ['password' => 's$cr$t']]);
|
||||||
$this->userProvider->expects($this->any())->method('loadUserByUsername')->willReturn(new User('test', 's$cr$t'));
|
|
||||||
$authenticator = new HttpBasicAuthenticator('test', $this->userProvider);
|
$authenticator = new HttpBasicAuthenticator('test', $this->userProvider);
|
||||||
|
|
||||||
$passport = $authenticator->authenticate($request);
|
$passport = $authenticator->authenticate($request);
|
||||||
|
@ -14,8 +14,8 @@ namespace Symfony\Component\Security\Http\Tests\Authenticator;
|
|||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
|
||||||
|
use Symfony\Component\Security\Core\User\InMemoryUserProvider;
|
||||||
use Symfony\Component\Security\Core\User\User;
|
use Symfony\Component\Security\Core\User\User;
|
||||||
use Symfony\Component\Security\Core\User\UserProviderInterface;
|
|
||||||
use Symfony\Component\Security\Http\Authenticator\RemoteUserAuthenticator;
|
use Symfony\Component\Security\Http\Authenticator\RemoteUserAuthenticator;
|
||||||
|
|
||||||
class RemoteUserAuthenticatorTest extends TestCase
|
class RemoteUserAuthenticatorTest extends TestCase
|
||||||
@ -23,7 +23,7 @@ class RemoteUserAuthenticatorTest extends TestCase
|
|||||||
/**
|
/**
|
||||||
* @dataProvider provideAuthenticators
|
* @dataProvider provideAuthenticators
|
||||||
*/
|
*/
|
||||||
public function testSupport(UserProviderInterface $userProvider, RemoteUserAuthenticator $authenticator, $parameterName)
|
public function testSupport(InMemoryUserProvider $userProvider, RemoteUserAuthenticator $authenticator, $parameterName)
|
||||||
{
|
{
|
||||||
$request = $this->createRequest([$parameterName => 'TheUsername']);
|
$request = $this->createRequest([$parameterName => 'TheUsername']);
|
||||||
|
|
||||||
@ -32,7 +32,7 @@ class RemoteUserAuthenticatorTest extends TestCase
|
|||||||
|
|
||||||
public function testSupportNoUser()
|
public function testSupportNoUser()
|
||||||
{
|
{
|
||||||
$authenticator = new RemoteUserAuthenticator($this->createMock(UserProviderInterface::class), new TokenStorage(), 'main');
|
$authenticator = new RemoteUserAuthenticator(new InMemoryUserProvider(), new TokenStorage(), 'main');
|
||||||
|
|
||||||
$this->assertFalse($authenticator->supports($this->createRequest([])));
|
$this->assertFalse($authenticator->supports($this->createRequest([])));
|
||||||
}
|
}
|
||||||
@ -40,27 +40,24 @@ class RemoteUserAuthenticatorTest extends TestCase
|
|||||||
/**
|
/**
|
||||||
* @dataProvider provideAuthenticators
|
* @dataProvider provideAuthenticators
|
||||||
*/
|
*/
|
||||||
public function testAuthenticate(UserProviderInterface $userProvider, RemoteUserAuthenticator $authenticator, $parameterName)
|
public function testAuthenticate(InMemoryUserProvider $userProvider, RemoteUserAuthenticator $authenticator, $parameterName)
|
||||||
{
|
{
|
||||||
$request = $this->createRequest([$parameterName => 'TheUsername']);
|
$request = $this->createRequest([$parameterName => 'TheUsername']);
|
||||||
|
|
||||||
$authenticator->supports($request);
|
$authenticator->supports($request);
|
||||||
|
|
||||||
$userProvider->expects($this->once())
|
$userProvider->createUser($user = new User('TheUsername', null));
|
||||||
->method('loadUserByUsername')
|
|
||||||
->with('TheUsername')
|
|
||||||
->willReturn($user = new User('TheUsername', null));
|
|
||||||
|
|
||||||
$passport = $authenticator->authenticate($request);
|
$passport = $authenticator->authenticate($request);
|
||||||
$this->assertEquals($user, $passport->getUser());
|
$this->assertTrue($user->isEqualTo($passport->getUser()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function provideAuthenticators()
|
public function provideAuthenticators()
|
||||||
{
|
{
|
||||||
$userProvider = $this->createMock(UserProviderInterface::class);
|
$userProvider = new InMemoryUserProvider();
|
||||||
yield [$userProvider, new RemoteUserAuthenticator($userProvider, new TokenStorage(), 'main'), 'REMOTE_USER'];
|
yield [$userProvider, new RemoteUserAuthenticator($userProvider, new TokenStorage(), 'main'), 'REMOTE_USER'];
|
||||||
|
|
||||||
$userProvider = $this->createMock(UserProviderInterface::class);
|
$userProvider = new InMemoryUserProvider();
|
||||||
yield [$userProvider, new RemoteUserAuthenticator($userProvider, new TokenStorage(), 'main', 'CUSTOM_USER_PARAMETER'), 'CUSTOM_USER_PARAMETER'];
|
yield [$userProvider, new RemoteUserAuthenticator($userProvider, new TokenStorage(), 'main', 'CUSTOM_USER_PARAMETER'), 'CUSTOM_USER_PARAMETER'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,8 +14,8 @@ namespace Symfony\Component\Security\Http\Tests\Authenticator;
|
|||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
|
||||||
|
use Symfony\Component\Security\Core\User\InMemoryUserProvider;
|
||||||
use Symfony\Component\Security\Core\User\User;
|
use Symfony\Component\Security\Core\User\User;
|
||||||
use Symfony\Component\Security\Core\User\UserProviderInterface;
|
|
||||||
use Symfony\Component\Security\Http\Authenticator\X509Authenticator;
|
use Symfony\Component\Security\Http\Authenticator\X509Authenticator;
|
||||||
|
|
||||||
class X509AuthenticatorTest extends TestCase
|
class X509AuthenticatorTest extends TestCase
|
||||||
@ -25,7 +25,7 @@ class X509AuthenticatorTest extends TestCase
|
|||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
$this->userProvider = $this->createMock(UserProviderInterface::class);
|
$this->userProvider = new InMemoryUserProvider();
|
||||||
$this->authenticator = new X509Authenticator($this->userProvider, new TokenStorage(), 'main');
|
$this->authenticator = new X509Authenticator($this->userProvider, new TokenStorage(), 'main');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,10 +45,7 @@ class X509AuthenticatorTest extends TestCase
|
|||||||
$request = $this->createRequest($serverVars);
|
$request = $this->createRequest($serverVars);
|
||||||
$this->assertTrue($this->authenticator->supports($request));
|
$this->assertTrue($this->authenticator->supports($request));
|
||||||
|
|
||||||
$this->userProvider->expects($this->any())
|
$this->userProvider->createUser(new User($username, null));
|
||||||
->method('loadUserByUsername')
|
|
||||||
->with($username)
|
|
||||||
->willReturn(new User($username, null));
|
|
||||||
|
|
||||||
$passport = $this->authenticator->authenticate($request);
|
$passport = $this->authenticator->authenticate($request);
|
||||||
$this->assertEquals($username, $passport->getUser()->getUsername());
|
$this->assertEquals($username, $passport->getUser()->getUsername());
|
||||||
@ -69,10 +66,7 @@ class X509AuthenticatorTest extends TestCase
|
|||||||
|
|
||||||
$this->assertTrue($this->authenticator->supports($request));
|
$this->assertTrue($this->authenticator->supports($request));
|
||||||
|
|
||||||
$this->userProvider->expects($this->once())
|
$this->userProvider->createUser(new User($emailAddress, null));
|
||||||
->method('loadUserByUsername')
|
|
||||||
->with($emailAddress)
|
|
||||||
->willReturn(new User($emailAddress, null));
|
|
||||||
|
|
||||||
$passport = $this->authenticator->authenticate($request);
|
$passport = $this->authenticator->authenticate($request);
|
||||||
$this->assertEquals($emailAddress, $passport->getUser()->getUsername());
|
$this->assertEquals($emailAddress, $passport->getUser()->getUsername());
|
||||||
@ -105,10 +99,7 @@ class X509AuthenticatorTest extends TestCase
|
|||||||
]);
|
]);
|
||||||
$this->assertTrue($authenticator->supports($request));
|
$this->assertTrue($authenticator->supports($request));
|
||||||
|
|
||||||
$this->userProvider->expects($this->once())
|
$this->userProvider->createUser(new User('TheUser', null));
|
||||||
->method('loadUserByUsername')
|
|
||||||
->with('TheUser')
|
|
||||||
->willReturn(new User('TheUser', null));
|
|
||||||
|
|
||||||
$passport = $this->authenticator->authenticate($request);
|
$passport = $this->authenticator->authenticate($request);
|
||||||
$this->assertEquals('TheUser', $passport->getUser()->getUsername());
|
$this->assertEquals('TheUser', $passport->getUser()->getUsername());
|
||||||
@ -123,10 +114,7 @@ class X509AuthenticatorTest extends TestCase
|
|||||||
]);
|
]);
|
||||||
$this->assertTrue($authenticator->supports($request));
|
$this->assertTrue($authenticator->supports($request));
|
||||||
|
|
||||||
$this->userProvider->expects($this->once())
|
$this->userProvider->createUser(new User('cert@example.com', null));
|
||||||
->method('loadUserByUsername')
|
|
||||||
->with('cert@example.com')
|
|
||||||
->willReturn(new User('cert@example.com', null));
|
|
||||||
|
|
||||||
$passport = $authenticator->authenticate($request);
|
$passport = $authenticator->authenticate($request);
|
||||||
$this->assertEquals('cert@example.com', $passport->getUser()->getUsername());
|
$this->assertEquals('cert@example.com', $passport->getUser()->getUsername());
|
||||||
|
@ -16,9 +16,10 @@ use Symfony\Component\HttpFoundation\Request;
|
|||||||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
||||||
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
|
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
|
||||||
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
|
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
|
||||||
|
use Symfony\Component\Security\Core\User\InMemoryUserProvider;
|
||||||
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
|
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
|
||||||
|
use Symfony\Component\Security\Core\User\User;
|
||||||
use Symfony\Component\Security\Core\User\UserInterface;
|
use Symfony\Component\Security\Core\User\UserInterface;
|
||||||
use Symfony\Component\Security\Core\User\UserProviderInterface;
|
|
||||||
use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface;
|
use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface;
|
||||||
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\PasswordUpgradeBadge;
|
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\PasswordUpgradeBadge;
|
||||||
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
|
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
|
||||||
@ -36,13 +37,12 @@ class PasswordMigratingListenerTest extends TestCase
|
|||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
$this->user = $this->createMock(UserInterface::class);
|
$this->user = new User('test', 'old-encoded-password');
|
||||||
$this->user->expects($this->any())->method('getPassword')->willReturn('old-encoded-password');
|
|
||||||
$encoder = $this->createMock(PasswordEncoderInterface::class);
|
$encoder = $this->createMock(PasswordEncoderInterface::class);
|
||||||
$encoder->expects($this->any())->method('needsRehash')->willReturn(true);
|
$encoder->expects($this->any())->method('needsRehash')->willReturn(true);
|
||||||
$encoder->expects($this->any())->method('encodePassword')->with('pa$$word', null)->willReturn('new-encoded-password');
|
$encoder->expects($this->any())->method('encodePassword')->with('pa$$word', null)->willReturn('new-encoded-password');
|
||||||
$this->encoderFactory = $this->createMock(EncoderFactoryInterface::class);
|
$this->encoderFactory = $this->createMock(EncoderFactoryInterface::class);
|
||||||
$this->encoderFactory->expects($this->any())->method('getEncoder')->with($this->user)->willReturn($encoder);
|
$this->encoderFactory->expects($this->any())->method('getEncoder')->with($this->callback(function ($user) { return $this->user->isEqualTo($user); }))->willReturn($encoder);
|
||||||
$this->listener = new PasswordMigratingListener($this->encoderFactory);
|
$this->listener = new PasswordMigratingListener($this->encoderFactory);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,12 +96,12 @@ class PasswordMigratingListenerTest extends TestCase
|
|||||||
|
|
||||||
public function testUpgradeWithoutUpgrader()
|
public function testUpgradeWithoutUpgrader()
|
||||||
{
|
{
|
||||||
$userLoader = $this->createMock(MigratingUserProvider::class);
|
$userLoader = $this->getMockBuilder(MigratingUserProvider::class)->setMethods(['upgradePassword'])->getMock();
|
||||||
$userLoader->expects($this->any())->method('loadUserByUsername')->willReturn($this->user);
|
$userLoader->createUser($this->user);
|
||||||
|
|
||||||
$userLoader->expects($this->once())
|
$userLoader->expects($this->once())
|
||||||
->method('upgradePassword')
|
->method('upgradePassword')
|
||||||
->with($this->user, 'new-encoded-password')
|
->with($this->callback(function ($user) { return $this->user->isEqualTo($user); }), 'new-encoded-password')
|
||||||
;
|
;
|
||||||
|
|
||||||
$event = $this->createEvent(new SelfValidatingPassport(new UserBadge('test', [$userLoader, 'loadUserByUsername']), [new PasswordUpgradeBadge('pa$$word')]));
|
$event = $this->createEvent(new SelfValidatingPassport(new UserBadge('test', [$userLoader, 'loadUserByUsername']), [new PasswordUpgradeBadge('pa$$word')]));
|
||||||
@ -119,7 +119,7 @@ class PasswordMigratingListenerTest extends TestCase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract class MigratingUserProvider implements UserProviderInterface, PasswordUpgraderInterface
|
class MigratingUserProvider extends InMemoryUserProvider implements PasswordUpgraderInterface
|
||||||
{
|
{
|
||||||
public function upgradePassword(UserInterface $user, string $newEncodedPassword): void
|
public function upgradePassword(UserInterface $user, string $newEncodedPassword): void
|
||||||
{
|
{
|
||||||
|
@ -12,9 +12,9 @@
|
|||||||
namespace Symfony\Component\Security\Http\Tests\EventListener;
|
namespace Symfony\Component\Security\Http\Tests\EventListener;
|
||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Symfony\Component\Security\Core\User\InMemoryUserProvider;
|
||||||
use Symfony\Component\Security\Core\User\User;
|
use Symfony\Component\Security\Core\User\User;
|
||||||
use Symfony\Component\Security\Core\User\UserInterface;
|
use Symfony\Component\Security\Core\User\UserInterface;
|
||||||
use Symfony\Component\Security\Core\User\UserProviderInterface;
|
|
||||||
use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface;
|
use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface;
|
||||||
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
|
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
|
||||||
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
|
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
|
||||||
@ -28,7 +28,7 @@ class UserProviderListenerTest extends TestCase
|
|||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
$this->userProvider = $this->createMock(UserProviderInterface::class);
|
$this->userProvider = new InMemoryUserProvider();
|
||||||
$this->listener = new UserProviderListener($this->userProvider);
|
$this->listener = new UserProviderListener($this->userProvider);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,8 +42,8 @@ class UserProviderListenerTest extends TestCase
|
|||||||
$this->assertEquals([$this->userProvider, 'loadUserByUsername'], $badge->getUserLoader());
|
$this->assertEquals([$this->userProvider, 'loadUserByUsername'], $badge->getUserLoader());
|
||||||
|
|
||||||
$user = new User('wouter', null);
|
$user = new User('wouter', null);
|
||||||
$this->userProvider->expects($this->once())->method('loadUserByUsername')->with('wouter')->willReturn($user);
|
$this->userProvider->createUser($user);
|
||||||
$this->assertSame($user, $passport->getUser());
|
$this->assertTrue($user->isEqualTo($passport->getUser()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -25,7 +25,6 @@ use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundE
|
|||||||
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
|
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
|
||||||
use Symfony\Component\Security\Core\User\User;
|
use Symfony\Component\Security\Core\User\User;
|
||||||
use Symfony\Component\Security\Core\User\UserCheckerInterface;
|
use Symfony\Component\Security\Core\User\UserCheckerInterface;
|
||||||
use Symfony\Component\Security\Core\User\UserInterface;
|
|
||||||
use Symfony\Component\Security\Core\User\UserProviderInterface;
|
use Symfony\Component\Security\Core\User\UserProviderInterface;
|
||||||
use Symfony\Component\Security\Http\Event\SwitchUserEvent;
|
use Symfony\Component\Security\Http\Event\SwitchUserEvent;
|
||||||
use Symfony\Component\Security\Http\Firewall\SwitchUserListener;
|
use Symfony\Component\Security\Http\Firewall\SwitchUserListener;
|
||||||
@ -112,13 +111,13 @@ class SwitchUserListenerTest extends TestCase
|
|||||||
|
|
||||||
public function testExitUserDispatchesEventWithRefreshedUser()
|
public function testExitUserDispatchesEventWithRefreshedUser()
|
||||||
{
|
{
|
||||||
$originalUser = $this->createMock(UserInterface::class);
|
$originalUser = new User('username', null);
|
||||||
$refreshedUser = $this->createMock(UserInterface::class);
|
$refreshedUser = new User('username', null);
|
||||||
$this
|
$this
|
||||||
->userProvider
|
->userProvider
|
||||||
->expects($this->any())
|
->expects($this->any())
|
||||||
->method('refreshUser')
|
->method('refreshUser')
|
||||||
->with($originalUser)
|
->with($this->identicalTo($originalUser))
|
||||||
->willReturn($refreshedUser);
|
->willReturn($refreshedUser);
|
||||||
$originalToken = new UsernamePasswordToken($originalUser, '', 'key');
|
$originalToken = new UsernamePasswordToken($originalUser, '', 'key');
|
||||||
$this->tokenStorage->setToken(new SwitchUserToken('username', '', 'key', ['ROLE_USER'], $originalToken));
|
$this->tokenStorage->setToken(new SwitchUserToken('username', '', 'key', ['ROLE_USER'], $originalToken));
|
||||||
@ -399,13 +398,13 @@ class SwitchUserListenerTest extends TestCase
|
|||||||
|
|
||||||
public function testSwitchUserRefreshesOriginalToken()
|
public function testSwitchUserRefreshesOriginalToken()
|
||||||
{
|
{
|
||||||
$originalUser = $this->createMock(UserInterface::class);
|
$originalUser = new User('username', null);
|
||||||
$refreshedOriginalUser = $this->createMock(UserInterface::class);
|
$refreshedOriginalUser = new User('username', null);
|
||||||
$this
|
$this
|
||||||
->userProvider
|
->userProvider
|
||||||
->expects($this->any())
|
->expects($this->any())
|
||||||
->method('refreshUser')
|
->method('refreshUser')
|
||||||
->with($originalUser)
|
->with($this->identicalTo($originalUser))
|
||||||
->willReturn($refreshedOriginalUser);
|
->willReturn($refreshedOriginalUser);
|
||||||
$originalToken = new UsernamePasswordToken($originalUser, '', 'key');
|
$originalToken = new UsernamePasswordToken($originalUser, '', 'key');
|
||||||
$this->tokenStorage->setToken(new SwitchUserToken('username', '', 'key', ['ROLE_USER'], $originalToken));
|
$this->tokenStorage->setToken(new SwitchUserToken('username', '', 'key', ['ROLE_USER'], $originalToken));
|
||||||
|
@ -29,7 +29,7 @@ class LoginLinkHandlerTest extends TestCase
|
|||||||
{
|
{
|
||||||
/** @var MockObject|UrlGeneratorInterface */
|
/** @var MockObject|UrlGeneratorInterface */
|
||||||
private $router;
|
private $router;
|
||||||
/** @var MockObject|UserProviderInterface */
|
/** @var TestLoginLinkHandlerUserProvider */
|
||||||
private $userProvider;
|
private $userProvider;
|
||||||
/** @var PropertyAccessorInterface */
|
/** @var PropertyAccessorInterface */
|
||||||
private $propertyAccessor;
|
private $propertyAccessor;
|
||||||
@ -39,7 +39,7 @@ class LoginLinkHandlerTest extends TestCase
|
|||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
$this->router = $this->createMock(UrlGeneratorInterface::class);
|
$this->router = $this->createMock(UrlGeneratorInterface::class);
|
||||||
$this->userProvider = $this->createMock(UserProviderInterface::class);
|
$this->userProvider = new TestLoginLinkHandlerUserProvider();
|
||||||
$this->propertyAccessor = PropertyAccess::createPropertyAccessor();
|
$this->propertyAccessor = PropertyAccess::createPropertyAccessor();
|
||||||
$this->expiredLinkStorage = $this->createMock(ExpiredLoginLinkStorage::class);
|
$this->expiredLinkStorage = $this->createMock(ExpiredLoginLinkStorage::class);
|
||||||
}
|
}
|
||||||
@ -94,10 +94,7 @@ class LoginLinkHandlerTest extends TestCase
|
|||||||
$request = Request::create(sprintf('/login/verify?user=weaverryan&hash=%s&expires=%d', $signature, $expires));
|
$request = Request::create(sprintf('/login/verify?user=weaverryan&hash=%s&expires=%d', $signature, $expires));
|
||||||
|
|
||||||
$user = new TestLoginLinkHandlerUser('weaverryan', 'ryan@symfonycasts.com', 'pwhash');
|
$user = new TestLoginLinkHandlerUser('weaverryan', 'ryan@symfonycasts.com', 'pwhash');
|
||||||
$this->userProvider->expects($this->once())
|
$this->userProvider->createUser($user);
|
||||||
->method('loadUserByUsername')
|
|
||||||
->with('weaverryan')
|
|
||||||
->willReturn($user);
|
|
||||||
|
|
||||||
$this->expiredLinkStorage->expects($this->once())
|
$this->expiredLinkStorage->expects($this->once())
|
||||||
->method('incrementUsages')
|
->method('incrementUsages')
|
||||||
@ -105,7 +102,7 @@ class LoginLinkHandlerTest extends TestCase
|
|||||||
|
|
||||||
$linker = $this->createLinker(['max_uses' => 3]);
|
$linker = $this->createLinker(['max_uses' => 3]);
|
||||||
$actualUser = $linker->consumeLoginLink($request);
|
$actualUser = $linker->consumeLoginLink($request);
|
||||||
$this->assertSame($user, $actualUser);
|
$this->assertEquals($user, $actualUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testConsumeLoginLinkWithExpired()
|
public function testConsumeLoginLinkWithExpired()
|
||||||
@ -116,10 +113,7 @@ class LoginLinkHandlerTest extends TestCase
|
|||||||
$request = Request::create(sprintf('/login/verify?user=weaverryan&hash=%s&expires=%d', $signature, $expires));
|
$request = Request::create(sprintf('/login/verify?user=weaverryan&hash=%s&expires=%d', $signature, $expires));
|
||||||
|
|
||||||
$user = new TestLoginLinkHandlerUser('weaverryan', 'ryan@symfonycasts.com', 'pwhash');
|
$user = new TestLoginLinkHandlerUser('weaverryan', 'ryan@symfonycasts.com', 'pwhash');
|
||||||
$this->userProvider->expects($this->once())
|
$this->userProvider->createUser($user);
|
||||||
->method('loadUserByUsername')
|
|
||||||
->with('weaverryan')
|
|
||||||
->willReturn($user);
|
|
||||||
|
|
||||||
$linker = $this->createLinker(['max_uses' => 3]);
|
$linker = $this->createLinker(['max_uses' => 3]);
|
||||||
$linker->consumeLoginLink($request);
|
$linker->consumeLoginLink($request);
|
||||||
@ -130,11 +124,6 @@ class LoginLinkHandlerTest extends TestCase
|
|||||||
$this->expectException(InvalidLoginLinkException::class);
|
$this->expectException(InvalidLoginLinkException::class);
|
||||||
$request = Request::create('/login/verify?user=weaverryan&hash=thehash&expires=10000');
|
$request = Request::create('/login/verify?user=weaverryan&hash=thehash&expires=10000');
|
||||||
|
|
||||||
$this->userProvider->expects($this->once())
|
|
||||||
->method('loadUserByUsername')
|
|
||||||
->with('weaverryan')
|
|
||||||
->willThrowException(new UsernameNotFoundException());
|
|
||||||
|
|
||||||
$linker = $this->createLinker();
|
$linker = $this->createLinker();
|
||||||
$linker->consumeLoginLink($request);
|
$linker->consumeLoginLink($request);
|
||||||
}
|
}
|
||||||
@ -145,10 +134,7 @@ class LoginLinkHandlerTest extends TestCase
|
|||||||
$request = Request::create(sprintf('/login/verify?user=weaverryan&hash=fake_hash&expires=%d', time() + 500));
|
$request = Request::create(sprintf('/login/verify?user=weaverryan&hash=fake_hash&expires=%d', time() + 500));
|
||||||
|
|
||||||
$user = new TestLoginLinkHandlerUser('weaverryan', 'ryan@symfonycasts.com', 'pwhash');
|
$user = new TestLoginLinkHandlerUser('weaverryan', 'ryan@symfonycasts.com', 'pwhash');
|
||||||
$this->userProvider->expects($this->once())
|
$this->userProvider->createUser($user);
|
||||||
->method('loadUserByUsername')
|
|
||||||
->with('weaverryan')
|
|
||||||
->willReturn($user);
|
|
||||||
|
|
||||||
$linker = $this->createLinker();
|
$linker = $this->createLinker();
|
||||||
$linker->consumeLoginLink($request);
|
$linker->consumeLoginLink($request);
|
||||||
@ -162,10 +148,7 @@ class LoginLinkHandlerTest extends TestCase
|
|||||||
$request = Request::create(sprintf('/login/verify?user=weaverryan&hash=%s&expires=%d', $signature, $expires));
|
$request = Request::create(sprintf('/login/verify?user=weaverryan&hash=%s&expires=%d', $signature, $expires));
|
||||||
|
|
||||||
$user = new TestLoginLinkHandlerUser('weaverryan', 'ryan@symfonycasts.com', 'pwhash');
|
$user = new TestLoginLinkHandlerUser('weaverryan', 'ryan@symfonycasts.com', 'pwhash');
|
||||||
$this->userProvider->expects($this->once())
|
$this->userProvider->createUser($user);
|
||||||
->method('loadUserByUsername')
|
|
||||||
->with('weaverryan')
|
|
||||||
->willReturn($user);
|
|
||||||
|
|
||||||
$this->expiredLinkStorage->expects($this->once())
|
$this->expiredLinkStorage->expects($this->once())
|
||||||
->method('countUsages')
|
->method('countUsages')
|
||||||
@ -198,6 +181,35 @@ class LoginLinkHandlerTest extends TestCase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class TestLoginLinkHandlerUserProvider implements UserProviderInterface
|
||||||
|
{
|
||||||
|
private $users = [];
|
||||||
|
|
||||||
|
public function createUser(TestLoginLinkHandlerUser $user): void
|
||||||
|
{
|
||||||
|
$this->users[$user->getUsername()] = $user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function loadUserByUsername(string $username): TestLoginLinkHandlerUser
|
||||||
|
{
|
||||||
|
if (!isset($this->users[$username])) {
|
||||||
|
throw new UsernameNotFoundException();
|
||||||
|
}
|
||||||
|
|
||||||
|
return clone $this->users[$username];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function refreshUser(UserInterface $user)
|
||||||
|
{
|
||||||
|
return $this->users[$username];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function supportsClass(string $class)
|
||||||
|
{
|
||||||
|
return TestLoginLinkHandlerUser::class === $class;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class TestLoginLinkHandlerUser implements UserInterface
|
class TestLoginLinkHandlerUser implements UserInterface
|
||||||
{
|
{
|
||||||
public $username;
|
public $username;
|
||||||
|
@ -22,9 +22,9 @@ use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken;
|
|||||||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
||||||
use Symfony\Component\Security\Core\Exception\CookieTheftException;
|
use Symfony\Component\Security\Core\Exception\CookieTheftException;
|
||||||
use Symfony\Component\Security\Core\Exception\TokenNotFoundException;
|
use Symfony\Component\Security\Core\Exception\TokenNotFoundException;
|
||||||
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
|
use Symfony\Component\Security\Core\User\InMemoryUserProvider;
|
||||||
|
use Symfony\Component\Security\Core\User\User;
|
||||||
use Symfony\Component\Security\Core\User\UserInterface;
|
use Symfony\Component\Security\Core\User\UserInterface;
|
||||||
use Symfony\Component\Security\Core\User\UserProviderInterface;
|
|
||||||
use Symfony\Component\Security\Http\RememberMe\PersistentTokenBasedRememberMeServices;
|
use Symfony\Component\Security\Http\RememberMe\PersistentTokenBasedRememberMeServices;
|
||||||
use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
|
use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
|
||||||
|
|
||||||
@ -94,12 +94,6 @@ class PersistentTokenBasedRememberMeServicesTest extends TestCase
|
|||||||
;
|
;
|
||||||
$service->setTokenProvider($tokenProvider);
|
$service->setTokenProvider($tokenProvider);
|
||||||
|
|
||||||
$userProvider
|
|
||||||
->expects($this->once())
|
|
||||||
->method('loadUserByUsername')
|
|
||||||
->willThrowException(new UsernameNotFoundException('user not found'))
|
|
||||||
;
|
|
||||||
|
|
||||||
$this->assertNull($service->autoLogin($request));
|
$this->assertNull($service->autoLogin($request));
|
||||||
$this->assertTrue($request->attributes->has(RememberMeServicesInterface::COOKIE_ATTR_NAME));
|
$this->assertTrue($request->attributes->has(RememberMeServicesInterface::COOKIE_ATTR_NAME));
|
||||||
}
|
}
|
||||||
@ -161,20 +155,10 @@ class PersistentTokenBasedRememberMeServicesTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function testAutoLogin(bool $hashTokenValue)
|
public function testAutoLogin(bool $hashTokenValue)
|
||||||
{
|
{
|
||||||
$user = $this->createMock(UserInterface::class);
|
$user = new User('foouser', null, ['ROLE_FOO']);
|
||||||
$user
|
|
||||||
->expects($this->once())
|
|
||||||
->method('getRoles')
|
|
||||||
->willReturn(['ROLE_FOO'])
|
|
||||||
;
|
|
||||||
|
|
||||||
$userProvider = $this->getProvider();
|
$userProvider = $this->getProvider();
|
||||||
$userProvider
|
$userProvider->createUser($user);
|
||||||
->expects($this->once())
|
|
||||||
->method('loadUserByUsername')
|
|
||||||
->with($this->equalTo('foouser'))
|
|
||||||
->willReturn($user)
|
|
||||||
;
|
|
||||||
|
|
||||||
$service = $this->getService($userProvider, ['name' => 'foo', 'path' => null, 'domain' => null, 'secure' => false, 'httponly' => false, 'always_remember_me' => true, 'lifetime' => 3600]);
|
$service = $this->getService($userProvider, ['name' => 'foo', 'path' => null, 'domain' => null, 'secure' => false, 'httponly' => false, 'always_remember_me' => true, 'lifetime' => 3600]);
|
||||||
$request = new Request();
|
$request = new Request();
|
||||||
@ -186,14 +170,14 @@ class PersistentTokenBasedRememberMeServicesTest extends TestCase
|
|||||||
->expects($this->once())
|
->expects($this->once())
|
||||||
->method('loadTokenBySeries')
|
->method('loadTokenBySeries')
|
||||||
->with($this->equalTo('fooseries'))
|
->with($this->equalTo('fooseries'))
|
||||||
->willReturn(new PersistentToken('fooclass', 'foouser', 'fooseries', $tokenValue, new \DateTime()))
|
->willReturn(new PersistentToken(User::class, 'foouser', 'fooseries', $tokenValue, new \DateTime()))
|
||||||
;
|
;
|
||||||
$service->setTokenProvider($tokenProvider);
|
$service->setTokenProvider($tokenProvider);
|
||||||
|
|
||||||
$returnedToken = $service->autoLogin($request);
|
$returnedToken = $service->autoLogin($request);
|
||||||
|
|
||||||
$this->assertInstanceOf(RememberMeToken::class, $returnedToken);
|
$this->assertInstanceOf(RememberMeToken::class, $returnedToken);
|
||||||
$this->assertSame($user, $returnedToken->getUser());
|
$this->assertTrue($user->isEqualTo($returnedToken->getUser()));
|
||||||
$this->assertEquals('foosecret', $returnedToken->getSecret());
|
$this->assertEquals('foosecret', $returnedToken->getSecret());
|
||||||
$this->assertTrue($request->attributes->has(RememberMeServicesInterface::COOKIE_ATTR_NAME));
|
$this->assertTrue($request->attributes->has(RememberMeServicesInterface::COOKIE_ATTR_NAME));
|
||||||
}
|
}
|
||||||
@ -339,14 +323,7 @@ class PersistentTokenBasedRememberMeServicesTest extends TestCase
|
|||||||
|
|
||||||
protected function getProvider()
|
protected function getProvider()
|
||||||
{
|
{
|
||||||
$provider = $this->createMock(UserProviderInterface::class);
|
return new InMemoryUserProvider();
|
||||||
$provider
|
|
||||||
->expects($this->any())
|
|
||||||
->method('supportsClass')
|
|
||||||
->willReturn(true)
|
|
||||||
;
|
|
||||||
|
|
||||||
return $provider;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function generateHash(string $tokenValue): string
|
protected function generateHash(string $tokenValue): string
|
||||||
|
@ -18,9 +18,9 @@ use Symfony\Component\HttpFoundation\Response;
|
|||||||
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
|
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
|
||||||
use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken;
|
use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken;
|
||||||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
||||||
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
|
use Symfony\Component\Security\Core\User\InMemoryUserProvider;
|
||||||
|
use Symfony\Component\Security\Core\User\User;
|
||||||
use Symfony\Component\Security\Core\User\UserInterface;
|
use Symfony\Component\Security\Core\User\UserInterface;
|
||||||
use Symfony\Component\Security\Core\User\UserProviderInterface;
|
|
||||||
use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
|
use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
|
||||||
use Symfony\Component\Security\Http\RememberMe\TokenBasedRememberMeServices;
|
use Symfony\Component\Security\Http\RememberMe\TokenBasedRememberMeServices;
|
||||||
|
|
||||||
@ -51,12 +51,6 @@ class TokenBasedRememberMeServicesTest extends TestCase
|
|||||||
$request = new Request();
|
$request = new Request();
|
||||||
$request->cookies->set('foo', $this->getCookie('fooclass', 'foouser', time() + 3600, 'foopass'));
|
$request->cookies->set('foo', $this->getCookie('fooclass', 'foouser', time() + 3600, 'foopass'));
|
||||||
|
|
||||||
$userProvider
|
|
||||||
->expects($this->once())
|
|
||||||
->method('loadUserByUsername')
|
|
||||||
->willThrowException(new UsernameNotFoundException('user not found'))
|
|
||||||
;
|
|
||||||
|
|
||||||
$this->assertNull($service->autoLogin($request));
|
$this->assertNull($service->autoLogin($request));
|
||||||
$this->assertTrue($request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME)->isCleared());
|
$this->assertTrue($request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME)->isCleared());
|
||||||
}
|
}
|
||||||
@ -68,19 +62,8 @@ class TokenBasedRememberMeServicesTest extends TestCase
|
|||||||
$request = new Request();
|
$request = new Request();
|
||||||
$request->cookies->set('foo', base64_encode('class:'.base64_encode('foouser').':123456789:fooHash'));
|
$request->cookies->set('foo', base64_encode('class:'.base64_encode('foouser').':123456789:fooHash'));
|
||||||
|
|
||||||
$user = $this->createMock(UserInterface::class);
|
$user = new User('foouser', 'foopass');
|
||||||
$user
|
$userProvider->createUser($user);
|
||||||
->expects($this->once())
|
|
||||||
->method('getPassword')
|
|
||||||
->willReturn('foopass')
|
|
||||||
;
|
|
||||||
|
|
||||||
$userProvider
|
|
||||||
->expects($this->once())
|
|
||||||
->method('loadUserByUsername')
|
|
||||||
->with($this->equalTo('foouser'))
|
|
||||||
->willReturn($user)
|
|
||||||
;
|
|
||||||
|
|
||||||
$this->assertNull($service->autoLogin($request));
|
$this->assertNull($service->autoLogin($request));
|
||||||
$this->assertTrue($request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME)->isCleared());
|
$this->assertTrue($request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME)->isCleared());
|
||||||
@ -93,19 +76,8 @@ class TokenBasedRememberMeServicesTest extends TestCase
|
|||||||
$request = new Request();
|
$request = new Request();
|
||||||
$request->cookies->set('foo', $this->getCookie('fooclass', 'foouser', time() - 1, 'foopass'));
|
$request->cookies->set('foo', $this->getCookie('fooclass', 'foouser', time() - 1, 'foopass'));
|
||||||
|
|
||||||
$user = $this->createMock(UserInterface::class);
|
$user = new User('foouser', 'foopass');
|
||||||
$user
|
$userProvider->createUser($user);
|
||||||
->expects($this->once())
|
|
||||||
->method('getPassword')
|
|
||||||
->willReturn('foopass')
|
|
||||||
;
|
|
||||||
|
|
||||||
$userProvider
|
|
||||||
->expects($this->once())
|
|
||||||
->method('loadUserByUsername')
|
|
||||||
->with($this->equalTo('foouser'))
|
|
||||||
->willReturn($user)
|
|
||||||
;
|
|
||||||
|
|
||||||
$this->assertNull($service->autoLogin($request));
|
$this->assertNull($service->autoLogin($request));
|
||||||
$this->assertTrue($request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME)->isCleared());
|
$this->assertTrue($request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME)->isCleared());
|
||||||
@ -118,34 +90,18 @@ class TokenBasedRememberMeServicesTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function testAutoLogin($username)
|
public function testAutoLogin($username)
|
||||||
{
|
{
|
||||||
$user = $this->createMock(UserInterface::class);
|
|
||||||
$user
|
|
||||||
->expects($this->once())
|
|
||||||
->method('getRoles')
|
|
||||||
->willReturn(['ROLE_FOO'])
|
|
||||||
;
|
|
||||||
$user
|
|
||||||
->expects($this->once())
|
|
||||||
->method('getPassword')
|
|
||||||
->willReturn('foopass')
|
|
||||||
;
|
|
||||||
|
|
||||||
$userProvider = $this->getProvider();
|
$userProvider = $this->getProvider();
|
||||||
$userProvider
|
$user = new User($username, 'foopass', ['ROLE_FOO']);
|
||||||
->expects($this->once())
|
$userProvider->createUser($user);
|
||||||
->method('loadUserByUsername')
|
|
||||||
->with($this->equalTo($username))
|
|
||||||
->willReturn($user)
|
|
||||||
;
|
|
||||||
|
|
||||||
$service = $this->getService($userProvider, ['name' => 'foo', 'always_remember_me' => true, 'lifetime' => 3600]);
|
$service = $this->getService($userProvider, ['name' => 'foo', 'always_remember_me' => true, 'lifetime' => 3600]);
|
||||||
$request = new Request();
|
$request = new Request();
|
||||||
$request->cookies->set('foo', $this->getCookie('fooclass', $username, time() + 3600, 'foopass'));
|
$request->cookies->set('foo', $this->getCookie(User::class, $username, time() + 3600, 'foopass'));
|
||||||
|
|
||||||
$returnedToken = $service->autoLogin($request);
|
$returnedToken = $service->autoLogin($request);
|
||||||
|
|
||||||
$this->assertInstanceOf(RememberMeToken::class, $returnedToken);
|
$this->assertInstanceOf(RememberMeToken::class, $returnedToken);
|
||||||
$this->assertSame($user, $returnedToken->getUser());
|
$this->assertTrue($user->isEqualTo($returnedToken->getUser()));
|
||||||
$this->assertEquals('foosecret', $returnedToken->getSecret());
|
$this->assertEquals('foosecret', $returnedToken->getSecret());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -279,13 +235,6 @@ class TokenBasedRememberMeServicesTest extends TestCase
|
|||||||
|
|
||||||
protected function getProvider()
|
protected function getProvider()
|
||||||
{
|
{
|
||||||
$provider = $this->createMock(UserProviderInterface::class);
|
return new InMemoryUserProvider();
|
||||||
$provider
|
|
||||||
->expects($this->any())
|
|
||||||
->method('supportsClass')
|
|
||||||
->willReturn(true)
|
|
||||||
;
|
|
||||||
|
|
||||||
return $provider;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user