addUser(
Fixture::VIEW_USER_LOGIN,
Fixture::VIEW_USER_PASSWORD,
'hello2@example.org',
Date::now()->getDatetime()
);
$model->addUserAccess(Fixture::VIEW_USER_LOGIN, 'view', [1]);
$model->addTokenAuth(
Fixture::VIEW_USER_LOGIN,
Fixture::VIEW_USER_TOKEN,
'View user token',
Date::now()->getDatetime()
);
}
public function testApiCallWithoutPermissionShouldHaveCorrectHttpStatus()
{
[$response, $info] = $this->curl(
Fixture::getRootUrl() . 'tests/PHPUnit/proxy/index.php?module=API&method=API.getPhpVersion',
['token_auth' => Fixture::VIEW_USER_TOKEN]
);
// The user doesn't have superuser access, so status code should be 401
$this->assertEquals(401, $info['http_code']);
}
public function testApiShouldHaveCorrectHttpStatus()
{
[$response, $info] = $this->curl(
Fixture::getRootUrl() . 'tests/PHPUnit/proxy/index.php?module=API&method=API.getPhpVersion',
['token_auth' => Fixture::ADMIN_USER_TOKEN]
);
// User has access so status code should be 200
$this->assertEquals(200, $info['http_code']);
}
public function testProcessedApiCallWithExceptionShouldHaveCorrectHttpStatus()
{
[$response, $info] = $this->curl(
Fixture::getRootUrl(
) . 'tests/PHPUnit/proxy/index.php?module=API&method=SitesManager.getSiteUrlsFromId&idSite=1&reThrow=1',
['token_auth' => Fixture::VIEW_USER_TOKEN]
);
// The message and status code from a nested API call should be used if they are not catched.
$this->assertEquals("\n\n \n", $response);
$this->assertEquals(401, $info['http_code']);
}
public function testProcessedApiCallWithCaughtExceptionShouldHaveCorrectHttpStatus()
{
[$response, $info] = $this->curl(
Fixture::getRootUrl(
) . 'tests/PHPUnit/proxy/index.php?module=API&method=SitesManager.getSiteUrlsFromId&idSite=1',
['token_auth' => Fixture::VIEW_USER_TOKEN]
);
// Even though the user doesn't have superuser access, the response code should be 200 as the exception in the nested API call is caught
$this->assertEquals("\nerror caught", $response);
$this->assertEquals(200, $info['http_code']);
}
public function testProcessedApiShouldHaveCorrectHttpStatus()
{
[$response, $info] = $this->curl(
Fixture::getRootUrl(
) . 'tests/PHPUnit/proxy/index.php?module=API&method=SitesManager.getSiteUrlsFromId&idSite=1',
['token_auth' => Fixture::ADMIN_USER_TOKEN]
);
// The user has access to nested API method, so it's response should be returned
$this->assertEquals("\nno error", $response);
$this->assertEquals(200, $info['http_code']);
}
public function testApiMissingRequiredParameterShouldReturn400()
{
[$response, $info] = $this->curl(
Fixture::getRootUrl() . 'tests/PHPUnit/proxy/index.php?module=API&method=SitesManager.getSiteFromId',
['token_auth' => Fixture::ADMIN_USER_TOKEN]
);
$this->assertEquals(400, $info['http_code']);
$this->assertStringContainsString('Please specify a value for \'idSite\'', $response);
}
public function testValidModuleInvalidActionShouldReturn404()
{
[$response, $info] = $this->curl(
Fixture::getRootUrl() . 'tests/PHPUnit/proxy/index.php?module=CoreHome&action=doesNotExist'
);
$this->assertEquals(404, $info['http_code']);
$this->assertStringContainsString("Action 'doesNotExist' not found in the module 'CoreHome'", $response);
}
public function testMissingAssetChunkShouldReturn404()
{
[, $info] = $this->curl(
Fixture::getRootUrl() . 'tests/PHPUnit/proxy/index.php?module=Proxy&action=getPluginUmdJs&plugin=NotAPlugin'
);
$this->assertEquals(404, $info['http_code']);
}
public function testMissingPluginShouldReturn404HttpStatus()
{
[$response, $info] = $this->curl(
Fixture::getRootUrl() . 'tests/PHPUnit/proxy/index.php?module=NotAPlugin&action=index'
);
$this->assertEquals(404, $info['http_code']);
$this->assertStringContainsString('The plugin NotAPlugin was not found.', $response);
}
private function curl($url, $postParams = [])
{
if (!function_exists('curl_init')) {
$this->markTestSkipped('Curl is not installed');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postParams);
$response = curl_exec($ch);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$response = substr($response, $headerSize);
$responseInfo = curl_getinfo($ch);
curl_close($ch);
return [$response, $responseInfo];
}
public function provideContainerConfig()
{
return [
'observers.global' => \DI\add(
[
[
'API.Request.intercept',
\DI\value(
function (&$returnedValue, $finalParameters, $pluginName, $methodName, $parametersRequest) {
if ($pluginName !== 'SitesManager' || $methodName !== 'getSiteUrlsFromId') {
return;
}
try {
// This call requires SuperUser access
Request::processRequest('API.getPhpVersion', [], $parametersRequest);
$returnedValue = 'no error';
} catch (\Exception $e) {
$returnedValue = 'error caught';
if (!empty($parametersRequest['reThrow'])) {
throw $e;
}
}
}
),
],
]
),
];
}
}
ResponseCodeTest::$fixture = new Fixture();