Skip to content

Commit

Permalink
Add test for TokenRetrievelKeyAuthenticator
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-helmich committed Oct 29, 2024
1 parent 3d52da5 commit 9e29c47
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions tests/Security/TokenRetrievalKeyAuthenticatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
namespace Mittwald\MStudio\Bundle\Security;

use DateTime;
use Mittwald\MStudio\Authentication\AuthenticationService;
use Mittwald\MStudio\Authentication\SSOToken;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use function PHPUnit\Framework\assertThat;
use function PHPUnit\Framework\equalTo;
use function PHPUnit\Framework\identicalTo;
use function PHPUnit\Framework\isInstanceOf;
use function PHPUnit\Framework\isNull;
use function PHPUnit\Framework\isTrue;
use function PHPUnit\Framework\logicalNot;
use function PHPUnit\Framework\once;

#[CoversClass(TokenRetrievalKeyAuthenticator::class)]
class TokenRetrievalKeyAuthenticatorTest extends TestCase
{
#[Test]
public function supportsRequestWithTokenInQuery(): void
{
$retrievalKey = base64_encode(random_bytes(16));

$request = new Request();
$request->query->set('accessTokenRetrievalKey', $retrievalKey);

$authService = $this
->getMockBuilder(AuthenticationService::class)
->disableOriginalConstructor()
->getMock();

$sut = new TokenRetrievalKeyAuthenticator($authService);

assertThat($sut->supports($request), isTrue());
}

#[Test]
public function authenticatesRequestWithCorrectUser(): void
{
$retrievalKey = base64_encode(random_bytes(16));
$userId = uuid_create(UUID_TYPE_RANDOM);

$request = new Request();
$request->query->set('accessTokenRetrievalKey', $retrievalKey);
$request->query->set('userId', $userId);

$token = new SSOToken(
accessToken: base64_encode(random_bytes(16)),
refreshToken: base64_encode(random_bytes(16)),
expiresAt: new DateTime("now + 3 days"),
);

$authService = $this
->getMockBuilder(AuthenticationService::class)
->disableOriginalConstructor()
->getMock();
$authService
->expects(once())
->method('authenticate')
->with($userId, $retrievalKey)
->willReturn($token);

$sut = new TokenRetrievalKeyAuthenticator($authService);
$passport = $sut->authenticate($request);

/** @var User $user */
$user = $passport->getUser();

assertThat($user, logicalNot(isNull()));
assertThat($user->getUserIdentifier(), equalTo($userId));
assertThat($user, isInstanceOf(User::class));
assertThat($user->getToken(), identicalTo($token));
}
}

0 comments on commit 9e29c47

Please sign in to comment.