diff --git a/tests/TestHelpers/GraphHelper.php b/tests/TestHelpers/GraphHelper.php index 6bc12b86ed..5a3e6e83d5 100644 --- a/tests/TestHelpers/GraphHelper.php +++ b/tests/TestHelpers/GraphHelper.php @@ -1701,4 +1701,38 @@ class GraphHelper { $body ); } + + /** + * @param string $baseUrl + * @param string $xRequestId + * @param string $user + * @param string $password + * @param string $spaceId + * @param string $itemId + * @param mixed $body + * @param string $shareId + * + * @return ResponseInterface + * @throws GuzzleException + */ + public static function setLinkSharePassword( + string $baseUrl, + string $xRequestId, + string $user, + string $password, + string $spaceId, + string $itemId, + $body, + string $shareId + ): ResponseInterface { + $url = self::getBetaFullUrl($baseUrl, "drives/$spaceId/items/$itemId/permissions/$shareId/setPassword"); + return HttpRequestHelper::post( + $url, + $xRequestId, + $user, + $password, + self::getRequestHeaders(), + $body + ); + } } diff --git a/tests/acceptance/config/behat.yml b/tests/acceptance/config/behat.yml index 52b9bebbfa..3a37d66cc0 100644 --- a/tests/acceptance/config/behat.yml +++ b/tests/acceptance/config/behat.yml @@ -262,6 +262,7 @@ default: - FeatureContext: *common_feature_context_params - SpacesContext: - SharingNgContext: + - PublicWebDavContext: - OcisConfigContext: diff --git a/tests/acceptance/features/apiSharingNg/linkShare.feature b/tests/acceptance/features/apiSharingNg/linkShare.feature index 83087931bc..537f5ae0f0 100644 --- a/tests/acceptance/features/apiSharingNg/linkShare.feature +++ b/tests/acceptance/features/apiSharingNg/linkShare.feature @@ -518,3 +518,69 @@ Feature: Create a share link for a resource } } """ + + @env-config + Scenario: set password on a file's link share + Given the following configs have been set: + | config | value | + | OCIS_SHARING_PUBLIC_SHARE_MUST_HAVE_PASSWORD | false | + And user "Alice" has uploaded file with content "other data" to "textfile1.txt" + And user "Alice" has created the following link share: + | resourceType | file | + | resource | textfile1.txt | + | space | Personal | + | role | view | + When user "Alice" sets the following password for the last link share using the Graph API: + | resourceType | file | + | resource | textfile1.txt | + | space | Personal | + | password | %public% | + Then the HTTP status code should be "200" + And the JSON data of the response should match + """ + { + "type": "object", + "required": [ + "hasPassword" + ], + "properties": { + "hasPassword": { + "type": "boolean", + "enum": [true] + } + } + } + """ + And the public should be able to download file "textfile1.txt" from the last link share with password "%public%" and the content should be "other data" + + Scenario: update password of a file's link share + Given user "Alice" has uploaded file with content "other data" to "textfile1.txt" + And user "Alice" has created the following link share: + | resourceType | file | + | resource | textfile1.txt | + | space | Personal | + | role | view | + | password | $heLlo*1234* | + When user "Alice" updates the following password for the last link share using the Graph API: + | resourceType | file | + | resource | textfile1.txt | + | space | Personal | + | password | %public% | + Then the HTTP status code should be "200" + And the JSON data of the response should match + """ + { + "type": "object", + "required": [ + "hasPassword" + ], + "properties": { + "hasPassword": { + "type": "boolean", + "enum": [true] + } + } + } + """ + And the public should be able to download file "textfile1.txt" from the last link share with password "%public%" and the content should be "other data" + And the public download of file "textfile1.txt" from the last link share with password "$heLlo*1234*" should fail with HTTP status code "401" using shareNg diff --git a/tests/acceptance/features/bootstrap/PublicWebDavContext.php b/tests/acceptance/features/bootstrap/PublicWebDavContext.php index 5827e3bce0..38e9910666 100644 --- a/tests/acceptance/features/bootstrap/PublicWebDavContext.php +++ b/tests/acceptance/features/bootstrap/PublicWebDavContext.php @@ -288,6 +288,7 @@ class PublicWebDavContext implements Context { * @param string $password * @param string $range ignored when empty * @param string $publicWebDAVAPIVersion + * @param bool $shareNg * * @return ResponseInterface */ @@ -295,11 +296,16 @@ class PublicWebDavContext implements Context { string $path, string $password, string $range, - string $publicWebDAVAPIVersion = "old" + string $publicWebDAVAPIVersion = "old", + bool $shareNg = false ):ResponseInterface { $path = \ltrim($path, "/"); $password = $this->featureContext->getActualPassword($password); - $token = $this->featureContext->getLastCreatedPublicShareToken(); + if ($shareNg) { + $token = $this->featureContext->shareNgGetLastCreatedLinkShareToken(); + } else { + $token = $this->featureContext->getLastCreatedPublicShareToken(); + } $davPath = WebDavHelper::getDavPath( $token, 0, @@ -913,6 +919,33 @@ class PublicWebDavContext implements Context { $this->featureContext->theHTTPStatusCodeShouldBe(200, "", $response); } + /** + * @Then /^the public should be able to download file "([^"]*)" from the last link share with password "([^"]*)" and the content should be "([^"]*)"$/ + * + * @param string $path + * @param string $password + * @param string $content + * + * @return void + * @throws Exception + */ + public function shouldBeAbleToDownloadFileInsidePublicSharedFolderWithPasswordForSharingNGAndContentShouldBe( + string $path, + string $password, + string $content + ):void { + $response = $this->downloadFileFromPublicFolder( + $path, + $password, + "", + "new", + true + ); + + $this->featureContext->theHTTPStatusCodeShouldBe(200, "", $response); + $this->featureContext->checkDownloadedContentMatches($content, "", $response); + } + /** * @Then /^the public should be able to download file "([^"]*)" from inside the last public link shared folder using the (old|new) public WebDAV API without password and the content should be "([^"]*)"$/ * @@ -957,6 +990,30 @@ class PublicWebDavContext implements Context { ); } + /** + * @Then /^the public download of file "([^"]*)" from the last link share with password "([^"]*)" should fail with HTTP status code "([^"]*)" using shareNg$/ + * + * @param string $path + * @param string $password + * @param string $expectedHttpCode + * + * @return void + */ + public function shouldNotBeAbleToDownloadFileWithPasswordForShareNg( + string $path, + string $password, + string $expectedHttpCode = "401" + ):void { + $this->shouldNotBeAbleToDownloadRangeOfFileInsidePublicSharedFolderWithPassword( + "", + $path, + "new", + $password, + $expectedHttpCode, + true + ); + } + /** * @Then /^the public should be able to download the range "([^"]*)" of file "([^"]*)" from inside the last public link shared folder using the (old|new) public WebDAV API with password "([^"]*)""$/ * @@ -995,6 +1052,7 @@ class PublicWebDavContext implements Context { * @param string $publicWebDAVAPIVersion * @param string $password * @param string $expectedHttpCode + * @param bool $shareNg * * @return void * @throws Exception @@ -1004,7 +1062,8 @@ class PublicWebDavContext implements Context { string $path, string $publicWebDAVAPIVersion, string $password, - string $expectedHttpCode = "401" + string $expectedHttpCode = "401", + bool $shareNg = false ):void { if ($publicWebDAVAPIVersion === "old") { return; @@ -1014,7 +1073,8 @@ class PublicWebDavContext implements Context { $path, $password, $range, - $publicWebDAVAPIVersion + $publicWebDAVAPIVersion, + $shareNg ); $responseContent = $response->getBody()->getContents(); diff --git a/tests/acceptance/features/bootstrap/Sharing.php b/tests/acceptance/features/bootstrap/Sharing.php index 1b79c77bb8..b450427224 100755 --- a/tests/acceptance/features/bootstrap/Sharing.php +++ b/tests/acceptance/features/bootstrap/Sharing.php @@ -184,6 +184,18 @@ trait Sharing { return $this->getJsonDecodedResponse($lastResponse)['id']; } + /** + * @return string + */ + public function shareNgGetLastCreatedLinkShareToken(): string { + $lastResponse = $this->shareNgGetLastCreatedLinkShare(); + if (!isset($this->getJsonDecodedResponse($lastResponse)['link']['webUrl'])) { + throw new Error('Response did not contain share id ' . $this->getJsonDecodedResponse($lastResponse)['link']['webUrl'] . ' for the created public link'); + } + $last_created_link_webURL = $this->getJsonDecodedResponse($lastResponse)['link']['webUrl']; + return substr(strrchr($last_created_link_webURL, "/"), 1); + } + /** * Split given permissions string each separated with "," into array of strings * diff --git a/tests/acceptance/features/bootstrap/SharingNgContext.php b/tests/acceptance/features/bootstrap/SharingNgContext.php index dc508ffc7e..683a37f87f 100644 --- a/tests/acceptance/features/bootstrap/SharingNgContext.php +++ b/tests/acceptance/features/bootstrap/SharingNgContext.php @@ -239,17 +239,59 @@ class SharingNgContext implements Context { throw new Error('Expiration date or role is missing to update for share link!'); } - $this->featureContext->setResponse( - GraphHelper::updateLinkShare( - $this->featureContext->getBaseUrl(), - $this->featureContext->getStepLineRef(), - $user, - $this->featureContext->getPasswordForUser($user), - $spaceId, - $itemId, - \json_encode($body), - $this->featureContext->shareNgGetLastCreatedLinkShareID() - ) + $response = GraphHelper::updateLinkShare( + $this->featureContext->getBaseUrl(), + $this->featureContext->getStepLineRef(), + $user, + $this->featureContext->getPasswordForUser($user), + $spaceId, + $itemId, + \json_encode($body), + $this->featureContext->shareNgGetLastCreatedLinkShareID() ); + $this->featureContext->setResponse($response); + } + + /** + * @When user :user sets/updates the following password for the last link share using the Graph API: + * + * @param string $user + * @param TableNode|null $body + * + * @return void + * @throws Exception + * @throws \GuzzleHttp\Exception\GuzzleException + */ + public function userSetsOrUpdatesFollowingPasswordForLastLinkShareUsingTheGraphApi(string $user, TableNode $body):void { + $bodyRows = $body->getRowsHash(); + $space = $bodyRows['space']; + $resourceType = $bodyRows['resourceType']; + $resource = $bodyRows['resource']; + $spaceId = ($this->spacesContext->getSpaceByName($user, $space))["id"]; + if ($resourceType === 'folder') { + $itemId = $this->spacesContext->getResourceId($user, $space, $resource); + } else { + $itemId = $this->spacesContext->getFileId($user, $space, $resource); + } + + if (\array_key_exists('password', $bodyRows)) { + $body = [ + "password" => $this->featureContext->getActualPassword($bodyRows['password']), + ]; + } else { + throw new Error('Password is missing to set for share link!'); + } + + $response = GraphHelper::setLinkSharePassword( + $this->featureContext->getBaseUrl(), + $this->featureContext->getStepLineRef(), + $user, + $this->featureContext->getPasswordForUser($user), + $spaceId, + $itemId, + \json_encode($body), + $this->featureContext->shareNgGetLastCreatedLinkShareID() + ); + $this->featureContext->setResponse($response); } }