Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions tests/acceptance/bootstrap/GraphContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,7 @@ public function deleteGroupWithName(
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(),
Expand Down
126 changes: 76 additions & 50 deletions tests/acceptance/bootstrap/Provisioning.php
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,80 @@ public function createLdapUser(array $setting): void {
$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
*
Expand Down Expand Up @@ -581,37 +655,7 @@ public function usersHaveBeenCreated(
);
}
} 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);
Expand Down Expand Up @@ -1069,24 +1113,7 @@ public function userHasBeenCreated(
);
}
} 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);
Expand Down Expand Up @@ -1709,7 +1736,6 @@ public function deleteUser(string $user): ResponseInterface {
$this->ocsApiVersion
);
} else {
// users can be deleted using the username in the GraphApi too
$response = $this->graphContext->adminDeletesUserUsingTheGraphApi($user);
}
return $response;
Expand Down