Skip to content

Commit

Permalink
backporting the PR
Browse files Browse the repository at this point in the history
  • Loading branch information
S-Panta committed Oct 30, 2023
1 parent 01ccfba commit 74e2a03
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 221 deletions.
85 changes: 40 additions & 45 deletions tests/acceptance/features/bootstrap/GraphContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,7 @@ public function theUserCreatesNewUser(string $user, TableNode $table): void {

/**
* adds a user to a group
* NOTE: If you want to make a request with non-existing user or group,provide "nonexistent" as their name
*
* @param string $group
* @param string $user
Expand All @@ -772,8 +773,16 @@ public function theUserCreatesNewUser(string $user, TableNode $table): void {
*/
public function addUserToGroup(string $group, string $user, ?string $byUser = null): ResponseInterface {
$credentials = $this->getAdminOrUserCredentials($byUser);
$groupId = $this->featureContext->getAttributeOfCreatedGroup($group, "id");
$userId = $this->featureContext->getAttributeOfCreatedUser($user, "id");
if ($group === "nonexistent") {
$groupId = WebDavHelper::generateUUIDv4();
} else {
$groupId = $this->featureContext->getAttributeOfCreatedGroup($group, "id");
}
if ($user === "nonexistent") {
$userId = WebDavHelper::generateUUIDv4();
} else {
$userId = $this->featureContext->getAttributeOfCreatedUser($user, "id");
}
return GraphHelper::addUserToGroup(
$this->featureContext->getBaseUrl(),
$this->featureContext->getStepLineRef(),
Expand All @@ -800,10 +809,8 @@ public function adminHasAddedUserToGroupUsingTheGraphApi(
string $group,
bool $checkResult = true
): void {
$result = $this->addUserToGroup($group, $user);
if ($checkResult && ($result->getStatusCode() !== 204)) {
$this->throwHttpException($result, "Could not add user '$user' to group '$group'.");
}
$response = $this->addUserToGroup($group, $user);
$this->featureContext->theHTTPStatusCodeShouldBe(204, '', $response);
}

/**
Expand All @@ -824,15 +831,14 @@ public function theAdministratorAddsTheFollowingUsersToTheFollowingGroupsUsingTh
}

/**
* @When the administrator tries to add user :user to group :group using the Graph API
* @When the administrator tries to add nonexistent user to group :group using the Graph API
*
* @param string $user
* @param string $group
*
* @return void
*/
public function theAdministratorTriesToAddUserToGroupUsingTheGraphAPI(string $user, string $group): void {
$this->featureContext->setResponse($this->addUserToGroup($group, $user));
public function theAdministratorTriesToAddNonExistentUserToGroupUsingTheGraphAPI(string $group): void {
$this->featureContext->setResponse($this->addUserToGroup($group, "nonexistent"));
}

/**
Expand All @@ -847,19 +853,7 @@ public function theAdministratorTriesToAddUserToGroupUsingTheGraphAPI(string $us
* @throws GuzzleException | Exception
*/
public function theAdministratorTriesToAddUserToNonExistentGroupUsingTheGraphAPI(string $user, ?string $byUser = null): void {
$credentials = $this->getAdminOrUserCredentials($byUser);
$groupId = WebDavHelper::generateUUIDv4();
$userId = $this->featureContext->getAttributeOfCreatedUser($user, "id");
$this->featureContext->setResponse(
GraphHelper::addUserToGroup(
$this->featureContext->getBaseUrl(),
$this->featureContext->getStepLineRef(),
$credentials['username'],
$credentials['password'],
$userId,
$groupId
)
);
$this->featureContext->setResponse($this->addUserToGroup("nonexistent", $user, $byUser));
}

/**
Expand Down Expand Up @@ -1423,25 +1417,21 @@ public function userTriesToGetOwnDriveInformation(string $user) {
* @param string $user
* @param array $userIds
* @param string $groupId
* @param TableNode $table
*
* @return void
* @throws GuzzleException
* @throws Exception
*/
public function addMultipleUsersToGroup(string $user, array $userIds, string $groupId, TableNode $table): void {
public function addMultipleUsersToGroup(string $user, array $userIds, string $groupId): ResponseInterface {
$credentials = $this->getAdminOrUserCredentials($user);
$this->featureContext->verifyTableNodeColumns($table, ['username']);

$this->featureContext->setResponse(
GraphHelper::addUsersToGroup(
$this->featureContext->getBaseUrl(),
$this->featureContext->getStepLineRef(),
$credentials["username"],
$credentials["password"],
$groupId,
$userIds
)
return GraphHelper::addUsersToGroup(
$this->featureContext->getBaseUrl(),
$this->featureContext->getStepLineRef(),
$credentials["username"],
$credentials["password"],
$groupId,
$userIds
);
}

Expand All @@ -1462,7 +1452,9 @@ public function theAdministratorAddsTheFollowingUsersToAGroupInASingleRequestUsi
foreach ($table->getHash() as $row) {
$userIds[] = $this->featureContext->getAttributeOfCreatedUser($row['username'], "id");
}
$this->addMultipleUsersToGroup($user, $userIds, $groupId, $table);
$this->featureContext->verifyTableNodeColumns($table, ['username']);
$response = $this->addMultipleUsersToGroup($user, $userIds, $groupId);
$this->featureContext->setResponse($response);
}

/**
Expand Down Expand Up @@ -1552,7 +1544,9 @@ public function theAdministratorTriesToAddsTheFollowingUsersToANonExistingGroupA
foreach ($table->getHash() as $row) {
$userIds[] = $this->featureContext->getAttributeOfCreatedUser($row['username'], "id");
}
$this->addMultipleUsersToGroup($user, $userIds, $groupId, $table);
$this->featureContext->verifyTableNodeColumns($table, ['username']);
$response = $this->addMultipleUsersToGroup($user, $userIds, $groupId);
$this->featureContext->setResponse($response);
}

/**
Expand All @@ -1572,7 +1566,9 @@ public function theAdministratorTriesToAddTheFollowingNonExistingUsersToAGroupAt
foreach ($table->getHash() as $row) {
$userIds[] = WebDavHelper::generateUUIDv4();
}
$this->addMultipleUsersToGroup($user, $userIds, $groupId, $table);
$this->featureContext->verifyTableNodeColumns($table, ['username']);
$response = $this->addMultipleUsersToGroup($user, $userIds, $groupId);
$this->featureContext->setResponse($response);
}

/**
Expand All @@ -1594,7 +1590,9 @@ public function theAdministratorTriesToAddTheFollowingUsersToAGroupAtOnceUsingTh
$userId = $this->featureContext->getAttributeOfCreatedUser($row['username'], "id");
$userIds[] = $userId ?: WebDavHelper::generateUUIDv4();
}
$this->addMultipleUsersToGroup($user, $userIds, $groupId, $table);
$this->featureContext->verifyTableNodeColumns($table, ['username']);
$response = $this->addMultipleUsersToGroup($user, $userIds, $groupId);
$this->featureContext->setResponse($response);
}

/**
Expand Down Expand Up @@ -1987,12 +1985,9 @@ public function theAdministratorHasAddedTheFollowingUsersToAGroupAtOnceUsingTheG
foreach ($table->getHash() as $row) {
$userIds[] = $this->featureContext->getAttributeOfCreatedUser($row['username'], "id");
}
$this->addMultipleUsersToGroup($user, $userIds, $groupId, $table);
$response = $this->featureContext->getResponse();
if ($response->getStatusCode() !== 204) {
$this->throwHttpException($response, "Cannot add users to group '$group'");
}
$this->featureContext->emptyLastHTTPStatusCodesArray();
$this->featureContext->verifyTableNodeColumns($table, ['username']);
$response = $this->addMultipleUsersToGroup($user, $userIds, $groupId);
$this->featureContext->theHTTPStatusCodeShouldBe(204, '', $response);
}

/**
Expand Down
193 changes: 17 additions & 176 deletions tests/acceptance/features/bootstrap/Provisioning.php
Original file line number Diff line number Diff line change
Expand Up @@ -2966,82 +2966,8 @@ public function userBelongsToGroup(string $user, string $group):bool {
* @throws Exception
*/
public function adminAddsUserToGroupUsingTheProvisioningApi(string $user, string $group):void {
$this->addUserToGroup($user, $group, "api");
}

/**
* @When the administrator adds the following users to the following groups using the provisioning API
*
* @param TableNode $table
*
* @return void
* @throws Exception
*/
public function theAdministratorAddsUserToTheFollowingGroupsUsingTheProvisioningApi(TableNode $table):void {
$this->verifyTableNodeColumns($table, ["username", "groupname"], ["comment"]);
$rows = $table->getHash();
foreach ($rows as $row) {
$this->adminAddsUserToGroupUsingTheProvisioningApi($row["username"], $row["groupname"]);
}
}

/**
* @When user :user tries to add user :otherUser to group :group using the provisioning API
*
* @param string $user
* @param string $otherUser
* @param string $group
*
* @return void
* @throws Exception
*/
public function userTriesToAddUserToGroupUsingTheProvisioningApi(string $user, string $otherUser, string $group):void {
$actualUser = $this->getActualUsername($user);
$actualPassword = $this->getUserPassword($actualUser);
$actualOtherUser = $this->getActualUsername($otherUser);
$result = UserHelper::addUserToGroup(
$this->getBaseUrl(),
$actualOtherUser,
$group,
$actualUser,
$actualPassword,
$this->getStepLineRef(),
$this->ocsApiVersion
);
$this->response = $result;
}

/**
* @When user :user tries to add himself to group :group using the provisioning API
*
* @param string $user
* @param string $group
*
* @return void
* @throws Exception
*/
public function userTriesToAddHimselfToGroupUsingTheProvisioningApi(string $user, string $group):void {
$this->userTriesToAddUserToGroupUsingTheProvisioningApi($user, $user, $group);
}

/**
* @When the administrator tries to add user :user to group :group using the provisioning API
*
* @param string $user
* @param string $group
*
* @return void
* @throws Exception
*/
public function theAdministratorTriesToAddUserToGroupUsingTheProvisioningApi(
string $user,
string $group
):void {
$this->userTriesToAddUserToGroupUsingTheProvisioningApi(
$this->getAdminUsername(),
$user,
$group
);
$response = $this->graphContext->addUserToGroup($group, $user);
$this->setResponse($response);
}

/**
Expand All @@ -3055,7 +2981,21 @@ public function theAdministratorTriesToAddUserToGroupUsingTheProvisioningApi(
*/
public function userHasBeenAddedToGroup(string $user, string $group):void {
$user = $this->getActualUsername($user);
$this->addUserToGroup($user, $group, null, true);
if ($this->isTestingWithLdap()) {
try {
$this->addUserToLdapGroup(
$user,
$group
);
} catch (LdapException $exception) {
throw new Exception(
"User $user cannot be added to $group Error: $exception"
);
}
} else {
$response = $this->graphContext->addUserToGroup($group, $user);
$this->theHTTPStatusCodeShouldBe(204, '', $response);
}
}

/**
Expand All @@ -3073,105 +3013,6 @@ public function theFollowingUserHaveBeenAddedToTheFollowingGroup(TableNode $tabl
}
}

/**
* @Given /^user "([^"]*)" has been added to database backend group "([^"]*)"$/
*
* @param string $user
* @param string $group
*
* @return void
* @throws Exception
*/
public function userHasBeenAddedToDatabaseBackendGroup(string $user, string $group):void {
$this->addUserToGroup($user, $group, 'api', true);
}

/**
* @param string $user
* @param string $group
* @param string|null $method how to add the user to the group api|occ
* @param bool $checkResult if true, then check the status of the operation. default false.
* for given step checkResult is expected to be set as true
* for when step checkResult is expected to be set as false
*
* @return void
* @throws Exception
*/
public function addUserToGroup(string $user, string $group, ?string $method = null, bool $checkResult = false):void {
$user = $this->getActualUsername($user);
if ($method === null
&& $this->isTestingWithLdap()
&& !$this->isLocalAdminGroup($group)
) {
//guess yourself
$method = "ldap";
} elseif ($method === null && OcisHelper::isTestingWithGraphApi()) {
$method = "graph";
} elseif ($method === null) {
$method = "api";
}
$method = \trim(\strtolower($method));
switch ($method) {
case "api":
$result = UserHelper::addUserToGroup(
$this->getBaseUrl(),
$user,
$group,
$this->getAdminUsername(),
$this->getAdminPassword(),
$this->getStepLineRef(),
$this->ocsApiVersion
);
if ($checkResult && ($result->getStatusCode() !== 200)) {
throw new Exception(
"could not add user to group. "
. $result->getStatusCode() . " " . $result->getBody()
);
}
$this->response = $result;
if (!$checkResult) {
// for when step only
$this->pushToLastStatusCodesArrays();
}
break;
case "ldap":
try {
$this->addUserToLdapGroup(
$user,
$group
);
} catch (LdapException $exception) {
throw new Exception(
"User $user cannot be added to $group Error: $exception"
);
};
break;
case "graph":
$this->graphContext->adminHasAddedUserToGroupUsingTheGraphApi(
$user,
$group
);
break;
default:
throw new InvalidArgumentException(
"Invalid method to add a user to a group"
);
}
}

/**
* @Given the administrator has been added to group :group
*
* @param string $group
*
* @return void
* @throws Exception
*/
public function theAdministratorHasBeenAddedToGroup(string $group):void {
$admin = $this->getAdminUsername();
$this->addUserToGroup($admin, $group, null, true);
}

/**
* @param string $group
* @param bool $shouldExist - true if the group should exist
Expand Down

0 comments on commit 74e2a03

Please sign in to comment.