Skip to content

Commit

Permalink
adds step for checking the status code of tus upload
Browse files Browse the repository at this point in the history
Signed-off-by: nabim777 <[email protected]>
  • Loading branch information
nabim777 committed Jan 6, 2025
1 parent fa52f68 commit 444ddb3
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 48 deletions.
65 changes: 64 additions & 1 deletion tests/acceptance/TestHelpers/TusClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@
namespace TestHelpers;

use Carbon\Carbon;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
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;

/**
Expand Down Expand Up @@ -80,7 +85,7 @@ public function createUploadWithResponse(string $key, int $bytes = -1): Response
$statusCode = $response->getStatusCode();

if ($statusCode !== HttpResponse::HTTP_CREATED) {
throw new FileException('Unable to create resource.');
return $response;
}

$uploadLocation = current($response->getHeader('location'));
Expand All @@ -94,4 +99,62 @@ public function createUploadWithResponse(string $key, int $bytes = -1): Response
);
return $response;
}

/**
* @param int $bytes
*
* @return ResponseInterface
* @throws GuzzleException
* @throws ConnectionException
* @throws TusException
*/
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;
}
} catch (ConnectException) {
throw new ConnectionException("Couldn't connect to server.");
}

// 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);
} catch (ConnectException) {
throw new ConnectionException("Couldn't connect to server.");
}
return $response;
}
}
34 changes: 19 additions & 15 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 @@ -169,24 +170,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 Down Expand Up @@ -236,7 +239,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 Down Expand Up @@ -302,14 +305,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
25 changes: 14 additions & 11 deletions tests/acceptance/bootstrap/TUSContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public function userUploadsUsingTusAFileTo(
* @param integer $bytes
* @param string $checksum
*
* @return void
* @return ResponseInterface
*/
public function uploadFileUsingTus(
?string $user,
Expand All @@ -290,7 +290,7 @@ public function uploadFileUsingTus(
int $noOfChunks = 1,
int $bytes = null,
string $checksum = ''
) {
): ResponseInterface {
$user = $this->featureContext->getActualUsername($user);
$password = $this->featureContext->getUserPassword($user);
$headers = [
Expand All @@ -310,7 +310,7 @@ public function uploadFileUsingTus(
$headers = \array_merge($headers, $checksumHeader);
}

$client = new Client(
$tusClient = new TusClient(
$this->featureContext->getBaseUrl(),
[
'verify' => false,
Expand All @@ -324,24 +324,27 @@ public function uploadFileUsingTus(
$suffixPath = $spaceId ?: $this->featureContext->getPersonalSpaceIdForUser($user);
}

$client->setChecksumAlgorithm('sha1');
$client->setApiPath(WebDavHelper::getDavPath($davPathVersion, $suffixPath));
$client->setMetadata($uploadMetadata);
$tusClient->setChecksumAlgorithm('sha1');
$tusClient->setApiPath(WebDavHelper::getDavPath($davPathVersion, $suffixPath));
$tusClient->setMetadata($uploadMetadata);
$sourceFile = $this->featureContext->acceptanceTestsDirLocation() . $source;
$client->setKey((string)rand())->file($sourceFile, $destination);
$tusClient->setKey((string)rand())->file($sourceFile, $destination);
$this->featureContext->pauseUploadDelete();
$response = null;

if ($bytes !== null) {
$client->file($sourceFile, $destination)->createWithUpload($client->getKey(), $bytes);
return $tusClient->file($sourceFile, $destination)
->createUploadWithResponse($tusClient->getKey(), $bytes);
} elseif (\filesize($sourceFile) === 0) {
$client->file($sourceFile, $destination)->createWithUpload($client->getKey(), 0);
return $tusClient->file($sourceFile, $destination)->createUploadWithResponse($tusClient->getKey(), 0);
} elseif ($noOfChunks === 1) {
$client->file($sourceFile, $destination)->upload();
return $tusClient->file($sourceFile, $destination)->uploadWithResponse();
} else {
$bytesPerChunk = (int)\ceil(\filesize($sourceFile) / $noOfChunks);
for ($i = 0; $i < $noOfChunks; $i++) {
$client->upload($bytesPerChunk);
$response = $tusClient->uploadWithResponse($bytesPerChunk);
}
return $response;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ The expected failures in this file are from features in the owncloud/ocis repo.

#### [PATCH request for TUS upload with wrong checksum gives incorrect response](https://github.com/owncloud/ocis/issues/1755)

- [apiSpacesShares/shareUploadTUS.feature:283](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiSpacesShares/shareUploadTUS.feature#L283)
- [apiSpacesShares/shareUploadTUS.feature:303](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiSpacesShares/shareUploadTUS.feature#L303)
- [apiSpacesShares/shareUploadTUS.feature:384](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiSpacesShares/shareUploadTUS.feature#L384)
- [apiSpacesShares/shareUploadTUS.feature:297](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiSpacesShares/shareUploadTUS.feature#L297)
- [apiSpacesShares/shareUploadTUS.feature:317](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiSpacesShares/shareUploadTUS.feature#L317)
- [apiSpacesShares/shareUploadTUS.feature:398](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiSpacesShares/shareUploadTUS.feature#L398)

#### [Settings service user can list other peoples assignments](https://github.com/owncloud/ocis/issues/5032)

Expand Down
6 changes: 3 additions & 3 deletions tests/acceptance/expected-failures-without-remotephp.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@
- [apiSpaces/uploadSpaces.feature:115](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiSpaces/uploadSpaces.feature#L115)
- [apiSpaces/uploadSpaces.feature:132](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiSpaces/uploadSpaces.feature#L132)
- [apiSpacesShares/shareSpacesViaLink.feature:61](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiSpacesShares/shareSpacesViaLink.feature#L61)
- [apiSpacesShares/shareUploadTUS.feature:407](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiSpacesShares/shareUploadTUS.feature#L407)
- [apiSpacesShares/shareUploadTUS.feature:424](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiSpacesShares/shareUploadTUS.feature#L424)
- [apiSpacesShares/shareUploadTUS.feature:443](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiSpacesShares/shareUploadTUS.feature#L443)
- [apiSpacesShares/shareUploadTUS.feature:421](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiSpacesShares/shareUploadTUS.feature#L421)
- [apiSpacesShares/shareUploadTUS.feature:438](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiSpacesShares/shareUploadTUS.feature#L438)
- [apiSpacesShares/shareUploadTUS.feature:457](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiSpacesShares/shareUploadTUS.feature#L457)
- [apiDepthInfinity/propfind.feature:74](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiDepthInfinity/propfind.feature#L74)
- [apiDepthInfinity/propfind.feature:124](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiDepthInfinity/propfind.feature#L124)
- [apiLocks/lockFiles.feature:490](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiLocks/lockFiles.feature#L490)
Expand Down
Loading

0 comments on commit 444ddb3

Please sign in to comment.