2022-11-16 17:57:38 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Integration\Services;
|
|
|
|
|
|
|
|
use App\Services\TokenManager;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
use Laravel\Sanctum\PersonalAccessToken;
|
2024-10-24 10:45:45 +00:00
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2022-11-16 17:57:38 +00:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
2024-01-11 12:41:33 +00:00
|
|
|
use function Tests\create_user;
|
|
|
|
|
2022-11-16 17:57:38 +00:00
|
|
|
class TokenManagerTest extends TestCase
|
|
|
|
{
|
|
|
|
private TokenManager $tokenManager;
|
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->tokenManager = app(TokenManager::class);
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function createTokenWithAllAbilities(): void
|
2022-11-16 17:57:38 +00:00
|
|
|
{
|
2024-01-11 12:41:33 +00:00
|
|
|
$token = $this->tokenManager->createToken(create_user());
|
2022-11-16 17:57:38 +00:00
|
|
|
|
|
|
|
self::assertTrue($token->accessToken->can('*'));
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function createTokenWithSpecificAbilities(): void
|
2022-11-16 17:57:38 +00:00
|
|
|
{
|
2024-01-11 12:41:33 +00:00
|
|
|
$token = $this->tokenManager->createToken(create_user(), ['audio']);
|
2022-11-16 17:57:38 +00:00
|
|
|
|
|
|
|
self::assertTrue($token->accessToken->can('audio'));
|
|
|
|
self::assertFalse($token->accessToken->can('video'));
|
|
|
|
self::assertFalse($token->accessToken->can('*'));
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function createCompositionToken(): void
|
2022-11-16 17:57:38 +00:00
|
|
|
{
|
2024-01-11 12:41:33 +00:00
|
|
|
$token = $this->tokenManager->createCompositeToken(create_user());
|
2022-11-16 17:57:38 +00:00
|
|
|
|
|
|
|
self::assertModelExists(PersonalAccessToken::findToken($token->apiToken));
|
|
|
|
|
|
|
|
$audioTokenInstance = PersonalAccessToken::findToken($token->audioToken);
|
|
|
|
self::assertModelExists($audioTokenInstance);
|
|
|
|
|
|
|
|
/** @var string $cachedAudioToken */
|
2024-01-09 23:26:16 +00:00
|
|
|
$cachedAudioToken = Cache::get("app.composite-tokens.$token->apiToken");
|
2022-11-16 17:57:38 +00:00
|
|
|
self::assertTrue($audioTokenInstance->is(PersonalAccessToken::findToken($cachedAudioToken)));
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function deleteCompositionToken(): void
|
2022-11-16 17:57:38 +00:00
|
|
|
{
|
2024-01-11 12:41:33 +00:00
|
|
|
$token = $this->tokenManager->createCompositeToken(create_user());
|
2022-11-16 17:57:38 +00:00
|
|
|
|
|
|
|
$this->tokenManager->deleteCompositionToken($token->apiToken);
|
|
|
|
|
|
|
|
self::assertNull(PersonalAccessToken::findToken($token->apiToken));
|
|
|
|
self::assertNull(PersonalAccessToken::findToken($token->audioToken));
|
2024-01-09 23:26:16 +00:00
|
|
|
self::assertNull(Cache::get("app.composite-tokens.$token->apiToken"));
|
2022-11-16 17:57:38 +00:00
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function destroyTokens(): void
|
2022-11-16 17:57:38 +00:00
|
|
|
{
|
2024-01-11 12:41:33 +00:00
|
|
|
$user = create_user();
|
2022-11-16 17:57:38 +00:00
|
|
|
$user->createToken('foo');
|
|
|
|
$user->createToken('bar');
|
|
|
|
|
|
|
|
self::assertSame(2, $user->tokens()->count());
|
|
|
|
|
|
|
|
$this->tokenManager->destroyTokens($user);
|
|
|
|
|
|
|
|
self::assertSame(0, $user->tokens()->count());
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function deleteTokenByPlainTextToken(): void
|
2022-11-16 17:57:38 +00:00
|
|
|
{
|
2024-01-11 12:41:33 +00:00
|
|
|
$token = $this->tokenManager->createToken(create_user());
|
2022-11-16 17:57:38 +00:00
|
|
|
self::assertModelExists($token->accessToken);
|
|
|
|
|
|
|
|
$this->tokenManager->deleteTokenByPlainTextToken($token->plainTextToken);
|
|
|
|
|
|
|
|
self::assertModelMissing($token->accessToken);
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function getUserFromPlainTextToken(): void
|
2022-11-16 17:57:38 +00:00
|
|
|
{
|
2024-01-11 12:41:33 +00:00
|
|
|
$user = create_user();
|
2022-11-16 17:57:38 +00:00
|
|
|
$token = $this->tokenManager->createToken($user);
|
|
|
|
|
|
|
|
self::assertTrue($user->is($this->tokenManager->getUserFromPlainTextToken($token->plainTextToken)));
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function replaceApiToken(): void
|
2022-11-16 17:57:38 +00:00
|
|
|
{
|
2024-01-11 12:41:33 +00:00
|
|
|
$oldToken = $this->tokenManager->createToken(create_user());
|
2022-11-16 17:57:38 +00:00
|
|
|
$newToken = $this->tokenManager->refreshApiToken($oldToken->plainTextToken);
|
|
|
|
|
|
|
|
self::assertModelMissing($oldToken->accessToken);
|
|
|
|
self::assertModelExists($newToken->accessToken);
|
|
|
|
}
|
|
|
|
}
|