diff --git a/src/Command/Auth/AuthLoginCommand.php b/src/Command/Auth/AuthLoginCommand.php index 580262308..ad6906221 100644 --- a/src/Command/Auth/AuthLoginCommand.php +++ b/src/Command/Auth/AuthLoginCommand.php @@ -5,6 +5,7 @@ namespace Acquia\Cli\Command\Auth; use Acquia\Cli\Command\CommandBase; +use Acquia\Cli\Exception\AcquiaCliException; use AcquiaCloudApi\Endpoints\Account; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; @@ -25,6 +26,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) && !array_key_exists($activeKey, $keys)) { + throw new AcquiaCliException('Invalid key in Cloud datastore; run acli auth:logout && acli auth:login to fix'); + } if ($activeKey) { $activeKeyLabel = $keys[$activeKey]['label']; $output->writeln("The following Cloud Platform API key is active: $activeKeyLabel"); diff --git a/tests/phpunit/src/Commands/Auth/AuthLoginCommandTest.php b/tests/phpunit/src/Commands/Auth/AuthLoginCommandTest.php index 6d3cfaadc..e990c7736 100644 --- a/tests/phpunit/src/Commands/Auth/AuthLoginCommandTest.php +++ b/tests/phpunit/src/Commands/Auth/AuthLoginCommandTest.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; use AcquiaCloudApi\Connector\Connector; use Generator; @@ -68,6 +69,18 @@ public function testAuthLoginInvalidInputCommand(array $inputs, array $args): vo $this->executeCommand($args, $inputs); } + public function testAuthLoginInvalidDatastore(): void { + $this->clientServiceProphecy->isMachineAuthenticated()->willReturn(FALSE); + $this->removeMockCloudConfigFile(); + $this->createDataStores(); + $this->datastoreCloud->set('keys', ['key1']); + $this->datastoreCloud->set('acli_key', 'key2'); + $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->executeCommand(); + } + protected function assertInteractivePrompts(string $output): void { // Your machine has already been authenticated with the Cloud Platform API, would you like to re-authenticate? $this->assertStringContainsString('You will need a Cloud Platform API token from https://cloud.acquia.com/a/profile/tokens', $output);