Skip to content

Commit

Permalink
tests: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
saw-jan committed Dec 4, 2024
1 parent 71c4806 commit dee504f
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 111 deletions.
2 changes: 2 additions & 0 deletions tests/acceptance/TestHelpers/GraphHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class GraphHelper {
'Space Editor Without Versions' => '3284f2d5-0070-4ad8-ac40-c247f7c1fb27',
];

public const SHARES_SPACE_ID = 'a0ca6a90-a365-4782-871e-d44447bbc668$a0ca6a90-a365-4782-871e-d44447bbc668';

/**
* @return string[]
*/
Expand Down
80 changes: 21 additions & 59 deletions tests/acceptance/TestHelpers/WebDavHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,35 +504,6 @@ public static function generateUUIDv4():string {
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}

/**
*
* @param string $baseUrl
* @param string $user
* @param string $password
* @param string $xRequestId
*
* @return string
* @throws GuzzleException
* @throws Exception
*/
public static function getSharesSpaceIdForUser(string $baseUrl, string $user, string $password, string $xRequestId): string {
if (\array_key_exists($user, self::$spacesIdRef) && \array_key_exists("virtual", self::$spacesIdRef[$user])) {
return self::$spacesIdRef[$user]["virtual"];
}

$response = GraphHelper::getMySpaces($baseUrl, $user, $password, '', $xRequestId);
$body = HttpRequestHelper::getJsonDecodedResponseBodyContent($response);

$spaceId = null;
foreach ($body->value as $spaces) {
if ($spaces->driveType === "virtual") {
$spaceId = $spaces->id;
break;
}
}
return $spaceId;
}

/**
* fetches personal space id for provided user
*
Expand All @@ -558,10 +529,18 @@ public static function getPersonalSpaceIdForUser(string $baseUrl, string $user,
$user,
$password
);
$bodyContents = $response->getBody()->getContents();
$json = \json_decode($bodyContents);
Assert::assertEquals(200, $response->getStatusCode(), "Cannot list drives for user '$user'");

$personalSpaceId = '';
if ($json === null) {
$drives = HttpRequestHelper::getJsonDecodedResponseBodyContent($response);
foreach ($drives->value as $drive) {
if ($drive->driveType === "personal") {
$personalSpaceId = $drive->id;
break;
}
}

if (!$personalSpaceId) {
// the graph endpoint did not give a useful answer
// try getting the information from the webdav endpoint
$fullUrl = "$trimmedBaseUrl/" . self::getDavPath(self::DAV_VERSION_NEW, $user);
Expand All @@ -582,13 +561,6 @@ public static function getPersonalSpaceIdForUser(string $baseUrl, string $user,
Assert::assertNotEmpty($xmlPart, "The 'oc:spaceid' for user '$user' was not found in the PROPFIND response");

$personalSpaceId = $xmlPart[0]->__toString();
} else {
foreach ($json->value as $spaces) {
if ($spaces->driveType === "personal") {
$personalSpaceId = $spaces->id;
break;
}
}
}

Assert::assertNotEmpty($personalSpaceId, "The personal space id for user '$user' was not found");
Expand All @@ -611,21 +583,16 @@ public static function getPersonalSpaceIdForUser(string $baseUrl, string $user,
* @throws Exception|GuzzleException
*/
public static function getPersonalSpaceIdForUserOrFakeIfNotFound(string $baseUrl, string $user, string $password, string $xRequestId):string {
try {
$spaceId = self::getPersonalSpaceIdForUser(
$baseUrl,
$user,
$password,
$xRequestId,
);
} catch (SpaceNotFoundException $e) {
// if the fetch fails, and the user is not found, then a fake space id is prepared
// this is useful for testing when the personal space is of a non-existing user
$fakeSpaceId = self::generateUUIDv4();
self::$spacesIdRef[$user]["personal"] = $fakeSpaceId;
$spaceId = $fakeSpaceId;
if (\str_starts_with($user, "non-exist") || \str_starts_with($user, "nonexist")) {
return self::generateUUIDv4();
}
return $spaceId;

return self::getPersonalSpaceIdForUser(
$baseUrl,
$user,
$password,
$xRequestId,
);
}

/**
Expand Down Expand Up @@ -692,12 +659,7 @@ public static function makeDavRequest(
if ($spaceId === null && $davPathVersionToUse === self::DAV_VERSION_SPACES && !\in_array($type, ["public-files", "versions"])) {
$path = \ltrim($path, "/");
if (\str_starts_with($path, "Shares/")) {
$spaceId = self::getSharesSpaceIdForUser(
$baseUrl,
$user,
$password,
$xRequestId
);
$spaceId = GraphHelper::SHARES_SPACE_ID;
$path = "/" . preg_replace("/^Shares\//", "", $path);
} else {
$spaceId = self::getPersonalSpaceIdForUserOrFakeIfNotFound(
Expand Down
1 change: 0 additions & 1 deletion tests/acceptance/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ public function rememberUserAutoSyncSetting(string $user, bool $value): void {
$this->autoSyncSettings[$user] = $value;
}

public const SHARES_SPACE_ID = 'a0ca6a90-a365-4782-871e-d44447bbc668$a0ca6a90-a365-4782-871e-d44447bbc668';
private bool $useSharingNG = false;

/**
Expand Down
61 changes: 46 additions & 15 deletions tests/acceptance/bootstrap/SharingNgContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,7 @@ public function userHasRemovedAccessOfUserOrGroupFromSpace(
* @throws JsonException
*/
public function hideOrUnhideSharedResource(string $sharee, string $shareID, bool $hide = true): ResponseInterface {
$shareSpaceId = FeatureContext::SHARES_SPACE_ID;
$shareSpaceId = GraphHelper::SHARES_SPACE_ID;
$itemId = $shareSpaceId . '!' . $shareID;
$body['@UI.Hidden'] = $hide;
return GraphHelper::hideOrUnhideShare(
Expand All @@ -1148,7 +1148,7 @@ public function hideOrUnhideSharedResource(string $sharee, string $shareID, bool
*/
public function userHasDisabledSyncOfLastSharedResource(string $user):void {
$shareItemId = $this->featureContext->shareNgGetLastCreatedUserGroupShareID();
$shareSpaceId = FeatureContext::SHARES_SPACE_ID;
$shareSpaceId = GraphHelper::SHARES_SPACE_ID;
$itemId = $shareSpaceId . '!' . $shareItemId;
$response = GraphHelper::disableShareSync(
$this->featureContext->getBaseUrl(),
Expand All @@ -1172,7 +1172,7 @@ public function userHasDisabledSyncOfLastSharedResource(string $user):void {
*/
public function userDisablesSyncOfShareUsingTheGraphApi(string $user):void {
$shareItemId = $this->featureContext->shareNgGetLastCreatedUserGroupShareID();
$shareSpaceId = FeatureContext::SHARES_SPACE_ID;
$shareSpaceId = GraphHelper::SHARES_SPACE_ID;
$itemId = $shareSpaceId . '!' . $shareItemId;
$response = GraphHelper::disableShareSync(
$this->featureContext->getBaseUrl(),
Expand Down Expand Up @@ -1247,7 +1247,7 @@ public function userUnhidesTheSharedResourceUsingTheGraphApi(string $user):void
public function userEnablesSyncOfShareUsingTheGraphApi(string $user, string $share, string $offeredBy, string $space):void {
$share = ltrim($share, '/');
$itemId = $this->spacesContext->getResourceId($offeredBy, $space, $share);
$shareSpaceId = FeatureContext::SHARES_SPACE_ID;
$shareSpaceId = GraphHelper::SHARES_SPACE_ID;
$response = GraphHelper::enableShareSync(
$this->featureContext->getBaseUrl(),
$this->featureContext->getStepLineRef(),
Expand All @@ -1272,7 +1272,7 @@ public function userEnablesSyncOfShareUsingTheGraphApi(string $user, string $sha
* @throws Exception|GuzzleException
*/
public function userTriesToEnableShareSyncOfResourceUsingTheGraphApi(string $user, string $resource):void {
$shareSpaceId = FeatureContext::SHARES_SPACE_ID;
$shareSpaceId = GraphHelper::SHARES_SPACE_ID;
$itemId = ($resource === 'nonexistent') ? WebDavHelper::generateUUIDv4() : $resource;

$response = GraphHelper::enableShareSync(
Expand All @@ -1296,7 +1296,7 @@ public function userTriesToEnableShareSyncOfResourceUsingTheGraphApi(string $use
* @throws Exception|GuzzleException
*/
public function userTriesToDisableShareSyncOfResourceUsingTheGraphApi(string $user, string $resource):void {
$shareSpaceId = FeatureContext::SHARES_SPACE_ID;
$shareSpaceId = GraphHelper::SHARES_SPACE_ID;
$shareID = ($resource === 'nonexistent') ? WebDavHelper::generateUUIDv4() : $resource;
$itemId = $shareSpaceId . '!' . $shareID;
$response = GraphHelper::disableShareSync(
Expand Down Expand Up @@ -1736,13 +1736,22 @@ public function userRemovesTheLastLinkShareOfSpaceUsingPermissionsEndpointOfGrap
* @param string $share
* @param string $sharee
* @param string $sharer
* @param string $space
* @param bool $shouldExist
*
* @return void
* @throws GuzzleException
* @throws JsonException
* @throws Exception
*/
public function checkIfShareExists(string $share, string $sharee, string $sharer): void {
public function checkIfShareExists(string $share, string $sharee, string $sharer, string $space, bool $shouldExist = true): void {
$share = \ltrim($share, "/");
if (\strtolower($space) === "personal") {
$remoteDriveAlias = "personal/" . \strtolower($sharer);
} else {
$remoteDriveAlias = "project/" . \strtolower($space);
}

// check share mountpoint
$response = GraphHelper::getMySpaces(
$this->featureContext->getBaseUrl(),
Expand All @@ -1754,11 +1763,11 @@ public function checkIfShareExists(string $share, string $sharee, string $sharer
$driveList = HttpRequestHelper::getJsonDecodedResponseBodyContent($response)->value;
$foundShareMountpoint = false;
foreach ($driveList as $drive) {
if ($drive->driveType === "mountpoint" && $drive->name === $share && $drive->root->remoteItem->driveAlias === "personal/" . \strtolower($sharer)) {
if ($drive->driveType === "mountpoint" && $drive->name === $share && $drive->root->remoteItem->driveAlias === $remoteDriveAlias) {
$foundShareMountpoint = true;
}
}
Assert::assertTrue($foundShareMountpoint, "Share mountpoint '$share' was not found in the drives list.");
Assert::assertSame($shouldExist, $foundShareMountpoint, "Share mountpoint '$share' was not found in the drives list.");

// check share in shared-with-me list
$response = GraphHelper::getSharesSharedWithMe(
Expand All @@ -1770,24 +1779,46 @@ public function checkIfShareExists(string $share, string $sharee, string $sharer
$sharedWithMeList = HttpRequestHelper::getJsonDecodedResponseBodyContent($response)->value;
$foundShareInSharedWithMe = false;
foreach ($sharedWithMeList as $item) {
if ($item->name === $share && $item->createdBy->user->displayName === $this->featureContext->getDisplayNameForUser($sharer)) {
$foundShareInSharedWithMe = true;
if ($item->name === $share) {
foreach ($item->remoteItem->permissions as $permission) {
$shareCreator = $permission->invitation->invitedBy->user->displayName;
if ($shareCreator === $this->featureContext->getDisplayNameForUser($sharer)) {
$foundShareInSharedWithMe = true;
break;
}
}
break;
}
}
Assert::assertTrue($foundShareInSharedWithMe, "Share '$share' was not found in the shared-with-me list");
Assert::assertSame($shouldExist, $foundShareInSharedWithMe, "Share '$share' was not found in the shared-with-me list");
}

/**
* @Then user :sharee should have a share :share shared by user :sharer
* @Then user :sharee should have a share :share shared by user :sharer from space :space
*
* @param string $sharee
* @param string $share
* @param string $sharer
* @param string $space
*
* @return void
*/
public function userShouldHaveShareSharedByUserFromSpace(string $sharee, string $share, string $sharer, string $space): void {
$this->checkIfShareExists($share, $sharee, $sharer, $space);
}

/**
* @Then user :sharee should not have a share :share shared by user :sharer from space :space
*
* @param string $sharee
* @param string $share
* @param string $sharer
* @param string $space
*
* @return void
*/
public function userShouldHaveShare(string $sharee, string $share, string $sharer): void {
$this->checkIfShareExists($share, $sharee, $sharer);
public function userShouldNotHaveShareSharedByUserFromSpace(string $sharee, string $share, string $sharer, string $space): void {
$this->checkIfShareExists($share, $sharee, $sharer, $space, false);
}

/**
Expand Down
Loading

0 comments on commit dee504f

Please sign in to comment.