Move logic to determine if a new key has a duplicated name to the APiKeyService

This commit is contained in:
Alejandro Celaya
2024-11-08 09:03:50 +01:00
parent b08c498b13
commit 6f837b3b91
6 changed files with 38 additions and 50 deletions

View File

@@ -108,13 +108,6 @@ class GenerateKeyCommand extends Command
roleDefinitions: $this->roleResolver->determineRoles($input),
);
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));

View File

@@ -71,21 +71,4 @@ class GenerateKeyCommandTest extends TestCase
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);
}
}