2022-11-04 12:39:54 +01:00
<? php
declare ( strict_types = 1 );
2022-03-25 15:33:35 +05:45
/**
* ownCloud
*
* @author Kiran Parajuli <kiran@jankaritech.com>
* @copyright Copyright (c) 2021 Kiran Parajuli kiran@jankaritech.com
*/
use Behat\Behat\Context\Context ;
use Behat\Behat\Hook\Scope\BeforeScenarioScope ;
2022-04-13 12:53:20 +05:45
use Behat\Gherkin\Node\TableNode ;
2022-03-25 15:33:35 +05:45
use GuzzleHttp\Exception\GuzzleException ;
2022-04-12 19:35:05 +05:45
use Psr\Http\Message\ResponseInterface ;
2022-04-06 20:16:33 +05:45
use TestHelpers\GraphHelper ;
2022-11-11 09:31:13 +05:45
use TestHelpers\WebDavHelper ;
2022-03-25 15:33:35 +05:45
use PHPUnit\Framework\Assert ;
2022-04-06 20:16:33 +05:45
require_once 'bootstrap.php' ;
2022-03-25 15:33:35 +05:45
/**
* Context for the provisioning specific steps using the Graph API
*/
class GraphContext implements Context {
/**
* @var FeatureContext
*/
private FeatureContext $featureContext ;
2022-12-16 16:29:49 +05:45
/**
* @var SpacesContext
*/
private SpacesContext $spacesContext ;
2022-11-30 16:50:58 +05:45
2022-03-25 15:33:35 +05:45
/**
* This will run before EVERY scenario.
* It will set the properties for this object.
*
* @BeforeScenario
*
* @param BeforeScenarioScope $scope
*
* @return void
*/
2022-04-19 10:21:05 +05:45
public function before ( BeforeScenarioScope $scope ) : void {
2022-03-25 15:33:35 +05:45
// Get the environment
$environment = $scope -> getEnvironment ();
// Get all the contexts you need in this context from here
$this -> featureContext = $environment -> getContext ( 'FeatureContext' );
}
/**
2022-04-06 20:16:33 +05:45
* @param string $user
* @param string|null $userName
* @param string|null $password
* @param string|null $email
* @param string|null $displayName
* @param string|null $requester
* @param string|null $requesterPassword
2022-03-25 15:33:35 +05:45
*
2022-04-19 10:21:05 +05:45
* @return void
2022-04-06 20:16:33 +05:45
* @throws JsonException
* @throws GuzzleException
*/
public function userHasBeenEditedUsingTheGraphApi (
string $user ,
? string $userName = null ,
? string $password = null ,
? string $email = null ,
? string $displayName = null ,
? string $requester = null ,
? string $requesterPassword = null
2022-04-19 10:21:05 +05:45
) : void {
2022-04-06 20:16:33 +05:45
if ( ! $requester ) {
$requester = $this -> featureContext -> getAdminUsername ();
$requesterPassword = $this -> featureContext -> getAdminPassword ();
}
$userId = $this -> featureContext -> getAttributeOfCreatedUser ( $user , 'id' );
$response = GraphHelper :: editUser (
$this -> featureContext -> getBaseUrl (),
$this -> featureContext -> getStepLineRef (),
$requester ,
$requesterPassword ,
$userId ,
$userName ,
$password ,
$email ,
$displayName
);
$this -> featureContext -> setResponse ( $response );
2022-12-12 12:46:10 +01:00
$this -> featureContext -> theHttpStatusCodeShouldBe ( 200 ); // TODO 204 when prefer=minimal header was sent
2022-04-06 20:16:33 +05:45
}
2022-12-14 14:15:18 +05:45
/**
* @When /^the user "([^"]*)" changes the email of user "([^"]*)" to "([^"]*)" using the Graph API$/
* @When /^the user "([^"]*)" tries to change the email of user "([^"]*)" to "([^"]*)" using the Graph API$/
*
* @param string $byUser
* @param string $user
* @param string $email
*
* @return void
* @throws GuzzleException
* @throws Exception
*/
public function theUserChangesTheEmailOfUserToUsingTheGraphApi ( string $byUser , string $user , string $email ) : void {
$response = $this -> editUserUsingTheGraphApi ( $byUser , $user , null , null , $email );
$this -> featureContext -> setResponse ( $response );
}
/**
* @When /^the user "([^"]*)" changes the display name of user "([^"]*)" to "([^"]*)" using the Graph API$/
* @When /^the user "([^"]*)" tries to change the display name of user "([^"]*)" to "([^"]*)" using the Graph API$/
*
* @param string $byUser
* @param string $user
* @param string $displayName
*
* @return void
* @throws GuzzleException
* @throws Exception
*/
public function theUserChangesTheDisplayNameOfUserToUsingTheGraphApi ( string $byUser , string $user , string $displayName ) : void {
$response = $this -> editUserUsingTheGraphApi ( $byUser , $user , null , null , null , $displayName );
$this -> featureContext -> setResponse ( $response );
}
/**
* @Then /^the user "([^"]*)" should have information with these key and value pairs:$/
*
* @param string $user
* @param TableNode $table
*
* @return void
* @throws Exception
* @throws GuzzleException
* @throws JsonException
*/
public function theUserShouldHaveInformationWithTheseKeyAndValuePairs ( string $user , TableNode $table ) : void {
$rows = $table -> getHash ();
$this -> adminHasRetrievedUserUsingTheGraphApi ( $user );
foreach ( $rows as $row ) {
$key = $row [ 'key' ];
$expectedValue = $row [ 'value' ];
$responseValue = $this -> featureContext -> getJsonDecodedResponse ( $this -> featureContext -> getResponse ())[ $key ];
Assert :: assertEquals (
$expectedValue ,
$responseValue
);
}
}
/**
* Edits the user information
*
* @param string $byUser
* @param string $user
* @param string|null $userName
* @param string|null $password
* @param string|null $email
* @param string|null $displayName
*
* @return void
* @throws GuzzleException
*/
public function editUserUsingTheGraphApi ( string $byUser , string $user , string $userName = null , string $password = null , string $email = null , string $displayName = null ) : ResponseInterface {
$user = $this -> featureContext -> getActualUsername ( $user );
$userId = $this -> featureContext -> getAttributeOfCreatedUser ( $user , 'id' );
$userId = $userId ?? $user ;
return GraphHelper :: editUser (
$this -> featureContext -> getBaseUrl (),
$this -> featureContext -> getStepLineRef (),
$byUser ,
$this -> featureContext -> getPasswordForUser ( $byUser ),
$userId ,
$userName ,
$password ,
$email ,
$displayName
);
}
2022-04-06 20:16:33 +05:45
/**
2022-03-25 15:33:35 +05:45
* @param string $user
*
* @return void
2022-04-06 20:16:33 +05:45
* @throws JsonException
2022-03-25 15:33:35 +05:45
* @throws GuzzleException
*/
2022-04-19 10:21:05 +05:45
public function adminHasRetrievedUserUsingTheGraphApi ( string $user ) : void {
2022-03-25 15:33:35 +05:45
$user = $this -> featureContext -> getActualUsername ( $user );
2022-04-06 20:48:29 +05:45
try {
$userId = $this -> featureContext -> getAttributeOfCreatedUser ( $user , "id" );
2022-12-14 14:15:18 +05:45
$userId = $userId ?? $user ;
2022-04-06 20:48:29 +05:45
} catch ( Exception $e ) {
$userId = $user ;
}
2022-04-06 20:16:33 +05:45
$result = GraphHelper :: getUser (
2022-03-25 15:33:35 +05:45
$this -> featureContext -> getBaseUrl (),
$this -> featureContext -> getStepLineRef (),
$this -> featureContext -> getAdminUsername (),
$this -> featureContext -> getAdminPassword (),
2022-04-06 20:48:29 +05:45
$userId
2022-04-06 20:16:33 +05:45
);
$this -> featureContext -> setResponse ( $result );
$this -> featureContext -> thenTheHTTPStatusCodeShouldBe ( 200 );
}
/**
* @param $requestingUser
* @param $targetUser
*
* @return void
* @throws JsonException
* @throws GuzzleException
*/
public function userHasRetrievedUserUsingTheGraphApi (
$requestingUser ,
$targetUser
2022-04-19 10:21:05 +05:45
) : void {
2022-04-06 20:16:33 +05:45
$requester = $this -> featureContext -> getActualUsername ( $requestingUser );
$requesterPassword = $this -> featureContext -> getPasswordForUser ( $requestingUser );
$user = $this -> featureContext -> getActualUsername ( $targetUser );
$userId = $this -> featureContext -> getAttributeOfCreatedUser ( $user , "id" );
$response = GraphHelper :: getUser (
$this -> featureContext -> getBaseUrl (),
$this -> featureContext -> getStepLineRef (),
$requester ,
$requesterPassword ,
$userId
2022-03-25 15:33:35 +05:45
);
$this -> featureContext -> setResponse ( $response );
2022-04-06 20:16:33 +05:45
$this -> featureContext -> thenTheHTTPStatusCodeShouldBe ( 200 );
}
2022-11-23 17:20:37 +05:45
/**
* @param string $groupId
* @param string|null $user
*
* @return ResponseInterface
* @throws GuzzleException
*/
public function userDeletesGroupWithGroupId (
string $groupId ,
? string $user = null
) : ResponseInterface {
$credentials = $this -> getAdminOrUserCredentials ( $user );
return GraphHelper :: deleteGroup (
$this -> featureContext -> getBaseUrl (),
$this -> featureContext -> getStepLineRef (),
$credentials [ "username" ],
$credentials [ "password" ],
$groupId
);
}
2022-04-19 10:21:05 +05:45
/**
* @param string $groupId
* @param bool $checkResult
*
* @return void
* @throws GuzzleException
*/
public function adminDeletesGroupWithGroupId (
string $groupId ,
bool $checkResult = false
) : void {
$this -> featureContext -> setResponse (
2022-11-23 17:20:37 +05:45
$this -> userDeletesGroupWithGroupId ( $groupId )
2022-04-19 10:21:05 +05:45
);
if ( $checkResult ) {
$this -> featureContext -> thenTheHTTPStatusCodeShouldBe ( 204 );
}
}
2022-04-13 12:53:20 +05:45
2022-04-06 20:16:33 +05:45
/**
* @param string $group
*
* @return void
* @throws Exception
* @throws GuzzleException
*/
public function adminDeletesGroupUsingTheGraphApi (
string $group
2022-04-19 10:21:05 +05:45
) : void {
2022-04-06 20:16:33 +05:45
$groupId = $this -> featureContext -> getAttributeOfCreatedGroup ( $group , "id" );
if ( $groupId ) {
2022-04-13 12:53:20 +05:45
$this -> adminDeletesGroupWithGroupId ( $groupId );
2022-04-06 20:16:33 +05:45
} else {
throw new Exception (
"Group id does not exist for ' $group ' in the created list."
. " Cannot delete group without id when using the Graph API."
);
}
}
/**
* @param string $group
*
* @return void
* @throws Exception
* @throws GuzzleException
*/
2022-04-19 10:21:05 +05:45
public function adminHasDeletedGroupUsingTheGraphApi ( string $group ) : void {
2022-04-06 20:16:33 +05:45
$this -> adminDeletesGroupUsingTheGraphApi ( $group );
$this -> featureContext -> thenTheHTTPStatusCodeShouldBe ( 204 );
}
/**
* sends a request to delete a user using the Graph API
*
* @param string $user username is used as the id
2022-11-30 18:54:09 +05:45
* @param string|null $byUser
2022-04-06 20:16:33 +05:45
*
* @return void
* @throws GuzzleException
*/
2022-11-30 18:54:09 +05:45
public function adminDeletesUserUsingTheGraphApi ( string $user , ? string $byUser = null ) : void {
$credentials = $this -> getAdminOrUserCredentials ( $byUser );
2022-04-06 20:16:33 +05:45
$this -> featureContext -> setResponse (
GraphHelper :: deleteUser (
$this -> featureContext -> getBaseUrl (),
$this -> featureContext -> getStepLineRef (),
2022-11-30 18:54:09 +05:45
$credentials [ "username" ],
$credentials [ "password" ],
2022-04-06 20:16:33 +05:45
$user
)
2022-03-25 15:33:35 +05:45
);
}
2022-11-28 14:13:52 +05:45
/**
* remove user from group
*
* @param string $groupId
* @param string $userId
* @param string|null $byUser
*
* @return ResponseInterface
* @throws GuzzleException
*/
public function removeUserFromGroup ( string $groupId , string $userId , ? string $byUser = null ) : ResponseInterface {
$credentials = $this -> getAdminOrUserCredentials ( $byUser );
return GraphHelper :: removeUserFromGroup (
$this -> featureContext -> getBaseUrl (),
$this -> featureContext -> getStepLineRef (),
$credentials [ 'username' ],
$credentials [ 'password' ],
$userId ,
$groupId ,
);
}
2022-11-30 18:54:09 +05:45
/**
* @When /^the user "([^"]*)" deletes a user "([^"]*)" using the Graph API$/
*
* @param string $byUser
* @param string $user
*
* @return void
* @throws Exception
* @throws GuzzleException
*/
public function theUserDeletesAUserUsingTheGraphAPI ( string $byUser , string $user ) : void {
$this -> adminDeletesUserUsingTheGraphApi ( $user , $byUser );
}
2022-03-25 15:33:35 +05:45
/**
2022-04-06 20:16:33 +05:45
* @param string $user
* @param string $group
2022-03-25 15:33:35 +05:45
*
* @return void
2022-04-06 20:16:33 +05:45
* @throws JsonException
* @throws GuzzleException
*/
2022-04-19 10:21:05 +05:45
public function adminHasRemovedUserFromGroupUsingTheGraphApi ( string $user , string $group ) : void {
2022-04-06 20:16:33 +05:45
$user = $this -> featureContext -> getActualUsername ( $user );
$groupId = $this -> featureContext -> getAttributeOfCreatedGroup ( $group , "id" );
2022-11-28 14:13:52 +05:45
$userId = $this -> featureContext -> getAttributeOfCreatedUser ( $user , "id" );
$response = $this -> removeUserFromGroup ( $groupId , $userId );
2022-04-06 20:16:33 +05:45
$this -> featureContext -> setResponse ( $response );
$this -> featureContext -> thenTheHTTPStatusCodeShouldBe ( 204 );
}
/**
* check if the provided user is present as a member in the provided group
*
* @param string $user
* @param string $group
*
* @return bool
* @throws JsonException
* @throws Exception
2022-03-25 15:33:35 +05:45
* @throws GuzzleException
*/
2022-04-06 20:16:33 +05:45
public function getUserPresenceInGroupUsingTheGraphApi ( string $user , string $group ) : bool {
$user = $this -> featureContext -> getActualUsername ( $user );
$userId = $this -> featureContext -> getAttributeOfCreatedUser ( $user , "id" );
$members = $this -> theAdminHasRetrievedMembersListOfGroupUsingTheGraphApi ( $group );
$found = false ;
foreach ( $members as $member ) {
if ( $member [ "id" ] === $userId ) {
$found = true ;
break ;
}
2022-03-25 15:33:35 +05:45
}
2022-04-06 20:16:33 +05:45
return $found ;
2022-03-25 15:33:35 +05:45
}
2022-04-12 19:35:05 +05:45
/**
* @param string $user
* @param string $group
*
* @return void
* @throws JsonException
* @throws GuzzleException
*/
2022-04-19 10:21:05 +05:45
public function userShouldNotBeMemberInGroupUsingTheGraphApi ( string $user , string $group ) : void {
2022-04-06 20:16:33 +05:45
$found = $this -> getUserPresenceInGroupUsingTheGraphApi ( $user , $group );
Assert :: assertFalse ( $found , __METHOD__ . " User $user is member of group $group " );
}
/**
* @param string $user
* @param string $group
2022-03-25 15:33:35 +05:45
*
* @return void
2022-04-06 20:16:33 +05:45
* @throws JsonException
2022-04-06 20:48:29 +05:45
* @throws GuzzleException
2022-04-06 20:16:33 +05:45
*/
2022-04-19 10:21:05 +05:45
public function userShouldBeMemberInGroupUsingTheGraphApi ( string $user , string $group ) : void {
2022-04-06 20:16:33 +05:45
$found = $this -> getUserPresenceInGroupUsingTheGraphApi ( $user , $group );
Assert :: assertTrue ( $found , __METHOD__ . "User $user is not member of group $group " );
}
/**
* @param string $user
* @param string $password
2022-12-14 14:15:18 +05:45
* @param string|null $byUser
2022-04-06 20:16:33 +05:45
*
* @return void
* @throws JsonException
*/
public function adminChangesPasswordOfUserToUsingTheGraphApi (
string $user ,
2022-12-14 14:15:18 +05:45
string $password ,
? string $byUser = null
2022-04-19 10:21:05 +05:45
) : void {
2022-12-14 14:15:18 +05:45
$credentials = $this -> getAdminOrUserCredentials ( $byUser );
2022-04-06 20:16:33 +05:45
$user = $this -> featureContext -> getActualUsername ( $user );
2022-12-14 14:15:18 +05:45
$userId = $this -> featureContext -> getAttributeOfCreatedUser ( $user , "id" );
$userId = $userId ?? $user ;
2022-04-06 20:16:33 +05:45
$response = GraphHelper :: editUser (
$this -> featureContext -> getBaseUrl (),
$this -> featureContext -> getStepLineRef (),
2022-12-14 14:15:18 +05:45
$credentials [ "username" ],
$credentials [ "password" ],
2022-04-06 20:16:33 +05:45
$userId ,
null ,
$password
);
$this -> featureContext -> setResponse ( $response );
}
2022-12-14 14:15:18 +05:45
/**
* @When /^the user "([^"]*)" resets the password of user "([^"]*)" to "([^"]*)" using the Graph API$/
*
* @param string $byUser
* @param string $user
* @param string $password
*
* @return void
* @throws Exception
*/
public function theUserResetsThePasswordOfUserToUsingTheGraphApi ( string $byUser , string $user , string $password ) {
$this -> adminChangesPasswordOfUserToUsingTheGraphApi ( $user , $password , $byUser );
}
2022-04-06 20:16:33 +05:45
/**
2022-04-12 19:35:05 +05:45
*
2022-11-15 20:56:43 +05:45
* @param array $groups
*
* @return void
* @throws Exception
*/
public function theseGroupsShouldBeInTheResponse ( array $groups ) : void {
$respondedGroups = $this -> getArrayOfGroupsResponded ( $this -> featureContext -> getResponse ());
foreach ( $groups as $group ) {
$found = false ;
foreach ( $respondedGroups as $respondedGroup ) {
if ( $respondedGroup [ "displayName" ] === $group ) {
$found = true ;
break ;
}
}
Assert :: assertTrue ( $found , "Group ' $group ' not found in the list" );
}
}
/**
*
* @param array $users
*
* @return void
2022-03-25 15:33:35 +05:45
* @throws Exception
2022-11-15 20:56:43 +05:45
*/
public function theseUsersShouldBeInTheResponse ( array $users ) : void {
$respondedUsers = $this -> getArrayOfUsersResponded ( $this -> featureContext -> getResponse ());
foreach ( $users as $user ) {
$found = false ;
foreach ( $respondedUsers as $respondedUser ) {
if ( $respondedUser [ "onPremisesSamAccountName" ] === $user ) {
$found = true ;
break ;
}
}
Assert :: assertTrue ( $found , "User ' $user ' not found in the list" );
}
}
/**
*
* @param string|null $user
*
* @return array
*/
public function getAdminOrUserCredentials ( ? string $user ) : array {
$credentials [ "username" ] = $user ? $this -> featureContext -> getActualUsername ( $user ) : $this -> featureContext -> getAdminUsername ();
$credentials [ "password" ] = $user ? $this -> featureContext -> getPasswordForUser ( $user ) : $this -> featureContext -> getAdminPassword ();
return $credentials ;
}
/**
*
* @param string|null $user
*
* @return ResponseInterface
2022-04-12 19:35:05 +05:45
* @throws GuzzleException
2022-03-25 15:33:35 +05:45
*/
2022-11-15 20:56:43 +05:45
public function listGroups ( ? string $user = null ) : ResponseInterface {
$credentials = $this -> getAdminOrUserCredentials ( $user );
return GraphHelper :: getGroups (
2022-04-06 20:16:33 +05:45
$this -> featureContext -> getBaseUrl (),
$this -> featureContext -> getStepLineRef (),
2022-11-15 20:56:43 +05:45
$credentials [ "username" ],
$credentials [ "password" ]
2022-03-25 15:33:35 +05:45
);
2022-11-15 20:56:43 +05:45
}
/**
* returns list of groups
*
* @param ResponseInterface $response
*
* @return array
* @throws Exception
*/
public function getArrayOfGroupsResponded ( ResponseInterface $response ) : array {
2022-04-06 20:16:33 +05:45
if ( $response -> getStatusCode () === 200 ) {
2022-04-12 19:35:05 +05:45
$jsonResponseBody = $this -> featureContext -> getJsonDecodedResponse ( $response );
return $jsonResponseBody [ "value" ];
2022-04-06 20:16:33 +05:45
} else {
2022-04-12 19:35:05 +05:45
$this -> throwHttpException ( $response , "Could not retrieve groups list." );
2022-04-06 20:16:33 +05:45
}
}
/**
*
* @return array
* @throws Exception
* @throws GuzzleException
*/
2022-11-15 20:56:43 +05:45
public function adminHasRetrievedGroupListUsingTheGraphApi () : array {
return $this -> getArrayOfGroupsResponded ( $this -> listGroups ());
}
/**
*
* @param string $group
* @param string|null $user
*
* @return ResponseInterface
* @throws GuzzleException
*/
public function listGroupMembers ( string $group , ? string $user = null ) : ResponseInterface {
$credentials = $this -> getAdminOrUserCredentials ( $user );
return GraphHelper :: getMembersList (
2022-04-06 20:16:33 +05:45
$this -> featureContext -> getBaseUrl (),
$this -> featureContext -> getStepLineRef (),
2022-11-15 20:56:43 +05:45
$credentials [ "username" ],
$credentials [ "password" ],
2022-04-06 20:16:33 +05:45
$this -> featureContext -> getAttributeOfCreatedGroup ( $group , 'id' )
);
2022-11-15 20:56:43 +05:45
}
/**
* returns list of users of a group
*
* @param ResponseInterface $response
*
* @return array
* @throws Exception
*/
public function getArrayOfUsersResponded ( ResponseInterface $response ) : array {
2022-04-06 20:16:33 +05:45
if ( $response -> getStatusCode () === 200 ) {
2022-04-06 20:48:29 +05:45
return $this -> featureContext -> getJsonDecodedResponse ( $response );
2022-04-06 20:16:33 +05:45
} else {
2022-11-15 20:56:43 +05:45
$this -> throwHttpException ( $response , "Could not retrieve group members list." );
2022-04-06 20:16:33 +05:45
}
}
2022-11-15 20:56:43 +05:45
/**
* returns a list of members in group
*
* @param string $group
*
* @return array
* @throws Exception
* @throws GuzzleException
*/
public function theAdminHasRetrievedMembersListOfGroupUsingTheGraphApi ( string $group ) : array {
return $this -> getArrayOfUsersResponded ( $this -> listGroupMembers ( $group ));
}
2022-04-06 20:16:33 +05:45
/**
* creates a user with provided data
* actor: the administrator
*
* @param string $user
* @param string $password
* @param string $email
* @param string $displayName
*
2022-04-13 12:53:20 +05:45
* @return void
2022-05-11 15:04:12 +05:45
* @throws Exception|GuzzleException
2022-04-06 20:16:33 +05:45
*/
public function theAdminHasCreatedUser (
string $user ,
string $password ,
string $email ,
string $displayName
2022-04-13 12:53:20 +05:45
) : void {
2022-04-06 20:16:33 +05:45
$response = GraphHelper :: createUser (
$this -> featureContext -> getBaseUrl (),
$this -> featureContext -> getStepLineRef (),
$this -> featureContext -> getAdminUsername (),
$this -> featureContext -> getAdminPassword (),
$user ,
$password ,
$email ,
$displayName
2022-03-25 15:33:35 +05:45
);
2022-04-06 20:16:33 +05:45
if ( $response -> getStatusCode () !== 200 ) {
2022-04-12 19:35:05 +05:45
$this -> throwHttpException ( $response , "Could not create user $user " );
2022-04-06 20:16:33 +05:45
} else {
2022-04-13 12:53:20 +05:45
$this -> featureContext -> setResponse ( $response );
2022-04-06 20:16:33 +05:45
}
2022-03-25 15:33:35 +05:45
}
2022-11-04 12:39:54 +01:00
/**
* @When /^the user "([^"]*)" creates a new user using GraphAPI with the following settings:$/
*
* @param string $user
* @param TableNode $table
*
* @return void
* @throws Exception|GuzzleException
*/
public function theUserCreatesNewUser ( string $user , TableNode $table ) : void {
$rows = $table -> getRowsHash ();
$response = GraphHelper :: createUser (
$this -> featureContext -> getBaseUrl (),
$this -> featureContext -> getStepLineRef (),
$user ,
$this -> featureContext -> getPasswordForUser ( $user ),
$rows [ "userName" ],
$rows [ "password" ],
$rows [ "email" ],
$rows [ "displayName" ]
);
// add created user to list except for the user with an empty name
// because request /graph/v1.0/users/emptyUserName exits with 200
// and we cannot check that the user with empty name doesn't exist
if ( ! empty ( $rows [ "userName" ])) {
$this -> featureContext -> addUserToCreatedUsersList (
$rows [ "userName" ],
$rows [ "password" ],
$rows [ "displayName" ],
$rows [ "email" ]
);
}
$this -> featureContext -> setResponse ( $response );
}
2022-11-30 18:54:09 +05:45
/**
* @Given /^the user "([^"]*)" has created a new user using the Graph API with the following settings:$/
*
* @param string $user
* @param TableNode $table
*
* @return void
* @throws Exception|GuzzleException
*/
public function theUserHasCreatedANewUserUsingGraphapiWithTheFollowingSettings ( string $user , TableNode $table ) : void {
$this -> theUserCreatesNewUser (
$user ,
$table
);
$rows = $table -> getRowsHash ();
$response = $this -> featureContext -> getResponse ();
if ( $response -> getStatusCode () !== 200 ) {
$this -> throwHttpException ( $response , "Could not create user ' $rows[userName] '" );
}
}
2022-03-25 15:33:35 +05:45
/**
2022-04-06 20:16:33 +05:45
* adds a user to a group
2022-03-25 15:33:35 +05:45
*
2022-11-23 16:18:04 +05:45
* @param string $group
* @param string $user
* @param string|null $byUser
*
* @return ResponseInterface
* @throws GuzzleException
*/
public function addUserToGroup ( string $group , string $user , ? string $byUser = null ) : ResponseInterface {
$credentials = $this -> getAdminOrUserCredentials ( $byUser );
try {
$groupId = $this -> featureContext -> getAttributeOfCreatedGroup ( $group , "id" );
} catch ( Exception $e ) {
$groupId = WebDavHelper :: generateUUIDv4 ();
}
try {
$userId = $this -> featureContext -> getAttributeOfCreatedUser ( $user , "id" );
} catch ( Exception $e ) {
$userId = WebDavHelper :: generateUUIDv4 ();
}
return GraphHelper :: addUserToGroup (
$this -> featureContext -> getBaseUrl (),
$this -> featureContext -> getStepLineRef (),
$credentials [ 'username' ],
$credentials [ 'password' ],
$userId ,
$groupId
);
}
/**
2022-07-08 05:18:40 +02:00
* @Given /^the administrator has added a user "([^"]*)" to the group "([^"]*)" using GraphApi$/
*
2022-04-06 20:16:33 +05:45
* @param string $user
2022-04-06 20:48:29 +05:45
* @param string $group
2022-04-06 20:16:33 +05:45
* @param bool $checkResult
2022-03-25 15:33:35 +05:45
*
* @return void
2022-04-06 20:16:33 +05:45
* @throws Exception
2022-03-25 15:33:35 +05:45
* @throws GuzzleException
*/
2022-04-06 20:16:33 +05:45
public function adminHasAddedUserToGroupUsingTheGraphApi (
string $user ,
2022-04-06 20:48:29 +05:45
string $group ,
2022-04-06 20:16:33 +05:45
bool $checkResult = true
2022-04-19 10:21:05 +05:45
) : void {
2022-11-23 16:18:04 +05:45
$result = $this -> addUserToGroup ( $group , $user );
2022-04-06 20:16:33 +05:45
if ( $checkResult && ( $result -> getStatusCode () !== 204 )) {
2022-04-12 19:35:05 +05:45
$this -> throwHttpException ( $result , "Could not add user ' $user ' to group ' $group '." );
2022-04-06 20:16:33 +05:45
}
2022-03-25 15:33:35 +05:45
}
2022-11-23 16:18:04 +05:45
/**
* @When the administrator adds the following users to the following groups using the Graph API
*
* @param TableNode $table
*
* @return void
*/
public function theAdministratorAddsTheFollowingUsersToTheFollowingGroupsUsingTheGraphAPI ( TableNode $table ) : void {
$this -> featureContext -> verifyTableNodeColumns ( $table , [ 'username' , 'groupname' ]);
$userGroupList = $table -> getColumnsHash ();
foreach ( $userGroupList as $userGroup ) {
$this -> featureContext -> setResponse ( $this -> addUserToGroup ( $userGroup [ 'groupname' ], $userGroup [ 'username' ]));
$this -> featureContext -> pushToLastHttpStatusCodesArray ();
}
}
/**
* @When the administrator tries to add user :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 ));
}
2022-11-22 13:03:56 +05:45
2022-11-23 16:18:04 +05:45
/**
* @When user :user tries to add himself/herself to group :group using the Graph API
*
* @param string $user
* @param string $group
*
* @return void
*/
public function theUserTriesToAddHimselfToGroupUsingTheGraphAPI ( string $user , string $group ) : void {
$this -> featureContext -> setResponse ( $this -> addUserToGroup ( $group , $user , $user ));
}
/**
* @When user :byUser tries to add user :user to group :group using the Graph API
*
* @param string $byUser
* @param string $user
* @param string $group
*
* @return void
*/
public function theUserTriesToAddAnotherUserToGroupUsingTheGraphAPI ( string $byUser , string $user , string $group ) : void {
$this -> featureContext -> setResponse ( $this -> addUserToGroup ( $group , $byUser , $user ));
}
2022-08-19 17:16:12 +05:45
/**
*
* @param string $group
2022-11-11 09:31:13 +05:45
* @param ?string $user
2022-08-19 17:16:12 +05:45
*
2022-11-11 09:31:13 +05:45
* @return ResponseInterface
2022-08-19 17:16:12 +05:45
* @throws GuzzleException
*/
2022-11-11 09:31:13 +05:45
public function createGroup ( string $group , ? string $user = null ) : ResponseInterface {
2022-11-15 20:56:43 +05:45
$credentials = $this -> getAdminOrUserCredentials ( $user );
2022-11-11 09:31:13 +05:45
return GraphHelper :: createGroup (
2022-08-19 17:16:12 +05:45
$this -> featureContext -> getBaseUrl (),
$this -> featureContext -> getStepLineRef (),
2022-11-15 20:56:43 +05:45
$credentials [ "username" ],
$credentials [ "password" ],
2022-08-19 17:16:12 +05:45
$group ,
);
2022-11-11 09:31:13 +05:45
}
/**
* @When /^the administrator creates a group "([^"]*)" using the Graph API$/
* @When user :user creates a group :group using the Graph API
* @When user :user tries to create a group :group using the Graph API
*
* @param string $group
* @param ?string $user
*
* @return void
* @throws Exception
* @throws GuzzleException
*/
public function userCreatesGroupUsingTheGraphApi ( string $group , ? string $user = null ) : void {
$response = $this -> createGroup ( $group , $user );
2022-08-19 17:16:12 +05:45
$this -> featureContext -> setResponse ( $response );
$this -> featureContext -> pushToLastHttpStatusCodesArray (( string ) $response -> getStatusCode ());
if ( $response -> getStatusCode () === 200 ) {
$groupId = $this -> featureContext -> getJsonDecodedResponse ( $response )[ "id" ];
$this -> featureContext -> addGroupToCreatedGroupsList ( $group , true , true , $groupId );
}
}
2022-03-25 15:33:35 +05:45
/**
2022-04-06 20:16:33 +05:45
* create group with provided data
2022-03-25 15:33:35 +05:45
*
2022-04-06 20:16:33 +05:45
* @param string $group
*
* @return array
* @throws Exception
2022-03-25 15:33:35 +05:45
* @throws GuzzleException
*/
2022-04-19 10:21:05 +05:45
public function adminHasCreatedGroupUsingTheGraphApi ( string $group ) : array {
2022-11-11 09:31:13 +05:45
$result = $this -> createGroup ( $group );
2022-04-06 20:16:33 +05:45
if ( $result -> getStatusCode () === 200 ) {
2022-04-06 20:48:29 +05:45
return $this -> featureContext -> getJsonDecodedResponse ( $result );
2022-04-06 20:16:33 +05:45
} else {
2022-04-12 19:35:05 +05:45
$this -> throwHttpException ( $result , "Could not create group ' $group '." );
}
}
/**
* @param ResponseInterface $response
* @param string $errorMsg
*
* @return void
* @throws Exception
*/
2022-04-19 10:21:05 +05:45
private function throwHttpException ( ResponseInterface $response , string $errorMsg ) : void {
2022-04-12 19:35:05 +05:45
try {
$jsonBody = $this -> featureContext -> getJsonDecodedResponse ( $response );
throw new Exception (
__METHOD__
. " \n $errorMsg "
. " \n HTTP status code: " . $response -> getStatusCode ()
. " \n Error code: " . $jsonBody [ "error" ][ "code" ]
. " \n Message: " . $jsonBody [ "error" ][ "message" ]
);
} catch ( TypeError $e ) {
throw new Exception (
__METHOD__
. " \n $errorMsg "
. " \n HTTP status code: " . $response -> getStatusCode ()
. " \n Response body: " . $response -> getBody ()
);
2022-03-25 15:33:35 +05:45
}
}
2022-04-13 12:53:20 +05:45
2022-04-19 10:21:05 +05:45
/**
* @param string $shouldOrNot (not|)
* @param TableNode $table
*
2022-09-06 12:32:56 +05:45
* @return void
2022-04-19 10:21:05 +05:45
* @throws GuzzleException
* @throws Exception
*/
public function theseGroupsShouldNotExist ( string $shouldOrNot , TableNode $table ) : void {
$should = ( $shouldOrNot !== "not" );
$this -> featureContext -> verifyTableNodeColumns ( $table , [ 'groupname' ]);
$actualGroupsList = $this -> adminHasRetrievedGroupListUsingTheGraphApi ();
$expectedGroups = $table -> getColumnsHash ();
// check if every expected group is(not) in the actual groups list
foreach ( $expectedGroups as $expectedGroup ) {
$groupName = $expectedGroup [ 'groupname' ];
$groupExists = false ;
foreach ( $actualGroupsList as $actualGroup ) {
if ( $actualGroup [ 'displayName' ] === $groupName ) {
$groupExists = true ;
break ;
}
}
if ( $groupExists !== $should ) {
throw new Exception (
__METHOD__
. " \n Group ' $groupName ' is expected " . ( $should ? "" : "not " )
. "to exist, but it does" . ( $should ? " not" : "" ) . " exist."
);
}
}
}
2022-08-31 13:02:33 +02:00
/**
* @When /^the user "([^"]*)" changes its own password "([^"]*)" to "([^"]*)" using the Graph API$/
2022-09-06 12:32:56 +05:45
*
2022-08-31 13:02:33 +02:00
* @param string $user
* @param string $currentPassword
* @param string $newPassword
*
2022-09-06 12:32:56 +05:45
* @return void
2022-08-31 13:02:33 +02:00
* @throws GuzzleException
* @throws Exception
*/
2022-11-04 12:39:54 +01:00
public function userChangesOwnPassword ( string $user , string $currentPassword , string $newPassword ) : void {
2022-08-31 13:02:33 +02:00
$response = GraphHelper :: changeOwnPassword (
$this -> featureContext -> getBaseUrl (),
$this -> featureContext -> getStepLineRef (),
$user ,
$this -> featureContext -> getPasswordForUser ( $user ),
$currentPassword ,
$newPassword
);
$this -> featureContext -> setResponse ( $response );
}
2022-11-15 20:56:43 +05:45
/**
* @When user :user gets all the groups using the Graph API
*
* @param string $user
*
* @return void
*/
public function userGetsAllTheGroupsUsingTheGraphApi ( string $user ) : void {
$this -> featureContext -> setResponse ( $this -> listGroups ( $user ));
}
/**
* @When user :user gets all the members of group :group using the Graph API
*
* @param string $user
* @param string $group
*
* @return void
*/
public function userGetsAllTheMembersOfGroupUsingTheGraphApi ( $user , $group ) : void {
$this -> featureContext -> setResponse ( $this -> listGroupMembers ( $group , $user ));
}
/**
* @Then the last response should be an unauthorized response
*
* @return void
*/
public function theLastResponseShouldBeUnauthorizedReponse () : void {
$response = $this -> featureContext -> getJsonDecodedResponse ( $this -> featureContext -> getResponse ());
$errorText = $response [ 'error' ][ 'message' ];
Assert :: assertEquals (
'Unauthorized' ,
$errorText ,
__METHOD__
. " \n Expected unauthorized message but got '" . $errorText . "'"
);
}
2022-11-23 16:18:04 +05:45
2022-11-23 17:20:37 +05:45
/**
* @When user :user deletes group :group using the Graph API
* @When the administrator deletes group :group using the Graph API
* @When user :user tries to delete group :group using the Graph API
*
* @param string $group
* @param string|null $user
*
* @return void
*/
public function userDeletesGroupUsingTheGraphApi ( string $group , ? string $user ) : void {
$groupId = $this -> featureContext -> getAttributeOfCreatedGroup ( $group , "id" );
$response = $this -> userDeletesGroupWithGroupId ( $groupId , $user );
$this -> featureContext -> setResponse ( $response );
}
2022-11-22 13:03:56 +05:45
2022-11-23 16:18:04 +05:45
/**
* @Then the following users should be listed in the following groups
*
* @param TableNode $table
*
* @return void
* @throws Exception
*/
public function theFollowingUsersShouldBeListedInFollowingGroups ( TableNode $table ) : void {
$this -> featureContext -> verifyTableNodeColumns ( $table , [ 'username' , 'groupname' ]);
$usersGroups = $table -> getColumnsHash ();
foreach ( $usersGroups as $userGroup ) {
$members = $this -> listGroupMembers ( $userGroup [ 'groupname' ]);
$members = $this -> featureContext -> getJsonDecodedResponse ( $members );
$exists = false ;
foreach ( $members as $member ) {
if ( $member [ 'onPremisesSamAccountName' ] === $userGroup [ 'username' ]) {
$exists = true ;
break ;
}
}
Assert :: assertTrue (
$exists ,
__METHOD__
. " \n Expected user '" . $userGroup [ 'username' ] . "' to be in group '" . $userGroup [ 'groupname' ] . "'. But not found."
);
}
}
2022-11-24 15:13:27 +05:45
/**
* rename group name
*
* @param string $oldGroup
* @param string $newGroup
* @param string $user
*
* @return ResponseInterface
* @throws GuzzleException
*/
public function renameGroup ( string $oldGroup , string $newGroup , ? string $user = null ) : ResponseInterface {
$credentials = $this -> getAdminOrUserCredentials ( $user );
$groupId = $this -> featureContext -> getAttributeOfCreatedGroup ( $oldGroup , "id" );
return GraphHelper :: updateGroup (
$this -> featureContext -> getBaseUrl (),
$this -> featureContext -> getStepLineRef (),
$credentials [ 'username' ],
$credentials [ 'password' ],
$groupId ,
$newGroup
);
}
/**
* @When user :user renames group :oldGroup to :newGroup using the Graph API
*
* @param string $user
* @param string $oldGroup
* @param string $newGroup
*
* @return void
*/
public function userRenamesGroupUsingTheGraphApi ( string $user , string $oldGroup , string $newGroup ) : void {
$this -> featureContext -> setResponse ( $this -> renameGroup ( $oldGroup , $newGroup , $user ));
}
2022-11-28 14:13:52 +05:45
/**
* @When the administrator removes the following users from the following groups using the Graph API
*
* @param TableNode $table
*
* @return void
*/
public function theAdministratorRemovesTheFollowingUsersFromTheFollowingGroupsUsingTheGraphApi ( TableNode $table ) : void {
$this -> featureContext -> verifyTableNodeColumns ( $table , [ 'username' , 'groupname' ]);
$usersGroups = $table -> getColumnsHash ();
foreach ( $usersGroups as $userGroup ) {
$groupId = $this -> featureContext -> getAttributeOfCreatedGroup ( $userGroup [ 'groupname' ], "id" );
$userId = $this -> featureContext -> getAttributeOfCreatedUser ( $userGroup [ 'username' ], "id" );
$this -> featureContext -> setResponse ( $this -> removeUserFromGroup ( $groupId , $userId ));
$this -> featureContext -> pushToLastHttpStatusCodesArray ();
}
}
/**
* @When the administrator removes user :user from group :group using the Graph API
*
* @param string $user
* @param string $group
*
* @return void
*/
public function theAdministratorTriesToRemoveUserFromGroupUsingTheGraphAPI ( string $user , string $group ) : void {
$groupId = $this -> featureContext -> getAttributeOfCreatedGroup ( $group , "id" );
$userId = $this -> featureContext -> getAttributeOfCreatedUser ( $user , "id" );
$this -> featureContext -> setResponse ( $this -> removeUserFromGroup ( $groupId , $userId ));
}
/**
* @When the administrator tries to remove user :user from group :group using the Graph API
* @When user :byUser tries to remove user :user from group :group using the Graph API
*
* @param string $user
* @param string $group
* @param string|null $byUser
*
* @return void
*/
public function theUserTriesToRemoveAnotherUserFromGroupUsingTheGraphAPI ( string $user , string $group , ? string $byUser = null ) : void {
try {
$groupId = $this -> featureContext -> getAttributeOfCreatedGroup ( $group , "id" );
} catch ( Exception $e ) {
$groupId = WebDavHelper :: generateUUIDv4 ();
}
try {
$userId = $this -> featureContext -> getAttributeOfCreatedUser ( $user , "id" );
} catch ( Exception $e ) {
$userId = WebDavHelper :: generateUUIDv4 ();
}
$this -> featureContext -> setResponse ( $this -> removeUserFromGroup ( $groupId , $userId , $byUser ));
}
2022-11-22 13:03:56 +05:45
/**
* @param string $user
*
* @return ResponseInterface
* @throws JsonException
* @throws GuzzleException
*/
public function retrieveUserInformationUsingGraphApi (
string $user
) : ResponseInterface {
$credentials = $this -> getAdminOrUserCredentials ( $user );
2022-11-24 14:12:26 +05:45
return GraphHelper :: getOwnInformationAndGroupMemberships (
2022-11-22 13:03:56 +05:45
$this -> featureContext -> getBaseUrl (),
$this -> featureContext -> getStepLineRef (),
$credentials [ "username" ],
$credentials [ "password" ],
);
}
/**
2022-11-24 14:12:26 +05:45
* @When /^the user "([^"]*)" retrieves (her|his) information using the Graph API$/
2022-11-22 13:03:56 +05:45
*
* @param string $user
*
* @return void
* @throws JsonException
*/
public function userRetrievesHisorHerInformationOfUserUsingGraphApi (
string $user
) : void {
$response = $this -> retrieveUserInformationUsingGraphApi ( $user );
$this -> featureContext -> setResponse ( $response );
}
/**
2022-11-24 14:12:26 +05:45
* @Then /^the user retrieve API response should contain the following information:$/
2022-11-22 13:03:56 +05:45
*
* @param TableNode $table
*
* @return void
2022-11-24 14:12:26 +05:45
* @throws GuzzleException
2022-11-22 13:03:56 +05:45
*/
2022-11-24 14:12:26 +05:45
public function theUserRetrieveApiResponseShouldContainTheFollowingInformation ( TableNode $table ) : void {
$rows = $table -> getHash ();
$apiResponse = $this -> featureContext -> getJsonDecodedResponse ( $this -> featureContext -> getResponse ());
foreach ( $rows as $row ) {
$this -> checkUserInformation ( $row , $apiResponse );
2022-11-22 13:03:56 +05:45
}
2022-11-24 14:12:26 +05:45
}
2022-11-22 13:03:56 +05:45
2022-11-24 14:12:26 +05:45
/**
* @param array $expectedValue
* @param array $actualValue
*
* @throws GuzzleException
* @return void
*/
public function checkUserInformation ( array $expectedValue , array $actualValue ) : void {
foreach ( array_keys ( $expectedValue ) as $keyName ) {
switch ( $keyName ) {
case "memberOf" :
$memberOfFromApiReponse = [];
$memberOf = preg_split ( '/\s*,\s*/' , trim ( $expectedValue [ 'memberOf' ]));
foreach ( $actualValue [ 'memberOf' ] as $member ) {
$memberOfFromApiReponse [] = $member [ 'displayName' ];
}
Assert :: assertEqualsCanonicalizing ( $memberOf , $memberOfFromApiReponse );
break ;
case "id" :
if ( $expectedValue [ $keyName ] !== '%uuid_v4%' ) {
throw new Error (
'Only UUIDv4 patterned user id can be checked' . ' but got '
. trim ( $expectedValue [ $keyName ], '%' )
);
}
2022-12-21 16:55:14 +05:45
Assert :: assertTrue ( GraphHelper :: isUUIDv4 ( $actualValue [ 'id' ]), __METHOD__ . ' Expected user_id to have UUIDv4 pattern but found: ' . $actualValue [ 'id' ]);
2022-11-24 14:12:26 +05:45
break ;
default :
Assert :: assertEquals (
$expectedValue [ $keyName ],
$actualValue [ $keyName ],
__METHOD__ .
' Expected ' . $keyName . 'to have value' . $expectedValue [ $keyName ]
. ' but got ' . $actualValue [ $keyName ]
);
break ;
2022-11-22 13:03:56 +05:45
}
}
}
2022-11-29 14:13:33 +05:45
2022-11-29 16:39:10 +05:45
/**
2022-12-19 11:34:25 +05:45
* @When user :byUser tries to get information of user :user using Graph API
* @When user :byUser gets information of user :user using Graph API
2022-11-29 16:39:10 +05:45
*
2022-12-19 11:34:25 +05:45
* @param string $byUser
2022-11-29 16:39:10 +05:45
* @param string $user
*
* @return void
2022-12-19 11:34:25 +05:45
* @throws GuzzleException
2022-11-29 16:39:10 +05:45
*/
2022-12-19 11:34:25 +05:45
public function userTriesToGetInformationOfUser ( string $byUser , string $user ) : void {
$credentials = $this -> getAdminOrUserCredentials ( $byUser );
2022-11-29 16:39:10 +05:45
$response = GraphHelper :: getUser (
$this -> featureContext -> getBaseUrl (),
$this -> featureContext -> getStepLineRef (),
$credentials [ 'username' ],
$credentials [ 'password' ],
2022-12-19 11:34:25 +05:45
$user
2022-11-29 16:39:10 +05:45
);
$this -> featureContext -> setResponse ( $response );
}
/**
2022-12-19 11:34:25 +05:45
* @When user :user tries to get all users using the Graph API
* @When user :user gets all users using the Graph API
2022-11-29 16:39:10 +05:45
*
* @param string $user
*
* @return void
2022-12-19 11:34:25 +05:45
* @throws GuzzleException
2022-11-29 16:39:10 +05:45
*/
public function userGetsAllUserUsingTheGraphApi ( string $user ) {
$credentials = $this -> getAdminOrUserCredentials ( $user );
$response = GraphHelper :: getUsers (
$this -> featureContext -> getBaseUrl (),
$this -> featureContext -> getStepLineRef (),
$credentials [ 'username' ],
$credentials [ 'password' ],
);
$this -> featureContext -> setResponse ( $response );
}
/**
2022-12-19 11:34:25 +05:45
* @Then the API response should contain all users with the following information:
2022-11-29 16:39:10 +05:45
*
* @param TableNode $table
*
* @throws Exception
* @return void
*/
public function theApiResponseShouldContainAllUserWithFollowingInformation ( TableNode $table ) : void {
$values = $table -> getHash ();
$apiResponse = $this -> featureContext -> getJsonDecodedResponse ( $this -> featureContext -> getResponse ())[ 'value' ];
foreach ( $values as $expectedValue ) {
$found = false ;
foreach ( $apiResponse as $key => $actualResponseValue ) {
if ( $expectedValue [ "displayName" ] === $actualResponseValue [ "displayName" ]) {
$found = true ;
$this -> checkUserInformation ( $expectedValue , $actualResponseValue );
unset ( $apiResponse [ $key ]);
break ;
}
}
if ( ! $found ) {
throw new Exception ( 'User ' . $expectedValue [ "displayName" ] . ' could not be found in the response.' );
}
}
}
/**
2022-12-19 11:34:25 +05:45
* @param string $byUser
* @param string|null $user
2022-11-29 16:39:10 +05:45
*
* @return ResponseInterface
* @throws JsonException
* @throws GuzzleException
*/
public function retrieveUserInformationAlongWithDriveUsingGraphApi (
2022-12-19 11:34:25 +05:45
string $byUser ,
? string $user = null
2022-11-29 16:39:10 +05:45
) : ResponseInterface {
2022-12-19 11:34:25 +05:45
$user = $user ?? $byUser ;
2022-11-29 16:39:10 +05:45
$credentials = $this -> getAdminOrUserCredentials ( $user );
return GraphHelper :: getUserWithDriveInformation (
$this -> featureContext -> getBaseUrl (),
$this -> featureContext -> getStepLineRef (),
$credentials [ "username" ],
$credentials [ "password" ],
2022-12-19 11:34:25 +05:45
$user
2022-11-29 16:39:10 +05:45
);
}
/**
2022-12-19 11:34:25 +05:45
* @When /^the user "([^"]*)" gets user "([^"]*)" along with (his|her) drive information using Graph API$/
2022-11-29 16:39:10 +05:45
*
2022-12-19 11:34:25 +05:45
* @param string $byUser
2022-11-29 16:39:10 +05:45
* @param string $user
*
* @return void
*/
2022-12-19 11:34:25 +05:45
public function userTriesToGetInformationOfUserAlongWithHisDriveData ( string $byUser , string $user ) {
$response = $this -> retrieveUserInformationAlongWithDriveUsingGraphApi ( $byUser , $user );
2022-11-29 16:39:10 +05:45
$this -> featureContext -> setResponse ( $response );
}
/**
*
2022-12-19 11:34:25 +05:45
* @When /^the user "([^"]*)" gets (his|her) drive information using Graph API$/
2022-11-29 16:39:10 +05:45
*
* @param string $user
*
* @return void
*/
public function userTriesToGetOwnDriveInformation ( string $user ) {
$response = $this -> retrieveUserInformationAlongWithDriveUsingGraphApi ( $user );
$this -> featureContext -> setResponse ( $response );
}
2022-11-30 16:50:58 +05:45
2022-12-16 16:29:49 +05:45
/**
* @param array $driveInformation
*
* @return string
*/
public static function getSpaceIdFromActualDriveinformation ( array $driveInformation ) : string {
return $driveInformation [ 'id' ];
}
2022-11-30 16:50:58 +05:45
2022-12-16 16:29:49 +05:45
/**
* check if single drive information is correct
*
* @param array $expectedDriveInformation
2022-12-21 16:55:14 +05:45
* @param array $actualDriveInformation
2022-12-16 16:29:49 +05:45
*
* @return void
*/
2022-12-21 16:55:14 +05:45
public function checkUserDriveInformation ( array $expectedDriveInformation , array $actualDriveInformation ) : void {
2022-12-16 16:29:49 +05:45
foreach ( array_keys ( $expectedDriveInformation ) as $keyName ) {
2022-12-28 14:47:53 +05:45
$actualKeyValue = GraphHelper :: separateAndGetValueForKey ( $keyName , $actualDriveInformation );
2022-12-16 16:29:49 +05:45
switch ( $expectedDriveInformation [ $keyName ]) {
case '%user_id%' :
2022-12-21 16:55:14 +05:45
Assert :: assertTrue ( GraphHelper :: isUUIDv4 ( $actualKeyValue ), __METHOD__ . ' Expected user_id to have UUIDv4 pattern but found: ' . $actualKeyValue );
2022-12-16 16:29:49 +05:45
break ;
case '%space_id%' :
2022-12-21 16:55:14 +05:45
Assert :: assertTrue ( GraphHelper :: isSpaceId ( $actualKeyValue ), __METHOD__ . ' Expected space_id to have a UUIDv4:UUIDv4 pattern but found: ' . $actualKeyValue );
2022-12-16 16:29:49 +05:45
break ;
default :
$expectedDriveInformation [ $keyName ] = $this -> featureContext -> substituteInLineCodes (
$expectedDriveInformation [ $keyName ],
$this -> featureContext -> getCurrentUser (),
[],
[
[
2022-12-21 16:55:14 +05:45
// the actual space_id is substituted from the actual drive information rather than making an API request and substituting
2022-12-16 16:29:49 +05:45
"code" => "%space_id%" ,
"function" =>
[ $this , "getSpaceIdFromActualDriveinformation" ],
2022-12-21 16:55:14 +05:45
"parameter" => [ $actualDriveInformation ]
2022-12-16 16:29:49 +05:45
],
]
);
2022-12-21 16:55:14 +05:45
Assert :: assertEquals ( $expectedDriveInformation [ $keyName ], $actualKeyValue );
2022-12-16 16:29:49 +05:45
}
}
}
2022-11-30 16:50:58 +05:45
2022-12-16 16:29:49 +05:45
/**
* @param TableNode $table
*
* @Then the user retrieve API response should contain the following drive information:
*
* @return void
*/
public function theResponseShouldContainTheFollowingDriveInformation ( TableNode $table ) : void {
$expectedDriveInformation = $table -> getRowsHash ();
// array of user drive information (Personal Drive Information Only)
2022-12-28 14:47:53 +05:45
$actualDriveInformation = $this -> featureContext -> getJsonDecodedResponse ( $this -> featureContext -> getResponse ());
if ( \is_array ( $actualDriveInformation ) && \array_key_exists ( 'drive' , $actualDriveInformation )) {
$this -> checkUserDriveInformation ( $expectedDriveInformation , $actualDriveInformation [ 'drive' ]);
} else {
throw new Error ( 'Response is not an array or the array does not consist key "drive"' );
}
2022-12-16 16:29:49 +05:45
}
2022-03-25 15:33:35 +05:45
}