def test_raise_invalid_with_localized_explanation(self, mock_translation):
        msg_template = gettextutils.Message("Invalid input: %(reason)s", "")
        reason = gettextutils.Message("Value is invalid", "")

        class MockESTranslations(gettext.GNUTranslations):
            def ugettext(self, msgid):
                if "Invalid input" in msgid:
                    return "Entrada invalida: %(reason)s"
                elif "Value is invalid" in msgid:
                    return "El valor es invalido"
                return msgid

        def translation(domain, localedir=None, languages=None, fallback=None):
            return MockESTranslations()

        mock_translation.side_effect = translation

        @webob.dec.wsgify
        def raiser(req):
            class MyInvalidInput(exception.InvalidInput):
                message = msg_template

            ex = MyInvalidInput(reason=reason)
            raise wsgi.Fault(exception.ConvertedException(code=ex.code,
                                                          explanation=ex.msg))

        req = webob.Request.blank("/.json")
        resp = req.get_response(raiser)
        self.assertEqual(resp.content_type, "application/json")
        self.assertEqual(resp.status_int, 400)
        # This response was comprised of Message objects from two different
        # exceptions, here we are testing that both got translated
        self.assertIn("Entrada invalida: El valor es invalido", resp.body)
Ejemplo n.º 2
0
    def test_raise_http_with_localized_explanation(self):
        params = ('blah', )
        expl = gettextutils.Message("String with params: %s" % params, 'test')

        def _mock_translation(msg, locale):
            return "Mensaje traducido"

        self.stubs.Set(gettextutils, "translate", _mock_translation)

        @webob.dec.wsgify
        def raiser(req):
            raise wsgi.Fault(webob.exc.HTTPNotFound(explanation=expl))

        req = webob.Request.blank('/.xml')
        resp = req.get_response(raiser)
        self.assertEqual(resp.content_type, "application/xml")
        self.assertEqual(resp.status_int, 404)
        self.assertIn(("Mensaje traducido"), resp.body)
        self.stubs.UnsetAll()
Ejemplo n.º 3
0
    def test_cinder_exception_with_localized_explanation(self, mock_t9n):
        msg = 'My Not Found'
        msg_translation = 'Mi No Encontrado'
        message = gettextutils.Message(msg, '')

        @webob.dec.wsgify
        def fail(req):
            class MyVolumeNotFound(exception.NotFound):
                def __init__(self):
                    self.msg = message
                    self.safe = True

            raise MyVolumeNotFound()

        # Test response without localization
        def mock_get_non_localized_message(msgid, locale):
            return msg

        mock_t9n.side_effect = mock_get_non_localized_message

        api = self._wsgi_app(fail)
        resp = webob.Request.blank('/').get_response(api)
        self.assertEqual(404, resp.status_int)
        self.assertIn(msg, resp.body)

        # Test response with localization
        def mock_translate(msgid, locale):
            if isinstance(msgid, gettextutils.Message):
                return msg_translation
            return msgid

        mock_t9n.side_effect = mock_translate

        api = self._wsgi_app(fail)
        resp = webob.Request.blank('/').get_response(api)
        self.assertEqual(404, resp.status_int)
        self.assertIn(msg_translation, resp.body)