Skip to content
New issue

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

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

Already on GitHub? # to your account

[BSS-106] Add RefreshSession Lexicon and Unit Tests for Token Handling #51

Merged
merged 1 commit into from
Jan 25, 2025
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
61 changes: 61 additions & 0 deletions src/Lexicons/Com/Atproto/Server/RefreshSession.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Atproto\Lexicons\Com\Atproto\Server;

use Atproto\Client;
use Atproto\Contracts\LexiconContract;
use Atproto\Contracts\Lexicons\RequestContract;
use Atproto\Contracts\Resources\ResponseContract;
use Atproto\Lexicons\APIRequest;
use Atproto\Lexicons\Traits\AuthenticatedEndpoint;
use Atproto\Responses\Com\Atproto\Server\CreateSessionResponse as SessionResponse;
use SplSubject;

class RefreshSession extends APIRequest implements LexiconContract
{
use AuthenticatedEndpoint;

public function __construct(Client $client, string $token = null)
{
parent::__construct($client);
$this->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);
}
}
47 changes: 47 additions & 0 deletions tests/Unit/Lexicons/Com/Atproto/Server/RefreshSessionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Tests\Unit\Lexicons\Com\Atproto\Server;

use Atproto\Client;
use PHPUnit\Framework\TestCase;

class RefreshSessionTest extends TestCase
{
public function testTokenCanSetNewToken()
{
$expectedToken = 'token';

$builder = (new Client())->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);
}
}
Loading