From 15a4941281aad87b00304f6bcc160f8f47eab33a Mon Sep 17 00:00:00 2001 From: Amrita <54478846+amrita-shrestha@users.noreply.github.com> Date: Wed, 15 Mar 2023 13:58:04 +0545 Subject: [PATCH] Add tests to add user in a group at once (#5831) --- tests/TestHelpers/GraphHelper.php | 38 +++++++++++++ ...ected-failures-localAPI-on-OCIS-storage.md | 6 +++ .../features/apiGraph/addUserToGroup.feature | 36 +++++++++++++ .../features/bootstrap/GraphContext.php | 53 +++++++++++++++++++ 4 files changed, 133 insertions(+) diff --git a/tests/TestHelpers/GraphHelper.php b/tests/TestHelpers/GraphHelper.php index d2f1a9888e..fb2fa496c5 100644 --- a/tests/TestHelpers/GraphHelper.php +++ b/tests/TestHelpers/GraphHelper.php @@ -503,6 +503,8 @@ class GraphHelper { } /** + * add multiple users to a group at once + * * @param string $baseUrl * @param string $xRequestId * @param string $adminUser @@ -537,6 +539,42 @@ class GraphHelper { ); } + /** + * tries to add a group to a group + * + * @param string $baseUrl + * @param string $xRequestId + * @param string $adminUser + * @param string $adminPassword + * @param string $groupId + * @param string $groupIdToAdd + * + * @return ResponseInterface + * @throws GuzzleException + */ + public static function addGroupToGroup( + string $baseUrl, + string $xRequestId, + string $adminUser, + string $adminPassword, + string $groupId, + string $groupIdToAdd + ): ResponseInterface { + $url = self::getFullUrl($baseUrl, 'groups/' . $groupId); + $payload = [ + "@odata.id" => self::getFullUrl($baseUrl, 'groups/' . $groupIdToAdd) + ]; + return HttpRequestHelper::sendRequest( + $url, + $xRequestId, + 'PATCH', + $adminUser, + $adminPassword, + self::getRequestHeaders(), + \json_encode($payload) + ); + } + /** * @param string $baseUrl * @param string $xRequestId diff --git a/tests/acceptance/expected-failures-localAPI-on-OCIS-storage.md b/tests/acceptance/expected-failures-localAPI-on-OCIS-storage.md index 9099e9b219..b891a4681d 100644 --- a/tests/acceptance/expected-failures-localAPI-on-OCIS-storage.md +++ b/tests/acceptance/expected-failures-localAPI-on-OCIS-storage.md @@ -110,5 +110,11 @@ The expected failures in this file are from features in the owncloud/ocis repo. #### [Using # in the onPremisesSamAccountName breaks getting users](https://github.com/owncloud/ocis/issues/5755) - [apiGraph/editUser.feature:44](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiGraph/editUser.feature#L44) +#### [Same users can be added in a group multiple time](https://github.com/owncloud/ocis/issues/5702) +- [apiGraph/addUserToGroup.feature:222](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiGraph/addUserToGroup.feature#L222) + +#### [Try to add group to a group return 204](https://github.com/owncloud/ocis/issues/5793) +- [apiGraph/addUserToGroup.feature:244](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiGraph/addUserToGroup.feature#L244) + Note: always have an empty line at the end of this file. The bash script that processes this file requires that the last line has a newline on the end. diff --git a/tests/acceptance/features/apiGraph/addUserToGroup.feature b/tests/acceptance/features/apiGraph/addUserToGroup.feature index 03e799358d..b6ba2c540a 100644 --- a/tests/acceptance/features/apiGraph/addUserToGroup.feature +++ b/tests/acceptance/features/apiGraph/addUserToGroup.feature @@ -217,3 +217,39 @@ Feature: add users to group And the following users should be listed in the following groups | username | groupname | | Alice | sales | + + @issue-5702 + Scenario: try to add users to a group twice + Given the administrator has given "Alice" the role "Admin" using the settings api + And these users have been created with default attributes and without skeleton files: + | username | + | Brian | + | Carol | + And user "Alice" has created a group "grp1" using the Graph API + And the administrator "Alice" has added the following users to a group "grp1" at once using the Graph API + | username | + | Brian | + | Carol | + When the administrator "Alice" adds the following users to a group "grp1" at once using the Graph API + | username | + | Brian | + | Carol | + Then the HTTP status code should be "400" + And the following users should be listed in the following groups + | username | groupname | + | Brian | grp1 | + | Carol | grp1 | + + @issue-5793 + Scenario: try to add a group to a group + Given the administrator has given "Alice" the role "Admin" using the settings api + And these users have been created with default attributes and without skeleton files: + | username | + | Brian | + And these groups have been created: + | groupname | + | student | + | music | + And user "Brian" has been added to group "music" + When the administrator "Alice" tries to add a group "music" to a group "student" using the Graph API + Then the HTTP status code should be "400" diff --git a/tests/acceptance/features/bootstrap/GraphContext.php b/tests/acceptance/features/bootstrap/GraphContext.php index 7d3aca3f2d..068c7162d8 100644 --- a/tests/acceptance/features/bootstrap/GraphContext.php +++ b/tests/acceptance/features/bootstrap/GraphContext.php @@ -2103,4 +2103,57 @@ class GraphContext implements Context { ) ); } + + /** + * @Given /^the administrator "([^"]*)" has added the following users to a group "([^"]*)" at once using the Graph API$/ + * + * @param string $user + * @param string $group + * @param TableNode $table + * + * @return void + * @throws GuzzleException + * @throws Exception + */ + public function theAdministratorHasAddedTheFollowingUsersToAGroupAtOnceUsingTheGraphApi(string $user, string $group, TableNode $table) { + $userIds = []; + $groupId = $this->featureContext->getAttributeOfCreatedGroup($group, "id"); + foreach ($table->getHash() as $row) { + $userIds[] = $this->featureContext->getAttributeOfCreatedUser($row['username'], "id"); + } + $this->addMultipleUsersToGroup($user, $userIds, $groupId, $table); + $response = $this->featureContext->getResponse(); + if ($response->getStatusCode() !== 204) { + $$this->throwHttpException($response, "Cannot add users to group '$group'"); + } + $this->featureContext->emptyLastHTTPStatusCodesArray(); + } + + /** + * @When /^the administrator "([^"]*)" tries to add a group "([^"]*)" to a group "([^"]*)" using the Graph API$/ + * + * @param string $user + * @param string $groupToAdd + * @param string $group + * + * @return void + * @throws GuzzleException + * @throws Exception + */ + public function theAdministratorAddGroupToAGroupAtOnceUsingTheGraphApi(string $user, string $groupToAdd, string $group) { + $groupId = $this->featureContext->getAttributeOfCreatedGroup($group, "id"); + $groupIdToAdd = $this->featureContext->getAttributeOfCreatedGroup($groupToAdd, "id"); + $credentials = $this->getAdminOrUserCredentials($user); + + $this->featureContext->setResponse( + GraphHelper::addGroupToGroup( + $this->featureContext->getBaseUrl(), + $this->featureContext->getStepLineRef(), + $credentials["username"], + $credentials["password"], + $groupId, + $groupIdToAdd + ) + ); + } }