From 43cdbdbf9d0228c32a54e69e7f03fa87b8987a22 Mon Sep 17 00:00:00 2001 From: sahil839 Date: Fri, 21 May 2021 23:48:47 +0530 Subject: [PATCH] tests: Move a part of email_address_visibility tests to test_users.py. We should only keep tests for changing email_address_visibility in test_realm.py. The tests for checking the value of delivery_email and email in the user dicts returned by 'GET users/{user_id}' endpoint according to email_address_visibility should be in test_users.py and not test_realm.py. The tests of other realm settings are also arranged in the same way. This commit makes the test more robust by checking for all possible values of email_address_visibility and checking emails and delivery emails values received by different user roles. --- zerver/tests/test_realm.py | 44 +-------------------------- zerver/tests/test_users.py | 62 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 43 deletions(-) diff --git a/zerver/tests/test_realm.py b/zerver/tests/test_realm.py index 05c2ec3657..aa29a3a46e 100644 --- a/zerver/tests/test_realm.py +++ b/zerver/tests/test_realm.py @@ -22,7 +22,7 @@ from zerver.lib.realm_description import get_realm_rendered_description, get_rea from zerver.lib.send_email import send_future_email from zerver.lib.streams import create_stream_if_needed from zerver.lib.test_classes import ZulipTestCase -from zerver.lib.test_helpers import reset_emails_in_zulip_realm, tornado_redirected_to_list +from zerver.lib.test_helpers import tornado_redirected_to_list from zerver.models import ( Attachment, CustomProfileField, @@ -470,8 +470,6 @@ class RealmTest(ZulipTestCase): def test_change_email_address_visibility(self) -> None: # We need an admin user. user_profile = self.example_user("iago") - hamlet = self.example_user("hamlet") - cordelia = self.example_user("cordelia") self.login_user(user_profile) invalid_value = 12 @@ -479,9 +477,6 @@ class RealmTest(ZulipTestCase): result = self.client_patch("/json/realm", req) self.assert_json_error(result, "Invalid email_address_visibility") - reset_emails_in_zulip_realm() - realm = get_realm("zulip") - req = dict( email_address_visibility=orjson.dumps(Realm.EMAIL_ADDRESS_VISIBILITY_ADMINS).decode() ) @@ -490,43 +485,6 @@ class RealmTest(ZulipTestCase): realm = get_realm("zulip") self.assertEqual(realm.email_address_visibility, Realm.EMAIL_ADDRESS_VISIBILITY_ADMINS) - edited_user_profile = get_user_profile_by_id(user_profile.id) - self.assertEqual( - edited_user_profile.email, f"user{edited_user_profile.id}@zulip.testserver" - ) - - # Check normal user cannot access email - result = self.api_get(cordelia, f"/api/v1/users/{hamlet.id}") - self.assert_json_success(result) - self.assertEqual(result.json()["user"]["email"], f"user{hamlet.id}@zulip.testserver") - self.assertEqual(result.json()["user"].get("delivery_email"), None) - - # Check administrator gets delivery_email with EMAIL_ADDRESS_VISIBILITY_ADMINS - result = self.api_get(user_profile, f"/api/v1/users/{hamlet.id}") - self.assert_json_success(result) - self.assertEqual(result.json()["user"]["email"], f"user{hamlet.id}@zulip.testserver") - self.assertEqual(result.json()["user"].get("delivery_email"), hamlet.delivery_email) - - req = dict( - email_address_visibility=orjson.dumps(Realm.EMAIL_ADDRESS_VISIBILITY_NOBODY).decode() - ) - result = self.client_patch("/json/realm", req) - self.assert_json_success(result) - - realm = get_realm("zulip") - self.assertEqual(realm.email_address_visibility, Realm.EMAIL_ADDRESS_VISIBILITY_NOBODY) - edited_user_profile = get_user_profile_by_id(user_profile.id) - self.assertEqual( - edited_user_profile.email, f"user{edited_user_profile.id}@zulip.testserver" - ) - - # Check even administrator doesn't get delivery_email with - # EMAIL_ADDRESS_VISIBILITY_NOBODY - result = self.api_get(user_profile, f"/api/v1/users/{hamlet.id}") - self.assert_json_success(result) - self.assertEqual(result.json()["user"]["email"], f"user{hamlet.id}@zulip.testserver") - self.assertEqual(result.json()["user"].get("delivery_email"), None) - def test_change_stream_creation_policy(self) -> None: # We need an admin user. self.login("iago") diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.py index 41d9995473..740d572e71 100644 --- a/zerver/tests/test_users.py +++ b/zerver/tests/test_users.py @@ -1861,6 +1861,68 @@ class GetProfileTest(ZulipTestCase): avatar_url(hamlet), ) + def test_user_email_according_to_email_address_visibility_setting(self) -> None: + hamlet = self.example_user("hamlet") + + realm = hamlet.realm + do_set_realm_property( + realm, + "email_address_visibility", + Realm.EMAIL_ADDRESS_VISIBILITY_NOBODY, + acting_user=None, + ) + + # Check that even admin cannot access email when setting is set to + # EMAIL_ADDRESS_VISIBILITY_NOBODY. + self.login("iago") + result = orjson.loads(self.client_get(f"/json/users/{hamlet.id}").content) + self.assertEqual(result["user"].get("delivery_email"), None) + self.assertEqual(result["user"].get("email"), f"user{hamlet.id}@zulip.testserver") + + do_set_realm_property( + realm, + "email_address_visibility", + Realm.EMAIL_ADDRESS_VISIBILITY_ADMINS, + acting_user=None, + ) + + # Check that admin can access email when setting is set to + # EMAIL_ADDRESS_VISIBILITY_ADMINS. + result = orjson.loads(self.client_get(f"/json/users/{hamlet.id}").content) + self.assertEqual(result["user"].get("delivery_email"), hamlet.delivery_email) + self.assertEqual(result["user"].get("email"), f"user{hamlet.id}@zulip.testserver") + + # Check that moderator cannot access email when setting is set to + # EMAIL_ADDRESS_VISIBILITY_ADMINS. + self.login("shiva") + result = orjson.loads(self.client_get(f"/json/users/{hamlet.id}").content) + self.assertEqual(result["user"].get("delivery_email"), None) + self.assertEqual(result["user"].get("email"), f"user{hamlet.id}@zulip.testserver") + + do_set_realm_property( + realm, + "email_address_visibility", + Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE, + acting_user=None, + ) + + # Check that moderator, member and guest all can access email when setting + # is set to EMAIL_ADDRESS_VISIBILITY_EVERYONE. + self.login("shiva") + result = orjson.loads(self.client_get(f"/json/users/{hamlet.id}").content) + self.assertEqual(result["user"].get("delivery_email"), None) + self.assertEqual(result["user"].get("email"), hamlet.delivery_email) + + self.login("cordelia") + result = orjson.loads(self.client_get(f"/json/users/{hamlet.id}").content) + self.assertEqual(result["user"].get("delivery_email"), None) + self.assertEqual(result["user"].get("email"), hamlet.delivery_email) + + self.login("polonius") + result = orjson.loads(self.client_get(f"/json/users/{hamlet.id}").content) + self.assertEqual(result["user"].get("delivery_email"), None) + self.assertEqual(result["user"].get("email"), hamlet.delivery_email) + class DeleteUserTest(ZulipTestCase): def test_do_delete_user(self) -> None: