Merge pull request #5579 from owncloud/add-assignRole-using-graph-api

[tests-only][full-ci]Add api tests to assign role using graph api
This commit is contained in:
Artur Neumann
2023-02-24 16:53:33 +05:45
committed by GitHub
5 changed files with 210 additions and 3 deletions
@@ -28,6 +28,13 @@ class GraphContext implements Context {
*/
private FeatureContext $featureContext;
/**
* application Entity
*
* @var array
*/
private $appEntity = [];
/**
* This will run before EVERY scenario.
* It will set the properties for this object.
@@ -1631,4 +1638,92 @@ class GraphContext implements Context {
);
$this->featureContext->setResponse($response);
}
/**
* @Given /^the administrator has assigned the role "([^"]*)" to user "([^"]*)" using the Graph API$/
*
* @param string $role
* @param string $user
*
* @return void
*
* @throws GuzzleException
* @throws Exception
*/
public function theAdministratorHasGivenTheRoleUsingTheGraphApi(string $role, string $user): void {
$userId = $this->featureContext->getAttributeOfCreatedUser($user, 'id') ?? $user;
if (empty($this->appEntity)) {
$applicationEntity = (
$this->featureContext->getJsonDecodedResponse(
GraphHelper::getApplications(
$this->featureContext->getBaseUrl(),
$this->featureContext->getStepLineRef(),
$this->featureContext->getAdminUsername(),
$this->featureContext->getAdminPassword(),
)
)
)['value'][0];
$this->appEntity["id"] = $applicationEntity["id"];
foreach ($applicationEntity["appRoles"] as $value) {
$this->appEntity["appRoles"][$value['displayName']] = $value['id'];
}
}
$response = GraphHelper::assignRole(
$this->featureContext->getBaseUrl(),
$this->featureContext->getStepLineRef(),
$this->featureContext->getAdminUsername(),
$this->featureContext->getAdminPassword(),
$this->appEntity["appRoles"][$role],
$this->appEntity["id"],
$userId
);
Assert::assertEquals(
201,
$response->getStatusCode(),
__METHOD__
. "\nExpected status code '200' but got '" . $response->getStatusCode() . "'"
);
}
/**
* @When /^the administrator retrieves the assigned role of user "([^"]*)" using the Graph API$/
*
* @param string $user
*
* @return void
* @throws GuzzleException
*/
public function userRetrievesAssignedRoleUsingTheGraphApi(string $user): void {
$admin = $this->featureContext->getAdminUserName();
$userId = $this->featureContext->getAttributeOfCreatedUser($user, 'id') ?? $user;
$this->featureContext->setResponse(
GraphHelper::getAssignedRole(
$this->featureContext->getBaseUrl(),
$this->featureContext->getStepLineRef(),
$admin,
$this->featureContext->getPasswordForUser($admin),
$userId
)
);
}
/**
* @Then /^the Graph API response should have the role "([^"]*)"$/
*
* @param string $role
*
* @return void
* @throws Exception
*/
public function theGraphApiResponseShouldHaveTheRole(string $role): void {
$response = $this->featureContext->getJsonDecodedResponse($this->featureContext->getResponse())['value'][0];
Assert::assertEquals(
$this->appEntity["appRoles"][$role],
$response['appRoleId'],
__METHOD__
. "\nExpected rolId for role '$role'' to be '" . $this->appEntity["appRoles"][$role] . "' but got '" . $response['appRoleId'] . "'"
);
}
}
@@ -127,8 +127,7 @@ class RoleAssignmentContext implements Context {
public function theAdministratorHasGivenUserTheRole(string $user, string $role): void {
$admin = $this->featureContext->getAdminUserName();
$roleId = $this->userGetRoleIdByRoleName($admin, $role);
$userId = $this->featureContext->getAttributeOfCreatedUser($user, 'id');
$userId = $userId ?? $user;
$userId = $this->featureContext->getAttributeOfCreatedUser($user, 'id') ?? $user;
$this->setRoleToUser($admin, $userId, $roleId);
}
@@ -239,7 +238,7 @@ class RoleAssignmentContext implements Context {
}
/**
* @When /^user "([^"]*)" should have the role "([^"]*)"$/
* @Then /^user "([^"]*)" should have the role "([^"]*)"$/
*
* @param string $user
* @param string $role
@@ -256,4 +255,18 @@ class RoleAssignmentContext implements Context {
$assignmentRoleId = \json_decode($rawBody, true, 512, JSON_THROW_ON_ERROR)["assignments"][0]["roleId"];
Assert::assertEquals($this->userGetRoleIdByRoleName($this->featureContext->getAdminUserName(), $role), $assignmentRoleId, "user $user has no role $role");
}
/**
* @Then /^the setting API response should have the role "([^"]*)"$/
*
* @param string $role
*
* @return void
*
* @throws Exception
*/
public function theSettingApiResponseShouldHaveTheRole(string $role): void {
$assignmentRoleId = $this->featureContext->getJsonDecodedResponse($this->featureContext->getResponse())["assignments"][0]["roleId"];
Assert::assertEquals($this->userGetRoleIdByRoleName($this->featureContext->getAdminUserName(), $role), $assignmentRoleId, "user has no role $role");
}
}