From 9e29c47affb1386467ac447486760181a88f85b1 Mon Sep 17 00:00:00 2001 From: Martin Helmich Date: Tue, 29 Oct 2024 17:43:14 +0100 Subject: [PATCH] Add test for TokenRetrievelKeyAuthenticator --- .../TokenRetrievalKeyAuthenticatorTest.php | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 tests/Security/TokenRetrievalKeyAuthenticatorTest.php diff --git a/tests/Security/TokenRetrievalKeyAuthenticatorTest.php b/tests/Security/TokenRetrievalKeyAuthenticatorTest.php new file mode 100644 index 0000000..ef5c0fe --- /dev/null +++ b/tests/Security/TokenRetrievalKeyAuthenticatorTest.php @@ -0,0 +1,78 @@ +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)); + } +} \ No newline at end of file