From 8466d85e35fd6bfbdc370c406b636294a3d3e4ac Mon Sep 17 00:00:00 2001 From: prashant-gurung899 Date: Thu, 24 Oct 2024 12:30:50 +0545 Subject: [PATCH] add test to upload via TUS by federated user Signed-off-by: prashant-gurung899 --- tests/acceptance/bootstrap/SpacesContext.php | 82 ++++++++++++++++++- .../acceptance/bootstrap/SpacesTUSContext.php | 37 +++++++-- tests/acceptance/bootstrap/TUSContext.php | 16 ++-- tests/acceptance/config/behat.yml | 1 + .../acceptance/features/apiCors/cors.feature | 10 +-- .../acceptance/features/apiOcm/share.feature | 21 +++++ .../apiSpacesShares/shareUploadTUS.feature | 18 ++-- .../cliCommands/uploadSessions.feature | 2 +- 8 files changed, 151 insertions(+), 36 deletions(-) diff --git a/tests/acceptance/bootstrap/SpacesContext.php b/tests/acceptance/bootstrap/SpacesContext.php index ecb6df0d441..154923e3480 100644 --- a/tests/acceptance/bootstrap/SpacesContext.php +++ b/tests/acceptance/bootstrap/SpacesContext.php @@ -254,6 +254,42 @@ public function getSpaceIdByName(string $user, string $spaceName): string { return $space["id"]; } + /** + * Retrieves the remoteItem ID for a given share in the Shares space. + * + * @param string $user + * @param string $share //share + * + * @return string + * + * @throws Exception|GuzzleException + */ + public function getSharesRemoteItemId(string $user, string $share): string { + $credentials = $this->featureContext->graphContext->getAdminOrUserCredentials($user); + $response = GraphHelper::getSharesSharedWithMe( + $this->featureContext->getBaseUrl(), + $this->featureContext->getStepLineRef(), + $credentials['username'], + $credentials['password'] + ); + + $jsonBody = $this->featureContext->getJsonDecodedResponseBodyContent($response); + + // Search for the folder name in the response to find the remoteItem ID + foreach ($jsonBody->value as $item) { + if (isset($item->name) && $item->name === $share) { + if (isset($item->remoteItem->id)) { + return $item->remoteItem->id; + } else { + throw new Exception("Failed to find remoteItem ID for share: $share."); + }//throw-- remote id id not found + } + } + + //no share + throw new Exception("Failed to find remoteItem ID for folder: $share in Shares space."); + } + /** * The method finds file by fileName and spaceName and returns data of file which contains in responseHeader * fileName contains the path, if the file is in the folder @@ -261,15 +297,24 @@ public function getSpaceIdByName(string $user, string $spaceName): string { * @param string $user * @param string $spaceName * @param string $fileName + * @param string|null $share * * @return ResponseInterface * @throws GuzzleException */ - public function getFileData(string $user, string $spaceName, string $fileName): ResponseInterface { - $space = $this->getSpaceByName($user, $spaceName); + public function getFileData(string $user, string $spaceName, string $fileName, ?string $share= null): ResponseInterface { $baseUrl = $this->featureContext->getBaseUrl(); - $davPath = WebdavHelper::getDavPath(WebDavHelper::DAV_VERSION_SPACES, $space["id"]); - $fullUrl = "$baseUrl/$davPath/$fileName"; + + if ($spaceName === 'Shares') { + $remoteItemId = $this->getSharesRemoteItemId($user, $share); + $remoteItemId = \rawurlencode($remoteItemId); + $davPath = WebdavHelper::getDavPath(WebDavHelper::DAV_VERSION_SPACES, $remoteItemId); + $fullUrl = "$baseUrl/$davPath/$fileName"; + } else { + $space = $this->getSpaceByName($user, $spaceName); + $davPath = WebdavHelper::getDavPath(WebDavHelper::DAV_VERSION_SPACES, $space["id"]); + $fullUrl = "$baseUrl/$davPath/$fileName"; + } return HttpRequestHelper::get( $fullUrl, @@ -874,6 +919,35 @@ public function checkFileContent( Assert::assertEquals($fileContent, $actualFileContent, "$file does not contain $fileContent"); } + /** + * @Then /^for user "([^"]*)" the content of the federated share file "([^"]*)" of space "([^"]*)" should be "([^"]*)"$/ + * + * @param string $user + * @param string $file + * @param string $space + * @param string $fileContent + * + * @return void + * + * @throws Exception|GuzzleException + */ + public function checkFileContentOfRemoteShare( + string $user, + string $file, + string $space, + string $fileContent + ): void { + // trimming the resource 'FOLDER/file.txt' to get the destination path 'file.txt' + // 'FOLDER' is represented by remoteItem id in the federation + $resource = \ltrim($file, "/"); + $resources = \explode("/", $resource); + $shareFolderName = array_shift($resources); + $file = array_pop($resources); + + $actualFileContent = $this->getFileData($user, $space, $file, $shareFolderName)->getBody()->getContents(); + Assert::assertEquals($fileContent, $actualFileContent, "$file does not contain $fileContent"); + } + /** * @Then /^the JSON response should contain space called "([^"]*)" (?:|(?:owned by|granted to) "([^"]*)" )(?:|(?:with description file|with space image) "([^"]*)" )and match$/ * diff --git a/tests/acceptance/bootstrap/SpacesTUSContext.php b/tests/acceptance/bootstrap/SpacesTUSContext.php index 283c383c569..7a6ed162125 100644 --- a/tests/acceptance/bootstrap/SpacesTUSContext.php +++ b/tests/acceptance/bootstrap/SpacesTUSContext.php @@ -16,6 +16,8 @@ use PHPUnit\Framework\Assert; use TestHelpers\WebDavHelper; use TestHelpers\BehatHelper; +use TestHelpers\GraphHelper; +use TestHelpers\HttpRequestHelper; require_once 'bootstrap.php'; @@ -89,11 +91,10 @@ public function userUploadsAFileViaTusInsideOfTheSpaceUsingTheWebdavApi( } /** - * @Given user :user has created a new TUS resource for the space :spaceName with content :content using the WebDAV API with these headers: + * @Given user :user has created a new TUS resource in the space :spaceName with the following headers: * * @param string $user * @param string $spaceName - * @param string $content * @param TableNode $headers * * @return void @@ -104,11 +105,10 @@ public function userUploadsAFileViaTusInsideOfTheSpaceUsingTheWebdavApi( public function userHasCreatedANewTusResourceForTheSpaceUsingTheWebdavApiWithTheseHeaders( string $user, string $spaceName, - string $content, TableNode $headers ): void { $spaceId = $this->spacesContext->getSpaceIdByName($user, $spaceName); - $response = $this->tusContext->createNewTUSResourceWithHeaders($user, $headers, $content, $spaceId); + $response = $this->tusContext->createNewTUSResourceWithHeaders($user, $headers, '', $spaceId); $this->featureContext->theHTTPStatusCodeShouldBe(201, "Expected response status code should be 201", $response); } @@ -164,6 +164,31 @@ private function uploadFileViaTus(string $user, string $content, string $resourc \unlink($tmpFile); } + /** + * @When /^user "([^"]*)" uploads a file with content "([^"]*)" to "([^"]*)" inside federated share "([^"]*)" via TUS using the WebDAV API$/ + * + * @param string $user + * @param string $content + * @param string $file + * @param string $destination + * + * @return void + * @throws Exception|GuzzleException + */ + public function userUploadsAFileWithContentToInsideFederatedShareViaTusUsingTheWebdavApi(string $user, string $content, string $file, string $destination): void { + $remoteItemId = $this->spacesContext->getSharesRemoteItemId($user, $destination); + $remoteItemId = \rawurlencode($remoteItemId); + $tmpFile = $this->tusContext->writeDataToTempFile($content); + $this->tusContext->uploadFileUsingTus( + $user, + \basename($tmpFile), + $file, + $remoteItemId + ); + $this->featureContext->setLastUploadDeleteTime(\time()); + \unlink($tmpFile); + } + /** * @When /^user "([^"]*)" uploads a file with content "([^"]*)" to "([^"]*)" via TUS inside of the space "([^"]*)" using the WebDAV API$/ * @@ -330,11 +355,10 @@ public function userSendsAChunkToTheLastCreatedTusLocationWithOffsetAndDataWithC } /** - * @When /^user "([^"]*)" sends a chunk to the last created TUS Location with data "([^"]*)" inside of the space "([^"]*)" with headers:$/ + * @When /^user "([^"]*)" sends a chunk to the last created TUS Location with data "([^"]*)" with the following headers:$/ * * @param string $user * @param string $data - * @param string $spaceName * @param TableNode $headers * * @return void @@ -343,7 +367,6 @@ public function userSendsAChunkToTheLastCreatedTusLocationWithOffsetAndDataWithC public function userSendsAChunkToTheLastCreatedTusLocationWithDataInsideOfTheSpaceWithHeaders( string $user, string $data, - string $spaceName, TableNode $headers ): void { $rows = $headers->getRowsHash(); diff --git a/tests/acceptance/bootstrap/TUSContext.php b/tests/acceptance/bootstrap/TUSContext.php index 5d7e1f47915..7581cdd7c6b 100644 --- a/tests/acceptance/bootstrap/TUSContext.php +++ b/tests/acceptance/bootstrap/TUSContext.php @@ -349,16 +349,12 @@ public function userUploadsAFileWithContentToUsingTus( string $destination ): void { $temporaryFileName = $this->writeDataToTempFile($content); - try { - $this->uploadFileUsingTus( - $user, - \basename($temporaryFileName), - $destination - ); - $this->featureContext->setLastUploadDeleteTime(\time()); - } catch (Exception $e) { - Assert::assertStringContainsString('TusPhp\Exception\FileException: Unable to create resource', (string)$e); - } + $this->uploadFileUsingTus( + $user, + \basename($temporaryFileName), + $destination + ); + $this->featureContext->setLastUploadDeleteTime(\time()); \unlink($temporaryFileName); } diff --git a/tests/acceptance/config/behat.yml b/tests/acceptance/config/behat.yml index 43fb2fca485..32ce04ca861 100644 --- a/tests/acceptance/config/behat.yml +++ b/tests/acceptance/config/behat.yml @@ -394,6 +394,7 @@ default: - OcisConfigContext: - NotificationContext: - SettingsContext: + - SpacesTUSContext: apiActivities: paths: diff --git a/tests/acceptance/features/apiCors/cors.feature b/tests/acceptance/features/apiCors/cors.feature index 6fcce1fd9e6..a728701835c 100644 --- a/tests/acceptance/features/apiCors/cors.feature +++ b/tests/acceptance/features/apiCors/cors.feature @@ -105,13 +105,13 @@ Feature: CORS headers @issue-8380 Scenario: CORS headers should be returned when uploading file using Tus and when CORS domain sending origin header in the Webdav api - Given user "Alice" has created a new TUS resource for the space "Personal" with content "" using the WebDAV API with these headers: + Given user "Alice" has created a new TUS resource in the space "Personal" with the following headers: | Upload-Length | 5 | # dGV4dEZpbGUudHh0 is the base64 encode of textFile.txt | Upload-Metadata | filename dGV4dEZpbGUudHh0 | | Tus-Resumable | 1.0.0 | | Origin | https://aphno.badal | - When user "Alice" sends a chunk to the last created TUS Location with data "01234" inside of the space "Personal" with headers: + When user "Alice" sends a chunk to the last created TUS Location with data "01234" with the following headers: | Origin | https://aphno.badal | | Upload-Checksum | MD5 4100c4d44da9177247e44a5fc1546778 | | Upload-Offset | 0 | @@ -123,13 +123,13 @@ Feature: CORS headers @issue-8380 Scenario: uploading file using Tus using different CORS headers - Given user "Alice" has created a new TUS resource for the space "Personal" with content "" using the WebDAV API with these headers: + Given user "Alice" has created a new TUS resource in the space "Personal" with the following headers: | Upload-Length | 5 | # dGV4dEZpbGUudHh0 is the base64 encode of textFile.txt | Upload-Metadata | filename dGV4dEZpbGUudHh0 | | Tus-Resumable | 1.0.0 | | Origin | https://something.else | - When user "Alice" sends a chunk to the last created TUS Location with data "01234" inside of the space "Personal" with headers: + When user "Alice" sends a chunk to the last created TUS Location with data "01234" with the following headers: | Origin | https://something.else | | Upload-Checksum | MD5 4100c4d44da9177247e44a5fc1546778 | | Upload-Offset | 0 | @@ -139,7 +139,7 @@ Feature: CORS headers # The Access-Control-Request-Headers need to be in lower-case and alphabetically order to comply with the rs/cors # package see: https://github.com/rs/cors/commit/4c32059b2756926619f6bf70281b91be7b5dddb2#diff-bf80d8fbedf172fab9ba2604da7f7be972e48b2f78a8d0cd21619d5f93665895R367 Scenario Outline: CORS headers should be returned when an preflight request is sent to Tus upload - Given user "Alice" has created a new TUS resource for the space "Personal" with content "" using the WebDAV API with these headers: + Given user "Alice" has created a new TUS resource in the space "Personal" with the following headers: | Upload-Length | 5 | # dGV4dEZpbGUudHh0 is the base64 encode of textFile.txt | Upload-Metadata | filename dGV4dEZpbGUudHh0 | diff --git a/tests/acceptance/features/apiOcm/share.feature b/tests/acceptance/features/apiOcm/share.feature index 40cdd8624a0..fe3b391baae 100755 --- a/tests/acceptance/features/apiOcm/share.feature +++ b/tests/acceptance/features/apiOcm/share.feature @@ -611,3 +611,24 @@ Feature: an user shares resources using ScienceMesh application } } """ + + @issue-10285 @issue-10536 + Scenario: federated user upload file to a shared folder via TUS + Given using spaces DAV path + And "Brian" has created the federation share invitation + And using server "LOCAL" + And "Alice" has accepted invitation + And using server "REMOTE" + And user "Brian" has created a folder "FOLDER" in space "Personal" + And user "Brian" has sent the following resource share invitation to federated user: + | resource | FOLDER | + | space | Personal | + | sharee | Alice | + | shareType | user | + | permissionsRole | Editor | + When using server "LOCAL" + And user "Alice" uploads a file with content "lorem" to "file.txt" inside federated share "FOLDER" via TUS using the WebDAV API + Then for user "Alice" the content of the federated share file "FOLDER/file.txt" of space "Shares" should be "lorem" +# Then for user "Alice" the content of the file "file.txt" of federation share "FOLDER" should be "lorem" + + diff --git a/tests/acceptance/features/apiSpacesShares/shareUploadTUS.feature b/tests/acceptance/features/apiSpacesShares/shareUploadTUS.feature index fa8628ba3ec..b353b9504fb 100644 --- a/tests/acceptance/features/apiSpacesShares/shareUploadTUS.feature +++ b/tests/acceptance/features/apiSpacesShares/shareUploadTUS.feature @@ -193,7 +193,7 @@ Feature: upload resources on share using TUS protocol | shareType | user | | permissionsRole | Editor | And user "Brian" has a share "FOLDER" synced - And user "Alice" has created a new TUS resource for the space "Personal" with content "" using the WebDAV API with these headers: + And user "Alice" has created a new TUS resource in the space "Personal" with the following headers: | Upload-Length | 5 | # L0ZPTERFUi90ZXh0RmlsZS50eHQ= is the base64 encode of /FOLDER/textFile.txt | Upload-Metadata | filename L0ZPTERFUi90ZXh0RmlsZS50eHQ= | @@ -213,7 +213,7 @@ Feature: upload resources on share using TUS protocol | shareType | user | | permissionsRole | Editor | And user "Brian" has a share "FOLDER" synced - And user "Alice" has created a new TUS resource for the space "Personal" with content "" using the WebDAV API with these headers: + And user "Alice" has created a new TUS resource in the space "Personal" with the following headers: | Upload-Length | 5 | # L0ZPTERFUi90ZXh0RmlsZS50eHQ= is the base64 encode of /FOLDER/textFile.txt | Upload-Metadata | filename L0ZPTERFUi90ZXh0RmlsZS50eHQ= | @@ -224,7 +224,7 @@ Feature: upload resources on share using TUS protocol Scenario: sharer shares a file with correct checksum should return the checksum in the propfind for sharee - Given user "Alice" has created a new TUS resource for the space "Personal" with content "" using the WebDAV API with these headers: + Given user "Alice" has created a new TUS resource in the space "Personal" with the following headers: | Upload-Length | 5 | # dGV4dEZpbGUudHh0 is the base64 encode of textFile.txt | Upload-Metadata | filename dGV4dEZpbGUudHh0 | @@ -243,7 +243,7 @@ Feature: upload resources on share using TUS protocol Scenario: sharer shares a file with correct checksum should return the checksum in the download header for sharee - Given user "Alice" has created a new TUS resource for the space "Personal" with content "" using the WebDAV API with these headers: + Given user "Alice" has created a new TUS resource in the space "Personal" with the following headers: | Upload-Length | 5 | # dGV4dEZpbGUudHh0 is the base64 encode of textFile.txt | Upload-Metadata | filename dGV4dEZpbGUudHh0 | @@ -309,7 +309,7 @@ Feature: upload resources on share using TUS protocol | shareType | user | | permissionsRole | Editor | And user "Brian" has a share "FOLDER" synced - And user "Alice" has created a new TUS resource for the space "Personal" with content "" using the WebDAV API with these headers: + And user "Alice" has created a new TUS resource in the space "Personal" with the following headers: | Upload-Length | 16 | # L0ZPTERFUi90ZXh0RmlsZS50eHQ= is the base64 encode of /FOLDER/textFile.txt | Upload-Metadata | filename L0ZPTERFUi90ZXh0RmlsZS50eHQ= | @@ -323,7 +323,7 @@ Feature: upload resources on share using TUS protocol Scenario: sharer uploads a chunked file with correct checksum and share it with sharee should work - Given user "Alice" has created a new TUS resource for the space "Personal" with content "" using the WebDAV API with these headers: + Given user "Alice" has created a new TUS resource in the space "Personal" with the following headers: | Upload-Length | 10 | # dGV4dEZpbGUudHh0 is the base64 encode of textFile.txt | Upload-Metadata | filename dGV4dEZpbGUudHh0 | @@ -345,7 +345,7 @@ Feature: upload resources on share using TUS protocol | shareType | user | | permissionsRole | Editor | And user "Brian" has a share "FOLDER" synced - And user "Brian" has created a new TUS resource for the space "Shares" with content "" using the WebDAV API with these headers: + And user "Brian" has created a new TUS resource in the space "Shares" with the following headers: | Upload-Length | 10 | # L0ZPTERFUi90ZXh0RmlsZS50eHQ= is the base64 encode of /FOLDER/textFile.txt | Upload-Metadata | filename L0ZPTERFUi90ZXh0RmlsZS50eHQ= | @@ -359,7 +359,7 @@ Feature: upload resources on share using TUS protocol Scenario: sharer uploads a file with checksum and as a sharee overwrites the shared file with new data and correct checksum - Given user "Alice" has created a new TUS resource for the space "Personal" with content "" using the WebDAV API with these headers: + Given user "Alice" has created a new TUS resource in the space "Personal" with the following headers: | Upload-Length | 16 | # dGV4dEZpbGUudHh0 is the base64 encode of textFile.txt | Upload-Metadata | filename dGV4dEZpbGUudHh0 | @@ -382,7 +382,7 @@ Feature: upload resources on share using TUS protocol @issue-1755 Scenario: sharer uploads a file with checksum and as a sharee overwrites the shared file with new data and invalid checksum - Given user "Alice" has created a new TUS resource for the space "Personal" with content "" using the WebDAV API with these headers: + Given user "Alice" has created a new TUS resource in the space "Personal" with the following headers: | Upload-Length | 16 | # dGV4dEZpbGUudHh0 is the base64 encode of textFile.txt | Upload-Metadata | filename dGV4dEZpbGUudHh0 | diff --git a/tests/acceptance/features/cliCommands/uploadSessions.feature b/tests/acceptance/features/cliCommands/uploadSessions.feature index 8190a2346f9..9262c954ce7 100644 --- a/tests/acceptance/features/cliCommands/uploadSessions.feature +++ b/tests/acceptance/features/cliCommands/uploadSessions.feature @@ -135,7 +135,7 @@ Feature: List upload sessions via CLI command | POSTPROCESSING_DELAY | 10s | And user "Alice" has uploaded file "filesForUpload/filesWithVirus/eicar.com" to "/virusFile.txt" And user "Alice" has uploaded file with content "upload content" to "/file1.txt" - And user "Alice" has created a new TUS resource for the space "Personal" with content "" using the WebDAV API with these headers: + And user "Alice" has created a new TUS resource in the space "Personal" with the following headers: | Upload-Length | 10 | # dGV4dEZpbGUudHh0 is the base64 encode of textFile.txt | Upload-Metadata | filename dGV4dEZpbGUudHh0 |