2016-08-06 06:18:27 -05:00
|
|
|
<?php
|
2019-10-05 10:26:10 -05:00
|
|
|
|
2017-10-12 03:13:20 -05:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-08-06 06:18:27 -05:00
|
|
|
namespace ShlinkioTest\Shlink\Rest\Service;
|
|
|
|
|
2018-09-29 05:52:32 -05:00
|
|
|
use Cake\Chronos\Chronos;
|
2016-08-06 06:18:27 -05:00
|
|
|
use Doctrine\ORM\EntityManager;
|
|
|
|
use Doctrine\ORM\EntityRepository;
|
2023-02-09 13:42:18 -06:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2022-10-23 16:07:17 -05:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2017-03-24 14:34:18 -05:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2019-02-16 03:53:45 -06:00
|
|
|
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
|
2022-09-23 12:03:32 -05:00
|
|
|
use Shlinkio\Shlink\Core\Domain\Entity\Domain;
|
2021-03-14 03:59:35 -05:00
|
|
|
use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta;
|
2021-01-10 13:05:14 -06:00
|
|
|
use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition;
|
2016-08-06 06:18:27 -05:00
|
|
|
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
|
|
|
use Shlinkio\Shlink\Rest\Service\ApiKeyService;
|
|
|
|
|
|
|
|
class ApiKeyServiceTest extends TestCase
|
|
|
|
{
|
2020-01-01 13:48:31 -06:00
|
|
|
private ApiKeyService $service;
|
2022-10-24 12:53:13 -05:00
|
|
|
private MockObject & EntityManager $em;
|
|
|
|
private MockObject & EntityRepository $repo;
|
2016-08-06 06:18:27 -05:00
|
|
|
|
2022-09-11 05:02:49 -05:00
|
|
|
protected function setUp(): void
|
2016-08-06 06:18:27 -05:00
|
|
|
{
|
2022-10-23 16:07:17 -05:00
|
|
|
$this->em = $this->createMock(EntityManager::class);
|
|
|
|
$this->repo = $this->createMock(EntityRepository::class);
|
|
|
|
$this->service = new ApiKeyService($this->em);
|
2016-08-06 06:18:27 -05:00
|
|
|
}
|
|
|
|
|
2019-12-01 03:47:56 -06:00
|
|
|
/**
|
2021-01-10 13:05:14 -06:00
|
|
|
* @param RoleDefinition[] $roles
|
2019-12-01 03:47:56 -06:00
|
|
|
*/
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test, DataProvider('provideCreationDate')]
|
2021-03-06 11:27:34 -06:00
|
|
|
public function apiKeyIsProperlyCreated(?Chronos $date, ?string $name, array $roles): void
|
2016-08-06 06:18:27 -05:00
|
|
|
{
|
2022-10-23 16:07:17 -05:00
|
|
|
$this->em->expects($this->once())->method('flush');
|
|
|
|
$this->em->expects($this->once())->method('persist')->with($this->isInstanceOf(ApiKey::class));
|
2016-08-06 06:18:27 -05:00
|
|
|
|
2021-03-06 11:27:34 -06:00
|
|
|
$key = $this->service->create($date, $name, ...$roles);
|
2019-12-01 03:47:56 -06:00
|
|
|
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertEquals($date, $key->getExpirationDate());
|
2021-03-06 11:27:34 -06:00
|
|
|
self::assertEquals($name, $key->name());
|
2021-01-10 13:05:14 -06:00
|
|
|
foreach ($roles as $roleDefinition) {
|
2022-04-23 11:41:16 -05:00
|
|
|
self::assertTrue($key->hasRole($roleDefinition->role));
|
2021-01-10 13:05:14 -06:00
|
|
|
}
|
2016-08-06 06:18:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-09 02:32:38 -06:00
|
|
|
public static function provideCreationDate(): iterable
|
2016-08-06 06:18:27 -05:00
|
|
|
{
|
2022-10-24 12:53:13 -05:00
|
|
|
$domain = Domain::withAuthority('');
|
|
|
|
$domain->setId('123');
|
|
|
|
|
2021-03-06 11:27:34 -06: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 12:53:13 -05:00
|
|
|
RoleDefinition::forDomain($domain),
|
2021-01-11 09:32:59 -06:00
|
|
|
RoleDefinition::forAuthoredShortUrls(),
|
|
|
|
]];
|
2021-03-06 11:27:34 -06:00
|
|
|
yield 'single name' => [null, 'Alice', []];
|
|
|
|
yield 'multi-word name' => [null, 'Alice and Bob', []];
|
|
|
|
yield 'empty name' => [null, '', []];
|
2016-08-06 06:18:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test, DataProvider('provideInvalidApiKeys')]
|
2019-12-01 03:47:56 -06:00
|
|
|
public function checkReturnsFalseForInvalidApiKeys(?ApiKey $invalidKey): void
|
2016-08-06 06:18:27 -05:00
|
|
|
{
|
2022-10-23 16:07:17 -05: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 06:18:27 -05:00
|
|
|
|
2020-11-08 04:28:27 -06:00
|
|
|
$result = $this->service->check('12345');
|
|
|
|
|
|
|
|
self::assertFalse($result->isValid());
|
2022-04-23 07:00:47 -05:00
|
|
|
self::assertSame($invalidKey, $result->apiKey);
|
2016-08-06 06:18:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-09 02:32:38 -06:00
|
|
|
public static function provideInvalidApiKeys(): iterable
|
2016-08-06 06:18:27 -05:00
|
|
|
{
|
2019-12-01 03:47:56 -06:00
|
|
|
yield 'non-existent api key' => [null];
|
2021-03-14 03:59:35 -05:00
|
|
|
yield 'disabled api key' => [ApiKey::create()->disable()];
|
|
|
|
yield 'expired api key' => [ApiKey::fromMeta(ApiKeyMeta::withExpirationDate(Chronos::now()->subDay()))];
|
2016-08-06 06:18:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test]
|
2019-12-01 03:47:56 -06:00
|
|
|
public function checkReturnsTrueWhenConditionsAreFavorable(): void
|
2016-08-06 06:18:27 -05:00
|
|
|
{
|
2021-03-14 03:59:35 -05:00
|
|
|
$apiKey = ApiKey::create();
|
2020-11-08 04:28:27 -06:00
|
|
|
|
2022-10-23 16:07:17 -05: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 06:18:27 -05:00
|
|
|
|
2020-11-08 04:28:27 -06:00
|
|
|
$result = $this->service->check('12345');
|
|
|
|
|
|
|
|
self::assertTrue($result->isValid());
|
2022-04-23 07:00:47 -05:00
|
|
|
self::assertSame($apiKey, $result->apiKey);
|
2016-08-06 06:18:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test]
|
2019-12-01 03:47:56 -06:00
|
|
|
public function disableThrowsExceptionWhenNoApiKeyIsFound(): void
|
2016-08-06 06:18:27 -05:00
|
|
|
{
|
2022-10-23 16:07:17 -05: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 06:18:27 -05:00
|
|
|
|
2019-02-16 03:53:45 -06:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2019-12-01 03:47:56 -06:00
|
|
|
|
2016-08-06 06:18:27 -05:00
|
|
|
$this->service->disable('12345');
|
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test]
|
2019-12-01 03:47:56 -06:00
|
|
|
public function disableReturnsDisabledApiKeyWhenFound(): void
|
2016-08-06 06:18:27 -05:00
|
|
|
{
|
2021-03-14 03:59:35 -05:00
|
|
|
$key = ApiKey::create();
|
2022-10-23 16:07:17 -05: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 06:18:27 -05:00
|
|
|
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertTrue($key->isEnabled());
|
2016-08-06 06:18:27 -05:00
|
|
|
$returnedKey = $this->service->disable('12345');
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertFalse($key->isEnabled());
|
|
|
|
self::assertSame($key, $returnedKey);
|
2016-08-06 06:18:27 -05:00
|
|
|
}
|
2016-08-06 11:08:09 -05:00
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test]
|
2019-12-01 03:47:56 -06:00
|
|
|
public function listFindsAllApiKeys(): void
|
2016-08-06 11:08:09 -05:00
|
|
|
{
|
2021-03-14 03:59:35 -05:00
|
|
|
$expectedApiKeys = [ApiKey::create(), ApiKey::create(), ApiKey::create()];
|
2019-12-01 03:47:56 -06:00
|
|
|
|
2022-10-23 16:07:17 -05:00
|
|
|
$this->repo->expects($this->once())->method('findBy')->with([])->willReturn($expectedApiKeys);
|
|
|
|
$this->em->method('getRepository')->with(ApiKey::class)->willReturn($this->repo);
|
2016-08-06 11:08:09 -05:00
|
|
|
|
2019-12-01 03:47:56 -06:00
|
|
|
$result = $this->service->listKeys();
|
|
|
|
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertEquals($expectedApiKeys, $result);
|
2016-08-06 11:08:09 -05:00
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test]
|
2019-12-01 03:47:56 -06:00
|
|
|
public function listEnabledFindsOnlyEnabledApiKeys(): void
|
2016-08-06 11:08:09 -05:00
|
|
|
{
|
2021-03-14 03:59:35 -05:00
|
|
|
$expectedApiKeys = [ApiKey::create(), ApiKey::create(), ApiKey::create()];
|
2019-12-01 03:47:56 -06:00
|
|
|
|
2022-10-23 16:07:17 -05: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 11:08:09 -05:00
|
|
|
|
2019-12-01 03:47:56 -06:00
|
|
|
$result = $this->service->listKeys(true);
|
|
|
|
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertEquals($expectedApiKeys, $result);
|
2016-08-06 11:08:09 -05:00
|
|
|
}
|
2016-08-06 06:18:27 -05:00
|
|
|
}
|