Skip to content

Commit

Permalink
Upgrade UsernamePasswordToken and Token accesses
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz committed May 3, 2022
1 parent 66c2606 commit 84ca2b9
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\User\InMemoryUser;

class SecuritySubscriberTest extends TestCase
{
Expand Down Expand Up @@ -77,6 +78,10 @@ public function testSetDefaultUserWithNullToken()

public function testSetDefaultUserWithAnonymousToken()
{
if (!\class_exists(AnonymousToken::class)) {
$this->markTestSkipped('The AnonymousToken is only available on Symfony 5.4');
}

$event = $this->prophesize(ConfigureOptionsEvent::class);

$optionsResolver = $this->prophesize(OptionsResolver::class);
Expand All @@ -100,7 +105,7 @@ public function testSetDefaultUserWithNonSuluUser()
$token = $this->prophesize(TokenInterface::class);
$this->tokenStorage->getToken()->willReturn($token->reveal());

$token->getUser()->willReturn(new \stdClass());
$token->getUser()->willReturn(new InMemoryUser('test', 'test'));

$optionsResolver->setDefault('user', null)->shouldBeCalled()->willReturn($optionsResolver->reveal());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2692,7 +2692,7 @@ public function testGetResourceLocatorsWithShadow(): void

private function createUserToken(): UsernamePasswordToken
{
return new UsernamePasswordToken($this->getTestUser(), 'testpass', 'fake_provider');
return new UsernamePasswordToken($this->getTestUser(), 'testpass');
}

private function getHomeUuid()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ protected function setUp(): void
$this->em->flush();

$this->getContainer()->get('sulu_security.system_store')->setSystem('Sulu');
$this->getContainer()->get('security.token_storage')->setToken(new UsernamePasswordToken($user, '', 'test'));
$this->getContainer()->get('security.token_storage')->setToken(new UsernamePasswordToken($user, 'test'));
}

private function prepareGetTestData()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ private function findUserByValidToken($token)
*/
private function loginUser(UserInterface $user, $request)
{
$token = new UsernamePasswordToken($user, null, 'admin', $user->getRoles());
$token = new UsernamePasswordToken($user, 'admin', $user->getRoles());
$this->tokenStorage->setToken($token); //now the user is logged in

//now dispatch the login event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function testGetResourceId(): void
public function testGetResourceTitle(): void
{
$user = $this->prophesize(UserInterface::class);
$user->getUsername()->shouldBeCalled()->willReturn('username');
$user->getUserIdentifier()->shouldBeCalled()->willReturn('username');
$event = new UserPasswordResettedEvent($user->reveal());

$this->assertSame('username', $event->getResourceTitle());
Expand Down
2 changes: 1 addition & 1 deletion src/Sulu/Bundle/SecurityBundle/User/UserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function loadUserByUsername($username)
return $this->loadUserByIdentifier($username);
}

public function loadUserByIdentifier(string $identifier)
public function loadUserByIdentifier(string $identifier): UserInterface
{
$exceptionMessage = \sprintf(
'Unable to find an Sulu\Component\Security\Authentication\UserInterface object identified by %s',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected function setUp(): void
);

$user = $this->getContainer()->get('test_user_provider')->getUser();
$this->getContainer()->get('security.token_storage')->setToken(new UsernamePasswordToken($user, '', 'test'));
$this->getContainer()->get('security.token_storage')->setToken(new UsernamePasswordToken($user, 'test'));
}

public function testLoadSnippetNotExists()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ public function testMainNavigationWithSecuredDocument()
$userRole->setRole($this->grantedRole);
$this->getEntityManager()->persist($userRole);
$this->user->addUserRole($userRole);
$this->tokenStorage->setToken(new UsernamePasswordToken($this->user, '', 'test'));
$this->tokenStorage->setToken(new UsernamePasswordToken($this->user, 'test'));

$main = $this->navigationMapper->getRootNavigation(
'sulu_io',
Expand Down Expand Up @@ -460,7 +460,7 @@ public function testNavigationExcerpt()
$userRole->setRole($this->grantedRole);
$this->getEntityManager()->persist($userRole);
$this->user->addUserRole($userRole);
$this->tokenStorage->setToken(new UsernamePasswordToken($this->user, '', 'test'));
$this->tokenStorage->setToken(new UsernamePasswordToken($this->user, 'test'));

$document = $this->createPageDocument();
$document->setStructureType('simple');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Prophecy\PhpUnit\ProphecyTrait;
use Sulu\Component\Security\Authorization\SecurityChecker;
use Sulu\Component\Security\Authorization\SecurityCheckerInterface;
use Symfony\Component\Security\Core\Authentication\Token\NullToken;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;

Expand Down Expand Up @@ -43,7 +44,8 @@ public function setUp(): void
parent::setUp();

$this->tokenStorage = $this->prophesize(TokenStorageInterface::class);
$this->tokenStorage->getToken()->willReturn(true); // stands for a valid token
$token = new NullToken(); // stands for a valid token
$this->tokenStorage->getToken()->willReturn($token);

$this->authorizationChecker = $this->prophesize(AuthorizationCheckerInterface::class);

Expand Down

0 comments on commit 84ca2b9

Please sign in to comment.