From f4b08e62eb85233a1894cec960fff3957725361e Mon Sep 17 00:00:00 2001 From: prajwol Date: Tue, 19 Sep 2023 17:03:20 +0545 Subject: [PATCH] added test coverage for previewing shared resources using spaces dav path --- tests/TestHelpers/GraphHelper.php | 42 ++++ .../acceptance/features/bootstrap/WebDav.php | 200 +++++++++++++++++- .../coreApiWebdavPreviews/previews.feature | 33 +-- 3 files changed, 256 insertions(+), 19 deletions(-) diff --git a/tests/TestHelpers/GraphHelper.php b/tests/TestHelpers/GraphHelper.php index 1db5b52972e..2a27df195e1 100644 --- a/tests/TestHelpers/GraphHelper.php +++ b/tests/TestHelpers/GraphHelper.php @@ -1426,4 +1426,46 @@ public static function unassignRole( self::getRequestHeaders(), ); } + + /** + * @param string $baseUrl + * @param string $xRequestId + * @param string $user + * @param string $password + * @param string $path + * + * @return string + * @throws GuzzleException + * @throws Exception + */ + public static function getShareMountId( + string $baseUrl, + string $xRequestId, + string $user, + string $password, + string $path + ): string { + $response = self::getMySpaces( + $baseUrl, + $user, + $password, + '', + $xRequestId + ); + $drives = json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR); + + // the response returns the shared resource in driveAlias all in lowercase, + // For example: if we get the property of a shared resource "FOLDER" then the response contains "driveAlias": "mountpoint/folder" + // In case of two shares with same name, the response for the second shared resource will contain, "driveAlias": "mountpoint/folder-(2)" + $path = strtolower($path); + foreach ($drives["value"] as $value) { + if ($value["driveAlias"] === "mountpoint/" . $path) { + return $value["id"]; + } + } + throw new \Exception( + __METHOD__ + . " Cannot find share mountpoint id of '$path' for user '$user'" + ); + } } diff --git a/tests/acceptance/features/bootstrap/WebDav.php b/tests/acceptance/features/bootstrap/WebDav.php index 9b55f096d65..64289299a0d 100644 --- a/tests/acceptance/features/bootstrap/WebDav.php +++ b/tests/acceptance/features/bootstrap/WebDav.php @@ -31,6 +31,7 @@ use TestHelpers\WebDavHelper; use TestHelpers\HttpRequestHelper; use TestHelpers\Asserts\WebDav as WebDavAssert; +use TestHelpers\GraphHelper; /** * WebDav functions @@ -499,7 +500,7 @@ public function downloadPreviews(string $user, ?string $path, ?string $doDavRequ [], null, "files", - '2', + null, false, null, $urlParameter, @@ -4629,6 +4630,195 @@ public function downloadPreviewOfFiles(string $user, string $path, string $width $this->setResponse($response); } + /** + * @When user :user downloads the preview of shared resource :path with width :width and height :height using the WebDAV API + * + * @param string $user + * @param string $path + * @param string $width + * @param string $height + * + * @return void + */ + public function userDownloadsThePreviewOfSharedResourceWithWidthAndHeightUsingTheWebdavApi(string $user, string $path, string $width, string $height): void { + if ($this->getDavPathVersion() === 3) { + $this->setResponse($this->downloadSharedFilePreview($user, $path, $width, $height)); + } else { + $this->setResponse($this->downloadPreviews($user, $path, null, $width, $height)); + } + } + + /** + * @Given user :user has downloaded the preview of shared resource :path with width :width and height :height + * + * @param string $user + * @param string $path + * @param string $width + * @param string $height + * + * @return void + */ + public function userHasDownloadedThePreviewOfSharedResourceWithWidthAndHeight(string $user, string $path, string $width, string $height): void { + if ($this->getDavPathVersion() === 3) { + $response = $this->downloadSharedFilePreview($user, $path, $width, $height); + } else { + $response = $this->downloadPreviews($user, $path, null, $width, $height); + } + $this->setResponse($response); + $this->theHTTPStatusCodeShouldBe(200, '', $response); + $this->imageDimensionsShouldBe($width, $height); + // save response to user response dictionary for further comparisons + $this->userResponseBodyContents[$user] = $this->responseBodyContent; + } + + /** + * @Then as user :user the preview of shared resource :path with width :width and height :height should have been changed + * + * @param string $user + * @param string $path + * @param string $width + * @param string $height + * + * @return void + */ + public function asUserThePreviewOfSharedResourceWithWidthAndHeightShouldHaveBeenChanged(string $user, string $path, string $width, string $height):void { + if ($this->getDavPathVersion() === 3) { + $response = $this->downloadSharedFilePreview($user, $path, $width, $height); + } else { + $response = $this->downloadPreviews($user, $path, null, $width, $height); + } + $this->setResponse($response); + $this->theHTTPStatusCodeShouldBe(200, '', $response); + $newResponseBodyContents = $this->response->getBody()->getContents(); + Assert::assertNotEquals( + $newResponseBodyContents, + // different users can download files before and after an update is made to a file + // previous response content is fetched from user response body content array for that user + $this->userResponseBodyContents[$user], + __METHOD__ . " previous and current previews content is same but expected to be different", + ); + // update the saved content for the next comparison + $this->userResponseBodyContents[$user] = $newResponseBodyContents; + } + + /** + * @When user :user uploads file with content :content to shared resource :destination using the WebDAV API + * + * @param string $user + * @param string $content + * @param string $destination + * + * @return void + */ + public function userUploadsFileWithContentSharedResourceToUsingTheWebdavApi(string $user, string $content, string $destination): void { + if ($this->getDavPathVersion() === 3) { + $this->setResponse($this->uploadToSharedFolder($user, $destination, $content)); + } else { + $this->uploadFileWithContent($user, $content, $destination); + } + } + + /** + * @param string $user + * @param string $path + * + * @return string + * @throws GuzzleException + */ + public function getMountSharesPath( + string $user, + string $path + ): string { + $user = $this->getActualUsername($user); + $path = trim($path, "/"); + $pathArray = explode("/", $path); + + $shareMountId = GraphHelper::getShareMountId( + $this->getBaseUrl(), + $this->getStepLineRef(), + $user, + $this->getPasswordForUser($user), + $pathArray[1] + ); + + if (\count($pathArray) > 2) { + $pathArray = \array_slice($pathArray, 2); + $path = '/' . implode("/", array_map("strval", $pathArray)); + } else { + $path = null; + } + return $shareMountId . $path; + } + + /** + * @param string $user + * @param string $path + * @param string|null $width + * @param string|null $height + * + * @return ResponseInterface + * @throws GuzzleException + */ + public function downloadSharedFilePreview( + string $user, + string $path, + ?string $width = null, + ?string $height = null + ): ResponseInterface { + if ($width !== null && $height !== null) { + $urlParameter = [ + 'x' => $width, + 'y' => $height, + 'forceIcon' => '0', + 'preview' => '1' + ]; + $urlParameter = \http_build_query($urlParameter, '', '&'); + } else { + $urlParameter = null; + } + $sharesPath = $this->getMountSharesPath($user, $path) . '/?' . $urlParameter; + + $davPath = WebDavHelper::getDavPath($user, $this->getDavPathVersion()); + $fullUrl = $this->getBaseUrl() . $davPath . $sharesPath; + + return HttpRequestHelper::sendRequest( + $fullUrl, + $this->getStepLineRef(), + 'GET', + $user, + $this->getPasswordForUser($user) + ); + } + + /** + * @param string $user + * @param string $destination + * @param string|null $content + * + * @return ResponseInterface + * @throws GuzzleException + */ + public function uploadToSharedFolder( + string $user, + string $destination, + ?string $content = null + ): ResponseInterface { + $sharesPath = $this->getMountSharesPath($user, $destination); + + $davPath = WebDavHelper::getDavPath($user, $this->getDavPathVersion()); + $fullUrl = $this->getBaseUrl() . $davPath . $sharesPath; + + return HttpRequestHelper::sendRequest( + $fullUrl, + $this->getStepLineRef(), + 'PUT', + $user, + $this->getPasswordForUser($user), + null, + $content + ); + } + /** * @When user :user1 downloads the preview of :path of :user2 with width :width and height :height using the WebDAV API * @@ -4726,8 +4916,8 @@ public function theDownloadedPreviewContentShouldMatchWithFixturesPreviewContent * @return void */ public function userDownloadsThePreviewOfWithWidthAndHeight(string $user, string $path, string $width, string $height):void { - $this->downloadPreviewOfFiles($user, $path, $width, $height); - $this->theHTTPStatusCodeShouldBe(200); + $response = $this->downloadPreviewOfFiles($user, $path, $width, $height); + $this->theHTTPStatusCodeShouldBe(200, '', $response); $this->imageDimensionsShouldBe($width, $height); // save response to user response dictionary for further comparisons $this->userResponseBodyContents[$user] = $this->responseBodyContent; @@ -4744,8 +4934,8 @@ public function userDownloadsThePreviewOfWithWidthAndHeight(string $user, string * @return void */ public function asUserThePreviewOfPathWithHeightAndWidthShouldHaveBeenChanged(string $user, string $path, string $width, string $height):void { - $this->downloadPreviewOfFiles($user, $path, $width, $height); - $this->theHTTPStatusCodeShouldBe(200); + $response = $this->downloadPreviewOfFiles($user, $path, $width, $height); + $this->theHTTPStatusCodeShouldBe(200, '', $response); $newResponseBodyContents = $this->response->getBody()->getContents(); Assert::assertNotEquals( $newResponseBodyContents, diff --git a/tests/acceptance/features/coreApiWebdavPreviews/previews.feature b/tests/acceptance/features/coreApiWebdavPreviews/previews.feature index c7f31aa99c4..7fcdb2eb3d5 100644 --- a/tests/acceptance/features/coreApiWebdavPreviews/previews.feature +++ b/tests/acceptance/features/coreApiWebdavPreviews/previews.feature @@ -141,7 +141,7 @@ Feature: previews of files downloaded through the webdav API And user "Alice" has uploaded file "filesForUpload/" to "/" And user "Alice" has shared file "/" with user "Brian" And user "Brian" has accepted share "/" offered by user "Alice" - When user "Brian" downloads the preview of "/Shares/" with width "32" and height "32" using the WebDAV API + When user "Brian" downloads the preview of shared resource "/Shares/" with width "32" and height "32" using the WebDAV API Then the HTTP status code should be "200" And the downloaded image should be "32" pixels wide and "32" pixels high Examples: @@ -150,6 +150,8 @@ Feature: previews of files downloaded through the webdav API | old | example.gif | | new | lorem.txt | | new | example.gif | + | spaces | lorem.txt | + | spaces | example.gif | Scenario Outline: user tries to download previews of other users files @@ -213,14 +215,15 @@ Feature: previews of files downloaded through the webdav API And user "Alice" has uploaded file "filesForUpload/lorem.txt" to "/parent.txt" And user "Alice" has shared file "/parent.txt" with user "Brian" And user "Brian" has accepted share "/parent.txt" offered by user "Alice" - And user "Brian" has downloaded the preview of "/Shares/parent.txt" with width "32" and height "32" + And user "Brian" has downloaded the preview of shared resource "/Shares/parent.txt" with width "32" and height "32" When user "Alice" uploads file with content "this is a file to upload" to "/parent.txt" using the WebDAV API Then the HTTP status code should be "204" - And as user "Brian" the preview of "/Shares/parent.txt" with width "32" and height "32" should have been changed + And as user "Brian" the preview of shared resource "/Shares/parent.txt" with width "32" and height "32" should have been changed Examples: | dav-path-version | | old | | new | + | spaces | Scenario Outline: it should update the preview content if the file content is updated (content with UTF chars) @@ -246,19 +249,20 @@ Feature: previews of files downloaded through the webdav API And user "Alice" has shared folder "FOLDER" with user "Brian" And user "Brian" has accepted share "/FOLDER" offered by user "Alice" And user "Alice" has downloaded the preview of "/FOLDER/lorem.txt" with width "32" and height "32" - And user "Brian" has downloaded the preview of "Shares/FOLDER/lorem.txt" with width "32" and height "32" + And user "Brian" has downloaded the preview of shared resource "Shares/FOLDER/lorem.txt" with width "32" and height "32" When user "Alice" uploads file "filesForUpload/lorem.txt" to "/FOLDER/lorem.txt" using the WebDAV API Then the HTTP status code should be "204" And as user "Alice" the preview of "/FOLDER/lorem.txt" with width "32" and height "32" should have been changed - And as user "Brian" the preview of "Shares/FOLDER/lorem.txt" with width "32" and height "32" should have been changed - When user "Brian" uploads file with content "new uploaded content" to "Shares/FOLDER/lorem.txt" using the WebDAV API + And as user "Brian" the preview of shared resource "Shares/FOLDER/lorem.txt" with width "32" and height "32" should have been changed + When user "Brian" uploads file with content "new uploaded content" to shared resource "Shares/FOLDER/lorem.txt" using the WebDAV API Then the HTTP status code should be "204" And as user "Alice" the preview of "/FOLDER/lorem.txt" with width "32" and height "32" should have been changed - And as user "Brian" the preview of "Shares/FOLDER/lorem.txt" with width "32" and height "32" should have been changed + And as user "Brian" the preview of shared resource "Shares/FOLDER/lorem.txt" with width "32" and height "32" should have been changed Examples: | dav-path-version | | old | | new | + | spaces | Scenario Outline: updates to a group shared file should change the preview for both sharees and sharers @@ -274,19 +278,20 @@ Feature: previews of files downloaded through the webdav API And user "Brian" has accepted share "/FOLDER" offered by user "Alice" And user "Carol" has accepted share "/FOLDER" offered by user "Alice" And user "Alice" has downloaded the preview of "/FOLDER/lorem.txt" with width "32" and height "32" - And user "Brian" has downloaded the preview of "Shares/FOLDER/lorem.txt" with width "32" and height "32" - And user "Carol" has downloaded the preview of "Shares/FOLDER/lorem.txt" with width "32" and height "32" + And user "Brian" has downloaded the preview of shared resource "Shares/FOLDER/lorem.txt" with width "32" and height "32" + And user "Carol" has downloaded the preview of shared resource "Shares/FOLDER/lorem.txt" with width "32" and height "32" When user "Alice" uploads file "filesForUpload/lorem.txt" to "/FOLDER/lorem.txt" using the WebDAV API Then the HTTP status code should be "204" And as user "Alice" the preview of "/FOLDER/lorem.txt" with width "32" and height "32" should have been changed - And as user "Brian" the preview of "Shares/FOLDER/lorem.txt" with width "32" and height "32" should have been changed - And as user "Carol" the preview of "Shares/FOLDER/lorem.txt" with width "32" and height "32" should have been changed - When user "Brian" uploads file with content "new uploaded content" to "Shares/FOLDER/lorem.txt" using the WebDAV API + And as user "Brian" the preview of shared resource "Shares/FOLDER/lorem.txt" with width "32" and height "32" should have been changed + And as user "Carol" the preview of shared resource "Shares/FOLDER/lorem.txt" with width "32" and height "32" should have been changed + When user "Brian" uploads file with content "new uploaded content" to shared resource "Shares/FOLDER/lorem.txt" using the WebDAV API Then the HTTP status code should be "204" And as user "Alice" the preview of "/FOLDER/lorem.txt" with width "32" and height "32" should have been changed - And as user "Brian" the preview of "Shares/FOLDER/lorem.txt" with width "32" and height "32" should have been changed - And as user "Carol" the preview of "Shares/FOLDER/lorem.txt" with width "32" and height "32" should have been changed + And as user "Brian" the preview of shared resource "Shares/FOLDER/lorem.txt" with width "32" and height "32" should have been changed + And as user "Carol" the preview of shared resource "Shares/FOLDER/lorem.txt" with width "32" and height "32" should have been changed Examples: | dav-path-version | | old | | new | + | spaces |