Helper and context refactorings

Signed-off-by: Parajuli Kiran <kiranparajuli589@gmail.com>
This commit is contained in:
Parajuli Kiran
2022-04-12 19:35:05 +05:45
committed by saw-jan
parent 3285ec9c92
commit e39f49ef11
4 changed files with 111 additions and 186 deletions

View File

@@ -1,5 +1,5 @@
# The test runner source for API tests
CORE_COMMITID=f34b52867471c7302a8abd154fc2affafd01f997
CORE_COMMITID=cce9bd7310802b2ce2bb1d7cbe3cf36bc57b42e9
CORE_BRANCH=use-graph-helper-from-ocis
# The test runner source for UI tests

View File

@@ -17,6 +17,11 @@ use Psr\Http\Message\ResponseInterface;
* A helper class for managing users and groups using the Graph API
*/
class GraphHelper {
private static function getGraphHeaders() {
return [
'Content-Type' => 'application/json',
];
}
/**
* @param string $baseUrl
* @param string $path
@@ -89,14 +94,13 @@ class GraphHelper {
$displayName
);
$headers = ['Content-Type' => 'application/json'];
$url = self::getFullUrl($baseUrl, 'users');
return HttpRequestHelper::post(
$url,
$xRequestId,
$adminUser,
$adminPassword,
$headers,
self::getGraphHeaders(),
$payload
);
}
@@ -131,7 +135,6 @@ class GraphHelper {
$email,
$displayName
);
$headers = ['Content-Type' => 'application/json'];
$url = self::getFullUrl($baseUrl, 'users/' . $userId);
return HttpRequestHelper::sendRequest(
$url,
@@ -139,7 +142,7 @@ class GraphHelper {
"PATCH",
$adminUser,
$adminPassword,
$headers,
self::getGraphHeaders(),
$payload
);
}
@@ -167,7 +170,7 @@ class GraphHelper {
$xRequestId,
$adminUser,
$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 $xRequestId
@@ -256,12 +217,16 @@ class GraphHelper {
string $adminPassword,
string $groupName
):ResponseInterface {
return self::postPatchGroup(
$baseUrl,
$url = self::getFullUrl($baseUrl, 'groups');
$payload['displayName'] = $groupName;
return HttpRequestHelper::sendRequest(
$url,
$xRequestId,
"POST",
$adminUser,
$adminPassword,
$groupName
self::getGraphHeaders(),
\json_encode($payload)
);
}
@@ -284,13 +249,16 @@ class GraphHelper {
string $groupId,
string $displayName
):ResponseInterface {
return self::postPatchGroup(
$baseUrl,
$url = self::getFullUrl($baseUrl, 'groups/' . $groupId);
$payload['displayName'] = $displayName;
return HttpRequestHelper::sendRequest(
$url,
$xRequestId,
"PATCH",
$adminUser,
$adminPassword,
$displayName,
true
self::getGraphHeaders(),
\json_encode($payload)
);
}
@@ -301,27 +269,47 @@ class GraphHelper {
* @param string $adminPassword
*
* @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(
string $baseUrl,
string $xRequestId,
string $adminUser,
string $adminPassword
):array {
): ResponseInterface {
$url = self::getFullUrl($baseUrl, 'groups');
$response = HttpRequestHelper::get(
return HttpRequestHelper::get(
$url,
$xRequestId,
$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 $adminPassword
* @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
*/
@@ -380,7 +369,7 @@ class GraphHelper {
$xRequestId,
$adminUser,
$adminPassword,
['Content-Type' => 'application/json'],
self::getGraphHeaders(),
\json_encode($payload)
);
}
@@ -413,7 +402,7 @@ class GraphHelper {
$xRequestId,
$adminUser,
$adminPassword,
["application/json"],
self::getGraphHeaders(),
\json_encode($body)
);
}
@@ -453,7 +442,7 @@ class GraphHelper {
* @param string $adminPassword
* @param string $groupId
*
* @return bool
* @return ResponseInterface
* @throws GuzzleException
*/
public static function getMembersList(
@@ -462,7 +451,7 @@ class GraphHelper {
string $adminUser,
string $adminPassword,
string $groupId
): bool {
): ResponseInterface {
$url = self::getFullUrl($baseUrl, 'groups/' . $groupId . '/members');
return HttpRequestHelper::get(
$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 $password

View File

@@ -9,6 +9,7 @@
use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Http\Message\ResponseInterface;
use TestHelpers\GraphHelper;
use PHPUnit\Framework\Assert;
@@ -79,17 +80,7 @@ class GraphContext implements Context {
$displayName
);
$this->featureContext->setResponse($response);
$this->featureContext->theHTTPStatusCodeShouldBeSuccess();
$response = GraphHelper::getUser(
$this->featureContext->getBaseUrl(),
$this->featureContext->getStepLineRef(),
$requester,
$requesterPassword,
$userId
);
$this->featureContext->setResponse($response);
$this->featureContext->theHTTPStatusCodeShouldBeSuccess();
return $this->featureContext->getJsonDecodedResponse();
$this->featureContext->theHttpStatusCodeShouldBe(200);
}
/**
@@ -254,14 +245,14 @@ class GraphContext implements Context {
return $found;
}
/**
* @param string $user
* @param string $group
*
* @return void
* @throws JsonException
* @throws GuzzleException
*/
/**
* @param string $user
* @param string $group
*
* @return void
* @throws JsonException
* @throws GuzzleException
*/
public function userShouldNotBeMemberInGroupUsingTheGraphApi(string $user, string $group):void {
$found = $this->getUserPresenceInGroupUsingTheGraphApi($user, $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
* @throws Exception
* @throws GuzzleException
*/
public function adminHasRetrievedGroupListUsingTheGraphApi():array {
$response = GraphHelper::getGroups(
@@ -317,25 +311,10 @@ class GraphContext implements Context {
$this->featureContext->getAdminPassword()
);
if ($response->getStatusCode() === 200) {
return $this->featureContext->getJsonDecodedResponse($response);
$jsonResponseBody = $this->featureContext->getJsonDecodedResponse($response);
return $jsonResponseBody["value"];
} else {
try {
$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()
);
}
$this->throwHttpException($response, "Could not retrieve groups list.");
}
}
@@ -359,23 +338,7 @@ class GraphContext implements Context {
if ($response->getStatusCode() === 200) {
return $this->featureContext->getJsonDecodedResponse($response);
} else {
try {
$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()
);
}
$this->throwHttpException($response, "Could not retrieve members list for group $group.");
}
}
@@ -408,22 +371,7 @@ class GraphContext implements Context {
$displayName
);
if ($response->getStatusCode() !== 200) {
try {
$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()
);
}
$this->throwHttpException($response, "Could not create user $user");
} else {
return $this->featureContext->getJsonDecodedResponse($response);
}
@@ -437,7 +385,6 @@ class GraphContext implements Context {
* @param bool $checkResult
*
* @return void
* @throws JsonException
* @throws Exception
* @throws GuzzleException
*/
@@ -457,12 +404,7 @@ class GraphContext implements Context {
$groupId
);
if ($checkResult && ($result->getStatusCode() !== 204)) {
throw new Exception(
__METHOD__
. "\nCould not add user to group. "
. "\n HTTP status: " . $result->getStatusCode()
. "\n Response body: " . $result->getBody()
);
$this->throwHttpException($result, "Could not add user '$user' to group '$group'.");
}
}
@@ -486,22 +428,34 @@ class GraphContext implements Context {
if ($result->getStatusCode() === 200) {
return $this->featureContext->getJsonDecodedResponse($result);
} else {
try {
$jsonBody = $this->featureContext->getJsonDecodedResponse($result);
throw new Exception(
__METHOD__
. "\nError: failed creating group '$group'"
. "\nStatus code: " . $jsonBody['error']['code']
. "\nMessage: " . $jsonBody['error']['message']
);
} catch (TypeError $e) {
throw new Exception(
__METHOD__
. "\nError: failed creating group '$group'"
. "\nHTTP status code: " . $result->getStatusCode()
. "\nResponse body: " . $result->getBody()
);
}
$this->throwHttpException($result, "Could not create group '$group'.");
}
}
/**
* @param ResponseInterface $response
* @param string $errorMsg
*
* @return void
* @throws Exception
*/
private function throwHttpException(ResponseInterface $response, string $errorMsg) {
try {
$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()
);
}
}
}

View File

@@ -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
* and return array like:
* [1] => Array
@@ -2056,7 +2056,9 @@ class SpacesContext implements Context {
};
if ($shouldOrNot === "not") {
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");
}
}
/**