Skip to content

Commit

Permalink
[tests-only] tests: refactor test script for removing permission from…
Browse files Browse the repository at this point in the history
… share (#8134)

* tests: refactor test script for removing permission from share

* address reviews

---------

Co-authored-by: Saw-jan <[email protected]>
  • Loading branch information
SwikritiT and saw-jan authored Jan 9, 2024
1 parent c3f7554 commit 954a1c0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 55 deletions.
40 changes: 1 addition & 39 deletions tests/TestHelpers/GraphHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1540,44 +1540,6 @@ public static function getPermissionsList(
);
}

/**
* @param string $baseUrl
* @param string $xRequestId
* @param string $sharer
* @param string $userIdOfShareeUser this is a uuidv4 of sharee
* @param string $password
* @param string $spaceId
* @param string $itemId
* @param string $shareType (user|group)
*
* @return string
*
* @throws GuzzleException
* @throws \JsonException
*/
public static function getSharePermissionId(
string $baseUrl,
string $xRequestId,
string $sharer,
string $userIdOfShareeUser,
string $password,
string $spaceId,
string $itemId,
string $shareType
): string {
$response = self::getPermissionsList($baseUrl, $xRequestId, $sharer, $password, $spaceId, $itemId);
$permissionList = json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);
foreach ($permissionList["value"] as $value) {
if ($value["grantedToV2"][$shareType]["id"] === $userIdOfShareeUser) {
return $value["id"];
}
}
throw new \Exception(
__METHOD__
. " Cannot find share permission id for user"
);
}

/**
* Get the role id by name
*
Expand Down Expand Up @@ -1787,7 +1749,7 @@ public static function setPassword(
*
* @throws GuzzleException
*/
public static function removeSharePermission(
public static function deleteSharePermission(
string $baseUrl,
string $xRequestId,
string $user,
Expand Down
34 changes: 33 additions & 1 deletion tests/acceptance/features/bootstrap/Sharing.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ trait Sharing {
*/
private array $shareNgCreatedLinkShares = [];

/**
* @var array
*/
private array $shareNgCreatedUserGroupShares = [];

/**
* @return string
*/
Expand Down Expand Up @@ -119,6 +124,22 @@ public function shareNgGetLastCreatedLinkShare():?ResponseInterface {
return \end($this->shareNgCreatedLinkShares);
}

/**
* @param ResponseInterface $response
*
* @return void
*/
public function shareNgAddToCreatedUserGroupShares(ResponseInterface $response): void {
$this->shareNgCreatedUserGroupShares[] = $response;
}

/**
* @return ResponseInterface|null
*/
public function shareNgGetLastCreatedUserGroupShare():?ResponseInterface {
return \end($this->shareNgCreatedUserGroupShares);
}

/**
* @param string $sharer
* @param SimpleXMLElement $shareData
Expand Down Expand Up @@ -179,7 +200,7 @@ public function getSharesEndpointPath(?string $postfix = ''):string {
public function shareNgGetLastCreatedLinkShareID(): string {
$lastResponse = $this->shareNgGetLastCreatedLinkShare();
if (!isset($this->getJsonDecodedResponse($lastResponse)['id'])) {
throw new Error('Response did not contain share id ' . $this->getJsonDecodedResponse($lastResponse)['id'] . ' for the created public link');
throw new Error('Response did not contain share id for the created public link');
}
return $this->getJsonDecodedResponse($lastResponse)['id'];
}
Expand All @@ -196,6 +217,17 @@ public function shareNgGetLastCreatedLinkShareToken(): string {
return substr(strrchr($last_created_link_webURL, "/"), 1);
}

/**
* @return string
*/
public function shareNgGetLastCreatedUserGroupShareID(): string {
$lastResponse = $this->shareNgGetLastCreatedUserGroupShare();
if (!isset($this->getJsonDecodedResponse($lastResponse)['value'][0]['id'])) {
throw new Error('Response did not contain share id for the last created share.');
}
return $this->getJsonDecodedResponse($lastResponse)['value'][0]['id'];
}

/**
* Split given permissions string each separated with "," into array of strings
*
Expand Down
22 changes: 7 additions & 15 deletions tests/acceptance/features/bootstrap/SharingNgContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function sendShareInvitation(string $user, TableNode $table): ResponseInt
$permission = $rows['permission'] ?? null;
$expireDate = $rows["expireDate"] ?? null;

return GraphHelper::sendSharingInvitation(
$response = GraphHelper::sendSharingInvitation(
$this->featureContext->getBaseUrl(),
$this->featureContext->getStepLineRef(),
$user,
Expand All @@ -165,6 +165,10 @@ public function sendShareInvitation(string $user, TableNode $table): ResponseInt
$permission,
$expireDate
);
if ($response->getStatusCode() === 200) {
$this->featureContext->shareNgAddToCreatedUserGroupShares($response);
}
return $response;
}

/**
Expand Down Expand Up @@ -351,21 +355,9 @@ public function userRemovesSharePermissionOfUserFromResourceOfSpaceUsingGraphAPI
$itemId = ($resourceType === 'folder')
? $this->spacesContext->getResourceId($sharer, $space, $resource)
: $this->spacesContext->getFileId($sharer, $space, $resource);
$userIdOfSharee = ($shareType === 'user')
? $this->featureContext->getUserIdByUserName($sharee)
: $this->featureContext->getGroupIdByGroupName($sharee);
$permId = GraphHelper::getSharePermissionId(
$this->featureContext->getBaseUrl(),
$this->featureContext->getStepLineRef(),
$sharer,
$userIdOfSharee,
$this->featureContext->getPasswordForUser($sharer),
$spaceId,
$itemId,
$shareType
);
$permId = $this->featureContext->shareNgGetLastCreatedUserGroupShareID();
$this->featureContext->setResponse(
GraphHelper::removeSharePermission(
GraphHelper::deleteSharePermission(
$this->featureContext->getBaseUrl(),
$this->featureContext->getStepLineRef(),
$sharer,
Expand Down

0 comments on commit 954a1c0

Please sign in to comment.