From 8bd4c3a9a2dc1697de86057db51ef2e0f7bb4b95 Mon Sep 17 00:00:00 2001 From: Amrita <54478846+amrita-shrestha@users.noreply.github.com> Date: Tue, 7 Mar 2023 14:36:04 +0545 Subject: [PATCH] Add tests to add user in a group at once (#5743) --- tests/TestHelpers/GraphHelper.php | 37 ++++- .../features/apiGraph/addUserToGroup.feature | 54 +++++++ .../features/apiGraph/createGroup.feature | 6 +- .../features/bootstrap/GraphContext.php | 133 ++++++++++++++++++ 4 files changed, 226 insertions(+), 4 deletions(-) diff --git a/tests/TestHelpers/GraphHelper.php b/tests/TestHelpers/GraphHelper.php index 00af894433..00c92f6bc1 100644 --- a/tests/TestHelpers/GraphHelper.php +++ b/tests/TestHelpers/GraphHelper.php @@ -502,6 +502,41 @@ class GraphHelper { ); } + /** + * @param string $baseUrl + * @param string $xRequestId + * @param string $adminUser + * @param string $adminPassword + * @param string $groupId + * @param array $userIds + * + * @return ResponseInterface + * @throws GuzzleException + */ + public static function addUsersToGroup( + string $baseUrl, + string $xRequestId, + string $adminUser, + string $adminPassword, + string $groupId, + array $userIds + ): ResponseInterface { + $url = self::getFullUrl($baseUrl, 'groups/' . $groupId); + $payload = [ "members@odata.bind" => [] ]; + foreach ($userIds as $userId) { + $payload["members@odata.bind"][] = self::getFullUrl($baseUrl, 'users/' . $userId); + } + return HttpRequestHelper::sendRequest( + $url, + $xRequestId, + 'PATCH', + $adminUser, + $adminPassword, + self::getRequestHeaders(), + \json_encode($payload) + ); + } + /** * @param string $baseUrl * @param string $xRequestId @@ -678,7 +713,7 @@ class GraphHelper { $payload['mail'] = $email; } $payload['accountEnabled'] = $accountEnabled; - + return \json_encode($payload); } diff --git a/tests/acceptance/features/apiGraph/addUserToGroup.feature b/tests/acceptance/features/apiGraph/addUserToGroup.feature index f4f6189912..d9a4850827 100644 --- a/tests/acceptance/features/apiGraph/addUserToGroup.feature +++ b/tests/acceptance/features/apiGraph/addUserToGroup.feature @@ -151,6 +151,60 @@ Feature: add users to group Then the HTTP status code should be "404" + Scenario: add multiple users to a group at once + 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 + 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 "204" + And the following users should be listed in the following groups + | username | groupname | + | Brian | grp1 | + | Carol | grp1 | + + + Scenario: admin tries to add users to a non-existing group at once + 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 | + When the administrator "Alice" tries to add the following users to a non-existing group at once using the Graph API + | username | + | Brian | + | Carol | + Then the HTTP status code should be "404" + + + Scenario: admin tries to add multiple non-existing users to a group at once + Given the administrator has given "Alice" the role "Admin" using the settings api + And user "Alice" has created a group "grp1" using the Graph API + When the administrator "Alice" tries to add the following non-existing users to a group "grp1" at once using the Graph API + | username | + | Brian | + | Carol | + Then the HTTP status code should be "404" + + + Scenario: admin tries to add non-existing and existing users to a group at once + 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 user "Alice" has created a group "grp1" using the Graph API + When the administrator "Alice" tries to add the following users to a group "grp1" at once using the Graph API + | username | + | Brian | + | Carol | + Then the HTTP status code should be "404" + + Scenario: adding a disabled user to a group Given these groups have been created: | groupname | comment | diff --git a/tests/acceptance/features/apiGraph/createGroup.feature b/tests/acceptance/features/apiGraph/createGroup.feature index 78f0e6968f..48af53fb0f 100644 --- a/tests/acceptance/features/apiGraph/createGroup.feature +++ b/tests/acceptance/features/apiGraph/createGroup.feature @@ -26,17 +26,17 @@ Feature: create group Scenario: admin user tries to create a group that already exists Given group "mygroup" has been created When user "Alice" tries to create a group "mygroup" using the Graph API - And the HTTP status code should be "400" + Then the HTTP status code should be "400" And group "mygroup" should exist Scenario: normal user tries to create a group Given user "Brian" has been created with default attributes and without skeleton files When user "Brian" tries to create a group "mygroup" using the Graph API - And the HTTP status code should be "401" + Then the HTTP status code should be "401" And group "mygroup" should not exist Scenario: admin user tries to create a group that is the empty string When user "Alice" tries to create a group "" using the Graph API - Then the HTTP status code should be "400" \ No newline at end of file + 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 c04968d181..1c552f0a1d 100644 --- a/tests/acceptance/features/bootstrap/GraphContext.php +++ b/tests/acceptance/features/bootstrap/GraphContext.php @@ -956,6 +956,28 @@ class GraphContext implements Context { } } + /** + * @Given /^the administrator has created a group "([^"]*)" using the Graph API$/ + * @Given user :user has created a group :group using the Graph API + * + * @param string $group + * @param ?string $user + * + * @return void + * @throws Exception + * @throws GuzzleException + */ + public function userHasCreatedGroupUsingTheGraphApi(string $group, ?string $user = null): void { + $response = $this->createGroup($group, $user); + + if ($response->getStatusCode() === 200) { + $groupId = $this->featureContext->getJsonDecodedResponse($response)["id"]; + $this->featureContext->addGroupToCreatedGroupsList($group, true, true, $groupId); + } else { + $this->throwHttpException($response, "Could not create group '$group'."); + } + } + /** * create group with provided data * @@ -1609,6 +1631,117 @@ class GraphContext implements Context { } } + /** + * add multiple users in a group at once + * + * @param string $user + * @param array $userIds + * @param string $groupId + * @param TableNode $table + * + * @return void + * @throws GuzzleException + * @throws Exception + */ + public function addMultipleUsersToGroup(string $user, array $userIds, string $groupId, TableNode $table): void { + $credentials = $this->getAdminOrUserCredentials($user); + $this->featureContext->verifyTableNodeColumns($table, ['username']); + + $this->featureContext->setResponse( + GraphHelper::addUsersToGroup( + $this->featureContext->getBaseUrl(), + $this->featureContext->getStepLineRef(), + $credentials["username"], + $credentials["password"], + $groupId, + $userIds + ) + ); + } + + /** + * @When /^the administrator "([^"]*)" adds the following users to a group "([^"]*)" at once using the Graph API$/ + * + * @param string $user + * @param string $group + * @param TableNode $table + * + * @return void + * @throws Exception + * @throws GuzzleException + */ + public function theAdministratorAddsTheFollowingUsersToAGroupInASingleRequestUsingTheGraphApi(string $user, string $group, TableNode $table): void { + $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); + } + + /** + * @When /^the administrator "([^"]*)" tries to add the following users to a non-existing group at once using the Graph API$/ + * + * @param string $user + * @param TableNode $table + * + * @return void + * @throws GuzzleException + * @throws Exception + */ + public function theAdministratorTriesToAddsTheFollowingUsersToANonExistingGroupAtOnceUsingTheGraphApi(string $user, TableNode $table): void { + $userIds = []; + $groupId = WebDavHelper::generateUUIDv4(); + foreach ($table->getHash() as $row) { + $userIds[] = $this->featureContext->getAttributeOfCreatedUser($row['username'], "id"); + } + $this->addMultipleUsersToGroup($user, $userIds, $groupId, $table); + } + + /** + * @When /^the administrator "([^"]*)" tries to add the following non-existing 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 theAdministratorTriesToAddTheFollowingNonExistingUsersToAGroupAtOnceUsingTheGraphApi(string $user, string $group, TableNode $table): void { + $userIds = []; + $groupId = $this->featureContext->getAttributeOfCreatedGroup($group, "id"); + foreach ($table->getHash() as $row) { + $userIds[] = WebDavHelper::generateUUIDv4(); + } + $this->addMultipleUsersToGroup($user, $userIds, $groupId, $table); + } + + /** + * @When /^the administrator "([^"]*)" tries to add 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 theAdministratorTriesToAddTheFollowingUsersToAGroupAtOnceUsingTheGraphApi(string $user, string $group, TableNode $table): void { + $userIds = []; + $groupId = $this->featureContext->getAttributeOfCreatedGroup($group, "id"); + foreach ($table->getHash() as $row) { + try { + $userIds[] = $this->featureContext->getAttributeOfCreatedUser($row['username'], "id"); + } catch (Exception $e) { + $userIds[] = WebDavHelper::generateUUIDv4(); + } + } + $this->addMultipleUsersToGroup($user, $userIds, $groupId, $table); + } + /** * @When user :user gets all applications using the Graph API *