diff --git a/mistral/exceptions.py b/mistral/exceptions.py index 3fdddd4e4..8d151153e 100644 --- a/mistral/exceptions.py +++ b/mistral/exceptions.py @@ -29,7 +29,8 @@ class MistralException(ex.Error): def __str__(self): return self.message - def __init__(self, message): + def __init__(self, message=message): + self.message = message super(MistralException, self).__init__( '%s: %s' % (self.code, self.message)) diff --git a/mistral/tests/api/base.py b/mistral/tests/api/base.py index 6a1da3702..2660e5035 100644 --- a/mistral/tests/api/base.py +++ b/mistral/tests/api/base.py @@ -16,6 +16,7 @@ import pecan import pecan.testing +from webtest.app import AppError from oslo.config import cfg @@ -55,3 +56,12 @@ class FunctionalTest(test_base.DbTestCase): def tearDown(self): super(FunctionalTest, self).tearDown() pecan.set_config({}, overwrite=True) + + def assertNotFound(self, url): + try: + self.app.get(url, headers={'Accept': 'application/json'}) + except AppError as error: + if hasattr(error, 'message'): + self.assertIn('Bad response: 404 Not Found', error.message) + return + self.fail('Expected 404 Not found but got OK') diff --git a/mistral/tests/api/v1/controllers/test_executions.py b/mistral/tests/api/v1/controllers/test_executions.py index 7336a994d..9d5e7200f 100644 --- a/mistral/tests/api/v1/controllers/test_executions.py +++ b/mistral/tests/api/v1/controllers/test_executions.py @@ -16,6 +16,8 @@ import mock +from mistral import exceptions as ex +from webtest.app import AppError from mistral.tests.api import base from mistral.db import api as db_api from mistral.engine import engine @@ -52,6 +54,11 @@ class TestExecutionsController(base.FunctionalTest): self.assertEqual(resp.status_int, 200) self.assertDictEqual(EXECS[0], resp.json) + @mock.patch.object(db_api, "execution_get", + mock.MagicMock(return_value=None)) + def test_get_empty(self): + self.assertNotFound('/v1/workbooks/my_workbook/executions/123') + @mock.patch.object(db_api, "execution_update", mock.MagicMock(return_value=UPDATED_EXEC)) def test_put(self): @@ -66,10 +73,17 @@ class TestExecutionsController(base.FunctionalTest): def test_post(self): resp = self.app.post_json('/v1/workbooks/my_workbook/executions', EXECS[0]) - self.assertEqual(resp.status_int, 201) self.assertDictEqual(EXECS[0], resp.json) + @mock.patch.object(engine, "start_workflow_execution", + mock.MagicMock(side_effect=ex.MistralException)) + def test_post_throws_exception(self): + with self.assertRaises(AppError) as context: + self.app.post_json('/v1/workbooks/my_workbook/executions', + EXECS[0]) + self.assertIn('Bad response: 400', context.exception.message) + @mock.patch.object(db_api, "execution_delete", mock.MagicMock(return_value=None)) def test_delete(self):