test: add settings helper

test: move methods to helper
This commit is contained in:
Saw-jan
2024-07-03 12:16:06 +05:45
parent 18d0eaa5c2
commit a8ee23734c
7 changed files with 353 additions and 136 deletions

View File

@@ -685,4 +685,15 @@ class HttpRequestHelper {
$timeout = \getenv("REQUEST_TIMEOUT");
return (int)$timeout ?: 60;
}
/**
* returns json decoded body content of a json response as an object
*
* @param ResponseInterface $response
*
* @return mixed
*/
public static function getJsonDecodedResponseBodyContent(ResponseInterface $response): mixed {
return json_decode($response->getBody()->getContents(), null, 512, JSON_THROW_ON_ERROR);
}
}

View File

@@ -0,0 +1,281 @@
<?php declare(strict_types=1);
/**
* ownCloud
*
* @author Sajan Gurung <sajan@jankaritech.com>
* @copyright Copyright (c) 2024 Sajan Gurung sajan@jankaritech.com
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License,
* as published by the Free Software Foundation;
* either version 3 of the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace TestHelpers;
use TestHelpers\HttpRequestHelper;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Http\Message\ResponseInterface;
use PHPUnit\Framework\Assert;
/**
* A helper class for ocis settings
*/
class SettingsHelper {
private static string $settingsEndpoint = '/api/v0/settings/';
/**
* @param string $baseUrl
* @param string $path
*
* @return string
*/
public static function buildFullUrl(string $baseUrl, string $path): string {
return $baseUrl . self::$settingsEndpoint . $path;
}
/**
* @param string $baseUrl
* @param string $user
* @param string $password
* @param string $xRequestId
* @param array $headers
*
* @return ResponseInterface
*
* @throws GuzzleException
* @throws Exception
*/
public static function getBundlesList(string $baseUrl, string $user, string $password, string $xRequestId, array $headers = []): ResponseInterface {
$fullUrl = self::buildFullUrl($baseUrl, "bundles-list");
return HttpRequestHelper::post(
$fullUrl,
$xRequestId,
$user,
$password,
$headers,
"{}"
);
}
/**
* @param string $baseUrl
* @param string $user
* @param string $password
* @param string $bundleName
* @param string $xRequestId
*
* @return array
*
* @throws GuzzleException
* @throws Exception
*/
public static function getBundleByName(string $baseUrl, string $user, string $password, string $bundleName, string $xRequestId): array {
$response = self::getBundlesList($baseUrl, $user, $password, $xRequestId);
Assert::assertEquals(201, $response->getStatusCode(), "Failed to get bundles list");
$bundlesList = HttpRequestHelper::getJsonDecodedResponseBodyContent($response);
foreach ($bundlesList->bundles as $value) {
if ($value->displayName === $bundleName) {
return json_decode(json_encode($value), true);
}
}
return [];
}
/**
* @param string $baseUrl
* @param string $user
* @param string $password
* @param string $xRequestId
* @param array $headers
*
* @return ResponseInterface
*
* @throws GuzzleException
* @throws Exception
*/
public static function getRolesList(string $baseUrl, string $user, string $password, string $xRequestId, array $headers = []): ResponseInterface {
$fullUrl = self::buildFullUrl($baseUrl, "roles-list");
return HttpRequestHelper::post(
$fullUrl,
$xRequestId,
$user,
$password,
$headers,
"{}"
);
}
/**
* @param string $baseUrl
* @param string $user
* @param string $password
* @param string $assigneeId
* @param string $roleId
* @param string $xRequestId
* @param array $headers
*
* @return ResponseInterface
*
* @throws GuzzleException
* @throws Exception
*/
public static function assignRoleToUser(string $baseUrl, string $user, string $password, string $assigneeId, string $roleId, string $xRequestId, array $headers = []): ResponseInterface {
$fullUrl = self::buildFullUrl($baseUrl, "assignments-add");
$body = json_encode(["account_uuid" => $assigneeId, "role_id" => $roleId], JSON_THROW_ON_ERROR);
return HttpRequestHelper::post(
$fullUrl,
$xRequestId,
$user,
$password,
$headers,
$body
);
}
/**
* @param string $baseUrl
* @param string $user
* @param string $password
* @param string $userId
* @param string $xRequestId
* @param array $headers
*
* @return ResponseInterface
*
* @throws GuzzleException
* @throws Exception
*/
public static function getAssignmentsList(string $baseUrl, string $user, string $password, string $userId, string $xRequestId, array $headers = []): ResponseInterface {
$fullUrl = self::buildFullUrl($baseUrl, "assignments-list");
$body = json_encode(["account_uuid" => $userId], JSON_THROW_ON_ERROR);
return HttpRequestHelper::post(
$fullUrl,
$xRequestId,
$user,
$password,
$headers,
$body
);
}
/**
* @param string $baseUrl
* @param string $user
* @param string $password
* @param string $xRequestId
* @param array $headers
*
* @return ResponseInterface
*
* @throws GuzzleException
* @throws Exception
*/
public static function getValuesList(string $baseUrl, string $user, string $password, string $xRequestId, array $headers = []): ResponseInterface {
$fullUrl = self::buildFullUrl($baseUrl, "values-list");
$body = json_encode(["account_uuid" => "me"], JSON_THROW_ON_ERROR);
return HttpRequestHelper::post(
$fullUrl,
$xRequestId,
$user,
$password,
$headers,
$body
);
}
/**
* @param string $baseUrl
* @param string $user
* @param string $password
* @param string $xRequestId
*
* @return bool
*
* @throws GuzzleException
* @throws Exception
*/
public static function getAutoAcceptSharesSettingValue(string $baseUrl, string $user, string $password, string $xRequestId): bool {
$response = self::getValuesList($baseUrl, $user, $password, $xRequestId);
Assert::assertEquals(201, $response->getStatusCode(), "Failed to get values list");
$valuesList = HttpRequestHelper::getJsonDecodedResponseBodyContent($response);
if (empty($valuesList)) {
return true;
}
foreach ($valuesList->values as $value) {
if ($value->identifier->setting === "auto-accept-shares") {
return $value->value->boolValue;
}
}
return true;
}
/**
* @param string $baseUrl
* @param string $user
* @param string $password
* @param string $xRequestId
*
* @return string
*
* @throws GuzzleException
* @throws Exception
*/
public static function getLanguageSettingValue(string $baseUrl, string $user, string $password, string $xRequestId): string {
$response = self::getValuesList($baseUrl, $user, $password, $xRequestId);
Assert::assertEquals(201, $response->getStatusCode(), "Failed to get values list");
$valuesList = HttpRequestHelper::getJsonDecodedResponseBodyContent($response);
// if no language is set, the request body is empty return English as the default language
if (empty($valuesList)) {
return "en";
}
foreach ($valuesList->values as $value) {
if ($value->identifier->setting === "language") {
return $value->value->listValue->values[0]->stringValue;
}
}
// if a language setting was still not found, return English
return "en";
}
/**
* @param string $baseUrl
* @param string $user
* @param string $password
* @param string $body
* @param string $xRequestId
* @param array $headers
*
* @return ResponseInterface
*
* @throws GuzzleException
* @throws Exception
*/
public static function updateSettings(string $baseUrl, string $user, string $password, string $body, string $xRequestId, array $headers = []): ResponseInterface {
$fullUrl = self::buildFullUrl($baseUrl, "values-save");
return HttpRequestHelper::post(
$fullUrl,
$xRequestId,
$user,
$password,
$headers,
$body
);
}
}

View File

@@ -42,6 +42,7 @@ use TestHelpers\HttpLogger;
use TestHelpers\OcisHelper;
use TestHelpers\GraphHelper;
use TestHelpers\WebDavHelper;
use TestHelpers\SettingsHelper;
require_once 'bootstrap.php';
@@ -181,7 +182,12 @@ class FeatureContext extends BehatVariablesContext {
if (\array_key_exists($user, $this->autoSyncSettings)) {
return $this->autoSyncSettings[$user];
}
$autoSyncSetting = $this->settingsContext->getAutoAcceptSharesSettingValue($user);
$autoSyncSetting = SettingsHelper::getAutoAcceptSharesSettingValue(
$this->baseUrl,
$user,
$this->getPasswordForUser($user),
$this->getStepLineRef()
);
$this->autoSyncSettings[$user] = $autoSyncSetting;
return $autoSyncSetting;
@@ -197,11 +203,6 @@ class FeatureContext extends BehatVariablesContext {
$this->autoSyncSettings[$user] = $value;
}
/**
* this is set true for db conversion tests
*/
private bool $dbConversion = false;
public const SHARES_SPACE_ID = 'a0ca6a90-a365-4782-871e-d44447bbc668$a0ca6a90-a365-4782-871e-d44447bbc668';
private bool $useSharingNG = false;
@@ -1472,7 +1473,7 @@ class FeatureContext extends BehatVariablesContext {
public function getJsonDecodedResponseBodyContent(ResponseInterface $response = null): mixed {
$response = $response ?? $this->response;
$response->getBody()->rewind();
return json_decode($response->getBody()->getContents(), null, 512, JSON_THROW_ON_ERROR);
return HttpRequestHelper::getJsonDecodedResponseBodyContent($response);
}
/**
@@ -2439,14 +2440,12 @@ class FeatureContext extends BehatVariablesContext {
$this->ocsContext = new OCSContext();
$this->authContext = new AuthContext();
$this->tusContext = new TUSContext();
$this->settingsContext = new SettingsContext();
$this->ocsContext->before($scope);
$this->authContext->setUpScenario($scope);
$this->tusContext->setUpScenario($scope);
$environment->registerContext($this->ocsContext);
$environment->registerContext($this->authContext);
$environment->registerContext($this->tusContext);
$environment->registerContext($this->settingsContext);
$scenarioLine = $scope->getScenario()->getLine();
$featureFile = $scope->getFeature()->getFile();
$suiteName = $scope->getSuite()->getName();

View File

@@ -2517,9 +2517,9 @@ class GraphContext implements Context {
$credentials['password']
);
$jsonObj = $this->featureContext->getJsonDecodedResponseBodyContent($response);
$jsonBody = $this->featureContext->getJsonDecodedResponseBodyContent($response);
foreach ($jsonObj->value as $share) {
foreach ($jsonBody->value as $share) {
$autoSync = $this->featureContext->getUserAutoSyncSetting($credentials['username']);
$tryAgain = !$share->{'@client.synchronize'} && $autoSync && $retried < HttpRequestHelper::numRetriesOnHttpTooEarly();

View File

@@ -13,6 +13,7 @@ use Behat\Gherkin\Node\PyStringNode;
use TestHelpers\EmailHelper;
use PHPUnit\Framework\Assert;
use TestHelpers\GraphHelper;
use TestHelpers\SettingsHelper;
use Behat\Gherkin\Node\TableNode;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Http\Message\ResponseInterface;
@@ -109,7 +110,13 @@ class NotificationContext implements Context {
*/
public function listAllNotifications(string $user):ResponseInterface {
$this->setUserRecipient($user);
$headers = ["accept-language" => $this->settingsContext->getSettingLanguageValue($user)];
$language = SettingsHelper::getLanguageSettingValue(
$this->featureContext->getBaseUrl(),
$this->featureContext->getActualUsername($user),
$this->featureContext->getPasswordForUser($user),
$this->featureContext->getStepLineRef()
);
$headers = ["accept-language" => $language];
return OcsApiHelper::sendRequest(
$this->featureContext->getBaseUrl(),
$this->featureContext->getActualUsername($user),

View File

@@ -553,7 +553,12 @@ class OCSContext implements Context {
* @throws Exception
*/
public function getOCSResponseStatusCode(ResponseInterface $response):string {
$jsonResponse = $this->featureContext->getJsonDecodedResponseBodyContent($response);
try {
$jsonResponse = $this->featureContext->getJsonDecodedResponseBodyContent($response);
} catch (JsonException $e) {
$jsonResponse = null;
}
if (\is_object($jsonResponse) && $jsonResponse->ocs->meta->statuscode) {
return (string) $jsonResponse->ocs->meta->statuscode;
}

View File

@@ -15,6 +15,7 @@ use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use PHPUnit\Framework\Assert;
use Psr\Http\Message\ResponseInterface;
use TestHelpers\HttpRequestHelper;
use TestHelpers\SettingsHelper;
use Behat\Gherkin\Node\TableNode;
require_once 'bootstrap.php';
@@ -54,14 +55,11 @@ class SettingsContext implements Context {
* @throws Exception
*/
public function getRoles(string $user): ResponseInterface {
$fullUrl = $this->baseUrl . $this->settingsUrl . "roles-list";
return HttpRequestHelper::post(
$fullUrl,
$this->featureContext->getStepLineRef(),
return SettingsHelper::getRolesList(
$this->baseUrl,
$user,
$this->featureContext->getPasswordForUser($user),
null,
"{}"
$this->featureContext->getStepLineRef()
);
}
@@ -91,15 +89,13 @@ class SettingsContext implements Context {
* @throws Exception
*/
public function assignRoleToUser(string $user, string $userId, string $roleId): ResponseInterface {
$fullUrl = $this->baseUrl . $this->settingsUrl . "assignments-add";
$body = json_encode(["account_uuid" => $userId, "role_id" => $roleId], JSON_THROW_ON_ERROR);
return HttpRequestHelper::post(
$fullUrl,
$this->featureContext->getStepLineRef(),
return SettingsHelper::assignRoleToUser(
$this->baseUrl,
$user,
$this->featureContext->getPasswordForUser($user),
null,
$body
$userId,
$roleId,
$this->featureContext->getStepLineRef(),
);
}
@@ -107,21 +103,18 @@ class SettingsContext implements Context {
* @param string $user
* @param string $userId
*
* @return void
* @return ResponseInterface
*
* @throws GuzzleException
* @throws Exception
*/
public function getAssignmentsList(string $user, string $userId): ResponseInterface {
$fullUrl = $this->baseUrl . $this->settingsUrl . "assignments-list";
$body = json_encode(["account_uuid" => $userId], JSON_THROW_ON_ERROR);
return HttpRequestHelper::post(
$fullUrl,
$this->featureContext->getStepLineRef(),
return SettingsHelper::getAssignmentsList(
$this->baseUrl,
$user,
$this->featureContext->getPasswordForUser($user),
null,
$body
$userId,
$this->featureContext->getStepLineRef(),
);
}
@@ -300,14 +293,11 @@ class SettingsContext implements Context {
* @throws Exception
*/
public function sendRequestGetBundlesList(string $user): ResponseInterface {
$fullUrl = $this->baseUrl . $this->settingsUrl . "bundles-list";
return HttpRequestHelper::post(
$fullUrl,
$this->featureContext->getStepLineRef(),
return SettingsHelper::getBundlesList(
$this->baseUrl,
$user,
$this->featureContext->getPasswordForUser($user),
null,
"{}"
$this->featureContext->getStepLineRef(),
);
}
@@ -320,21 +310,14 @@ class SettingsContext implements Context {
* @throws GuzzleException
* @throws Exception
*/
public function getBundlesList(string $user, string $bundleName): array {
$response = $this->sendRequestGetBundlesList($user);
$this->featureContext->theHTTPStatusCodeShouldBe(
201,
"Expected response status code should be 201",
$response
public function getBundleByName(string $user, string $bundleName): array {
return SettingsHelper::getBundleByName(
$this->baseUrl,
$user,
$this->featureContext->getPasswordForUser($user),
$bundleName,
$this->featureContext->getStepLineRef()
);
$body = json_decode((string)$response->getBody(), true, 512, JSON_THROW_ON_ERROR);
foreach ($body["bundles"] as $value) {
if ($value["displayName"] === $bundleName) {
return $value;
}
}
return [];
}
/**
@@ -347,49 +330,15 @@ class SettingsContext implements Context {
* @throws Exception
*/
public function sendRequestGetSettingsValuesList(string $user, array $headers = null): ResponseInterface {
$fullUrl = $this->baseUrl . $this->settingsUrl . "values-list";
$body = json_encode(["account_uuid" => "me"], JSON_THROW_ON_ERROR);
return HttpRequestHelper::post(
$fullUrl,
$this->featureContext->getStepLineRef(),
return SettingsHelper::getValuesList(
$this->baseUrl,
$user,
$this->featureContext->getPasswordForUser($user),
$headers,
$body
$this->featureContext->getStepLineRef(),
$headers
);
}
/**
* @param string $user
*
* @return bool
*
* @throws GuzzleException
* @throws Exception
*/
public function getAutoAcceptSharesSettingValue(string $user): bool {
$response = $this->sendRequestGetSettingsValuesList($user);
$this->featureContext->theHTTPStatusCodeShouldBe(
201,
"Expected response status code should be 201",
$response
);
$body = $this->featureContext->getJsonDecodedResponseBodyContent($response);
if (empty($body)) {
return true;
}
foreach ($body->values as $value) {
if ($value->identifier->setting === "auto-accept-shares") {
return $value->value->boolValue;
}
}
return true;
}
/**
* @When /^user "([^"]*)" lists values-list with headers using the Settings API$/
*
@@ -413,37 +362,6 @@ class SettingsContext implements Context {
$this->featureContext->setResponse($this->sendRequestGetSettingsValuesList($user, $headers));
}
/**
* @param string $user
*
* @return string
*
* @throws GuzzleException
* @throws Exception
*/
public function getSettingLanguageValue(string $user): string {
$response = $this->sendRequestGetSettingsValuesList($user);
$this->featureContext->theHTTPStatusCodeShouldBe(
201,
"Expected response status code should be 201",
$response
);
$body = json_decode((string)$response->getBody(), true, 512, JSON_THROW_ON_ERROR);
// if no language is set, the request body is empty return English as the default language
if (empty($body)) {
return "en";
}
foreach ($body["values"] as $value) {
if ($value["identifier"]["setting"] === "language") {
return $value["value"]["listValue"]["values"][0]["stringValue"];
}
}
// if a language setting was still not found, return English
return "en";
}
/**
* @param string $user
* @param string $language
@@ -454,7 +372,7 @@ class SettingsContext implements Context {
* @throws Exception
*/
public function sendRequestToSwitchSystemLanguage(string $user, string $language): ResponseInterface {
$profileBundlesList = $this->getBundlesList($user, "Profile");
$profileBundlesList = $this->getBundleByName($user, "Profile");
Assert::assertNotEmpty($profileBundlesList, "bundles list is empty");
$settingId = '';
@@ -466,7 +384,6 @@ class SettingsContext implements Context {
}
Assert::assertNotEmpty($settingId, "settingId is empty");
$fullUrl = $this->baseUrl . $this->settingsUrl . "values-save";
$userId = $this->featureContext->getAttributeOfCreatedUser($user, 'id');
$body = json_encode(
[
@@ -489,13 +406,12 @@ class SettingsContext implements Context {
],
JSON_THROW_ON_ERROR
);
return HttpRequestHelper::post(
$fullUrl,
$this->featureContext->getStepLineRef(),
return SettingsHelper::updateSettings(
$this->baseUrl,
$user,
$this->featureContext->getPasswordForUser($user),
null,
$body
$body,
$this->featureContext->getStepLineRef()
);
}
@@ -528,7 +444,6 @@ class SettingsContext implements Context {
* @throws Exception
*/
public function sendRequestToDisableAutoAccepting(string $user): ResponseInterface {
$fullUrl = $this->baseUrl . $this->settingsUrl . "values-save";
$body = json_encode(
[
"value" => [
@@ -544,13 +459,12 @@ class SettingsContext implements Context {
JSON_THROW_ON_ERROR
);
return HttpRequestHelper::post(
$fullUrl,
$this->featureContext->getStepLineRef(),
return SettingsHelper::updateSettings(
$this->baseUrl,
$user,
$this->featureContext->getPasswordForUser($user),
[],
$body
$body,
$this->featureContext->getStepLineRef()
);
}