From 52a0c346c35662040d566f573f6926fac7188edf Mon Sep 17 00:00:00 2001 From: Sabin Date: Fri, 12 Jul 2024 13:27:34 +0545 Subject: [PATCH] added test for updating admin password via cli --- .../features/bootstrap/CliContext.php | 2 +- .../features/bootstrap/GraphContext.php | 54 +++++++++++++++++++ .../features/bootstrap/Provisioning.php | 20 +++++++ .../cliCommands/resetUserPassword.feature | 31 +++++++++++ 4 files changed, 106 insertions(+), 1 deletion(-) diff --git a/tests/acceptance/features/bootstrap/CliContext.php b/tests/acceptance/features/bootstrap/CliContext.php index e75f323ab..ad5cb9107 100644 --- a/tests/acceptance/features/bootstrap/CliContext.php +++ b/tests/acceptance/features/bootstrap/CliContext.php @@ -60,7 +60,7 @@ class CliContext implements Context { } /** - * @Given the administrator has started the server + * @Given /^the administrator (?:starts|has started) the server$/ * * @return void */ diff --git a/tests/acceptance/features/bootstrap/GraphContext.php b/tests/acceptance/features/bootstrap/GraphContext.php index d29ea1fb1..65ebd8fbc 100644 --- a/tests/acceptance/features/bootstrap/GraphContext.php +++ b/tests/acceptance/features/bootstrap/GraphContext.php @@ -2647,4 +2647,58 @@ class GraphContext implements Context { "Response contains email '$email' but should not." ); } + + /** + * @Then user :byUser using password :password should be able to create a new user :user with default attributes + * + * @param string $byUser + * @param string $password + * @param string $user + * + * @return void + * @throws GuzzleException + * @throws JsonException + */ + public function userUsingPasswordShouldBeAbleToCreateANewUserWithDefaultAttributes(string $byUser, string $password, string $user): void { + $response = GraphHelper::createUser( + $this->featureContext->getBaseUrl(), + $this->featureContext->getStepLineRef(), + $byUser, + $password, + $user, + $this->featureContext->getPasswordForUser($user) + ); + Assert::assertEquals( + 201, + $response->getStatusCode(), + __METHOD__ . " cannot create new user '$user' by user '$byUser'.\nResponse:" . + json_encode($this->featureContext->getJsonDecodedResponse($response)) + ); + $this->featureContext->addUserToCreatedUsersList($user, $this->featureContext->getPasswordForUser($user)); + } + + /** + * @Given user :byUser has changed the username to :userName + * + * @param string $byUser + * @param string $userName + * + * @return void + * @throws GuzzleException + * @throws JsonException + */ + public function theUserHasChangedItsOwnUsernameTo(string $byUser, string $userName): void { + $userId = $this->featureContext->getUserIdByUserName($byUser); + $response = GraphHelper::editUser( + $this->featureContext->getBaseUrl(), + $this->featureContext->getStepLineRef(), + $byUser, + $this->featureContext->getPasswordForUser($byUser), + $userId, + 'PATCH', + $userName, + ); + $this->featureContext->theHTTPStatusCodeShouldBe(200, '', $response); + $this->featureContext->updateUsernameInCreatedUserList($byUser, $userName); + } } diff --git a/tests/acceptance/features/bootstrap/Provisioning.php b/tests/acceptance/features/bootstrap/Provisioning.php index 0432b1121..9f3122be3 100644 --- a/tests/acceptance/features/bootstrap/Provisioning.php +++ b/tests/acceptance/features/bootstrap/Provisioning.php @@ -1156,6 +1156,26 @@ trait Provisioning { } } + /** + * @param string $oldUserName + * @param string $newUserName + * + * @return void + */ + public function updateUsernameInCreatedUserList(string $oldUserName, string $newUserName) :void { + $normalizedUsername = $this->normalizeUsername($oldUserName); + $normalizeNewUserName = $this->normalizeUsername($newUserName); + if (\array_key_exists($normalizedUsername, $this->createdUsers)) { + foreach ($this->createdUsers as $createdUser) { + if ($createdUser['actualUsername'] === $oldUserName) { + $this->createdUsers[$normalizeNewUserName] = $this->createdUsers[$normalizedUsername]; + $this->createdUsers[$normalizeNewUserName]['actualUsername'] = $newUserName; + unset($this->createdUsers[$normalizedUsername]); + } + } + } + } + /** * Remembers that a user from the list of users that were created during * test runs is no longer expected to exist. Useful if a user was created diff --git a/tests/acceptance/features/cliCommands/resetUserPassword.feature b/tests/acceptance/features/cliCommands/resetUserPassword.feature index 16dc0ca22..f1ad3d36e 100644 --- a/tests/acceptance/features/cliCommands/resetUserPassword.feature +++ b/tests/acceptance/features/cliCommands/resetUserPassword.feature @@ -22,3 +22,34 @@ Feature: reset user password via CLI command When the administrator resets the password of non-existing user "Alice" to "newpass" using the CLI Then the command should be successful But the command output should contain "Failed to update user password: entry does not exist" + + + Scenario: reset password of admin user + Given the user "Admin" has created a new user with the following attributes: + | userName | Alice | + | displayName | Alice Hansen | + | password | %alt1% | + And the administrator has assigned the role "Admin" to user "Alice" using the Graph API + And the administrator has stopped the server + When the administrator resets the password of existing user "Alice" to "newpass" using the CLI + Then the command should be successful + And the command output should contain "Password for user 'uid=Alice,ou=users,o=libregraph-idm' updated." + But the command output should not contain "Failed to update user password: entry does not exist" + And the administrator starts the server + And user "Alice" using password "newpass" should be able to create a new user "Brian" with default attributes + + + Scenario: reset password after renaming the admin user + Given the user "Admin" has created a new user with the following attributes: + | userName | Alice | + | displayName | Alice Hansen | + | password | %alt1% | + And the administrator has assigned the role "Admin" to user "Alice" using the Graph API + And user "Alice" has changed the username to "superUser" + And the administrator has stopped the server + When the administrator resets the password of existing user "superUser" to "newpass" using the CLI + Then the command should be successful + And the command output should contain "Password for user 'uid=superUser,ou=users,o=libregraph-idm' updated." + But the command output should not contain "Failed to update user password: entry does not exist" + And the administrator starts the server + And user "superUser" using password "newpass" should be able to create a new user "Brian" with default attributes