From 249adc7cd7342966e5dc9f5277a1c32692919ded Mon Sep 17 00:00:00 2001 From: Swikriti Tripathi <41103328+SwikritiT@users.noreply.github.com> Date: Thu, 9 Mar 2023 16:21:50 +0545 Subject: [PATCH] Seperate test steps for positive and negative cases (#5773) fix php style use the correct step fix other steps fix other php style remove unwanted param Refactor test function for handeling errors fix return types clean up code address reviews --- .../features/apiGraph/addUserToGroup.feature | 10 +-- .../apiGraph/removeUserFromGroup.feature | 4 +- .../features/bootstrap/FeatureContext.php | 2 +- .../features/bootstrap/GraphContext.php | 89 ++++++++++++------- .../features/bootstrap/Provisioning.php | 8 +- 5 files changed, 65 insertions(+), 48 deletions(-) diff --git a/tests/acceptance/features/apiGraph/addUserToGroup.feature b/tests/acceptance/features/apiGraph/addUserToGroup.feature index d9a4850827..03e799358d 100644 --- a/tests/acceptance/features/apiGraph/addUserToGroup.feature +++ b/tests/acceptance/features/apiGraph/addUserToGroup.feature @@ -136,7 +136,7 @@ Feature: add users to group Scenario: admin tries to add user to a non-existing group - When the administrator tries to add user "Alice" to group "nonexistentgroup" using the Graph API + When the administrator tries to add user "Alice" to a nonexistent group using the Graph API Then the HTTP status code should be "404" @@ -147,7 +147,7 @@ Feature: add users to group Scenario: admin tries to add user to a group without sending the group - When the administrator tries to add user "Alice" to group "" using the Graph API + When the administrator tries to add user "Alice" to a nonexistent group using the Graph API Then the HTTP status code should be "404" @@ -175,7 +175,7 @@ Feature: add users to group | username | | Brian | | Carol | - When the administrator "Alice" tries to add the following users to a non-existing group at once using the Graph API + When the administrator "Alice" tries to add the following users to a nonexistent group at once using the Graph API | username | | Brian | | Carol | @@ -185,7 +185,7 @@ Feature: add users to group 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 + When the administrator "Alice" tries to add the following nonexistent users to a group "grp1" at once using the Graph API | username | | Brian | | Carol | @@ -198,7 +198,7 @@ Feature: add users to group | 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 + When the administrator "Alice" tries to add the following existent and nonexistent users to a group "grp1" at once using the Graph API | username | | Brian | | Carol | diff --git a/tests/acceptance/features/apiGraph/removeUserFromGroup.feature b/tests/acceptance/features/apiGraph/removeUserFromGroup.feature index f2a51a62f6..554773c8c2 100644 --- a/tests/acceptance/features/apiGraph/removeUserFromGroup.feature +++ b/tests/acceptance/features/apiGraph/removeUserFromGroup.feature @@ -152,8 +152,8 @@ Feature: remove a user from a group | Alice | var/../etc | - Scenario: admin tries to remove a user from a non-existing group - When the administrator tries to remove user "Alice" from group "nonexistentgroup" using the Graph API + Scenario: admin tries to remove a user from a nonexistent group + When the administrator tries to remove user "Alice" from a nonexistent group using the Graph API Then the HTTP status code should be "404" diff --git a/tests/acceptance/features/bootstrap/FeatureContext.php b/tests/acceptance/features/bootstrap/FeatureContext.php index 532047a70a..95c793b84f 100644 --- a/tests/acceptance/features/bootstrap/FeatureContext.php +++ b/tests/acceptance/features/bootstrap/FeatureContext.php @@ -4600,7 +4600,7 @@ class FeatureContext extends BehatVariablesContext { * @return string * @throws Exception|GuzzleException */ - public function getGroupIdByGroupName(string $groupName): string { + public function getGroupIdByGroupName(string $groupName):string { $response = GraphHelper::getGroup( $this->getBaseUrl(), $this->getStepLineRef(), diff --git a/tests/acceptance/features/bootstrap/GraphContext.php b/tests/acceptance/features/bootstrap/GraphContext.php index 1c552f0a1d..9cb5fa8025 100644 --- a/tests/acceptance/features/bootstrap/GraphContext.php +++ b/tests/acceptance/features/bootstrap/GraphContext.php @@ -241,12 +241,8 @@ class GraphContext implements Context { */ public function adminHasRetrievedUserUsingTheGraphApi(string $user): void { $user = $this->featureContext->getActualUsername($user); - try { - $userId = $this->featureContext->getAttributeOfCreatedUser($user, "id"); - $userId = $userId ?? $user; - } catch (Exception $e) { - $userId = $user; - } + $userId = $this->featureContext->getAttributeOfCreatedUser($user, "id"); + $userId = $userId ? $userId : $user; $result = GraphHelper::getUser( $this->featureContext->getBaseUrl(), $this->featureContext->getStepLineRef(), @@ -816,17 +812,8 @@ class GraphContext implements Context { */ public function addUserToGroup(string $group, string $user, ?string $byUser = null): ResponseInterface { $credentials = $this->getAdminOrUserCredentials($byUser); - try { - $groupId = $this->featureContext->getAttributeOfCreatedGroup($group, "id"); - } catch (Exception $e) { - $groupId = WebDavHelper::generateUUIDv4(); - } - try { - $userId = $this->featureContext->getAttributeOfCreatedUser($user, "id"); - } catch (Exception $e) { - $userId = WebDavHelper::generateUUIDv4(); - } - + $groupId = $this->featureContext->getAttributeOfCreatedGroup($group, "id"); + $userId = $this->featureContext->getAttributeOfCreatedUser($user, "id"); return GraphHelper::addUserToGroup( $this->featureContext->getBaseUrl(), $this->featureContext->getStepLineRef(), @@ -888,6 +875,33 @@ class GraphContext implements Context { $this->featureContext->setResponse($this->addUserToGroup($group, $user)); } + /** + * @When the administrator tries to add user :user to a nonexistent group using the Graph API + * @When the user :byUser tries to add user :user to a nonexistent group using the Graph API + * + * @param string $user + * @param string|null $byUser + * + * @return void + * + * @throws GuzzleException | Exception + */ + public function theAdministratorTriesToAddUserToNonExistentGroupUsingTheGraphAPI(string $user, ?string $byUser = null): void { + $credentials = $this->getAdminOrUserCredentials($byUser); + $groupId = WebDavHelper::generateUUIDv4(); + $userId = $this->featureContext->getAttributeOfCreatedUser($user, "id"); + $this->featureContext->setResponse( + GraphHelper::addUserToGroup( + $this->featureContext->getBaseUrl(), + $this->featureContext->getStepLineRef(), + $credentials['username'], + $credentials['password'], + $userId, + $groupId + ) + ); + } + /** * @When user :user tries to add himself/herself to group :group using the Graph API * @@ -1297,18 +1311,27 @@ class GraphContext implements Context { * @param string|null $byUser * * @return void + * @throws Exception | GuzzleException */ public function theUserTriesToRemoveAnotherUserFromGroupUsingTheGraphAPI(string $user, string $group, ?string $byUser = null): void { - try { - $groupId = $this->featureContext->getAttributeOfCreatedGroup($group, "id"); - } catch (Exception $e) { - $groupId = WebDavHelper::generateUUIDv4(); - } - try { - $userId = $this->featureContext->getAttributeOfCreatedUser($user, "id"); - } catch (Exception $e) { - $userId = WebDavHelper::generateUUIDv4(); - } + $groupId = $this->featureContext->getAttributeOfCreatedGroup($group, "id"); + $userId = $this->featureContext->getAttributeOfCreatedUser($user, "id"); + $this->featureContext->setResponse($this->removeUserFromGroup($groupId, $userId, $byUser)); + } + + /** + * @When the administrator tries to remove user :user from a nonexistent group using the Graph API + * @When user :byUser tries to remove user :user from a nonexistent group using the Graph API + * + * @param string $user + * @param string|null $byUser + * + * @return void + * @throws GuzzleException + */ + public function theUserTriesToRemoveAnotherUserFromNonExistentGroupUsingTheGraphAPI(string $user, ?string $byUser = null): void { + $groupId = WebDavHelper::generateUUIDv4(); + $userId = $this->featureContext->getAttributeOfCreatedUser($user, "id"); $this->featureContext->setResponse($this->removeUserFromGroup($groupId, $userId, $byUser)); } @@ -1680,7 +1703,7 @@ class GraphContext implements Context { } /** - * @When /^the administrator "([^"]*)" tries to add the following users to a non-existing group at once using the Graph API$/ + * @When /^the administrator "([^"]*)" tries to add the following users to a nonexistent group at once using the Graph API$/ * * @param string $user * @param TableNode $table @@ -1699,7 +1722,7 @@ class GraphContext implements Context { } /** - * @When /^the administrator "([^"]*)" tries to add the following non-existing users to a group "([^"]*)" at once using the Graph API$/ + * @When /^the administrator "([^"]*)" tries to add the following nonexistent users to a group "([^"]*)" at once using the Graph API$/ * * @param string $user * @param string $group @@ -1720,6 +1743,7 @@ class GraphContext implements Context { /** * @When /^the administrator "([^"]*)" tries to add the following users to a group "([^"]*)" at once using the Graph API$/ + * @When /^the administrator "([^"]*)" tries to add the following existent and nonexistent users to a group "([^"]*)" at once using the Graph API$/ * * @param string $user * @param string $group @@ -1733,11 +1757,8 @@ class GraphContext implements Context { $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(); - } + $userId = $this->featureContext->getAttributeOfCreatedUser($row['username'], "id"); + $userIds[] = $userId ? $userId : WebDavHelper::generateUUIDv4(); } $this->addMultipleUsersToGroup($user, $userIds, $groupId, $table); } diff --git a/tests/acceptance/features/bootstrap/Provisioning.php b/tests/acceptance/features/bootstrap/Provisioning.php index 64ae80f989..f1cf17664b 100644 --- a/tests/acceptance/features/bootstrap/Provisioning.php +++ b/tests/acceptance/features/bootstrap/Provisioning.php @@ -265,9 +265,7 @@ trait Provisioning { ); } } else { - throw new Exception( - __METHOD__ . ": User '$user' does not exist in the created list." - ); + return false; } } @@ -296,9 +294,7 @@ trait Provisioning { ); } } else { - throw new Exception( - __METHOD__ . ": Group '$group' does not exist in the created list." - ); + return false; } }