Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
email_change: Show deactivated page if realm is now deactivated.
Alex Vandivera2f89f21e3f0
diff --git a/zerver/tests/test_email_change.py b/zerver/tests/test_email_change.pyindex f45239e1da6..11b65a76acd 100644--- a/zerver/tests/test_email_change.py+++ b/zerver/tests/test_email_change.py@@ -11,7 +11,12 @@ from confirmation.models import (create_confirmation_link,generate_key,)-from zerver.lib.actions import do_set_realm_property, do_start_email_change_process+from zerver.lib.actions import (+ do_deactivate_realm,+ do_deactivate_user,+ do_set_realm_property,+ do_start_email_change_process,+)from zerver.lib.test_classes import ZulipTestCasefrom zerver.models import (EmailChangeStatus,@@ -92,6 +97,35 @@ class EmailChangeTestCase(ZulipTestCase):obj.refresh_from_db()self.assertEqual(obj.status, 1)+ def test_change_email_deactivated_user_realm(self) -> None:+ data = {"email": "[email protected]"}+ user_profile = self.example_user("hamlet")+ self.login_user(user_profile)+ url = "/json/settings"+ self.assert_length(mail.outbox, 0)+ result = self.client_patch(url, data)+ self.assert_length(mail.outbox, 1)+ self.assert_json_success(result)+ email_message = mail.outbox[0]+ self.assertEqual(+ email_message.subject,+ "Verify your new email address",+ )+ body = email_message.body+ self.assertIn("We received a request to change the email", body)++ activation_url = [s for s in body.split("\n") if s][2]++ do_deactivate_user(user_profile, acting_user=None)+ response = self.client_get(activation_url)+ self.assertEqual(response.status_code, 401)++ do_deactivate_realm(user_profile.realm, acting_user=None)++ response = self.client_get(activation_url)+ self.assertEqual(response.status_code, 302)+ self.assertTrue(response["Location"].endswith("/accounts/deactivated/"))+def test_start_email_change_process(self) -> None:user_profile = self.example_user("hamlet")do_start_email_change_process(user_profile, "[email protected]")diff --git a/zerver/views/user_settings.py b/zerver/views/user_settings.pyindex 68524e46016..f5feac76dce 100644--- a/zerver/views/user_settings.py+++ b/zerver/views/user_settings.py@@ -34,7 +34,7 @@ from zerver.lib.email_validation import (validate_email_is_valid,validate_email_not_already_in_realm,)-from zerver.lib.exceptions import JsonableError, RateLimited+from zerver.lib.exceptions import JsonableError, RateLimited, UserDeactivatedErrorfrom zerver.lib.i18n import get_available_language_codesfrom zerver.lib.rate_limiter import RateLimitedUserfrom zerver.lib.request import REQ, has_request_variables@@ -43,6 +43,7 @@ from zerver.lib.send_email import FromAddress, send_emailfrom zerver.lib.upload import upload_avatar_imagefrom zerver.lib.validator import check_bool, check_int, check_int_in, check_string_infrom zerver.models import UserProfile, avatar_changes_disabled, name_changes_disabled+from zerver.views.auth import redirect_to_deactivation_noticefrom zproject.backends import check_password_strength, email_belongs_to_ldapAVATAR_CHANGES_DISABLED_ERROR = gettext_lazy("Avatar changes are disabled in this organization.")@@ -58,6 +59,13 @@ def confirm_email_change(request: HttpRequest, confirmation_key: str) -> HttpResold_email = email_change_object.old_emailuser_profile = email_change_object.user_profile+ if user_profile.realm.deactivated:+ return redirect_to_deactivation_notice()++ if not user_profile.is_active:+ # TODO: Make this into a user-facing error, not JSON+ raise UserDeactivatedError()+if user_profile.realm.email_changes_disabled and not user_profile.is_realm_admin:raise JsonableError(_("Email address changes are disabled in this organization."))