From 9813fe2841f5352bd2675904a429d3f6afb4c463 Mon Sep 17 00:00:00 2001 From: "sagargurung1001@gmail.com" Date: Wed, 23 Nov 2022 15:18:42 +0545 Subject: [PATCH] Replace asserting userID with regex match --- ....feature => getUserOwnInformation.feature} | 10 ++-- .../features/bootstrap/GraphContext.php | 46 ++++++++++++++----- 2 files changed, 40 insertions(+), 16 deletions(-) rename tests/acceptance/features/apiGraph/{getUser.feature => getUserOwnInformation.feature} (79%) diff --git a/tests/acceptance/features/apiGraph/getUser.feature b/tests/acceptance/features/apiGraph/getUserOwnInformation.feature similarity index 79% rename from tests/acceptance/features/apiGraph/getUser.feature rename to tests/acceptance/features/apiGraph/getUserOwnInformation.feature index 96a71cb4b9..62e5f8e8e9 100644 --- a/tests/acceptance/features/apiGraph/getUser.feature +++ b/tests/acceptance/features/apiGraph/getUserOwnInformation.feature @@ -1,5 +1,5 @@ @api -Feature: get user information +Feature: get user own information As user I want to be able to retrieve my own information So that I can see my information @@ -11,9 +11,9 @@ Feature: get user information Scenario: user gets his/her own information with no group involvement When the user "Alice" retrives her information using the Graph API Then the HTTP status code should be "200" - And the api response for user "Alice" should contains the following information: + And the api response should contains the following information: | displayName | Alice Hansen | - | id | %user_id% | + | id | %UUIDv4% | | mail | alice@example.org | | onPremisesSamAccountName | Alice | | memberOf | | @@ -25,9 +25,9 @@ Feature: get user information 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 for user "Alice" should contains the following information: + And the api response should contains the following information: | displayName | Alice Hansen | - | id | %user_id% | + | id | %UUIDv4% | | onPremisesSamAccountName | Alice | | mail | alice@example.org | | memberOf | tea-lover, coffee-lover | diff --git a/tests/acceptance/features/bootstrap/GraphContext.php b/tests/acceptance/features/bootstrap/GraphContext.php index ad786df71a..47cc933c85 100644 --- a/tests/acceptance/features/bootstrap/GraphContext.php +++ b/tests/acceptance/features/bootstrap/GraphContext.php @@ -201,6 +201,20 @@ 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 * @@ -1034,14 +1048,13 @@ class GraphContext implements Context { } /** - * @Then /^the api response for user "([^"]*)" should contains the following information:$/ + * @Then /^the api response should contains the following information:$/ * - * @param string $user * @param TableNode $table * * @return void */ - public function theApiResponseForUserShouldContainsTheFollowingInformation(string $user, TableNode $table): void { + 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 @@ -1054,25 +1067,36 @@ class GraphContext implements Context { } Assert::assertEqualsCanonicalizing($memberOf, $memberOfFromApiReponse); } - // get user_id for the given user - $rows['id'] = $this->featureContext->substituteInLineCodes( + // check if the user_if from response is in format UUIDv4 + $isUUIDv4 = $this->featureContext->substituteInLineCodes( $rows['id'], $this->featureContext->getCurrentUser(), [], [ [ - "code" => "%user_id%", + "code" => "%UUIDv4%", "function" => - [$this->spacesContext, "getUserIdByUserName"], - "parameter" => [$user] + [$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' + // assertion for remaining key other than 'memberOf' and foreach (array_keys($rows) as $keyName) { - if ($keyName !== 'memberOf') { - Assert::assertEquals($rows[$keyName], $apiResponse[$keyName]); + if ($keyName !== 'memberOf' && $keyName !== 'id') { + Assert::assertEquals( + $rows[$keyName], + $apiResponse[$keyName], + __METHOD__ . + ' Expected ' . $rows[$keyName] . ' but got ' . $apiResponse[$keyName] + ); } } }