From 9f0a22af7e2d9b3a55900d9d3c5429cc61377f6f Mon Sep 17 00:00:00 2001 From: "sagargurung1001@gmail.com" Date: Tue, 29 Nov 2022 14:13:33 +0545 Subject: [PATCH 01/10] Added test for getting user information by admin using Graph API --- tests/TestHelpers/GraphHelper.php | 26 +++++ .../features/apiGraph/getUser.feature | 62 ++++++++++++ .../features/bootstrap/GraphContext.php | 97 +++++++++++++++++++ 3 files changed, 185 insertions(+) create mode 100644 tests/acceptance/features/apiGraph/getUser.feature diff --git a/tests/TestHelpers/GraphHelper.php b/tests/TestHelpers/GraphHelper.php index a81470095f..d2c49af5f5 100644 --- a/tests/TestHelpers/GraphHelper.php +++ b/tests/TestHelpers/GraphHelper.php @@ -242,6 +242,32 @@ class GraphHelper { ); } + /** + * @param string $baseUrl + * @param string $xRequestId + * @param string $adminUser + * @param string $adminPassword + * @param string $ofUser + * + * @return ResponseInterface + * @throws GuzzleException + */ + public static function getUserWithDriveInformation( + string $baseUrl, + string $xRequestId, + string $user, + string $userPassword, + string $ofUser + ): ResponseInterface { + $url = self::getFullUrl($baseUrl, 'users/'. $ofUser . '?%24select=&%24expand=drive'); + return HttpRequestHelper::get( + $url, + $xRequestId, + $user, + $userPassword, + ); + } + /** * @param string $baseUrl * @param string $xRequestId diff --git a/tests/acceptance/features/apiGraph/getUser.feature b/tests/acceptance/features/apiGraph/getUser.feature new file mode 100644 index 0000000000..dea0b77356 --- /dev/null +++ b/tests/acceptance/features/apiGraph/getUser.feature @@ -0,0 +1,62 @@ +@api @skipOnOcV10 +Feature: get users + As an admin + I want to be able to retrieve user information + So that I can see the information + + Background: + Given user "Alice" has been created with default attributes and without skeleton files + And the administrator has given "Alice" the role "Admin" using the settings api + + + Scenario: admin user tries get information of a user + Given user "Brian" has been created with default attributes and without skeleton files + When user "Alice" tries to get information of user "Brian" using 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 | + | Brian Murphy | %uuid_v4% | brian@example.org | Brian | + + + Scenario: non-admin user tries get information of a user + Given user "Brian" has been created with default attributes and without skeleton files + When user "Brian" tries to get information of user "Alice" using Graph API + Then the HTTP status code should be "200" + And the last response should be an unauthorized response + + + Scenario: admin user tries get all user + Given these users have been created with default attributes and without skeleton files: + | username | + | Brian | + | Carol | + | David | + When user "Alice" tries to get all user using the Graph API + Then the HTTP status code should be "200" + And the API response should contain all user with following information: + | displayName | id | mail | onPremisesSamAccountName | + | Brian Murphy | %uuid_v4% | brian@example.org | Brian | + | David Lopez | %uuid_v4% | david@example.org | David | + | Carol King | %uuid_v4% | carol@example.org | Carol | + + + Scenario: non-admin user tries get all user + Given these users have been created with default attributes and without skeleton files: + | username | + | Brian | + | Carol | + | David | + When user "Brian" tries to get all user using the Graph API + Then the HTTP status code should be "401" + And the last response should be an unauthorized response + + + Scenario: admin user tries to get drive data of another user + Given these users have been created with default attributes and without skeleton files: + | username | + | Brian | + When the user "Alice" tries to get information of user "Brian" along with his drive data using 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 | + | Brian Murphy | %uuid_v4% | brian@example.org | Brian | diff --git a/tests/acceptance/features/bootstrap/GraphContext.php b/tests/acceptance/features/bootstrap/GraphContext.php index 0eb723b37f..158f6dcfc4 100644 --- a/tests/acceptance/features/bootstrap/GraphContext.php +++ b/tests/acceptance/features/bootstrap/GraphContext.php @@ -1240,4 +1240,101 @@ class GraphContext implements Context { } } } + + /** + * @When user :user tries to get information of user :ofUser using Graph API + */ + public function userTriesToGetInformationOfUser($user, $ofUser) { + $credentials = $this->getAdminOrUserCredentials($user); + $response = GraphHelper::getUser( + $this->featureContext->getBaseUrl(), + $this->featureContext->getStepLineRef(), + $credentials['username'], + $credentials['password'], + $ofUser + ); + $this->featureContext->setResponse($response); + } + + + /** + * @When user :user tries to get all user using the Graph API + * + * @param string $user + * + * @return void + */ + public function userGetsAllUserUsingTheGraphApi(string $user) { + $credentials = $this->getAdminOrUserCredentials($user); + $response = GraphHelper::getUsers( + $this->featureContext->getBaseUrl(), + $this->featureContext->getStepLineRef(), + $credentials['username'], + $credentials['password'], + ); + $this->featureContext->setResponse($response); + } + + /** + * @Then the API response should contain all user with following information: + * + * @param TableNode $table + * + * @throws Exception + */ + public function theApiResponseShouldContainAllUserWithFollowingInformation(TableNode $table) { + $values = $table->getHash(); + $apiResponse = $this->featureContext->getJsonDecodedResponse($this->featureContext->getResponse())['value']; + foreach ($values as $expectedValue) { + $found = false; + foreach ($apiResponse as $key => $actualResponseValue) { + if ($expectedValue["displayName"] === $actualResponseValue["displayName"]) { + $found = true; + $this->checkUserInformation($expectedValue, $actualResponseValue); + unset($apiResponse[$key]); + break; + } + } + if (!$found) { + throw new Exception('User ' . $expectedValue["displayName"] . ' could not be found in the response.'); + } + } + } + + /** + * @param string $user + * @param string $ofUser + * + * @return ResponseInterface + * @throws JsonException + * @throws GuzzleException + */ + public function retrieveUserInformationAlongWithDriveUsingGraphApi( + string $user, + string $ofUser + ):ResponseInterface { + $credentials = $this->getAdminOrUserCredentials($user); + return GraphHelper::getUserWithDriveInformation( + $this->featureContext->getBaseUrl(), + $this->featureContext->getStepLineRef(), + $credentials["username"], + $credentials["password"], + $ofUser + ); + } + + /** + * + * @When /^the user "([^"]*)" tries to get information of user "([^"]*)" along with (his|her) drive data using Graph API$/ + * + * @param string $user + * @param string $ofUser + * + * @return void + */ + public function userTriesToGetInformationOfUserAlongWithHisDriveData(string $user, string $ofUser) + { + $response = $this->retrieveUserInformationAlongWithDriveUsingGraphApi($user, $ofUser); + $this->featureContext->setResponse($response); + } } From 6ff9d6196f71965b7425d8cdd13426d408967b7d Mon Sep 17 00:00:00 2001 From: "sagargurung1001@gmail.com" Date: Tue, 29 Nov 2022 16:39:10 +0545 Subject: [PATCH 02/10] Added expected to failure file --- tests/TestHelpers/GraphHelper.php | 50 ++--- ...ected-failures-localAPI-on-OCIS-storage.md | 3 + .../features/apiGraph/getUser.feature | 31 ++- .../features/bootstrap/GraphContext.php | 202 ++++++++++-------- 4 files changed, 168 insertions(+), 118 deletions(-) diff --git a/tests/TestHelpers/GraphHelper.php b/tests/TestHelpers/GraphHelper.php index d2c49af5f5..f38023a429 100644 --- a/tests/TestHelpers/GraphHelper.php +++ b/tests/TestHelpers/GraphHelper.php @@ -242,31 +242,31 @@ class GraphHelper { ); } - /** - * @param string $baseUrl - * @param string $xRequestId - * @param string $adminUser - * @param string $adminPassword - * @param string $ofUser - * - * @return ResponseInterface - * @throws GuzzleException - */ - public static function getUserWithDriveInformation( - string $baseUrl, - string $xRequestId, - string $user, - string $userPassword, - string $ofUser - ): ResponseInterface { - $url = self::getFullUrl($baseUrl, 'users/'. $ofUser . '?%24select=&%24expand=drive'); - return HttpRequestHelper::get( - $url, - $xRequestId, - $user, - $userPassword, - ); - } + /** + * @param string $baseUrl + * @param string $xRequestId + * @param string $user + * @param string $userPassword + * @param string|null $ofUser + * + * @return ResponseInterface + * @throws GuzzleException + */ + public static function getUserWithDriveInformation( + string $baseUrl, + string $xRequestId, + string $user, + string $userPassword, + ?string $ofUser = null + ): ResponseInterface { + $url = self::getFullUrl($baseUrl, 'users/' . $ofUser . '?%24select=&%24expand=drive'); + return HttpRequestHelper::get( + $url, + $xRequestId, + $user, + $userPassword, + ); + } /** * @param string $baseUrl diff --git a/tests/acceptance/expected-failures-localAPI-on-OCIS-storage.md b/tests/acceptance/expected-failures-localAPI-on-OCIS-storage.md index b611ea8c9a..cdbbcc1865 100644 --- a/tests/acceptance/expected-failures-localAPI-on-OCIS-storage.md +++ b/tests/acceptance/expected-failures-localAPI-on-OCIS-storage.md @@ -78,3 +78,6 @@ The expected failures in this file are from features in the owncloud/ocis repo. #### [Requests with invalid credentials do not return CORS headers](https://github.com/owncloud/ocis/issues/5194) - [apiCors/cors.feature:67](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiCors/cors.feature#L67) - [apiCors/cors.feature:68](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiCors/cors.feature#L68) + +#### [A User can get information of another user with Graph API](https://github.com/owncloud/ocis/issues/5125) +- [apiGraph/getUser.feature:21](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiGraph/getUser.feature#L21) diff --git a/tests/acceptance/features/apiGraph/getUser.feature b/tests/acceptance/features/apiGraph/getUser.feature index dea0b77356..05df4502d9 100644 --- a/tests/acceptance/features/apiGraph/getUser.feature +++ b/tests/acceptance/features/apiGraph/getUser.feature @@ -51,12 +51,39 @@ Feature: get users And the last response should be an unauthorized response - Scenario: admin user tries to get drive data of another user + Scenario: admin user tries to get drive information of a user Given these users have been created with default attributes and without skeleton files: | username | | Brian | - When the user "Alice" tries to get information of user "Brian" along with his drive data using Graph API + When the user "Alice" tries to get user "Brian" along with his drive information using 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 | | Brian Murphy | %uuid_v4% | brian@example.org | Brian | +# And the drive information of user "Brian" should contain: +# | key | value | +# | driveType | personal | +# | driveAlias | personal/alice | +# | id | %space_id% | +# | name | Brian Murphy | +# | quota@@@state | normal | +# | root@@@webDavUrl | %base_url%/dav/spaces/%space_id% | + + + Scenario: normal user tries to get hid/her own drive information + Given these users have been created with default attributes and without skeleton files: + | username | + | Brian | + When the user "Brian" tries to get his drive information using 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 | + | Brian Murphy | %uuid_v4% | brian@example.org | Brian | +# And the drive information of user "Brian" should contain: +# | key | value | +# | driveType | personal | +# | driveAlias | personal/brian | +# | id | %space_id% | +# | name | Brian Murphy | +# | quota@@@state | normal | +# | root@@@webDavUrl | %base_url%/dav/spaces/%space_id% | diff --git a/tests/acceptance/features/bootstrap/GraphContext.php b/tests/acceptance/features/bootstrap/GraphContext.php index 158f6dcfc4..2b2f251c64 100644 --- a/tests/acceptance/features/bootstrap/GraphContext.php +++ b/tests/acceptance/features/bootstrap/GraphContext.php @@ -1241,100 +1241,120 @@ class GraphContext implements Context { } } - /** - * @When user :user tries to get information of user :ofUser using Graph API - */ - public function userTriesToGetInformationOfUser($user, $ofUser) { - $credentials = $this->getAdminOrUserCredentials($user); - $response = GraphHelper::getUser( - $this->featureContext->getBaseUrl(), - $this->featureContext->getStepLineRef(), - $credentials['username'], - $credentials['password'], - $ofUser - ); - $this->featureContext->setResponse($response); - } + /** + * @When user :user tries to get information of user :ofUser using Graph API + * + * @param string $user + * @param string $ofUser + * + * @return void + */ + public function userTriesToGetInformationOfUser($user, $ofUser): void { + $credentials = $this->getAdminOrUserCredentials($user); + $response = GraphHelper::getUser( + $this->featureContext->getBaseUrl(), + $this->featureContext->getStepLineRef(), + $credentials['username'], + $credentials['password'], + $ofUser + ); + $this->featureContext->setResponse($response); + } + /** + * @When user :user tries to get all user using the Graph API + * + * @param string $user + * + * @return void + */ + public function userGetsAllUserUsingTheGraphApi(string $user) { + $credentials = $this->getAdminOrUserCredentials($user); + $response = GraphHelper::getUsers( + $this->featureContext->getBaseUrl(), + $this->featureContext->getStepLineRef(), + $credentials['username'], + $credentials['password'], + ); + $this->featureContext->setResponse($response); + } - /** - * @When user :user tries to get all user using the Graph API - * - * @param string $user - * - * @return void - */ - public function userGetsAllUserUsingTheGraphApi(string $user) { - $credentials = $this->getAdminOrUserCredentials($user); - $response = GraphHelper::getUsers( - $this->featureContext->getBaseUrl(), - $this->featureContext->getStepLineRef(), - $credentials['username'], - $credentials['password'], - ); - $this->featureContext->setResponse($response); - } + /** + * @Then the API response should contain all user with following information: + * + * @param TableNode $table + * + * @throws Exception + * @return void + */ + public function theApiResponseShouldContainAllUserWithFollowingInformation(TableNode $table): void { + $values = $table->getHash(); + $apiResponse = $this->featureContext->getJsonDecodedResponse($this->featureContext->getResponse())['value']; + foreach ($values as $expectedValue) { + $found = false; + foreach ($apiResponse as $key => $actualResponseValue) { + if ($expectedValue["displayName"] === $actualResponseValue["displayName"]) { + $found = true; + $this->checkUserInformation($expectedValue, $actualResponseValue); + unset($apiResponse[$key]); + break; + } + } + if (!$found) { + throw new Exception('User ' . $expectedValue["displayName"] . ' could not be found in the response.'); + } + } + } - /** - * @Then the API response should contain all user with following information: - * - * @param TableNode $table - * - * @throws Exception - */ - public function theApiResponseShouldContainAllUserWithFollowingInformation(TableNode $table) { - $values = $table->getHash(); - $apiResponse = $this->featureContext->getJsonDecodedResponse($this->featureContext->getResponse())['value']; - foreach ($values as $expectedValue) { - $found = false; - foreach ($apiResponse as $key => $actualResponseValue) { - if ($expectedValue["displayName"] === $actualResponseValue["displayName"]) { - $found = true; - $this->checkUserInformation($expectedValue, $actualResponseValue); - unset($apiResponse[$key]); - break; - } - } - if (!$found) { - throw new Exception('User ' . $expectedValue["displayName"] . ' could not be found in the response.'); - } - } - } + /** + * @param string $user + * @param string|null $ofUser + * + * @return ResponseInterface + * @throws JsonException + * @throws GuzzleException + */ + public function retrieveUserInformationAlongWithDriveUsingGraphApi( + string $user, + ?string $ofUser = null + ):ResponseInterface { + if ($ofUser === null) { + $ofUser = $user; + } + $credentials = $this->getAdminOrUserCredentials($user); + return GraphHelper::getUserWithDriveInformation( + $this->featureContext->getBaseUrl(), + $this->featureContext->getStepLineRef(), + $credentials["username"], + $credentials["password"], + $ofUser + ); + } - /** - * @param string $user - * @param string $ofUser - * - * @return ResponseInterface - * @throws JsonException - * @throws GuzzleException - */ - public function retrieveUserInformationAlongWithDriveUsingGraphApi( - string $user, - string $ofUser - ):ResponseInterface { - $credentials = $this->getAdminOrUserCredentials($user); - return GraphHelper::getUserWithDriveInformation( - $this->featureContext->getBaseUrl(), - $this->featureContext->getStepLineRef(), - $credentials["username"], - $credentials["password"], - $ofUser - ); - } + /** + * + * @When /^the user "([^"]*)" tries to get user "([^"]*)" along with (his|her) drive information using Graph API$/ + * + * @param string $user + * @param string $ofUser + * + * @return void + */ + public function userTriesToGetInformationOfUserAlongWithHisDriveData(string $user, $ofUser) { + $response = $this->retrieveUserInformationAlongWithDriveUsingGraphApi($user, $ofUser); + $this->featureContext->setResponse($response); + } - /** - * - * @When /^the user "([^"]*)" tries to get information of user "([^"]*)" along with (his|her) drive data using Graph API$/ - * - * @param string $user - * @param string $ofUser - * - * @return void - */ - public function userTriesToGetInformationOfUserAlongWithHisDriveData(string $user, string $ofUser) - { - $response = $this->retrieveUserInformationAlongWithDriveUsingGraphApi($user, $ofUser); - $this->featureContext->setResponse($response); - } + /** + * + * @When /^the user "([^"]*)" tries to get (his|her) drive information using Graph API$/ + * + * @param string $user + * + * @return void + */ + public function userTriesToGetOwnDriveInformation(string $user) { + $response = $this->retrieveUserInformationAlongWithDriveUsingGraphApi($user); + $this->featureContext->setResponse($response); + } } From c80e773234ef4d662e16884253cd5953c5fe7098 Mon Sep 17 00:00:00 2001 From: "sagargurung1001@gmail.com" Date: Wed, 30 Nov 2022 16:50:58 +0545 Subject: [PATCH 03/10] Added drive info check --- .../features/apiGraph/getUser.feature | 24 ++++++----------- .../features/bootstrap/GraphContext.php | 27 +++++++++++++++++++ .../features/bootstrap/SpacesContext.php | 3 +++ 3 files changed, 38 insertions(+), 16 deletions(-) diff --git a/tests/acceptance/features/apiGraph/getUser.feature b/tests/acceptance/features/apiGraph/getUser.feature index 05df4502d9..cc21ad5090 100644 --- a/tests/acceptance/features/apiGraph/getUser.feature +++ b/tests/acceptance/features/apiGraph/getUser.feature @@ -60,14 +60,10 @@ Feature: get users And the user retrieve API response should contain the following information: | displayName | id | mail | onPremisesSamAccountName | | Brian Murphy | %uuid_v4% | brian@example.org | Brian | -# And the drive information of user "Brian" should contain: -# | key | value | -# | driveType | personal | -# | driveAlias | personal/alice | -# | id | %space_id% | -# | name | Brian Murphy | -# | quota@@@state | normal | -# | root@@@webDavUrl | %base_url%/dav/spaces/%space_id% | + And the response should contain the following drive information: + | driveType | personal | + | driveAlias | personal/brian | + | name | Brian Murphy | Scenario: normal user tries to get hid/her own drive information @@ -79,11 +75,7 @@ Feature: get users And the user retrieve API response should contain the following information: | displayName | id | mail | onPremisesSamAccountName | | Brian Murphy | %uuid_v4% | brian@example.org | Brian | -# And the drive information of user "Brian" should contain: -# | key | value | -# | driveType | personal | -# | driveAlias | personal/brian | -# | id | %space_id% | -# | name | Brian Murphy | -# | quota@@@state | normal | -# | root@@@webDavUrl | %base_url%/dav/spaces/%space_id% | + And the response should contain the following drive information: + | driveType | personal | + | driveAlias | personal/brian | + | name | Brian Murphy | diff --git a/tests/acceptance/features/bootstrap/GraphContext.php b/tests/acceptance/features/bootstrap/GraphContext.php index 2b2f251c64..6f33387364 100644 --- a/tests/acceptance/features/bootstrap/GraphContext.php +++ b/tests/acceptance/features/bootstrap/GraphContext.php @@ -28,6 +28,11 @@ 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. @@ -43,6 +48,7 @@ class GraphContext implements Context { $environment = $scope->getEnvironment(); // Get all the contexts you need in this context from here $this->featureContext = $environment->getContext('FeatureContext'); + $this->spacesContext = $environment->getContext('SpacesContext'); } /** @@ -1357,4 +1363,25 @@ class GraphContext implements Context { $response = $this->retrieveUserInformationAlongWithDriveUsingGraphApi($user); $this->featureContext->setResponse($response); } + + + + /** + * @Then the response should contain the following drive information: + */ + public function theResponseShouldContainTheFollowingDriveInformation(TableNode $table) + { + $rows = $table->getRowsHash(); + $responseAPI = $this->featureContext->getJsonDecodedResponse($this->featureContext->getResponse())['drive']; + foreach (array_keys($rows) as $keyName) { + Assert::assertEquals( + $rows[$keyName], + $responseAPI[$keyName], + __METHOD__ . + ' Expected ' . $keyName . 'to have value' . $responseAPI[$keyName] + . ' but got ' . $rows[$keyName] + ); + } + } + } diff --git a/tests/acceptance/features/bootstrap/SpacesContext.php b/tests/acceptance/features/bootstrap/SpacesContext.php index c0e7b23711..ba7e541313 100644 --- a/tests/acceptance/features/bootstrap/SpacesContext.php +++ b/tests/acceptance/features/bootstrap/SpacesContext.php @@ -913,6 +913,9 @@ class SpacesContext implements Context { foreach ($table->getHash() as $row) { // remember the original Space Array $original = $spaceAsArray; + var_dump( + $original + ); $row['value'] = $this->featureContext->substituteInLineCodes( $row['value'], $this->featureContext->getCurrentUser(), From 265db41819e6d19649ecc07f1bfe5f15f47e7307 Mon Sep 17 00:00:00 2001 From: "sagargurung1001@gmail.com" Date: Fri, 2 Dec 2022 09:45:55 +0545 Subject: [PATCH 04/10] remove before --- tests/acceptance/features/bootstrap/GraphContext.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/acceptance/features/bootstrap/GraphContext.php b/tests/acceptance/features/bootstrap/GraphContext.php index 6f33387364..9fa1777fff 100644 --- a/tests/acceptance/features/bootstrap/GraphContext.php +++ b/tests/acceptance/features/bootstrap/GraphContext.php @@ -48,7 +48,6 @@ class GraphContext implements Context { $environment = $scope->getEnvironment(); // Get all the contexts you need in this context from here $this->featureContext = $environment->getContext('FeatureContext'); - $this->spacesContext = $environment->getContext('SpacesContext'); } /** From 46575abe11fba2faaa2d16566de3b202579e46c1 Mon Sep 17 00:00:00 2001 From: "sagargurung1001@gmail.com" Date: Wed, 7 Dec 2022 16:28:25 +0545 Subject: [PATCH 05/10] Drive infor assertion --- .../features/apiGraph/getUser.feature | 40 +++++++++------- .../features/bootstrap/GraphContext.php | 46 +++++++++++++------ 2 files changed, 56 insertions(+), 30 deletions(-) diff --git a/tests/acceptance/features/apiGraph/getUser.feature b/tests/acceptance/features/apiGraph/getUser.feature index cc21ad5090..8aa64d2b9f 100644 --- a/tests/acceptance/features/apiGraph/getUser.feature +++ b/tests/acceptance/features/apiGraph/getUser.feature @@ -60,22 +60,28 @@ Feature: get users And the user retrieve API response should contain the following information: | displayName | id | mail | onPremisesSamAccountName | | Brian Murphy | %uuid_v4% | brian@example.org | Brian | - And the response should contain the following drive information: - | driveType | personal | - | driveAlias | personal/brian | - | name | Brian Murphy | + And the user retrieve API response should contain the following drive information: +# | driveType | personal | +# | driveAlias | personal/brian | +# | id | %space_id% | +# | name | Brian Murphy | + | owner@@@user@@@id | %user_id% | +# | quota@@@state | normal | +# | root@@@id | %space_id% | +# | root@@@webDavUrl | %base_url%/dav/spaces/%space_id% | +# | webUrl | %base_url%/f/%space_id% | - Scenario: normal user tries to get hid/her own drive information - Given these users have been created with default attributes and without skeleton files: - | username | - | Brian | - When the user "Brian" tries to get his drive information using 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 | - | Brian Murphy | %uuid_v4% | brian@example.org | Brian | - And the response should contain the following drive information: - | driveType | personal | - | driveAlias | personal/brian | - | name | Brian Murphy | +# Scenario: normal user tries to get hid/her own drive information +# Given these users have been created with default attributes and without skeleton files: +# | username | +# | Brian | +# When the user "Brian" tries to get his drive information using 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 | +# | Brian Murphy | %uuid_v4% | brian@example.org | Brian | +# And the response should contain the following drive information: +# | driveType | personal | +# | driveAlias | personal/brian | +# | name | Brian Murphy | diff --git a/tests/acceptance/features/bootstrap/GraphContext.php b/tests/acceptance/features/bootstrap/GraphContext.php index 9fa1777fff..a37cc1642d 100644 --- a/tests/acceptance/features/bootstrap/GraphContext.php +++ b/tests/acceptance/features/bootstrap/GraphContext.php @@ -1364,23 +1364,43 @@ class GraphContext implements Context { } - /** - * @Then the response should contain the following drive information: + * @param array $expectedDriveInformation + * @param array $actualValueDriveInformation + * + * @throws GuzzleException + * @return void */ - public function theResponseShouldContainTheFollowingDriveInformation(TableNode $table) - { - $rows = $table->getRowsHash(); - $responseAPI = $this->featureContext->getJsonDecodedResponse($this->featureContext->getResponse())['drive']; - foreach (array_keys($rows) as $keyName) { - Assert::assertEquals( - $rows[$keyName], - $responseAPI[$keyName], - __METHOD__ . - ' Expected ' . $keyName . 'to have value' . $responseAPI[$keyName] - . ' but got ' . $rows[$keyName] + public function checkUserDriveInformation(array $expectedDriveInformation, array $actualValueDriveInformation):void { + foreach (array_keys($expectedDriveInformation) as $keyName) { + // break the segment with @@@ here + $separatedKey = explode("@@@",$keyName); + $actualKeyAvalue = null; + $actualKeyAvalue = $actualValueDriveInformation['owner']; + var_dump( + $actualKeyAvalue + ); + foreach ($separatedKey as $key) { + // this loop sets and get the actual keyvalue from the actual API response + $actualKeyAvalue = $actualValueDriveInformation[$key]; + } + var_dump( + $actualKeyAvalue ); } } + + + /** + * @Then the user retrieve API response should contain the following drive information: + */ + public function theResponseShouldContainTheFollowingDriveInformation(TableNode $table) + { + $expectedDriveInformation = $table->getRowsHash(); + // array of user drive information (Personal Drive Information Only) + $actualDriveInformation = $this->featureContext->getJsonDecodedResponse($this->featureContext->getResponse())['drive']; + $this->checkUserDriveInformation($expectedDriveInformation, $actualDriveInformation); + } + } From e15afb04d4e182f62c1c90d2d801e7f1e2051d91 Mon Sep 17 00:00:00 2001 From: "sagargurung1001@gmail.com" Date: Fri, 16 Dec 2022 16:29:49 +0545 Subject: [PATCH 06/10] Implement user drive information --- tests/TestHelpers/GraphHelper.php | 29 ++++- .../features/apiGraph/getUser.feature | 48 ++++--- .../features/bootstrap/GraphContext.php | 120 ++++++++++++------ .../features/bootstrap/SpacesContext.php | 3 - 4 files changed, 134 insertions(+), 66 deletions(-) diff --git a/tests/TestHelpers/GraphHelper.php b/tests/TestHelpers/GraphHelper.php index f38023a429..9b633505cb 100644 --- a/tests/TestHelpers/GraphHelper.php +++ b/tests/TestHelpers/GraphHelper.php @@ -26,16 +26,43 @@ class GraphHelper { ]; } + /** + * + * @return string + */ + public static function getUUIDv4Regex(): string { + return '[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}'; + } + /** * @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'; + $regex = "/^" . self::getUUIDv4Regex() . "/i"; return preg_match($regex, $id); } + /** + * @param string $spaceId + * + * @return int (1 = true | 0 = false) + */ + public static function isSpaceId(string $spaceId): int { + $regex = "/^" . self::getUUIDv4Regex() . '\\$' . self::getUUIDv4Regex() . "/i"; + return preg_match($regex, $spaceId); + } + + /** + * @param string $id + * + * @return string + */ + public static function isUUIDv44(string $id): string { + return "hello"; + } + /** * @param string $baseUrl * @param string $path diff --git a/tests/acceptance/features/apiGraph/getUser.feature b/tests/acceptance/features/apiGraph/getUser.feature index 8aa64d2b9f..96e1a41d25 100644 --- a/tests/acceptance/features/apiGraph/getUser.feature +++ b/tests/acceptance/features/apiGraph/getUser.feature @@ -61,27 +61,33 @@ Feature: get users | displayName | id | mail | onPremisesSamAccountName | | Brian Murphy | %uuid_v4% | brian@example.org | Brian | And the user retrieve API response should contain the following drive information: -# | driveType | personal | -# | driveAlias | personal/brian | -# | id | %space_id% | -# | name | Brian Murphy | + | driveType | personal | + | driveAlias | personal/brian | + | id | %space_id% | + | name | Brian Murphy | | owner@@@user@@@id | %user_id% | -# | quota@@@state | normal | -# | root@@@id | %space_id% | -# | root@@@webDavUrl | %base_url%/dav/spaces/%space_id% | -# | webUrl | %base_url%/f/%space_id% | + | quota@@@state | normal | + | root@@@id | %space_id% | + | root@@@webDavUrl | %base_url%/dav/spaces/%space_id% | + | webUrl | %base_url%/f/%space_id% | -# Scenario: normal user tries to get hid/her own drive information -# Given these users have been created with default attributes and without skeleton files: -# | username | -# | Brian | -# When the user "Brian" tries to get his drive information using 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 | -# | Brian Murphy | %uuid_v4% | brian@example.org | Brian | -# And the response should contain the following drive information: -# | driveType | personal | -# | driveAlias | personal/brian | -# | name | Brian Murphy | + Scenario: normal user tries to get hid/her own drive information + Given these users have been created with default attributes and without skeleton files: + | username | + | Brian | + When the user "Brian" tries to get his drive information using 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 | + | Brian Murphy | %uuid_v4% | brian@example.org | Brian | + And the user retrieve API response should contain the following drive information: + | driveType | personal | + | driveAlias | personal/brian | + | id | %space_id% | + | name | Brian Murphy | + | owner@@@user@@@id | %user_id% | + | quota@@@state | normal | + | root@@@id | %space_id% | + | root@@@webDavUrl | %base_url%/dav/spaces/%space_id% | + | webUrl | %base_url%/f/%space_id% | diff --git a/tests/acceptance/features/bootstrap/GraphContext.php b/tests/acceptance/features/bootstrap/GraphContext.php index a37cc1642d..f66678ee18 100644 --- a/tests/acceptance/features/bootstrap/GraphContext.php +++ b/tests/acceptance/features/bootstrap/GraphContext.php @@ -28,10 +28,10 @@ class GraphContext implements Context { */ private FeatureContext $featureContext; - /** - * @var SpacesContext - */ - private SpacesContext $spacesContext; + /** + * @var SpacesContext + */ + private SpacesContext $spacesContext; /** * This will run before EVERY scenario. @@ -1363,44 +1363,82 @@ class GraphContext implements Context { $this->featureContext->setResponse($response); } + /** + * @param array $driveInformation + * + * @return string + */ + public static function getSpaceIdFromActualDriveinformation(array $driveInformation): string { + return $driveInformation['id']; + } - /** - * @param array $expectedDriveInformation - * @param array $actualValueDriveInformation - * - * @throws GuzzleException - * @return void - */ - public function checkUserDriveInformation(array $expectedDriveInformation, array $actualValueDriveInformation):void { - foreach (array_keys($expectedDriveInformation) as $keyName) { - // break the segment with @@@ here - $separatedKey = explode("@@@",$keyName); - $actualKeyAvalue = null; - $actualKeyAvalue = $actualValueDriveInformation['owner']; - var_dump( - $actualKeyAvalue - ); - foreach ($separatedKey as $key) { - // this loop sets and get the actual keyvalue from the actual API response - $actualKeyAvalue = $actualValueDriveInformation[$key]; - } - var_dump( - $actualKeyAvalue - ); - } - } + /** + * check if single drive information is correct + * + * @param array $expectedDriveInformation + * @param array $actualValueDriveInformation + * + * @return void + */ + public function checkUserDriveInformation(array $expectedDriveInformation, array $actualValueDriveInformation):void { + foreach (array_keys($expectedDriveInformation) as $keyName) { + // break the segment with @@@ to find the actual value from the drive infromation from response + $separatedKey = explode("@@@", $keyName); + // this stores the actual value of each key from drive information response used for assertion + $actualKeyValueFromDriveInformation = null; + $tempDriveInfo = $actualValueDriveInformation; + foreach ($separatedKey as $key) { + $actualKeyValueFromDriveInformation = $tempDriveInfo[$key]; + $tempDriveInfo = $actualKeyValueFromDriveInformation; + } + switch ($expectedDriveInformation[$keyName]) { + case '%user_id%': + Assert::assertEquals( + 1, + GraphHelper::isUUIDv4($actualKeyValueFromDriveInformation), + __METHOD__ . + ' Expected user_id to have UUIDv4 pattern but found: ' . $actualKeyValueFromDriveInformation + ); + break; + case '%space_id%': + Assert::assertEquals( + 1, + GraphHelper::isSpaceId($actualKeyValueFromDriveInformation), + __METHOD__ . + ' Expected user_id to have UUIDv4 pattern but found: ' . $actualKeyValueFromDriveInformation + ); + break; + default: + $expectedDriveInformation[$keyName] = $this->featureContext->substituteInLineCodes( + $expectedDriveInformation[$keyName], + $this->featureContext->getCurrentUser(), + [], + [ + [ + "code" => "%space_id%", + "function" => + [$this, "getSpaceIdFromActualDriveinformation"], + "parameter" => [$actualValueDriveInformation] + ], + ] + ); + Assert::assertEquals($expectedDriveInformation[$keyName], $actualKeyValueFromDriveInformation); + } + } + } - - /** - * @Then the user retrieve API response should contain the following drive information: - */ - public function theResponseShouldContainTheFollowingDriveInformation(TableNode $table) - { - $expectedDriveInformation = $table->getRowsHash(); - // array of user drive information (Personal Drive Information Only) - $actualDriveInformation = $this->featureContext->getJsonDecodedResponse($this->featureContext->getResponse())['drive']; - $this->checkUserDriveInformation($expectedDriveInformation, $actualDriveInformation); - } - + /** + * @param TableNode $table + * + * @Then the user retrieve API response should contain the following drive information: + * + * @return void + */ + public function theResponseShouldContainTheFollowingDriveInformation(TableNode $table): void { + $expectedDriveInformation = $table->getRowsHash(); + // array of user drive information (Personal Drive Information Only) + $actualDriveInformation = $this->featureContext->getJsonDecodedResponse($this->featureContext->getResponse())['drive']; + $this->checkUserDriveInformation($expectedDriveInformation, $actualDriveInformation); + } } diff --git a/tests/acceptance/features/bootstrap/SpacesContext.php b/tests/acceptance/features/bootstrap/SpacesContext.php index ba7e541313..c0e7b23711 100644 --- a/tests/acceptance/features/bootstrap/SpacesContext.php +++ b/tests/acceptance/features/bootstrap/SpacesContext.php @@ -913,9 +913,6 @@ class SpacesContext implements Context { foreach ($table->getHash() as $row) { // remember the original Space Array $original = $spaceAsArray; - var_dump( - $original - ); $row['value'] = $this->featureContext->substituteInLineCodes( $row['value'], $this->featureContext->getCurrentUser(), From 1874280a560805ccc5dfe498efba153e3ecee8a9 Mon Sep 17 00:00:00 2001 From: "sagargurung1001@gmail.com" Date: Mon, 19 Dec 2022 11:34:25 +0545 Subject: [PATCH 07/10] Addressed review --- tests/TestHelpers/GraphHelper.php | 25 +++---- ...ected-failures-localAPI-on-OCIS-storage.md | 2 +- .../features/apiGraph/getUser.feature | 68 +++++++------------ .../features/bootstrap/GraphContext.php | 50 +++++++------- 4 files changed, 60 insertions(+), 85 deletions(-) diff --git a/tests/TestHelpers/GraphHelper.php b/tests/TestHelpers/GraphHelper.php index 9b633505cb..4d783da4c9 100644 --- a/tests/TestHelpers/GraphHelper.php +++ b/tests/TestHelpers/GraphHelper.php @@ -40,7 +40,7 @@ class GraphHelper { * @return int (1 = true | 0 = false) */ public static function isUUIDv4(string $id): int { - $regex = "/^" . self::getUUIDv4Regex() . "/i"; + $regex = "/^" . self::getUUIDv4Regex() . "$/i"; return preg_match($regex, $id); } @@ -50,19 +50,10 @@ class GraphHelper { * @return int (1 = true | 0 = false) */ public static function isSpaceId(string $spaceId): int { - $regex = "/^" . self::getUUIDv4Regex() . '\\$' . self::getUUIDv4Regex() . "/i"; + $regex = "/^" . self::getUUIDv4Regex() . '\\$' . self::getUUIDv4Regex() . "$/i"; return preg_match($regex, $spaceId); } - /** - * @param string $id - * - * @return string - */ - public static function isUUIDv44(string $id): string { - return "hello"; - } - /** * @param string $baseUrl * @param string $path @@ -272,9 +263,9 @@ class GraphHelper { /** * @param string $baseUrl * @param string $xRequestId - * @param string $user + * @param string $byUser * @param string $userPassword - * @param string|null $ofUser + * @param string|null $user * * @return ResponseInterface * @throws GuzzleException @@ -282,15 +273,15 @@ class GraphHelper { public static function getUserWithDriveInformation( string $baseUrl, string $xRequestId, - string $user, + string $byUser, string $userPassword, - ?string $ofUser = null + ?string $user = null ): ResponseInterface { - $url = self::getFullUrl($baseUrl, 'users/' . $ofUser . '?%24select=&%24expand=drive'); + $url = self::getFullUrl($baseUrl, 'users/' . $user . '?%24select=&%24expand=drive'); return HttpRequestHelper::get( $url, $xRequestId, - $user, + $byUser, $userPassword, ); } diff --git a/tests/acceptance/expected-failures-localAPI-on-OCIS-storage.md b/tests/acceptance/expected-failures-localAPI-on-OCIS-storage.md index cdbbcc1865..4073214d05 100644 --- a/tests/acceptance/expected-failures-localAPI-on-OCIS-storage.md +++ b/tests/acceptance/expected-failures-localAPI-on-OCIS-storage.md @@ -80,4 +80,4 @@ The expected failures in this file are from features in the owncloud/ocis repo. - [apiCors/cors.feature:68](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiCors/cors.feature#L68) #### [A User can get information of another user with Graph API](https://github.com/owncloud/ocis/issues/5125) -- [apiGraph/getUser.feature:21](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiGraph/getUser.feature#L21) +- [apiGraph/getUser.feature:23](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiGraph/getUser.feature#L23) diff --git a/tests/acceptance/features/apiGraph/getUser.feature b/tests/acceptance/features/apiGraph/getUser.feature index 96e1a41d25..4a849165a8 100644 --- a/tests/acceptance/features/apiGraph/getUser.feature +++ b/tests/acceptance/features/apiGraph/getUser.feature @@ -5,57 +5,44 @@ Feature: get users So that I can see the information Background: - Given user "Alice" has been created with default attributes and without skeleton files + Given these users have been created with default attributes and without skeleton files: + | username | + | Alice | + | Brian | And the administrator has given "Alice" the role "Admin" using the settings api - Scenario: admin user tries get information of a user - Given user "Brian" has been created with default attributes and without skeleton files - When user "Alice" tries to get information of user "Brian" using Graph API + Scenario: admin user gets the information of a user + When user "Alice" gets information of user "Brian" using 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 | | Brian Murphy | %uuid_v4% | brian@example.org | Brian | - Scenario: non-admin user tries get information of a user - Given user "Brian" has been created with default attributes and without skeleton files + Scenario: non-admin user tries to get the information of a user When user "Brian" tries to get information of user "Alice" using Graph API - Then the HTTP status code should be "200" - And the last response should be an unauthorized response - - - Scenario: admin user tries get all user - Given these users have been created with default attributes and without skeleton files: - | username | - | Brian | - | Carol | - | David | - When user "Alice" tries to get all user using the Graph API - Then the HTTP status code should be "200" - And the API response should contain all user with following information: - | displayName | id | mail | onPremisesSamAccountName | - | Brian Murphy | %uuid_v4% | brian@example.org | Brian | - | David Lopez | %uuid_v4% | david@example.org | David | - | Carol King | %uuid_v4% | carol@example.org | Carol | - - - Scenario: non-admin user tries get all user - Given these users have been created with default attributes and without skeleton files: - | username | - | Brian | - | Carol | - | David | - When user "Brian" tries to get all user using the Graph API Then the HTTP status code should be "401" And the last response should be an unauthorized response - Scenario: admin user tries to get drive information of a user - Given these users have been created with default attributes and without skeleton files: - | username | - | Brian | - When the user "Alice" tries to get user "Brian" along with his drive information using Graph API + 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: + | displayName | id | mail | onPremisesSamAccountName | + | Alice Hansen | %uuid_v4% | alice@example.org | Alice | + | Brian Murphy | %uuid_v4% | brian@example.org | Brian | + + + Scenario: non-admin user tries get all user + When user "Brian" tries to get all users using the Graph API + Then the HTTP status code should be "401" + And the last response should be an unauthorized response + + + Scenario: admin user gets the drive information of a user + When the user "Alice" gets user "Brian" along with his drive information using 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 | @@ -72,11 +59,8 @@ Feature: get users | webUrl | %base_url%/f/%space_id% | - Scenario: normal user tries to get hid/her own drive information - Given these users have been created with default attributes and without skeleton files: - | username | - | Brian | - When the user "Brian" tries to get his drive information using Graph API + Scenario: normal user gets his/her own drive information + When the user "Brian" gets his drive information using 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 | diff --git a/tests/acceptance/features/bootstrap/GraphContext.php b/tests/acceptance/features/bootstrap/GraphContext.php index f66678ee18..2d7e466083 100644 --- a/tests/acceptance/features/bootstrap/GraphContext.php +++ b/tests/acceptance/features/bootstrap/GraphContext.php @@ -1247,31 +1247,35 @@ class GraphContext implements Context { } /** - * @When user :user tries to get information of user :ofUser using Graph API + * @When user :byUser tries to get information of user :user using Graph API + * @When user :byUser gets information of user :user using Graph API * + * @param string $byUser * @param string $user - * @param string $ofUser * * @return void + * @throws GuzzleException */ - public function userTriesToGetInformationOfUser($user, $ofUser): void { - $credentials = $this->getAdminOrUserCredentials($user); + public function userTriesToGetInformationOfUser(string $byUser, string $user): void { + $credentials = $this->getAdminOrUserCredentials($byUser); $response = GraphHelper::getUser( $this->featureContext->getBaseUrl(), $this->featureContext->getStepLineRef(), $credentials['username'], $credentials['password'], - $ofUser + $user ); $this->featureContext->setResponse($response); } /** - * @When user :user tries to get all user using the Graph API + * @When user :user tries to get all users using the Graph API + * @When user :user gets all users using the Graph API * * @param string $user * * @return void + * @throws GuzzleException */ public function userGetsAllUserUsingTheGraphApi(string $user) { $credentials = $this->getAdminOrUserCredentials($user); @@ -1285,7 +1289,7 @@ class GraphContext implements Context { } /** - * @Then the API response should contain all user with following information: + * @Then the API response should contain all users with the following information: * * @param TableNode $table * @@ -1312,47 +1316,44 @@ class GraphContext implements Context { } /** - * @param string $user - * @param string|null $ofUser + * @param string $byUser + * @param string|null $user * * @return ResponseInterface * @throws JsonException * @throws GuzzleException */ public function retrieveUserInformationAlongWithDriveUsingGraphApi( - string $user, - ?string $ofUser = null + string $byUser, + ?string $user = null ):ResponseInterface { - if ($ofUser === null) { - $ofUser = $user; - } + $user = $user ?? $byUser; $credentials = $this->getAdminOrUserCredentials($user); return GraphHelper::getUserWithDriveInformation( $this->featureContext->getBaseUrl(), $this->featureContext->getStepLineRef(), $credentials["username"], $credentials["password"], - $ofUser + $user ); } /** + * @When /^the user "([^"]*)" gets user "([^"]*)" along with (his|her) drive information using Graph API$/ * - * @When /^the user "([^"]*)" tries to get user "([^"]*)" along with (his|her) drive information using Graph API$/ - * + * @param string $byUser * @param string $user - * @param string $ofUser * * @return void */ - public function userTriesToGetInformationOfUserAlongWithHisDriveData(string $user, $ofUser) { - $response = $this->retrieveUserInformationAlongWithDriveUsingGraphApi($user, $ofUser); + public function userTriesToGetInformationOfUserAlongWithHisDriveData(string $byUser, string $user) { + $response = $this->retrieveUserInformationAlongWithDriveUsingGraphApi($byUser, $user); $this->featureContext->setResponse($response); } /** * - * @When /^the user "([^"]*)" tries to get (his|her) drive information using Graph API$/ + * @When /^the user "([^"]*)" gets (his|her) drive information using Graph API$/ * * @param string $user * @@ -1385,13 +1386,12 @@ class GraphContext implements Context { // break the segment with @@@ to find the actual value from the drive infromation from response $separatedKey = explode("@@@", $keyName); // this stores the actual value of each key from drive information response used for assertion - $actualKeyValueFromDriveInformation = null; - $tempDriveInfo = $actualValueDriveInformation; + $actualKeyValueFromDriveInformation = $actualValueDriveInformation; foreach ($separatedKey as $key) { - $actualKeyValueFromDriveInformation = $tempDriveInfo[$key]; - $tempDriveInfo = $actualKeyValueFromDriveInformation; + $actualKeyValueFromDriveInformation = $actualKeyValueFromDriveInformation[$key]; } + switch ($expectedDriveInformation[$keyName]) { case '%user_id%': Assert::assertEquals( From a05a746297cbff4dd3e4e2c36c6849fa498440f3 Mon Sep 17 00:00:00 2001 From: "sagargurung1001@gmail.com" Date: Wed, 21 Dec 2022 16:55:14 +0545 Subject: [PATCH 08/10] Addressed review --- tests/TestHelpers/GraphHelper.php | 12 +++---- .../features/bootstrap/GraphContext.php | 34 ++++++------------- 2 files changed, 16 insertions(+), 30 deletions(-) diff --git a/tests/TestHelpers/GraphHelper.php b/tests/TestHelpers/GraphHelper.php index 4d783da4c9..c80e5f8508 100644 --- a/tests/TestHelpers/GraphHelper.php +++ b/tests/TestHelpers/GraphHelper.php @@ -37,21 +37,21 @@ class GraphHelper { /** * @param string $id * - * @return int (1 = true | 0 = false) + * @return bool */ - public static function isUUIDv4(string $id): int { + public static function isUUIDv4(string $id): bool { $regex = "/^" . self::getUUIDv4Regex() . "$/i"; - return preg_match($regex, $id); + return (bool)preg_match($regex, $id); } /** * @param string $spaceId * - * @return int (1 = true | 0 = false) + * @return bool */ - public static function isSpaceId(string $spaceId): int { + public static function isSpaceId(string $spaceId): bool { $regex = "/^" . self::getUUIDv4Regex() . '\\$' . self::getUUIDv4Regex() . "$/i"; - return preg_match($regex, $spaceId); + return (bool)preg_match($regex, $spaceId); } /** diff --git a/tests/acceptance/features/bootstrap/GraphContext.php b/tests/acceptance/features/bootstrap/GraphContext.php index 2d7e466083..80353269ea 100644 --- a/tests/acceptance/features/bootstrap/GraphContext.php +++ b/tests/acceptance/features/bootstrap/GraphContext.php @@ -1226,12 +1226,7 @@ class GraphContext implements Context { . trim($expectedValue[$keyName], '%') ); } - Assert::assertEquals( - 1, - GraphHelper::isUUIDv4($actualValue['id']), - __METHOD__ . - ' Expected user_id to have UUIDv4 pattern but found: ' . $actualValue['id'] - ); + Assert::assertTrue(GraphHelper::isUUIDv4($actualValue['id']), __METHOD__ . ' Expected user_id to have UUIDv4 pattern but found: ' . $actualValue['id']); break; default: Assert::assertEquals( @@ -1377,37 +1372,27 @@ class GraphContext implements Context { * check if single drive information is correct * * @param array $expectedDriveInformation - * @param array $actualValueDriveInformation + * @param array $actualDriveInformation * * @return void */ - public function checkUserDriveInformation(array $expectedDriveInformation, array $actualValueDriveInformation):void { + public function checkUserDriveInformation(array $expectedDriveInformation, array $actualDriveInformation):void { foreach (array_keys($expectedDriveInformation) as $keyName) { // break the segment with @@@ to find the actual value from the drive infromation from response $separatedKey = explode("@@@", $keyName); // this stores the actual value of each key from drive information response used for assertion - $actualKeyValueFromDriveInformation = $actualValueDriveInformation; + $actualKeyValue = $actualDriveInformation; foreach ($separatedKey as $key) { - $actualKeyValueFromDriveInformation = $actualKeyValueFromDriveInformation[$key]; + $actualKeyValue = $actualKeyValue[$key]; } switch ($expectedDriveInformation[$keyName]) { case '%user_id%': - Assert::assertEquals( - 1, - GraphHelper::isUUIDv4($actualKeyValueFromDriveInformation), - __METHOD__ . - ' Expected user_id to have UUIDv4 pattern but found: ' . $actualKeyValueFromDriveInformation - ); + Assert::assertTrue(GraphHelper::isUUIDv4($actualKeyValue), __METHOD__ . ' Expected user_id to have UUIDv4 pattern but found: ' . $actualKeyValue); break; case '%space_id%': - Assert::assertEquals( - 1, - GraphHelper::isSpaceId($actualKeyValueFromDriveInformation), - __METHOD__ . - ' Expected user_id to have UUIDv4 pattern but found: ' . $actualKeyValueFromDriveInformation - ); + Assert::assertTrue(GraphHelper::isSpaceId($actualKeyValue), __METHOD__ . ' Expected space_id to have a UUIDv4:UUIDv4 pattern but found: ' . $actualKeyValue); break; default: $expectedDriveInformation[$keyName] = $this->featureContext->substituteInLineCodes( @@ -1416,14 +1401,15 @@ class GraphContext implements Context { [], [ [ + // the actual space_id is substituted from the actual drive information rather than making an API request and substituting "code" => "%space_id%", "function" => [$this, "getSpaceIdFromActualDriveinformation"], - "parameter" => [$actualValueDriveInformation] + "parameter" => [$actualDriveInformation] ], ] ); - Assert::assertEquals($expectedDriveInformation[$keyName], $actualKeyValueFromDriveInformation); + Assert::assertEquals($expectedDriveInformation[$keyName], $actualKeyValue); } } } From b9fda28235ce52dc3e79307d35604bfb5bf01336 Mon Sep 17 00:00:00 2001 From: "sagargurung1001@gmail.com" Date: Wed, 28 Dec 2022 14:47:53 +0545 Subject: [PATCH 09/10] Review address for separate functon --- tests/TestHelpers/GraphHelper.php | 22 +++++++++++++++++++ .../features/bootstrap/GraphContext.php | 18 ++++++--------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/tests/TestHelpers/GraphHelper.php b/tests/TestHelpers/GraphHelper.php index c80e5f8508..2fbf2c74aa 100644 --- a/tests/TestHelpers/GraphHelper.php +++ b/tests/TestHelpers/GraphHelper.php @@ -54,6 +54,28 @@ class GraphHelper { return (bool)preg_match($regex, $spaceId); } + /** + * Key name can consist of @@@ + * This function separate such key and return its actual value from actual drive response which can be used for assertion + * + * @param string $keyName + * @param array $actualDriveInformation + * + * @return string + */ + public static function separateAndGetValueForKey(string $keyName, array $actualDriveInformation): string { + // break the segment with @@@ to find the actual value from the actual drive infromation + $separatedKey = explode("@@@", $keyName); + // this stores the actual value of each key from drive information response used for assertion + $actualKeyValue = $actualDriveInformation; + + foreach ($separatedKey as $key) { + $actualKeyValue = $actualKeyValue[$key]; + } + + return $actualKeyValue; + } + /** * @param string $baseUrl * @param string $path diff --git a/tests/acceptance/features/bootstrap/GraphContext.php b/tests/acceptance/features/bootstrap/GraphContext.php index 80353269ea..92b857452f 100644 --- a/tests/acceptance/features/bootstrap/GraphContext.php +++ b/tests/acceptance/features/bootstrap/GraphContext.php @@ -1378,15 +1378,7 @@ class GraphContext implements Context { */ public function checkUserDriveInformation(array $expectedDriveInformation, array $actualDriveInformation):void { foreach (array_keys($expectedDriveInformation) as $keyName) { - // break the segment with @@@ to find the actual value from the drive infromation from response - $separatedKey = explode("@@@", $keyName); - // this stores the actual value of each key from drive information response used for assertion - $actualKeyValue = $actualDriveInformation; - - foreach ($separatedKey as $key) { - $actualKeyValue = $actualKeyValue[$key]; - } - + $actualKeyValue = GraphHelper::separateAndGetValueForKey($keyName, $actualDriveInformation); switch ($expectedDriveInformation[$keyName]) { case '%user_id%': Assert::assertTrue(GraphHelper::isUUIDv4($actualKeyValue), __METHOD__ . ' Expected user_id to have UUIDv4 pattern but found: ' . $actualKeyValue); @@ -1424,7 +1416,11 @@ class GraphContext implements Context { public function theResponseShouldContainTheFollowingDriveInformation(TableNode $table): void { $expectedDriveInformation = $table->getRowsHash(); // array of user drive information (Personal Drive Information Only) - $actualDriveInformation = $this->featureContext->getJsonDecodedResponse($this->featureContext->getResponse())['drive']; - $this->checkUserDriveInformation($expectedDriveInformation, $actualDriveInformation); + $actualDriveInformation = $this->featureContext->getJsonDecodedResponse($this->featureContext->getResponse()); + if (\is_array($actualDriveInformation) && \array_key_exists('drive', $actualDriveInformation)) { + $this->checkUserDriveInformation($expectedDriveInformation, $actualDriveInformation['drive']); + } else { + throw new Error('Response is not an array or the array does not consist key "drive"'); + } } } From d0087f98b8dfaa3740ac1f4610469050d87c4a45 Mon Sep 17 00:00:00 2001 From: Phil Davis Date: Fri, 30 Dec 2022 17:32:21 +0545 Subject: [PATCH 10/10] Apply suggestions from code review --- tests/TestHelpers/GraphHelper.php | 2 +- tests/acceptance/features/apiGraph/getUser.feature | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/TestHelpers/GraphHelper.php b/tests/TestHelpers/GraphHelper.php index 2fbf2c74aa..2056fb6d8c 100644 --- a/tests/TestHelpers/GraphHelper.php +++ b/tests/TestHelpers/GraphHelper.php @@ -64,7 +64,7 @@ class GraphHelper { * @return string */ public static function separateAndGetValueForKey(string $keyName, array $actualDriveInformation): string { - // break the segment with @@@ to find the actual value from the actual drive infromation + // break the segment with @@@ to find the actual value from the actual drive information $separatedKey = explode("@@@", $keyName); // this stores the actual value of each key from drive information response used for assertion $actualKeyValue = $actualDriveInformation; diff --git a/tests/acceptance/features/apiGraph/getUser.feature b/tests/acceptance/features/apiGraph/getUser.feature index 4a849165a8..b125df5558 100644 --- a/tests/acceptance/features/apiGraph/getUser.feature +++ b/tests/acceptance/features/apiGraph/getUser.feature @@ -35,7 +35,7 @@ Feature: get users | Brian Murphy | %uuid_v4% | brian@example.org | Brian | - Scenario: non-admin user tries get all user + Scenario: non-admin user tries to get all users When user "Brian" tries to get all users using the Graph API Then the HTTP status code should be "401" And the last response should be an unauthorized response