2016-08-06 14:18:27 +03:00
|
|
|
<?php
|
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;
|
|
|
|
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
|
|
|
use Shlinkio\Shlink\Rest\Service\ApiKeyService;
|
|
|
|
|
|
|
|
class ApiKeyServiceTest extends TestCase
|
|
|
|
{
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var ApiKeyService */
|
2018-11-20 21:37:22 +03:00
|
|
|
private $service;
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var ObjectProphecy */
|
2018-11-20 21:37:22 +03:00
|
|
|
private $em;
|
2016-08-06 14:18:27 +03:00
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->em = $this->prophesize(EntityManager::class);
|
|
|
|
$this->service = new ApiKeyService($this->em->reveal());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function keyIsProperlyCreated()
|
|
|
|
{
|
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();
|
|
|
|
$this->assertNull($key->getExpirationDate());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function keyIsProperlyCreatedWithExpirationDate()
|
|
|
|
{
|
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
|
|
|
|
2018-09-29 13:52:32 +03:00
|
|
|
$date = Chronos::parse('2030-01-01');
|
2016-08-06 14:18:27 +03:00
|
|
|
$key = $this->service->create($date);
|
|
|
|
$this->assertSame($date, $key->getExpirationDate());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function checkReturnsFalseWhenKeyIsInvalid()
|
|
|
|
{
|
|
|
|
$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());
|
|
|
|
|
|
|
|
$this->assertFalse($this->service->check('12345'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function checkReturnsFalseWhenKeyIsDisabled()
|
|
|
|
{
|
|
|
|
$key = new ApiKey();
|
|
|
|
$key->disable();
|
|
|
|
$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());
|
|
|
|
|
|
|
|
$this->assertFalse($this->service->check('12345'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function checkReturnsFalseWhenKeyIsExpired()
|
|
|
|
{
|
2018-10-28 17:24:41 +03:00
|
|
|
$key = new ApiKey(Chronos::now()->subDay());
|
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());
|
|
|
|
|
|
|
|
$this->assertFalse($this->service->check('12345'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function checkReturnsTrueWhenConditionsAreFavorable()
|
|
|
|
{
|
|
|
|
$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());
|
|
|
|
|
|
|
|
$this->assertTrue($this->service->check('12345'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @expectedException \Shlinkio\Shlink\Common\Exception\InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function disableThrowsExceptionWhenNoTokenIsFound()
|
|
|
|
{
|
|
|
|
$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());
|
|
|
|
|
|
|
|
$this->service->disable('12345');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function disableReturnsDisabledKeyWhenFOund()
|
|
|
|
{
|
|
|
|
$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
|
|
|
|
|
|
|
$this->assertTrue($key->isEnabled());
|
|
|
|
$returnedKey = $this->service->disable('12345');
|
|
|
|
$this->assertFalse($key->isEnabled());
|
|
|
|
$this->assertSame($key, $returnedKey);
|
|
|
|
}
|
2016-08-06 19:08:09 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function listFindsAllApiKeys()
|
|
|
|
{
|
|
|
|
$repo = $this->prophesize(EntityRepository::class);
|
|
|
|
$repo->findBy([])->willReturn([])
|
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());
|
|
|
|
|
|
|
|
$this->service->listKeys();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function listEnabledFindsOnlyEnabledApiKeys()
|
|
|
|
{
|
|
|
|
$repo = $this->prophesize(EntityRepository::class);
|
|
|
|
$repo->findBy(['enabled' => true])->willReturn([])
|
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());
|
|
|
|
|
|
|
|
$this->service->listKeys(true);
|
|
|
|
}
|
2016-08-06 14:18:27 +03:00
|
|
|
}
|