Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable10] move public upload/downlod steps into separate context #33999

Merged
merged 1 commit into from
Jan 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions tests/acceptance/config/behat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -107,6 +109,7 @@ default:
- FeatureContext: *common_feature_context_params
- LoggingContext:
- SearchContext:
- PublicWebDavContext:

apiWebdavProperties:
paths:
Expand Down Expand Up @@ -202,6 +205,7 @@ default:
- WebUISearchContext:
- WebUISharingContext:
- OccContext:
- PublicWebDavContext:

webUILogin:
paths:
Expand All @@ -223,6 +227,7 @@ default:
- WebUIGeneralContext:
- WebUILoginContext:
- WebUISharingContext:
- PublicWebDavContext:

webUIPersonalSettings:
paths:
Expand All @@ -247,6 +252,7 @@ default:
- WebUIGeneralContext:
- WebUILoginContext:
- WebUISharingContext:
- PublicWebDavContext:

webUIRenameFolders:
paths:
Expand Down Expand Up @@ -278,6 +284,7 @@ default:
- WebUIGeneralContext:
- WebUILoginContext:
- WebUISharingContext:
- PublicWebDavContext:

webUISharingInternalGroups:
paths:
Expand Down Expand Up @@ -341,6 +348,7 @@ default:
- WebUIGeneralContext:
- WebUILoginContext:
- WebUISharingContext:
- PublicWebDavContext:

# This suite is part of the user_management app in later core versions
webUIManageQuota:
Expand Down Expand Up @@ -390,6 +398,7 @@ default:
- WebUILoginContext:
- WebUIWebDavLockingContext:
- WebUISharingContext:
- PublicWebDavContext:

extensions:
jarnaiz\JUnitFormatter\JUnitFormatterExtension:
Expand Down
293 changes: 293 additions & 0 deletions tests/acceptance/features/bootstrap/PublicWebDavContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,293 @@
<?php
/**
* ownCloud
*
* @author Artur Neumann <artur@jankaritech.com>
* @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 <http://www.gnu.org/licenses/>
*
*/

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');
}
}
Loading