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 b56281f
Show file tree
Hide file tree
Showing 60 changed files with 169 additions and 34 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;
}
}
29 changes: 14 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,21 @@ 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 +234,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 +300,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
28 changes: 21 additions & 7 deletions tests/acceptance/features/apiSpacesShares/shareUploadTUS.feature
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ Feature: upload resources on share using TUS protocol
| permissionsRole | Editor |
And user "Brian" has a share "toShare" synced
When user "Brian" uploads a file "filesForUpload/textfile.txt" to "toShare/file.txt" with mtime "Thu, 08 Aug 2012 04:18:13 GMT" via TUS inside of the space "Shares" using the WebDAV API
Then for user "Brian" folder "toShare" of the space "Shares" should contain these entries:
Then the HTTP status code should be "204"
And for user "Brian" folder "toShare" of the space "Shares" should contain these entries:
| file.txt |
And as "Brian" the mtime of the file "/toShare/file.txt" in space "Shares" should be "Thu, 08 Aug 2012 04:18:13 GMT"
And as "Alice" the mtime of the file "/toShare/file.txt" in space "Personal" should be "Thu, 08 Aug 2012 04:18:13 GMT"
Expand All @@ -37,7 +38,8 @@ Feature: upload resources on share using TUS protocol
| permissionsRole | Editor |
And user "Brian" has a share "toShare" synced
When user "Alice" uploads a file "filesForUpload/textfile.txt" to "toShare/file.txt" with mtime "Thu, 08 Aug 2012 04:18:13 GMT" via TUS inside of the space "Personal" using the WebDAV API
Then for user "Alice" folder "toShare" of the space "Personal" should contain these entries:
Then the HTTP status code should be "204"
And for user "Alice" folder "toShare" of the space "Personal" should contain these entries:
| file.txt |
And as "Alice" the mtime of the file "/toShare/file.txt" in space "Personal" should be "Thu, 08 Aug 2012 04:18:13 GMT"
And as "Brian" the mtime of the file "/toShare/file.txt" in space "Shares" should be "Thu, 08 Aug 2012 04:18:13 GMT"
Expand All @@ -54,7 +56,8 @@ Feature: upload resources on share using TUS protocol
And user "Brian" has a share "toShare" synced
And user "Alice" has uploaded file with content "uploaded content" to "/toShare/file.txt"
When user "Brian" uploads a file "filesForUpload/textfile.txt" to "toShare/file.txt" with mtime "Thu, 08 Aug 2012 04:18:13 GMT" via TUS inside of the space "Shares" using the WebDAV API
Then for user "Brian" folder "toShare" of the space "Shares" should contain these entries:
Then the HTTP status code should be "204"
And for user "Brian" folder "toShare" of the space "Shares" should contain these entries:
| file.txt |
And as "Brian" the mtime of the file "/toShare/file.txt" in space "Shares" should be "Thu, 08 Aug 2012 04:18:13 GMT"
And as "Alice" the mtime of the file "/toShare/file.txt" in space "Personal" should be "Thu, 08 Aug 2012 04:18:13 GMT"
Expand All @@ -71,7 +74,8 @@ Feature: upload resources on share using TUS protocol
And user "Brian" has a share "toShare" synced
And user "Brian" has uploaded a file inside space "Shares" with content "uploaded content" to "toShare/file.txt"
When user "Alice" uploads a file "filesForUpload/textfile.txt" to "toShare/file.txt" with mtime "Thu, 08 Aug 2012 04:18:13 GMT" via TUS inside of the space "Personal" using the WebDAV API
Then for user "Alice" folder "toShare" of the space "Personal" should contain these entries:
Then the HTTP status code should be "204"
And for user "Alice" folder "toShare" of the space "Personal" should contain these entries:
| file.txt |
And as "Alice" the mtime of the file "/toShare/file.txt" in space "Personal" should be "Thu, 08 Aug 2012 04:18:13 GMT"
And as "Brian" the mtime of the file "/toShare/file.txt" in space "Shares" should be "Thu, 08 Aug 2012 04:18:13 GMT"
Expand All @@ -88,6 +92,7 @@ Feature: upload resources on share using TUS protocol
| permissionsRole | Editor |
And user "Brian" has a share "toShare" synced
When user "Brian" uploads a file with content "uploaded content" to "/toShare/nonExistentFolder/file.txt" via TUS inside of the space "Shares" using the WebDAV API
Then the HTTP status code should be "412"
Then for user "Brian" folder "toShare" of the space "Shares" should not contain these entries:
| nonExistentFolder |

Expand All @@ -103,6 +108,7 @@ Feature: upload resources on share using TUS protocol
| permissionsRole | Viewer |
And user "Brian" has a share "toShare" synced
When user "Brian" uploads a file with content "uploaded content" to "/toShare/nonExistentFolder/file.txt" via TUS inside of the space "Shares" using the WebDAV API
Then the HTTP status code should be "403"
Then for user "Brian" folder "toShare" of the space "Shares" should not contain these entries:
| nonExistentFolder |

Expand All @@ -117,6 +123,7 @@ Feature: upload resources on share using TUS protocol
| permissionsRole | Editor |
And user "Brian" has a share "toShare" synced
When user "Brian" uploads a file with content "uploaded content" to "/toShare/file.txt" via TUS inside of the space "Shares" using the WebDAV API
Then the HTTP status code should be "204"
Then for user "Alice" folder "toShare" of the space "Personal" should contain these entries:
| file.txt |
And for user "Alice" the content of the file "toShare/file.txt" of the space "Personal" should be "uploaded content"
Expand All @@ -132,6 +139,7 @@ Feature: upload resources on share using TUS protocol
| permissionsRole | Uploader |
And user "Brian" has a share "toShare" synced
When user "Brian" uploads a file with content "uploaded content" to "/toShare/file.txt" via TUS inside of the space "Shares" using the WebDAV API
Then the HTTP status code should be "204"
Then for user "Alice" folder "toShare" of the space "Personal" should contain these entries:
| file.txt |
And for user "Alice" the content of the file "toShare/file.txt" of the space "Personal" should be "uploaded content"
Expand All @@ -149,6 +157,7 @@ Feature: upload resources on share using TUS protocol
| permissionsRole | Uploader |
And user "Brian" has a share "toShare" synced
When user "Brian" uploads a file with content "uploaded content" to "/toShare/file.txt" via TUS inside of the space "Shares" using the WebDAV API
Then the HTTP status code should be "204"
Then for user "Alice" folder "toShare" of the space "Personal" should contain these entries:
| file.txt |
And for user "Alice" the content of the file "toShare/file.txt" of the space "Personal" should be "uploaded content"
Expand All @@ -165,6 +174,7 @@ Feature: upload resources on share using TUS protocol
| permissionsRole | Editor |
And user "Brian" has a share "toShare" synced
When user "Brian" uploads a file with content "overwritten content" to "/toShare/file.txt" via TUS inside of the space "Shares" using the WebDAV API
Then the HTTP status code should be "204"
Then for user "Alice" folder "toShare" of the space "Personal" should contain these entries:
| file.txt |
And for user "Alice" the content of the file "toShare/file.txt" of the space "Personal" should be "overwritten content"
Expand All @@ -180,6 +190,7 @@ Feature: upload resources on share using TUS protocol
| permissionsRole | Viewer |
And user "Brian" has a share "toShare" synced
When user "Brian" uploads a file with content "uploaded content" to "/toShare/file.txt" via TUS inside of the space "Shares" using the WebDAV API
Then the HTTP status code should be "403"
Then for user "Brian" folder "toShare" of the space "Shares" should not contain these entries:
| file.txt |

Expand Down Expand Up @@ -220,6 +231,7 @@ Feature: upload resources on share using TUS protocol
| Tus-Resumable | 1.0.0 |
And user "Alice" has uploaded file with checksum "SHA1 8cb2237d0679ca88db6464eac60da96345513964" to the last created TUS Location with offset "0" and content "12345" via TUS inside of the space "Personal" using the WebDAV API
When user "Brian" downloads the file "/FOLDER/textFile.txt" of the space "Shares" using the WebDAV API
Then the HTTP status code should be "200"
Then the header checksum should match "SHA1:8cb2237d0679ca88db6464eac60da96345513964"


Expand Down Expand Up @@ -257,7 +269,8 @@ Feature: upload resources on share using TUS protocol
| permissionsRole | File Editor |
And user "Brian" has a share "textFile.txt" synced
When user "Brian" downloads the file "/textFile.txt" of the space "Shares" using the WebDAV API
Then the header checksum should match "SHA1:8cb2237d0679ca88db6464eac60da96345513964"
Then the HTTP status code should be "200"
And the header checksum should match "SHA1:8cb2237d0679ca88db6464eac60da96345513964"


Scenario: sharee uploads a file to a received share folder with correct checksum
Expand All @@ -269,13 +282,14 @@ Feature: upload resources on share using TUS protocol
| shareType | user |
| permissionsRole | Editor |
And user "Brian" has a share "FOLDER" synced
When user "Brian" creates a new TUS resource for the space "Shares" with content "" using the WebDAV API with these headers:
When user "Brian" creates a new TUS resource for the space "Shares" with content " " using the WebDAV API with these headers:
| Upload-Length | 5 |
# L0ZPTERFUi90ZXh0RmlsZS50eHQ= is the base64 encode of /FOLDER/textFile.txt
| Upload-Metadata | filename L0ZPTERFUi90ZXh0RmlsZS50eHQ= |
| Tus-Resumable | 1.0.0 |
And user "Brian" uploads file with checksum "MD5 827ccb0eea8a706c4c34a16891f84e7b" to the last created TUS Location with offset "0" and content "12345" via TUS inside of the space "Shares" using the WebDAV API
Then for user "Alice" folder "FOLDER" of the space "Personal" should contain these entries:
Then the HTTP status code should be "204"
And for user "Alice" folder "FOLDER" of the space "Personal" should contain these entries:
| textFile.txt |
And for user "Alice" the content of the file "FOLDER/textFile.txt" of the space "Personal" should be "12345"

Expand Down
1 change: 1 addition & 0 deletions tests/acceptance/tus-upload-test-5Ll3nG
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uploaded content
1 change: 1 addition & 0 deletions tests/acceptance/tus-upload-test-5M1ZBT
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uploaded content
1 change: 1 addition & 0 deletions tests/acceptance/tus-upload-test-7MmEvv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uploaded content
1 change: 1 addition & 0 deletions tests/acceptance/tus-upload-test-92BkXL
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uploaded content
1 change: 1 addition & 0 deletions tests/acceptance/tus-upload-test-9fk74n
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uploaded content
1 change: 1 addition & 0 deletions tests/acceptance/tus-upload-test-ApKNOM
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uploaded content
1 change: 1 addition & 0 deletions tests/acceptance/tus-upload-test-C1eId7
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uploaded content
1 change: 1 addition & 0 deletions tests/acceptance/tus-upload-test-CCo8QS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uploaded content
1 change: 1 addition & 0 deletions tests/acceptance/tus-upload-test-CWUPBi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uploaded content
1 change: 1 addition & 0 deletions tests/acceptance/tus-upload-test-FDcq1F
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uploaded content
1 change: 1 addition & 0 deletions tests/acceptance/tus-upload-test-FQnFRU
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uploaded content
1 change: 1 addition & 0 deletions tests/acceptance/tus-upload-test-Fm5rjb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uploaded content
1 change: 1 addition & 0 deletions tests/acceptance/tus-upload-test-G37XNL
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uploaded content
1 change: 1 addition & 0 deletions tests/acceptance/tus-upload-test-Gsy1jC
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uploaded content
1 change: 1 addition & 0 deletions tests/acceptance/tus-upload-test-J3O22k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uploaded content
1 change: 1 addition & 0 deletions tests/acceptance/tus-upload-test-Jfnf8I
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uploaded content
Loading

0 comments on commit b56281f

Please sign in to comment.