mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-06 23:33:13 +08:00
Roll back change to allow creating API keys with custom value
This commit is contained in:
@@ -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,
|
||||
|
||||
|
||||
@@ -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],
|
||||
|
||||
|
||||
@@ -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),
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
22
module/CLI/test-cli/Command/GenerateApiKeyTest.php
Normal file
22
module/CLI/test-cli/Command/GenerateApiKeyTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user