From 89954e1a771fc99df156a72f8355d0d0453c24c7 Mon Sep 17 00:00:00 2001 From: Eldar Shahmaliyev Date: Sat, 25 Jan 2025 18:46:46 +0400 Subject: [PATCH] Add RefreshSession Lexicon and Unit Tests for Token Handling - Introduced `RefreshSession` class under `Atproto\Lexicons\Com\Atproto\Server` namespace. - Implements `LexiconContract` to handle session refresh logic. - Supports authenticated endpoints using the `AuthenticatedEndpoint` trait. - Allows token management with `token()` method to set or retrieve the token. - Added unit tests for `RefreshSession` in `tests/Unit/Lexicons/Com/Atproto/Server/RefreshSessionTest.php`. - Ensures tokens can be set and updated. - Verifies the functionality works without authentication. --- .../Com/Atproto/Server/RefreshSession.php | 61 +++++++++++++++++++ .../Com/Atproto/Server/RefreshSessionTest.php | 47 ++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 src/Lexicons/Com/Atproto/Server/RefreshSession.php create mode 100644 tests/Unit/Lexicons/Com/Atproto/Server/RefreshSessionTest.php diff --git a/src/Lexicons/Com/Atproto/Server/RefreshSession.php b/src/Lexicons/Com/Atproto/Server/RefreshSession.php new file mode 100644 index 0000000..26adfab --- /dev/null +++ b/src/Lexicons/Com/Atproto/Server/RefreshSession.php @@ -0,0 +1,61 @@ +update($client); + + $this->method('POST'); + + if ($token) { + $this->headers(array_merge(self::API_BASE_HEADERS, [ + 'Authorization' => "Bearer {$token}" + ])); + } + } + + public function update(SplSubject $client): void + { + parent::update($client); + + if ($authenticated = $client->authenticated()) { + $this->header("Authorization", "Bearer " . $authenticated->refreshJwt()); + } + } + + public function token(string $token = null) + { + if (! $token) { + return str_replace("Bearer ", '', $this->header('Authorization')); + } + + $this->header("Authorization", "Bearer $token"); + + return $this; + } + + public function build(): RequestContract + { + return $this; + } + + public function response(array $data): ResponseContract + { + return new SessionResponse($data); + } +} diff --git a/tests/Unit/Lexicons/Com/Atproto/Server/RefreshSessionTest.php b/tests/Unit/Lexicons/Com/Atproto/Server/RefreshSessionTest.php new file mode 100644 index 0000000..ff0aa1e --- /dev/null +++ b/tests/Unit/Lexicons/Com/Atproto/Server/RefreshSessionTest.php @@ -0,0 +1,47 @@ +com()->atproto()->server()->refreshSession()->forge() + ->token($expectedToken); + + $this->assertSame($expectedToken, $builder->token()); + } + + public function testItWorksWithoutAuth(): void + { + $this->expectNotToPerformAssertions(); + + (new Client())->com()->atproto()->server()->refreshSession()->forge(); + } + + public function testForgeCanSetNewToken(): void + { + $expectedToken = 'token'; + $actualToken = (new Client())->com()->atproto()->server()->refreshSession() + ->forge($expectedToken) + ->token(); + + $this->assertSame($expectedToken, $actualToken); + } + + public function testTokenCanUpdateTokenAfterForge(): void + { + $expectedToken = 'token'; + $actualToken = (new Client())->com()->atproto()->server()->refreshSession() + ->forge('another token') + ->token($expectedToken) + ->token(); + + $this->assertSame($expectedToken, $actualToken); + } +}