test: refactor upload expired cli test scenario

Signed-off-by: Saw-jan <saw.jan.grg3e@gmail.com>
This commit is contained in:
Saw-jan committed 2025-06-19 10:31:27 +05:45
1 parent 9fc043abc5
commit b64384a205
6 files changed
+114 -29

No files matched your search

+39 -4
View File
@@ -150,15 +150,44 @@ class UploadHelper extends Assert {
return $chunks;
}
/**
* @param string $sizeString (e.g. "1GB", "500MB", "100KB", "200B", "1000")
*
* @return int
* @throws \InvalidArgumentException
*/
public static function convertToBytes(string $sizeString): int {
$sizeString = \strtoupper(\trim($sizeString));
$sizeUnit = \preg_replace('/\d+/', '', $sizeString);
$size = \intval($sizeString);
switch ($sizeUnit) {
case 'GB':
return 1024 ** 3 * $size;
case 'MB':
return 1024 ** 2 * $size;
case 'KB':
return 1024 * $size;
case 'B':
case '':
return $size;
default:
throw new \InvalidArgumentException(
"Invalid size unit '$sizeUnit' in '$sizeString'. Use GB, MB, KB or no unit for bytes."
);
}
}
/**
* creates a File with a specific size
*
* @param string|null $name full path of the file to create
* @param int|null $size
* @param string $name full path of the file to create
* @param string $size
*
* @return void
*/
public static function createFileSpecificSize(?string $name, ?int $size): void {
public static function createFileSpecificSize(string $name, string $size): void {
$size = self::convertToBytes($size);
if (\file_exists($name)) {
\unlink($name);
}
@@ -204,6 +233,12 @@ class UploadHelper extends Assert {
* @return string
*/
public static function getUploadFilesDir(?string $name): string {
return \getenv("FILES_FOR_UPLOAD") . $name;
$envPath = \getenv("FILES_FOR_UPLOAD");
$envPath = \rtrim($envPath, "/");
$name = \trim($name, "/");
if ($envPath) {
return "$envPath/$name";
}
return \dirname(__FILE__) . "/../filesForUpload/$name";
}
}
@@ -1681,10 +1681,13 @@ class FeatureContext extends BehatVariablesContext {
/**
* @Then the HTTP status code should be failure
*
* @param ResponseInterface|null $response
*
* @return void
*/
public function theHTTPStatusCodeShouldBeFailure(): void {
$statusCode = $this->response->getStatusCode();
public function theHTTPStatusCodeShouldBeFailure(?ResponseInterface $response): void {
$response = $response ?? $this->response;
$statusCode = $response->getStatusCode();
$message = "The HTTP status code $statusCode is not greater than or equals to 400";
Assert::assertGreaterThanOrEqual(
400,
@@ -13,12 +13,11 @@ use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Gherkin\Node\TableNode;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\ClientException;
use PHPUnit\Framework\Assert;
use Psr\Http\Message\ResponseInterface;
use TestHelpers\WebDavHelper;
use TestHelpers\BehatHelper;
use TestHelpers\GraphHelper;
use TestHelpers\HttpRequestHelper;
require_once 'bootstrap.php';
@@ -78,6 +77,45 @@ class SpacesTUSContext implements Context {
);
}
/**
* @Given /^user "([^"]*)" has tried to upload file "([^"]*)" to "([^"]*)" inside space "([^"]*)" via TUS$/
*
* @param string $user
* @param string $source
* @param string $destination
* @param string $spaceName
*
* @return void
*
* @throws Exception
* @throws GuzzleException
*/
public function userHasTriedToUploadFileInsideSpaceViaTUS(
string $user,
string $source,
string $destination,
string $spaceName
): void {
$spaceId = $this->spacesContext->getSpaceIdByName($user, $spaceName);
try {
$response = $this->tusContext->uploadFileUsingTus(
$user,
$source,
$destination,
$spaceId,
);
Assert::fail(
__METHOD__ .
" - Expected an exception, but the upload was successful. Status: " .
$response->getStatusCode()
);
} catch (ClientException $e) {
if (!\str_contains($e->getMessage(), "403 Forbidden")) {
Assert::fail(__METHOD__ . " - " . $e->getMessage());
}
}
}
/**
* @When /^user "([^"]*)" uploads a file from "([^"]*)" to "([^"]*)" via TUS inside of the space "([^"]*)" using the WebDAV API$/
*
+20
View File
@@ -4838,4 +4838,24 @@ trait WebDav {
$response
);
}
/**
* @Given a file :name with the size of :size has been created locally
*
* @param string $name
* @param string $size (e.g.: 10MB, 100KB, 1GB)
*
* @return void
* @throws InvalidArgumentException
*/
public function aFileWithSizeHasBeenCreatedLocally(string $name, string $size): void {
$fullPath = UploadHelper::getUploadFilesDir($name);
if (\file_exists($fullPath)) {
throw new InvalidArgumentException(
__METHOD__ . " could not create '$fullPath'. File already exists."
);
}
UploadHelper::createFileSpecificSize($fullPath, $size);
$this->createdFiles[] = $fullPath;
}
}
@@ -395,7 +395,7 @@ The expected failures in this file are from features in the owncloud/ocis repo.
#### [[CLI] Error while resuming an upload](https://github.com/owncloud/ocis/issues/11290)
- [cliCommands/uploadSessions.feature:173](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/cliCommands/uploadSessions.feature#L173)
- [cliCommands/uploadSessions.feature:162](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/cliCommands/uploadSessions.feature#L162)
Note: always have an empty line at the end of this file.
The bash script that processes this file requires that the last line has a newline on the end.
@@ -56,35 +56,24 @@ Feature: List upload sessions via CLI command
| file1.txt |
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"
And user "Alice" has uploaded file with content "uploaded content" to "/file2.txt"
And user "Alice" has uploaded file with content "uploaded content" to "/file3.txt"
Scenario: list and cleanup the expired upload sessions
Given a file "large.zip" with the size of "2GB" has been created locally
And the config "STORAGE_USERS_UPLOAD_EXPIRATION" has been set to "1"
And user "Alice" has uploaded a file from "filesForUpload/textfile.txt" to "file.txt" via TUS inside of the space "Personal" using the WebDAV API
And user "Alice" has tried to upload file "filesForUpload/large.zip" to "large.zip" inside space "Personal" via TUS
When the administrator lists all the upload sessions with flag "expired"
Then the command should be successful
And the CLI response should contain these entries:
| file2.txt |
| file3.txt |
| large.zip |
And the CLI response should not contain these entries:
| file1.txt |
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"
And user "Alice" has uploaded file with content "upload content" to "/file2.txt"
And user "Alice" has uploaded file with content "upload content" to "/file3.txt"
| file.txt |
When the administrator cleans upload sessions with the following flags:
| expired |
Then the command should be successful
And the CLI response should contain these entries:
| file2.txt |
| file3.txt |
| large.zip |
And the CLI response should not contain these entries:
| file1.txt |
| file.txt |
Scenario: restart upload sessions that are in postprocessing