<?php
declare(strict_types=1);
namespace Tests\Feature;
use App\Models\MicropubToken;
use App\Services\TokenService;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
class TokenServiceTest extends TestCase
{
/**
* Given the token is dependent on a random value and stored only as a
* hash, to test, we shall create a token, and then verify it.
*/
#[Test]
public function tokenservice_creates_valid_tokens(): void
$tokenService = new TokenService;
$data = [
'me' => 'https://example.org',
'client_id' => 'https://quill.p3k.io',
'scope' => 'post',
];
$token = $tokenService->getNewToken($data);
$response = $this->get('/api/post', ['HTTP_Authorization' => 'Bearer '.$token]);
$response->assertJson([
'response' => 'token',
'token' => [
'me' => $data['me'],
'client_id' => $data['client_id'],
'scope' => $data['scope'],
],
]);
}
public function unknown_tokens_are_not_valid(): void
$response = $this->get('/api/post', ['HTTP_Authorization' => 'Bearer '.bin2hex(random_bytes(32))]);
'response' => 'error',
'error' => 'invalid_token',
'error_description' => 'The provided token did not pass validation',
public function revoked_tokens_are_not_valid(): void
MicropubToken::where('token_hash', hash('sha256', $token))->firstOrFail()->revoke();