Skip to content

Commit

Permalink
CLI-1219: API key labels should be email addresses (#1653)
Browse files Browse the repository at this point in the history
* CLI-1219: API key labels should be email addresses

* fix
  • Loading branch information
danepowell authored Dec 15, 2023
1 parent ab3bd5b commit ce271ed
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 40 deletions.
2 changes: 2 additions & 0 deletions .phpstorm.meta.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php
namespace PHPSTORM_META {

use AcquiaCloudApi\Response\AccountResponse;
use AcquiaCloudApi\Response\ApplicationResponse;
use AcquiaCloudApi\Response\ApplicationsResponse;
use AcquiaCloudApi\Response\DatabasesResponse;
use AcquiaCloudApi\Response\EnvironmentsResponse;

override(\Acquia\Cli\Tests\TestBase::mockRequest(), map([
'getAccount' => AccountResponse::class,
'getApplications' => ApplicationsResponse::class,
'getApplicationByUuid' => ApplicationResponse::class,
'getApplicationEnvironments' => EnvironmentsResponse::class,
Expand Down
6 changes: 4 additions & 2 deletions src/Command/Auth/AuthLoginCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Acquia\Cli\Command\Auth;

use Acquia\Cli\Command\CommandBase;
use AcquiaCloudApi\Endpoints\Account;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -61,10 +62,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

private function writeApiCredentialsToDisk(string $apiKey, string $apiSecret): void {
$tokenInfo = $this->cloudApiClientService->getClient()->request('get', "/account/tokens/$apiKey");
$account = new Account($this->cloudApiClientService->getClient());
$accountInfo = $account->get();
$keys = $this->datastoreCloud->get('keys');
$keys[$apiKey] = [
'label' => $tokenInfo->label,
'label' => $accountInfo->mail,
'secret' => $apiSecret,
'uuid' => $apiKey,
];
Expand Down
7 changes: 3 additions & 4 deletions tests/phpunit/src/Commands/Acsf/AcsfApiCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Acquia\Cli\AcsfApi\AcsfClient;
use Acquia\Cli\AcsfApi\AcsfClientService;
use Acquia\Cli\AcsfApi\AcsfCredentials;
use Acquia\Cli\CloudApi\ClientService;
use Acquia\Cli\Command\Acsf\AcsfApiBaseCommand;
use Acquia\Cli\Command\Acsf\AcsfCommandFactory;
use Acquia\Cli\Command\CommandBase;
Expand All @@ -32,7 +31,7 @@ public function setUp(): void {
protected function createCommand(): CommandBase {
$this->createMockCloudConfigFile($this->getAcsfCredentialsFileContents());
$this->cloudCredentials = new AcsfCredentials($this->datastoreCloud);
$this->setClientProphecies(AcsfClientService::class);
$this->setClientProphecies();
return $this->injectCommand(AcsfApiBaseCommand::class);
}

Expand Down Expand Up @@ -119,11 +118,11 @@ public function testAcsfCommandExecutionForHttpGetMultiple(mixed $method, mixed
$contents = json_decode($output, TRUE);
}

protected function setClientProphecies(?string $clientServiceClass = ClientService::class): void {
protected function setClientProphecies(): void {
$this->clientProphecy = $this->prophet->prophesize(AcsfClient::class);
$this->clientProphecy->addOption('headers', ['User-Agent' => 'acli/UNKNOWN']);
$this->clientProphecy->addOption('debug', Argument::type(OutputInterface::class));
$this->clientServiceProphecy = $this->prophet->prophesize($clientServiceClass);
$this->clientServiceProphecy = $this->prophet->prophesize(AcsfClientService::class);
$this->clientServiceProphecy->getClient()
->willReturn($this->clientProphecy->reveal());
$this->clientServiceProphecy->isMachineAuthenticated()
Expand Down
43 changes: 11 additions & 32 deletions tests/phpunit/src/Commands/Auth/AuthLoginCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use Acquia\Cli\Config\CloudDataConfig;
use Acquia\Cli\DataStore\CloudDataStore;
use Acquia\Cli\Tests\CommandTestBase;
use AcquiaCloudApi\Connector\Connector;
use Generator;
use Prophecy\Argument;
use Symfony\Component\Validator\Exception\ValidatorException;

/**
Expand All @@ -21,41 +23,18 @@ protected function createCommand(): CommandBase {
return $this->injectCommand(AuthLoginCommand::class);
}

public function providerTestAuthLoginCommand(): Generator {
yield 'Keys as args' => [
// $machineIsAuthenticated
FALSE,
// $assertCloudPrompts
FALSE,
// No interaction
[],
// Args.
['--key' => $this->key, '--secret' => $this->secret],
// Output to assert.
'Saved credentials',
];
}

/**
* @dataProvider providerTestAuthLoginCommand
* @group brokenProphecy
*/
public function testAuthLoginCommand(bool $machineIsAuthenticated, bool $assertCloudPrompts, array $inputs, array $args, string $outputToAssert): void {
$this->mockRequest('getAccountToken', $this->key);
if (!$machineIsAuthenticated) {
$this->clientServiceProphecy->isMachineAuthenticated()->willReturn(FALSE);
$this->removeMockCloudConfigFile();
$this->createDataStores();
$this->command = $this->createCommand();
}
public function testAuthLoginCommand(): void {
$this->mockRequest('getAccount');
$this->clientServiceProphecy->setConnector(Argument::type(Connector::class))->shouldBeCalled();
$this->clientServiceProphecy->isMachineAuthenticated()->willReturn(FALSE);
$this->removeMockCloudConfigFile();
$this->createDataStores();
$this->command = $this->createCommand();

$this->executeCommand($args, $inputs);
$this->executeCommand(['--key' => $this->key, '--secret' => $this->secret]);
$output = $this->getDisplay();

if ($assertCloudPrompts) {
$this->assertInteractivePrompts($output);
}
$this->assertStringContainsString($outputToAssert, $output);
$this->assertStringContainsString('Saved credentials', $output);
$this->assertKeySavedCorrectly();
}

Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/src/TestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -731,11 +731,11 @@ public function mockGuzzleClientForUpdate(array $releases): ObjectProphecy {
return $guzzleClient;
}

protected function setClientProphecies(?string $clientServiceClass = ClientService::class): void {
protected function setClientProphecies(): void {
$this->clientProphecy = $this->prophet->prophesize(Client::class);
$this->clientProphecy->addOption('headers', ['User-Agent' => 'acli/UNKNOWN']);
$this->clientProphecy->addOption('debug', Argument::type(OutputInterface::class));
$this->clientServiceProphecy = $this->prophet->prophesize($clientServiceClass);
$this->clientServiceProphecy = $this->prophet->prophesize(ClientService::class);
$this->clientServiceProphecy->getClient()
->willReturn($this->clientProphecy->reveal());
$this->clientServiceProphecy->isMachineAuthenticated()
Expand Down

0 comments on commit ce271ed

Please sign in to comment.