From 081d3af6bef842309bd3afeff91834109bf1ec56 Mon Sep 17 00:00:00 2001 From: "sagargurung1001@gmail.com" Date: Mon, 19 Feb 2024 10:08:28 +0545 Subject: [PATCH] Refactor code Signed-off-by: sagargurung1001@gmail.com --- .../apiSharingNg/listPermissions.feature | 23 +++-- .../features/bootstrap/SharingNgContext.php | 86 ++++++++++++------- 2 files changed, 75 insertions(+), 34 deletions(-) diff --git a/tests/acceptance/features/apiSharingNg/listPermissions.feature b/tests/acceptance/features/apiSharingNg/listPermissions.feature index ff2564f31f5..4e746e2e358 100644 --- a/tests/acceptance/features/apiSharingNg/listPermissions.feature +++ b/tests/acceptance/features/apiSharingNg/listPermissions.feature @@ -198,12 +198,25 @@ Feature: List a sharing permissions """ - Scenario: user send share invitation for all allowed roles defined in permission lists + Scenario: user send share invitation for all allowed roles defined in permission lists for a file + Given user "Alice" has uploaded file with content "hello text" to "textfile.txt" + And user "Brian" has been created with default attributes and without skeleton files + When user "Alice" gets permissions list for file "textfile.txt" of the space "Personal" using the Graph API + Then the HTTP status code should be "200" + And user "Alice" should be able to send share invitation with all allowed permission roles from the above response: + | resource | textfile.txt | + | space | Personal | + | sharee | Brian | + | shareType | user | + + + Scenario: user send share invitation for all allowed roles defined in permission lists for a folder Given user "Alice" has created folder "folder" + And user "Brian" has been created with default attributes and without skeleton files When user "Alice" gets permissions list for folder "folder" of the space "Personal" using the Graph API Then the HTTP status code should be "200" And user "Alice" should be able to send share invitation with all allowed permission roles from the above response: - | resource | folder | - | space | Personal | - | sharee | Brian | - | shareType | user | + | resource | folder | + | space | Personal | + | sharee | Brian | + | shareType | user | diff --git a/tests/acceptance/features/bootstrap/SharingNgContext.php b/tests/acceptance/features/bootstrap/SharingNgContext.php index 7ebe7f9f23b..258ab84c2e0 100644 --- a/tests/acceptance/features/bootstrap/SharingNgContext.php +++ b/tests/acceptance/features/bootstrap/SharingNgContext.php @@ -531,33 +531,61 @@ public function forUserTheSpaceSharesShouldContainTheseEntries(string $user, str Assert::assertSame($should, $fileFound, $assertMessage); } - /** - * @Then user :user should be able to send share invitation with all allowed permission roles from the above response: - * - * @param string $user - * @param TableNode $table - * - * - * @throws JsonException - * @throws \GuzzleHttp\Exception\GuzzleException - */ - public function userCanSendShareInvitationToUserWithAllAllowedPermissionRolesForFolder( string $user, TableNode $table) - { - // Get all the permission saved in the above response - // make loop through all the allowed roles - // Make a share invitation request - // Make the assertion status code and also with response was there [one item in the array] - // delete the share response after - $listPermissionResponse = $this->featureContext->getJsonDecodedResponseBodyContent(); - if(!isset($listPermissionResponse->{'@libre.graph.permissions.roles.allowedValues'})){ - Assert::fail('list permission did not have any permission roles allowed!'); - } - Assert::assertNotEmpty($listPermissionResponse->{'@libre.graph.permissions.roles.allowedValues'}); - $allowedPermissionRoles = $listPermissionResponse->{'@libre.graph.permissions.roles.allowedValues'}; - - foreach ($allowedPermissionRoles as $role) { - $response = $this->sendShareInvitation($user, new TableNode(array_merge($table->getTable(), [['permissionRole', $role->displayName]]))); - var_dump($response); - } - } + /** + * @Then user :user should be able to send share invitation with all allowed permission roles from the above response: + * + * @param string $user + * @param TableNode $table + * + * @throws JsonException + * @throws \GuzzleHttp\Exception\GuzzleException + * + * @return void + */ + public function userCanSendShareInvitationToUserWithAllAllowedPermissionRolesForFolder(string $user, TableNode $table): void { + $listPermissionResponse = $this->featureContext->getJsonDecodedResponseBodyContent(); + if (!isset($listPermissionResponse->{'@libre.graph.permissions.roles.allowedValues'})) { + Assert::fail( + 'The response' . $listPermissionResponse . + 'does not contain any @libre.graph.permissions.roles.allowedValues' + ); + } + Assert::assertNotEmpty( + $listPermissionResponse->{'@libre.graph.permissions.roles.allowedValues'}, + '@libre.graph.permissions.roles.allowedValues in the response should not be empty!' + ); + $allowedPermissionRoles = $listPermissionResponse->{'@libre.graph.permissions.roles.allowedValues'}; + // this info is needed for log to see which roles allowed and which were not when tests fail + $shareInvitationRequestResult = "From the given allowed role lists from the permissions:\n"; + $areAllowedRolesAllowsShareInvitation = true; + foreach ($allowedPermissionRoles as $role) { + // we should be able to send share invitation for each of the role allowed for the files/folders which are listed in permissions (allowed) + // the allowed roles in the response have 2 editor role but of different role id + // we have distinguished it from or test code as 'Editor' and 'File Editor' + if ($role->id === 'fb6c3e19-e378-47e5-b277-9732f9de6e21') { + $roleAllowed = 'Editor'; + } elseif ($role->id === '2d00ce52-1fc2-4dbc-8b95-a73b73395f5a') { + $roleAllowed = 'File Editor'; + } else { + $roleAllowed = $role->displayName; + } + $responseSendInvitation = $this->sendShareInvitation($user, new TableNode(array_merge($table->getTable(), [['permissionsRole', $roleAllowed]]))); + $jsonResponseSendInvitation = $this->featureContext->getJsonDecodedResponseBodyContent($responseSendInvitation); + $httpsStatusCode = $responseSendInvitation->getStatusCode(); + if ($httpsStatusCode === 200 && !empty($jsonResponseSendInvitation->value)) { + // remove the share so that the same user can be share for the next allowed roles + $rows = $table->getRowsHash(); + $resource = $rows['resource']; + $shareType = $rows['shareType']; + $space = $rows['space']; + $removePermissionsResponse = $this->removeSharePermission($user, $shareType, $resource, $space); + Assert::assertEquals(204, $removePermissionsResponse->getStatusCode()); + $shareInvitationRequestResult = $shareInvitationRequestResult . "\tShare invitation for resource with role '" . $roleAllowed . "' was allowed.\n"; + } else { + $areAllowedRolesAllowsShareInvitation = false; + $shareInvitationRequestResult = $shareInvitationRequestResult . "\tShare invitation for resource with role '" . $roleAllowed . "' failed and was not allowed.\n"; + } + } + Assert::assertTrue($areAllowedRolesAllowsShareInvitation, $shareInvitationRequestResult); + } }