Skip to content

Commit

Permalink
CLI-1294: [auth:logout] warning when key is invalid (#1716)
Browse files Browse the repository at this point in the history
* CLI-1294: [auth:logout] warning when key is invalid

* fix tests

* add coverage
  • Loading branch information
danepowell authored Apr 2, 2024
1 parent a1f59d3 commit 8bb887e
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Command/Auth/AuthLoginCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
3 changes: 3 additions & 0 deletions src/Command/Auth/AuthLogoutCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
6 changes: 6 additions & 0 deletions src/EventListener/ExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand All @@ -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] = '<options=bold>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);
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/src/Commands/Auth/AuthLoginCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
22 changes: 22 additions & 0 deletions tests/phpunit/src/Commands/Auth/AuthLogoutCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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();
}

}
5 changes: 5 additions & 0 deletions tests/phpunit/src/Misc/ExceptionListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function testHelp(Throwable $error, string|array $helpText): void {
else {
$messages = array_merge([$helpText], $messages1);
}
$messages[0] = "<options=bold>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());
Expand Down Expand Up @@ -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 <bg=blue;fg=white;options=bold>acli ide:delete</> or contact your Account Manager or Acquia Sales to purchase additional IDEs.',
Expand Down

0 comments on commit 8bb887e

Please sign in to comment.