Updated API test fixtures to include API keys with roles

This commit is contained in:
Alejandro Celaya 2021-01-10 08:40:18 +01:00
parent 380915948b
commit f827186c77
5 changed files with 57 additions and 19 deletions

View file

@ -71,7 +71,7 @@
"phpunit/phpunit": "^9.5",
"roave/security-advisories": "dev-master",
"shlinkio/php-coding-standard": "~2.1.1",
"shlinkio/shlink-test-utils": "^1.6",
"shlinkio/shlink-test-utils": "^1.7",
"symfony/var-dumper": "^5.2",
"veewee/composer-run-parallel": "^0.1.0"
},

View file

@ -45,6 +45,14 @@ class ApiKey extends AbstractEntity
return $apiKey;
}
public static function withKey(string $key, ?Chronos $expirationDate = null): self
{
$apiKey = new self($expirationDate);
$apiKey->key = $key;
return $apiKey;
}
public function getExpirationDate(): ?Chronos
{
return $this->expirationDate;

View file

@ -5,28 +5,43 @@ declare(strict_types=1);
namespace ShlinkioApiTest\Shlink\Rest\Fixtures;
use Cake\Chronos\Chronos;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;
use ReflectionObject;
use Shlinkio\Shlink\Core\Entity\Domain;
use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
class ApiKeyFixture implements FixtureInterface
class ApiKeyFixture extends AbstractFixture implements DependentFixtureInterface
{
public function getDependencies(): array
{
return [DomainFixture::class];
}
public function load(ObjectManager $manager): void
{
$manager->persist($this->buildApiKey('valid_api_key', true));
$manager->persist($this->buildApiKey('disabled_api_key', false));
$manager->persist($this->buildApiKey('expired_api_key', true, Chronos::now()->subDay()));
$authorApiKey = $this->buildApiKey('author_api_key', true);
$authorApiKey->registerRole(RoleDefinition::forAuthoredShortUrls());
$manager->persist($authorApiKey);
$this->addReference('author_api_key', $authorApiKey);
/** @var Domain $exampleDomain */
$exampleDomain = $this->getReference('example_domain');
$domainApiKey = $this->buildApiKey('domain_api_key', true);
$domainApiKey->registerRole(RoleDefinition::forDomain($exampleDomain->getId()));
$manager->persist($domainApiKey);
$manager->flush();
}
private function buildApiKey(string $key, bool $enabled, ?Chronos $expiresAt = null): ApiKey
{
$apiKey = new ApiKey($expiresAt);
$refObj = new ReflectionObject($apiKey);
$keyProp = $refObj->getProperty('key');
$keyProp->setAccessible(true);
$keyProp->setValue($apiKey, $key);
$apiKey = ApiKey::withKey($key, $expiresAt);
if (! $enabled) {
$apiKey->disable();

View file

@ -12,8 +12,11 @@ class DomainFixture extends AbstractFixture
{
public function load(ObjectManager $manager): void
{
$orphanDomain = new Domain('this_domain_is_detached.com');
$manager->persist($orphanDomain);
$domain = new Domain('example.com');
$manager->persist($domain);
$this->addReference('example_domain', $domain);
$manager->persist(new Domain('this_domain_is_detached.com'));
$manager->flush();
}
}

View file

@ -6,34 +6,45 @@ namespace ShlinkioApiTest\Shlink\Rest\Fixtures;
use Cake\Chronos\Chronos;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;
use ReflectionObject;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
use Shlinkio\Shlink\Core\ShortUrl\Resolver\PersistenceShortUrlRelationResolver;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
class ShortUrlsFixture extends AbstractFixture
class ShortUrlsFixture extends AbstractFixture implements DependentFixtureInterface
{
/**
* Load data fixtures with the passed EntityManager
*
*/
public function getDependencies(): array
{
return [ApiKeyFixture::class];
}
public function load(ObjectManager $manager): void
{
/** @var ApiKey $authorApiKey */
$authorApiKey = $this->getReference('author_api_key');
$abcShortUrl = $this->setShortUrlDate(
new ShortUrl('https://shlink.io', ShortUrlMeta::fromRawData(['customSlug' => 'abc123'])),
new ShortUrl('https://shlink.io', ShortUrlMeta::fromRawData(
['customSlug' => 'abc123', 'apiKey' => $authorApiKey],
)),
'2018-05-01',
);
$manager->persist($abcShortUrl);
$defShortUrl = $this->setShortUrlDate(new ShortUrl(
'https://blog.alejandrocelaya.com/2017/12/09/acmailer-7-0-the-most-important-release-in-a-long-time/',
ShortUrlMeta::fromRawData(['validSince' => Chronos::parse('2020-05-01'), 'customSlug' => 'def456']),
ShortUrlMeta::fromRawData(
['validSince' => Chronos::parse('2020-05-01'), 'customSlug' => 'def456', 'apiKey' => $authorApiKey],
),
), '2019-01-01 00:00:10');
$manager->persist($defShortUrl);
$customShortUrl = $this->setShortUrlDate(new ShortUrl(
'https://shlink.io',
ShortUrlMeta::fromRawData(['customSlug' => 'custom', 'maxVisits' => 2]),
ShortUrlMeta::fromRawData(['customSlug' => 'custom', 'maxVisits' => 2, 'apiKey' => $authorApiKey]),
), '2019-01-01 00:00:20');
$manager->persist($customShortUrl);
@ -46,6 +57,7 @@ class ShortUrlsFixture extends AbstractFixture
$withDomainDuplicatingShortCode = $this->setShortUrlDate(new ShortUrl(
'https://blog.alejandrocelaya.com/2019/04/27/considerations-to-properly-use-open-source-software-projects/',
ShortUrlMeta::fromRawData(['domain' => 'example.com', 'customSlug' => 'ghi789']),
new PersistenceShortUrlRelationResolver($manager),
), '2019-01-01 00:00:30');
$manager->persist($withDomainDuplicatingShortCode);