Skip to content

Commit

Permalink
test: gateway checker
Browse files Browse the repository at this point in the history
  • Loading branch information
individual-it committed Mar 18, 2020
1 parent 49bb8d9 commit a84acfb
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 3 deletions.
36 changes: 33 additions & 3 deletions tests/integration/bootstrap/RestContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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.
Expand Down Expand Up @@ -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 "([^"]*)"$/
*/
Expand All @@ -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
]);
Expand Down Expand Up @@ -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$/
Expand Down Expand Up @@ -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");
}
}

Expand Down
68 changes: 68 additions & 0 deletions tests/integration/gwchecker.feature
Original file line number Diff line number Diff line change
@@ -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 "/<endpoint>?gwcheck=<gwCheckValue>"
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 "<gwCheckValue>"
And the "data._REQUEST.gwcheck" property equals "<gwCheckValue>"
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=<gwCheckValue>"
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/<endpoint>?gwcheck=<gwCheckValue>"
#the following line might fail when running tests on Apache
And the "data._SERVER.PATH_INFO" property equals "/api/v3/<endpoint>"
And the "data._SERVER.HTTP_USER_AGENT" property contains "GuzzleHttp"
And the "data._SERVER.DOCUMENT_ROOT" property contains "/platform"
And the "data._SERVER.SCRIPT_FILENAME" property contains "/platform/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=<gwCheckValue>"
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 |

0 comments on commit a84acfb

Please sign in to comment.