From df39e1640fb9d08cbdc2f5e6505d70bf78843cb5 Mon Sep 17 00:00:00 2001 From: Artur Neumann Date: Mon, 16 Mar 2020 17:26:04 +0545 Subject: [PATCH] test: gateway checker second try --- tests/integration/bootstrap/RestContext.php | 36 ++++++++++- tests/integration/gwchecker.feature | 68 +++++++++++++++++++++ 2 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 tests/integration/gwchecker.feature diff --git a/tests/integration/bootstrap/RestContext.php b/tests/integration/bootstrap/RestContext.php index e63af8300a..7ce480fce6 100644 --- a/tests/integration/bootstrap/RestContext.php +++ b/tests/integration/bootstrap/RestContext.php @@ -26,6 +26,9 @@ class RestContext implements Context private $restObjectType = null; private $restObjectMethod = 'get'; private $client = null; + /** + * @var \GuzzleHttp\Psr7\Response + */ private $response = null; private $requestUrl = null; private $apiUrl = 'api/v3'; @@ -37,6 +40,8 @@ class RestContext implements Context private $postFields = []; private $postFiles = []; + const DEBUG_MODE_SWITCH_FILE_PATH = __DIR__ . "/../../../bootstrap/install_debug_mode.enabled"; + /** * Initializes context. * Every scenario gets it's own context object. @@ -201,6 +206,24 @@ public function thatThePostFileIs($fieldName, $fieldValue) $this->postFiles[$fieldName] = $fieldValue; } + /** + * @Given /^that I have enabled debug mode$/ + */ + public function thatIHaveEnabledDebugMode() + { + fopen(self::DEBUG_MODE_SWITCH_FILE_PATH, "w"); + } + + /** + * @AfterScenario + * @Given /^that I have disabled debug mode$/ + */ + public function disableDebugModeAfterScenario() + { + if (file_exists(self::DEBUG_MODE_SWITCH_FILE_PATH)) { + unlink(self::DEBUG_MODE_SWITCH_FILE_PATH); + } + } /** * @When /^I request "([^"]*)"$/ */ @@ -211,9 +234,9 @@ public function iRequest($pageUrl) switch (strtoupper($this->restObjectMethod)) { case 'GET': $request = (array)$this->restObject; - $id = ( isset($request['id']) ) ? $request['id'] : ''; + $idAttachment = ( isset($request['id']) ) ? "/" . $request['id'] : ''; $response = $this->client - ->get($this->requestUrl.'/'.$id, [ + ->get($this->requestUrl.$idAttachment, [ 'query' => isset($request['query string']) ? trim($request['query string']) : null, 'headers' => $this->headers ]); @@ -403,6 +426,13 @@ public function theResponseIsJsonp() } } + /** + * @Then /^the response is empty/ + */ + public function theResponseIsEmpty() + { + \PHPUnit_Framework_Assert::assertEquals(0, $this->response->getBody()->getSize()); + } /** * @Given /^the response has a "([^"]*)" property$/ * @Given /^the response has an "([^"]*)" property$/ @@ -631,7 +661,7 @@ public function thePropertyIsEmpty($propertyName) $actualPropertyValue = array_get($data, $propertyName); if (!empty($actualPropertyValue)) { - throw new \Exception("Property '{$propertyName}' is not empty!\n"); + throw new \Exception("Property '{$propertyName}' is not empty but '{$actualPropertyValue}'\n"); } } diff --git a/tests/integration/gwchecker.feature b/tests/integration/gwchecker.feature new file mode 100644 index 0000000000..1f19bd89f4 --- /dev/null +++ b/tests/integration/gwchecker.feature @@ -0,0 +1,68 @@ +Feature: Gateway checker + + Scenario: normal API request should still work with enabled GW checker + Given that I have enabled debug mode + And that I want to get all "Configs" + When I request "/config" + Then the response is JSON + And the response has a "count" property + And the type of the "count" property is "numeric" + And the guzzle status code should be 200 + + Scenario Outline: get debug information with enabled GW checker + Given that I have enabled debug mode + And that I want to get all "Configs" + When I request "/?gwcheck=" + Then the response is JSON + And the response does not have a "count" property + And the "api.name" property equals "ushahidi:platform:gwcheck" + And the "api.version" property equals "0.1" + And the "data._GET.gwcheck" property equals "" + And the "data._REQUEST.gwcheck" property equals "" + And the "data._POST" property is empty + And the "data._SERVER.REQUEST_METHOD" property equals "GET" + And the "data._SERVER.QUERY_STRING" property equals "gwcheck=" + And the "data._SERVER.HTTP_ACCEPT" property equals "application/json" + And the "data._SERVER.SCRIPT_NAME" property contains "/index.php" + And the "data._SERVER.REQUEST_URI" property equals "/api/v3/?gwcheck=" + #the following line might fail when running tests on Apache + And the "data._SERVER.PATH_INFO" property equals "/api/v3/" + And the "data._SERVER.HTTP_USER_AGENT" property contains "GuzzleHttp" + And the "data._SERVER.DOCUMENT_ROOT" property contains "/httpdocs" + And the "data._SERVER.SCRIPT_FILENAME" property contains "/httpdocs/index.php" + And the "data._SERVER.HTTP_ACCEPT_CHARSET" property is empty + And the "data._SERVER.HTTP_ACCEPT_ENCODING" property is empty + And the "data._SERVER.HTTP_ACCEPT_LANGUAGE" property is empty + And the "data._SERVER.HTTP_AUTHORIZATION" property is empty + And the "data._SERVER.HTTP_CONNECTION" property is empty + And the "data._SERVER.HTTP_ORIGIN" property is empty + And the "data._SERVER.HTTP_REFERER" property is empty + And the "data._SERVER.ORIG_PATH_INFO" property is empty + And the guzzle status code should be 200 + Examples: + | endpoint | gwCheckValue | + | config | true | + | config | TRUE | + | config | 1 | + | config | anyString | + | config | false | + | does-not-exist | true | + | does-not-exist | TRUE | + | does-not-exist | 1 | + | does-not-exist | anyString | + | does-not-exist | false | + + Scenario Outline: debug information should not be visible with disabled GW checker + Given that I have disabled debug mode + And that I want to get all "Configs" + When I request "/config?gwcheck=" + Then the response is empty + And the guzzle status code should be 204 + And the "X-Ushahidi-Platform-Install-Debug-Mode" header should be "off" + Examples: + | gwCheckValue | + | true | + | TRUE | + | 1 | + | anyString | + | false |