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;
|
2017-03-24 22:34:18 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-08-06 14:18:27 +03:00
|
|
|
use Prophecy\Argument;
|
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
2019-02-16 12:53:45 +03:00
|
|
|
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
|
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;
|
|
|
|
private ObjectProphecy $em;
|
2016-08-06 14:18:27 +03:00
|
|
|
|
2019-02-16 12:53:45 +03:00
|
|
|
public function setUp(): void
|
2016-08-06 14:18:27 +03:00
|
|
|
{
|
|
|
|
$this->em = $this->prophesize(EntityManager::class);
|
|
|
|
$this->service = new ApiKeyService($this->em->reveal());
|
|
|
|
}
|
|
|
|
|
2019-12-01 12:47:56 +03:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideCreationDate
|
|
|
|
*/
|
|
|
|
public function apiKeyIsProperlyCreated(?Chronos $date): 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
|
|
|
|
|
|
|
$key = $this->service->create($date);
|
2019-12-01 12:47:56 +03:00
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertEquals($date, $key->getExpirationDate());
|
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
|
|
|
{
|
2019-12-01 12:47:56 +03:00
|
|
|
yield 'no expiration date' => [null];
|
|
|
|
yield 'expiration date' => [Chronos::parse('2030-01-01')];
|
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
|
|
|
{
|
|
|
|
$repo = $this->prophesize(EntityRepository::class);
|
2019-12-01 12:47:56 +03:00
|
|
|
$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-10-04 01:35:14 +03:00
|
|
|
self::assertFalse($this->service->check('12345'));
|
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];
|
|
|
|
yield 'disabled api key' => [(new ApiKey())->disable()];
|
|
|
|
yield 'expired api key' => [new ApiKey(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
|
|
|
{
|
|
|
|
$repo = $this->prophesize(EntityRepository::class);
|
|
|
|
$repo->findOneBy(['key' => '12345'])->willReturn(new 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-10-04 01:35:14 +03:00
|
|
|
self::assertTrue($this->service->check('12345'));
|
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
|
|
|
{
|
|
|
|
$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);
|
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
|
|
|
{
|
|
|
|
$key = new ApiKey();
|
|
|
|
$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
|
|
|
}
|
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
|
|
|
{
|
2019-12-01 12:47:56 +03:00
|
|
|
$expectedApiKeys = [new ApiKey(), new ApiKey(), new ApiKey()];
|
|
|
|
|
2016-08-06 19:08:09 +03:00
|
|
|
$repo = $this->prophesize(EntityRepository::class);
|
2019-12-01 12:47:56 +03:00
|
|
|
$repo->findBy([])->willReturn($expectedApiKeys)
|
2018-11-11 15:18:21 +03:00
|
|
|
->shouldBeCalledOnce();
|
2016-08-06 19:08:09 +03:00
|
|
|
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
|
|
|
|
|
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
|
|
|
{
|
2019-12-01 12:47:56 +03:00
|
|
|
$expectedApiKeys = [new ApiKey(), new ApiKey(), new ApiKey()];
|
|
|
|
|
2016-08-06 19:08:09 +03:00
|
|
|
$repo = $this->prophesize(EntityRepository::class);
|
2019-12-01 12:47:56 +03:00
|
|
|
$repo->findBy(['enabled' => true])->willReturn($expectedApiKeys)
|
2018-11-11 15:18:21 +03:00
|
|
|
->shouldBeCalledOnce();
|
2016-08-06 19:08:09 +03:00
|
|
|
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
|
|
|
|
|
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
|
|
|
}
|