Commit 60b655cc authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

add tokens tests

parent bdc39cb7
......@@ -69,10 +69,11 @@ class DatabaseTokensRepository implements TokensRepository
*/
public function invalidateToken(string $token): void
{
$found = \App\Token::where('api_token', $token)
->where('expires_at', '<', Carbon::now())
->where('expires_at', '!=', null)
->first();
$found = \App\Token::where('api_token', $token)->where('expires_at', '>', Carbon::now())->orWhere(
static function (Builder $query) use ($token) {
$query->where('api_token', $token)->where('expires_at', null);
}
)->first();
if ($found) {
$found->expires_at = Carbon::now();
......
<?php
namespace Tests\Database;
use Carbon\Carbon;
use Source\Entities\User;
use Source\Entities\Token;
use Tests\DatabaseTestCase;
use Source\Exceptions\EntityNotFoundException;
use Source\Gateways\Users\DatabaseUsersRepository;
use Source\Gateways\Tokens\DatabaseTokensRepository;
class TokenDatabaseTest extends DatabaseTestCase
{
/**
* @var \Source\Gateways\Tokens\DatabaseTokensRepository
*/
protected DatabaseTokensRepository $tokens;
/**
* @var \Source\Gateways\Users\DatabaseUsersRepository
*/
protected DatabaseUsersRepository $users;
public function setUp(): void
{
parent::setUp();
$this->tokens = new DatabaseTokensRepository();
$this->users = new DatabaseUsersRepository();
$this->users->create(new User(0, '', '', '', null, '', null, null));
}
/**
* @test
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function it_creates_and_finds_tokens(): void
{
$token = $this->tokens->create(new Token(0, 1, 'token'));
$dbToken = $this->tokens->findValidToken('token');
$this->assertEquals($token, $dbToken);
$this->assertEquals(1, $dbToken->getUserId());
$nullToken = $this->tokens->findValidToken('reeee');
$this->assertNull($nullToken);
}
/**
* @test
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function it_cannot_find_expired_tokens(): void
{
$this->tokens->create(new Token(0, 1, 'token', 'nomen', Carbon::now()->subDays(1)));
$token = $this->tokens->findValidToken('token');
$this->assertNull($token);
}
/**
* @test
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function it_can_find_tokens_with_an_expired_date_in_the_future(): void
{
$t = $this->tokens->create(new Token(0, 1, 'token', 'nomen', Carbon::now()->addDays(1)));
$token = $this->tokens->findValidToken('token');
$this->assertEquals($t, $token);
}
/**
* @test
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function it_cannot_create_a_token_for_non_existent_users(): void
{
$this->expectException(EntityNotFoundException::class);
$this->tokens->create(new Token(0, 500, 'token'));
}
/**
* @test
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function it_invalidates_tokens(): void
{
$this->tokens->create(new Token(0, 1, 'token', 'nomen', Carbon::now()->addDays(1)));
$this->tokens->invalidateToken('token');
$this->assertNull($this->tokens->findValidToken('token'));
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment