Roll back change to allow creating API keys with custom value

This commit is contained in:
Alejandro Celaya 2023-09-21 08:58:05 +02:00
parent f6b1cc7556
commit 6db46b50e9
8 changed files with 38 additions and 62 deletions

View file

@ -84,7 +84,7 @@ return [
'command' => 'bin/cli ' . Command\Visit\DownloadGeoLiteDbCommand::NAME,
],
InstallationCommand::API_KEY_GENERATE->value => [
'command' => 'bin/cli ' . Command\Api\CreateKeyCommand::NAME,
'command' => 'bin/cli ' . Command\Api\GenerateKeyCommand::NAME,
],
],
],

View file

@ -12,8 +12,13 @@ fi
php vendor/bin/shlink-installer init ${flags}
# TODO If INIT_API_KEY was provided, create an initial API key
#if [ -n "${INIT_API_KEY}" ]; then
# php bin/cli api-key:initial "${INIT_API_KEY}"
#fi
# Periodically run visit:locate every hour, if ENABLE_PERIODIC_VISIT_LOCATE=true was provided and running as root
# ENABLE_PERIODIC_VISIT_LOCATE is deprecated. Remove cron support in Shlink 4.0.0
# FIXME: ENABLE_PERIODIC_VISIT_LOCATE is deprecated. Remove cron support in Shlink 4.0.0
if [ "${ENABLE_PERIODIC_VISIT_LOCATE}" = "true" ] && [ "${SHLINK_USER_ID}" = "root" ]; then
echo "Configuring periodic visit location..."
echo "0 * * * * php /etc/shlink/bin/cli visit:locate -q" > /etc/crontabs/root

View file

@ -21,8 +21,7 @@ return [
Command\Visit\DeleteOrphanVisitsCommand::NAME => Command\Visit\DeleteOrphanVisitsCommand::class,
Command\Visit\GetNonOrphanVisitsCommand::NAME => Command\Visit\GetNonOrphanVisitsCommand::class,
Command\Api\CreateKeyCommand::NAME => Command\Api\CreateKeyCommand::class,
Command\Api\CreateKeyCommand::ALIAS => Command\Api\CreateKeyCommand::class,
Command\Api\GenerateKeyCommand::NAME => Command\Api\GenerateKeyCommand::class,
Command\Api\DisableKeyCommand::NAME => Command\Api\DisableKeyCommand::class,
Command\Api\ListKeysCommand::NAME => Command\Api\ListKeysCommand::class,

View file

@ -50,7 +50,7 @@ return [
Command\Visit\DeleteOrphanVisitsCommand::class => ConfigAbstractFactory::class,
Command\Visit\GetNonOrphanVisitsCommand::class => ConfigAbstractFactory::class,
Command\Api\CreateKeyCommand::class => ConfigAbstractFactory::class,
Command\Api\GenerateKeyCommand::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\CreateKeyCommand::class => [ApiKeyService::class, ApiKey\RoleResolver::class],
Command\Api\GenerateKeyCommand::class => [ApiKeyService::class, ApiKey\RoleResolver::class],
Command\Api\DisableKeyCommand::class => [ApiKeyService::class],
Command\Api\ListKeysCommand::class => [ApiKeyService::class],

View file

@ -13,7 +13,6 @@ use Shlinkio\Shlink\Rest\ApiKey\Role;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
@ -22,10 +21,9 @@ use Symfony\Component\Console\Style\SymfonyStyle;
use function Shlinkio\Shlink\Core\arrayToString;
use function sprintf;
class CreateKeyCommand extends Command
class GenerateKeyCommand extends Command
{
public const NAME = 'api-key:create';
public const ALIAS = 'api-key:generate';
public const NAME = 'api-key:generate';
public function __construct(
private readonly ApiKeyServiceInterface $apiKeyService,
@ -60,13 +58,7 @@ class CreateKeyCommand extends Command
$this
->setName(self::NAME)
->setDescription('Creates a new valid API key.')
->setAliases([self::ALIAS])
->addArgument(
'key',
InputArgument::OPTIONAL,
'The API key to create. A random one will be generated if not provided',
)
->setDescription('Generate a new valid API key.')
->addOption(
'name',
'm',
@ -102,7 +94,6 @@ class CreateKeyCommand extends Command
$expirationDate = $input->getOption('expiration-date');
$apiKey = $this->apiKeyService->create(ApiKeyMeta::fromParams(
key: $input->getArgument('key'),
name: $input->getOption('name'),
expirationDate: isset($expirationDate) ? Chronos::parse($expirationDate) : null,
roleDefinitions: $this->roleResolver->determineRoles($input),

View file

@ -1,31 +0,0 @@
<?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);
}
}

View file

@ -0,0 +1,22 @@
<?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);
}
}

View file

@ -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\CreateKeyCommand;
use Shlinkio\Shlink\CLI\Command\Api\GenerateKeyCommand;
use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
@ -17,7 +17,7 @@ use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Tester\CommandTester;
class CreateKeyCommandTest extends TestCase
class GenerateKeyCommandTest extends TestCase
{
use CliTestUtilsTrait;
@ -30,7 +30,7 @@ class CreateKeyCommandTest extends TestCase
$roleResolver = $this->createMock(RoleResolverInterface::class);
$roleResolver->method('determineRoles')->with($this->isInstanceOf(InputInterface::class))->willReturn([]);
$command = new CreateKeyCommand($this->apiKeyService, $roleResolver);
$command = new GenerateKeyCommand($this->apiKeyService, $roleResolver);
$this->commandTester = $this->testerForCommand($command);
}
@ -70,14 +70,4 @@ class CreateKeyCommandTest extends TestCase
'--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']);
}
}