From 4124173a51cdd2c5cd320a1771b76bb7b9861f41 Mon Sep 17 00:00:00 2001 From: "sagargurung1001@gmail.com" Date: Thu, 24 Nov 2022 14:12:26 +0545 Subject: [PATCH] Review Address --- tests/TestHelpers/GraphHelper.php | 12 +- .../apiGraph/getUserOwnInformation.feature | 30 ++--- .../features/bootstrap/GraphContext.php | 109 ++++++++---------- 3 files changed, 73 insertions(+), 78 deletions(-) diff --git a/tests/TestHelpers/GraphHelper.php b/tests/TestHelpers/GraphHelper.php index 28ac5e7cc0..a81470095f 100644 --- a/tests/TestHelpers/GraphHelper.php +++ b/tests/TestHelpers/GraphHelper.php @@ -26,6 +26,16 @@ class GraphHelper { ]; } + /** + * @param string $id + * + * @return int (1 = true | 0 = false) + */ + public static function isUUIDv4(string $id): int { + $regex = '/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i'; + return preg_match($regex, $id); + } + /** * @param string $baseUrl * @param string $path @@ -190,7 +200,7 @@ class GraphHelper { * @return ResponseInterface * @throws GuzzleException */ - public static function getUserInformation( + public static function getOwnInformationAndGroupMemberships( string $baseUrl, string $xRequestId, string $user, diff --git a/tests/acceptance/features/apiGraph/getUserOwnInformation.feature b/tests/acceptance/features/apiGraph/getUserOwnInformation.feature index 62e5f8e8e9..bafeb3bc0d 100644 --- a/tests/acceptance/features/apiGraph/getUserOwnInformation.feature +++ b/tests/acceptance/features/apiGraph/getUserOwnInformation.feature @@ -1,5 +1,5 @@ -@api -Feature: get user own information +@api @skipOnOcV10 +Feature: get user's own information As user I want to be able to retrieve my own information So that I can see my information @@ -9,14 +9,11 @@ Feature: get user own information Scenario: user gets his/her own information with no group involvement - When the user "Alice" retrives her information using the Graph API + When the user "Alice" retrieves her information using the Graph API Then the HTTP status code should be "200" - And the api response should contains the following information: - | displayName | Alice Hansen | - | id | %UUIDv4% | - | mail | alice@example.org | - | onPremisesSamAccountName | Alice | - | memberOf | | + And the user retrieve API response should contain the following information: + | displayName | id | mail | onPremisesSamAccountName | + | Alice Hansen | %uuid_v4% | alice@example.org | Alice | Scenario: user gets his/her own information with group involvement @@ -24,13 +21,8 @@ Feature: get user own information And group "coffee-lover" has been created And user "Alice" has been added to group "tea-lover" And user "Alice" has been added to group "coffee-lover" - When the user "Alice" retrives her information using the Graph API - And the api response should contains the following information: - | displayName | Alice Hansen | - | id | %UUIDv4% | - | onPremisesSamAccountName | Alice | - | mail | alice@example.org | - | memberOf | tea-lover, coffee-lover | - - - + When the user "Alice" retrieves her information using the Graph API + Then the HTTP status code should be "200" + And the user retrieve API response should contain the following information: + | displayName | id | mail | onPremisesSamAccountName | memberOf | + | Alice Hansen | %uuid_v4% | alice@example.org | Alice | tea-lover, coffee-lover | diff --git a/tests/acceptance/features/bootstrap/GraphContext.php b/tests/acceptance/features/bootstrap/GraphContext.php index 47cc933c85..0ac34ba176 100644 --- a/tests/acceptance/features/bootstrap/GraphContext.php +++ b/tests/acceptance/features/bootstrap/GraphContext.php @@ -201,20 +201,6 @@ class GraphContext implements Context { } } - /** - * This method check if the userUUIDv4 is in correct pattern or not - * - * @param string $userUUIDv4 - * - * @return int - * @throws Exception - * @throws GuzzleException - */ - public function checkUUIDv4PatternForUserId(string $userUUIDv4): int { - $UUIDv4Regex = '/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i'; - return preg_match($UUIDv4Regex, $userUUIDv4); - } - /** * @param string $group * @@ -1024,7 +1010,7 @@ class GraphContext implements Context { string $user ):ResponseInterface { $credentials = $this->getAdminOrUserCredentials($user); - return GraphHelper::getUserInformation( + return GraphHelper::getOwnInformationAndGroupMemberships( $this->featureContext->getBaseUrl(), $this->featureContext->getStepLineRef(), $credentials["username"], @@ -1033,7 +1019,7 @@ class GraphContext implements Context { } /** - * @When /^the user "([^"]*)" retrives (:?her|his) information using the Graph API$/ + * @When /^the user "([^"]*)" retrieves (her|his) information using the Graph API$/ * * @param string $user * @@ -1048,55 +1034,62 @@ class GraphContext implements Context { } /** - * @Then /^the api response should contains the following information:$/ + * @Then /^the user retrieve API response should contain the following information:$/ * * @param TableNode $table * * @return void + * @throws GuzzleException */ - public function theApiResponseForUserShouldContainsTheFollowingInformation(TableNode $table): void { - $rows = $table->getRowsHash(); - $apiResponse = \json_decode((string)$this->featureContext->getResponse()->getBody(), true, 512, JSON_THROW_ON_ERROR); - // assertion of the user is member of groups - if ($rows['memberOf']) { - // collect memberOf from response - $memberOfFromApiReponse = []; - $memberOf = preg_split('/\s*,\s*/', trim($rows['memberOf'])); - foreach ($apiResponse['memberOf'] as $member) { - $memberOfFromApiReponse[] = $member['displayName']; - } - Assert::assertEqualsCanonicalizing($memberOf, $memberOfFromApiReponse); + public function theUserRetrieveApiResponseShouldContainTheFollowingInformation(TableNode $table): void { + $rows = $table->getHash(); + $apiResponse = $this->featureContext->getJsonDecodedResponse($this->featureContext->getResponse()); + foreach ($rows as $row) { + $this->checkUserInformation($row, $apiResponse); } - // check if the user_if from response is in format UUIDv4 - $isUUIDv4 = $this->featureContext->substituteInLineCodes( - $rows['id'], - $this->featureContext->getCurrentUser(), - [], - [ - [ - "code" => "%UUIDv4%", - "function" => - [$this, "checkUUIDv4PatternForUserId"], - "parameter" => [$apiResponse['id']] - ], - ] - ); - Assert::assertEquals( - 1, - $isUUIDv4, - __METHOD__ . - $apiResponse['id'] . ' ID is not in the format of UUIDv4' - ); + } - // assertion for remaining key other than 'memberOf' and - foreach (array_keys($rows) as $keyName) { - if ($keyName !== 'memberOf' && $keyName !== 'id') { - Assert::assertEquals( - $rows[$keyName], - $apiResponse[$keyName], - __METHOD__ . - ' Expected ' . $rows[$keyName] . ' but got ' . $apiResponse[$keyName] - ); + /** + * @param array $expectedValue + * @param array $actualValue + * + * @throws GuzzleException + * @return void + */ + public function checkUserInformation(array $expectedValue, array $actualValue):void { + foreach (array_keys($expectedValue) as $keyName) { + switch ($keyName) { + case "memberOf": + $memberOfFromApiReponse = []; + $memberOf = preg_split('/\s*,\s*/', trim($expectedValue['memberOf'])); + foreach ($actualValue['memberOf'] as $member) { + $memberOfFromApiReponse[] = $member['displayName']; + } + Assert::assertEqualsCanonicalizing($memberOf, $memberOfFromApiReponse); + break; + case "id": + if ($expectedValue[$keyName] !== '%uuid_v4%') { + throw new Error( + 'Only UUIDv4 patterned user id can be checked' . ' but got ' + . trim($expectedValue[$keyName], '%') + ); + } + Assert::assertEquals( + 1, + GraphHelper::isUUIDv4($actualValue['id']), + __METHOD__ . + ' Expected user_id to have UUIDv4 pattern but found: ' . $actualValue['id'] + ); + break; + default: + Assert::assertEquals( + $expectedValue[$keyName], + $actualValue[$keyName], + __METHOD__ . + ' Expected ' . $keyName . 'to have value' . $expectedValue[$keyName] + . ' but got ' . $actualValue[$keyName] + ); + break; } } }