2020-06-11 00:54:34 +02:00
|
|
|
|
from http.cookies import SimpleCookie
|
2016-06-04 20:28:02 +02:00
|
|
|
|
from typing import Any
|
2020-05-26 07:16:25 +02:00
|
|
|
|
from unittest import mock
|
2020-06-11 00:54:34 +02:00
|
|
|
|
|
2020-08-07 01:09:47 +02:00
|
|
|
|
import orjson
|
2016-05-19 17:33:30 +02:00
|
|
|
|
from django.conf import settings
|
2018-12-14 08:41:42 +01:00
|
|
|
|
from django.core import mail
|
2020-06-11 00:54:34 +02:00
|
|
|
|
from django.http import HttpResponse
|
|
|
|
|
from django.utils import translation
|
2016-05-19 17:33:30 +02:00
|
|
|
|
|
2019-03-15 18:51:39 +01:00
|
|
|
|
from zerver.lib.email_notifications import enqueue_welcome_emails
|
2020-06-11 00:54:34 +02:00
|
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
|
|
|
|
from zerver.management.commands import makemessages
|
|
|
|
|
from zerver.models import get_realm_stream
|
|
|
|
|
|
2016-06-01 14:58:54 +02:00
|
|
|
|
|
2018-12-14 08:41:42 +01:00
|
|
|
|
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)
|
2018-12-19 19:09:56 +01:00
|
|
|
|
elif request_type == "patch": # nocoverage: see comment below
|
2018-12-14 08:41:42 +01:00
|
|
|
|
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()
|
2018-12-22 05:41:54 +01:00
|
|
|
|
stream = get_realm_stream("Denmark", realm.id)
|
2020-03-06 18:40:46 +01:00
|
|
|
|
self.login_user(hamlet)
|
2018-12-14 08:41:42 +01:00
|
|
|
|
|
2018-12-19 19:09:56 +01:00
|
|
|
|
# TODO: Uncomment and replace with translation once we have German translations for the strings
|
|
|
|
|
# in confirm_new_email.txt.
|
|
|
|
|
# Also remove the "nocoverage" from check_translation above.
|
|
|
|
|
# check_translation("Viele Grüße", "patch", "/json/settings", {"email": "hamlets-new@zulip.com"})
|
2021-02-12 08:19:30 +01:00
|
|
|
|
check_translation(
|
|
|
|
|
"Incrível!",
|
|
|
|
|
"post",
|
|
|
|
|
"/accounts/home/",
|
|
|
|
|
{"email": "new-email@zulip.com"},
|
|
|
|
|
HTTP_ACCEPT_LANGUAGE="pt",
|
|
|
|
|
)
|
|
|
|
|
check_translation(
|
|
|
|
|
"Danke, dass du", "post", '/accounts/find/', {'emails': hamlet.delivery_email}
|
|
|
|
|
)
|
|
|
|
|
check_translation(
|
|
|
|
|
"Hallo",
|
|
|
|
|
"post",
|
|
|
|
|
"/json/invites",
|
|
|
|
|
{
|
|
|
|
|
"invitee_emails": "new-email@zulip.com",
|
|
|
|
|
"stream_ids": orjson.dumps([stream.id]).decode(),
|
|
|
|
|
},
|
|
|
|
|
)
|
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
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
2016-08-23 02:08:42 +02:00
|
|
|
|
class TranslationTestCase(ZulipTestCase):
|
2016-05-19 17:33:30 +02:00
|
|
|
|
"""
|
2020-03-28 01:25:56 +01:00
|
|
|
|
Translations strings should change with locale. URLs should be locale
|
2016-05-19 17:33:30 +02:00
|
|
|
|
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)
|
2019-10-18 16:11:48 +02:00
|
|
|
|
super().tearDown()
|
2017-05-24 09:56:05 +02:00
|
|
|
|
|
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)
|
2021-02-12 08:19:30 +01:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
response.status_code,
|
|
|
|
|
expected_status,
|
|
|
|
|
msg=f"Expected {expected_status}, received {response.status_code} for {method} to {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:
|
2021-02-12 08:19:30 +01:00
|
|
|
|
languages = [
|
|
|
|
|
('en', 'Sign up'),
|
|
|
|
|
('de', 'Registrieren'),
|
|
|
|
|
('sr', 'Упишите се'),
|
|
|
|
|
('zh-hans', '注册'),
|
|
|
|
|
]
|
2016-05-19 17:33:30 +02:00
|
|
|
|
|
|
|
|
|
for lang, word in languages:
|
2021-02-12 08:19:30 +01:00
|
|
|
|
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
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
|
def test_cookie(self) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
|
languages = [
|
|
|
|
|
('en', 'Sign up'),
|
|
|
|
|
('de', 'Registrieren'),
|
|
|
|
|
('sr', 'Упишите се'),
|
|
|
|
|
('zh-hans', '注册'),
|
|
|
|
|
]
|
2016-05-19 17:33:30 +02:00
|
|
|
|
|
|
|
|
|
for lang, word in languages:
|
2020-10-23 02:43:28 +02:00
|
|
|
|
# Applying str function to LANGUAGE_COOKIE_NAME to convert Unicode
|
2016-11-08 11:36:32 +01:00
|
|
|
|
# into an ascii otherwise SimpleCookie will raise an exception
|
2019-08-10 00:30:34 +02:00
|
|
|
|
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:
|
2021-02-12 08:19:30 +01:00
|
|
|
|
languages = [
|
|
|
|
|
('en', 'Sign up'),
|
|
|
|
|
('de', 'Registrieren'),
|
|
|
|
|
('sr', 'Упишите се'),
|
|
|
|
|
('zh-hans', '注册'),
|
|
|
|
|
]
|
2016-05-19 17:33:30 +02:00
|
|
|
|
|
|
|
|
|
for lang, word in languages:
|
2020-06-09 00:25:09 +02:00
|
|
|
|
response = self.fetch('get', f'/{lang}/integrations/', 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)
|
2019-10-18 16:11:48 +02:00
|
|
|
|
super().tearDown()
|
2017-05-24 09:56:05 +02:00
|
|
|
|
|
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
|
|
|
|
|
|
2020-03-06 18:40:46 +01:00
|
|
|
|
self.login('hamlet')
|
2021-02-12 08:19:30 +01:00
|
|
|
|
result = self.client_post("/json/invites", HTTP_ACCEPT_LANGUAGE='de')
|
2016-06-01 14:58:54 +02:00
|
|
|
|
|
2020-04-09 21:51:58 +02:00
|
|
|
|
expected_error = "this arg is bad: 'invitee_emails' (translated to German)"
|
2021-02-12 08:19:30 +01:00
|
|
|
|
self.assert_json_error_contains(result, expected_error, status_code=400)
|
2016-06-01 14:58:54 +02:00
|
|
|
|
|
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
|
|
|
|
|
|
2020-03-06 18:40:46 +01:00
|
|
|
|
self.login('hamlet')
|
2016-07-28 00:38:45 +02:00
|
|
|
|
result = self.client_get("/de/accounts/login/jwt/")
|
2016-06-01 14:58:54 +02:00
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
self.assert_json_error_contains(result, dummy_value, status_code=400)
|
2016-07-15 11:02:05 +02:00
|
|
|
|
|
|
|
|
|
|
2020-07-01 04:19:54 +02:00
|
|
|
|
class FrontendRegexTestCase(ZulipTestCase):
|
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 = [
|
2021-02-12 08:19:30 +01:00
|
|
|
|
(
|
|
|
|
|
'{{#tr context}}english text with __variable__{{/tr}}{{/tr}}',
|
|
|
|
|
'english text with __variable__',
|
|
|
|
|
),
|
|
|
|
|
('{{t "english text" }}, "extra"}}', 'english text'),
|
|
|
|
|
("{{t 'english text' }}, 'extra'}}", 'english text'),
|
|
|
|
|
("{{> template var=(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'),
|
2016-07-15 11:02:05 +02:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
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)
|