Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Security] Use concrete UserInterface and UserProviderInterface classes in the tests #40609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@

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\UserInterface;
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
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\User;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\User\InMemoryUserProvider;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Http\Authenticator\FormLoginAuthenticator;
Expand All @@ -37,8 +36,7 @@ class FormLoginAuthenticatorTest extends TestCase

protected function setUp(): void
{
$this->userProvider = $this->createMock(UserProviderInterface::class);
$this->userProvider->expects($this->any())->method('loadUserByUsername')->willReturn(new User('test', 's$cr$t'));
$this->userProvider = new InMemoryUserProvider(['test' => ['password' => 's$cr$t']]);
$this->successHandler = $this->createMock(AuthenticationSuccessHandlerInterface::class);
$this->failureHandler = $this->createMock(AuthenticationFailureHandlerInterface::class);
}
Expand Down Expand Up @@ -149,8 +147,7 @@ public function testUpgradePassword()
$request = Request::create('/login_check', 'POST', ['_username' => 'wouter', '_password' => 's$cr$t']);
$request->setSession($this->createSession());

$this->userProvider = $this->createMock(PasswordUpgraderProvider::class);
$this->userProvider->expects($this->any())->method('loadUserByUsername')->willReturn(new User('test', 's$cr$t'));
$this->userProvider = new PasswordUpgraderProvider(['test' => ['password' => 's$cr$t']]);

$this->setUpAuthenticator();
$passport = $this->authenticator->authenticate($request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
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\UserProviderInterface;
use Symfony\Component\Security\Http\Authenticator\HttpBasicAuthenticator;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\PasswordUpgradeBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
Expand All @@ -22,7 +22,7 @@ class HttpBasicAuthenticatorTest extends TestCase

protected function setUp(): void
{
$this->userProvider = $this->createMock(UserProviderInterface::class);
$this->userProvider = new InMemoryUserProvider();
$this->encoderFactory = $this->createMock(EncoderFactoryInterface::class);
$this->encoder = $this->createMock(PasswordEncoderInterface::class);
$this->encoderFactory
Expand All @@ -40,16 +40,12 @@ public function testExtractCredentialsAndUserFromRequest()
'PHP_AUTH_PW' => 'ThePassword',
]);

$this->userProvider
->expects($this->any())
->method('loadUserByUsername')
->with('TheUsername')
->willReturn($user = new User('TheUsername', 'ThePassword'));
$this->userProvider->createUser($user = new User('TheUsername', 'ThePassword'));

$passport = $this->authenticator->authenticate($request);
$this->assertEquals('ThePassword', $passport->getBadge(PasswordCredentials::class)->getPassword());

$this->assertSame($user, $passport->getUser());
$this->assertTrue($user->isEqualTo($passport->getUser()));
}

/**
Expand Down Expand Up @@ -77,8 +73,7 @@ public function testUpgradePassword()
'PHP_AUTH_PW' => 'ThePassword',
]);

$this->userProvider = $this->createMock(PasswordUpgraderProvider::class);
$this->userProvider->expects($this->any())->method('loadUserByUsername')->willReturn(new User('test', 's$cr$t'));
$this->userProvider = new PasswordUpgraderProvider(['test' => ['password' => 's$cr$t']]);
$authenticator = new HttpBasicAuthenticator('test', $this->userProvider);

$passport = $authenticator->authenticate($request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
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\UserProviderInterface;
use Symfony\Component\Security\Http\Authenticator\RemoteUserAuthenticator;

class RemoteUserAuthenticatorTest extends TestCase
{
/**
* @dataProvider provideAuthenticators
*/
public function testSupport(UserProviderInterface $userProvider, RemoteUserAuthenticator $authenticator, $parameterName)
public function testSupport(InMemoryUserProvider $userProvider, RemoteUserAuthenticator $authenticator, $parameterName)
{
$request = $this->createRequest([$parameterName => 'TheUsername']);

Expand All @@ -32,35 +32,32 @@ public function testSupport(UserProviderInterface $userProvider, RemoteUserAuthe

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([])));
}

/**
* @dataProvider provideAuthenticators
*/
public function testAuthenticate(UserProviderInterface $userProvider, RemoteUserAuthenticator $authenticator, $parameterName)
public function testAuthenticate(InMemoryUserProvider $userProvider, RemoteUserAuthenticator $authenticator, $parameterName)
{
$request = $this->createRequest([$parameterName => 'TheUsername']);

$authenticator->supports($request);

$userProvider->expects($this->once())
->method('loadUserByUsername')
->with('TheUsername')
->willReturn($user = new User('TheUsername', null));
$userProvider->createUser($user = new User('TheUsername', null));

$passport = $authenticator->authenticate($request);
$this->assertEquals($user, $passport->getUser());
$this->assertTrue($user->isEqualTo($passport->getUser()));
}

public function provideAuthenticators()
{
$userProvider = $this->createMock(UserProviderInterface::class);
$userProvider = new InMemoryUserProvider();
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'];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
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\UserProviderInterface;
use Symfony\Component\Security\Http\Authenticator\X509Authenticator;

class X509AuthenticatorTest extends TestCase
Expand All @@ -25,7 +25,7 @@ class X509AuthenticatorTest extends TestCase

protected function setUp(): void
{
$this->userProvider = $this->createMock(UserProviderInterface::class);
$this->userProvider = new InMemoryUserProvider();
$this->authenticator = new X509Authenticator($this->userProvider, new TokenStorage(), 'main');
}

Expand All @@ -45,10 +45,7 @@ public function testAuthentication($username, $credentials)
$request = $this->createRequest($serverVars);
$this->assertTrue($this->authenticator->supports($request));

$this->userProvider->expects($this->any())
->method('loadUserByUsername')
->with($username)
->willReturn(new User($username, null));
$this->userProvider->createUser(new User($username, null));

$passport = $this->authenticator->authenticate($request);
$this->assertEquals($username, $passport->getUser()->getUsername());
Expand All @@ -69,10 +66,7 @@ public function testAuthenticationNoUser($emailAddress, $credentials)

$this->assertTrue($this->authenticator->supports($request));

$this->userProvider->expects($this->once())
->method('loadUserByUsername')
->with($emailAddress)
->willReturn(new User($emailAddress, null));
$this->userProvider->createUser(new User($emailAddress, null));

$passport = $this->authenticator->authenticate($request);
$this->assertEquals($emailAddress, $passport->getUser()->getUsername());
Expand Down Expand Up @@ -105,10 +99,7 @@ public function testAuthenticationCustomUserKey()
]);
$this->assertTrue($authenticator->supports($request));

$this->userProvider->expects($this->once())
->method('loadUserByUsername')
->with('TheUser')
->willReturn(new User('TheUser', null));
$this->userProvider->createUser(new User('TheUser', null));

$passport = $this->authenticator->authenticate($request);
$this->assertEquals('TheUser', $passport->getUser()->getUsername());
Expand All @@ -123,10 +114,7 @@ public function testAuthenticationCustomCredentialsKey()
]);
$this->assertTrue($authenticator->supports($request));

$this->userProvider->expects($this->once())
->method('loadUserByUsername')
->with('[email protected]')
->willReturn(new User('[email protected]', null));
$this->userProvider->createUser(new User('[email protected]', null));

$passport = $authenticator->authenticate($request);
$this->assertEquals('[email protected]', $passport->getUser()->getUsername());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
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\User;
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\Passport\Badge\PasswordUpgradeBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
Expand All @@ -36,13 +37,12 @@ class PasswordMigratingListenerTest extends TestCase

protected function setUp(): void
{
$this->user = $this->createMock(UserInterface::class);
$this->user->expects($this->any())->method('getPassword')->willReturn('old-encoded-password');
$this->user = new User('test', 'old-encoded-password');
$encoder = $this->createMock(PasswordEncoderInterface::class);
$encoder->expects($this->any())->method('needsRehash')->willReturn(true);
$encoder->expects($this->any())->method('encodePassword')->with('pa$$word', null)->willReturn('new-encoded-password');
$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);
}

Expand Down Expand Up @@ -96,12 +96,12 @@ public function testUpgradeWithUpgrader()

public function testUpgradeWithoutUpgrader()
{
$userLoader = $this->createMock(MigratingUserProvider::class);
$userLoader->expects($this->any())->method('loadUserByUsername')->willReturn($this->user);
$userLoader = $this->getMockBuilder(MigratingUserProvider::class)->setMethods(['upgradePassword'])->getMock();
$userLoader->createUser($this->user);

$userLoader->expects($this->once())
->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')]));
Expand All @@ -119,7 +119,7 @@ private function createEvent(PassportInterface $passport)
}
}

abstract class MigratingUserProvider implements UserProviderInterface, PasswordUpgraderInterface
class MigratingUserProvider extends InMemoryUserProvider implements PasswordUpgraderInterface
{
public function upgradePassword(UserInterface $user, string $newEncodedPassword): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
namespace Symfony\Component\Security\Http\Tests\EventListener;

use PHPUnit\Framework\TestCase;
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\UserProviderInterface;
use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
Expand All @@ -28,7 +28,7 @@ class UserProviderListenerTest extends TestCase

protected function setUp(): void
{
$this->userProvider = $this->createMock(UserProviderInterface::class);
$this->userProvider = new InMemoryUserProvider();
$this->listener = new UserProviderListener($this->userProvider);
}

Expand All @@ -42,8 +42,8 @@ public function testSetUserProvider()
$this->assertEquals([$this->userProvider, 'loadUserByUsername'], $badge->getUserLoader());

$user = new User('wouter', null);
$this->userProvider->expects($this->once())->method('loadUserByUsername')->with('wouter')->willReturn($user);
$this->assertSame($user, $passport->getUser());
$this->userProvider->createUser($user);
$this->assertTrue($user->isEqualTo($passport->getUser()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\User\User;
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\Http\Event\SwitchUserEvent;
use Symfony\Component\Security\Http\Firewall\SwitchUserListener;
Expand Down Expand Up @@ -112,13 +111,13 @@ public function testExitUserUpdatesToken()

public function testExitUserDispatchesEventWithRefreshedUser()
{
$originalUser = $this->createMock(UserInterface::class);
$refreshedUser = $this->createMock(UserInterface::class);
$originalUser = new User('username', null);
$refreshedUser = new User('username', null);
$this
->userProvider
->expects($this->any())
->method('refreshUser')
->with($originalUser)
->with($this->identicalTo($originalUser))
->willReturn($refreshedUser);
$originalToken = new UsernamePasswordToken($originalUser, '', 'key');
$this->tokenStorage->setToken(new SwitchUserToken('username', '', 'key', ['ROLE_USER'], $originalToken));
Expand Down Expand Up @@ -399,13 +398,13 @@ public function testSwitchUserStateless()

public function testSwitchUserRefreshesOriginalToken()
{
$originalUser = $this->createMock(UserInterface::class);
$refreshedOriginalUser = $this->createMock(UserInterface::class);
$originalUser = new User('username', null);
$refreshedOriginalUser = new User('username', null);
$this
->userProvider
->expects($this->any())
->method('refreshUser')
->with($originalUser)
->with($this->identicalTo($originalUser))
->willReturn($refreshedOriginalUser);
$originalToken = new UsernamePasswordToken($originalUser, '', 'key');
$this->tokenStorage->setToken(new SwitchUserToken('username', '', 'key', ['ROLE_USER'], $originalToken));
Expand Down
Loading