Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add secanrio to assign role using graph and list role using setting api
Browse files Browse the repository at this point in the history
'
amrita-shrestha committed Feb 15, 2023
1 parent 3a04158 commit 2263b23
Showing 4 changed files with 174 additions and 147 deletions.
122 changes: 61 additions & 61 deletions tests/TestHelpers/GraphHelper.php
Original file line number Diff line number Diff line change
@@ -1020,66 +1020,66 @@ public static function getApplications(
);
}

/**
* @param string $baseUrl
* @param string $xRequestId
* @param string $user
* @param string $password
* @param string $appRoleId
* @param string $applicationId
* @param string $userId
*
* @return ResponseInterface
* @throws GuzzleException
*/
public static function assignRole(
string $baseUrl,
string $xRequestId,
string $user,
string $password,
string $appRoleId,
string $applicationId,
string $userId
): ResponseInterface {
$url = self::getFullUrl($baseUrl, 'users/'.$userId.'/appRoleAssignments');
$payload['principalId'] = $userId;
$payload['appRoleId'] = $appRoleId;
$payload['resourceId'] = $applicationId;
return HttpRequestHelper::sendRequest(
$url,
$xRequestId,
"POST",
$user,
$password,
self::getRequestHeaders(),
\json_encode($payload)
);
}
/**
* @param string $baseUrl
* @param string $xRequestId
* @param string $user
* @param string $password
* @param string $appRoleId
* @param string $applicationId
* @param string $userId
*
* @return ResponseInterface
* @throws GuzzleException
*/
public static function assignRole(
string $baseUrl,
string $xRequestId,
string $user,
string $password,
string $appRoleId,
string $applicationId,
string $userId
): ResponseInterface {
$url = self::getFullUrl($baseUrl, 'users/'.$userId.'/appRoleAssignments');
$payload['principalId'] = $userId;
$payload['appRoleId'] = $appRoleId;
$payload['resourceId'] = $applicationId;
return HttpRequestHelper::sendRequest(
$url,
$xRequestId,
"POST",
$user,
$password,
self::getRequestHeaders(),
\json_encode($payload)
);
}

/**
* @param string $baseUrl
* @param string $xRequestId
* @param string $user
* @param string $password
* @param string $userId
*
* @return ResponseInterface
* @throws GuzzleException
*/
public static function getAssignedRole(
string $baseUrl,
string $xRequestId,
string $user,
string $password,
string $userId
): ResponseInterface {
$url = self::getFullUrl($baseUrl, 'users/'.$userId.'/appRoleAssignments');
return HttpRequestHelper::get(
$url,
$xRequestId,
$user,
$password,
self::getRequestHeaders()
);
}
/**
* @param string $baseUrl
* @param string $xRequestId
* @param string $user
* @param string $password
* @param string $userId
*
* @return ResponseInterface
* @throws GuzzleException
*/
public static function getAssignedRole(
string $baseUrl,
string $xRequestId,
string $user,
string $password,
string $userId
): ResponseInterface {
$url = self::getFullUrl($baseUrl, 'users/'.$userId.'/appRoleAssignments');
return HttpRequestHelper::get(
$url,
$xRequestId,
$user,
$password,
self::getRequestHeaders()
);
}
}
Original file line number Diff line number Diff line change
@@ -75,3 +75,16 @@ Feature: assign role
| Space Admin |
| User |
| Guest |

Scenario Outline: assign role to the user with setting api and list role with graph api
Given user "Alice" has been created with default attributes and without skeleton files
And the administrator has given "Alice" the role "<userRole>" using the Graph API
When user "Alice" tries to get list of assignment
Then the HTTP status code should be "201"
And the setting API response should have the role "<userRole>"
Examples:
| userRole |
| Admin |
| Space Admin |
| User |
| Guest |
168 changes: 83 additions & 85 deletions tests/acceptance/features/bootstrap/GraphContext.php
Original file line number Diff line number Diff line change
@@ -33,17 +33,17 @@ class GraphContext implements Context {
*/
private SpacesContext $spacesContext;

/**
* list of appRole
*
* @var array
*/
private $appRole = [];
/**
* list of appRole
*
* @var array
*/
private $appRole = [];

/**
* @var string
*/
private $applicationId = '';
/**
* @var string
*/
private $applicationId = '';

/**
* This will run before EVERY scenario.
@@ -1490,81 +1490,79 @@ public function userGetsAllApplicationsUsingTheGraphApi(string $user) {
$this->featureContext->setResponse($response);
}

/**
* @When /^the administrator has given "([^"]*)" the role "([^"]*)" using the Graph API$/
*
* @param string $user
* @param string $role
*
* @return void
*
* @throws GuzzleException
* @throws Exception
*/
public function theAdministratorHasGivenTheRoleUsingTheGraphApi(string $user, string $role): void {
$admin = $this->featureContext->getAdminUserName();
$userId = $this->featureContext->getAttributeOfCreatedUser($user, 'id');
$userId = $userId ?? $user;
$this->userGetsAllApplicationsUsingTheGraphApi($user);

$applicationEntity = ($this->featureContext->getJsonDecodedResponse($this->featureContext->getResponse()))['value'][0];
foreach ($applicationEntity["appRoles"] as $value ){
$this->appRole[$value['displayName']] = $value['id'];
}
$this->applicationId = $applicationEntity["id"];

$response=$this->featureContext->getJsonDecodedResponse(GraphHelper::assignRole(
$this->featureContext->getBaseUrl(),
$this->featureContext->getStepLineRef(),
$admin,
$this->featureContext->getPasswordForUser($admin),
$this->appRole[$role],
$this->applicationId,
$userId
));
if (!\array_key_exists('appRoleId', $response) && $response['appRoleId'] !== $this->appRole[$role]) {
throw new Error('Could not assign role'. $role);
};
}

/**
* @When /^user "([^"]*)" retrieves assigned role using the Graph API$/
*
* @param string $user
* @return void
*
* @throws GuzzleException
*/
public function userRetrievesAssignedRoleUsingTheGraphApi(string $user): void {
$userId = $this->featureContext->getAttributeOfCreatedUser($user, 'id');
$userId = $userId ?? $user;
$this->featureContext->setResponse(GraphHelper::getAssignedRole(
$this->featureContext->getBaseUrl(),
$this->featureContext->getStepLineRef(),
$user,
$this->featureContext->getPasswordForUser($user),
$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];
if ($this->appRole[$role] !==$response['appRoleId']){
throw new Error('App role should be'. $role.' but found '.$response['appRoleId']);
}

}

/**
/**
* @When /^the administrator has given "([^"]*)" the role "([^"]*)" using the Graph API$/
*
* @param string $user
* @param string $role
*
* @return void
*
* @throws GuzzleException
* @throws Exception
*/
public function theAdministratorHasGivenTheRoleUsingTheGraphApi(string $user, string $role): void {
$admin = $this->featureContext->getAdminUserName();
$userId = $this->featureContext->getAttributeOfCreatedUser($user, 'id');
$userId = $userId ?? $user;
$this->userGetsAllApplicationsUsingTheGraphApi($user);

$applicationEntity = ($this->featureContext->getJsonDecodedResponse($this->featureContext->getResponse()))['value'][0];
foreach ($applicationEntity["appRoles"] as $value) {
$this->appRole[$value['displayName']] = $value['id'];
}
$this->applicationId = $applicationEntity["id"];

$response=$this->featureContext->getJsonDecodedResponse(GraphHelper::assignRole(
$this->featureContext->getBaseUrl(),
$this->featureContext->getStepLineRef(),
$admin,
$this->featureContext->getPasswordForUser($admin),
$this->appRole[$role],
$this->applicationId,
$userId
));
if (!\array_key_exists('appRoleId', $response) && $response['appRoleId'] !== $this->appRole[$role]) {
throw new Error('Could not assign role'. $role);
};
}

/**
* @When /^user "([^"]*)" retrieves assigned role using the Graph API$/
*
* @param string $user
* @return void
*
* @throws GuzzleException
*/
public function userRetrievesAssignedRoleUsingTheGraphApi(string $user): void {
$userId = $this->featureContext->getAttributeOfCreatedUser($user, 'id');
$userId = $userId ?? $user;
$this->featureContext->setResponse(GraphHelper::getAssignedRole(
$this->featureContext->getBaseUrl(),
$this->featureContext->getStepLineRef(),
$user,
$this->featureContext->getPasswordForUser($user),
$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];
if ($this->appRole[$role] !==$response['appRoleId']) {
throw new Error('App role should be'. $role.' but found '.$response['appRoleId']);
}
}

/**
* @Then the user retrieve API response should contain the following applications information:
*
* @param TableNode $table
18 changes: 17 additions & 1 deletion tests/acceptance/features/bootstrap/RoleAssignmentContext.php
Original file line number Diff line number Diff line change
@@ -239,7 +239,7 @@ public function userGetAssignmentsList(string $user): void {
}

/**
* @When /^user "([^"]*)" should have the role "([^"]*)"$/
* @Then /^user "([^"]*)" should have the role "([^"]*)"$/
*
* @param string $user
* @param string $role
@@ -256,4 +256,20 @@ public function userShouldHaveRole(string $user, string $role): void {
$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 GuzzleException
* @throws JsonException
*/
public function theSettingApiResponseShouldHaveTheRole(string $role): void {
$rawBody = $this->featureContext->getResponse()->getBody()->getContents();
$assignmentRoleId = \json_decode($rawBody, true, 512, JSON_THROW_ON_ERROR)["assignments"][0]["roleId"];
Assert::assertEquals($this->userGetRoleIdByRoleName($this->featureContext->getAdminUserName(), $role), $assignmentRoleId, "user has no role $role");
}
}

0 comments on commit 2263b23

Please sign in to comment.