diff --git a/src/clmc-webservice/clmcservice/__init__.py b/src/clmc-webservice/clmcservice/__init__.py index 82db449f6dcae14bc98c6ddaff42af2b2dc4e676..3875d4274a30b328c946d8763709df6dfdb25eb7 100644 --- a/src/clmc-webservice/clmcservice/__init__.py +++ b/src/clmc-webservice/clmcservice/__init__.py @@ -44,7 +44,7 @@ def main(global_config, **settings): config.add_view(AggregatorConfig, attr='get', request_method='GET') config.add_view(AggregatorConfig, attr='put', request_method='PUT') - config.add_route('aggregator_starter', '/aggregator/control') + config.add_route('aggregator_controller', '/aggregator/control') config.add_view(AggregatorController, attr='get', request_method='GET') config.add_view(AggregatorController, attr='put', request_method='PUT') diff --git a/src/clmc-webservice/clmcservice/tests.py b/src/clmc-webservice/clmcservice/tests.py index 1e4342e8f661e4b14edf3227047311dd0bc168f4..9b2370398a40f5e6dc719dc1cf5f36cbc827bbe9 100644 --- a/src/clmc-webservice/clmcservice/tests.py +++ b/src/clmc-webservice/clmcservice/tests.py @@ -211,3 +211,31 @@ class TestAggregator(object): # kill the started process after the test is over pid = request.registry.settings[PROCESS_ATTRIBUTE].pid os.kill(pid, signal.SIGTERM) + + @pytest.mark.parametrize("input_body", [ + '{"action": "malformed"}', + '{"action": true}', + '{"action": false}', + '{"action": 1}', + '{invalid-json}', + '{"action": "start", "unneeded_argument": false}', + '{}' + ]) + def test_malformed_actions(self, input_body): + from clmcservice.views import AggregatorController # nested import so that importing the class view is part of the test itself + + assert not self.config.get_settings().get(STATUS_ATTRIBUTE), "Initially aggregator is not running." + assert self.config.get_settings().get(PROCESS_ATTRIBUTE) is None, "Initially no aggregator process is running." + + # test restarting the aggregator process when it is running + request = testing.DummyRequest() + input_body = input_body + request.body = input_body.encode(request.charset) + + error_raised = False + try: + AggregatorController(request).put() + except HTTPBadRequest: + error_raised = True + + assert error_raised diff --git a/src/clmc-webservice/clmcservice/views.py b/src/clmc-webservice/clmcservice/views.py index 3d235ca2aea833e264095dd90d18cef45dae660d..5d9a8e40930a5466919870300404c48cbda52937 100644 --- a/src/clmc-webservice/clmcservice/views.py +++ b/src/clmc-webservice/clmcservice/views.py @@ -28,12 +28,6 @@ from clmcservice.utilities import validate_config_content, validate_action_conte import os.path -# TODO -# 1) Is authorization needed at this stage ? -# 2) Validating the URL address and the database name ? -# 3) Restart the aggregator when configuration is updated - - @view_defaults(route_name='aggregator_config', renderer='json') class AggregatorConfig(object): """ @@ -88,7 +82,7 @@ class AggregatorConfig(object): raise HTTPBadRequest("Bad request content - configuration format is incorrect.") -@view_defaults(route_name='aggregator_starter', renderer='json') +@view_defaults(route_name='aggregator_controller', renderer='json') class AggregatorController(object): """ @@ -121,7 +115,7 @@ class AggregatorController(object): A PUT API call for the status of the aggregator. :return: A JSON response to the PUT call - essentially saying whether the aggregator is running or not - :raises HTTPBadRequest: if request body is not a valid JSON for the starter + :raises HTTPBadRequest: if request body is not a valid JSON for the controller """ content = self.request.body.decode(self.request.charset) @@ -152,7 +146,7 @@ class AggregatorController(object): return {STATUS_ATTRIBUTE: self.request.registry.settings.get(STATUS_ATTRIBUTE)} except AssertionError: - raise HTTPBadRequest("Bad request content - must be in JSON format: {action: value}, where value is 'start', 'stop' or 'restart'.") + raise HTTPBadRequest('Bad request content - must be in JSON format: {"action": value}, where value is "start", "stop" or "restart".') @staticmethod def start_aggregator(config):