2016-05-19 17:33:30 +02:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
2016-06-04 20:28:02 +02:00
|
|
|
|
from typing import Any
|
|
|
|
|
|
2016-06-01 14:58:54 +02:00
|
|
|
|
import mock
|
2016-05-19 17:33:30 +02:00
|
|
|
|
from django.test import TestCase
|
|
|
|
|
from django.conf import settings
|
2016-06-04 20:28:02 +02:00
|
|
|
|
from django.http import HttpResponse
|
2016-07-05 06:18:59 +02:00
|
|
|
|
from six.moves.http_cookies import SimpleCookie
|
2016-05-19 17:33:30 +02:00
|
|
|
|
|
2016-08-23 02:08:42 +02:00
|
|
|
|
from zerver.lib.test_helpers import ZulipTestCase
|
2016-07-15 11:02:05 +02:00
|
|
|
|
from zerver.management.commands import makemessages
|
2016-06-01 14:58:54 +02:00
|
|
|
|
|
2016-05-19 17:33:30 +02:00
|
|
|
|
|
2016-08-23 02:08:42 +02:00
|
|
|
|
class TranslationTestCase(ZulipTestCase):
|
2016-05-19 17:33:30 +02:00
|
|
|
|
"""
|
|
|
|
|
Tranlations strings should change with locale. URLs should be locale
|
|
|
|
|
aware.
|
|
|
|
|
"""
|
|
|
|
|
|
2016-07-28 00:30:22 +02:00
|
|
|
|
# e.g. self.client_post(url) if method is "post"
|
2016-05-19 17:33:30 +02:00
|
|
|
|
def fetch(self, method, url, expected_status, **kwargs):
|
2016-06-04 20:28:02 +02:00
|
|
|
|
# type: (str, str, int, **Any) -> HttpResponse
|
2016-05-19 17:33:30 +02:00
|
|
|
|
response = getattr(self.client, method)(url, **kwargs)
|
|
|
|
|
self.assertEqual(response.status_code, expected_status,
|
|
|
|
|
msg="Expected %d, received %d for %s to %s" % (
|
|
|
|
|
expected_status, response.status_code, method, url))
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
def test_accept_language_header(self):
|
2016-06-04 20:28:02 +02:00
|
|
|
|
# type: () -> None
|
2016-07-12 15:41:45 +02:00
|
|
|
|
languages = [('en', u'Register'),
|
|
|
|
|
('de', u'Registrieren'),
|
|
|
|
|
('sr', u'Региструј се'),
|
|
|
|
|
('zh-cn', u'注册'),
|
2016-05-19 17:33:30 +02:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
for lang, word in languages:
|
|
|
|
|
response = self.fetch('get', '/integrations/', 200,
|
|
|
|
|
HTTP_ACCEPT_LANGUAGE=lang)
|
2016-07-12 15:41:45 +02:00
|
|
|
|
self.assert_in_response(word, response)
|
2016-05-19 17:33:30 +02:00
|
|
|
|
|
|
|
|
|
def test_cookie(self):
|
2016-06-04 20:28:02 +02:00
|
|
|
|
# type: () -> None
|
2016-07-12 15:41:45 +02:00
|
|
|
|
languages = [('en', u'Register'),
|
|
|
|
|
('de', u'Registrieren'),
|
|
|
|
|
('sr', u'Региструј се'),
|
|
|
|
|
('zh-cn', u'注册'),
|
2016-05-19 17:33:30 +02:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
for lang, word in languages:
|
2016-07-05 06:18:59 +02:00
|
|
|
|
self.client.cookies = SimpleCookie({settings.LANGUAGE_COOKIE_NAME: lang}) # type: ignore # SimpleCookie has incomplete stubs in python 3
|
2016-05-19 17:33:30 +02:00
|
|
|
|
|
|
|
|
|
response = self.fetch('get', '/integrations/', 200)
|
2016-07-12 15:41:45 +02:00
|
|
|
|
self.assert_in_response(word, response)
|
2016-05-19 17:33:30 +02:00
|
|
|
|
|
|
|
|
|
def test_i18n_urls(self):
|
2016-06-04 20:28:02 +02:00
|
|
|
|
# type: () -> None
|
2016-07-12 15:41:45 +02:00
|
|
|
|
languages = [('en', u'Register'),
|
|
|
|
|
('de', u'Registrieren'),
|
|
|
|
|
('sr', u'Региструј се'),
|
|
|
|
|
('zh-cn', u'注册'),
|
2016-05-19 17:33:30 +02:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
for lang, word in languages:
|
|
|
|
|
response = self.fetch('get', '/{}/integrations/'.format(lang), 200)
|
2016-07-12 15:41:45 +02:00
|
|
|
|
self.assert_in_response(word, response)
|
2016-06-01 14:58:54 +02:00
|
|
|
|
|
|
|
|
|
|
2016-08-23 02:08:42 +02:00
|
|
|
|
class JsonTranslationTestCase(ZulipTestCase):
|
2016-06-01 14:58:54 +02:00
|
|
|
|
@mock.patch('zerver.lib.request._')
|
|
|
|
|
def test_json_error(self, mock_gettext):
|
2016-07-30 00:31:35 +02:00
|
|
|
|
# type: (Any) -> None
|
2016-06-01 14:58:54 +02:00
|
|
|
|
dummy_value = "Some other language '%s'"
|
|
|
|
|
mock_gettext.return_value = dummy_value
|
|
|
|
|
|
|
|
|
|
self.login("hamlet@zulip.com")
|
2016-07-28 00:30:22 +02:00
|
|
|
|
result = self.client_post("/json/refer_friend",
|
2016-06-01 14:58:54 +02:00
|
|
|
|
HTTP_ACCEPT_LANGUAGE='de')
|
|
|
|
|
|
|
|
|
|
self.assert_json_error_contains(result,
|
|
|
|
|
dummy_value % 'email',
|
|
|
|
|
status_code=400)
|
|
|
|
|
|
2016-10-12 04:50:38 +02:00
|
|
|
|
@mock.patch('zerver.views.auth._')
|
2016-06-01 14:58:54 +02:00
|
|
|
|
def test_jsonable_error(self, mock_gettext):
|
2016-07-30 00:31:35 +02:00
|
|
|
|
# type: (Any) -> None
|
2016-06-01 14:58:54 +02:00
|
|
|
|
dummy_value = "Some other language"
|
|
|
|
|
mock_gettext.return_value = dummy_value
|
|
|
|
|
|
|
|
|
|
self.login("hamlet@zulip.com")
|
2016-07-28 00:38:45 +02:00
|
|
|
|
result = self.client_get("/de/accounts/login/jwt/")
|
2016-06-01 14:58:54 +02:00
|
|
|
|
|
|
|
|
|
self.assert_json_error_contains(result,
|
|
|
|
|
dummy_value,
|
|
|
|
|
status_code=400)
|
2016-07-15 11:02:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FrontendRegexTestCase(TestCase):
|
|
|
|
|
def test_regexes(self):
|
2016-07-30 00:31:35 +02:00
|
|
|
|
# type: () -> None
|
2016-07-15 11:02:05 +02:00
|
|
|
|
command = makemessages.Command()
|
|
|
|
|
|
|
|
|
|
data = [
|
|
|
|
|
('{{#tr context}}english text with __variable__{{/tr}}{{/tr}}',
|
|
|
|
|
'english text with __variable__'),
|
|
|
|
|
|
|
|
|
|
('{{t "english text" }}, "extra"}}',
|
|
|
|
|
'english text'),
|
|
|
|
|
|
|
|
|
|
("{{t 'english text' }}, 'extra'}}",
|
|
|
|
|
'english text'),
|
|
|
|
|
|
|
|
|
|
('i18n.t("english text"), "extra",)',
|
|
|
|
|
'english text'),
|
|
|
|
|
|
|
|
|
|
('i18n.t("english text", context), "extra",)',
|
|
|
|
|
'english text'),
|
|
|
|
|
|
|
|
|
|
("i18n.t('english text'), 'extra',)",
|
|
|
|
|
'english text'),
|
|
|
|
|
|
|
|
|
|
("i18n.t('english text', context), 'extra',)",
|
|
|
|
|
'english text'),
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
for input_text, expected in data:
|
|
|
|
|
result = list(command.extract_strings(input_text).keys())
|
|
|
|
|
self.assertEqual(len(result), 1)
|
|
|
|
|
self.assertEqual(result[0], expected)
|