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

166 lines
5.6 KiB
PHP
Raw Normal View History

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;
use Cake\Chronos\Chronos;
2016-08-06 14:18:27 +03:00
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
2017-03-24 22:34:18 +03:00
use PHPUnit\Framework\TestCase;
2016-08-06 14:18:27 +03:00
use Prophecy\Argument;
2020-11-02 13:50:19 +03:00
use Prophecy\PhpUnit\ProphecyTrait;
2016-08-06 14:18:27 +03:00
use Prophecy\Prophecy\ObjectProphecy;
2019-02-16 12:53:45 +03:00
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
use Shlinkio\Shlink\Core\Domain\Entity\Domain;
use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta;
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-11-02 13:50:19 +03:00
use ProphecyTrait;
2020-01-01 22:48:31 +03:00
private ApiKeyService $service;
private ObjectProphecy $em;
2016-08-06 14:18:27 +03:00
protected function setUp(): void
2016-08-06 14:18:27 +03:00
{
$this->em = $this->prophesize(EntityManager::class);
$this->service = new ApiKeyService($this->em->reveal());
}
/**
* @test
* @dataProvider provideCreationDate
* @param RoleDefinition[] $roles
*/
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
{
2018-11-11 15:18:21 +03:00
$this->em->flush()->shouldBeCalledOnce();
$this->em->persist(Argument::type(ApiKey::class))->shouldBeCalledOnce();
2016-08-06 14:18:27 +03:00
2021-03-06 20:27:34 +03:00
$key = $this->service->create($date, $name, ...$roles);
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());
foreach ($roles as $roleDefinition) {
2022-04-23 19:41:16 +03:00
self::assertTrue($key->hasRole($roleDefinition->role));
}
2016-08-06 14:18:27 +03:00
}
public function provideCreationDate(): iterable
2016-08-06 14:18:27 +03:00
{
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, [
2021-07-22 21:48:58 +03:00
RoleDefinition::forDomain(Domain::withAuthority('')->setId('123')),
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
}
/**
* @test
* @dataProvider provideInvalidApiKeys
*/
public function checkReturnsFalseForInvalidApiKeys(?ApiKey $invalidKey): void
2016-08-06 14:18:27 +03:00
{
$repo = $this->prophesize(EntityRepository::class);
$repo->findOneBy(['key' => '12345'])->willReturn($invalidKey)
2018-11-11 15:18:21 +03:00
->shouldBeCalledOnce();
2016-08-06 14:18:27 +03:00
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
2020-11-08 13:28:27 +03:00
$result = $this->service->check('12345');
self::assertFalse($result->isValid());
self::assertSame($invalidKey, $result->apiKey);
2016-08-06 14:18:27 +03:00
}
public function provideInvalidApiKeys(): iterable
2016-08-06 14:18:27 +03:00
{
yield 'non-existent api key' => [null];
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 */
public function checkReturnsTrueWhenConditionsAreFavorable(): void
2016-08-06 14:18:27 +03:00
{
$apiKey = ApiKey::create();
2020-11-08 13:28:27 +03:00
2016-08-06 14:18:27 +03:00
$repo = $this->prophesize(EntityRepository::class);
2020-11-08 13:28:27 +03:00
$repo->findOneBy(['key' => '12345'])->willReturn($apiKey)
2018-11-11 15:18:21 +03:00
->shouldBeCalledOnce();
2016-08-06 14:18:27 +03:00
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
2020-11-08 13:28:27 +03:00
$result = $this->service->check('12345');
self::assertTrue($result->isValid());
self::assertSame($apiKey, $result->apiKey);
2016-08-06 14:18:27 +03:00
}
2019-02-16 12:53:45 +03:00
/** @test */
public function disableThrowsExceptionWhenNoApiKeyIsFound(): void
2016-08-06 14:18:27 +03:00
{
$repo = $this->prophesize(EntityRepository::class);
$repo->findOneBy(['key' => '12345'])->willReturn(null)
2018-11-11 15:18:21 +03:00
->shouldBeCalledOnce();
2016-08-06 14:18:27 +03:00
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
2019-02-16 12:53:45 +03:00
$this->expectException(InvalidArgumentException::class);
2016-08-06 14:18:27 +03:00
$this->service->disable('12345');
}
2019-02-17 22:28:34 +03:00
/** @test */
public function disableReturnsDisabledApiKeyWhenFound(): void
2016-08-06 14:18:27 +03:00
{
$key = ApiKey::create();
2016-08-06 14:18:27 +03:00
$repo = $this->prophesize(EntityRepository::class);
$repo->findOneBy(['key' => '12345'])->willReturn($key)
2018-11-11 15:18:21 +03:00
->shouldBeCalledOnce();
2016-08-06 14:18:27 +03:00
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
2018-11-11 15:18:21 +03:00
$this->em->flush()->shouldBeCalledOnce();
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
}
2019-02-17 22:28:34 +03:00
/** @test */
public function listFindsAllApiKeys(): void
{
$expectedApiKeys = [ApiKey::create(), ApiKey::create(), ApiKey::create()];
$repo = $this->prophesize(EntityRepository::class);
$repo->findBy([])->willReturn($expectedApiKeys)
2018-11-11 15:18:21 +03:00
->shouldBeCalledOnce();
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
$result = $this->service->listKeys();
2020-10-04 01:35:14 +03:00
self::assertEquals($expectedApiKeys, $result);
}
2019-02-17 22:28:34 +03:00
/** @test */
public function listEnabledFindsOnlyEnabledApiKeys(): void
{
$expectedApiKeys = [ApiKey::create(), ApiKey::create(), ApiKey::create()];
$repo = $this->prophesize(EntityRepository::class);
$repo->findBy(['enabled' => true])->willReturn($expectedApiKeys)
2018-11-11 15:18:21 +03:00
->shouldBeCalledOnce();
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
$result = $this->service->listKeys(true);
2020-10-04 01:35:14 +03:00
self::assertEquals($expectedApiKeys, $result);
}
2016-08-06 14:18:27 +03:00
}