Helper and context refactorings

Signed-off-by: Parajuli Kiran <kiranparajuli589@gmail.com>
This commit is contained in:
Parajuli Kiran
2022-04-21 16:55:05 +05:45
committed by saw-jan
parent 3285ec9c92
commit e39f49ef11
4 changed files with 111 additions and 186 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
# The test runner source for API tests # The test runner source for API tests
CORE_COMMITID=f34b52867471c7302a8abd154fc2affafd01f997 CORE_COMMITID=cce9bd7310802b2ce2bb1d7cbe3cf36bc57b42e9
CORE_BRANCH=use-graph-helper-from-ocis CORE_BRANCH=use-graph-helper-from-ocis
# The test runner source for UI tests # The test runner source for UI tests
+58 -89
View File
@@ -17,6 +17,11 @@ use Psr\Http\Message\ResponseInterface;
* A helper class for managing users and groups using the Graph API * A helper class for managing users and groups using the Graph API
*/ */
class GraphHelper { class GraphHelper {
private static function getGraphHeaders() {
return [
'Content-Type' => 'application/json',
];
}
/** /**
* @param string $baseUrl * @param string $baseUrl
* @param string $path * @param string $path
@@ -89,14 +94,13 @@ class GraphHelper {
$displayName $displayName
); );
$headers = ['Content-Type' => 'application/json'];
$url = self::getFullUrl($baseUrl, 'users'); $url = self::getFullUrl($baseUrl, 'users');
return HttpRequestHelper::post( return HttpRequestHelper::post(
$url, $url,
$xRequestId, $xRequestId,
$adminUser, $adminUser,
$adminPassword, $adminPassword,
$headers, self::getGraphHeaders(),
$payload $payload
); );
} }
@@ -131,7 +135,6 @@ class GraphHelper {
$email, $email,
$displayName $displayName
); );
$headers = ['Content-Type' => 'application/json'];
$url = self::getFullUrl($baseUrl, 'users/' . $userId); $url = self::getFullUrl($baseUrl, 'users/' . $userId);
return HttpRequestHelper::sendRequest( return HttpRequestHelper::sendRequest(
$url, $url,
@@ -139,7 +142,7 @@ class GraphHelper {
"PATCH", "PATCH",
$adminUser, $adminUser,
$adminPassword, $adminPassword,
$headers, self::getGraphHeaders(),
$payload $payload
); );
} }
@@ -167,7 +170,7 @@ class GraphHelper {
$xRequestId, $xRequestId,
$adminUser, $adminUser,
$adminPassword, $adminPassword,
["Content-Type" => "application/json"] self::getGraphHeaders()
); );
} }
@@ -197,48 +200,6 @@ class GraphHelper {
); );
} }
/**
* can send a request to the graph api to:
* - create a group
* - update a group
*
* displayName is the only field that can be assigned/updated
*
* @param string $baseUrl
* @param string $xRequestId
* @param string $adminUser
* @param string $adminPassword
* @param string $groupName - the displayName of the group
* @param bool|null $update
*
* @return ResponseInterface
* @throws GuzzleException
*/
private static function postPatchGroup(
string $baseUrl,
string $xRequestId,
string $adminUser,
string $adminPassword,
string $groupName,
?bool $update = false
): ResponseInterface {
$url = ($update)
? self::getFullUrl($baseUrl, 'groups/' . $groupName)
: self::getFullUrl($baseUrl, 'groups');
$method = ($update) ? 'PATCH' : 'POST';
$headers = ['Content-Type' => 'application/json'];
$payload['displayName'] = $groupName;
return HttpRequestHelper::sendRequest(
$url,
$xRequestId,
$method,
$adminUser,
$adminPassword,
$headers,
\json_encode($payload)
);
}
/** /**
* @param string $baseUrl * @param string $baseUrl
* @param string $xRequestId * @param string $xRequestId
@@ -256,12 +217,16 @@ class GraphHelper {
string $adminPassword, string $adminPassword,
string $groupName string $groupName
):ResponseInterface { ):ResponseInterface {
return self::postPatchGroup( $url = self::getFullUrl($baseUrl, 'groups');
$baseUrl, $payload['displayName'] = $groupName;
return HttpRequestHelper::sendRequest(
$url,
$xRequestId, $xRequestId,
"POST",
$adminUser, $adminUser,
$adminPassword, $adminPassword,
$groupName self::getGraphHeaders(),
\json_encode($payload)
); );
} }
@@ -284,13 +249,16 @@ class GraphHelper {
string $groupId, string $groupId,
string $displayName string $displayName
):ResponseInterface { ):ResponseInterface {
return self::postPatchGroup( $url = self::getFullUrl($baseUrl, 'groups/' . $groupId);
$baseUrl, $payload['displayName'] = $displayName;
return HttpRequestHelper::sendRequest(
$url,
$xRequestId, $xRequestId,
"PATCH",
$adminUser, $adminUser,
$adminPassword, $adminPassword,
$displayName, self::getGraphHeaders(),
true \json_encode($payload)
); );
} }
@@ -301,27 +269,47 @@ class GraphHelper {
* @param string $adminPassword * @param string $adminPassword
* *
* @return array * @return array
* @throws Exception * @throws GuzzleException
*/
public static function getUsers(
string $baseUrl,
string $xRequestId,
string $adminUser,
string $adminPassword
):array {
$url = self::getFullUrl($baseUrl, 'users');
return HttpRequestHelper::get(
$url,
$xRequestId,
$adminUser,
$adminPassword,
self::getGraphHeaders(),
);
}
/**
* @param string $baseUrl
* @param string $xRequestId
* @param string $adminUser
* @param string $adminPassword
*
* @return ResponseInterface
* @throws GuzzleException
*/ */
public static function getGroups( public static function getGroups(
string $baseUrl, string $baseUrl,
string $xRequestId, string $xRequestId,
string $adminUser, string $adminUser,
string $adminPassword string $adminPassword
):array { ): ResponseInterface {
$url = self::getFullUrl($baseUrl, 'groups'); $url = self::getFullUrl($baseUrl, 'groups');
$response = HttpRequestHelper::get( return HttpRequestHelper::get(
$url, $url,
$xRequestId, $xRequestId,
$adminUser, $adminUser,
$adminPassword $adminPassword,
self::getGraphHeaders(),
); );
$groupsListEncoded = \json_decode($response->getBody()->getContents(), true);
if (!isset($groupsListEncoded['value'])) {
throw new Exception('No groups found');
} else {
return $groupsListEncoded['value'];
}
} }
/** /**
@@ -356,7 +344,8 @@ class GraphHelper {
* @param string $adminUser * @param string $adminUser
* @param string $adminPassword * @param string $adminPassword
* @param string $groupId * @param string $groupId
* @param array $users expects users array with user ids [ [ 'id' => 'some_id' ], ] * @param array $users expects users array with user ids
* [ [ 'id' => 'some_id' ], ]
* *
* @return ResponseInterface * @return ResponseInterface
*/ */
@@ -380,7 +369,7 @@ class GraphHelper {
$xRequestId, $xRequestId,
$adminUser, $adminUser,
$adminPassword, $adminPassword,
['Content-Type' => 'application/json'], self::getGraphHeaders(),
\json_encode($payload) \json_encode($payload)
); );
} }
@@ -413,7 +402,7 @@ class GraphHelper {
$xRequestId, $xRequestId,
$adminUser, $adminUser,
$adminPassword, $adminPassword,
["application/json"], self::getGraphHeaders(),
\json_encode($body) \json_encode($body)
); );
} }
@@ -453,7 +442,7 @@ class GraphHelper {
* @param string $adminPassword * @param string $adminPassword
* @param string $groupId * @param string $groupId
* *
* @return bool * @return ResponseInterface
* @throws GuzzleException * @throws GuzzleException
*/ */
public static function getMembersList( public static function getMembersList(
@@ -462,7 +451,7 @@ class GraphHelper {
string $adminUser, string $adminUser,
string $adminPassword, string $adminPassword,
string $groupId string $groupId
): bool { ): ResponseInterface {
$url = self::getFullUrl($baseUrl, 'groups/' . $groupId . '/members'); $url = self::getFullUrl($baseUrl, 'groups/' . $groupId . '/members');
return HttpRequestHelper::get( return HttpRequestHelper::get(
$url, $url,
@@ -472,26 +461,6 @@ class GraphHelper {
); );
} }
/**
* @param string $baseUrl
* @param string $xRequestId
* @param string $adminUser
* @param string $adminPassword
* @param string $userId
*
* @return void
*/
public static function getGroupListOfAUser(
string $baseUrl,
string $xRequestId,
string $adminUser,
string $adminPassword,
string $userId
) {
// TODO: endpoint not available https://github.com/owncloud/ocis/issues/3363
// Not implemented yet
}
/** /**
* @param string|null $userName * @param string|null $userName
* @param string|null $password * @param string|null $password
@@ -9,6 +9,7 @@
use Behat\Behat\Context\Context; use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\BeforeScenarioScope; use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Exception\GuzzleException;
use Psr\Http\Message\ResponseInterface;
use TestHelpers\GraphHelper; use TestHelpers\GraphHelper;
use PHPUnit\Framework\Assert; use PHPUnit\Framework\Assert;
@@ -79,17 +80,7 @@ class GraphContext implements Context {
$displayName $displayName
); );
$this->featureContext->setResponse($response); $this->featureContext->setResponse($response);
$this->featureContext->theHTTPStatusCodeShouldBeSuccess(); $this->featureContext->theHttpStatusCodeShouldBe(200);
$response = GraphHelper::getUser(
$this->featureContext->getBaseUrl(),
$this->featureContext->getStepLineRef(),
$requester,
$requesterPassword,
$userId
);
$this->featureContext->setResponse($response);
$this->featureContext->theHTTPStatusCodeShouldBeSuccess();
return $this->featureContext->getJsonDecodedResponse();
} }
/** /**
@@ -254,14 +245,14 @@ class GraphContext implements Context {
return $found; return $found;
} }
/** /**
* @param string $user * @param string $user
* @param string $group * @param string $group
* *
* @return void * @return void
* @throws JsonException * @throws JsonException
* @throws GuzzleException * @throws GuzzleException
*/ */
public function userShouldNotBeMemberInGroupUsingTheGraphApi(string $user, string $group):void { public function userShouldNotBeMemberInGroupUsingTheGraphApi(string $user, string $group):void {
$found = $this->getUserPresenceInGroupUsingTheGraphApi($user, $group); $found = $this->getUserPresenceInGroupUsingTheGraphApi($user, $group);
Assert::assertFalse($found, __METHOD__ . " User $user is member of group $group"); Assert::assertFalse($found, __METHOD__ . " User $user is member of group $group");
@@ -306,8 +297,11 @@ class GraphContext implements Context {
} }
/** /**
* returns list of all groups
*
* @return array * @return array
* @throws Exception * @throws Exception
* @throws GuzzleException
*/ */
public function adminHasRetrievedGroupListUsingTheGraphApi():array { public function adminHasRetrievedGroupListUsingTheGraphApi():array {
$response = GraphHelper::getGroups( $response = GraphHelper::getGroups(
@@ -317,25 +311,10 @@ class GraphContext implements Context {
$this->featureContext->getAdminPassword() $this->featureContext->getAdminPassword()
); );
if ($response->getStatusCode() === 200) { if ($response->getStatusCode() === 200) {
return $this->featureContext->getJsonDecodedResponse($response); $jsonResponseBody = $this->featureContext->getJsonDecodedResponse($response);
return $jsonResponseBody["value"];
} else { } else {
try { $this->throwHttpException($response, "Could not retrieve groups list.");
$jsonBody = $this->featureContext->getJsonDecodedResponse($response);
throw new Exception(
__METHOD__
. "\nCould not retrieve groups list."
. "\nHTTP status code: " . $response->getStatusCode()
. "\nError code: " . $jsonBody["error"]["code"]
. "\nMessage: " . $jsonBody["error"]["message"]
);
} catch (TypeError $e) {
throw new Exception(
__METHOD__
. "\nCould not retrieve groups list."
. "\nHTTP status code: " . $response->getStatusCode()
. "\nResponse body: " . $response->getBody()
);
}
} }
} }
@@ -359,23 +338,7 @@ class GraphContext implements Context {
if ($response->getStatusCode() === 200) { if ($response->getStatusCode() === 200) {
return $this->featureContext->getJsonDecodedResponse($response); return $this->featureContext->getJsonDecodedResponse($response);
} else { } else {
try { $this->throwHttpException($response, "Could not retrieve members list for group $group.");
$jsonBody = $this->featureContext->getJsonDecodedResponse($response);
throw new Exception(
__METHOD__
. "\nCould not retrieve members list for group $group."
. "\nHTTP status code: " . $response->getStatusCode()
. "\nError code: " . $jsonBody["error"]["code"]
. "\nMessage: " . $jsonBody["error"]["message"]
);
} catch (TypeError $e) {
throw new Exception(
__METHOD__
. "\nCould not retrieve members list for group $group."
. "\nHTTP status code: " . $response->getStatusCode()
. "\nResponse body: " . $response->getBody()
);
}
} }
} }
@@ -408,22 +371,7 @@ class GraphContext implements Context {
$displayName $displayName
); );
if ($response->getStatusCode() !== 200) { if ($response->getStatusCode() !== 200) {
try { $this->throwHttpException($response, "Could not create user $user");
$jsonResponseBody = $this->featureContext->getJsonDecodedResponse($response);
throw new Exception(
__METHOD__
. "\nCould not create user $user"
. "\nError code: {$jsonResponseBody['error']['code']}"
. "\nError message: {$jsonResponseBody['error']['message']}"
);
} catch (TypeError $e) {
throw new Exception(
__METHOD__
. "\nCould not create user $user"
. "\nHTTP status code: " . $response->getStatusCode()
. "\nResponse body: " . $response->getBody()
);
}
} else { } else {
return $this->featureContext->getJsonDecodedResponse($response); return $this->featureContext->getJsonDecodedResponse($response);
} }
@@ -437,7 +385,6 @@ class GraphContext implements Context {
* @param bool $checkResult * @param bool $checkResult
* *
* @return void * @return void
* @throws JsonException
* @throws Exception * @throws Exception
* @throws GuzzleException * @throws GuzzleException
*/ */
@@ -457,12 +404,7 @@ class GraphContext implements Context {
$groupId $groupId
); );
if ($checkResult && ($result->getStatusCode() !== 204)) { if ($checkResult && ($result->getStatusCode() !== 204)) {
throw new Exception( $this->throwHttpException($result, "Could not add user '$user' to group '$group'.");
__METHOD__
. "\nCould not add user to group. "
. "\n HTTP status: " . $result->getStatusCode()
. "\n Response body: " . $result->getBody()
);
} }
} }
@@ -486,22 +428,34 @@ class GraphContext implements Context {
if ($result->getStatusCode() === 200) { if ($result->getStatusCode() === 200) {
return $this->featureContext->getJsonDecodedResponse($result); return $this->featureContext->getJsonDecodedResponse($result);
} else { } else {
try { $this->throwHttpException($result, "Could not create group '$group'.");
$jsonBody = $this->featureContext->getJsonDecodedResponse($result); }
throw new Exception( }
__METHOD__
. "\nError: failed creating group '$group'" /**
. "\nStatus code: " . $jsonBody['error']['code'] * @param ResponseInterface $response
. "\nMessage: " . $jsonBody['error']['message'] * @param string $errorMsg
); *
} catch (TypeError $e) { * @return void
throw new Exception( * @throws Exception
__METHOD__ */
. "\nError: failed creating group '$group'" private function throwHttpException(ResponseInterface $response, string $errorMsg) {
. "\nHTTP status code: " . $result->getStatusCode() try {
. "\nResponse body: " . $result->getBody() $jsonBody = $this->featureContext->getJsonDecodedResponse($response);
); throw new Exception(
} __METHOD__
. "\n$errorMsg"
. "\nHTTP status code: " . $response->getStatusCode()
. "\nError code: " . $jsonBody["error"]["code"]
. "\nMessage: " . $jsonBody["error"]["message"]
);
} catch (TypeError $e) {
throw new Exception(
__METHOD__
. "\n$errorMsg"
. "\nHTTP status code: " . $response->getStatusCode()
. "\nResponse body: " . $response->getBody()
);
} }
} }
} }
@@ -1997,8 +1997,8 @@ class SpacesContext implements Context {
} }
/** /**
* User get all objects in the trash of project space * User get all objects in the trash of project space
* *
* method "getTrashbinContentFromResponseXml" borrowed from core repository * method "getTrashbinContentFromResponseXml" borrowed from core repository
* and return array like: * and return array like:
* [1] => Array * [1] => Array
@@ -2056,7 +2056,9 @@ class SpacesContext implements Context {
}; };
if ($shouldOrNot === "not") { if ($shouldOrNot === "not") {
Assert::assertEmpty($expectedObject, "$object is found in the trash, but should not be there"); Assert::assertEmpty($expectedObject, "$object is found in the trash, but should not be there");
} else Assert::assertNotEmpty($expectedObject, "$object is not found in the trash"); } else {
Assert::assertNotEmpty($expectedObject, "$object is not found in the trash");
}
} }
/** /**