From 3e3d3f9de2919368f5849061e9c3ea6894c6457c Mon Sep 17 00:00:00 2001 From: Viktor Scharf Date: Thu, 16 Feb 2023 16:34:13 +0100 Subject: [PATCH] [test-only] ApiTest. get users of members of several groups (#5575) * get users with group filter * fix test --- tests/TestHelpers/GraphHelper.php | 54 ++++++++++++ tests/acceptance/config/behat.yml | 6 ++ .../features/apiGraph/getUser.feature | 30 ++++++- .../features/bootstrap/FeatureContext.php | 85 ++++++++++++++++++- .../features/bootstrap/GraphContext.php | 61 +++++++++++-- .../features/bootstrap/SpacesContext.php | 81 +++--------------- 6 files changed, 236 insertions(+), 81 deletions(-) diff --git a/tests/TestHelpers/GraphHelper.php b/tests/TestHelpers/GraphHelper.php index 388c36f508..2f0902bdc6 100644 --- a/tests/TestHelpers/GraphHelper.php +++ b/tests/TestHelpers/GraphHelper.php @@ -1019,4 +1019,58 @@ class GraphHelper { self::getRequestHeaders() ); } + + /** + * @param string $baseUrl + * @param string $xRequestId + * @param string $user + * @param string $password + * @param string $groupId + * + * @return ResponseInterface + * @throws GuzzleException + */ + public static function getUsersWithFilterMemberOf( + string $baseUrl, + string $xRequestId, + string $user, + string $password, + string $groupId + ): ResponseInterface { + $url = self::getFullUrl($baseUrl, 'users' . '?$filter=memberOf/any(m:m/id ' . "eq '$groupId')"); + return HttpRequestHelper::get( + $url, + $xRequestId, + $user, + $password, + self::getRequestHeaders() + ); + } + + /** + * @param string $baseUrl + * @param string $xRequestId + * @param string $user + * @param string $password + * @param array $groupIdArray + * + * @return ResponseInterface + * @throws GuzzleException + */ + public static function getUsersOfTwoGroups( + string $baseUrl, + string $xRequestId, + string $user, + string $password, + array $groupIdArray + ): ResponseInterface { + $url = self::getFullUrl($baseUrl, 'users' . '?$filter=memberOf/any(m:m/id ' . "eq '$groupIdArray[0]') " . "and memberOf/any(m:m/id eq '$groupIdArray[1]')"); + return HttpRequestHelper::get( + $url, + $xRequestId, + $user, + $password, + self::getRequestHeaders() + ); + } } diff --git a/tests/acceptance/config/behat.yml b/tests/acceptance/config/behat.yml index 422e63d80a..ed8924b4c3 100644 --- a/tests/acceptance/config/behat.yml +++ b/tests/acceptance/config/behat.yml @@ -53,6 +53,7 @@ default: - WebDavPropertiesContext: - TUSContext: - SpacesTUSContext: + - GraphContext: apiSpacesShares: paths: @@ -75,6 +76,7 @@ default: - TUSContext: - SpacesTUSContext: - ArchiverContext: + - GraphContext: apiContract: paths: @@ -96,6 +98,7 @@ default: - WebDavPropertiesContext: - TUSContext: - SpacesTUSContext: + - GraphContext: apiArchiver: paths: @@ -115,6 +118,7 @@ default: - FavoritesContext: - TrashbinContext: - WebDavPropertiesContext: + - GraphContext: apiGraph: paths: @@ -152,6 +156,7 @@ default: - FilesVersionsContext: - OCSContext: - TrashbinContext: + - GraphContext: apiAsyncUpload: paths: @@ -167,6 +172,7 @@ default: - FilesVersionsContext: - OCSContext: - TrashbinContext: + - GraphContext: extensions: diff --git a/tests/acceptance/features/apiGraph/getUser.feature b/tests/acceptance/features/apiGraph/getUser.feature index f98170a1de..f5e92927a6 100644 --- a/tests/acceptance/features/apiGraph/getUser.feature +++ b/tests/acceptance/features/apiGraph/getUser.feature @@ -29,7 +29,7 @@ Feature: get users Scenario: admin user gets all users When user "Alice" gets all users using the Graph API Then the HTTP status code should be "200" - And the API response should contain all users with the following information: + And the API response should contain following users with the information: | displayName | id | mail | onPremisesSamAccountName | | Alice Hansen | %uuid_v4% | alice@example.org | Alice | | Brian Murphy | %uuid_v4% | brian@example.org | Brian | @@ -96,4 +96,30 @@ Feature: get users When the user "Carol" gets user "Brian" along with his group information using Graph API Then the HTTP status code should be "401" And the last response should be an unauthorized response - \ No newline at end of file + + + Scenario: admin user gets all users of certain groups + Given user "Carol" has been created with default attributes and without skeleton files + And group "tea-lover" has been created + And group "coffee-lover" has been created + And user "Alice" has been added to group "tea-lover" + And user "Brian" has been added to group "tea-lover" + And user "Brian" has been added to group "coffee-lover" + When the user "Alice" gets all users of the group "tea-lover" using the Graph API + Then the HTTP status code should be "200" + And the API response should contain following users with the information: + | displayName | id | mail | onPremisesSamAccountName | + | Alice Hansen | %uuid_v4% | alice@example.org | Alice | + | Brian Murphy | %uuid_v4% | brian@example.org | Brian | + But the API response should not contain following user with the information: + | displayName | id | mail | onPremisesSamAccountName | + | Carol King | %uuid_v4% | carol@example.org | Carol | + When the user "Alice" gets all users of two groups "tea-lover,coffee-lover" using the Graph API + Then the HTTP status code should be "200" + And the API response should contain following user with the information: + | displayName | id | mail | onPremisesSamAccountName | + | Brian Murphy | %uuid_v4% | brian@example.org | Brian | + But the API response should not contain following users with the information: + | displayName | id | mail | onPremisesSamAccountName | + | Alice Hansen | %uuid_v4% | alice@example.org | Alice | + | Carol King | %uuid_v4% | carol@example.org | Carol | diff --git a/tests/acceptance/features/bootstrap/FeatureContext.php b/tests/acceptance/features/bootstrap/FeatureContext.php index b989863198..78be6159bd 100644 --- a/tests/acceptance/features/bootstrap/FeatureContext.php +++ b/tests/acceptance/features/bootstrap/FeatureContext.php @@ -39,6 +39,7 @@ use TestHelpers\HttpRequestHelper; use TestHelpers\UploadHelper; use TestHelpers\OcisHelper; use Laminas\Ldap\Ldap; +use TestHelpers\GraphHelper; use TestHelpers\WebDavHelper; require_once 'bootstrap.php'; @@ -3104,6 +3105,8 @@ class FeatureContext extends BehatVariablesContext { * "parameter" => [] * ], * ] + * @param string|null $group + * @param string|null $userName * * @return string */ @@ -3111,7 +3114,9 @@ class FeatureContext extends BehatVariablesContext { ?string $value, ?string $user = null, ?array $functions = [], - ?array $additionalSubstitutions = [] + ?array $additionalSubstitutions = [], + ?string $group = null, + ?string $userName = null ): ?string { $substitutions = [ [ @@ -3249,6 +3254,18 @@ class FeatureContext extends BehatVariablesContext { "getLastPublicShareToken" ], "parameter" => [] + ], + [ + "code" => "%user_id%", + "function" => + [$this, "getUserIdByUserName"], + "parameter" => [$userName] + ], + [ + "code" => "%group_id%", + "function" => + [$this, "getGroupIdByGroupName"], + "parameter" => [$group] ] ]; if ($user !== null) { @@ -3293,6 +3310,18 @@ class FeatureContext extends BehatVariablesContext { "getPersonalSpaceIdForUser", ], "parameter" => [$user, true] + ], + [ + "code" => "%user_id%", + "function" => + [$this, "getUserIdByUserName"], + "parameter" => [$userName] + ], + [ + "code" => "%group_id%", + "function" => + [$this, "getGroupIdByGroupName"], + "parameter" => [$group] ] ); } @@ -4485,4 +4514,58 @@ class FeatureContext extends BehatVariablesContext { $this->getBaseUrl() ); } + + /** + * The method returns userId + * + * @param string $userName + * + * @return string + * @throws Exception|GuzzleException + */ + public function getUserIdByUserName(string $userName): string { + $response = GraphHelper::getUser( + $this->getBaseUrl(), + $this->getStepLineRef(), + $this->getAdminUsername(), + $this->getAdminPassword(), + $userName + ); + if ($response) { + $data = \json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR); + if (isset($data["id"])) { + return $data["id"]; + } else { + throw new Exception(__METHOD__ . " accounts-list is empty"); + } + } + throw new Exception(__METHOD__ . " user with name $userName not found"); + } + + /** + * The method returns groupId + * + * @param string $groupName + * + * @return string + * @throws Exception|GuzzleException + */ + public function getGroupIdByGroupName(string $groupName): string { + $response = GraphHelper::getGroup( + $this->getBaseUrl(), + $this->getStepLineRef(), + $this->getAdminUsername(), + $this->getAdminPassword(), + $groupName + ); + if ($response) { + $data = $this->getJsonDecodedResponse($response); + if (isset($data["id"])) { + return $data["id"]; + } else { + throw new Exception(__METHOD__ . " accounts-list is empty"); + } + } + throw new Exception(__METHOD__ . " Group with name $groupName not found"); + } } diff --git a/tests/acceptance/features/bootstrap/GraphContext.php b/tests/acceptance/features/bootstrap/GraphContext.php index b4cf800921..0b084b4430 100644 --- a/tests/acceptance/features/bootstrap/GraphContext.php +++ b/tests/acceptance/features/bootstrap/GraphContext.php @@ -28,11 +28,6 @@ class GraphContext implements Context { */ private FeatureContext $featureContext; - /** - * @var SpacesContext - */ - private SpacesContext $spacesContext; - /** * This will run before EVERY scenario. * It will set the properties for this object. @@ -1284,14 +1279,15 @@ class GraphContext implements Context { } /** - * @Then the API response should contain all users with the following information: + * @Then /^the API response should (not|)\s?contain following (user|users) with the information:$/ * + * @param string $shouldOrNot (not|) * @param TableNode $table * * @throws Exception * @return void */ - public function theApiResponseShouldContainAllUserWithFollowingInformation(TableNode $table): void { + public function theApiResponseShouldContainAllUserWithFollowingInformation(string $shouldOrNot, TableNode $table): void { $values = $table->getHash(); $apiResponse = $this->featureContext->getJsonDecodedResponse($this->featureContext->getResponse())['value']; foreach ($values as $expectedValue) { @@ -1304,8 +1300,10 @@ class GraphContext implements Context { break; } } - if (!$found) { - throw new Exception('User ' . $expectedValue["displayName"] . ' could not be found in the response.'); + if ($shouldOrNot === 'not') { + Assert::assertFalse($found, $expectedValue["displayName"] . ' has been found in the response, but should not be.'); + } else { + Assert::assertTrue($found, $expectedValue["displayName"] . ' could not be found in the response.'); } } } @@ -1520,4 +1518,49 @@ class GraphContext implements Context { Assert::assertTrue($foundRoleInResponse, "the response does not contain the role $row[0]"); } } + + /** + * @When the user :user gets all users of the group :group using the Graph API + * + * @param string $user + * @param string $group + * + * @return void + * @throws GuzzleException + */ + public function userGetsAllUsersOfTheGroupUsingTheGraphApi(string $user, string $group) { + $groupId = $this->featureContext->getGroupIdByGroupName($group); + $response = GraphHelper::getUsersWithFilterMemberOf( + $this->featureContext->getBaseUrl(), + $this->featureContext->getStepLineRef(), + $user, + $this->featureContext->getPasswordForUser($user), + $groupId + ); + $this->featureContext->setResponse($response); + } + + /** + * @When the user :user gets all users of two groups :groups using the Graph API + * + * @param string $user + * @param string $groups + * + * @return void + * @throws GuzzleException + */ + public function userGetsAllUsersOfTwoGroupsUsingTheGraphApi(string $user, string $groups) { + $groupsIdArray = []; + foreach (explode(',', $groups) as $group) { + array_push($groupsIdArray, $this->featureContext->getGroupIdByGroupName($group)); + } + $response = GraphHelper::getUsersOfTwoGroups( + $this->featureContext->getBaseUrl(), + $this->featureContext->getStepLineRef(), + $user, + $this->featureContext->getPasswordForUser($user), + $groupsIdArray + ); + $this->featureContext->setResponse($response); + } } diff --git a/tests/acceptance/features/bootstrap/SpacesContext.php b/tests/acceptance/features/bootstrap/SpacesContext.php index 3221397ab2..df48b530cd 100644 --- a/tests/acceptance/features/bootstrap/SpacesContext.php +++ b/tests/acceptance/features/bootstrap/SpacesContext.php @@ -69,11 +69,17 @@ class SpacesContext implements Context { * @var ChecksumContext */ private ChecksumContext $checksumContext; + /** * @var FilesVersionsContext */ private FilesVersionsContext $filesVersionsContext; + /** + * @var GraphContext + */ + private GraphContext $graphContext; + /** * @var string */ @@ -397,60 +403,6 @@ class SpacesContext implements Context { return $fileData["Etag"][0]; } - /** - * The method returns userId - * - * @param string $userName - * - * @return string - * @throws Exception|GuzzleException - */ - public function getUserIdByUserName(string $userName): string { - $response = GraphHelper::getUser( - $this->featureContext->getBaseUrl(), - $this->featureContext->getStepLineRef(), - $this->featureContext->getAdminUsername(), - $this->featureContext->getAdminPassword(), - $userName - ); - if ($response) { - $data = \json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR); - if (isset($data["id"])) { - return $data["id"]; - } else { - throw new Exception(__METHOD__ . " accounts-list is empty"); - } - } - throw new Exception(__METHOD__ . " user with name $userName not found"); - } - - /** - * The method returns groupId - * - * @param string $groupName - * - * @return string - * @throws Exception|GuzzleException - */ - public function getGroupIdByGroupName(string $groupName): string { - $response = GraphHelper::getGroup( - $this->featureContext->getBaseUrl(), - $this->featureContext->getStepLineRef(), - $this->featureContext->getAdminUsername(), - $this->featureContext->getAdminPassword(), - $groupName - ); - if ($response) { - $data = $this->featureContext->getJsonDecodedResponse($response); - if (isset($data["id"])) { - return $data["id"]; - } else { - throw new Exception(__METHOD__ . " accounts-list is empty"); - } - } - throw new Exception(__METHOD__ . " Group with name $groupName not found"); - } - /** * using method from core to set share data * @@ -485,6 +437,7 @@ class SpacesContext implements Context { $this->favoritesContext = $environment->getContext('FavoritesContext'); $this->checksumContext = $environment->getContext('ChecksumContext'); $this->filesVersionsContext = $environment->getContext('FilesVersionsContext'); + $this->graphContext = $environment->getContext('GraphContext'); // Run the BeforeScenario function in OCSContext to set it up correctly $this->ocsContext->before($scope); $this->baseUrl = \trim($this->featureContext->getBaseUrl(), "/"); @@ -990,12 +943,6 @@ class SpacesContext implements Context { [$this, "getSpaceIdByNameFromResponse"], "parameter" => [$spaceName] ], - [ - "code" => "%user_id%", - "function" => - [$this, "getUserIdByUserName"], - "parameter" => [$userName] - ], [ "code" => "%file_id%", "function" => @@ -1007,14 +954,10 @@ class SpacesContext implements Context { "function" => [$this, "getETag"], "parameter" => [$userName, $spaceName, $fileName] - ], - [ - "code" => "%group_id%", - "function" => - [$this, "getGroupIdByGroupName"], - "parameter" => [$groupName] ] - ] + ], + $groupName, + $userName ); $segments = explode("@@@", $row["key"]); // traverse down in the array @@ -1111,7 +1054,7 @@ class SpacesContext implements Context { ); Assert::assertIsArray($spaceAsArray = $this->getSpaceByNameFromResponse($spaceName), "No space with name $spaceName found"); $permissions = $spaceAsArray["root"]["permissions"]; - $userId = $this->getUserIdByUserName($grantedUser); + $userId = $this->featureContext->getUserIdByUserName($grantedUser); $userRole = ""; foreach ($permissions as $permission) { @@ -3277,7 +3220,7 @@ class SpacesContext implements Context { "Expected response status code should be 200" ); Assert::assertIsArray($spaceAsArray = $this->getSpaceByNameFromResponse($spaceName), "No space with name $spaceName found"); - $recipientType === 'user' ? $recipientId = $this->getUserIdByUserName($recipient) : $recipientId = $this->getGroupIdByGroupName($recipient); + $recipientType === 'user' ? $recipientId = $this->featureContext->getUserIdByUserName($recipient) : $recipientId = $this->featureContext->getGroupIdByGroupName($recipient); $foundRoleInResponse = false; foreach ($spaceAsArray['root']['permissions'] as $permission) { if (isset($permission['grantedToIdentities'][0][$recipientType]) && $permission['roles'][0] === $role && $permission['grantedToIdentities'][0][$recipientType]['id'] === $recipientId) {