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

152 lines
5.4 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;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
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;
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-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
protected function setUp(): void
2016-08-06 14:18:27 +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
}
/**
* @param RoleDefinition[] $roles
*/
#[Test, DataProvider('provideCreationDate')]
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
{
$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);
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
}
2023-02-09 11:32:38 +03:00
public static 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),
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
{
$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());
self::assertSame($invalidKey, $result->apiKey);
2016-08-06 14:18:27 +03:00
}
2023-02-09 11:32:38 +03:00
public static 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
}
#[Test]
public function checkReturnsTrueWhenConditionsAreFavorable(): void
2016-08-06 14:18:27 +03:00
{
$apiKey = ApiKey::create();
2020-11-08 13:28:27 +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());
self::assertSame($apiKey, $result->apiKey);
2016-08-06 14:18:27 +03:00
}
#[Test]
public function disableThrowsExceptionWhenNoApiKeyIsFound(): void
2016-08-06 14:18:27 +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);
2016-08-06 14:18:27 +03:00
$this->service->disable('12345');
}
#[Test]
public function disableReturnsDisabledApiKeyWhenFound(): void
2016-08-06 14:18:27 +03:00
{
$key = ApiKey::create();
$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
}
#[Test]
public function listFindsAllApiKeys(): void
{
$expectedApiKeys = [ApiKey::create(), ApiKey::create(), ApiKey::create()];
$this->repo->expects($this->once())->method('findBy')->with([])->willReturn($expectedApiKeys);
$this->em->method('getRepository')->with(ApiKey::class)->willReturn($this->repo);
$result = $this->service->listKeys();
2020-10-04 01:35:14 +03:00
self::assertEquals($expectedApiKeys, $result);
}
#[Test]
public function listEnabledFindsOnlyEnabledApiKeys(): void
{
$expectedApiKeys = [ApiKey::create(), ApiKey::create(), ApiKey::create()];
$this->repo->expects($this->once())->method('findBy')->with(['enabled' => true])->willReturn($expectedApiKeys);
$this->em->method('getRepository')->with(ApiKey::class)->willReturn($this->repo);
$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
}