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