add test to upload via TUS by federated user

Signed-off-by: prashant-gurung899 <prasantgrg777@gmail.com>
This commit is contained in:
prashant-gurung899
2024-11-22 09:47:08 +05:45
parent 271455703b
commit e40015c2e2
7 changed files with 150 additions and 26 deletions
+66 -3
View File
@@ -254,6 +254,38 @@ class SpacesContext implements Context {
return $space["id"];
}
/**
* @param string $user
* @param string $share
*
* @return string
*
* @throws Exception|GuzzleException
*/
public function getSharesRemoteItemId(string $user, string $share): string {
$credentials = $this->featureContext->graphContext->getAdminOrUserCredentials($user);
$response = GraphHelper::getSharesSharedWithMe(
$this->featureContext->getBaseUrl(),
$this->featureContext->getStepLineRef(),
$credentials['username'],
$credentials['password']
);
$jsonBody = $this->featureContext->getJsonDecodedResponseBodyContent($response);
// Search remoteItem ID of a given share
foreach ($jsonBody->value as $item) {
if (isset($item->name) && $item->name === $share) {
if (isset($item->remoteItem->id)) {
return $item->remoteItem->id;
}
throw new Exception("Failed to find remoteItem ID for share: $share");
}
}
throw new Exception("Cannot find share: $share");
}
/**
* The method finds file by fileName and spaceName and returns data of file which contains in responseHeader
* fileName contains the path, if the file is in the folder
@@ -261,14 +293,23 @@ class SpacesContext implements Context {
* @param string $user
* @param string $spaceName
* @param string $fileName
* @param bool $federatedShare
*
* @return ResponseInterface
* @throws GuzzleException
*/
public function getFileData(string $user, string $spaceName, string $fileName): ResponseInterface {
$space = $this->getSpaceByName($user, $spaceName);
public function getFileData(string $user, string $spaceName, string $fileName, bool $federatedShare = false): ResponseInterface {
$baseUrl = $this->featureContext->getBaseUrl();
$davPath = WebdavHelper::getDavPath(WebDavHelper::DAV_VERSION_SPACES, $space["id"]);
if ($federatedShare) {
$remoteItemId = $this->getSharesRemoteItemId($user, $spaceName);
$spaceId = \rawurlencode($remoteItemId);
} else {
$space = $this->getSpaceByName($user, $spaceName);
$spaceId = $space["id"];
}
$davPath = WebdavHelper::getDavPath(WebDavHelper::DAV_VERSION_SPACES, $spaceId);
$fullUrl = "$baseUrl/$davPath/$fileName";
return HttpRequestHelper::get(
@@ -874,6 +915,28 @@ class SpacesContext implements Context {
Assert::assertEquals($fileContent, $actualFileContent, "$file does not contain $fileContent");
}
/**
* @Then /^for user "([^"]*)" the content of file "([^"]*)" of federated share "([^"]*)" should be "([^"]*)"$/
*
* @param string $user
* @param string $file
* @param string $share
* @param string $fileContent
*
* @return void
*
* @throws Exception|GuzzleException
*/
public function forUserTheContentOfFileOfFederatedShareShouldBe(
string $user,
string $file,
string $share,
string $fileContent
): void {
$actualFileContent = $this->getFileData($user, $share, $file, true)->getBody()->getContents();
Assert::assertEquals($fileContent, $actualFileContent, "File content did not match");
}
/**
* @Then /^the JSON response should contain space called "([^"]*)" (?:|(?:owned by|granted to) "([^"]*)" )(?:|(?:with description file|with space image) "([^"]*)" )and match$/
*
@@ -16,6 +16,8 @@ use GuzzleHttp\Exception\GuzzleException;
use PHPUnit\Framework\Assert;
use TestHelpers\WebDavHelper;
use TestHelpers\BehatHelper;
use TestHelpers\GraphHelper;
use TestHelpers\HttpRequestHelper;
require_once 'bootstrap.php';
@@ -89,11 +91,10 @@ class SpacesTUSContext implements Context {
}
/**
* @Given user :user has created a new TUS resource for the space :spaceName with content :content using the WebDAV API with these headers:
* @Given user :user has created a new TUS resource in the space :spaceName with the following headers:
*
* @param string $user
* @param string $spaceName
* @param string $content
* @param TableNode $headers
*
* @return void
@@ -104,11 +105,10 @@ class SpacesTUSContext implements Context {
public function userHasCreatedANewTusResourceForTheSpaceUsingTheWebdavApiWithTheseHeaders(
string $user,
string $spaceName,
string $content,
TableNode $headers
): void {
$spaceId = $this->spacesContext->getSpaceIdByName($user, $spaceName);
$response = $this->tusContext->createNewTUSResourceWithHeaders($user, $headers, $content, $spaceId);
$response = $this->tusContext->createNewTUSResourceWithHeaders($user, $headers, '', $spaceId);
$this->featureContext->theHTTPStatusCodeShouldBe(201, "Expected response status code should be 201", $response);
}
@@ -164,6 +164,31 @@ class SpacesTUSContext implements Context {
\unlink($tmpFile);
}
/**
* @When /^user "([^"]*)" uploads a file with content "([^"]*)" to "([^"]*)" inside federated share "([^"]*)" via TUS using the WebDAV API$/
*
* @param string $user
* @param string $content
* @param string $file
* @param string $destination
*
* @return void
* @throws Exception|GuzzleException
*/
public function userUploadsAFileWithContentToInsideFederatedShareViaTusUsingTheWebdavApi(string $user, string $content, string $file, string $destination): void {
$remoteItemId = $this->spacesContext->getSharesRemoteItemId($user, $destination);
$remoteItemId = \rawurlencode($remoteItemId);
$tmpFile = $this->tusContext->writeDataToTempFile($content);
$this->tusContext->uploadFileUsingTus(
$user,
\basename($tmpFile),
$file,
$remoteItemId
);
$this->featureContext->setLastUploadDeleteTime(\time());
\unlink($tmpFile);
}
/**
* @When /^user "([^"]*)" uploads a file with content "([^"]*)" to "([^"]*)" via TUS inside of the space "([^"]*)" using the WebDAV API$/
*
@@ -330,11 +355,10 @@ class SpacesTUSContext implements Context {
}
/**
* @When /^user "([^"]*)" sends a chunk to the last created TUS Location with data "([^"]*)" inside of the space "([^"]*)" with headers:$/
* @When /^user "([^"]*)" sends a chunk to the last created TUS Location with data "([^"]*)" with the following headers:$/
*
* @param string $user
* @param string $data
* @param string $spaceName
* @param TableNode $headers
*
* @return void
@@ -343,7 +367,6 @@ class SpacesTUSContext implements Context {
public function userSendsAChunkToTheLastCreatedTusLocationWithDataInsideOfTheSpaceWithHeaders(
string $user,
string $data,
string $spaceName,
TableNode $headers
): void {
$rows = $headers->getRowsHash();