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

165 lines
5.5 KiB
PHP
Raw Normal View History

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;
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;
use Shlinkio\Shlink\Core\Entity\Domain;
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());
}
/**
* @test
* @dataProvider provideCreationDate
* @param RoleDefinition[] $roles
*/
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
{
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-03-06 11:27:34 -06:00
$key = $this->service->create($date, $name, ...$roles);
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());
foreach ($roles as $roleDefinition) {
self::assertTrue($key->hasRole($roleDefinition->roleName()));
}
2016-08-06 06:18:27 -05:00
}
public function provideCreationDate(): iterable
2016-08-06 06:18:27 -05:00
{
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, [
RoleDefinition::forDomain((new Domain(''))->setId('123')),
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
}
/**
* @test
* @dataProvider provideInvalidApiKeys
*/
public function checkReturnsFalseForInvalidApiKeys(?ApiKey $invalidKey): void
2016-08-06 06:18:27 -05:00
{
$repo = $this->prophesize(EntityRepository::class);
$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
}
public function provideInvalidApiKeys(): iterable
2016-08-06 06:18:27 -05: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 */
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 */
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);
2016-08-06 06:18:27 -05:00
$this->service->disable('12345');
}
2019-02-17 13:28:34 -06:00
/** @test */
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
}
2019-02-17 13:28:34 -06:00
/** @test */
public function listFindsAllApiKeys(): void
{
$expectedApiKeys = [new ApiKey(), new ApiKey(), new ApiKey()];
$repo = $this->prophesize(EntityRepository::class);
$repo->findBy([])->willReturn($expectedApiKeys)
2018-11-11 06:18:21 -06:00
->shouldBeCalledOnce();
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
$result = $this->service->listKeys();
2020-10-03 17:35:14 -05:00
self::assertEquals($expectedApiKeys, $result);
}
2019-02-17 13:28:34 -06:00
/** @test */
public function listEnabledFindsOnlyEnabledApiKeys(): void
{
$expectedApiKeys = [new ApiKey(), new ApiKey(), new ApiKey()];
$repo = $this->prophesize(EntityRepository::class);
$repo->findBy(['enabled' => true])->willReturn($expectedApiKeys)
2018-11-11 06:18:21 -06:00
->shouldBeCalledOnce();
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
$result = $this->service->listKeys(true);
2020-10-03 17:35:14 -05:00
self::assertEquals($expectedApiKeys, $result);
}
2016-08-06 06:18:27 -05:00
}