From f6b1cc75563ca3e7baea545d3e14e34a1df6f671 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya <alejandrocelaya@gmail.com> Date: Tue, 19 Sep 2023 10:14:04 +0200 Subject: [PATCH] Test API key creation with custom key --- config/autoload/installer.global.php | 2 +- module/CLI/config/cli.config.php | 4 +-- module/CLI/config/dependencies.config.php | 4 +-- ...ateKeyCommand.php => CreateKeyCommand.php} | 3 +- .../CLI/test-cli/Command/CreateApiKeyTest.php | 31 +++++++++++++++++++ .../test-cli/Command/GenerateApiKeyTest.php | 22 ------------- ...mmandTest.php => CreateKeyCommandTest.php} | 20 ++++++++---- 7 files changed, 51 insertions(+), 35 deletions(-) rename module/CLI/src/Command/Api/{GenerateKeyCommand.php => CreateKeyCommand.php} (98%) create mode 100644 module/CLI/test-cli/Command/CreateApiKeyTest.php delete mode 100644 module/CLI/test-cli/Command/GenerateApiKeyTest.php rename module/CLI/test/Command/Api/{GenerateKeyCommandTest.php => CreateKeyCommandTest.php} (78%) diff --git a/config/autoload/installer.global.php b/config/autoload/installer.global.php index 029a50d6..de6a69a3 100644 --- a/config/autoload/installer.global.php +++ b/config/autoload/installer.global.php @@ -84,7 +84,7 @@ return [ 'command' => 'bin/cli ' . Command\Visit\DownloadGeoLiteDbCommand::NAME, ], InstallationCommand::API_KEY_GENERATE->value => [ - 'command' => 'bin/cli ' . Command\Api\GenerateKeyCommand::NAME, + 'command' => 'bin/cli ' . Command\Api\CreateKeyCommand::NAME, ], ], ], diff --git a/module/CLI/config/cli.config.php b/module/CLI/config/cli.config.php index 2a1bc5e8..1857bec7 100644 --- a/module/CLI/config/cli.config.php +++ b/module/CLI/config/cli.config.php @@ -21,8 +21,8 @@ return [ Command\Visit\DeleteOrphanVisitsCommand::NAME => Command\Visit\DeleteOrphanVisitsCommand::class, Command\Visit\GetNonOrphanVisitsCommand::NAME => Command\Visit\GetNonOrphanVisitsCommand::class, - Command\Api\GenerateKeyCommand::NAME => Command\Api\GenerateKeyCommand::class, - Command\Api\GenerateKeyCommand::ALIAS => Command\Api\GenerateKeyCommand::class, + Command\Api\CreateKeyCommand::NAME => Command\Api\CreateKeyCommand::class, + Command\Api\CreateKeyCommand::ALIAS => Command\Api\CreateKeyCommand::class, Command\Api\DisableKeyCommand::NAME => Command\Api\DisableKeyCommand::class, Command\Api\ListKeysCommand::NAME => Command\Api\ListKeysCommand::class, diff --git a/module/CLI/config/dependencies.config.php b/module/CLI/config/dependencies.config.php index 6b7fc552..c96b13c1 100644 --- a/module/CLI/config/dependencies.config.php +++ b/module/CLI/config/dependencies.config.php @@ -50,7 +50,7 @@ return [ Command\Visit\DeleteOrphanVisitsCommand::class => ConfigAbstractFactory::class, Command\Visit\GetNonOrphanVisitsCommand::class => ConfigAbstractFactory::class, - Command\Api\GenerateKeyCommand::class => ConfigAbstractFactory::class, + Command\Api\CreateKeyCommand::class => ConfigAbstractFactory::class, Command\Api\DisableKeyCommand::class => ConfigAbstractFactory::class, Command\Api\ListKeysCommand::class => ConfigAbstractFactory::class, @@ -102,7 +102,7 @@ return [ Command\Visit\DeleteOrphanVisitsCommand::class => [Visit\VisitsDeleter::class], Command\Visit\GetNonOrphanVisitsCommand::class => [Visit\VisitsStatsHelper::class, ShortUrlStringifier::class], - Command\Api\GenerateKeyCommand::class => [ApiKeyService::class, ApiKey\RoleResolver::class], + Command\Api\CreateKeyCommand::class => [ApiKeyService::class, ApiKey\RoleResolver::class], Command\Api\DisableKeyCommand::class => [ApiKeyService::class], Command\Api\ListKeysCommand::class => [ApiKeyService::class], diff --git a/module/CLI/src/Command/Api/GenerateKeyCommand.php b/module/CLI/src/Command/Api/CreateKeyCommand.php similarity index 98% rename from module/CLI/src/Command/Api/GenerateKeyCommand.php rename to module/CLI/src/Command/Api/CreateKeyCommand.php index ec7b5cb2..30d4ca58 100644 --- a/module/CLI/src/Command/Api/GenerateKeyCommand.php +++ b/module/CLI/src/Command/Api/CreateKeyCommand.php @@ -22,10 +22,9 @@ use Symfony\Component\Console\Style\SymfonyStyle; use function Shlinkio\Shlink\Core\arrayToString; use function sprintf; -class GenerateKeyCommand extends Command +class CreateKeyCommand extends Command { public const NAME = 'api-key:create'; - /** @deprecated */ public const ALIAS = 'api-key:generate'; public function __construct( diff --git a/module/CLI/test-cli/Command/CreateApiKeyTest.php b/module/CLI/test-cli/Command/CreateApiKeyTest.php new file mode 100644 index 00000000..1b2b9c0d --- /dev/null +++ b/module/CLI/test-cli/Command/CreateApiKeyTest.php @@ -0,0 +1,31 @@ +<?php + +declare(strict_types=1); + +namespace ShlinkioCliTest\Shlink\CLI\Command; + +use PHPUnit\Framework\Attributes\Test; +use Shlinkio\Shlink\CLI\Command\Api\CreateKeyCommand; +use Shlinkio\Shlink\CLI\Util\ExitCode; +use Shlinkio\Shlink\TestUtils\CliTest\CliTestCase; + +class CreateApiKeyTest extends CliTestCase +{ + #[Test] + public function outputIsCorrect(): void + { + [$output, $exitCode] = $this->exec([CreateKeyCommand::NAME]); + + self::assertStringContainsString('[OK] Generated API key', $output); + self::assertEquals(ExitCode::EXIT_SUCCESS, $exitCode); + } + + #[Test] + public function allowsCustomKeyToBeProvided(): void + { + [$output, $exitCode] = $this->exec([CreateKeyCommand::NAME, 'custom_api_key']); + + self::assertStringContainsString('[OK] Generated API key: "custom_api_key"', $output); + self::assertEquals(ExitCode::EXIT_SUCCESS, $exitCode); + } +} diff --git a/module/CLI/test-cli/Command/GenerateApiKeyTest.php b/module/CLI/test-cli/Command/GenerateApiKeyTest.php deleted file mode 100644 index 7d90c336..00000000 --- a/module/CLI/test-cli/Command/GenerateApiKeyTest.php +++ /dev/null @@ -1,22 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace ShlinkioCliTest\Shlink\CLI\Command; - -use PHPUnit\Framework\Attributes\Test; -use Shlinkio\Shlink\CLI\Command\Api\GenerateKeyCommand; -use Shlinkio\Shlink\CLI\Util\ExitCode; -use Shlinkio\Shlink\TestUtils\CliTest\CliTestCase; - -class GenerateApiKeyTest extends CliTestCase -{ - #[Test] - public function outputIsCorrect(): void - { - [$output, $exitCode] = $this->exec([GenerateKeyCommand::NAME]); - - self::assertStringContainsString('[OK] Generated API key', $output); - self::assertEquals(ExitCode::EXIT_SUCCESS, $exitCode); - } -} diff --git a/module/CLI/test/Command/Api/GenerateKeyCommandTest.php b/module/CLI/test/Command/Api/CreateKeyCommandTest.php similarity index 78% rename from module/CLI/test/Command/Api/GenerateKeyCommandTest.php rename to module/CLI/test/Command/Api/CreateKeyCommandTest.php index 0b306357..44481114 100644 --- a/module/CLI/test/Command/Api/GenerateKeyCommandTest.php +++ b/module/CLI/test/Command/Api/CreateKeyCommandTest.php @@ -9,7 +9,7 @@ use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Shlinkio\Shlink\CLI\ApiKey\RoleResolverInterface; -use Shlinkio\Shlink\CLI\Command\Api\GenerateKeyCommand; +use Shlinkio\Shlink\CLI\Command\Api\CreateKeyCommand; use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta; use Shlinkio\Shlink\Rest\Entity\ApiKey; use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface; @@ -17,9 +17,7 @@ use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Tester\CommandTester; -use function is_string; - -class GenerateKeyCommandTest extends TestCase +class CreateKeyCommandTest extends TestCase { use CliTestUtilsTrait; @@ -32,7 +30,7 @@ class GenerateKeyCommandTest extends TestCase $roleResolver = $this->createMock(RoleResolverInterface::class); $roleResolver->method('determineRoles')->with($this->isInstanceOf(InputInterface::class))->willReturn([]); - $command = new GenerateKeyCommand($this->apiKeyService, $roleResolver); + $command = new CreateKeyCommand($this->apiKeyService, $roleResolver); $this->commandTester = $this->testerForCommand($command); } @@ -65,11 +63,21 @@ class GenerateKeyCommandTest extends TestCase public function nameIsDefinedIfProvided(): void { $this->apiKeyService->expects($this->once())->method('create')->with( - $this->callback(fn (ApiKeyMeta $meta) => is_string($meta->name)), + $this->callback(fn (ApiKeyMeta $meta) => $meta->name === 'Alice'), )->willReturn(ApiKey::create()); $this->commandTester->execute([ '--name' => 'Alice', ]); } + + #[Test] + public function createsCustomApiKeyWhenProvided(): void + { + $this->apiKeyService->expects($this->once())->method('create')->with( + $this->callback(fn (ApiKeyMeta $meta) => $meta->key === 'my_custom_key'), + )->willReturn(ApiKey::create()); + + $this->commandTester->execute(['key' => 'my_custom_key']); + } }