2020-06-11 00:54:34 +02:00
|
|
|
|
from http.cookies import SimpleCookie
|
2022-06-08 04:52:09 +02:00
|
|
|
|
from typing import TYPE_CHECKING, 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.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
|
2022-01-20 15:49:21 +01:00
|
|
|
|
from zerver.lib.i18n import get_browser_language_code
|
2020-06-11 00:54:34 +02:00
|
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2022-01-20 15:49:21 +01:00
|
|
|
|
from zerver.lib.test_helpers import HostRequestMock
|
2020-06-11 00:54:34 +02:00
|
|
|
|
from zerver.management.commands import makemessages
|
|
|
|
|
from zerver.models import get_realm_stream
|
|
|
|
|
|
2022-06-08 04:52:09 +02:00
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from django.test.client import _MonkeyPatchedWSGIResponse as TestHttpResponse
|
|
|
|
|
|
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)
|
2022-02-10 11:52:34 +01:00
|
|
|
|
invite_expires_in_minutes = 2 * 24 * 60
|
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(
|
2021-02-12 08:20:45 +01:00
|
|
|
|
"Danke, dass du", "post", "/accounts/find/", {"emails": hamlet.delivery_email}
|
2021-02-12 08:19:30 +01:00
|
|
|
|
)
|
|
|
|
|
check_translation(
|
|
|
|
|
"Hallo",
|
|
|
|
|
"post",
|
|
|
|
|
"/json/invites",
|
|
|
|
|
{
|
|
|
|
|
"invitee_emails": "new-email@zulip.com",
|
|
|
|
|
"stream_ids": orjson.dumps([stream.id]).decode(),
|
2022-02-10 11:52:34 +01:00
|
|
|
|
"invite_expires_in_minutes": invite_expires_in_minutes,
|
2021-02-12 08:19:30 +01:00
|
|
|
|
},
|
|
|
|
|
)
|
2018-12-14 08:41:42 +01:00
|
|
|
|
|
|
|
|
|
with self.settings(DEVELOPMENT_LOG_EMAILS=True):
|
|
|
|
|
enqueue_welcome_emails(hamlet)
|
2023-02-03 02:16:43 +01:00
|
|
|
|
# TODO: Uncomment and replace with translation once we have German translations for the strings
|
|
|
|
|
# in followup_day1 emails.
|
|
|
|
|
# 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"
|
2022-06-08 04:52:09 +02:00
|
|
|
|
def fetch(
|
|
|
|
|
self, method: str, url: str, expected_status: int, **kwargs: Any
|
|
|
|
|
) -> "TestHttpResponse":
|
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 = [
|
2021-02-12 08:20:45 +01:00
|
|
|
|
("en", "Sign up"),
|
|
|
|
|
("de", "Registrieren"),
|
|
|
|
|
("sr", "Упишите се"),
|
|
|
|
|
("zh-hans", "注册"),
|
2021-02-12 08:19:30 +01:00
|
|
|
|
]
|
2016-05-19 17:33:30 +02:00
|
|
|
|
|
|
|
|
|
for lang, word in languages:
|
2021-02-12 08:20:45 +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 = [
|
2021-02-12 08:20:45 +01:00
|
|
|
|
("en", "Sign up"),
|
|
|
|
|
("de", "Registrieren"),
|
|
|
|
|
("sr", "Упишите се"),
|
|
|
|
|
("zh-hans", "注册"),
|
2021-02-12 08:19:30 +01:00
|
|
|
|
]
|
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
|
|
|
|
|
2021-02-12 08:20:45 +01: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 = [
|
2021-02-12 08:20:45 +01:00
|
|
|
|
("en", "Sign up"),
|
|
|
|
|
("de", "Registrieren"),
|
|
|
|
|
("sr", "Упишите се"),
|
|
|
|
|
("zh-hans", "注册"),
|
2021-02-12 08:19:30 +01:00
|
|
|
|
]
|
2016-05-19 17:33:30 +02:00
|
|
|
|
|
|
|
|
|
for lang, word in languages:
|
2021-02-12 08:20:45 +01: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
|
|
|
|
|
2022-01-20 15:49:21 +01:00
|
|
|
|
def test_get_browser_language_code(self) -> None:
|
|
|
|
|
req = HostRequestMock()
|
|
|
|
|
self.assertIsNone(get_browser_language_code(req))
|
|
|
|
|
|
2022-05-12 08:28:00 +02:00
|
|
|
|
req = HostRequestMock()
|
2022-01-20 15:49:21 +01:00
|
|
|
|
req.META["HTTP_ACCEPT_LANGUAGE"] = "de"
|
|
|
|
|
self.assertEqual(get_browser_language_code(req), "de")
|
|
|
|
|
|
2022-05-12 08:28:00 +02:00
|
|
|
|
req = HostRequestMock()
|
2022-01-20 15:49:21 +01:00
|
|
|
|
req.META["HTTP_ACCEPT_LANGUAGE"] = "en-GB,en;q=0.8"
|
|
|
|
|
self.assertEqual(get_browser_language_code(req), "en-gb")
|
|
|
|
|
|
|
|
|
|
# Case when unsupported language has higher weight.
|
2022-05-12 08:28:00 +02:00
|
|
|
|
req = HostRequestMock()
|
2022-01-20 15:49:21 +01:00
|
|
|
|
req.META["HTTP_ACCEPT_LANGUAGE"] = "en-IND;q=0.9,de;q=0.8"
|
|
|
|
|
self.assertEqual(get_browser_language_code(req), "de")
|
|
|
|
|
|
|
|
|
|
# Browser locale is set to unsupported language.
|
2022-05-12 08:28:00 +02:00
|
|
|
|
req = HostRequestMock()
|
2022-01-20 15:49:21 +01:00
|
|
|
|
req.META["HTTP_ACCEPT_LANGUAGE"] = "en-IND"
|
|
|
|
|
self.assertIsNone(get_browser_language_code(req))
|
|
|
|
|
|
2022-05-12 08:28:00 +02:00
|
|
|
|
req = HostRequestMock()
|
2022-01-20 15:49:21 +01:00
|
|
|
|
req.META["HTTP_ACCEPT_LANGUAGE"] = "*"
|
|
|
|
|
self.assertIsNone(get_browser_language_code(req))
|
|
|
|
|
|
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
|
|
|
|
|
2021-02-12 08:20:45 +01: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
|
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
|
self.login("hamlet")
|
|
|
|
|
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
|
|
|
|
|
2021-02-12 08:20:45 +01: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
|
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
|
self.login("hamlet")
|
2023-01-30 00:06:53 +01:00
|
|
|
|
result = self.client_post("/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
|
|
|
|
(
|
2021-04-14 03:04:02 +02:00
|
|
|
|
"{{#tr}}english text with {variable}{{/tr}}{{/tr}}",
|
|
|
|
|
"english text with {variable}",
|
2021-02-12 08:19:30 +01:00
|
|
|
|
),
|
2021-02-12 08:20:45 +01:00
|
|
|
|
('{{t "english text" }}, "extra"}}', "english text"),
|
|
|
|
|
("{{t 'english text' }}, 'extra'}}", "english text"),
|
|
|
|
|
("{{> template var=(t 'english text') }}, '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)
|
2021-05-17 05:41:32 +02:00
|
|
|
|
self.assert_length(result, 1)
|
2016-07-15 11:02:05 +02:00
|
|
|
|
self.assertEqual(result[0], expected)
|