Skip to content

Commit

Permalink
GL-1723: Added expires_at mandatory arg for codestudio token. (#1617)
Browse files Browse the repository at this point in the history
* GL-1723: Added expires_at mandatory arg for codestudio token.

* GL-1723: Optimised lines in code.

* increase expiration

* Cleaning up tests

* fix tests

---------

Co-authored-by: Dane Powell <[email protected]>
  • Loading branch information
akashkska and danepowell authored Oct 31, 2023
1 parent 86af6e4 commit bf2b514
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 19 deletions.
2 changes: 2 additions & 0 deletions src/Command/CodeStudio/CodeStudioWizardCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Acquia\Cli\Command\WizardCommandBase;
use Acquia\Cli\Output\Checklist;
use AcquiaCloudApi\Endpoints\Account;
use DateTime;
use Gitlab\Exception\ValidationFailedException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -163,6 +164,7 @@ private function createProjectAccessToken(array $project, string $projectAccessT
$this->checklist->addItem("Creating access token named <comment>$projectAccessTokenName</comment>");
$projectAccessToken = $this->gitLabClient->projects()
->createProjectAccessToken($project['id'], [
'expires_at' => new DateTime('+365 days'),
'name' => $projectAccessTokenName,
'scopes' => ['api', 'write_repository'],
]);
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/src/Commands/Acsf/AcsfApiCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function testAcsfCommandExecutionForHttpGetMultiple(mixed $method, mixed
$contents = json_decode($output, TRUE);
}

protected function setClientProphecies(mixed $clientServiceClass = ClientService::class): void {
protected function setClientProphecies(?string $clientServiceClass = ClientService::class): void {
$this->clientProphecy = $this->prophet->prophesize(AcsfClient::class);
$this->clientProphecy->addOption('headers', ['User-Agent' => 'acli/UNKNOWN']);
$this->clientProphecy->addOption('debug', Argument::type(OutputInterface::class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Acquia\Cli\Tests\Commands\Ide\IdeRequiredTestTrait;
use Acquia\Cli\Tests\Commands\WizardTestBase;
use Acquia\Cli\Tests\TestBase;
use AcquiaCloudApi\Connector\Connector;
use DateTime;
use Gitlab\Api\Groups;
use Gitlab\Api\ProjectNamespaces;
use Gitlab\Api\Schedules;
Expand Down Expand Up @@ -187,14 +189,9 @@ public function providerTestCommand(): array {

/**
* @dataProvider providerTestCommand
* @param $mockedGitlabProjects
* @param $args
* @param $inputs
*/
public function testCommand(mixed $mockedGitlabProjects, mixed $inputs, mixed $args): void {
$environmentsResponse = $this->getMockEnvironmentsResponse();
$selectedEnvironment = $environmentsResponse->_embedded->items[0];
$this->clientProphecy->request('get', "/applications/{$this::$applicationUuid}/environments")->willReturn($environmentsResponse->_embedded->items)->shouldBeCalled();
public function testCommand(array $mockedGitlabProjects, array $inputs, array $args): void {
$this->clientServiceProphecy->setConnector(Argument::type(Connector::class))->shouldBeCalled();
$this->mockRequest('getAccount');
$this->mockGitLabPermissionsRequest($this::$applicationUuid);

Expand All @@ -204,15 +201,43 @@ public function testCommand(mixed $mockedGitlabProjects, mixed $inputs, mixed $a
$this->mockGitLabNamespaces($gitlabClient);

$projects = $this->mockGetGitLabProjects($this::$applicationUuid, $this->gitLabProjectId, $mockedGitlabProjects);
$projects->create(Argument::type('string'), Argument::type('array'))->willReturn($this->getMockedGitLabProject($this->gitLabProjectId));
$parameters = [
'container_registry_access_level' => 'disabled',
'description' => 'Source repository for Acquia Cloud Platform application <comment>a47ac10b-58cc-4372-a567-0e02b2c3d470</comment>',
'namespace_id' => 47,
'topics' => 'Acquia Cloud Application',
];
$projects->create('Sample-application-1', $parameters)->willReturn($this->getMockedGitLabProject($this->gitLabProjectId));
$this->mockGitLabProjectsTokens($projects);
$projects->update($this->gitLabProjectId, Argument::type('array'));
$parameters = [
'container_registry_access_level' => 'disabled',
'description' => 'Source repository for Acquia Cloud Platform application <comment>a47ac10b-58cc-4372-a567-0e02b2c3d470</comment>',
'topics' => 'Acquia Cloud Application',
];
$projects->update($this->gitLabProjectId, $parameters)->shouldBeCalled();
$projects->uploadAvatar(
33,
Argument::type('string'),
)->shouldBeCalled();
$this->mockGitLabVariables($this->gitLabProjectId, $projects);
$schedules = $this->prophet->prophesize(Schedules::class);
$schedules->showAll($this->gitLabProjectId)->willReturn([]);
$pipeline = ['id' => 1];
$schedules->create($this->gitLabProjectId, Argument::type('array'))->willReturn($pipeline);
$schedules->addVariable($this->gitLabProjectId, $pipeline['id'], Argument::type('array'));
$parameters = [
# Every Thursday at midnight.
'cron' => '0 0 * * 4',
'description' => 'Code Studio Automatic Updates',
'ref' => 'master',
];
$schedules->create($this->gitLabProjectId, $parameters)->willReturn($pipeline);
$schedules->addVariable($this->gitLabProjectId, $pipeline['id'], [
'key' => 'ACQUIA_JOBS_DEPRECATED_UPDATE',
'value' => 'true',
])->shouldBeCalled();
$schedules->addVariable($this->gitLabProjectId, $pipeline['id'], [
'key' => 'ACQUIA_JOBS_COMPOSER_UPDATE',
'value' => 'true',
])->shouldBeCalled();
$gitlabClient->schedules()->willReturn($schedules->reveal());
$gitlabClient->projects()->willReturn($projects);

Expand All @@ -231,13 +256,13 @@ public function testCommand(mixed $mockedGitlabProjects, mixed $inputs, mixed $a

/** @var Filesystem|ObjectProphecy $fileSystem */
$fileSystem = $this->prophet->prophesize(Filesystem::class);
$localMachineHelper->getFilesystem()->willReturn($fileSystem->reveal())->shouldBeCalled();
$this->command->localMachineHelper = $localMachineHelper->reveal();

// Set properties and execute.
$this->executeCommand($args, $inputs);

// Assertions.
$this->prophet->checkPredictions();
$this->assertEquals(0, $this->getStatusCode());
}

Expand Down Expand Up @@ -275,7 +300,7 @@ protected function mockGitLabProjectsTokens(ObjectProphecy $projects): void {
'access_level' => 40,
'active' => TRUE,
'created_at' => '2021-12-28T20:08:21.629Z',
'expires_at' => NULL,
'expires_at' => new DateTime('+365 days'),
'id' => $this->gitLabTokenId,
'name' => 'acquia-codestudio',
'revoked' => FALSE,
Expand All @@ -286,8 +311,8 @@ protected function mockGitLabProjectsTokens(ObjectProphecy $projects): void {
'user_id' => 154,
],
];
$projects->projectAccessTokens($this->gitLabProjectId)->willReturn($tokens);
$projects->deleteProjectAccessToken($this->gitLabProjectId, $this->gitLabTokenId);
$projects->projectAccessTokens($this->gitLabProjectId)->willReturn($tokens)->shouldBeCalled();
$projects->deleteProjectAccessToken($this->gitLabProjectId, $this->gitLabTokenId)->shouldBeCalled();
$token = $tokens[0];
$token['token'] = 'token';
$projects->createProjectAccessToken($this->gitLabProjectId, Argument::type('array'))->willReturn($token);
Expand Down Expand Up @@ -386,9 +411,10 @@ protected function mockGitLabNamespaces(ObjectProphecy $gitlabClient): void {
$gitlabClient->namespaces()->willReturn($namespaces->reveal());
}

protected function mockGitLabVariables(mixed $gitlabProjectId, ObjectProphecy $projects): void {
protected function mockGitLabVariables(int $gitlabProjectId, ObjectProphecy $projects): void {
$projects->variables($gitlabProjectId)->willReturn($this->getMockGitLabVariables());
$projects->addVariable($gitlabProjectId, Argument::type('string'), Argument::type('string'), Argument::type('bool'), NULL, Argument::type('array'));
$projects->addVariable($gitlabProjectId, Argument::type('string'), Argument::type('string'), Argument::type('bool'), NULL, Argument::type('array'))->shouldBeCalled();
$projects->updateVariable($this->gitLabProjectId, Argument::type('string'), Argument::type('string'), FALSE, NULL, ["masked" => TRUE, "variable_type" => "env_var"])->shouldBeCalled();
}

}
2 changes: 1 addition & 1 deletion tests/phpunit/src/TestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ public function mockGuzzleClientForUpdate(array $releases): ObjectProphecy {
return $guzzleClient;
}

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

0 comments on commit bf2b514

Please sign in to comment.