Skip to content

Commit

Permalink
added test coverage for previewing shared resources using spaces dav …
Browse files Browse the repository at this point in the history
…path
  • Loading branch information
PrajwolAmatya committed Oct 6, 2023
1 parent 9cee6bc commit 8aef11c
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 7 deletions.
41 changes: 41 additions & 0 deletions tests/TestHelpers/GraphHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1426,4 +1426,45 @@ public static function unassignRole(
self::getRequestHeaders(),
);
}

/**
* @param string $baseUrl
* @param string $xRequestId
* @param string $user
* @param string $password
* @param string $path
*
* @return string
* @throws GuzzleException
* @throws Exception
*/
public static function getShareMountId(
string $baseUrl,
string $xRequestId,
string $user,
string $password,
string $path
): string {
$response = self::getMySpaces(
$baseUrl,
$user,
$password,
'',
$xRequestId
);
$drives = json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);

// the response returns the shared resource in driveAlias all in lowercase,
// For example: if we get the property of a shared resource "FOLDER" then the response contains "driveAlias": "mountpoint/folder"
$path = strtolower($path);
foreach ($drives["value"] as $value) {
if ($value["driveAlias"] === "mountpoint/" . $path) {
return $value["id"];
}
}
throw new \Exception(
__METHOD__
. " Cannot find share mountpoint id of '$path' for user '$user'"
);
}
}
81 changes: 74 additions & 7 deletions tests/acceptance/features/bootstrap/WebDav.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use TestHelpers\WebDavHelper;
use TestHelpers\HttpRequestHelper;
use TestHelpers\Asserts\WebDav as WebDavAssert;
use TestHelpers\GraphHelper;

/**
* WebDav functions
Expand Down Expand Up @@ -471,7 +472,7 @@ public function downloadPreviews(string $user, ?string $path, ?string $doDavRequ
[],
null,
"files",
'2',
null,
false,
null,
$urlParameter,
Expand Down Expand Up @@ -3032,7 +3033,11 @@ public function userUploadsAFileWithContentTo(
?string $content,
string $destination
):void {
$this->uploadFileWithContent($user, $content, $destination);
if ($this->getDavPathVersion() === 3 && str_contains($destination, 'Shares')) {
$this->setResponse($this->userDownloadsOrUploadsSharedResource($user, $destination, 'PUT', null, null, $content));
} else {
$this->uploadFileWithContent($user, $content, $destination);
}
$this->pushToLastHttpStatusCodesArray();
}

Expand Down Expand Up @@ -4637,14 +4642,76 @@ public function userHasDeletedEverythingInFolder(string $user, string $folder):v
* @return void
*/
public function downloadPreviewOfFiles(string $user, string $path, string $width, string $height):void {
$response = $this->downloadPreviews(
if ($this->getDavPathVersion() === 3 && str_contains($path, 'Shares')) {
$this->setResponse($this->userDownloadsOrUploadsSharedResource($user, $path, 'GET', $width, $height));
} else {
$this->setResponse($this->downloadPreviews($user, $path, null, $width, $height));
}
}

/**
* @param string $user
* @param string $path
* @param string $method
* @param string|null $width
* @param string|null $height
* @param string|null $content
*
* @return ResponseInterface
* @throws GuzzleException
*/
public function userDownloadsOrUploadsSharedResource(
string $user,
string $path,
string $method,
?string $width,
?string $height,
?string $content = null
): ResponseInterface {
$user = $this->getActualUsername($user);
$path = trim($path, "/");
$pathArray = explode("/", $path);

$shareMountId = GraphHelper::getShareMountId(
$this->getBaseUrl(),
$this->getStepLineRef(),
$user,
$path,
$this->getPasswordForUser($user),
$pathArray[1]
);

if (\count($pathArray) > 2) {
$pathArray = \array_slice($pathArray, 2);
$path = '/' . implode("/", array_map("strval", $pathArray));
} else {
$path = null;
}

if ($width !== null && $height !== null) {
$urlParameter = [
'x' => $width,
'y' => $height,
'forceIcon' => '0',
'preview' => '1'
];
$urlParameter = \http_build_query($urlParameter, '', '&');
} else {
$urlParameter = null;
}
$sharesPath = $shareMountId . $path . '/?' . $urlParameter;

$davPath = WebDavHelper::getDavPath($user, $this->getDavPathVersion());
$fullUrl = $this->getBaseUrl() . $davPath . $sharesPath;

return HttpRequestHelper::sendRequest(
$fullUrl,
$this->getStepLineRef(),
$method,
$user,
$this->getPasswordForUser($user),
null,
$width,
$height
$content
);
$this->setResponse($response);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ Feature: previews of files downloaded through the webdav API
| old | example.gif |
| new | lorem.txt |
| new | example.gif |
| spaces | lorem.txt |
| spaces | example.gif |


Scenario Outline: user tries to download previews of other users files
Expand Down Expand Up @@ -221,6 +223,7 @@ Feature: previews of files downloaded through the webdav API
| dav-path-version |
| old |
| new |
| spaces |


Scenario Outline: it should update the preview content if the file content is updated (content with UTF chars)
Expand Down Expand Up @@ -259,6 +262,7 @@ Feature: previews of files downloaded through the webdav API
| dav-path-version |
| old |
| new |
| spaces |


Scenario Outline: updates to a group shared file should change the preview for both sharees and sharers
Expand Down Expand Up @@ -290,3 +294,4 @@ Feature: previews of files downloaded through the webdav API
| dav-path-version |
| old |
| new |
| spaces |

0 comments on commit 8aef11c

Please sign in to comment.