add apiTests. preview of the file (#3731)

This commit is contained in:
Viktor Scharf
2022-05-11 09:13:59 +02:00
committed by GitHub
parent 161480546c
commit 575312c2ce
7 changed files with 245 additions and 3 deletions
@@ -71,6 +71,11 @@ class SpacesContext implements Context {
*/
private $ocsApiUrl = '/ocs/v2.php/apps/files_sharing/api/v1/shares';
/**
* @var string
*/
private $davSpacesUrl = '/remote.php/dav/spaces/';
/**
* @param string $spaceName
*
@@ -250,7 +255,7 @@ class SpacesContext implements Context {
*/
public function getFileData(string $user, string $spaceName, string $fileName): ResponseInterface {
$space = $this->getSpaceByName($user, $spaceName);
$fullUrl = $this->baseUrl . "/remote.php/dav/spaces/" . $space["id"] . "/" . $fileName;
$fullUrl = $this->baseUrl . $this->davSpacesUrl . $space["id"] . "/" . $fileName;
$this->featureContext->setResponse(
HttpRequestHelper::get(
@@ -2085,7 +2090,7 @@ class SpacesContext implements Context {
string $spaceName
): void {
$space = $this->getSpaceByName($user, $spaceName);
$fullUrl = $this->baseUrl . "/remote.php/dav/spaces/trash-bin/" . $space["id"];
$fullUrl = $this->baseUrl . $this->davSpacesUrl . "trash-bin/" . $space["id"];
$this->featureContext->setResponse(
HttpRequestHelper::sendRequest(
$fullUrl,
@@ -2191,7 +2196,7 @@ class SpacesContext implements Context {
}
};
$destination = $this->baseUrl . "/remote.php/dav/spaces/" . $space["id"] . $destination;
$destination = $this->baseUrl . $this->davSpacesUrl . $space["id"] . $destination;
$header = ["Destination" => $destination, "Overwrite" => "F"];
$fullUrl = $this->baseUrl . $pathToDeletedObject;
@@ -2207,4 +2212,47 @@ class SpacesContext implements Context {
)
);
}
/**
* User downloads a priview of the file inside of the project space
* @When /^user "([^"]*)" downloads the preview of "([^"]*)" of the space "([^"]*)" with width "([^"]*)" and height "([^"]*)" using the WebDAV API$/
*
* @param string $user
* @param string $fileName
* @param string $spaceName
* @param string $width
* @param string $height
*
* @throws GuzzleException
*/
public function downloadPreview(
string $user,
string $fileName,
string $spaceName,
string $width,
string $height
): void {
$eTag = str_replace("\"", "", $this->getETag($user, $spaceName, $fileName));
$urlParameters = [
'scalingup' => 0,
'preview' => '1',
'a' => '1',
'c' => $eTag,
'x' => $width,
'y' => $height
];
$urlParameters = \http_build_query($urlParameters, '', '&');
$space = $this->getSpaceByName($user, $spaceName);
$fullUrl = $this->baseUrl . $this->davSpacesUrl . $space['id'] . '/' . $fileName . '?' . $urlParameters;
$this->featureContext->setResponse(
HttpRequestHelper::get(
$fullUrl,
"",
$user,
$this->featureContext->getPasswordForUser($user)
)
);
}
}
@@ -0,0 +1,152 @@
<?php
declare(strict_types=1);
/**
* ownCloud
*
* @author Viktor Scharf <v.scharf@owncloud.com>
* @copyright Copyright (c) 2022 Viktor Scharf v.scharf@owncloud.com
*/
use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use GuzzleHttp\Exception\GuzzleException;
use TestHelpers\HttpRequestHelper;
require_once 'bootstrap.php';
/**
* Context for the provisioning specific steps using the Graph API
*/
class TusContext implements Context
{
/**
* @var FeatureContext
*/
private FeatureContext $featureContext;
/**
* @var SpacesContext
*/
private SpacesContext $spacesContext;
/**
* @var string
*/
private string $baseUrl;
/**
* @return string
*/
public function acceptanceTestsDirLocation(): string
{
return \dirname(__FILE__) . "/../../filesForUpload/";
}
/**
* This will run before EVERY scenario.
* It will set the properties for this object.
*
* @BeforeScenario
*
* @param BeforeScenarioScope $scope
*
* @return void
*/
public function before(BeforeScenarioScope $scope): void
{
// Get the environment
$environment = $scope->getEnvironment();
// Get all the contexts you need in this context from here
$this->featureContext = $environment->getContext('FeatureContext');
$this->spacesContext = $environment->getContext('SpacesContext');
$this->baseUrl = \trim($this->featureContext->getBaseUrl(), "/");
}
/**
* @Given /^user "([^"]*)" has uploaded a file "([^"]*)" via TUS inside of the space "([^"]*)" using WebDAV API$/
*
* @param string $user
* @param string $resource
* @param string $spaceName
*
* @return void
*
* @throws Exception
* @throws GuzzleException
*/
public function uploadFileViaTus(string $user, string $resource, string $spaceName): void
{
$resourceLocation = $this->getResourceLocation($user, $resource, $spaceName);
$file = \fopen($this->acceptanceTestsDirLocation() . $resource, 'r');
$this->featureContext->setResponse(
HttpRequestHelper::sendRequest(
$resourceLocation,
"",
'HEAD',
$user,
$this->featureContext->getPasswordForUser($user),
[],
""
)
);
$this->featureContext->theHTTPStatusCodeShouldBe(200, "Expected response status code should be 200");
$this->featureContext->setResponse(
HttpRequestHelper::sendRequest(
$resourceLocation,
"",
'PATCH',
$user,
$this->featureContext->getPasswordForUser($user),
["Tus-Resumable" => "1.0.0", "Upload-Offset" => 0, 'Content-Type' => 'application/offset+octet-stream'],
$file
)
);
$this->featureContext->theHTTPStatusCodeShouldBe(204, "Expected response status code should be 204");
}
/**
* send POST and return the url of the resource location in the response header
*
* @param string $user
* @param string $resource
* @param string $spaceName
*
* @return string
*/
public function getResourceLocation(string $user, string $resource, string $spaceName): string
{
$space = $this->spacesContext->getSpaceByName($user, $spaceName);
$fullUrl = $this->baseUrl . "/remote.php/dav/spaces/" . $space["id"];
$tusEndpoint = "tusEndpoint " . base64_encode(str_replace("$", "%", $fullUrl));
$fileName = "filename " . base64_encode($resource);
$headers = [
"Tus-Resumable" => "1.0.0",
"Upload-Metadata" => $tusEndpoint . ',' . $fileName,
"Upload-Length" => filesize($this->acceptanceTestsDirLocation() . $resource)
];
$this->featureContext->setResponse(
HttpRequestHelper::post(
$fullUrl,
"",
$this->featureContext->getActualUsername($user),
$this->featureContext->getUserPassword($user),
$headers,
''
)
);
$this->featureContext->theHTTPStatusCodeShouldBe(201, "Expected response status code should be 201");
$locationHeader = $this->featureContext->getResponse()->getHeader('Location');
if (\sizeof($locationHeader) > 0) {
return $locationHeader[0];
}
}
}