From bf2b5140ca56229869ae18976325a91f8bccddee Mon Sep 17 00:00:00 2001 From: Akash Karangale <56954201+akashkska@users.noreply.github.com> Date: Wed, 1 Nov 2023 01:01:51 +0530 Subject: [PATCH] GL-1723: Added expires_at mandatory arg for codestudio token. (#1617) * 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 --- .../CodeStudio/CodeStudioWizardCommand.php | 2 + .../src/Commands/Acsf/AcsfApiCommandTest.php | 2 +- .../CodeStudioWizardCommandTest.php | 60 +++++++++++++------ tests/phpunit/src/TestBase.php | 2 +- 4 files changed, 47 insertions(+), 19 deletions(-) diff --git a/src/Command/CodeStudio/CodeStudioWizardCommand.php b/src/Command/CodeStudio/CodeStudioWizardCommand.php index 2d83e22a2..152679d4f 100644 --- a/src/Command/CodeStudio/CodeStudioWizardCommand.php +++ b/src/Command/CodeStudio/CodeStudioWizardCommand.php @@ -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; @@ -163,6 +164,7 @@ private function createProjectAccessToken(array $project, string $projectAccessT $this->checklist->addItem("Creating access token named $projectAccessTokenName"); $projectAccessToken = $this->gitLabClient->projects() ->createProjectAccessToken($project['id'], [ + 'expires_at' => new DateTime('+365 days'), 'name' => $projectAccessTokenName, 'scopes' => ['api', 'write_repository'], ]); diff --git a/tests/phpunit/src/Commands/Acsf/AcsfApiCommandTest.php b/tests/phpunit/src/Commands/Acsf/AcsfApiCommandTest.php index a420087eb..a06237123 100644 --- a/tests/phpunit/src/Commands/Acsf/AcsfApiCommandTest.php +++ b/tests/phpunit/src/Commands/Acsf/AcsfApiCommandTest.php @@ -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)); diff --git a/tests/phpunit/src/Commands/CodeStudio/CodeStudioWizardCommandTest.php b/tests/phpunit/src/Commands/CodeStudio/CodeStudioWizardCommandTest.php index ca22288e2..dfc987653 100644 --- a/tests/phpunit/src/Commands/CodeStudio/CodeStudioWizardCommandTest.php +++ b/tests/phpunit/src/Commands/CodeStudio/CodeStudioWizardCommandTest.php @@ -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; @@ -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); @@ -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 a47ac10b-58cc-4372-a567-0e02b2c3d470', + '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 a47ac10b-58cc-4372-a567-0e02b2c3d470', + '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); @@ -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()); } @@ -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, @@ -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); @@ -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(); } } diff --git a/tests/phpunit/src/TestBase.php b/tests/phpunit/src/TestBase.php index 0f0e36ff4..a9329ba0f 100644 --- a/tests/phpunit/src/TestBase.php +++ b/tests/phpunit/src/TestBase.php @@ -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));