From 470ca175e23abca44da9f032e5a2dd404db11adb Mon Sep 17 00:00:00 2001 From: Jim Rollenhagen <jim@jimrollenhagen.com> Date: Wed, 12 Mar 2014 09:50:32 -0700 Subject: [PATCH] get api tests passing --- teeth_agent/api/controllers/v1/command.py | 15 ++----- teeth_agent/base.py | 26 ++++++------ teeth_agent/tests/api.py | 50 ++++++++++++----------- 3 files changed, 44 insertions(+), 47 deletions(-) diff --git a/teeth_agent/api/controllers/v1/command.py b/teeth_agent/api/controllers/v1/command.py index 5785f85cb..be98355e1 100644 --- a/teeth_agent/api/controllers/v1/command.py +++ b/teeth_agent/api/controllers/v1/command.py @@ -24,10 +24,10 @@ from teeth_agent.api.controllers.v1 import base class CommandResult(base.APIBase): id = types.text command_name = types.text - command_params = base.json_type + command_params = types.DictType(types.text, base.json_type) command_status = types.text command_error = base.exception_type - command_result = types.text + command_result = types.DictType(types.text, base.json_type) @classmethod def from_result(cls, result): @@ -51,15 +51,8 @@ class CommandResultList(base.APIBase): class Command(base.APIBase): """A command representation.""" - name = types.text - params = base.json_type - - @classmethod - def deserialize(cls, obj): - instance = cls() - instance.name = obj['name'] - instance.params = obj['params'] - return instance + name = types.wsattr(types.text, mandatory=True) + params = types.wsattr(base.MultiType(dict), mandatory=True) class CommandController(rest.RestController): diff --git a/teeth_agent/base.py b/teeth_agent/base.py index 3170678a6..4ed81e7aa 100644 --- a/teeth_agent/base.py +++ b/teeth_agent/base.py @@ -26,29 +26,29 @@ from teeth_agent import errors class AgentCommandStatus(object): - RUNNING = 'RUNNING' - SUCCEEDED = 'SUCCEEDED' - FAILED = 'FAILED' + RUNNING = u'RUNNING' + SUCCEEDED = u'SUCCEEDED' + FAILED = u'FAILED' class BaseCommandResult(encoding.Serializable): def __init__(self, command_name, command_params): - self.id = str(uuid.uuid4()) + self.id = unicode(uuid.uuid4()) self.command_name = command_name self.command_params = command_params self.command_status = AgentCommandStatus.RUNNING self.command_error = None self.command_result = None - def serialize(self, view): - return collections.OrderedDict([ - ('id', self.id), - ('command_name', self.command_name), - ('command_params', self.command_params), - ('command_status', self.command_status), - ('command_error', self.command_error), - ('command_result', self.command_result), - ]) + def serialize(self): + return dict(( + (u'id', self.id), + (u'command_name', self.command_name), + (u'command_params', self.command_params), + (u'command_status', self.command_status), + (u'command_error', self.command_error), + (u'command_result', self.command_result), + )) def is_done(self): return self.command_status != AgentCommandStatus.RUNNING diff --git a/teeth_agent/tests/api.py b/teeth_agent/tests/api.py index fc0686640..50ff05ba8 100644 --- a/teeth_agent/tests/api.py +++ b/teeth_agent/tests/api.py @@ -25,7 +25,7 @@ from werkzeug import wrappers from teeth_rest import encoding from teeth_agent import agent -from teeth_agent import api +from teeth_agent.api import app from teeth_agent import base @@ -48,9 +48,9 @@ class TestTeethAPI(unittest.TestCase): status = agent.TeethAgentStatus('TEST_MODE', time.time(), 'v72ac9') mock_agent = mock.MagicMock() mock_agent.get_status.return_value = status - api_server = api.TeethAgentAPIServer(mock_agent) + api_server = app.setup_app(mock_agent) - response = self._make_request(api_server, 'GET', '/v1.0/status') + response = self._make_request(api_server, 'GET', '/v1/status') mock_agent.get_status.assert_called_once_with() self.assertEqual(response.status_code, 200) @@ -72,11 +72,11 @@ class TestTeethAPI(unittest.TestCase): mock_agent = mock.MagicMock() mock_agent.execute_command.return_value = result - api_server = api.TeethAgentAPIServer(mock_agent) + api_server = app.setup_app(mock_agent) response = self._make_request(api_server, 'POST', - '/v1.0/commands', + '/v1/commands/', data=command) self.assertEqual(mock_agent.execute_command.call_count, 1) @@ -90,50 +90,54 @@ class TestTeethAPI(unittest.TestCase): def test_execute_agent_command_validation(self): mock_agent = mock.MagicMock() - api_server = api.TeethAgentAPIServer(mock_agent) + api_server = app.setup_app(mock_agent) - invalid_command = {} + invalid_command = {'invalid': 'stuff'} response = self._make_request(api_server, 'POST', - '/v1.0/commands', + '/v1/commands', data=invalid_command) self.assertEqual(response.status_code, 400) data = json.loads(response.data) - self.assertEqual(data['details'], 'Missing command \'name\' field.') + msg = 'Invalid input for field/attribute name.' + self.assertTrue(msg in data['faultstring']) + msg = 'Mandatory field missing' + self.assertTrue(msg in data['faultstring']) def test_execute_agent_command_params_validation(self): mock_agent = mock.MagicMock() - api_server = api.TeethAgentAPIServer(mock_agent) + api_server = app.setup_app(mock_agent) invalid_command = {'name': 'do_things', 'params': []} response = self._make_request(api_server, 'POST', - '/v1.0/commands', + '/v1/commands', data=invalid_command) self.assertEqual(response.status_code, 400) data = json.loads(response.data) - self.assertEqual(data['details'], - 'Command params must be a dictionary.') + # this message is actually much longer, but I'm ok with this + msg = 'Invalid input for field/attribute params.' + self.assertTrue(msg in data['faultstring']) def test_list_command_results(self): - cmd_result = base.SyncCommandResult('do_things', - {'key': 'value'}, + self.maxDiff = 10000 + cmd_result = base.SyncCommandResult(u'do_things', + {u'key': u'value'}, True, - {'test': 'result'}) + {u'test': u'result'}) mock_agent = mock.create_autospec(agent.TeethAgent) mock_agent.list_command_results.return_value = [ cmd_result, ] - api_server = api.TeethAgentAPIServer(mock_agent) - response = self._make_request(api_server, 'GET', '/v1.0/commands') + api_server = app.setup_app(mock_agent) + response = self._make_request(api_server, 'GET', '/v1/commands') self.assertEqual(response.status_code, 200) self.assertEqual(json.loads(response.data), { - 'items': [ - cmd_result.serialize(encoding.SerializationViews.PUBLIC), + u'commands': [ + cmd_result.serialize(None), ], - 'links': [], }) def test_get_command_result(self): @@ -148,10 +152,10 @@ class TestTeethAPI(unittest.TestCase): mock_agent = mock.create_autospec(agent.TeethAgent) mock_agent.get_command_result.return_value = cmd_result - api_server = api.TeethAgentAPIServer(mock_agent) + api_server = app.setup_app(mock_agent) response = self._make_request(api_server, 'GET', - '/v1.0/commands/abc123') + '/v1/commands/abc123') self.assertEqual(response.status_code, 200) data = json.loads(response.data) self.assertEqual(data, serialized_cmd_result)