From 6043ddfb922f61467361c71c575b2696c574195b Mon Sep 17 00:00:00 2001 From: Swikriti Tripathi Date: Fri, 5 Jan 2024 12:20:22 +0545 Subject: [PATCH 1/2] tests: refactor test script for removing permission from share --- tests/TestHelpers/GraphHelper.php | 40 +------------------ .../acceptance/features/bootstrap/Sharing.php | 34 +++++++++++++++- .../features/bootstrap/SharingNgContext.php | 22 ++++------ 3 files changed, 41 insertions(+), 55 deletions(-) diff --git a/tests/TestHelpers/GraphHelper.php b/tests/TestHelpers/GraphHelper.php index cb0989b8494..5e4a0805e7e 100644 --- a/tests/TestHelpers/GraphHelper.php +++ b/tests/TestHelpers/GraphHelper.php @@ -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 * @@ -1787,7 +1749,7 @@ public static function setPassword( * * @throws GuzzleException */ - public static function removeSharePermission( + public static function deleteSharePermission( string $baseUrl, string $xRequestId, string $user, diff --git a/tests/acceptance/features/bootstrap/Sharing.php b/tests/acceptance/features/bootstrap/Sharing.php index b4504272241..95be73efcc3 100755 --- a/tests/acceptance/features/bootstrap/Sharing.php +++ b/tests/acceptance/features/bootstrap/Sharing.php @@ -80,6 +80,11 @@ trait Sharing { */ private array $shareNgCreatedLinkShares = []; + /** + * @var array + */ + private array $shareNgCreatedUserGroupShares = []; + /** * @return string */ @@ -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 @@ -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']; } @@ -196,6 +217,17 @@ public function shareNgGetLastCreatedLinkShareToken(): string { return substr(strrchr($last_created_link_webURL, "/"), 1); } + /** + * @return string + */ + public function shareNgGetLastCreatedUserShareShareID(): 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 * diff --git a/tests/acceptance/features/bootstrap/SharingNgContext.php b/tests/acceptance/features/bootstrap/SharingNgContext.php index ef377fb821f..2fa8ce9118f 100644 --- a/tests/acceptance/features/bootstrap/SharingNgContext.php +++ b/tests/acceptance/features/bootstrap/SharingNgContext.php @@ -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, @@ -165,6 +165,10 @@ public function sendShareInvitation(string $user, TableNode $table): ResponseInt $permission, $expireDate ); + if ($response->getStatusCode() === 200) { + $this->featureContext->shareNgAddToCreatedUserGroupShares($response); + } + return $response; } /** @@ -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->shareNgGetLastCreatedUserShareShareID(); $this->featureContext->setResponse( - GraphHelper::removeSharePermission( + GraphHelper::deleteSharePermission( $this->featureContext->getBaseUrl(), $this->featureContext->getStepLineRef(), $sharer, From 87e511648f8a595ffb9c227c5576ed791c763cee Mon Sep 17 00:00:00 2001 From: Saw-jan Date: Mon, 8 Jan 2024 13:09:44 +0545 Subject: [PATCH 2/2] address reviews --- tests/acceptance/features/bootstrap/Sharing.php | 2 +- tests/acceptance/features/bootstrap/SharingNgContext.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/acceptance/features/bootstrap/Sharing.php b/tests/acceptance/features/bootstrap/Sharing.php index 95be73efcc3..0b6ec311a31 100755 --- a/tests/acceptance/features/bootstrap/Sharing.php +++ b/tests/acceptance/features/bootstrap/Sharing.php @@ -220,7 +220,7 @@ public function shareNgGetLastCreatedLinkShareToken(): string { /** * @return string */ - public function shareNgGetLastCreatedUserShareShareID(): 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.'); diff --git a/tests/acceptance/features/bootstrap/SharingNgContext.php b/tests/acceptance/features/bootstrap/SharingNgContext.php index 2fa8ce9118f..4a9a9bed7c1 100644 --- a/tests/acceptance/features/bootstrap/SharingNgContext.php +++ b/tests/acceptance/features/bootstrap/SharingNgContext.php @@ -355,7 +355,7 @@ public function userRemovesSharePermissionOfUserFromResourceOfSpaceUsingGraphAPI $itemId = ($resourceType === 'folder') ? $this->spacesContext->getResourceId($sharer, $space, $resource) : $this->spacesContext->getFileId($sharer, $space, $resource); - $permId = $this->featureContext->shareNgGetLastCreatedUserShareShareID(); + $permId = $this->featureContext->shareNgGetLastCreatedUserGroupShareID(); $this->featureContext->setResponse( GraphHelper::deleteSharePermission( $this->featureContext->getBaseUrl(),