Files
shlink/module/Rest/test/Service/ApiKeyServiceTest.php

270 lines
9.7 KiB
PHP
Raw Normal View History

2016-08-06 13:18:27 +02:00
<?php
2019-10-05 17:26:10 +02:00
2017-10-12 10:13:20 +02:00
declare(strict_types=1);
2016-08-06 13:18:27 +02:00
namespace ShlinkioTest\Shlink\Rest\Service;
use Cake\Chronos\Chronos;
2016-08-06 13:18:27 +02:00
use Doctrine\ORM\EntityManager;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
2017-03-24 20:34:18 +01:00
use PHPUnit\Framework\TestCase;
2019-02-16 10:53:45 +01:00
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
use Shlinkio\Shlink\Core\Domain\Entity\Domain;
2024-11-08 08:25:07 +01:00
use Shlinkio\Shlink\Core\Model\Renaming;
use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta;
use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition;
use Shlinkio\Shlink\Rest\ApiKey\Repository\ApiKeyRepositoryInterface;
2016-08-06 13:18:27 +02:00
use Shlinkio\Shlink\Rest\Entity\ApiKey;
use Shlinkio\Shlink\Rest\Service\ApiKeyService;
use function substr;
2016-08-06 13:18:27 +02:00
class ApiKeyServiceTest extends TestCase
{
2020-01-01 20:48:31 +01:00
private ApiKeyService $service;
2022-10-24 19:53:13 +02:00
private MockObject & EntityManager $em;
private MockObject & ApiKeyRepositoryInterface $repo;
2016-08-06 13:18:27 +02:00
protected function setUp(): void
2016-08-06 13:18:27 +02:00
{
$this->em = $this->createMock(EntityManager::class);
$this->em->method('wrapInTransaction')->willReturnCallback(fn (callable $callback) => $callback());
$this->repo = $this->createMock(ApiKeyRepositoryInterface::class);
$this->service = new ApiKeyService($this->em, $this->repo);
2016-08-06 13:18:27 +02:00
}
/**
* @param RoleDefinition[] $roles
*/
#[Test, DataProvider('provideCreationDate')]
2024-10-28 22:27:30 +01:00
public function apiKeyIsProperlyCreated(Chronos|null $date, string|null $name, array $roles): void
2016-08-06 13:18:27 +02:00
{
$this->repo->expects($this->once())->method('nameExists')->with(
! empty($name) ? $name : $this->isType('string'),
)->willReturn(false);
$this->em->expects($this->once())->method('persist')->with($this->isInstanceOf(ApiKey::class));
2016-08-06 13:18:27 +02:00
$meta = ApiKeyMeta::fromParams(name: $name, expirationDate: $date, roleDefinitions: $roles);
$key = $this->service->create($meta);
2024-03-18 18:33:56 +01:00
self::assertEquals($date, $key->expirationDate);
self::assertEquals(
empty($name) ? substr($meta->key, 0, 8) . '-****-****-****-************' : $name,
$key->name,
);
foreach ($roles as $roleDefinition) {
2022-04-23 18:41:16 +02:00
self::assertTrue($key->hasRole($roleDefinition->role));
}
2016-08-06 13:18:27 +02:00
}
2023-02-09 09:32:38 +01:00
public static function provideCreationDate(): iterable
2016-08-06 13:18:27 +02:00
{
2022-10-24 19:53:13 +02:00
$domain = Domain::withAuthority('');
$domain->setId('123');
2021-03-06 17:27:34 +00:00
yield 'no expiration date or name' => [null, null, []];
yield 'expiration date' => [Chronos::parse('2030-01-01'), null, []];
yield 'roles' => [null, null, [
2022-10-24 19:53:13 +02:00
RoleDefinition::forDomain($domain),
RoleDefinition::forAuthoredShortUrls(),
]];
2021-03-06 17:27:34 +00:00
yield 'single name' => [null, 'Alice', []];
yield 'multi-word name' => [null, 'Alice and Bob', []];
yield 'empty name' => [null, '', []];
2016-08-06 13:18:27 +02:00
}
#[Test]
public function autoGeneratedNameIsRegeneratedIfAlreadyExists(): void
{
$callCount = 0;
$this->repo->expects($this->exactly(3))->method('nameExists')->with(
$this->isType('string'),
)->willReturnCallback(function () use (&$callCount): bool {
$callCount++;
return $callCount < 3;
});
$this->em->expects($this->once())->method('persist')->with($this->isInstanceOf(ApiKey::class));
$this->service->create(ApiKeyMeta::create());
}
#[Test]
public function exceptionIsThrownWhileCreatingIfExplicitlyProvidedNameIsInUse(): void
{
$this->repo->expects($this->once())->method('nameExists')->with('the_name')->willReturn(true);
$this->em->expects($this->never())->method('persist');
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Another API key with name "the_name" already exists');
$this->service->create(ApiKeyMeta::fromParams(name: 'the_name'));
}
#[Test, DataProvider('provideInvalidApiKeys')]
2024-10-28 22:27:30 +01:00
public function checkReturnsFalseForInvalidApiKeys(ApiKey|null $invalidKey): void
2016-08-06 13:18:27 +02:00
{
$this->repo->expects($this->once())->method('findOneBy')->with(['key' => ApiKey::hashKey('12345')])->willReturn(
$invalidKey,
);
2016-08-06 13:18:27 +02:00
2020-11-08 11:28:27 +01:00
$result = $this->service->check('12345');
self::assertFalse($result->isValid());
self::assertSame($invalidKey, $result->apiKey);
2016-08-06 13:18:27 +02:00
}
2023-02-09 09:32:38 +01:00
public static function provideInvalidApiKeys(): iterable
2016-08-06 13:18:27 +02:00
{
yield 'non-existent api key' => [null];
yield 'disabled api key' => [ApiKey::create()->disable()];
2023-09-23 09:06:38 +02:00
yield 'expired api key' => [
ApiKey::fromMeta(ApiKeyMeta::fromParams(expirationDate: Chronos::now()->subDays(1))),
];
2016-08-06 13:18:27 +02:00
}
#[Test]
public function checkReturnsTrueWhenConditionsAreFavorable(): void
2016-08-06 13:18:27 +02:00
{
$apiKey = ApiKey::create();
2020-11-08 11:28:27 +01:00
$this->repo->expects($this->once())->method('findOneBy')->with(['key' => ApiKey::hashKey('12345')])->willReturn(
$apiKey,
);
2016-08-06 13:18:27 +02:00
2020-11-08 11:28:27 +01:00
$result = $this->service->check('12345');
self::assertTrue($result->isValid());
self::assertSame($apiKey, $result->apiKey);
2016-08-06 13:18:27 +02:00
}
#[Test, DataProvider('provideDisableArgs')]
public function disableThrowsExceptionWhenNoApiKeyIsFound(string $disableMethod, array $findOneByArg): void
2016-08-06 13:18:27 +02:00
{
$this->repo->expects($this->once())->method('findOneBy')->with($findOneByArg)->willReturn(null);
2016-08-06 13:18:27 +02:00
2019-02-16 10:53:45 +01:00
$this->expectException(InvalidArgumentException::class);
$this->service->{$disableMethod}('12345');
2016-08-06 13:18:27 +02:00
}
#[Test, DataProvider('provideDisableArgs')]
public function disableReturnsDisabledApiKeyWhenFound(string $disableMethod, array $findOneByArg): void
2016-08-06 13:18:27 +02:00
{
$key = ApiKey::create();
$this->repo->expects($this->once())->method('findOneBy')->with($findOneByArg)->willReturn($key);
$this->em->expects($this->once())->method('flush');
2016-08-06 13:18:27 +02:00
2020-10-04 00:35:14 +02:00
self::assertTrue($key->isEnabled());
$returnedKey = $this->service->{$disableMethod}('12345');
2020-10-04 00:35:14 +02:00
self::assertFalse($key->isEnabled());
self::assertSame($key, $returnedKey);
2016-08-06 13:18:27 +02:00
}
public static function provideDisableArgs(): iterable
{
yield 'disableByKey' => ['disableByKey', ['key' => ApiKey::hashKey('12345')]];
yield 'disableByName' => ['disableByName', ['name' => '12345']];
}
#[Test]
public function listFindsAllApiKeys(): void
{
$expectedApiKeys = [ApiKey::create(), ApiKey::create(), ApiKey::create()];
$this->repo->expects($this->once())->method('findBy')->with([])->willReturn($expectedApiKeys);
$result = $this->service->listKeys();
2020-10-04 00:35:14 +02:00
self::assertEquals($expectedApiKeys, $result);
}
#[Test]
public function listEnabledFindsOnlyEnabledApiKeys(): void
{
$expectedApiKeys = [ApiKey::create(), ApiKey::create(), ApiKey::create()];
$this->repo->expects($this->once())->method('findBy')->with(['enabled' => true])->willReturn($expectedApiKeys);
2023-09-19 09:10:17 +02:00
$result = $this->service->listKeys(enabledOnly: true);
2020-10-04 00:35:14 +02:00
self::assertEquals($expectedApiKeys, $result);
}
#[Test, DataProvider('provideInitialApiKeys')]
2024-10-28 22:27:30 +01:00
public function createInitialDelegatesToRepository(ApiKey|null $apiKey): void
{
$this->repo->expects($this->once())->method('createInitialApiKey')->with('the_key')->willReturn($apiKey);
$result = $this->service->createInitial('the_key');
self::assertSame($result, $apiKey);
}
public static function provideInitialApiKeys(): iterable
{
yield 'first api key' => [ApiKey::create()];
yield 'existing api keys' => [null];
}
2024-11-08 08:25:07 +01:00
#[Test]
public function renameApiKeyThrowsExceptionIfApiKeyIsNotFound(): void
{
$renaming = Renaming::fromNames(oldName: 'old', newName: 'new');
$this->repo->expects($this->once())->method('findOneBy')->with(['name' => 'old'])->willReturn(null);
$this->repo->expects($this->never())->method('nameExists');
2024-11-08 08:25:07 +01:00
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('API key with name "old" could not be found');
$this->service->renameApiKey($renaming);
}
#[Test]
public function renameApiKeyReturnsApiKeyVerbatimIfBothNamesAreEqual(): void
{
$renaming = Renaming::fromNames(oldName: 'same_value', newName: 'same_value');
$apiKey = ApiKey::create();
$this->repo->expects($this->once())->method('findOneBy')->with(['name' => 'same_value'])->willReturn($apiKey);
$this->repo->expects($this->never())->method('nameExists');
2024-11-08 08:25:07 +01:00
$result = $this->service->renameApiKey($renaming);
self::assertSame($apiKey, $result);
}
#[Test]
public function renameApiKeyThrowsExceptionIfNewNameIsInUse(): void
{
$renaming = Renaming::fromNames(oldName: 'old', newName: 'new');
$apiKey = ApiKey::create();
$this->repo->expects($this->once())->method('findOneBy')->with(['name' => 'old'])->willReturn($apiKey);
$this->repo->expects($this->once())->method('nameExists')->with('new')->willReturn(true);
2024-11-08 08:25:07 +01:00
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Another API key with name "new" already exists');
$this->service->renameApiKey($renaming);
}
#[Test]
public function renameApiKeyReturnsApiKeyWithNewName(): void
{
$renaming = Renaming::fromNames(oldName: 'old', newName: 'new');
$apiKey = ApiKey::fromMeta(ApiKeyMeta::fromParams(name: 'old'));
$this->repo->expects($this->once())->method('findOneBy')->with(['name' => 'old'])->willReturn($apiKey);
$this->repo->expects($this->once())->method('nameExists')->with('new')->willReturn(false);
2024-11-08 08:25:07 +01:00
$result = $this->service->renameApiKey($renaming);
self::assertSame($apiKey, $result);
self::assertEquals('new', $apiKey->name);
}
2016-08-06 13:18:27 +02:00
}