From 3950a8e19d811f59fd30add33cacfcdc2c523c44 Mon Sep 17 00:00:00 2001 From: Sahil Batra Date: Wed, 1 Mar 2023 12:04:25 +0530 Subject: [PATCH] test_helpers: Rename reset_emails_in_zulip_realm. This commit renames reset_emails_in_zulip_realm function to reset_email_visibility_to_everyone_in_zulip_realm which makes it more clear to understand what the function actually does. This commit also adds a comment explaining what this function does. --- analytics/tests/test_support_views.py | 4 ++-- tools/test-api | 4 ++-- zerver/lib/test_helpers.py | 9 ++++++++- zerver/tests/test_event_system.py | 4 ++-- zerver/tests/test_events.py | 14 +++++++------- zerver/tests/test_message_send.py | 6 +++--- zerver/tests/test_mirror_users.py | 6 +++--- zerver/tests/test_presence.py | 4 ++-- zerver/tests/test_signup.py | 4 ++-- zerver/tests/test_subs.py | 4 ++-- zerver/tests/test_users.py | 10 +++++----- 11 files changed, 38 insertions(+), 31 deletions(-) diff --git a/analytics/tests/test_support_views.py b/analytics/tests/test_support_views.py index f0b244440a..6fbfa41929 100644 --- a/analytics/tests/test_support_views.py +++ b/analytics/tests/test_support_views.py @@ -11,7 +11,7 @@ from zerver.actions.invites import do_create_multiuse_invite_link from zerver.actions.realm_settings import do_change_realm_org_type, do_send_realm_reactivation_email from zerver.actions.user_settings import do_change_user_setting from zerver.lib.test_classes import ZulipTestCase -from zerver.lib.test_helpers import reset_emails_in_zulip_realm +from zerver.lib.test_helpers import reset_email_visibility_to_everyone_in_zulip_realm from zerver.models import ( MultiuseInvite, PreregistrationUser, @@ -28,7 +28,7 @@ if TYPE_CHECKING: class TestSupportEndpoint(ZulipTestCase): def test_search(self) -> None: - reset_emails_in_zulip_realm() + reset_email_visibility_to_everyone_in_zulip_realm() lear_user = self.lear_user("king") lear_user.is_staff = True lear_user.save(update_fields=["is_staff"]) diff --git a/tools/test-api b/tools/test-api index 0439dd3ea6..ff3d34ff0c 100755 --- a/tools/test-api +++ b/tools/test-api @@ -34,7 +34,7 @@ with test_server_running( from zerver.actions.create_user import do_create_user, do_reactivate_user from zerver.actions.realm_settings import do_deactivate_realm, do_reactivate_realm from zerver.actions.users import change_user_is_active - from zerver.lib.test_helpers import reset_emails_in_zulip_realm + from zerver.lib.test_helpers import reset_email_visibility_to_everyone_in_zulip_realm from zerver.lib.users import get_api_key from zerver.models import get_realm, get_user from zerver.openapi.javascript_examples import test_js_bindings @@ -48,7 +48,7 @@ with test_server_running( print("Running API tests...") - reset_emails_in_zulip_realm() + reset_email_visibility_to_everyone_in_zulip_realm() # Prepare the admin client email = "iago@zulip.com" # Iago is an admin diff --git a/zerver/lib/test_helpers.py b/zerver/lib/test_helpers.py index 2dffbe7294..05d52c3d2b 100644 --- a/zerver/lib/test_helpers.py +++ b/zerver/lib/test_helpers.py @@ -198,7 +198,14 @@ def stdout_suppressed() -> Iterator[IO[str]]: sys.stdout = stdout -def reset_emails_in_zulip_realm() -> None: +def reset_email_visibility_to_everyone_in_zulip_realm() -> None: + """ + This function is used to reset email visibility for all users and + RealmUserDefault object in the zulip realm in development environment + to "EMAIL_ADDRESS_VISIBILITY_EVERYONE" since the default value is + "EMAIL_ADDRESS_VISIBILITY_ADMINS". This function is needed in + tests that want "email" field of users to be set to their real email. + """ realm = get_realm("zulip") realm_user_default = RealmUserDefault.objects.get(realm=realm) do_set_realm_user_default_setting( diff --git a/zerver/tests/test_event_system.py b/zerver/tests/test_event_system.py index 25dc1cceb4..e144847e69 100644 --- a/zerver/tests/test_event_system.py +++ b/zerver/tests/test_event_system.py @@ -23,7 +23,7 @@ from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_helpers import ( HostRequestMock, dummy_handler, - reset_emails_in_zulip_realm, + reset_email_visibility_to_everyone_in_zulip_realm, stub_event_queue_user_events, ) from zerver.lib.users import get_api_key, get_raw_user_data @@ -702,7 +702,7 @@ class FetchInitialStateDataTest(ZulipTestCase): and urlsplit(user_dict["avatar_url"]).hostname == "secure.gravatar.com" ] - reset_emails_in_zulip_realm() + reset_email_visibility_to_everyone_in_zulip_realm() # Test again with client_gravatar = True result = fetch_initial_state_data( diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py index 1fb3666b63..d3329312f1 100644 --- a/zerver/tests/test_events.py +++ b/zerver/tests/test_events.py @@ -195,7 +195,7 @@ from zerver.lib.test_helpers import ( create_dummy_file, get_subscription, get_test_image_file, - reset_emails_in_zulip_realm, + reset_email_visibility_to_everyone_in_zulip_realm, stdout_suppressed, ) from zerver.lib.topic import TOPIC_NAME @@ -881,7 +881,7 @@ class NormalActionsTest(BaseAction): check_invites_changed("events[0]", events[0]) def test_invitation_accept_invite_event(self) -> None: - reset_emails_in_zulip_realm() + reset_email_visibility_to_everyone_in_zulip_realm() self.user_profile = self.example_user("iago") streams = [] @@ -1694,7 +1694,7 @@ class NormalActionsTest(BaseAction): check_realm_update("events[0]", events[0], "signup_notifications_stream_id") def test_change_is_admin(self) -> None: - reset_emails_in_zulip_realm() + reset_email_visibility_to_everyone_in_zulip_realm() # Important: We need to refresh from the database here so that # we don't have a stale UserProfile object with an old value @@ -1719,7 +1719,7 @@ class NormalActionsTest(BaseAction): check_user_group_add_members("events[3]", events[3]) def test_change_is_billing_admin(self) -> None: - reset_emails_in_zulip_realm() + reset_email_visibility_to_everyone_in_zulip_realm() # Important: We need to refresh from the database here so that # we don't have a stale UserProfile object with an old value @@ -1731,7 +1731,7 @@ class NormalActionsTest(BaseAction): self.assertEqual(events[0]["person"]["is_billing_admin"], True) def test_change_is_owner(self) -> None: - reset_emails_in_zulip_realm() + reset_email_visibility_to_everyone_in_zulip_realm() # Important: We need to refresh from the database here so that # we don't have a stale UserProfile object with an old value @@ -1756,7 +1756,7 @@ class NormalActionsTest(BaseAction): check_user_group_add_members("events[3]", events[3]) def test_change_is_moderator(self) -> None: - reset_emails_in_zulip_realm() + reset_email_visibility_to_everyone_in_zulip_realm() # Important: We need to refresh from the database here so that # we don't have a stale UserProfile object with an old value @@ -1784,7 +1784,7 @@ class NormalActionsTest(BaseAction): stream = Stream.objects.get(name="Denmark") do_add_default_stream(stream) - reset_emails_in_zulip_realm() + reset_email_visibility_to_everyone_in_zulip_realm() # Important: We need to refresh from the database here so that # we don't have a stale UserProfile object with an old value diff --git a/zerver/tests/test_message_send.py b/zerver/tests/test_message_send.py index 708ffb45d4..7d0fdd4a44 100644 --- a/zerver/tests/test_message_send.py +++ b/zerver/tests/test_message_send.py @@ -42,7 +42,7 @@ from zerver.lib.test_helpers import ( message_stream_count, most_recent_message, most_recent_usermessage, - reset_emails_in_zulip_realm, + reset_email_visibility_to_everyone_in_zulip_realm, ) from zerver.lib.timestamp import convert_to_UTC, datetime_to_timestamp from zerver.models import ( @@ -1152,7 +1152,7 @@ class MessagePOSTTest(ZulipTestCase): self.assert_json_error(result, "Mirroring not allowed with recipient user IDs") def test_send_message_irc_mirror(self) -> None: - reset_emails_in_zulip_realm() + reset_email_visibility_to_everyone_in_zulip_realm() self.login("hamlet") bot_info = { "full_name": "IRC bot", @@ -1215,7 +1215,7 @@ class MessagePOSTTest(ZulipTestCase): self.assertEqual(int(datetime_to_timestamp(msg.date_sent)), int(fake_timestamp)) def test_unsubscribed_can_forge_sender(self) -> None: - reset_emails_in_zulip_realm() + reset_email_visibility_to_everyone_in_zulip_realm() cordelia = self.example_user("cordelia") stream_name = "private_stream" diff --git a/zerver/tests/test_mirror_users.py b/zerver/tests/test_mirror_users.py index c81cdec32b..377256c898 100644 --- a/zerver/tests/test_mirror_users.py +++ b/zerver/tests/test_mirror_users.py @@ -7,7 +7,7 @@ from django.utils.timezone import now as timezone_now from zerver.actions.message_send import create_mirror_user_if_needed from zerver.lib.create_user import create_user_profile from zerver.lib.test_classes import ZulipTestCase -from zerver.lib.test_helpers import reset_emails_in_zulip_realm +from zerver.lib.test_helpers import reset_email_visibility_to_everyone_in_zulip_realm from zerver.models import UserProfile, get_client, get_realm, get_user from zerver.views.message_send import InvalidMirrorInputError, create_mirrored_message_users @@ -94,7 +94,7 @@ class MirroredMessageUsersTest(ZulipTestCase): self.assertTrue(mirror_sender.is_mirror_dummy) def test_irc_mirror(self) -> None: - reset_emails_in_zulip_realm() + reset_email_visibility_to_everyone_in_zulip_realm() user = self.example_user("hamlet") sender = user @@ -123,7 +123,7 @@ class MirroredMessageUsersTest(ZulipTestCase): self.assertTrue(bob.is_mirror_dummy) def test_jabber_mirror(self) -> None: - reset_emails_in_zulip_realm() + reset_email_visibility_to_everyone_in_zulip_realm() user = self.example_user("hamlet") sender = user diff --git a/zerver/tests/test_presence.py b/zerver/tests/test_presence.py index 9e3da838ed..3d1d72768d 100644 --- a/zerver/tests/test_presence.py +++ b/zerver/tests/test_presence.py @@ -8,7 +8,7 @@ from django.utils.timezone import now as timezone_now from zerver.actions.users import do_deactivate_user from zerver.lib.presence import get_presence_dict_by_realm from zerver.lib.test_classes import ZulipTestCase -from zerver.lib.test_helpers import make_client, reset_emails_in_zulip_realm +from zerver.lib.test_helpers import make_client, reset_email_visibility_to_everyone_in_zulip_realm from zerver.lib.timestamp import datetime_to_timestamp from zerver.models import ( PushDeviceToken, @@ -346,7 +346,7 @@ class SingleUserPresenceTests(ZulipTestCase): self.assert_json_error(result, "No presence data for email@zulip.com") def test_single_user_get(self) -> None: - reset_emails_in_zulip_realm() + reset_email_visibility_to_everyone_in_zulip_realm() # First, we set up the test with some data user = self.example_user("othello") diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.py index f3c5a2ee5d..78e434a011 100644 --- a/zerver/tests/test_signup.py +++ b/zerver/tests/test_signup.py @@ -87,7 +87,7 @@ from zerver.lib.test_helpers import ( most_recent_message, most_recent_usermessage, queries_captured, - reset_emails_in_zulip_realm, + reset_email_visibility_to_everyone_in_zulip_realm, ) from zerver.models import ( CustomProfileField, @@ -932,7 +932,7 @@ class LoginTest(ZulipTestCase): self.assert_logged_in_user_id(None) def test_register(self) -> None: - reset_emails_in_zulip_realm() + reset_email_visibility_to_everyone_in_zulip_realm() realm = get_realm("zulip") hamlet = self.example_user("hamlet") diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.py index 9127d8257e..e13c3ccd92 100644 --- a/zerver/tests/test_subs.py +++ b/zerver/tests/test_subs.py @@ -74,7 +74,7 @@ from zerver.lib.test_helpers import ( get_subscription, most_recent_message, most_recent_usermessage, - reset_emails_in_zulip_realm, + reset_email_visibility_to_everyone_in_zulip_realm, ) from zerver.lib.types import ( APIStreamDict, @@ -392,7 +392,7 @@ class TestCreateStreams(ZulipTestCase): def test_auto_mark_stream_created_message_as_read_for_stream_creator(self) -> None: # This test relies on email == delivery_email for # convenience. - reset_emails_in_zulip_realm() + reset_email_visibility_to_everyone_in_zulip_realm() realm = Realm.objects.get(name="Zulip Dev") iago = self.example_user("iago") diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.py index 16f020e0ed..53b9e8a177 100644 --- a/zerver/tests/test_users.py +++ b/zerver/tests/test_users.py @@ -41,7 +41,7 @@ from zerver.lib.test_helpers import ( cache_tries_captured, get_subscription, get_test_image_file, - reset_emails_in_zulip_realm, + reset_email_visibility_to_everyone_in_zulip_realm, simulated_empty_cache, ) from zerver.lib.upload import upload_avatar_image @@ -300,7 +300,7 @@ class PermissionTest(ZulipTestCase): self.assert_json_error(result, "Insufficient permission") def test_admin_api_hide_emails(self) -> None: - reset_emails_in_zulip_realm() + reset_email_visibility_to_everyone_in_zulip_realm() user = self.example_user("hamlet") admin = self.example_user("iago") @@ -1095,7 +1095,7 @@ class UserProfileTest(ZulipTestCase): self.assertEqual(result[webhook_bot.email].email, webhook_bot.email) def test_get_accounts_for_email(self) -> None: - reset_emails_in_zulip_realm() + reset_email_visibility_to_everyone_in_zulip_realm() def check_account_present_in_accounts(user: UserProfile, accounts: List[Accounts]) -> None: for account in accounts: @@ -1139,7 +1139,7 @@ class UserProfileTest(ZulipTestCase): check_account_present_in_accounts(user, accounts) def test_get_source_profile(self) -> None: - reset_emails_in_zulip_realm() + reset_email_visibility_to_everyone_in_zulip_realm() zulip_realm_id = get_realm("zulip").id iago = get_source_profile("iago@zulip.com", zulip_realm_id) assert iago is not None @@ -1979,7 +1979,7 @@ class RecipientInfoTest(ZulipTestCase): class BulkUsersTest(ZulipTestCase): def test_client_gravatar_option(self) -> None: - reset_emails_in_zulip_realm() + reset_email_visibility_to_everyone_in_zulip_realm() self.login("cordelia") hamlet = self.example_user("hamlet")