From 9e6f129de6fe066525083ef3e23e1095f54cc3b5 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 7 Nov 2024 14:52:06 +0100 Subject: [PATCH] Make sure a unique name is required by api-key:generate command --- .../src/Command/Api/GenerateKeyCommand.php | 15 ++++++++----- .../Command/Api/GenerateKeyCommandTest.php | 22 ++++++++++++++++++- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/module/CLI/src/Command/Api/GenerateKeyCommand.php b/module/CLI/src/Command/Api/GenerateKeyCommand.php index a6b8bad0..3a1432ac 100644 --- a/module/CLI/src/Command/Api/GenerateKeyCommand.php +++ b/module/CLI/src/Command/Api/GenerateKeyCommand.php @@ -100,16 +100,22 @@ class GenerateKeyCommand extends Command protected function execute(InputInterface $input, OutputInterface $output): int { + $io = new SymfonyStyle($input, $output); $expirationDate = $input->getOption('expiration-date'); - $apiKeyMeta = ApiKeyMeta::fromParams( name: $input->getOption('name'), expirationDate: isset($expirationDate) ? Chronos::parse($expirationDate) : null, roleDefinitions: $this->roleResolver->determineRoles($input), ); - $apiKey = $this->apiKeyService->create($apiKeyMeta); - $io = new SymfonyStyle($input, $output); + if ($this->apiKeyService->existsWithName($apiKeyMeta->name)) { + $io->warning( + sprintf('An API key with name "%s" already exists. Try with a different ome', $apiKeyMeta->name), + ); + return ExitCode::EXIT_WARNING; + } + + $apiKey = $this->apiKeyService->create($apiKeyMeta); $io->success(sprintf('Generated API key: "%s"', $apiKeyMeta->key)); if ($input->isInteractive()) { @@ -120,8 +126,7 @@ class GenerateKeyCommand extends Command ShlinkTable::default($io)->render( ['Role name', 'Role metadata'], $apiKey->mapRoles(fn (Role $role, array $meta) => [$role->value, arrayToString($meta, indentSize: 0)]), - null, - 'Roles', + headerTitle: 'Roles', ); } diff --git a/module/CLI/test/Command/Api/GenerateKeyCommandTest.php b/module/CLI/test/Command/Api/GenerateKeyCommandTest.php index 9c1d337e..10633b9a 100644 --- a/module/CLI/test/Command/Api/GenerateKeyCommandTest.php +++ b/module/CLI/test/Command/Api/GenerateKeyCommandTest.php @@ -10,6 +10,7 @@ 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\Util\ExitCode; use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta; use Shlinkio\Shlink\Rest\Entity\ApiKey; use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface; @@ -64,8 +65,27 @@ class GenerateKeyCommandTest extends TestCase $this->callback(fn (ApiKeyMeta $meta) => $meta->name === 'Alice'), )->willReturn(ApiKey::create()); - $this->commandTester->execute([ + $exitCode = $this->commandTester->execute([ '--name' => 'Alice', ]); + + self::assertEquals(ExitCode::EXIT_SUCCESS, $exitCode); + } + + #[Test] + public function warningIsPrintedIfProvidedNameAlreadyExists(): void + { + $name = 'The API key'; + + $this->apiKeyService->expects($this->never())->method('create'); + $this->apiKeyService->expects($this->once())->method('existsWithName')->with($name)->willReturn(true); + + $exitCode = $this->commandTester->execute([ + '--name' => $name, + ]); + $output = $this->commandTester->getDisplay(); + + self::assertEquals(ExitCode::EXIT_WARNING, $exitCode); + self::assertStringContainsString('An API key with name "The API key" already exists.', $output); } }