From 515b4a0bbfb417d5b197762e19cde28260be35a1 Mon Sep 17 00:00:00 2001 From: Akash Karangale Date: Mon, 30 Oct 2023 16:38:38 +0530 Subject: [PATCH 1/5] GL-1723: Added expires_at mandatory arg for codestudio token. --- src/Command/CodeStudio/CodeStudioWizardCommand.php | 5 +++++ .../src/Commands/CodeStudio/CodeStudioWizardCommandTest.php | 6 +++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Command/CodeStudio/CodeStudioWizardCommand.php b/src/Command/CodeStudio/CodeStudioWizardCommand.php index 2d83e22a2..8ea2aa3f0 100644 --- a/src/Command/CodeStudio/CodeStudioWizardCommand.php +++ b/src/Command/CodeStudio/CodeStudioWizardCommand.php @@ -161,8 +161,13 @@ private function createProjectAccessToken(array $project, string $projectAccessT $this->checklist->completePreviousItem(); } $this->checklist->addItem("Creating access token named $projectAccessTokenName"); + $d = new \DateTime('tomorrow'); + $formattedDate = $d->format('Y-m-d\TH:i:s\Z'); + $format = "Y-m-d\TH:i:s\Z"; + $dateTime = \DateTime::createFromFormat($format, $formattedDate); $projectAccessToken = $this->gitLabClient->projects() ->createProjectAccessToken($project['id'], [ + 'expires_at' => $dateTime, 'name' => $projectAccessTokenName, 'scopes' => ['api', 'write_repository'], ]); diff --git a/tests/phpunit/src/Commands/CodeStudio/CodeStudioWizardCommandTest.php b/tests/phpunit/src/Commands/CodeStudio/CodeStudioWizardCommandTest.php index ca22288e2..3b55a9f83 100644 --- a/tests/phpunit/src/Commands/CodeStudio/CodeStudioWizardCommandTest.php +++ b/tests/phpunit/src/Commands/CodeStudio/CodeStudioWizardCommandTest.php @@ -270,12 +270,16 @@ public function testMissingGitLabCredentials(): void { } protected function mockGitLabProjectsTokens(ObjectProphecy $projects): void { + $d = new \DateTime('tomorrow'); + $formattedDate = $d->format('Y-m-d\TH:i:s\Z'); + $format = "Y-m-d\TH:i:s\Z"; + $dateTime = \DateTime::createFromFormat($format, $formattedDate); $tokens = [ 0 => [ 'access_level' => 40, 'active' => TRUE, 'created_at' => '2021-12-28T20:08:21.629Z', - 'expires_at' => NULL, + 'expires_at' => $dateTime, 'id' => $this->gitLabTokenId, 'name' => 'acquia-codestudio', 'revoked' => FALSE, From 828d644b789f417a95048e331bddaa9b843b411d Mon Sep 17 00:00:00 2001 From: Akash Karangale Date: Tue, 31 Oct 2023 16:36:46 +0530 Subject: [PATCH 2/5] GL-1723: Optimised lines in code. --- src/Command/CodeStudio/CodeStudioWizardCommand.php | 5 +---- .../src/Commands/CodeStudio/CodeStudioWizardCommandTest.php | 5 +---- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/Command/CodeStudio/CodeStudioWizardCommand.php b/src/Command/CodeStudio/CodeStudioWizardCommand.php index 8ea2aa3f0..0d967204b 100644 --- a/src/Command/CodeStudio/CodeStudioWizardCommand.php +++ b/src/Command/CodeStudio/CodeStudioWizardCommand.php @@ -161,10 +161,7 @@ private function createProjectAccessToken(array $project, string $projectAccessT $this->checklist->completePreviousItem(); } $this->checklist->addItem("Creating access token named $projectAccessTokenName"); - $d = new \DateTime('tomorrow'); - $formattedDate = $d->format('Y-m-d\TH:i:s\Z'); - $format = "Y-m-d\TH:i:s\Z"; - $dateTime = \DateTime::createFromFormat($format, $formattedDate); + $dateTime = new \DateTime('tomorrow'); $projectAccessToken = $this->gitLabClient->projects() ->createProjectAccessToken($project['id'], [ 'expires_at' => $dateTime, diff --git a/tests/phpunit/src/Commands/CodeStudio/CodeStudioWizardCommandTest.php b/tests/phpunit/src/Commands/CodeStudio/CodeStudioWizardCommandTest.php index 3b55a9f83..ede91ff72 100644 --- a/tests/phpunit/src/Commands/CodeStudio/CodeStudioWizardCommandTest.php +++ b/tests/phpunit/src/Commands/CodeStudio/CodeStudioWizardCommandTest.php @@ -270,10 +270,7 @@ public function testMissingGitLabCredentials(): void { } protected function mockGitLabProjectsTokens(ObjectProphecy $projects): void { - $d = new \DateTime('tomorrow'); - $formattedDate = $d->format('Y-m-d\TH:i:s\Z'); - $format = "Y-m-d\TH:i:s\Z"; - $dateTime = \DateTime::createFromFormat($format, $formattedDate); + $dateTime = new \DateTime('tomorrow'); $tokens = [ 0 => [ 'access_level' => 40, From 7036c9dcf5cd946fdbddf937e3180324efa5a850 Mon Sep 17 00:00:00 2001 From: Dane Powell Date: Tue, 31 Oct 2023 09:45:44 -0700 Subject: [PATCH 3/5] increase expiration --- src/Command/CodeStudio/CodeStudioWizardCommand.php | 4 ++-- .../src/Commands/CodeStudio/CodeStudioWizardCommandTest.php | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Command/CodeStudio/CodeStudioWizardCommand.php b/src/Command/CodeStudio/CodeStudioWizardCommand.php index 0d967204b..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; @@ -161,10 +162,9 @@ private function createProjectAccessToken(array $project, string $projectAccessT $this->checklist->completePreviousItem(); } $this->checklist->addItem("Creating access token named $projectAccessTokenName"); - $dateTime = new \DateTime('tomorrow'); $projectAccessToken = $this->gitLabClient->projects() ->createProjectAccessToken($project['id'], [ - 'expires_at' => $dateTime, + 'expires_at' => new DateTime('+365 days'), 'name' => $projectAccessTokenName, 'scopes' => ['api', 'write_repository'], ]); diff --git a/tests/phpunit/src/Commands/CodeStudio/CodeStudioWizardCommandTest.php b/tests/phpunit/src/Commands/CodeStudio/CodeStudioWizardCommandTest.php index ede91ff72..b75319466 100644 --- a/tests/phpunit/src/Commands/CodeStudio/CodeStudioWizardCommandTest.php +++ b/tests/phpunit/src/Commands/CodeStudio/CodeStudioWizardCommandTest.php @@ -9,6 +9,7 @@ use Acquia\Cli\Tests\Commands\Ide\IdeRequiredTestTrait; use Acquia\Cli\Tests\Commands\WizardTestBase; use Acquia\Cli\Tests\TestBase; +use DateTime; use Gitlab\Api\Groups; use Gitlab\Api\ProjectNamespaces; use Gitlab\Api\Schedules; @@ -270,13 +271,12 @@ public function testMissingGitLabCredentials(): void { } protected function mockGitLabProjectsTokens(ObjectProphecy $projects): void { - $dateTime = new \DateTime('tomorrow'); $tokens = [ 0 => [ 'access_level' => 40, 'active' => TRUE, 'created_at' => '2021-12-28T20:08:21.629Z', - 'expires_at' => $dateTime, + 'expires_at' => new DateTime('+365 days'), 'id' => $this->gitLabTokenId, 'name' => 'acquia-codestudio', 'revoked' => FALSE, @@ -287,7 +287,7 @@ protected function mockGitLabProjectsTokens(ObjectProphecy $projects): void { 'user_id' => 154, ], ]; - $projects->projectAccessTokens($this->gitLabProjectId)->willReturn($tokens); + $projects->projectAccessTokens($this->gitLabProjectId)->willReturn($tokens)->shouldBeCalled(); $projects->deleteProjectAccessToken($this->gitLabProjectId, $this->gitLabTokenId); $token = $tokens[0]; $token['token'] = 'token'; From 4857e84c33de3d8f943035e13fed438ba12a444f Mon Sep 17 00:00:00 2001 From: Dane Powell Date: Tue, 31 Oct 2023 10:14:20 -0700 Subject: [PATCH 4/5] Cleaning up tests --- .../CodeStudioWizardCommandTest.php | 35 ++++++++++++++----- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/tests/phpunit/src/Commands/CodeStudio/CodeStudioWizardCommandTest.php b/tests/phpunit/src/Commands/CodeStudio/CodeStudioWizardCommandTest.php index b75319466..73233dd5d 100644 --- a/tests/phpunit/src/Commands/CodeStudio/CodeStudioWizardCommandTest.php +++ b/tests/phpunit/src/Commands/CodeStudio/CodeStudioWizardCommandTest.php @@ -188,13 +188,9 @@ public function providerTestCommand(): array { /** * @dataProvider providerTestCommand - * @param $mockedGitlabProjects - * @param $args - * @param $inputs */ - public function testCommand(mixed $mockedGitlabProjects, mixed $inputs, mixed $args): void { + public function testCommand(array $mockedGitlabProjects, array $inputs, array $args): void { $environmentsResponse = $this->getMockEnvironmentsResponse(); - $selectedEnvironment = $environmentsResponse->_embedded->items[0]; $this->clientProphecy->request('get', "/applications/{$this::$applicationUuid}/environments")->willReturn($environmentsResponse->_embedded->items)->shouldBeCalled(); $this->mockRequest('getAccount'); $this->mockGitLabPermissionsRequest($this::$applicationUuid); @@ -205,15 +201,35 @@ 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); $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', + ]); $gitlabClient->schedules()->willReturn($schedules->reveal()); $gitlabClient->projects()->willReturn($projects); @@ -239,6 +255,7 @@ public function testCommand(mixed $mockedGitlabProjects, mixed $inputs, mixed $a $this->executeCommand($args, $inputs); // Assertions. + //$this->prophet->checkPredictions(); $this->assertEquals(0, $this->getStatusCode()); } From 01a88da4b74877492ee732e775b577b2a4139092 Mon Sep 17 00:00:00 2001 From: Dane Powell Date: Tue, 31 Oct 2023 12:13:49 -0700 Subject: [PATCH 5/5] fix tests --- .../src/Commands/Acsf/AcsfApiCommandTest.php | 2 +- .../CodeStudioWizardCommandTest.php | 26 ++++++++++++------- tests/phpunit/src/TestBase.php | 2 +- 3 files changed, 19 insertions(+), 11 deletions(-) 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 73233dd5d..dfc987653 100644 --- a/tests/phpunit/src/Commands/CodeStudio/CodeStudioWizardCommandTest.php +++ b/tests/phpunit/src/Commands/CodeStudio/CodeStudioWizardCommandTest.php @@ -9,6 +9,7 @@ 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; @@ -190,8 +191,7 @@ public function providerTestCommand(): array { * @dataProvider providerTestCommand */ public function testCommand(array $mockedGitlabProjects, array $inputs, array $args): void { - $environmentsResponse = $this->getMockEnvironmentsResponse(); - $this->clientProphecy->request('get', "/applications/{$this::$applicationUuid}/environments")->willReturn($environmentsResponse->_embedded->items)->shouldBeCalled(); + $this->clientServiceProphecy->setConnector(Argument::type(Connector::class))->shouldBeCalled(); $this->mockRequest('getAccount'); $this->mockGitLabPermissionsRequest($this::$applicationUuid); @@ -214,7 +214,11 @@ public function testCommand(array $mockedGitlabProjects, array $inputs, array $a 'description' => 'Source repository for Acquia Cloud Platform application a47ac10b-58cc-4372-a567-0e02b2c3d470', 'topics' => 'Acquia Cloud Application', ]; - $projects->update($this->gitLabProjectId, $parameters); + $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([]); @@ -229,7 +233,11 @@ public function testCommand(array $mockedGitlabProjects, array $inputs, array $a $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); @@ -248,14 +256,13 @@ public function testCommand(array $mockedGitlabProjects, array $inputs, array $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->prophet->checkPredictions(); $this->assertEquals(0, $this->getStatusCode()); } @@ -305,7 +312,7 @@ protected function mockGitLabProjectsTokens(ObjectProphecy $projects): void { ], ]; $projects->projectAccessTokens($this->gitLabProjectId)->willReturn($tokens)->shouldBeCalled(); - $projects->deleteProjectAccessToken($this->gitLabProjectId, $this->gitLabTokenId); + $projects->deleteProjectAccessToken($this->gitLabProjectId, $this->gitLabTokenId)->shouldBeCalled(); $token = $tokens[0]; $token['token'] = 'token'; $projects->createProjectAccessToken($this->gitLabProjectId, Argument::type('array'))->willReturn($token); @@ -404,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));