Json drill for coreApiCapabilities Suite (#5672)

Refactor acording to core

Signed-off-by: sagargurung1001@gmail.com <sagargurung1001@gmail.com>

Review Address

Co-authored-by: sagargurung1001@gmail.com <sagargurung1001@gmail.com>
This commit is contained in:
Sawjan Gurung
2023-02-28 15:13:36 +05:45
committed by GitHub
co-authored by sagargurung1001@gmail.com <sagargurung1001@gmail.com>
parent 5902679277
commit 0c4f53a03e
8 changed files with 611 additions and 171 deletions
@@ -132,7 +132,21 @@ class AppConfigurationContext implements Context {
* @throws GuzzleException
* @throws JsonException
*/
public function userGetsCapabilities(string $username):void {
public function userRetrievesCapabilities(string $username): void {
$user = $this->featureContext->getActualUsername($username);
$this->userGetsCapabilities($user, true);
}
/**
*
* @param string $username
* @param boolean $formatJson // if true then formats the response in json
*
* @return void
* @throws GuzzleException
* @throws JsonException
*/
public function userGetsCapabilities(string $username, ?bool $formatJson = false):void {
$user = $this->featureContext->getActualUsername($username);
$password = $this->featureContext->getPasswordForUser($user);
$this->featureContext->setResponse(
@@ -141,7 +155,7 @@ class AppConfigurationContext implements Context {
$user,
$password,
'GET',
'/cloud/capabilities',
'/cloud/capabilities' . ($formatJson ? '?format=json' : ''),
$this->featureContext->getStepLineRef(),
[],
$this->featureContext->getOcsApiVersion()
@@ -214,7 +228,8 @@ class AppConfigurationContext implements Context {
* @return void
*/
public function theAdministratorGetsCapabilities():void {
$this->userGetsCapabilities($this->getAdminUsernameForCapabilitiesCheck());
$user = $this->getAdminUsernameForCapabilitiesCheck();
$this->userGetsCapabilities($user, true);
}
/**
@@ -39,72 +39,6 @@ class CapabilitiesContext implements Context {
*/
private $featureContext;
/**
* @Then the capabilities should contain
*
* @param TableNode|null $formData
*
* @return void
* @throws Exception
*/
public function checkCapabilitiesResponse(TableNode $formData):void {
$capabilitiesXML = $this->featureContext->appConfigurationContext->getCapabilitiesXml(__METHOD__);
$assertedSomething = false;
$this->featureContext->verifyTableNodeColumns($formData, ['value', 'path_to_element', 'capability']);
foreach ($formData->getHash() as $row) {
$row['value'] = $this->featureContext->substituteInLineCodes($row['value']);
Assert::assertEquals(
$row['value'] === "EMPTY" ? '' : $row['value'],
$this->featureContext->appConfigurationContext->getParameterValueFromXml(
$capabilitiesXML,
$row['capability'],
$row['path_to_element']
),
"Failed field {$row['capability']} {$row['path_to_element']}"
);
$assertedSomething = true;
}
Assert::assertTrue(
$assertedSomething,
'there was nothing in the table of expected capabilities'
);
}
/**
* @Then the version data in the response should contain
*
* @param TableNode|null $formData
*
* @return void
* @throws Exception
*/
public function checkVersionResponse(TableNode $formData):void {
$versionXML = $this->featureContext->appConfigurationContext->getVersionXml(__METHOD__);
$assertedSomething = false;
$this->featureContext->verifyTableNodeColumns($formData, ['name', 'value']);
foreach ($formData->getHash() as $row) {
$row['value'] = $this->featureContext->substituteInLineCodes($row['value']);
$actualValue = $versionXML->{$row['name']};
Assert::assertEquals(
$row['value'] === "EMPTY" ? '' : $row['value'],
$actualValue,
"Failed field {$row['name']}"
);
$assertedSomething = true;
}
Assert::assertTrue(
$assertedSomething,
'there was nothing in the table of expected version data'
);
}
/**
* @Then the major-minor-micro version data in the response should match the version string
*
@@ -112,8 +46,9 @@ class CapabilitiesContext implements Context {
* @throws Exception
*/
public function checkVersionMajorMinorMicroResponse():void {
$versionXML = $this->featureContext->appConfigurationContext->getVersionXml(__METHOD__);
$versionString = (string) $versionXML->string;
$jsonResponse = $this->featureContext->getJsonDecodedResponseBodyContent();
$versionData = $jsonResponse->ocs->data->version;
$versionString = (string) $versionData->string;
// We expect that versionString will be in a format like "10.9.2 beta" or "10.9.2-alpha" or "10.9.2"
$result = \preg_match('/^[0-9]+\.[0-9]+\.[0-9]+/', $versionString, $matches);
Assert::assertSame(
@@ -126,9 +61,9 @@ class CapabilitiesContext implements Context {
$expectedMajor = $semVerParts[0];
$expectedMinor = $semVerParts[1];
$expectedMicro = $semVerParts[2];
$actualMajor = (string) $versionXML->major;
$actualMinor = (string) $versionXML->minor;
$actualMicro = (string) $versionXML->micro;
$actualMajor = (string) $versionData->major;
$actualMinor = (string) $versionData->minor;
$actualMicro = (string) $versionData->micro;
Assert::assertSame(
$expectedMajor,
$actualMajor,
@@ -175,35 +110,6 @@ class CapabilitiesContext implements Context {
);
}
/**
* @Then the capabilities should not contain
*
* @param TableNode|null $formData
*
* @return void
*/
public function theCapabilitiesShouldNotContain(TableNode $formData):void {
$capabilitiesXML = $this->featureContext->appConfigurationContext->getCapabilitiesXml(__METHOD__);
$assertedSomething = false;
foreach ($formData->getHash() as $row) {
Assert::assertFalse(
$this->featureContext->appConfigurationContext->parameterValueExistsInXml(
$capabilitiesXML,
$row['capability'],
$row['path_to_element']
),
"Capability {$row['capability']} {$row['path_to_element']} exists but it should not exist"
);
$assertedSomething = true;
}
Assert::assertTrue(
$assertedSomething,
'there was nothing in the table of not expected capabilities'
);
}
/**
* This will run before EVERY scenario.
* It will set the properties for this object.
@@ -24,6 +24,7 @@
use Behat\Behat\Hook\Scope\BeforeStepScope;
use GuzzleHttp\Exception\GuzzleException;
use Helmich\JsonAssert\JsonAssertions;
use rdx\behatvars\BehatVariablesContext;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Gherkin\Node\PyStringNode;
@@ -1642,6 +1643,55 @@ class FeatureContext extends BehatVariablesContext {
$this->emptyLastHTTPStatusCodesArray();
}
/**
* @param PyStringNode $schemaString
*
* @return mixed
*/
private function getJSONSchema(PyStringNode $schemaString) {
$schemaRawString = $schemaString->getRaw();
// substitute the inline codes or values
$schemaRawString = $this->substituteInLineCodes($schemaRawString);
$schema = json_decode($schemaRawString);
Assert::assertNotNull($schema, 'schema is not valid JSON');
return $schema;
}
/**
* returns json decoded body content of a json response as an object
*
* @param ResponseInterface|null $response
*
* @return object
*/
public function getJsonDecodedResponseBodyContent(ResponseInterface $response = null):?object {
$response = $response ?? $this->response;
if ($response !== null) {
$response->getBody()->rewind();
return json_decode($response->getBody()->getContents());
}
return null;
}
/**
* @Then the JSON data of the response should match
*
* @param PyStringNode $schemaString
*
* @return void
*
* @throws Exception
*/
public function theDataOfTheResponseShouldMatch(
PyStringNode $schemaString
): void {
$jsonResponse = $this->getJsonDecodedResponseBodyContent();
JsonAssertions::assertJsonDocumentMatchesSchema(
$jsonResponse->ocs->data,
$this->getJSONSchema($schemaString)
);
}
/**
* @Then /^the HTTP status code should be "([^"]*)"$/
*
@@ -931,12 +931,17 @@ class OCSContext implements Context {
* @throws Exception
*/
public function getOCSResponseStatusCode(ResponseInterface $response):string {
$jsonResponse = $this->featureContext->getJsonDecodedResponseBodyContent($response);
if (\is_object($jsonResponse) && $jsonResponse->ocs->meta->statuscode) {
return (string) $jsonResponse->ocs->meta->statuscode;
}
// go to xml response when json response is null (it means not formated and get status code)
$responseXml = $this->featureContext->getResponseXml($response, __METHOD__);
if (isset($responseXml->meta[0], $responseXml->meta[0]->statuscode)) {
return (string) $responseXml->meta[0]->statuscode;
}
throw new Exception(
"No OCS status code found in responseXml"
"No OCS status code found in response"
);
}