test: combine spaces and fix user deletion

This commit is contained in:
Sawjan Gurung
2025-02-17 18:44:30 +05:45
committed by Saw-jan
parent 9713e9872b
commit bdf274658c
10 changed files with 124 additions and 135 deletions

View File

@@ -41,12 +41,6 @@ trait Provisioning {
* key is the lowercase username, value is an array of user attributes
*/
private array $createdUsers = [];
/**
* list of users that were created on the remote server during test runs
* key is the lowercase username, value is an array of user attributes
*/
private array $createdRemoteUsers = [];
private array $startingGroups = [];
private array $createdRemoteGroups = [];
private array $createdGroups = [];
@@ -83,13 +77,6 @@ trait Provisioning {
return $this->createdUsers;
}
/**
* @return array
*/
public function getAllCreatedUsers(): array {
return array_merge($this->createdUsers, $this->createdRemoteUsers);
}
/**
* @return array
*/
@@ -107,7 +94,7 @@ trait Provisioning {
*/
public function getUserDisplayName(string $username): string {
$normalizedUsername = $this->normalizeUsername($username);
$users = $this->getAllCreatedUsers();
$users = $this->getCreatedUsers();
if (isset($users[$normalizedUsername]['displayname'])) {
$displayName = (string) $users[$normalizedUsername]['displayname'];
if ($displayName !== '') {
@@ -125,7 +112,7 @@ trait Provisioning {
* @throws Exception
*/
public function getAttributeOfCreatedUser(string $user, string $attribute) {
$usersList = $this->getAllCreatedUsers();
$usersList = $this->getCreatedUsers();
$normalizedUsername = $this->normalizeUsername($user);
if (\array_key_exists($normalizedUsername, $usersList)) {
if (\array_key_exists($attribute, $usersList[$normalizedUsername])) {
@@ -175,8 +162,6 @@ trait Provisioning {
$password = $this->getAdminPassword();
} elseif (\array_key_exists($normalizedUsername, $this->createdUsers)) {
$password = $this->createdUsers[$normalizedUsername]['password'];
} elseif (\array_key_exists($normalizedUsername, $this->createdRemoteUsers)) {
$password = $this->createdRemoteUsers[$normalizedUsername]['password'];
} else {
throw new Exception(
"user '$username' was not created by this test run"
@@ -768,7 +753,6 @@ trait Provisioning {
$this->userExists($user),
"User '$user' should not exist but does exist"
);
$this->rememberThatUserIsNotExpectedToExist($user);
}
/**
@@ -855,7 +839,6 @@ trait Provisioning {
$this->userExists($user),
"User '$user' should not exist but does exist"
);
$this->rememberThatUserIsNotExpectedToExist($user);
}
/**
@@ -977,8 +960,8 @@ trait Provisioning {
* makes it possible to use this list in other test steps
* or to delete them at the end of the test
*
* @param string|null $user
* @param string|null $password
* @param string $user
* @param string $password
* @param string|null $displayName
* @param string|null $email
* @param string|null $userId only set for the users created using the Graph API
@@ -988,8 +971,8 @@ trait Provisioning {
* @throws JsonException
*/
public function addUserToCreatedUsersList(
?string $user,
?string $password,
string $user,
string $password,
?string $displayName = null,
?string $email = null,
?string $userId = null,
@@ -997,30 +980,17 @@ trait Provisioning {
): void {
$user = $this->getActualUsername($user);
$normalizedUsername = $this->normalizeUsername($user);
$userData = [
"password" => $password,
"displayname" => $displayName,
"email" => $email,
"shouldExist" => $shouldExist,
"actualUsername" => $user,
"id" => $userId
];
if ($this->currentServer === 'LOCAL') {
// Only remember this user creation if it was expected to have been successful
// or the user has not been processed before. Some tests create a user the
// first time (successfully) and then purposely try to create the user again.
// The 2nd user creation is expected to fail, and in that case we want to
// still remember the details of the first user creation.
if ($shouldExist || !\array_key_exists($normalizedUsername, $this->createdUsers)) {
$this->createdUsers[$normalizedUsername] = $userData;
}
} elseif ($this->currentServer === 'REMOTE') {
// See comment above about the LOCAL case. The logic is the same for the remote case.
if ($shouldExist || !\array_key_exists($normalizedUsername, $this->createdRemoteUsers)) {
$this->createdRemoteUsers[$normalizedUsername] = $userData;
$this->createdUsers[$normalizedUsername] = $userData;
}
if ($shouldExist || !\array_key_exists($normalizedUsername, $this->createdUsers)) {
$this->createdUsers[$normalizedUsername] = [
"password" => $password,
"displayname" => $displayName,
"email" => $email,
"shouldExist" => $shouldExist,
"actualUsername" => $user,
"id" => $userId,
"serverType" => $this->currentServer
];
}
}
@@ -1038,14 +1008,8 @@ trait Provisioning {
string $password
): void {
$normalizedUsername = $this->normalizeUsername($user);
if ($this->currentServer === 'LOCAL') {
if (\array_key_exists($normalizedUsername, $this->createdUsers)) {
$this->createdUsers[$normalizedUsername]['password'] = $password;
}
} elseif ($this->currentServer === 'REMOTE') {
if (\array_key_exists($normalizedUsername, $this->createdRemoteUsers)) {
$this->createdRemoteUsers[$user]['password'] = $password;
}
if (\array_key_exists($normalizedUsername, $this->createdUsers)) {
$this->createdUsers[$normalizedUsername]['password'] = $password;
}
}
@@ -1792,6 +1756,9 @@ trait Provisioning {
// users can be deleted using the username in the GraphApi too
$response = $this->graphContext->adminDeletesUserUsingTheGraphApi($user);
}
if ($response->getStatusCode() === 204) {
$this->rememberThatUserIsNotExpectedToExist($user);
}
return $response;
}
@@ -1998,8 +1965,13 @@ trait Provisioning {
*/
public function cleanupDatabaseUsers(): void {
$previousServer = $this->currentServer;
$this->usingServer('LOCAL');
foreach ($this->createdUsers as $userData) {
// do not try to delete if user doesn't exist
if (!$userData["shouldExist"]) {
continue;
}
$this->usingServer($userData["serverType"]);
$user = $userData['actualUsername'];
$response = $this->deleteUser($user);
$this->theHTTPStatusCodeShouldBe(204, "Failed to delete user '$user'", $response);
@@ -2007,18 +1979,6 @@ trait Provisioning {
$this->userExists($user),
"User '$user' should not exist but does exist"
);
$this->rememberThatUserIsNotExpectedToExist($user);
}
$this->usingServer('REMOTE');
foreach ($this->createdRemoteUsers as $userData) {
$user = $userData['actualUsername'];
$response = $this->deleteUser($user);
$this->theHTTPStatusCodeShouldBe(204, "Failed to delete user '$user'", $response);
Assert::assertFalse(
$this->userExists($user),
"User '$user' should not exist but does exist"
);
$this->rememberThatUserIsNotExpectedToExist($user);
}
$this->usingServer($previousServer);
}