From 4475d1efc07fe5d438ebb5378fd2ed9533be9326 Mon Sep 17 00:00:00 2001 From: Prajwol Amatya Date: Tue, 8 Oct 2024 12:58:25 +0545 Subject: [PATCH] add test to restart upload sessions --- tests/acceptance/bootstrap/CliContext.php | 65 +++++++++++++++++-- .../cliCommands/uploadSessions.feature | 34 ++++++++-- 2 files changed, 87 insertions(+), 12 deletions(-) diff --git a/tests/acceptance/bootstrap/CliContext.php b/tests/acceptance/bootstrap/CliContext.php index 9e6869b0c5..a7e52a189e 100644 --- a/tests/acceptance/bootstrap/CliContext.php +++ b/tests/acceptance/bootstrap/CliContext.php @@ -27,6 +27,7 @@ use PHPUnit\Framework\Assert; use TestHelpers\CliHelper; use TestHelpers\OcisConfigHelper; use TestHelpers\BehatHelper; +use Psr\Http\Message\ResponseInterface; /** * CLI context @@ -268,6 +269,45 @@ class CliContext implements Context { $this->featureContext->setResponse(CliHelper::runCommand($body)); } + /** + * @When the administrator restarts the upload sessions that are in postprocessing + * + * @return void + */ + public function theAdministratorRestartsTheUploadSessionsThatAreInPostprocessing(): void { + $command = "storage-users uploads sessions --processing --restart --json"; + $body = [ + "command" => $command + ]; + $this->featureContext->setResponse(CliHelper::runCommand($body)); + } + + /** + * @When the administrator restarts the upload sessions of file :file + * + * @param string $file + * + * @return void + * @throws JsonException + */ + public function theAdministratorRestartsUploadSessionsOfFile(string $file): void { + $response = CliHelper::runCommand(["command" => "storage-users uploads sessions --json"]); + $this->featureContext->theHTTPStatusCodeShouldBe(200, '', $response); + $responseArray = $this->getJSONDecodedCliMessage($response); + + foreach ($responseArray as $item) { + if ($item->filename === $file) { + $uploadId = $item->id; + } + } + + $command = "storage-users uploads sessions --id=$uploadId --restart --json"; + $body = [ + "command" => $command + ]; + $this->featureContext->setResponse(CliHelper::runCommand($body)); + } + /** * @Then /^the CLI response (should|should not) contain these entries:$/ * @@ -275,16 +315,11 @@ class CliContext implements Context { * @param TableNode $table * * @return void + * @throws JsonException */ public function theCLIResponseShouldContainTheseEntries(string $shouldOrNot, TableNode $table): void { $expectedFiles = $table->getColumn(0); - $responseBody = $this->featureContext->getJsonDecodedResponse($this->featureContext->getResponse()); - - // $responseBody["message"] contains a message info with the array of output json of the upload sessions command - // Example Output: "INFO memory is not limited, skipping package=github.com/KimMachineGun/automemlimit/memlimit [{}]" - // So, only extracting the array of output json from the message - preg_match('/(\[.*\])/', $responseBody["message"], $matches); - $responseArray = \json_decode($matches[1], null, 512, JSON_THROW_ON_ERROR); + $responseArray = $this->getJSONDecodedCliMessage($this->featureContext->getResponse()); $resourceNames = []; foreach ($responseArray as $item) { @@ -310,6 +345,22 @@ class CliContext implements Context { } } + /** + * @param ResponseInterface $response + * + * @return array + * @throws JsonException + */ + public function getJSONDecodedCliMessage(ResponseInterface $response): array { + $responseBody = $this->featureContext->getJsonDecodedResponse($response); + + // $responseBody["message"] contains a message info with the array of output json of the upload sessions command + // Example Output: "INFO memory is not limited, skipping package=github.com/KimMachineGun/automemlimit/memlimit [{}]" + // So, only extracting the array of output json from the message + \preg_match('/(\[.*\])/', $responseBody["message"], $matches); + return \json_decode($matches[1], null, 512, JSON_THROW_ON_ERROR); + } + /** * @AfterScenario @cli-uploads-sessions * diff --git a/tests/acceptance/features/cliCommands/uploadSessions.feature b/tests/acceptance/features/cliCommands/uploadSessions.feature index b219ffdddc..241e2bf14a 100644 --- a/tests/acceptance/features/cliCommands/uploadSessions.feature +++ b/tests/acceptance/features/cliCommands/uploadSessions.feature @@ -8,7 +8,7 @@ Feature: List upload sessions via CLI command Given user "Alice" has been created with default attributes and without skeleton files - Scenario: user lists all upload sessions + Scenario: list all upload sessions Given user "Alice" has uploaded file with content "uploaded content" to "/file0.txt" And the config "POSTPROCESSING_DELAY" has been set to "10s" And user "Alice" has uploaded file with content "uploaded content" to "/file1.txt" @@ -22,7 +22,7 @@ Feature: List upload sessions via CLI command | file0.txt | - Scenario: user lists all upload sessions that are currently in postprocessing + Scenario: list all upload sessions that are currently in postprocessing Given the following configs have been set: | config | value | | POSTPROCESSING_STEPS | virusscan | @@ -40,7 +40,7 @@ Feature: List upload sessions via CLI command | virusFile.txt | - Scenario: user lists all upload sessions that are infected by virus + Scenario: list all upload sessions that are infected by virus Given the following configs have been set: | config | value | | POSTPROCESSING_STEPS | virusscan | @@ -55,7 +55,7 @@ Feature: List upload sessions via CLI command | file1.txt | - Scenario: user lists all expired upload sessions + Scenario: list all expired upload sessions Given the config "POSTPROCESSING_DELAY" has been set to "10s" And user "Alice" has uploaded file with content "uploaded content" to "/file1.txt" And the config "STORAGE_USERS_UPLOAD_EXPIRATION" has been set to "0" @@ -70,7 +70,7 @@ Feature: List upload sessions via CLI command | file1.txt | - Scenario: user cleans all expired upload sessions + Scenario: clean all expired upload sessions Given the config "POSTPROCESSING_DELAY" has been set to "10s" And user "Alice" has uploaded file with content "upload content" to "/file1.txt" And the config "STORAGE_USERS_UPLOAD_EXPIRATION" has been set to "0" @@ -83,3 +83,27 @@ Feature: List upload sessions via CLI command | file3.txt | And the CLI response should not contain these entries: | file1.txt | + + + Scenario: restart upload sessions that are in postprocessing + Given user "Alice" has uploaded file with content "upload content" to "/file1.txt" + And the config "POSTPROCESSING_DELAY" has been set to "10s" + And user "Alice" has uploaded file with content "upload content" to "/file2.txt" + When the administrator restarts the upload sessions that are in postprocessing + Then the command should be successful + And the CLI response should contain these entries: + | file2.txt | + And the CLI response should not contain these entries: + | file1.txt | + + + Scenario: restart upload sessions of a single file + Given the config "POSTPROCESSING_DELAY" has been set to "10s" + And user "Alice" has uploaded file with content "upload content" to "/file1.txt" + And user "Alice" has uploaded file with content "upload content" to "/file2.txt" + When the administrator restarts the upload sessions of file "file1.txt" + Then the command should be successful + And the CLI response should contain these entries: + | file1.txt | + And the CLI response should not contain these entries: + | file2.txt |