Ejemplo n.º 1
0
    def test_mapped_tacker_error_localized(self, mock_translation):
        gettextutils.install('blaa', lazy=True)
        msg_translation = 'Translated error'
        mock_translation.return_value = msg_translation
        msg = _('Unmapped error')

        class TestException(n_exc.TackerException):
            message = msg

        controller = mock.MagicMock()
        controller.test.side_effect = TestException()
        faults = {TestException: exc.HTTPGatewayTimeout}
        resource = webtest.TestApp(
            wsgi_resource.Resource(controller, faults=faults))

        environ = {
            'wsgiorg.routing_args': (None, {
                'action': 'test',
                'format': 'json'
            })
        }

        res = resource.get('', extra_environ=environ, expect_errors=True)
        self.assertEqual(res.status_int, exc.HTTPGatewayTimeout.code)
        self.assertIn(msg_translation,
                      str(wsgi.JSONDeserializer().deserialize(res.body)))
Ejemplo n.º 2
0
    def test_unmapped_tacker_error_with_json(self):
        msg = u'\u7f51\u7edc'

        class TestException(n_exc.TackerException):
            message = msg

        expected_res = {
            'body': {
                'TackerError': {
                    'type': 'TestException',
                    'message': msg,
                    'detail': ''
                }
            }
        }
        controller = mock.MagicMock()
        controller.test.side_effect = TestException()

        resource = webtest.TestApp(wsgi_resource.Resource(controller))

        environ = {
            'wsgiorg.routing_args': (None, {
                'action': 'test',
                'format': 'json'
            })
        }
        res = resource.get('', extra_environ=environ, expect_errors=True)
        self.assertEqual(exc.HTTPInternalServerError.code, res.status_int)
        self.assertEqual(expected_res,
                         wsgi.JSONDeserializer().deserialize(res.body))
Ejemplo n.º 3
0
    def test_mapped_tacker_error_with_xml(self):
        msg = u'\u7f51\u7edc'

        class TestException(n_exc.TackerException):
            message = msg

        expected_res = {
            'body': {
                'TackerError': {
                    'type': 'TestException',
                    'message': msg,
                    'detail': ''
                }
            }
        }
        controller = mock.MagicMock()
        controller.test.side_effect = TestException()

        faults = {TestException: exc.HTTPGatewayTimeout}
        resource = webtest.TestApp(
            wsgi_resource.Resource(controller, faults=faults))

        environ = {
            'wsgiorg.routing_args': (None, {
                'action': 'test',
                'format': 'xml'
            })
        }
        res = resource.get('', extra_environ=environ, expect_errors=True)
        self.assertEqual(res.status_int, exc.HTTPGatewayTimeout.code)
        self.assertEqual(wsgi.XMLDeserializer().deserialize(res.body),
                         expected_res)
Ejemplo n.º 4
0
    def test_unhandled_error_with_json(self):
        expected_res = {
            'body': {
                'TackerError': {
                    'detail':
                    '',
                    'message':
                    _('Request Failed: internal server error'
                      ' while processing your request.'),
                    'type':
                    'HTTPInternalServerError'
                }
            }
        }
        controller = mock.MagicMock()
        controller.test.side_effect = Exception()

        resource = webtest.TestApp(wsgi_resource.Resource(controller))

        environ = {
            'wsgiorg.routing_args': (None, {
                'action': 'test',
                'format': 'json'
            })
        }
        res = resource.get('', extra_environ=environ, expect_errors=True)
        self.assertEqual(exc.HTTPInternalServerError.code, res.status_int)
        self.assertEqual(expected_res,
                         wsgi.JSONDeserializer().deserialize(res.body))
Ejemplo n.º 5
0
    def test_no_route_args(self):
        controller = mock.MagicMock()

        resource = webtest.TestApp(wsgi_resource.Resource(controller))

        environ = {}
        res = resource.get('', extra_environ=environ, expect_errors=True)
        self.assertEqual(res.status_int, exc.HTTPInternalServerError.code)
Ejemplo n.º 6
0
    def test_post_with_body(self):
        controller = mock.MagicMock()
        controller.test = lambda request, body: {'foo': 'bar'}

        resource = webtest.TestApp(wsgi_resource.Resource(controller))

        environ = {'wsgiorg.routing_args': (None, {'action': 'test'})}
        res = resource.post('', params='{"key": "val"}', extra_environ=environ)
        self.assertEqual(res.status_int, 200)
Ejemplo n.º 7
0
    def test_status_204(self):
        controller = mock.MagicMock()
        controller.test = lambda request: {'foo': 'bar'}

        resource = webtest.TestApp(wsgi_resource.Resource(controller))

        environ = {'wsgiorg.routing_args': (None, {'action': 'delete'})}
        res = resource.delete('', extra_environ=environ)
        self.assertEqual(res.status_int, 204)
Ejemplo n.º 8
0
    def test_http_error(self):
        controller = mock.MagicMock()
        controller.test.side_effect = exc.HTTPGatewayTimeout()

        resource = webtest.TestApp(wsgi_resource.Resource(controller))

        environ = {'wsgiorg.routing_args': (None, {'action': 'test'})}
        res = resource.get('', extra_environ=environ, expect_errors=True)
        self.assertEqual(res.status_int, exc.HTTPGatewayTimeout.code)
Ejemplo n.º 9
0
def create_resource(collection, resource, plugin, params, allow_bulk=False,
                    member_actions=None, parent=None, allow_pagination=False,
                    allow_sorting=False):
    controller = Controller(plugin, collection, resource, params, allow_bulk,
                            member_actions=member_actions, parent=parent,
                            allow_pagination=allow_pagination,
                            allow_sorting=allow_sorting)

    return wsgi_resource.Resource(controller, FAULT_MAP)
Ejemplo n.º 10
0
    def _make_request_with_side_effect(side_effect):
        controller = mock.MagicMock()
        controller.test.side_effect = side_effect

        resource = webtest.TestApp(wsgi_resource.Resource(controller))

        routing_args = {'action': 'test'}
        environ = {'wsgiorg.routing_args': (None, routing_args)}
        res = resource.get('', extra_environ=environ, expect_errors=True)
        return res
Ejemplo n.º 11
0
    def _test_error_log_level(self, map_webob_exc, expect_log_info=False,
                              use_fault_map=True):
        class TestException(n_exc.TackerException):
            message = 'Test Exception'

        controller = mock.MagicMock()
        controller.test.side_effect = TestException()
        faults = {TestException: map_webob_exc} if use_fault_map else {}
        resource = webtest.TestApp(wsgi_resource.Resource(controller, faults))
        environ = {'wsgiorg.routing_args': (None, {'action': 'test'})}
        with mock.patch.object(wsgi_resource, 'LOG') as log:
            res = resource.get('', extra_environ=environ, expect_errors=True)
            self.assertEqual(res.status_int, map_webob_exc.code)
        self.assertEqual(expect_log_info, log.info.called)
        self.assertNotEqual(expect_log_info, log.exception.called)
Ejemplo n.º 12
0
    def test_unmapped_tacker_error_localized(self, mock_translation):
        oslo_i18n.install('blaa')
        msg_translation = 'Translated error'
        mock_translation.return_value = msg_translation
        msg = _('Unmapped error')

        class TestException(n_exc.TackerException):
            message = msg

        controller = mock.MagicMock()
        controller.test.side_effect = TestException()
        resource = webtest.TestApp(wsgi_resource.Resource(controller))

        environ = {'wsgiorg.routing_args': (None, {'action': 'test',
                                                   'format': 'json'})}

        res = resource.get('', extra_environ=environ, expect_errors=True)
        self.assertEqual(exc.HTTPInternalServerError.code, res.status_int)
        self.assertIn(msg_translation,
                      str(wsgi.JSONDeserializer().deserialize(res.body)))