From 463a0f08afca3c158ae92b6603e1a09fe60a8f11 Mon Sep 17 00:00:00 2001 From: Dane Powell Date: Fri, 29 Mar 2024 13:45:03 -0700 Subject: [PATCH 1/3] CLI-1294: [auth:logout] warning when key is invalid --- src/Application.php | 2 +- src/Command/Auth/AuthLoginCommand.php | 2 +- src/Command/Auth/AuthLogoutCommand.php | 3 +++ src/EventListener/ExceptionListener.php | 6 ++++++ 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Application.php b/src/Application.php index b98c466a2..7a8e3a8f7 100644 --- a/src/Application.php +++ b/src/Application.php @@ -24,7 +24,7 @@ private function getHelpMessages(): array { return $this->helpMessages; } - public function setHelpMessages(mixed $helpMessages): void { + public function setHelpMessages(array $helpMessages): void { $this->helpMessages = $helpMessages; } diff --git a/src/Command/Auth/AuthLoginCommand.php b/src/Command/Auth/AuthLoginCommand.php index 8fb898daa..acceb2ba0 100644 --- a/src/Command/Auth/AuthLoginCommand.php +++ b/src/Command/Auth/AuthLoginCommand.php @@ -27,7 +27,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $keys = $this->datastoreCloud->get('keys'); $activeKey = $this->datastoreCloud->get('acli_key'); if (is_array($keys) && !empty($keys) && !array_key_exists($activeKey, $keys)) { - throw new AcquiaCliException('Invalid key in Cloud datastore; run acli auth:logout && acli auth:login to fix'); + throw new AcquiaCliException('Invalid key in datastore at {filepath}', ['filepath' => $this->datastoreCloud->filepath]); } if ($activeKey) { $activeKeyLabel = $keys[$activeKey]['label']; diff --git a/src/Command/Auth/AuthLogoutCommand.php b/src/Command/Auth/AuthLogoutCommand.php index e498709fc..61f6959da 100644 --- a/src/Command/Auth/AuthLogoutCommand.php +++ b/src/Command/Auth/AuthLogoutCommand.php @@ -22,6 +22,9 @@ protected function configure(): void { protected function execute(InputInterface $input, OutputInterface $output): int { $keys = $this->datastoreCloud->get('keys'); $activeKey = $this->datastoreCloud->get('acli_key'); + if (is_array($keys) && !empty($keys) && !array_key_exists($activeKey, $keys)) { + throw new AcquiaCliException('Invalid key in datastore at {filepath}', ['filepath' => $this->datastoreCloud->filepath]); + } if (!$activeKey) { throw new AcquiaCliException('There is no active Cloud Platform API key'); } diff --git a/src/EventListener/ExceptionListener.php b/src/EventListener/ExceptionListener.php index 42ca280a4..d6368a296 100644 --- a/src/EventListener/ExceptionListener.php +++ b/src/EventListener/ExceptionListener.php @@ -67,6 +67,9 @@ public function onConsoleError(ConsoleErrorEvent $event): void { case 'This machine is not yet authenticated with Site Factory.': $this->helpMessages[] = 'Run `acli auth:acsf-login` to re-authenticate with Site Factory.'; break; + case 'Invalid key in datastore at {filepath}': + $this->helpMessages[] = 'Delete the datastore and run this command again.'; + break; } } @@ -89,6 +92,9 @@ public function onConsoleError(ConsoleErrorEvent $event): void { $newErrorMessage = 'Cloud Platform API returned an unexpected data type. This is not an issue with Acquia CLI but could indicate a problem with your Cloud Platform application.'; } + if (!empty($this->helpMessages)) { + $this->helpMessages[0] = 'How to fix it: ' . $this->helpMessages[0]; + } $this->helpMessages[] = "You can find Acquia CLI documentation at https://docs.acquia.com/acquia-cli/"; $this->writeUpdateHelp($event); $this->writeSupportTicketHelp($event); From cf3445abd9d30372a0ed27a7d051ee9c8bde5a50 Mon Sep 17 00:00:00 2001 From: Dane Powell Date: Fri, 29 Mar 2024 13:56:36 -0700 Subject: [PATCH 2/3] fix tests --- tests/phpunit/src/Commands/Auth/AuthLoginCommandTest.php | 2 +- tests/phpunit/src/Misc/ExceptionListenerTest.php | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/src/Commands/Auth/AuthLoginCommandTest.php b/tests/phpunit/src/Commands/Auth/AuthLoginCommandTest.php index a2ccbbd67..d1638e777 100644 --- a/tests/phpunit/src/Commands/Auth/AuthLoginCommandTest.php +++ b/tests/phpunit/src/Commands/Auth/AuthLoginCommandTest.php @@ -102,7 +102,7 @@ public function testAuthLoginInvalidDatastore(): void { $this->createDataStores(); $this->command = $this->createCommand(); $this->expectException(AcquiaCliException::class); - $this->expectExceptionMessage('Invalid key in Cloud datastore; run acli auth:logout && acli auth:login to fix'); + $this->expectExceptionMessage("Invalid key in datastore at $this->cloudConfigFilepath"); $this->executeCommand(); } diff --git a/tests/phpunit/src/Misc/ExceptionListenerTest.php b/tests/phpunit/src/Misc/ExceptionListenerTest.php index 4b9bdce42..bebe30803 100644 --- a/tests/phpunit/src/Misc/ExceptionListenerTest.php +++ b/tests/phpunit/src/Misc/ExceptionListenerTest.php @@ -33,6 +33,7 @@ public function testHelp(Throwable $error, string|array $helpText): void { else { $messages = array_merge([$helpText], $messages1); } + $messages[0] = "How to fix it: $messages[0]"; $applicationProphecy->setHelpMessages($messages)->shouldBeCalled(); $commandProphecy->getApplication()->willReturn($applicationProphecy->reveal()); $consoleErrorEvent = new ConsoleErrorEvent($this->input, $this->output, $error, $commandProphecy->reveal()); @@ -90,6 +91,10 @@ public function providerTestHelp(): array { new AcquiaCliException('This machine is not yet authenticated with Site Factory.'), 'Run `acli auth:acsf-login` to re-authenticate with Site Factory.', ], + [ + new AcquiaCliException('Invalid key in datastore at {filepath}'), + 'Delete the datastore and run this command again.', + ], [ new ApiErrorException((object) ['error' => '', 'message' => "There are no available Cloud IDEs for this application.\n"]), 'Delete an existing IDE via acli ide:delete or contact your Account Manager or Acquia Sales to purchase additional IDEs.', From 86b0ce4d6250733c3e2ed216942bffe0f881f5e9 Mon Sep 17 00:00:00 2001 From: Dane Powell Date: Fri, 29 Mar 2024 14:01:46 -0700 Subject: [PATCH 3/3] add coverage --- .../Commands/Auth/AuthLogoutCommandTest.php | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/phpunit/src/Commands/Auth/AuthLogoutCommandTest.php b/tests/phpunit/src/Commands/Auth/AuthLogoutCommandTest.php index 5f03bb4ec..c034dfc51 100644 --- a/tests/phpunit/src/Commands/Auth/AuthLogoutCommandTest.php +++ b/tests/phpunit/src/Commands/Auth/AuthLogoutCommandTest.php @@ -8,6 +8,7 @@ use Acquia\Cli\Command\CommandBase; use Acquia\Cli\Config\CloudDataConfig; use Acquia\Cli\DataStore\CloudDataStore; +use Acquia\Cli\Exception\AcquiaCliException; use Acquia\Cli\Tests\CommandTestBase; /** @@ -31,4 +32,25 @@ public function testAuthLogoutCommand(): void { $this->assertStringContainsString('The active Cloud Platform API credentials were deactivated', $output); } + public function testAuthLogoutInvalidDatastore(): void { + $this->clientServiceProphecy->isMachineAuthenticated()->willReturn(FALSE); + $this->removeMockCloudConfigFile(); + $data = [ + 'acli_key' => 'key2', + 'keys' => [ + 'key1' => [ + 'label' => 'foo', + 'secret' => 'foo', + 'uuid' => 'foo', + ], + ], + ]; + $this->fs->dumpFile($this->cloudConfigFilepath, json_encode($data)); + $this->createDataStores(); + $this->command = $this->createCommand(); + $this->expectException(AcquiaCliException::class); + $this->expectExceptionMessage("Invalid key in datastore at $this->cloudConfigFilepath"); + $this->executeCommand(); + } + }