From 0a5a9b4470aaf905e865ba0182beb8dbd8261a3f Mon Sep 17 00:00:00 2001 From: Artur Neumann Date: Tue, 1 Jan 2019 10:38:58 +0545 Subject: [PATCH] move public upload/downlod steps into separate context --- tests/acceptance/config/behat.yml | 9 + .../bootstrap/PublicWebDavContext.php | 293 ++++++++++++++++++ .../acceptance/features/bootstrap/Sharing.php | 131 -------- .../acceptance/features/bootstrap/WebDav.php | 97 ------ 4 files changed, 302 insertions(+), 228 deletions(-) create mode 100644 tests/acceptance/features/bootstrap/PublicWebDavContext.php diff --git a/tests/acceptance/config/behat.yml b/tests/acceptance/config/behat.yml index 80c46edd1456..4ce16d0937e7 100644 --- a/tests/acceptance/config/behat.yml +++ b/tests/acceptance/config/behat.yml @@ -74,12 +74,14 @@ default: - '%paths.base%/../features/apiShareManagement' contexts: - FeatureContext: *common_feature_context_params + - PublicWebDavContext: apiShareOperations: paths: - '%paths.base%/../features/apiShareOperations' contexts: - FeatureContext: *common_feature_context_params + - PublicWebDavContext: apiSharingNotifications: paths: @@ -107,6 +109,7 @@ default: - FeatureContext: *common_feature_context_params - LoggingContext: - SearchContext: + - PublicWebDavContext: apiWebdavProperties: paths: @@ -202,6 +205,7 @@ default: - WebUISearchContext: - WebUISharingContext: - OccContext: + - PublicWebDavContext: webUILogin: paths: @@ -223,6 +227,7 @@ default: - WebUIGeneralContext: - WebUILoginContext: - WebUISharingContext: + - PublicWebDavContext: webUIPersonalSettings: paths: @@ -247,6 +252,7 @@ default: - WebUIGeneralContext: - WebUILoginContext: - WebUISharingContext: + - PublicWebDavContext: webUIRenameFolders: paths: @@ -278,6 +284,7 @@ default: - WebUIGeneralContext: - WebUILoginContext: - WebUISharingContext: + - PublicWebDavContext: webUISharingInternalGroups: paths: @@ -341,6 +348,7 @@ default: - WebUIGeneralContext: - WebUILoginContext: - WebUISharingContext: + - PublicWebDavContext: # This suite is part of the user_management app in later core versions webUIManageQuota: @@ -390,6 +398,7 @@ default: - WebUILoginContext: - WebUIWebDavLockingContext: - WebUISharingContext: + - PublicWebDavContext: extensions: jarnaiz\JUnitFormatter\JUnitFormatterExtension: diff --git a/tests/acceptance/features/bootstrap/PublicWebDavContext.php b/tests/acceptance/features/bootstrap/PublicWebDavContext.php new file mode 100644 index 000000000000..2baa15ccddb4 --- /dev/null +++ b/tests/acceptance/features/bootstrap/PublicWebDavContext.php @@ -0,0 +1,293 @@ + + * @copyright Copyright (c) 2018, ownCloud GmbH + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, + * as published by the Free Software Foundation; + * either version 3 of the License, or any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see + * + */ + +use Behat\Behat\Context\Context; +use Behat\Behat\Hook\Scope\BeforeScenarioScope; +use TestHelpers\HttpRequestHelper; + +require_once 'bootstrap.php'; + +/** + * context file for steps that execute actions as "the public". + */ +class PublicWebDavContext implements Context { + /** + * + * @var FeatureContext + */ + private $featureContext; + + /** + * @When /^the public downloads the last public shared file with range "([^"]*)" using the public WebDAV API$/ + * + * @param string $range + * + * @return void + */ + public function downloadPublicFileWithRange($range) { + $token = $this->featureContext->getLastShareData()->data->token; + $fullUrl = $this->featureContext->getBaseUrl() . "/public.php/webdav"; + $headers = [ + 'X-Requested-With' => 'XMLHttpRequest', + 'Range' => $range + ]; + $this->featureContext->setResponse( + HttpRequestHelper::get($fullUrl, $token, "", $headers) + ); + } + + /** + * @When /^the public downloads file "([^"]*)" from inside the last public shared folder with range "([^"]*)" using the public WebDAV API$/ + * + * @param string $path + * @param string $range + * + * @return void + */ + public function downloadPublicFileInsideAFolderWithRange($path, $range) { + $fullUrl = $this->featureContext->getBaseUrl() . "/public.php/webdav$path"; + $headers = [ + 'X-Requested-With' => 'XMLHttpRequest', + 'Range' => $range + ]; + $response = HttpRequestHelper::get( + $fullUrl, $this->featureContext->getLastShareData()->data->token, + "", $headers + ); + $this->featureContext->setResponse($response); + } + + /** + * @When /^the public downloads file "([^"]*)" from inside the last public shared folder with password "([^"]*)" with range "([^"]*)" using the public WebDAV API$/ + * + * @param string $path + * @param string $password + * @param string $range + * + * @return void + */ + public function publicDownloadsTheFileInsideThePublicSharedFolderWithPassword( + $path, $password, $range + ) { + $path = \ltrim($path, "/"); + $password = $this->featureContext->getActualPassword($password); + $fullUrl = $this->featureContext->getBaseUrl() . "/public.php/webdav/$path"; + $headers = [ + 'X-Requested-With' => 'XMLHttpRequest', + 'Range' => $range + ]; + $response = HttpRequestHelper::get( + $fullUrl, $this->featureContext->getLastShareData()->data->token, + $password, $headers + ); + $this->featureContext->setResponse($response); + } + + /** + * @When the public uploads file ":filename" using the old WebDAV API + * @Given the public has uploaded file ":filename" + * + * @param string $source target file name + * + * @return void + */ + public function publiclyUploadingFile($source) { + $file = \GuzzleHttp\Stream\Stream::factory(\fopen($source, 'r')); + $this->publicUploadContent(\basename($source), '', $file->getContents()); + } + + /** + * @When the public uploads file ":filename" with content ":body" with autorename mode using the public WebDAV API + * @Given the public has uploaded file ":filename" with content ":body" with autorename mode + * + * @param string $filename target file name + * @param string $body content to upload + * + * @return void + */ + public function publiclyUploadingContentAutorename($filename, $body = 'test') { + $this->publicUploadContent($filename, '', $body, true); + } + + /** + * @When the public uploads file ":filename" with password ":password" and content ":body" using the public WebDAV API + * @Given the public has uploaded file ":filename" with password ":password" and content ":body" + * + * @param string $filename target file name + * @param string $password + * @param string $body content to upload + * + * @return void + */ + public function publiclyUploadingContentWithPassword( + $filename, $password = '', $body = 'test' + ) { + $this->publicUploadContent($filename, $password, $body); + } + + /** + * @When the public overwrites file ":filename" with content ":body" using the old WebDAV API + * @Given the public has overwritten file ":filename" with content ":body" + * + * @param string $filename target file name + * @param string $body content to upload + * + * @return void + */ + public function publiclyOverwritingContent($filename, $body = 'test') { + $this->publicUploadContent($filename, '', $body, false); + } + + /** + * @When the public uploads file ":filename" with content ":body" using the public WebDAV API + * @Given the public has uploaded file ":filename" with content ":body" + * + * @param string $filename target file name + * @param string $body content to upload + * + * @return void + */ + public function publiclyUploadingContent($filename, $body = 'test') { + $this->publicUploadContent($filename, '', $body); + } + + /** + * @Then /^the public should be able to download the range "([^"]*)" of file "([^"]*)" from inside the last public shared folder with password "([^"]*)" and the content should be "([^"]*)"$/ + * + * @param string $range + * @param string $path + * @param string $password + * @param string $content + * + * @return void + */ + public function shouldBeAbleToDownloadFileInsidePublicSharedFolderWithPassword( + $range, $path, $password, $content + ) { + $this->publicDownloadsTheFileInsideThePublicSharedFolderWithPassword( + $path, $password, $range + ); + $this->featureContext->downloadedContentShouldBe($content); + } + + /** + * @Then /^the public should be able to download the range "([^"]*)" of file "([^"]*)" from inside the last public shared folder and the content should be "([^"]*)"$/ + * + * @param string $range + * @param string $path + * @param string $content + * + * @return void + */ + public function shouldBeAbleToDownloadFileInsidePublicSharedFolder( + $range, $path, $content + ) { + $this->publicDownloadsTheFileInsideThePublicSharedFolderWithPassword( + $path, null, $range + ); + $this->featureContext->downloadedContentShouldBe($content); + } + + /** + * @Then publicly uploading a file should not work + * + * @return void + */ + public function publiclyUploadingShouldNotWork() { + $this->publicUploadContent('whateverfilefortesting.txt', '', 'test'); + $response = $this->featureContext->getResponse(); + PHPUnit_Framework_Assert::assertTrue( + ($response->getStatusCode() == 507) + || ( + ($response->getStatusCode() >= 400) + && ($response->getStatusCode() <= 499) + ), + "upload should have failed but passed with code " . + $response->getStatusCode() + ); + } + + /** + * @Then publicly uploading a file should work + * + * @return void + */ + public function publiclyUploadingShouldWork() { + $path = 'whateverfilefortesting.txt'; + $content = 'test'; + $this->publicUploadContent($path, '', $content); + $response = $this->featureContext->getResponse(); + PHPUnit_Framework_Assert::assertTrue( + ($response->getStatusCode() == 201), + "upload should have passed but failed with code " . + $response->getStatusCode() + ); + $this->shouldBeAbleToDownloadFileInsidePublicSharedFolder( + "bytes=0-3", $path, $content + ); + } + + /** + * Uploads a file through the public WebDAV API and sets $reponse in FeatureContext + * + * @param string $filename + * @param string $password + * @param string $body + * @param bool $autorename + * + * @return void + */ + public function publicUploadContent( + $filename, + $password = '', + $body = 'test', + $autorename = false + ) { + $password = $this->featureContext->getActualPassword($password); + $url = $this->featureContext->getBaseUrl() . "/public.php/webdav/"; + $url .= \rawurlencode(\ltrim($filename, '/')); + $token = $this->featureContext->getLastShareToken(); + $headers = ['X-Requested-With' => 'XMLHttpRequest']; + + if ($autorename) { + $headers['OC-Autorename'] = 1; + } + $response = HttpRequestHelper::put( + $url, $token, $password, $headers, $body + ); + $this->featureContext->setResponse($response); + } + + /** + * @BeforeScenario + * + * @param BeforeScenarioScope $scope + * + * @return void + */ + public function setUpScenario(BeforeScenarioScope $scope) { + // Get the environment + $environment = $scope->getEnvironment(); + // Get all the contexts you need in this context + $this->featureContext = $environment->getContext('FeatureContext'); + } +} diff --git a/tests/acceptance/features/bootstrap/Sharing.php b/tests/acceptance/features/bootstrap/Sharing.php index b5f34970f720..d805db72dcb6 100644 --- a/tests/acceptance/features/bootstrap/Sharing.php +++ b/tests/acceptance/features/bootstrap/Sharing.php @@ -510,74 +510,6 @@ private function checkDownload( } } - /** - * @When the public uploads file ":filename" with content ":body" using the public WebDAV API - * @Given the public has uploaded file ":filename" with content ":body" - * - * @param string $filename target file name - * @param string $body content to upload - * - * @return void - */ - public function publiclyUploadingContent($filename, $body = 'test') { - $this->publicUploadContent($filename, '', $body); - } - - /** - * @When the public overwrites file ":filename" with content ":body" using the old WebDAV API - * @Given the public has overwritten file ":filename" with content ":body" - * - * @param string $filename target file name - * @param string $body content to upload - * - * @return void - */ - public function publiclyOverwritingContent($filename, $body = 'test') { - $this->publicUploadContent($filename, '', $body, false); - } - - /** - * @When the public uploads file ":filename" with password ":password" and content ":body" using the public WebDAV API - * @Given the public has uploaded file ":filename" with password ":password" and content ":body" - * - * @param string $filename target file name - * @param string $password - * @param string $body content to upload - * - * @return void - */ - public function publiclyUploadingContentWithPassword( - $filename, $password = '', $body = 'test' - ) { - $this->publicUploadContent($filename, $password, $body); - } - - /** - * @When the public uploads file ":filename" with content ":body" with autorename mode using the public WebDAV API - * @Given the public has uploaded file ":filename" with content ":body" with autorename mode - * - * @param string $filename target file name - * @param string $body content to upload - * - * @return void - */ - public function publiclyUploadingContentAutorename($filename, $body = 'test') { - $this->publicUploadContent($filename, '', $body, true); - } - - /** - * @When the public uploads file ":filename" using the old WebDAV API - * @Given the public has uploaded file ":filename" - * - * @param string $source target file name - * - * @return void - */ - public function publiclyUploadingFile($source) { - $file = \GuzzleHttp\Stream\Stream::factory(\fopen($source, 'r')); - $this->publicUploadContent(\basename($source), '', $file->getContents()); - } - /** * @Then /^user "([^"]*)" should not be able to create a public link share of (?:file|folder) "([^"]*)" using the sharing API$/ * @@ -594,69 +526,6 @@ public function shouldNotBeAbleToCreatePublicLinkShare($sharer, $filepath) { ); } - /** - * @Then publicly uploading a file should not work - * - * @return void - */ - public function publiclyUploadingShouldNotWork() { - $this->publicUploadContent('whateverfilefortesting.txt', '', 'test'); - - PHPUnit_Framework_Assert::assertTrue( - ($this->response->getStatusCode() == 507) - || ( - ($this->response->getStatusCode() >= 400) - && ($this->response->getStatusCode() <= 499) - ), - "upload should have failed but passed with code " . $this->response->getStatusCode() - ); - } - - /** - * @Then publicly uploading a file should work - * - * @return void - */ - public function publiclyUploadingShouldWork() { - $path = 'whateverfilefortesting.txt'; - $content = 'test'; - $this->publicUploadContent($path, '', $content); - - PHPUnit_Framework_Assert::assertTrue( - ($this->response->getStatusCode() == 201), - "upload should have passed but failed with code " . $this->response->getStatusCode() - ); - $this->shouldBeAbleToDownloadFileInsidePublicSharedFolder("bytes=0-3", $path, $content); - } - - /** - * @param string $filename - * @param string $password - * @param string $body - * @param bool $autorename - * - * @return void - */ - private function publicUploadContent( - $filename, - $password = '', - $body = 'test', - $autorename = false - ) { - $password = $this->getActualPassword($password); - $url = $this->getBaseUrl() . "/public.php/webdav/"; - $url .= \rawurlencode(\ltrim($filename, '/')); - $token = $this->getLastShareToken(); - $headers = ['X-Requested-With' => 'XMLHttpRequest']; - - if ($autorename) { - $headers['OC-Autorename'] = 1; - } - $this->response = HttpRequestHelper::put( - $url, $token, $password, $headers, $body - ); - } - /** * @When /^the user adds an expiration date to the last share using the sharing API$/ * diff --git a/tests/acceptance/features/bootstrap/WebDav.php b/tests/acceptance/features/bootstrap/WebDav.php index 2429104258e7..527e64ba724e 100644 --- a/tests/acceptance/features/bootstrap/WebDav.php +++ b/tests/acceptance/features/bootstrap/WebDav.php @@ -535,103 +535,6 @@ public function userDownloadsFileWithRange($user, $fileSource, $range) { ); } - /** - * @When /^the public downloads the last public shared file with range "([^"]*)" using the public WebDAV API$/ - * - * @param string $range - * - * @return void - */ - public function downloadPublicFileWithRange($range) { - $token = $this->lastShareData->data->token; - $fullUrl = $this->getBaseUrl() . "/public.php/webdav"; - $headers = [ - 'X-Requested-With' => 'XMLHttpRequest', - 'Range' => $range - ]; - $this->response = HttpRequestHelper::get($fullUrl, $token, "", $headers); - } - - /** - * @When /^the public downloads file "([^"]*)" from inside the last public shared folder with range "([^"]*)" using the public WebDAV API$/ - * - * @param string $path - * @param string $range - * - * @return void - */ - public function downloadPublicFileInsideAFolderWithRange($path, $range) { - $fullUrl = $this->getBaseUrl() . "/public.php/webdav$path"; - $headers = [ - 'X-Requested-With' => 'XMLHttpRequest', - 'Range' => $range - ]; - $this->response = HttpRequestHelper::get( - $fullUrl, $this->lastShareData->data->token, "", $headers - ); - } - - /** - * @When /^the public downloads file "([^"]*)" from inside the last public shared folder with password "([^"]*)" with range "([^"]*)" using the public WebDAV API$/ - * - * @param string $path - * @param string $password - * @param string $range - * - * @return void - */ - public function publicDownloadsTheFileInsideThePublicSharedFolderWithPassword( - $path, $password, $range - ) { - $path = \ltrim($path, "/"); - $password = $this->getActualPassword($password); - $fullUrl = $this->getBaseUrl() . "/public.php/webdav/$path"; - $headers = [ - 'X-Requested-With' => 'XMLHttpRequest', - 'Range' => $range - ]; - $this->response = HttpRequestHelper::get( - $fullUrl, $this->lastShareData->data->token, $password, $headers - ); - } - - /** - * @Then /^the public should be able to download the range "([^"]*)" of file "([^"]*)" from inside the last public shared folder with password "([^"]*)" and the content should be "([^"]*)"$/ - * - * @param string $range - * @param string $path - * @param string $password - * @param string $content - * - * @return void - */ - public function shouldBeAbleToDownloadFileInsidePublicSharedFolderWithPassword( - $range, $path, $password, $content - ) { - $this->publicDownloadsTheFileInsideThePublicSharedFolderWithPassword( - $path, $password, $range - ); - $this->downloadedContentShouldBe($content); - } - - /** - * @Then /^the public should be able to download the range "([^"]*)" of file "([^"]*)" from inside the last public shared folder and the content should be "([^"]*)"$/ - * - * @param string $range - * @param string $path - * @param string $content - * - * @return void - */ - public function shouldBeAbleToDownloadFileInsidePublicSharedFolder( - $range, $path, $content - ) { - $this->publicDownloadsTheFileInsideThePublicSharedFolderWithPassword( - $path, null, $range - ); - $this->downloadedContentShouldBe($content); - } - /** * @Then /^user "([^"]*)" using password "([^"]*)" should not be able to download file "([^"]*)"$/ *