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;
|
|
|
|
|
2018-09-29 12:52:32 +02:00
|
|
|
use Cake\Chronos\Chronos;
|
2016-08-06 13:18:27 +02:00
|
|
|
use Doctrine\ORM\EntityManager;
|
|
|
|
use Doctrine\ORM\EntityRepository;
|
2023-02-09 20:42:18 +01:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2022-10-23 23:07:17 +02:00
|
|
|
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;
|
2022-09-23 19:03:32 +02:00
|
|
|
use Shlinkio\Shlink\Core\Domain\Entity\Domain;
|
2021-03-14 09:59:35 +01:00
|
|
|
use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta;
|
2021-01-10 20:05:14 +01:00
|
|
|
use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition;
|
2016-08-06 13:18:27 +02:00
|
|
|
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
|
|
|
use Shlinkio\Shlink\Rest\Service\ApiKeyService;
|
|
|
|
|
|
|
|
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 & EntityRepository $repo;
|
2016-08-06 13:18:27 +02:00
|
|
|
|
2022-09-11 12:02:49 +02:00
|
|
|
protected function setUp(): void
|
2016-08-06 13:18:27 +02:00
|
|
|
{
|
2022-10-23 23:07:17 +02:00
|
|
|
$this->em = $this->createMock(EntityManager::class);
|
|
|
|
$this->repo = $this->createMock(EntityRepository::class);
|
|
|
|
$this->service = new ApiKeyService($this->em);
|
2016-08-06 13:18:27 +02:00
|
|
|
}
|
|
|
|
|
2019-12-01 10:47:56 +01:00
|
|
|
/**
|
2021-01-10 20:05:14 +01:00
|
|
|
* @param RoleDefinition[] $roles
|
2019-12-01 10:47:56 +01:00
|
|
|
*/
|
2023-02-09 20:42:18 +01:00
|
|
|
#[Test, DataProvider('provideCreationDate')]
|
2021-03-06 17:27:34 +00:00
|
|
|
public function apiKeyIsProperlyCreated(?Chronos $date, ?string $name, array $roles): void
|
2016-08-06 13:18:27 +02:00
|
|
|
{
|
2022-10-23 23:07:17 +02:00
|
|
|
$this->em->expects($this->once())->method('flush');
|
|
|
|
$this->em->expects($this->once())->method('persist')->with($this->isInstanceOf(ApiKey::class));
|
2016-08-06 13:18:27 +02:00
|
|
|
|
2021-03-06 17:27:34 +00:00
|
|
|
$key = $this->service->create($date, $name, ...$roles);
|
2019-12-01 10:47:56 +01:00
|
|
|
|
2020-10-04 00:35:14 +02:00
|
|
|
self::assertEquals($date, $key->getExpirationDate());
|
2021-03-06 17:27:34 +00:00
|
|
|
self::assertEquals($name, $key->name());
|
2021-01-10 20:05:14 +01:00
|
|
|
foreach ($roles as $roleDefinition) {
|
2022-04-23 18:41:16 +02:00
|
|
|
self::assertTrue($key->hasRole($roleDefinition->role));
|
2021-01-10 20:05:14 +01:00
|
|
|
}
|
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),
|
2021-01-11 16:32:59 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-02-09 20:42:18 +01:00
|
|
|
#[Test, DataProvider('provideInvalidApiKeys')]
|
2019-12-01 10:47:56 +01:00
|
|
|
public function checkReturnsFalseForInvalidApiKeys(?ApiKey $invalidKey): void
|
2016-08-06 13:18:27 +02:00
|
|
|
{
|
2022-10-23 23:07:17 +02:00
|
|
|
$this->repo->expects($this->once())->method('findOneBy')->with(['key' => '12345'])->willReturn($invalidKey);
|
|
|
|
$this->em->method('getRepository')->with(ApiKey::class)->willReturn($this->repo);
|
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());
|
2022-04-23 14:00:47 +02:00
|
|
|
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
|
|
|
{
|
2019-12-01 10:47:56 +01:00
|
|
|
yield 'non-existent api key' => [null];
|
2021-03-14 09:59:35 +01:00
|
|
|
yield 'disabled api key' => [ApiKey::create()->disable()];
|
|
|
|
yield 'expired api key' => [ApiKey::fromMeta(ApiKeyMeta::withExpirationDate(Chronos::now()->subDay()))];
|
2016-08-06 13:18:27 +02:00
|
|
|
}
|
|
|
|
|
2023-02-09 20:42:18 +01:00
|
|
|
#[Test]
|
2019-12-01 10:47:56 +01:00
|
|
|
public function checkReturnsTrueWhenConditionsAreFavorable(): void
|
2016-08-06 13:18:27 +02:00
|
|
|
{
|
2021-03-14 09:59:35 +01:00
|
|
|
$apiKey = ApiKey::create();
|
2020-11-08 11:28:27 +01:00
|
|
|
|
2022-10-23 23:07:17 +02:00
|
|
|
$this->repo->expects($this->once())->method('findOneBy')->with(['key' => '12345'])->willReturn($apiKey);
|
|
|
|
$this->em->method('getRepository')->with(ApiKey::class)->willReturn($this->repo);
|
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());
|
2022-04-23 14:00:47 +02:00
|
|
|
self::assertSame($apiKey, $result->apiKey);
|
2016-08-06 13:18:27 +02:00
|
|
|
}
|
|
|
|
|
2023-02-09 20:42:18 +01:00
|
|
|
#[Test]
|
2019-12-01 10:47:56 +01:00
|
|
|
public function disableThrowsExceptionWhenNoApiKeyIsFound(): void
|
2016-08-06 13:18:27 +02:00
|
|
|
{
|
2022-10-23 23:07:17 +02:00
|
|
|
$this->repo->expects($this->once())->method('findOneBy')->with(['key' => '12345'])->willReturn(null);
|
|
|
|
$this->em->method('getRepository')->with(ApiKey::class)->willReturn($this->repo);
|
2016-08-06 13:18:27 +02:00
|
|
|
|
2019-02-16 10:53:45 +01:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2019-12-01 10:47:56 +01:00
|
|
|
|
2016-08-06 13:18:27 +02:00
|
|
|
$this->service->disable('12345');
|
|
|
|
}
|
|
|
|
|
2023-02-09 20:42:18 +01:00
|
|
|
#[Test]
|
2019-12-01 10:47:56 +01:00
|
|
|
public function disableReturnsDisabledApiKeyWhenFound(): void
|
2016-08-06 13:18:27 +02:00
|
|
|
{
|
2021-03-14 09:59:35 +01:00
|
|
|
$key = ApiKey::create();
|
2022-10-23 23:07:17 +02:00
|
|
|
$this->repo->expects($this->once())->method('findOneBy')->with(['key' => '12345'])->willReturn($key);
|
|
|
|
$this->em->method('getRepository')->with(ApiKey::class)->willReturn($this->repo);
|
|
|
|
$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());
|
2016-08-06 13:18:27 +02:00
|
|
|
$returnedKey = $this->service->disable('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
|
|
|
}
|
2016-08-06 18:08:09 +02:00
|
|
|
|
2023-02-09 20:42:18 +01:00
|
|
|
#[Test]
|
2019-12-01 10:47:56 +01:00
|
|
|
public function listFindsAllApiKeys(): void
|
2016-08-06 18:08:09 +02:00
|
|
|
{
|
2021-03-14 09:59:35 +01:00
|
|
|
$expectedApiKeys = [ApiKey::create(), ApiKey::create(), ApiKey::create()];
|
2019-12-01 10:47:56 +01:00
|
|
|
|
2022-10-23 23:07:17 +02:00
|
|
|
$this->repo->expects($this->once())->method('findBy')->with([])->willReturn($expectedApiKeys);
|
|
|
|
$this->em->method('getRepository')->with(ApiKey::class)->willReturn($this->repo);
|
2016-08-06 18:08:09 +02:00
|
|
|
|
2019-12-01 10:47:56 +01:00
|
|
|
$result = $this->service->listKeys();
|
|
|
|
|
2020-10-04 00:35:14 +02:00
|
|
|
self::assertEquals($expectedApiKeys, $result);
|
2016-08-06 18:08:09 +02:00
|
|
|
}
|
|
|
|
|
2023-02-09 20:42:18 +01:00
|
|
|
#[Test]
|
2019-12-01 10:47:56 +01:00
|
|
|
public function listEnabledFindsOnlyEnabledApiKeys(): void
|
2016-08-06 18:08:09 +02:00
|
|
|
{
|
2021-03-14 09:59:35 +01:00
|
|
|
$expectedApiKeys = [ApiKey::create(), ApiKey::create(), ApiKey::create()];
|
2019-12-01 10:47:56 +01:00
|
|
|
|
2022-10-23 23:07:17 +02:00
|
|
|
$this->repo->expects($this->once())->method('findBy')->with(['enabled' => true])->willReturn($expectedApiKeys);
|
|
|
|
$this->em->method('getRepository')->with(ApiKey::class)->willReturn($this->repo);
|
2016-08-06 18:08:09 +02:00
|
|
|
|
2019-12-01 10:47:56 +01:00
|
|
|
$result = $this->service->listKeys(true);
|
|
|
|
|
2020-10-04 00:35:14 +02:00
|
|
|
self::assertEquals($expectedApiKeys, $result);
|
2016-08-06 18:08:09 +02:00
|
|
|
}
|
2016-08-06 13:18:27 +02:00
|
|
|
}
|