From 27be27560b54f553729c24dc245fd892654f72a5 Mon Sep 17 00:00:00 2001 From: Zixuan James Li <359101898@qq.com> Date: Mon, 30 May 2022 00:18:57 -0400 Subject: [PATCH] typing: Access LANGUAGE_CODE via `django.utils.translation.get_language`. We no longer need to access the internal `LANGUAGE_CODE` attribute by using `django.utils.translation.get_language`. A test case overriding the translation is added to ensure the password reset form sending to users requested from a wrong domain is properly translated. This is a part of django-stubs refactorings. Signed-off-by: Zixuan James Li --- zerver/forms.py | 4 +++- zerver/tests/test_signup.py | 22 +++++++++++++++++++++- zerver/views/registration.py | 3 ++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/zerver/forms.py b/zerver/forms.py index 79b9c30f99..36702761ba 100644 --- a/zerver/forms.py +++ b/zerver/forms.py @@ -13,6 +13,7 @@ from django.core.validators import validate_email from django.http import HttpRequest from django.urls import reverse from django.utils.http import urlsafe_base64_encode +from django.utils.translation import get_language from django.utils.translation import gettext as _ from django.utils.translation import gettext_lazy from markupsafe import Markup as mark_safe @@ -382,7 +383,8 @@ class ZulipPasswordResetForm(PasswordResetForm): ) if active_accounts_in_other_realms: context["active_accounts_in_other_realms"] = active_accounts_in_other_realms - language = request.LANGUAGE_CODE + language = get_language() + send_email( "zerver/emails/password_reset", to_emails=[email], diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.py index c7e00ffe14..ab81bc9abc 100644 --- a/zerver/tests/test_signup.py +++ b/zerver/tests/test_signup.py @@ -14,7 +14,9 @@ from django.core.exceptions import ValidationError from django.http import HttpRequest, HttpResponse from django.test import Client, override_settings from django.urls import reverse +from django.utils import translation from django.utils.timezone import now as timezone_now +from django.utils.translation import gettext as _ from confirmation import settings as confirmation_settings from confirmation.models import ( @@ -378,9 +380,10 @@ class PasswordResetTest(ZulipTestCase): [message] = outbox self.assertEqual(self.email_envelope_from(message), settings.NOREPLY_EMAIL_ADDRESS) + # The email might be sent in different languages for i18n testing self.assertRegex( self.email_display_from(message), - rf"^Zulip Account Security <{self.TOKENIZED_NOREPLY_REGEX}>\Z", + rf'^{_("Zulip Account Security")} <{self.TOKENIZED_NOREPLY_REGEX}>\Z', ) self.assertIn(f"{subdomain}.testserver", message.extra_headers["List-Id"]) @@ -659,6 +662,23 @@ class PasswordResetTest(ZulipTestCase): self.assertNotIn("reset your password", body) self.assertNotIn("deactivated", body) + def test_wrong_subdomain_i18n(self) -> None: + user_profile = self.example_user("hamlet") + email = user_profile.delivery_email + + # Send a password reset request with a different language to a wrong subdomain + result = self.client_post( + "/accounts/password/reset/", + {"email": email}, + HTTP_ACCEPT_LANGUAGE="de", + subdomain="lear", + ) + self.assertEqual(result.status_code, 302) + + with translation.override("de"): + body = self.get_reset_mail_body("lear") + self.assertIn("hat ein neues Passwort", body) + def test_invalid_subdomain(self) -> None: email = self.example_email("hamlet") diff --git a/zerver/views/registration.py b/zerver/views/registration.py index 6d152c3e66..ce051bd612 100644 --- a/zerver/views/registration.py +++ b/zerver/views/registration.py @@ -12,6 +12,7 @@ from django.db.models import Q from django.http import HttpRequest, HttpResponse, HttpResponseRedirect from django.shortcuts import redirect, render from django.urls import reverse +from django.utils.translation import get_language from django.utils.translation import gettext as _ from django_auth_ldap.backend import LDAPBackend, _LDAPUser @@ -581,7 +582,7 @@ def send_confirm_registration_email( "zerver/emails/confirm_registration", to_emails=[email], from_address=FromAddress.tokenized_no_reply_address(), - language=request.LANGUAGE_CODE if request is not None else None, + language=get_language() if request is not None else None, context={ "create_realm": (realm is None), "activate_url": activation_url,