Skip to content
Snippets Groups Projects
Commit d14c26c7 authored by Nikolay Stanchev's avatar Nikolay Stanchev
Browse files

Added tests for malformed request action being sent to /aggregator/control

parent d74fd685
No related branches found
No related tags found
No related merge requests found
......@@ -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')
......
......@@ -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
......@@ -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):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment