2016-05-19 17:33:30 +02:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
2016-06-04 20:28:02 +02:00
|
|
|
|
from typing import Any
|
|
|
|
|
|
2016-11-08 11:36:32 +01:00
|
|
|
|
import django
|
2016-06-01 14:58:54 +02:00
|
|
|
|
import mock
|
2016-05-19 17:33:30 +02:00
|
|
|
|
from django.test import TestCase
|
2017-05-24 09:56:05 +02:00
|
|
|
|
from django.utils import translation
|
2016-05-19 17:33:30 +02:00
|
|
|
|
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-11-10 19:30:09 +01:00
|
|
|
|
from zerver.lib.test_classes 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.
|
|
|
|
|
"""
|
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
|
def tearDown(self) -> None:
|
2017-05-24 09:56:05 +02:00
|
|
|
|
translation.activate(settings.LANGUAGE_CODE)
|
|
|
|
|
|
2016-07-28 00:30:22 +02:00
|
|
|
|
# e.g. self.client_post(url) if method is "post"
|
2017-11-05 10:51:25 +01:00
|
|
|
|
def fetch(self, method: str, url: str, expected_status: int, **kwargs: 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" % (
|
2017-01-24 07:06:13 +01:00
|
|
|
|
expected_status, response.status_code, method, url))
|
2016-05-19 17:33:30 +02:00
|
|
|
|
return response
|
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
|
def test_accept_language_header(self) -> None:
|
2016-07-12 15:41:45 +02:00
|
|
|
|
languages = [('en', u'Register'),
|
|
|
|
|
('de', u'Registrieren'),
|
|
|
|
|
('sr', u'Региструј се'),
|
2017-10-06 19:06:41 +02:00
|
|
|
|
('zh-hans', u'注册'),
|
2016-05-19 17:33:30 +02:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
for lang, word in languages:
|
2017-10-06 19:06:41 +02:00
|
|
|
|
response = self.fetch('get', '/integrations/', 200,
|
2016-05-19 17:33:30 +02:00
|
|
|
|
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
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
|
def test_cookie(self) -> None:
|
2016-07-12 15:41:45 +02:00
|
|
|
|
languages = [('en', u'Register'),
|
|
|
|
|
('de', u'Registrieren'),
|
|
|
|
|
('sr', u'Региструј се'),
|
2017-10-06 19:06:41 +02:00
|
|
|
|
('zh-hans', u'注册'),
|
2016-05-19 17:33:30 +02:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
for lang, word in languages:
|
2016-11-08 11:36:32 +01:00
|
|
|
|
# Applying str function to LANGUAGE_COOKIE_NAME to convert unicode
|
|
|
|
|
# into an ascii otherwise SimpleCookie will raise an exception
|
|
|
|
|
self.client.cookies = SimpleCookie({str(settings.LANGUAGE_COOKIE_NAME): lang})
|
2016-05-19 17:33:30 +02:00
|
|
|
|
|
2017-10-06 19:06:41 +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
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
|
def test_i18n_urls(self) -> None:
|
2016-07-12 15:41:45 +02:00
|
|
|
|
languages = [('en', u'Register'),
|
|
|
|
|
('de', u'Registrieren'),
|
|
|
|
|
('sr', u'Региструј се'),
|
2017-10-06 19:06:41 +02:00
|
|
|
|
('zh-hans', u'注册'),
|
2016-05-19 17:33:30 +02:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
for lang, word in languages:
|
2017-10-06 19:06:41 +02:00
|
|
|
|
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):
|
2017-11-05 10:51:25 +01:00
|
|
|
|
def tearDown(self) -> None:
|
2017-05-24 09:56:05 +02:00
|
|
|
|
translation.activate(settings.LANGUAGE_CODE)
|
|
|
|
|
|
2016-06-01 14:58:54 +02:00
|
|
|
|
@mock.patch('zerver.lib.request._')
|
2017-11-05 10:51:25 +01:00
|
|
|
|
def test_json_error(self, mock_gettext: Any) -> None:
|
2017-07-21 02:17:28 +02:00
|
|
|
|
dummy_value = "this arg is bad: '{var_name}' (translated to German)"
|
2016-06-01 14:58:54 +02:00
|
|
|
|
mock_gettext.return_value = dummy_value
|
|
|
|
|
|
2017-05-24 05:08:49 +02:00
|
|
|
|
email = self.example_email('hamlet')
|
|
|
|
|
self.login(email)
|
2017-07-31 20:55:57 +02:00
|
|
|
|
result = self.client_post("/json/invites",
|
2016-06-01 14:58:54 +02:00
|
|
|
|
HTTP_ACCEPT_LANGUAGE='de')
|
|
|
|
|
|
2017-07-06 16:58:55 +02:00
|
|
|
|
expected_error = u"this arg is bad: 'invitee_emails' (translated to German)"
|
2016-06-01 14:58:54 +02:00
|
|
|
|
self.assert_json_error_contains(result,
|
2017-07-06 16:58:55 +02:00
|
|
|
|
expected_error,
|
2016-06-01 14:58:54 +02:00
|
|
|
|
status_code=400)
|
|
|
|
|
|
2016-10-12 04:50:38 +02:00
|
|
|
|
@mock.patch('zerver.views.auth._')
|
2017-11-05 10:51:25 +01:00
|
|
|
|
def test_jsonable_error(self, mock_gettext: Any) -> None:
|
2016-06-01 14:58:54 +02:00
|
|
|
|
dummy_value = "Some other language"
|
|
|
|
|
mock_gettext.return_value = dummy_value
|
|
|
|
|
|
2017-05-24 05:08:49 +02:00
|
|
|
|
email = self.example_email('hamlet')
|
|
|
|
|
self.login(email)
|
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):
|
2017-11-05 10:51:25 +01:00
|
|
|
|
def test_regexes(self) -> 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:
|
2017-10-05 09:46:56 +02:00
|
|
|
|
result = command.extract_strings(input_text)
|
2016-07-15 11:02:05 +02:00
|
|
|
|
self.assertEqual(len(result), 1)
|
|
|
|
|
self.assertEqual(result[0], expected)
|