Extracted ApiKey metadata to the ApiKeyMeta object

This commit is contained in:
Alejandro Celaya
2021-03-14 09:59:35 +01:00
parent 9b55389538
commit 0a5c265b12
36 changed files with 186 additions and 121 deletions

View File

@@ -40,11 +40,7 @@ class GenerateKeyCommandTest extends TestCase
/** @test */
public function noExpirationDateIsDefinedIfNotProvided(): void
{
$this->apiKeyService->create(
null, // Expiration date
null, // Name
)->shouldBeCalledOnce()
->willReturn(new ApiKey());
$this->apiKeyService->create(null, null)->shouldBeCalledOnce()->willReturn(ApiKey::create());
$this->commandTester->execute([]);
$output = $this->commandTester->getDisplay();
@@ -55,11 +51,9 @@ class GenerateKeyCommandTest extends TestCase
/** @test */
public function expirationDateIsDefinedIfProvided(): void
{
$this->apiKeyService->create(
Argument::type(Chronos::class), // Expiration date
null, // Name
)->shouldBeCalledOnce()
->willReturn(new ApiKey());
$this->apiKeyService->create(Argument::type(Chronos::class), null)->shouldBeCalledOnce()->willReturn(
ApiKey::create(),
);
$this->commandTester->execute([
'--expiration-date' => '2016-01-01',
@@ -69,11 +63,9 @@ class GenerateKeyCommandTest extends TestCase
/** @test */
public function nameIsDefinedIfProvided(): void
{
$this->apiKeyService->create(
null, // Expiration date
Argument::type('string'), // Name
)->shouldBeCalledOnce()
->willReturn(new ApiKey());
$this->apiKeyService->create(null, Argument::type('string'))->shouldBeCalledOnce()->willReturn(
ApiKey::create(),
);
$this->commandTester->execute([
'--name' => 'Alice',

View File

@@ -9,6 +9,7 @@ use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\CLI\Command\Api\ListKeysCommand;
use Shlinkio\Shlink\Core\Entity\Domain;
use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta;
use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
@@ -49,66 +50,66 @@ class ListKeysCommandTest extends TestCase
public function provideKeysAndOutputs(): iterable
{
yield 'all keys' => [
[ApiKey::withKey('foo'), ApiKey::withKey('bar'), ApiKey::withKey('baz')],
[$apiKey1 = ApiKey::create(), $apiKey2 = ApiKey::create(), $apiKey3 = ApiKey::create()],
false,
<<<OUTPUT
+-----+------+------------+-----------------+-------+
| Key | Name | Is enabled | Expiration date | Roles |
+-----+------+------------+-----------------+-------+
| foo | - | +++ | - | Admin |
| bar | - | +++ | - | Admin |
| baz | - | +++ | - | Admin |
+-----+------+------------+-----------------+-------+
+--------------------------------------+------+------------+-----------------+-------+
| Key | Name | Is enabled | Expiration date | Roles |
+--------------------------------------+------+------------+-----------------+-------+
| {$apiKey1} | - | +++ | - | Admin |
| {$apiKey2} | - | +++ | - | Admin |
| {$apiKey3} | - | +++ | - | Admin |
+--------------------------------------+------+------------+-----------------+-------+
OUTPUT,
];
yield 'enabled keys' => [
[ApiKey::withKey('foo')->disable(), ApiKey::withKey('bar')],
[$apiKey1 = ApiKey::create()->disable(), $apiKey2 = ApiKey::create()],
true,
<<<OUTPUT
+-----+------+-----------------+-------+
| Key | Name | Expiration date | Roles |
+-----+------+-----------------+-------+
| foo | - | - | Admin |
| bar | - | - | Admin |
+-----+------+-----------------+-------+
+--------------------------------------+------+-----------------+-------+
| Key | Name | Expiration date | Roles |
+--------------------------------------+------+-----------------+-------+
| {$apiKey1} | - | - | Admin |
| {$apiKey2} | - | - | Admin |
+--------------------------------------+------+-----------------+-------+
OUTPUT,
];
yield 'with roles' => [
[
ApiKey::withKey('foo'),
$this->apiKeyWithRoles('bar', [RoleDefinition::forAuthoredShortUrls()]),
$this->apiKeyWithRoles('baz', [RoleDefinition::forDomain((new Domain('example.com'))->setId('1'))]),
ApiKey::withKey('foo2'),
$this->apiKeyWithRoles('baz2', [
$apiKey1 = ApiKey::create(),
$apiKey2 = $this->apiKeyWithRoles([RoleDefinition::forAuthoredShortUrls()]),
$apiKey3 = $this->apiKeyWithRoles([RoleDefinition::forDomain((new Domain('example.com'))->setId('1'))]),
$apiKey4 = ApiKey::create(),
$apiKey5 = $this->apiKeyWithRoles([
RoleDefinition::forAuthoredShortUrls(),
RoleDefinition::forDomain((new Domain('example.com'))->setId('1')),
]),
ApiKey::withKey('foo3'),
$apiKey6 = ApiKey::create(),
],
true,
<<<OUTPUT
+------+------+-----------------+--------------------------+
| Key | Name | Expiration date | Roles |
+------+------+-----------------+--------------------------+
| foo | - | - | Admin |
| bar | - | - | Author only |
| baz | - | - | Domain only: example.com |
| foo2 | - | - | Admin |
| baz2 | - | - | Author only |
| | | | Domain only: example.com |
| foo3 | - | - | Admin |
+------+------+-----------------+--------------------------+
+--------------------------------------+------+-----------------+--------------------------+
| Key | Name | Expiration date | Roles |
+--------------------------------------+------+-----------------+--------------------------+
| {$apiKey1} | - | - | Admin |
| {$apiKey2} | - | - | Author only |
| {$apiKey3} | - | - | Domain only: example.com |
| {$apiKey4} | - | - | Admin |
| {$apiKey5} | - | - | Author only |
| | | | Domain only: example.com |
| {$apiKey6} | - | - | Admin |
+--------------------------------------+------+-----------------+--------------------------+
OUTPUT,
];
yield 'with names' => [
[
$apiKey1 = ApiKey::withName('Alice'),
$apiKey2 = ApiKey::withName('Alice and Bob'),
$apiKey3 = ApiKey::withName(''),
$apiKey4 = new ApiKey(),
$apiKey1 = ApiKey::fromMeta(ApiKeyMeta::withName('Alice')),
$apiKey2 = ApiKey::fromMeta(ApiKeyMeta::withName('Alice and Bob')),
$apiKey3 = ApiKey::fromMeta(ApiKeyMeta::withName('')),
$apiKey4 = ApiKey::create(),
],
true,
<<<OUTPUT
@@ -125,9 +126,9 @@ class ListKeysCommandTest extends TestCase
];
}
private function apiKeyWithRoles(string $key, array $roles): ApiKey
private function apiKeyWithRoles(array $roles): ApiKey
{
$apiKey = ApiKey::withKey($key);
$apiKey = ApiKey::create();
foreach ($roles as $role) {
$apiKey->registerRole($role);
}