Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tests-only][full-ci] add tests for checking the header location for tus upload #10807

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions tests/acceptance/TestHelpers/TusClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php declare(strict_types=1);
/**
* ownCloud
*
* @author Nabin Magar <[email protected]>
* @copyright Copyright (c) 2025 Nabin Magar [email protected]
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License,
* as published by the Free Software Foundation;
* either version 3 of the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace TestHelpers;

use Carbon\Carbon;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\GuzzleException;
use PHPUnit\Framework\Assert;
use TusPhp\Exception\ConnectionException;
use TusPhp\Exception\FileException;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\HttpFoundation\Response as HttpResponse;
use TusPhp\Exception\TusException;
use TusPhp\Tus\Client;

/**
* A TUS client based on TusPhp\Tus\Client
*/

class TusClient extends Client {

/**
* @param string $key
* @param int $bytes
*
* @return ResponseInterface
* @throws GuzzleException
*/
public function createUploadWithResponse(string $key, int $bytes = -1): ResponseInterface {
$bytes = $bytes < 0 ? $this->fileSize : $bytes;
$headers = $this->headers + [
'Upload-Length' => $this->fileSize,
'Upload-Key' => $key,
'Upload-Checksum' => $this->getUploadChecksumHeader(),
'Upload-Metadata' => $this->getUploadMetadataHeader(),
];
$data = '';
if ($bytes > 0) {
$data = $this->getData(0, $bytes);

$headers += [
'Content-Type' => self::HEADER_CONTENT_TYPE,
'Content-Length' => \strlen($data),
];
}
if ($this->isPartial()) {
$headers += ['Upload-Concat' => 'partial'];
}
try {
$response = $this->getClient()->post(
$this->apiPath,
[
'body' => $data,
'headers' => $headers,
]
);
} catch (ClientException $e) {
$response = $e->getResponse();
}
$statusCode = $response->getStatusCode();
if ($statusCode !== HttpResponse::HTTP_CREATED) {
return $response;
}
$uploadLocation = current($response->getHeader('location'));
$this->getCache()->set(
$this->getKey(),
[
'location' => $uploadLocation,
'expires_at' => Carbon::now()->addSeconds($this->getCache()->getTtl())->format($this->getCache()::RFC_7231),
]
);
return $response;
}

/**
* @param int $bytes
*
* @return ResponseInterface
* @throws GuzzleException
* @throws TusException | ConnectionException
*/
public function uploadWithResponse(int $bytes = -1): ResponseInterface {
$bytes = $bytes < 0 ? $this->getFileSize() : $bytes;
$offset = $this->partialOffset < 0 ? 0 : $this->partialOffset;
try {
// Check if this upload exists with HEAD request.
$offset = $this->sendHeadRequest();
} catch (FileException | ClientException $e) {
// Create a new upload.
$this->url = $this->createUploadWithResponse($this->getKey(), 0);
if ($this->url->getStatusCode() !== HttpResponse::HTTP_CREATED) {
return $this->url;
}
}
// Verify that upload is not yet expired.
if ($this->isExpired()) {
throw new TusException('Upload expired.');
}
$data = $this->getData($offset, $bytes);
$headers = $this->headers + [
'Content-Type' => self::HEADER_CONTENT_TYPE,
'Content-Length' => \strlen($data),
'Upload-Checksum' => $this->getUploadChecksumHeader(),
];
if ($this->isPartial()) {
$headers += ['Upload-Concat' => self::UPLOAD_TYPE_PARTIAL];
} else {
$headers += ['Upload-Offset' => $offset];
}
try {
$response = $this->getClient()->patch(
$this->getUrl(),
[
'body' => $data,
'headers' => $headers,
]
);
} catch (ClientException $e) {
throw $this->handleClientException($e);
}
return $response;
}
}
74 changes: 55 additions & 19 deletions tests/acceptance/bootstrap/SpacesTUSContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Behat\Gherkin\Node\TableNode;
use GuzzleHttp\Exception\GuzzleException;
use PHPUnit\Framework\Assert;
use Psr\Http\Message\ResponseInterface;
use TestHelpers\WebDavHelper;
use TestHelpers\BehatHelper;
use TestHelpers\GraphHelper;
Expand Down Expand Up @@ -68,8 +69,13 @@ public function userHasUploadedFileViaTusInSpace(
string $spaceName
): void {
$spaceId = $this->spacesContext->getSpaceIdByName($user, $spaceName);
$this->tusContext->uploadFileUsingTus($user, $source, $destination, $spaceId);
$response = $this->tusContext->uploadFileUsingTus($user, $source, $destination, $spaceId);
$this->featureContext->setLastUploadDeleteTime(\time());
$this->featureContext->theHTTPStatusCodeShouldBe(
["201", "204"],
"HTTP status code was not 201 or 204 while trying to upload file '$destination' for user '$user'",
$response
);
}

/**
Expand All @@ -91,8 +97,29 @@ public function userUploadsAFileViaTusInsideOfTheSpaceUsingTheWebdavApi(
string $spaceName
): void {
$spaceId = $this->spacesContext->getSpaceIdByName($user, $spaceName);
$this->tusContext->uploadFileUsingTus($user, $source, $destination, $spaceId);
$response = $this->tusContext->uploadFileUsingTus($user, $source, $destination, $spaceId);
$this->featureContext->setLastUploadDeleteTime(\time());
$this->featureContext->setResponse($response);
}

/**
* @When the public uploads file :source to :destination via TUS inside last link shared folder with password :password using the WebDAV API
*
* @param string $source
* @param string $destination
* @param string $password
*
* @return void
* @throws Exception|GuzzleException
*/
public function thePublicUploadsFileViaTusInsideLastSharedFolderWithPasswordUsingTheWebdavApi(
string $source,
string $destination,
string $password
): void {
$response = $this->tusContext->publicUploadFileUsingTus($source, $destination, $password);
$this->featureContext->setLastUploadDeleteTime(\time());
$this->featureContext->setResponse($response);
}

/**
Expand Down Expand Up @@ -149,24 +176,26 @@ public function userCreatesANewTusResourceForTheSpaceUsingTheWebdavApiWithTheseH
* @param string $resource
* @param string $spaceName
*
* @return void
* @return ResponseInterface
* @throws Exception|GuzzleException
*/
private function uploadFileViaTus(string $user, string $content, string $resource, string $spaceName): void {
private function uploadFileViaTus(
string $user,
string $content,
string $resource,
string $spaceName
): ResponseInterface {
$spaceId = $this->spacesContext->getSpaceIdByName($user, $spaceName);
$tmpFile = $this->tusContext->writeDataToTempFile($content);
try {
$this->tusContext->uploadFileUsingTus(
$user,
\basename($tmpFile),
$resource,
$spaceId
);
$this->featureContext->setLastUploadDeleteTime(\time());
} catch (Exception $e) {
Assert::assertStringContainsString('Unable to create resource', (string)$e);
}
$response = $this->tusContext->uploadFileUsingTus(
$user,
\basename($tmpFile),
$resource,
$spaceId
);
$this->featureContext->setLastUploadDeleteTime(\time());
\unlink($tmpFile);
return $response;
}

/**
Expand All @@ -189,14 +218,15 @@ public function userUploadsAFileWithContentToInsideFederatedShareViaTusUsingTheW
$remoteItemId = $this->spacesContext->getSharesRemoteItemId($user, $destination);
$remoteItemId = \rawurlencode($remoteItemId);
$tmpFile = $this->tusContext->writeDataToTempFile($content);
$this->tusContext->uploadFileUsingTus(
$response = $this->tusContext->uploadFileUsingTus(
$user,
\basename($tmpFile),
$file,
$remoteItemId
);
$this->featureContext->setLastUploadDeleteTime(\time());
\unlink($tmpFile);
$this->featureContext->setResponse($response);
}

/**
Expand All @@ -216,7 +246,7 @@ public function userUploadsAFileWithContentToViaTusInsideOfTheSpaceUsingTheWebda
string $resource,
string $spaceName
): void {
$this->uploadFileViaTus($user, $content, $resource, $spaceName);
$this->featureContext->setResponse($this->uploadFileViaTus($user, $content, $resource, $spaceName));
}

/**
Expand All @@ -236,7 +266,12 @@ public function userHasUploadedAFileWithContentToViaTusInsideOfTheSpace(
string $resource,
string $spaceName
): void {
$this->uploadFileViaTus($user, $content, $resource, $spaceName);
$response = $this->uploadFileViaTus($user, $content, $resource, $spaceName);
$this->featureContext->theHTTPStatusCodeShouldBe(
["201", "204"],
"HTTP status code was not 201 or 204 while trying to upload file '$resource' for user '$user'",
$response
);
}

/**
Expand Down Expand Up @@ -282,14 +317,15 @@ public function userUploadsAFileToWithMtimeViaTusInsideOfTheSpaceUsingTheWebdavA
$mtime = new DateTime($mtime);
$mtime = $mtime->format('U');
$user = $this->featureContext->getActualUsername($user);
$this->tusContext->uploadFileUsingTus(
$response = $this->tusContext->uploadFileUsingTus(
$user,
$source,
$destination,
$spaceId,
['mtime' => $mtime]
);
$this->featureContext->setLastUploadDeleteTime(\time());
$this->featureContext->setResponse($response);
}

/**
Expand Down
Loading