From 92e6c8f3ff2327376164d632feecac353d6a6503 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Duffeck?= Date: Fri, 12 Jun 2026 15:13:32 +0200 Subject: [PATCH] Do not choke on users that weren't cleaned up yet --- tests/acceptance/bootstrap/GraphContext.php | 4 +- tests/acceptance/bootstrap/Provisioning.php | 126 ++++++++++++-------- 2 files changed, 77 insertions(+), 53 deletions(-) diff --git a/tests/acceptance/bootstrap/GraphContext.php b/tests/acceptance/bootstrap/GraphContext.php index 49782fa137..cf350be6be 100644 --- a/tests/acceptance/bootstrap/GraphContext.php +++ b/tests/acceptance/bootstrap/GraphContext.php @@ -287,9 +287,7 @@ class GraphContext implements Context { public function adminDeletesUserUsingTheGraphApi(string $user, ?string $byUser = null): ?ResponseInterface { $credentials = $this->getAdminOrUserCredentials($byUser); $userId = $this->featureContext->getAttributeOfCreatedUser($user, 'id'); - if ($userId === null) { - throw new \RuntimeException("Cannot delete user '$user': no userId found"); - } + $userId = $userId ?: $user; return GraphHelper::deleteUserByUserId( $this->featureContext->getBaseUrl(), $this->featureContext->getStepLineRef(), diff --git a/tests/acceptance/bootstrap/Provisioning.php b/tests/acceptance/bootstrap/Provisioning.php index 00a8cc26c8..b50ccf1f4e 100644 --- a/tests/acceptance/bootstrap/Provisioning.php +++ b/tests/acceptance/bootstrap/Provisioning.php @@ -469,6 +469,80 @@ trait Provisioning { $this->ldapCreatedUsers[] = $setting["userid"]; } + /** + * Creates a user through the Graph API and replaces a stale existing user if needed. + * + * @param string $userName + * @param string $password + * @param string|null $email + * @param string|null $displayName + * @param string|null $byUser + * + * @return string + * @throws Exception|GuzzleException + */ + private function createGraphUserWithReplacement( + string $userName, + string $password, + ?string $email, + ?string $displayName, + ?string $byUser = null + ): string { + $userName = $this->getActualUsername($userName); + $userName = \trim($userName); + if ($displayName === null) { + $displayName = $this->getDisplayNameForUser($userName); + if ($displayName === null) { + $displayName = $this->getDisplayNameForUser('regularuser'); + } + } + if ($email === null) { + $email = $this->getEmailAddressForUser($userName); + if ($email === null) { + $email = \str_replace(["@", " "], "", $userName) . '@opencloud.eu'; + } + } + $reqUser = $byUser ? $this->getActualUsername($byUser) : $this->getAdminUsername(); + $response = GraphHelper::createUser( + $this->getBaseUrl(), + $this->getStepLineRef(), + $reqUser, + $this->getPasswordForUser($reqUser), + $userName, + $password, + $email, + $displayName, + ); + + if ($response->getStatusCode() === 409) { + $responseBody = $this->getJsonDecodedResponse($response); + if (($responseBody['error']['code'] ?? null) === 'nameAlreadyExists') { + $deleteResponse = $this->deleteUser($userName); + $this->theHTTPStatusCodeShouldBe(204, "", $deleteResponse); + + $response = GraphHelper::createUser( + $this->getBaseUrl(), + $this->getStepLineRef(), + $reqUser, + $this->getPasswordForUser($reqUser), + $userName, + $password, + $email, + $displayName, + ); + } + } + + Assert::assertEquals( + 201, + $response->getStatusCode(), + __METHOD__ . " cannot create user '$userName'.\nResponse:" . + json_encode($this->getJsonDecodedResponse($response)) + ); + + return (string)$this->getJsonDecodedResponse($response)['id']; + } + /** * @param string $group group name * @@ -581,37 +655,7 @@ trait Provisioning { ); } } else { - // Use the same logic as userHasBeenCreated for email generation - if ($email === null) { - $email = $this->getEmailAddressForUser($userName); - if ($email === null) { - // escape @ & space if present in userId - $email = \str_replace(["@", " "], "", $userName) . '@opencloud.eu'; - } - } - - $userName = $this->getActualUsername($userName); - $userName = \trim($userName); - - $response = GraphHelper::createUser( - $this->getBaseUrl(), - $this->getStepLineRef(), - $this->getAdminUsername(), - $this->getAdminPassword(), - $userName, - $password, - $email, - $displayName, - ); - - Assert::assertEquals( - 201, - $response->getStatusCode(), - __METHOD__ . " cannot create user '$userName'.\nResponse:" . - json_encode($this->getJsonDecodedResponse($response)) - ); - - $userId = $this->getJsonDecodedResponse($response)['id']; + $userId = $this->createGraphUserWithReplacement($userName, $password, $email, $displayName); } $this->addUserToCreatedUsersList($userName, $password, $displayName, $email, $userId ?? null); @@ -1069,24 +1113,7 @@ trait Provisioning { ); } } else { - $reqUser = $byUser ? $this->getActualUsername($byUser) : $this->getAdminUsername(); - $response = GraphHelper::createUser( - $this->getBaseUrl(), - $this->getStepLineRef(), - $reqUser, - $this->getPasswordForUser($reqUser), - $user, - $password, - $email, - $displayName, - ); - Assert::assertEquals( - 201, - $response->getStatusCode(), - __METHOD__ . " cannot create user '$user'.\nResponse:" . - json_encode($this->getJsonDecodedResponse($response)) - ); - $userId = $this->getJsonDecodedResponse($response)['id']; + $userId = $this->createGraphUserWithReplacement($user, $password, $email, $displayName, $byUser); } $this->addUserToCreatedUsersList($user, $password, $displayName, $email, $userId); @@ -1709,7 +1736,6 @@ trait Provisioning { $this->ocsApiVersion ); } else { - // users can be deleted using the username in the GraphApi too $response = $this->graphContext->adminDeletesUserUsingTheGraphApi($user); } return $response;