Skip to content

Commit

Permalink
Refactor and add integration tests of invalid REST parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronweeden committed Dec 4, 2023
1 parent 13875e3 commit a9de499
Show file tree
Hide file tree
Showing 5 changed files with 316 additions and 90 deletions.
18 changes: 16 additions & 2 deletions tests/integration_tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,25 @@
// Autoloader for main framework test classes.
spl_autoload_register(
function ($className) use ($dir) {
// Look for classes relative to the `lib` directory; each namespace has
// its own directory in `lib` with the exception of the
// `IntegrationTests` namespace whose classes are defined in files
// directly in `lib`. For example, `TestHarness\XdmodTestHelper`
// resolves to `$dir/lib/TestHarness/XdmodTestHelper.php`, whereas
// `IntegrationTests\TokenAuthTest` resolves to
// `$dir/lib/TokenAuthTest.php`.
$classPath
= $dir
. '/../../../xdmod/tests/integration/lib/'
. str_replace('\\', '/', $className)
. '.php';
. str_replace(
'\\',
'/',
preg_replace(
'/IntegrationTests\\\\?/',
'',
$className
)
) . '.php';

if (is_readable($classPath)) {
return require_once $classPath;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

namespace IntegrationTests\REST\Efficiency;
namespace IntegrationTests\REST;

class EfficiencyTest extends \PHPUnit_Framework_TestCase
use IntegrationTests\BaseTest;

class EfficiencyTest extends BaseTest
{
const ENDPOINT = 'rest/v1/efficiency/';

Expand Down Expand Up @@ -58,7 +60,7 @@ public function testAnalytics()
$analytics = array();
foreach ($response[0]['data'] as $analyticType){
$analytic = $analyticType['analytics'];
foreach($analytic as $key => $value){
foreach($analytic as $value){
$analytics[] = $value['analytic'];
}
}
Expand Down Expand Up @@ -145,27 +147,109 @@ public function testCPUUsageScatterPlotEndpointPub()
$this->assertFalse($response[0]['success']);
}

public function scatterPlotDataMalformedRequest()
{
$inputs = array();

$inputs[] = array('cd', array('start' => 0, 'limit' => 10));
$inputs[] = array('cd', array('start' => 0, 'limit' => 10, 'config' => ''));
$inputs[] = array('cd', array('start' => 0, 'limit' => 10, 'config' => '{"realm": "SUPREMM"}'));
$inputs[] = array('cd', array('start' => 0, 'limit' => 10, 'config' => 'not json data'));
$inputs[] = array('cd', array('start' => 'smart', 'limit' => 'asdf', 'config' => '{"realm": "SUPREMM"}'));

return $inputs;
}

/**
* @dataProvider scatterPlotDataMalformedRequest
* @dataProvider provideCPUUsageScatterPlotEndpointMalformedRequest
*/
public function testCPUUsageScatterPlotEndpointMalformedRequest($usr, $params)
public function testCPUUsageScatterPlotEndpointMalformedRequest(
$id,
$role,
$input,
$output
) {
parent::requestAndValidateJson(
self::$helpers[$role],
$input,
$output
);
}

public function provideCPUUsageScatterPlotEndpointMalformedRequest()
{
$response = self::$helpers[$usr]->get(self::ENDPOINT . 'scatterPlot/CPU%20Usage', $params);
$this->assertEquals(400, $response[1]['http_code']);
$this->assertFalse($response[0]['success']);
$validInput = [
'path' => self::ENDPOINT . 'scatterPlot/CPU%20Usage',
'method' => 'get',
'params' => $this->getScatterPlotDataParameters(),
'data' => null
];
// Run some standard endpoint tests.
$tests = parent::provideRestEndpointTests(
$validInput,
[
'authentication' => true,
'int_params' => ['start', 'limit'],
'string_params' => ['config']
]
);
// Test bad request parameters.
$tests[] = [
'invalid_config',
'cd',
parent::mergeParams(
$validInput,
'params',
['config' => 'foo']
),
parent::validateBadRequestResponse(
'syntax error in config parameter',
104
)
];
$config = json_decode($validInput['params']['config'], true);
$tests = $this->getCPUUsageScatterPlotEndpointMalformedParamTests(
$tests,
$validInput,
$config,
null,
'missing_config_',
function ($param) {
return "Missing mandatory config property $param";
}
);
$tests = $this->getCPUUsageScatterPlotEndpointMalformedParamTests(
$tests,
$validInput,
$config,
'order_by',
'missing_config_order_by_',
function () {
return 'Malformed config property order_by';
}
);
return $tests;
}

private function getCPUUsageScatterPlotEndpointMalformedParamTests(
array $tests,
array $validInput,
array $config,
$key,
$idPrefix,
$getMessage
) {
if (is_null($key)) {
$params = $config;
} else {
$params = $config[$key];
}
foreach (array_keys($params) as $param) {
$newConfig = $config;
if (is_null($key)) {
unset($newConfig[$param]);
} else {
unset($newConfig[$key][$param]);
}
$tests[] = [
$idPrefix . $param,
'cd',
parent::mergeParams(
$validInput,
'params',
['config' => json_encode($newConfig)]
),
parent::validateBadRequestResponse($getMessage($param), 104)
];
}
return $tests;
}

/***
Expand Down Expand Up @@ -313,10 +397,6 @@ public function drilldownDataMalformedRequest()
{
$inputs = array();

$inputs[] = array('cd', 400, array(''));
$inputs[] = array('cd', 400, array('not json data'));
$inputs[] = array('cd', 400, array('{"realm": "SUPREMM"}'));

$params = $this->getDrillDownDataParameters(array(
'global_filters' => array(
'data' => array(
Expand Down Expand Up @@ -391,4 +471,43 @@ public function testCPUUsageDrillownEndpointMalformedRequest($usr, $http_code, $
$this->assertFalse($resdata['success']);
}
}

/**
* @dataProvider provideGetHistogramDataParamValidation
*/
public function testGetHistogramDataParamValidation(
$id,
$role,
$input,
$output
) {
parent::requestAndValidateJson(
self::$helpers[$role],
$input,
$output
);
}

public function provideGetHistogramDataParamValidation()
{
$validInput = [
'path' => self::ENDPOINT . 'histogram/cpuuser',
'method' => 'get',
'params' => [],
'data' => null
];
// Run some standard endpoint tests.
return parent::provideRestEndpointTests(
$validInput,
[
'run_as' => 'cd',
'additional_params' => $this->getDrillDownDataParameters(),
// note that 'limit' is not included in the int_params because
// the path to getIntParam('limit') via getDimensionValues()
// is not taken if 'limit' is an invalid integer.
'int_params' => ['offset'],
'string_params' => ['search_text', 'realm']
]
);
}
}
62 changes: 0 additions & 62 deletions tests/integration_tests/lib/REST/SupremmDataflow/QualityTest.php

This file was deleted.

Loading

0 comments on commit a9de499

Please sign in to comment.