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
|
2018-12-14 08:41:42 +01:00
|
|
|
|
from django.core import mail
|
2018-05-10 05:43:44 +02:00
|
|
|
|
from 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
|
2018-12-14 08:41:42 +01:00
|
|
|
|
from zerver.lib.notifications import enqueue_welcome_emails
|
2016-06-01 14:58:54 +02:00
|
|
|
|
|
2018-12-14 08:41:42 +01:00
|
|
|
|
from django.utils.timezone import now as timezone_now
|
|
|
|
|
|
|
|
|
|
class EmailTranslationTestCase(ZulipTestCase):
|
|
|
|
|
def test_email_translation(self) -> None:
|
|
|
|
|
def check_translation(phrase: str, request_type: str, *args: Any, **kwargs: Any) -> None:
|
|
|
|
|
if request_type == "post":
|
|
|
|
|
self.client_post(*args, **kwargs)
|
|
|
|
|
elif request_type == "patch":
|
|
|
|
|
self.client_patch(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
email_message = mail.outbox[0]
|
|
|
|
|
self.assertIn(phrase, email_message.body)
|
|
|
|
|
|
|
|
|
|
for i in range(len(mail.outbox)):
|
|
|
|
|
mail.outbox.pop()
|
|
|
|
|
|
|
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
|
hamlet.default_language = "de"
|
|
|
|
|
hamlet.save()
|
|
|
|
|
realm = hamlet.realm
|
|
|
|
|
realm.default_language = "de"
|
|
|
|
|
realm.save()
|
|
|
|
|
|
|
|
|
|
self.login(hamlet.email)
|
|
|
|
|
|
|
|
|
|
check_translation("Viele Grüße", "patch", "/json/settings", {"email": "hamlets-new@zulip.com"})
|
2018-12-19 19:47:58 +01:00
|
|
|
|
check_translation("Incrível!", "post", "/accounts/home/", {"email": "new-email@zulip.com"}, HTTP_ACCEPT_LANGUAGE="pt")
|
2018-12-14 08:41:42 +01:00
|
|
|
|
check_translation("Danke, dass Du", "post", '/accounts/find/', {'emails': hamlet.email})
|
2018-12-19 23:47:27 +01:00
|
|
|
|
check_translation("Hallo", "post", "/json/invites", {"invitee_emails": "new-email@zulip.com", "stream": ["Denmark"]})
|
2018-12-14 08:41:42 +01:00
|
|
|
|
|
|
|
|
|
with self.settings(DEVELOPMENT_LOG_EMAILS=True):
|
|
|
|
|
enqueue_welcome_emails(hamlet)
|
|
|
|
|
check_translation("Viele Grüße", "")
|
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:
|
2017-12-14 06:46:57 +01:00
|
|
|
|
languages = [('en', u'Sign up'),
|
2016-07-12 15:41:45 +02:00
|
|
|
|
('de', u'Registrieren'),
|
2017-12-14 06:46:57 +01:00
|
|
|
|
('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:
|
2017-12-14 06:46:57 +01:00
|
|
|
|
languages = [('en', u'Sign up'),
|
2016-07-12 15:41:45 +02:00
|
|
|
|
('de', u'Registrieren'),
|
2017-12-14 06:46:57 +01:00
|
|
|
|
('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
|
2018-05-10 05:43:44 +02:00
|
|
|
|
self.client.cookies = SimpleCookie({str(settings.LANGUAGE_COOKIE_NAME): lang}) # type: ignore # https://github.com/python/typeshed/issues/1476
|
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:
|
2017-12-14 06:46:57 +01:00
|
|
|
|
languages = [('en', u'Sign up'),
|
2016-07-12 15:41:45 +02:00
|
|
|
|
('de', u'Registrieren'),
|
2017-12-14 06:46:57 +01:00
|
|
|
|
('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)
|