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;
|
2017-03-24 14:34:18 -05:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-08-06 06:18:27 -05:00
|
|
|
use Prophecy\Argument;
|
2020-11-02 04:50:19 -06:00
|
|
|
use Prophecy\PhpUnit\ProphecyTrait;
|
2016-08-06 06:18:27 -05:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
2019-02-16 03:53:45 -06:00
|
|
|
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
|
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-11-02 04:50:19 -06:00
|
|
|
use ProphecyTrait;
|
|
|
|
|
2020-01-01 13:48:31 -06:00
|
|
|
private ApiKeyService $service;
|
|
|
|
private ObjectProphecy $em;
|
2016-08-06 06:18:27 -05:00
|
|
|
|
2019-02-16 03:53:45 -06:00
|
|
|
public function setUp(): void
|
2016-08-06 06:18:27 -05:00
|
|
|
{
|
|
|
|
$this->em = $this->prophesize(EntityManager::class);
|
|
|
|
$this->service = new ApiKeyService($this->em->reveal());
|
|
|
|
}
|
|
|
|
|
2019-12-01 03:47:56 -06:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideCreationDate
|
2021-01-10 13:05:14 -06:00
|
|
|
* @param RoleDefinition[] $roles
|
2019-12-01 03:47:56 -06:00
|
|
|
*/
|
2021-01-10 13:05:14 -06:00
|
|
|
public function apiKeyIsProperlyCreated(?Chronos $date, array $roles): void
|
2016-08-06 06:18:27 -05:00
|
|
|
{
|
2018-11-11 06:18:21 -06:00
|
|
|
$this->em->flush()->shouldBeCalledOnce();
|
|
|
|
$this->em->persist(Argument::type(ApiKey::class))->shouldBeCalledOnce();
|
2016-08-06 06:18:27 -05:00
|
|
|
|
2021-01-10 13:05:14 -06:00
|
|
|
$key = $this->service->create($date, ...$roles);
|
2019-12-01 03:47:56 -06:00
|
|
|
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertEquals($date, $key->getExpirationDate());
|
2021-01-10 13:05:14 -06:00
|
|
|
foreach ($roles as $roleDefinition) {
|
|
|
|
self::assertTrue($key->hasRole($roleDefinition->roleName()));
|
|
|
|
}
|
2016-08-06 06:18:27 -05:00
|
|
|
}
|
|
|
|
|
2019-12-01 03:47:56 -06:00
|
|
|
public function provideCreationDate(): iterable
|
2016-08-06 06:18:27 -05:00
|
|
|
{
|
2021-01-10 13:05:14 -06:00
|
|
|
yield 'no expiration date' => [null, []];
|
|
|
|
yield 'expiration date' => [Chronos::parse('2030-01-01'), []];
|
|
|
|
yield 'roles' => [null, [RoleDefinition::forDomain('123'), RoleDefinition::forAuthoredShortUrls()]];
|
2016-08-06 06:18:27 -05:00
|
|
|
}
|
|
|
|
|
2019-12-01 03:47:56 -06:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideInvalidApiKeys
|
|
|
|
*/
|
|
|
|
public function checkReturnsFalseForInvalidApiKeys(?ApiKey $invalidKey): void
|
2016-08-06 06:18:27 -05:00
|
|
|
{
|
|
|
|
$repo = $this->prophesize(EntityRepository::class);
|
2019-12-01 03:47:56 -06:00
|
|
|
$repo->findOneBy(['key' => '12345'])->willReturn($invalidKey)
|
2018-11-11 06:18:21 -06:00
|
|
|
->shouldBeCalledOnce();
|
2016-08-06 06:18:27 -05:00
|
|
|
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
|
|
|
|
|
2020-11-08 04:28:27 -06:00
|
|
|
$result = $this->service->check('12345');
|
|
|
|
|
|
|
|
self::assertFalse($result->isValid());
|
|
|
|
self::assertSame($invalidKey, $result->apiKey());
|
2016-08-06 06:18:27 -05:00
|
|
|
}
|
|
|
|
|
2019-12-01 03:47:56 -06:00
|
|
|
public 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];
|
|
|
|
yield 'disabled api key' => [(new ApiKey())->disable()];
|
|
|
|
yield 'expired api key' => [new ApiKey(Chronos::now()->subDay())];
|
2016-08-06 06:18:27 -05:00
|
|
|
}
|
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
/** @test */
|
2019-12-01 03:47:56 -06:00
|
|
|
public function checkReturnsTrueWhenConditionsAreFavorable(): void
|
2016-08-06 06:18:27 -05:00
|
|
|
{
|
2020-11-08 04:28:27 -06:00
|
|
|
$apiKey = new ApiKey();
|
|
|
|
|
2016-08-06 06:18:27 -05:00
|
|
|
$repo = $this->prophesize(EntityRepository::class);
|
2020-11-08 04:28:27 -06:00
|
|
|
$repo->findOneBy(['key' => '12345'])->willReturn($apiKey)
|
2018-11-11 06:18:21 -06:00
|
|
|
->shouldBeCalledOnce();
|
2016-08-06 06:18:27 -05:00
|
|
|
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
|
|
|
|
|
2020-11-08 04:28:27 -06:00
|
|
|
$result = $this->service->check('12345');
|
|
|
|
|
|
|
|
self::assertTrue($result->isValid());
|
|
|
|
self::assertSame($apiKey, $result->apiKey());
|
2016-08-06 06:18:27 -05:00
|
|
|
}
|
|
|
|
|
2019-02-16 03:53:45 -06:00
|
|
|
/** @test */
|
2019-12-01 03:47:56 -06:00
|
|
|
public function disableThrowsExceptionWhenNoApiKeyIsFound(): void
|
2016-08-06 06:18:27 -05:00
|
|
|
{
|
|
|
|
$repo = $this->prophesize(EntityRepository::class);
|
|
|
|
$repo->findOneBy(['key' => '12345'])->willReturn(null)
|
2018-11-11 06:18:21 -06:00
|
|
|
->shouldBeCalledOnce();
|
2016-08-06 06:18:27 -05:00
|
|
|
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
|
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
/** @test */
|
2019-12-01 03:47:56 -06:00
|
|
|
public function disableReturnsDisabledApiKeyWhenFound(): void
|
2016-08-06 06:18:27 -05:00
|
|
|
{
|
|
|
|
$key = new ApiKey();
|
|
|
|
$repo = $this->prophesize(EntityRepository::class);
|
|
|
|
$repo->findOneBy(['key' => '12345'])->willReturn($key)
|
2018-11-11 06:18:21 -06:00
|
|
|
->shouldBeCalledOnce();
|
2016-08-06 06:18:27 -05:00
|
|
|
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
|
|
|
|
|
2018-11-11 06:18:21 -06:00
|
|
|
$this->em->flush()->shouldBeCalledOnce();
|
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
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
/** @test */
|
2019-12-01 03:47:56 -06:00
|
|
|
public function listFindsAllApiKeys(): void
|
2016-08-06 11:08:09 -05:00
|
|
|
{
|
2019-12-01 03:47:56 -06:00
|
|
|
$expectedApiKeys = [new ApiKey(), new ApiKey(), new ApiKey()];
|
|
|
|
|
2016-08-06 11:08:09 -05:00
|
|
|
$repo = $this->prophesize(EntityRepository::class);
|
2019-12-01 03:47:56 -06:00
|
|
|
$repo->findBy([])->willReturn($expectedApiKeys)
|
2018-11-11 06:18:21 -06:00
|
|
|
->shouldBeCalledOnce();
|
2016-08-06 11:08:09 -05:00
|
|
|
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
/** @test */
|
2019-12-01 03:47:56 -06:00
|
|
|
public function listEnabledFindsOnlyEnabledApiKeys(): void
|
2016-08-06 11:08:09 -05:00
|
|
|
{
|
2019-12-01 03:47:56 -06:00
|
|
|
$expectedApiKeys = [new ApiKey(), new ApiKey(), new ApiKey()];
|
|
|
|
|
2016-08-06 11:08:09 -05:00
|
|
|
$repo = $this->prophesize(EntityRepository::class);
|
2019-12-01 03:47:56 -06:00
|
|
|
$repo->findBy(['enabled' => true])->willReturn($expectedApiKeys)
|
2018-11-11 06:18:21 -06:00
|
|
|
->shouldBeCalledOnce();
|
2016-08-06 11:08:09 -05:00
|
|
|
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
|
|
|
|
|
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
|
|
|
}
|