2016-08-06 19:50:50 +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 19:50:50 +03:00
|
|
|
namespace ShlinkioTest\Shlink\CLI\Command\Api;
|
|
|
|
|
2021-12-10 15:42:33 +03:00
|
|
|
use Cake\Chronos\Chronos;
|
2017-03-24 22:34:18 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-08-06 19:50:50 +03:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
|
|
|
use Shlinkio\Shlink\CLI\Command\Api\ListKeysCommand;
|
2021-01-11 19:01:01 +03:00
|
|
|
use Shlinkio\Shlink\Core\Entity\Domain;
|
2021-03-14 11:59:35 +03:00
|
|
|
use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta;
|
2021-01-11 19:01:01 +03:00
|
|
|
use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition;
|
2016-08-06 19:50:50 +03:00
|
|
|
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
2019-12-30 00:27:00 +03:00
|
|
|
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
|
2021-04-08 14:42:56 +03:00
|
|
|
use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait;
|
2016-08-06 19:50:50 +03:00
|
|
|
use Symfony\Component\Console\Tester\CommandTester;
|
|
|
|
|
|
|
|
class ListKeysCommandTest extends TestCase
|
|
|
|
{
|
2021-04-08 14:42:56 +03:00
|
|
|
use CliTestUtilsTrait;
|
2020-11-02 13:50:19 +03:00
|
|
|
|
2019-12-30 00:27:00 +03:00
|
|
|
private CommandTester $commandTester;
|
|
|
|
private ObjectProphecy $apiKeyService;
|
2016-08-06 19:50:50 +03:00
|
|
|
|
2022-09-11 13:02:49 +03:00
|
|
|
protected function setUp(): void
|
2016-08-06 19:50:50 +03:00
|
|
|
{
|
2019-12-30 00:27:00 +03:00
|
|
|
$this->apiKeyService = $this->prophesize(ApiKeyServiceInterface::class);
|
2021-04-08 14:42:56 +03:00
|
|
|
$this->commandTester = $this->testerForCommand(new ListKeysCommand($this->apiKeyService->reveal()));
|
2016-08-06 19:50:50 +03:00
|
|
|
}
|
|
|
|
|
2021-01-11 19:01:01 +03:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideKeysAndOutputs
|
|
|
|
*/
|
|
|
|
public function returnsExpectedOutput(array $keys, bool $enabledOnly, string $expected): void
|
2016-08-06 19:50:50 +03:00
|
|
|
{
|
2021-01-11 19:01:01 +03:00
|
|
|
$listKeys = $this->apiKeyService->listKeys($enabledOnly)->willReturn($keys);
|
2018-11-17 19:36:22 +03:00
|
|
|
|
2021-01-30 13:25:20 +03:00
|
|
|
$this->commandTester->execute(['--enabled-only' => $enabledOnly]);
|
2018-11-17 19:36:22 +03:00
|
|
|
$output = $this->commandTester->getDisplay();
|
|
|
|
|
2021-01-11 19:01:01 +03:00
|
|
|
self::assertEquals($expected, $output);
|
|
|
|
$listKeys->shouldHaveBeenCalledOnce();
|
2016-08-06 19:50:50 +03:00
|
|
|
}
|
|
|
|
|
2021-01-11 19:01:01 +03:00
|
|
|
public function provideKeysAndOutputs(): iterable
|
2016-08-06 19:50:50 +03:00
|
|
|
{
|
2021-12-10 15:42:33 +03:00
|
|
|
$dateInThePast = Chronos::createFromFormat('Y-m-d H:i:s', '2020-01-01 00:00:00');
|
|
|
|
|
2021-01-11 19:01:01 +03:00
|
|
|
yield 'all keys' => [
|
2021-12-10 15:42:33 +03:00
|
|
|
[
|
|
|
|
$apiKey1 = ApiKey::create()->disable(),
|
|
|
|
$apiKey2 = ApiKey::fromMeta(ApiKeyMeta::withExpirationDate($dateInThePast)),
|
|
|
|
$apiKey3 = ApiKey::create(),
|
|
|
|
],
|
2021-01-11 19:01:01 +03:00
|
|
|
false,
|
|
|
|
<<<OUTPUT
|
2021-12-10 15:42:33 +03:00
|
|
|
+--------------------------------------+------+------------+---------------------------+-------+
|
|
|
|
| Key | Name | Is enabled | Expiration date | Roles |
|
|
|
|
+--------------------------------------+------+------------+---------------------------+-------+
|
|
|
|
| {$apiKey1} | - | --- | - | Admin |
|
|
|
|
+--------------------------------------+------+------------+---------------------------+-------+
|
|
|
|
| {$apiKey2} | - | --- | 2020-01-01T00:00:00+00:00 | Admin |
|
|
|
|
+--------------------------------------+------+------------+---------------------------+-------+
|
|
|
|
| {$apiKey3} | - | +++ | - | Admin |
|
|
|
|
+--------------------------------------+------+------------+---------------------------+-------+
|
2018-11-17 19:36:22 +03:00
|
|
|
|
2021-01-11 19:01:01 +03:00
|
|
|
OUTPUT,
|
|
|
|
];
|
|
|
|
yield 'enabled keys' => [
|
2021-03-14 11:59:35 +03:00
|
|
|
[$apiKey1 = ApiKey::create()->disable(), $apiKey2 = ApiKey::create()],
|
2021-01-11 19:01:01 +03:00
|
|
|
true,
|
|
|
|
<<<OUTPUT
|
2021-03-14 11:59:35 +03:00
|
|
|
+--------------------------------------+------+-----------------+-------+
|
|
|
|
| Key | Name | Expiration date | Roles |
|
|
|
|
+--------------------------------------+------+-----------------+-------+
|
|
|
|
| {$apiKey1} | - | - | Admin |
|
2021-08-03 11:21:42 +03:00
|
|
|
+--------------------------------------+------+-----------------+-------+
|
2021-03-14 11:59:35 +03:00
|
|
|
| {$apiKey2} | - | - | Admin |
|
|
|
|
+--------------------------------------+------+-----------------+-------+
|
2021-01-11 19:01:01 +03:00
|
|
|
|
|
|
|
OUTPUT,
|
|
|
|
];
|
|
|
|
yield 'with roles' => [
|
|
|
|
[
|
2021-03-14 11:59:35 +03:00
|
|
|
$apiKey1 = ApiKey::create(),
|
|
|
|
$apiKey2 = $this->apiKeyWithRoles([RoleDefinition::forAuthoredShortUrls()]),
|
2021-07-22 21:48:58 +03:00
|
|
|
$apiKey3 = $this->apiKeyWithRoles(
|
|
|
|
[RoleDefinition::forDomain(Domain::withAuthority('example.com')->setId('1'))],
|
|
|
|
),
|
2021-03-14 11:59:35 +03:00
|
|
|
$apiKey4 = ApiKey::create(),
|
|
|
|
$apiKey5 = $this->apiKeyWithRoles([
|
2021-01-11 19:01:01 +03:00
|
|
|
RoleDefinition::forAuthoredShortUrls(),
|
2021-07-22 21:48:58 +03:00
|
|
|
RoleDefinition::forDomain(Domain::withAuthority('example.com')->setId('1')),
|
2021-01-11 19:01:01 +03:00
|
|
|
]),
|
2021-03-14 11:59:35 +03:00
|
|
|
$apiKey6 = ApiKey::create(),
|
2021-01-11 19:01:01 +03:00
|
|
|
],
|
|
|
|
true,
|
|
|
|
<<<OUTPUT
|
2021-03-14 11:59:35 +03:00
|
|
|
+--------------------------------------+------+-----------------+--------------------------+
|
|
|
|
| Key | Name | Expiration date | Roles |
|
|
|
|
+--------------------------------------+------+-----------------+--------------------------+
|
|
|
|
| {$apiKey1} | - | - | Admin |
|
2021-08-03 11:21:42 +03:00
|
|
|
+--------------------------------------+------+-----------------+--------------------------+
|
2021-03-14 11:59:35 +03:00
|
|
|
| {$apiKey2} | - | - | Author only |
|
2021-08-03 11:21:42 +03:00
|
|
|
+--------------------------------------+------+-----------------+--------------------------+
|
2021-03-14 11:59:35 +03:00
|
|
|
| {$apiKey3} | - | - | Domain only: example.com |
|
2021-08-03 11:21:42 +03:00
|
|
|
+--------------------------------------+------+-----------------+--------------------------+
|
2021-03-14 11:59:35 +03:00
|
|
|
| {$apiKey4} | - | - | Admin |
|
2021-08-03 11:21:42 +03:00
|
|
|
+--------------------------------------+------+-----------------+--------------------------+
|
2021-03-14 11:59:35 +03:00
|
|
|
| {$apiKey5} | - | - | Author only |
|
|
|
|
| | | | Domain only: example.com |
|
2021-08-03 11:21:42 +03:00
|
|
|
+--------------------------------------+------+-----------------+--------------------------+
|
2021-03-14 11:59:35 +03:00
|
|
|
| {$apiKey6} | - | - | Admin |
|
|
|
|
+--------------------------------------+------+-----------------+--------------------------+
|
2021-03-06 20:27:34 +03:00
|
|
|
|
|
|
|
OUTPUT,
|
|
|
|
];
|
|
|
|
yield 'with names' => [
|
|
|
|
[
|
2021-03-14 11:59:35 +03:00
|
|
|
$apiKey1 = ApiKey::fromMeta(ApiKeyMeta::withName('Alice')),
|
|
|
|
$apiKey2 = ApiKey::fromMeta(ApiKeyMeta::withName('Alice and Bob')),
|
|
|
|
$apiKey3 = ApiKey::fromMeta(ApiKeyMeta::withName('')),
|
|
|
|
$apiKey4 = ApiKey::create(),
|
2021-03-06 20:27:34 +03:00
|
|
|
],
|
|
|
|
true,
|
|
|
|
<<<OUTPUT
|
2021-03-14 10:28:21 +03:00
|
|
|
+--------------------------------------+---------------+-----------------+-------+
|
|
|
|
| Key | Name | Expiration date | Roles |
|
|
|
|
+--------------------------------------+---------------+-----------------+-------+
|
|
|
|
| {$apiKey1} | Alice | - | Admin |
|
2021-08-03 11:21:42 +03:00
|
|
|
+--------------------------------------+---------------+-----------------+-------+
|
2021-03-14 10:28:21 +03:00
|
|
|
| {$apiKey2} | Alice and Bob | - | Admin |
|
2021-08-03 11:21:42 +03:00
|
|
|
+--------------------------------------+---------------+-----------------+-------+
|
2021-03-14 10:28:21 +03:00
|
|
|
| {$apiKey3} | | - | Admin |
|
2021-08-03 11:21:42 +03:00
|
|
|
+--------------------------------------+---------------+-----------------+-------+
|
2021-03-14 10:28:21 +03:00
|
|
|
| {$apiKey4} | - | - | Admin |
|
|
|
|
+--------------------------------------+---------------+-----------------+-------+
|
2021-01-11 19:01:01 +03:00
|
|
|
|
|
|
|
OUTPUT,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-03-14 11:59:35 +03:00
|
|
|
private function apiKeyWithRoles(array $roles): ApiKey
|
2021-01-11 19:01:01 +03:00
|
|
|
{
|
2021-03-14 11:59:35 +03:00
|
|
|
$apiKey = ApiKey::create();
|
2021-01-11 19:01:01 +03:00
|
|
|
foreach ($roles as $role) {
|
|
|
|
$apiKey->registerRole($role);
|
|
|
|
}
|
2018-11-17 19:36:22 +03:00
|
|
|
|
2021-01-11 19:01:01 +03:00
|
|
|
return $apiKey;
|
2016-08-06 19:50:50 +03:00
|
|
|
}
|
|
|
|
}
|