63 lines
2 KiB
PHP
63 lines
2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Infra\Adapters\Services;
|
|
|
|
use Application\Security\Models\ValueObjects\ClientIdentifier;
|
|
use Application\Security\Models\ValueObjects\Request\Nonce;
|
|
use Illuminate\Contracts\Cache\Repository as CacheRepository;
|
|
use Infra\Adapters\Services\CachedNonceValidator;
|
|
use Mockery;
|
|
use Psr\Log\NullLogger;
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
covers(CachedNonceValidator::class);
|
|
|
|
describe('CachedNonceValidator Unit Tests', function (): void {
|
|
it('validates and consumes a new nonce atomically', function (): void {
|
|
// GIVEN
|
|
$cacheStore = Mockery::mock(CacheRepository::class);
|
|
$logger = new NullLogger();
|
|
$validator = new CachedNonceValidator($logger, $cacheStore);
|
|
|
|
$clientIdentifier = new ClientIdentifier('arcadia_id_' . str_repeat('a', 32));
|
|
$nonce = new Nonce(Uuid::uuid7());
|
|
|
|
$expectedKey = 'nonce:' . $clientIdentifier->value . ':' . $nonce->value->toString();
|
|
|
|
$cacheStore->expects('add')
|
|
->once()
|
|
->with($expectedKey, 'used', 300)
|
|
->andReturn(true);
|
|
|
|
// WHEN
|
|
$isValid = $validator->validateAndConsume($nonce, $clientIdentifier);
|
|
|
|
// THEN
|
|
expect($isValid)->toBeTrue();
|
|
});
|
|
|
|
it('rejects an already consumed nonce when atomic add returns false', function (): void {
|
|
// GIVEN
|
|
$cacheStore = Mockery::mock(CacheRepository::class);
|
|
$logger = new NullLogger();
|
|
$validator = new CachedNonceValidator($logger, $cacheStore);
|
|
|
|
$clientIdentifier = new ClientIdentifier('arcadia_id_' . str_repeat('a', 32));
|
|
$nonce = new Nonce(Uuid::uuid7());
|
|
|
|
$expectedKey = 'nonce:' . $clientIdentifier->value . ':' . $nonce->value->toString();
|
|
|
|
$cacheStore->expects('add')
|
|
->once()
|
|
->with($expectedKey, 'used', 300)
|
|
->andReturn(false);
|
|
|
|
// WHEN
|
|
$isValid = $validator->validateAndConsume($nonce, $clientIdentifier);
|
|
|
|
// THEN
|
|
expect($isValid)->toBeFalse();
|
|
});
|
|
});
|