Skip to content

Commit

Permalink
restore commands
Browse files Browse the repository at this point in the history
  • Loading branch information
danepowell committed May 16, 2024
1 parent 5cdf8ec commit 569f796
Show file tree
Hide file tree
Showing 86 changed files with 9,962 additions and 0 deletions.
154 changes: 154 additions & 0 deletions tests/phpunit/src/Commands/Acsf/AcsfApiCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php

declare(strict_types = 1);

namespace Acquia\Cli\Tests\Commands\Acsf;

use Acquia\Cli\AcsfApi\AcsfClient;
use Acquia\Cli\AcsfApi\AcsfClientService;
use Acquia\Cli\AcsfApi\AcsfCredentials;
use Acquia\Cli\Command\Acsf\AcsfCommandFactory;
use Acquia\Cli\Command\Api\ApiBaseCommand;
use Acquia\Cli\Command\CommandBase;
use Acquia\Cli\CommandFactoryInterface;
use Acquia\Cli\Exception\AcquiaCliException;
use Prophecy\Argument;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @property \Acquia\Cli\Command\Api\ApiBaseCommand $command
*/
class AcsfApiCommandTest extends AcsfCommandTestBase {

public function setUp(): void {
parent::setUp();
$this->clientProphecy->addOption('headers', ['Accept' => 'application/hal+json, version=2']);
putenv('ACQUIA_CLI_USE_CLOUD_API_SPEC_CACHE=1');
}

protected function createCommand(): CommandBase {
$this->createMockCloudConfigFile($this->getAcsfCredentialsFileContents());
$this->cloudCredentials = new AcsfCredentials($this->datastoreCloud);
$this->setClientProphecies();
return $this->injectCommand(ApiBaseCommand::class);
}

public function testAcsfCommandExecutionForHttpPostWithMultipleDataTypes(): void {
$mockBody = $this->getMockResponseFromSpec('/api/v1/groups/{group_id}/members', 'post', '200');
$this->clientProphecy->request('post', '/api/v1/groups/1/members')->willReturn($mockBody)->shouldBeCalled();
$this->clientProphecy->addOption('json', ["uids" => ["1", "2", "3"]])->shouldBeCalled();
$this->command = $this->getApiCommandByName('acsf:groups:add-members');
$this->executeCommand([
'uids' => '1,2,3',
], [
// group_id.
'1',
]);

// Assert.
$output = $this->getDisplay();
}

public function testAcsfCommandExecutionBool(): void {
$mockBody = $this->getMockResponseFromSpec('/api/v1/update/pause', 'post', '200');
$this->clientProphecy->request('post', '/api/v1/update/pause')->willReturn($mockBody)->shouldBeCalled();
$this->clientProphecy->addOption('json', ["pause" => TRUE])->shouldBeCalled();
$this->command = $this->getApiCommandByName('acsf:updates:pause');
$this->executeCommand([], [
// Pause.
'1',
]);

// Assert.
}

public function testAcsfCommandExecutionForHttpGet(): void {
$mockBody = $this->getMockResponseFromSpec('/api/v1/audit', 'get', '200');
$this->clientProphecy->addQuery('limit', '1')->shouldBeCalled();
$this->clientProphecy->request('get', '/api/v1/audit')->willReturn($mockBody)->shouldBeCalled();
$this->command = $this->getApiCommandByName('acsf:info:audit-events-find');
// Our mock Client doesn't actually return a limited dataset, but we still assert it was passed added to the
// client's query correctly.
$this->executeCommand(['--limit' => '1']);

// Assert.
$output = $this->getDisplay();
$this->assertNotNull($output);
$this->assertJson($output);
$contents = json_decode($output, TRUE);
$this->assertArrayHasKey('count', $contents);
}

/**
* @return array<mixed>
*/
public function providerTestAcsfCommandExecutionForHttpGetMultiple(): array {
return [
['get', '/api/v1/audit', '/api/v1/audit', 'acsf:info:audit-events-find', [], []],
['post', '/api/v1/sites', '/api/v1/sites', 'acsf:sites:create', ['site_name' => 'foobar', '--stack_id' => '1', 'group_ids' => ['91,81']], ['site_name' => 'foobar', 'stack_id' => '1', 'group_ids' => [91, 81]]],
['post', '/api/v1/sites', '/api/v1/sites', 'acsf:sites:create', ['site_name' => 'foobar', '--stack_id' => '1', 'group_ids' => ['91','81']], ['site_name' => 'foobar', 'stack_id' => '1', 'group_ids' => [91, 81]]],
['post', '/api/v1/sites/{site_id}/backup', '/api/v1/sites/1/backup', 'acsf:sites:backup', ['--label' => 'foo', 'site_id' => '1'], ['label' => 'foo']],
['post', '/api/v1/groups/{group_id}/members', '/api/v1/groups/2/members', 'acsf:groups:add-members', ['group_id' => '2', 'uids' => '1'], ['group_id' => 'foo', 'uids' => 1]],
['post', '/api/v1/groups/{group_id}/members', '/api/v1/groups/2/members', 'acsf:groups:add-members', ['group_id' => '2', 'uids' => '1,3'], ['group_id' => 'foo', 'uids' => [1, 3]]],
];
}

/**
* @dataProvider providerTestAcsfCommandExecutionForHttpGetMultiple
*/
public function testAcsfCommandExecutionForHttpGetMultiple(string $method, string $specPath, string $path, string $command, array $arguments = [], array $jsonArguments = []): void {
$mockBody = $this->getMockResponseFromSpec($specPath, $method, '200');
$this->clientProphecy->request($method, $path)->willReturn($mockBody)->shouldBeCalled();
foreach ($jsonArguments as $argumentName => $value) {
$this->clientProphecy->addOption('json', [$argumentName => $value]);
}
$this->command = $this->getApiCommandByName($command);
$this->executeCommand($arguments);

// Assert.
$output = $this->getDisplay();
$this->assertNotNull($output);
$this->assertJson($output);
json_decode($output, TRUE);
}

public function testAcsfUnauthenticatedFailure(): void {
$this->clientServiceProphecy->isMachineAuthenticated()->willReturn(FALSE);
$this->removeMockConfigFiles();

$inputs = [
// Would you like to share anonymous performance usage and data?
'n',
];
$this->expectException(AcquiaCliException::class);
$this->expectExceptionMessage('This machine is not yet authenticated with Site Factory.');
$this->executeCommand([], $inputs);
}

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(AcsfClientService::class);
$this->clientServiceProphecy->getClient()
->willReturn($this->clientProphecy->reveal());
$this->clientServiceProphecy->isMachineAuthenticated()
->willReturn(TRUE);
}

protected function getCommandFactory(): CommandFactoryInterface {
return new AcsfCommandFactory(
$this->localMachineHelper,
$this->datastoreCloud,
$this->datastoreAcli,
$this->cloudCredentials,
$this->telemetryHelper,
$this->projectDir,
$this->clientServiceProphecy->reveal(),
$this->sshHelper,
$this->sshDir,
$this->logger,
);
}

}
140 changes: 140 additions & 0 deletions tests/phpunit/src/Commands/Acsf/AcsfAuthLoginCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php

declare(strict_types = 1);

namespace Acquia\Cli\Tests\Commands\Acsf;

use Acquia\Cli\AcsfApi\AcsfCredentials;
use Acquia\Cli\Command\Auth\AuthAcsfLoginCommand;
use Acquia\Cli\Command\CommandBase;
use Acquia\Cli\Config\CloudDataConfig;
use Acquia\Cli\DataStore\CloudDataStore;

/**
* @property \Acquia\Cli\Command\Auth\AuthLoginCommand $command
*/
class AcsfAuthLoginCommandTest extends AcsfCommandTestBase {

protected function createCommand(): CommandBase {
$this->cloudCredentials = new AcsfCredentials($this->datastoreCloud);
return $this->injectCommand(AuthAcsfLoginCommand::class);
}

/**
* @return array<mixed>
*/
public function providerTestAuthLoginCommand(): array {
return [
// Data set 0.
[
// $machineIsAuthenticated
FALSE,
// $inputs
[
// Would you like to share anonymous performance usage and data? (yes/no) [yes].
'yes',
// Enter the full URL of the factory.
$this->acsfCurrentFactoryUrl,
// Enter a value for username.
$this->acsfUsername,
// Enter a value for key.
$this->acsfKey,
],
// No arguments, all interactive.
[],
// Output to assert.
'Saved credentials',
],
// Data set 1.
[
// $machineIsAuthenticated
FALSE,
// $inputs
[],
// Arguments.
[
// Enter the full URL of the factory.
'--factory-url' => $this->acsfCurrentFactoryUrl,
// Enter a value for key.
'--key' => $this->acsfKey,
// Enter a value for username.
'--username' => $this->acsfUsername,
],
// Output to assert.
'Saved credentials',
// $config.
$this->getAcsfCredentialsFileContents(),
],
// Data set 2.
[
// $machineIsAuthenticated
TRUE,
// $inputs
[
// Choose a factory to log in to.
$this->acsfCurrentFactoryUrl,
// Choose which user to log in as.
$this->acsfUsername,
],
// Arguments.
[],
// Output to assert.
"Acquia CLI is now logged in to $this->acsfCurrentFactoryUrl as $this->acsfUsername",
// $config.
$this->getAcsfCredentialsFileContents(),
],
];
}

/**
* @dataProvider providerTestAuthLoginCommand
* @requires OS linux|darwin
*/
public function testAcsfAuthLoginCommand(bool $machineIsAuthenticated, array $inputs, array $args, string $outputToAssert, array $config = []): void {
if (!$machineIsAuthenticated) {
$this->clientServiceProphecy->isMachineAuthenticated()->willReturn(FALSE);
$this->removeMockCloudConfigFile();
}
else {
$this->removeMockCloudConfigFile();
$this->createMockCloudConfigFile($config);
}
$this->createDataStores();
$this->command = $this->createCommand();

$this->executeCommand($args, $inputs);
$output = $this->getDisplay();
$this->assertStringContainsString($outputToAssert, $output);
if (!$machineIsAuthenticated && !array_key_exists('--key', $args)) {
$this->assertStringContainsString('Enter your Site Factory key (option -k, --key) (input will be hidden):', $output);
}
$this->assertKeySavedCorrectly();
$this->assertEquals($this->acsfActiveUser, $this->cloudCredentials->getCloudKey());
$this->assertEquals($this->acsfKey, $this->cloudCredentials->getCloudSecret());
$this->assertEquals($this->acsfCurrentFactoryUrl, $this->cloudCredentials->getBaseUri());

}

protected function assertKeySavedCorrectly(): void {
$credsFile = $this->cloudConfigFilepath;
$this->assertFileExists($credsFile);
$config = new CloudDataStore($this->localMachineHelper, new CloudDataConfig(), $credsFile);
$this->assertTrue($config->exists('acsf_active_factory'));
$factoryUrl = $config->get('acsf_active_factory');
$this->assertTrue($config->exists('acsf_factories'));
$factories = $config->get('acsf_factories');
$this->assertArrayHasKey($factoryUrl, $factories);
$factory = $factories[$factoryUrl];
$this->assertArrayHasKey('users', $factory);
$this->assertArrayHasKey('active_user', $factory);
$this->assertEquals($this->acsfUsername, $factory['active_user']);
$users = $factory['users'];
$this->assertArrayHasKey($factory['active_user'], $users);
$user = $users[$factory['active_user']];
$this->assertArrayHasKey('username', $user);
$this->assertArrayHasKey('key', $user);
$this->assertEquals($this->acsfUsername, $user['username']);
$this->assertEquals($this->acsfKey, $user['key']);
}

}
73 changes: 73 additions & 0 deletions tests/phpunit/src/Commands/Acsf/AcsfAuthLogoutCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types = 1);

namespace Acquia\Cli\Tests\Commands\Acsf;

use Acquia\Cli\AcsfApi\AcsfCredentials;
use Acquia\Cli\Command\Auth\AuthAcsfLogoutCommand;
use Acquia\Cli\Command\CommandBase;
use Acquia\Cli\Config\CloudDataConfig;
use Acquia\Cli\DataStore\CloudDataStore;

/**
* @property AcsfAuthLogoutCommandTest $command
*/
class AcsfAuthLogoutCommandTest extends AcsfCommandTestBase {

protected function createCommand(): CommandBase {
$this->cloudCredentials = new AcsfCredentials($this->datastoreCloud);
return $this->injectCommand(AuthAcsfLogoutCommand::class);
}

/**
* @return array<mixed>
*/
public function providerTestAuthLogoutCommand(): array {
return [
// Data set 0.
[
// $machineIsAuthenticated
FALSE,
// $inputs
[],
],
// Data set 1.
[
// $machineIsAuthenticated
TRUE,
// $inputs
[
// Choose a Factory to logout of.
0,
],
// $config.
$this->getAcsfCredentialsFileContents(),
],
];
}

/**
* @dataProvider providerTestAuthLogoutCommand
*/
public function testAcsfAuthLogoutCommand(bool $machineIsAuthenticated, array $inputs, array $config = []): void {
if (!$machineIsAuthenticated) {
$this->clientServiceProphecy->isMachineAuthenticated()->willReturn(FALSE);
$this->removeMockCloudConfigFile();
}
else {
$this->createMockCloudConfigFile($config);
}

$this->createDataStores();
$this->command = $this->createCommand();
$this->executeCommand([], $inputs);
$output = $this->getDisplay();
// Assert creds are removed locally.
$this->assertFileExists($this->cloudConfigFilepath);
$config = new CloudDataStore($this->localMachineHelper, new CloudDataConfig(), $this->cloudConfigFilepath);
$this->assertFalse($config->exists('acli_key'));
$this->assertNull($config->get('acsf_active_factory'));
}

}
Loading

0 comments on commit 569f796

Please sign in to comment.