Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
user_settings: Flush caches keyed by old email on visibility change.
Ayan Alamd1411a37dac6
diff --git a/zerver/actions/user_settings.py b/zerver/actions/user_settings.pyindex 23590030026..9e759a64975 100644--- a/zerver/actions/user_settings.py+++ b/zerver/actions/user_settings.py@@ -579,6 +579,9 @@ def bulk_change_user_setting(# that field, so we can save work and return here.return+ # The other cache flushes for this change only see the new+ # email address, so flush the caches keyed by the old one.+ delete_user_profile_caches([user_profile], user_profile.realm_id)user_profile.email = get_display_email_address(user_profile)user_profile.save(update_fields=["email"])diff --git a/zerver/tests/test_presence.py b/zerver/tests/test_presence.pyindex 05aba795a13..4f50fd5878f 100644--- a/zerver/tests/test_presence.py+++ b/zerver/tests/test_presence.py@@ -681,6 +681,50 @@ class SingleUserPresenceTests(ZulipTestCase):result = self.client_get("/json/users/[email protected]/presence")self.assert_json_error(result, "No presence data for [email protected]")+ def test_email_access_after_email_address_visibility_change(self) -> None:+ self.login("hamlet")++ othello = self.example_user("othello")+ self.api_post(othello, "/api/v1/users/me/presence", {"status": "active"})+ do_change_user_setting(+ othello,+ "email_address_visibility",+ UserProfile.EMAIL_ADDRESS_VISIBILITY_EVERYONE,+ acting_user=None,+ )+ real_email = othello.delivery_email+ self.assertEqual(othello.email, real_email)++ # This caches the lookup of othello by the real email address.+ result = self.client_get(f"/json/users/{real_email}/presence")+ self.assert_json_success(result)++ do_change_user_setting(+ othello,+ "email_address_visibility",+ UserProfile.EMAIL_ADDRESS_VISIBILITY_ADMINS,+ acting_user=None,+ )+ dummy_email = othello.email+ self.assertNotEqual(dummy_email, real_email)++ result = self.client_get(f"/json/users/{real_email}/presence")+ self.assert_json_error(result, "No such user")+ result = self.client_get(f"/json/users/{dummy_email}/presence")+ self.assert_json_success(result)++ do_change_user_setting(+ othello,+ "email_address_visibility",+ UserProfile.EMAIL_ADDRESS_VISIBILITY_EVERYONE,+ acting_user=None,+ )++ result = self.client_get(f"/json/users/{dummy_email}/presence")+ self.assert_json_error(result, "No such user")+ result = self.client_get(f"/json/users/{real_email}/presence")+ self.assert_json_success(result)+def test_single_user_get(self) -> None:reset_email_visibility_to_everyone_in_zulip_realm()diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.pyindex 549f33b4606..da160ad123f 100644--- a/zerver/tests/test_users.py+++ b/zerver/tests/test_users.py@@ -3322,6 +3322,41 @@ class GetProfileTest(ZulipTestCase):self.assert_length(cache_queries, 1)self.assertEqual(user_profile.email, email)+ def test_cache_behavior_after_email_address_visibility_change(self) -> None:+ hamlet = self.example_user("hamlet")+ realm = hamlet.realm+ do_change_user_setting(+ hamlet,+ "email_address_visibility",+ UserProfile.EMAIL_ADDRESS_VISIBILITY_EVERYONE,+ acting_user=None,+ )+ real_email = hamlet.delivery_email+ self.assertEqual(hamlet.email, real_email)+ self.assertEqual(get_user(real_email, realm), hamlet)++ do_change_user_setting(+ hamlet,+ "email_address_visibility",+ UserProfile.EMAIL_ADDRESS_VISIBILITY_ADMINS,+ acting_user=None,+ )+ dummy_email = hamlet.email+ self.assertNotEqual(dummy_email, real_email)+ with self.assertRaises(UserProfile.DoesNotExist):+ get_user(real_email, realm)+ self.assertEqual(get_user(dummy_email, realm), hamlet)++ do_change_user_setting(+ hamlet,+ "email_address_visibility",+ UserProfile.EMAIL_ADDRESS_VISIBILITY_EVERYONE,+ acting_user=None,+ )+ with self.assertRaises(UserProfile.DoesNotExist):+ get_user(dummy_email, realm)+ self.assertEqual(get_user(real_email, realm), hamlet)+def test_get_user_profile(self) -> None:hamlet = self.example_user("hamlet")iago = self.example_user("iago")