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

@@ -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();