events: Add delivery_email to user initial state data.

For admins, this adds `delivery_email` in addition to `email` for the
case when email address visibility is set to admins only.
This commit is contained in:
Pragati Agrawal 2019-08-11 17:09:27 +05:30 committed by Tim Abbott
parent 8c85040a92
commit 857470c5ca
3 changed files with 44 additions and 4 deletions

View File

@ -352,7 +352,7 @@ realm_user_dict_fields = [
'id', 'full_name', 'short_name', 'email',
'avatar_source', 'avatar_version', 'is_active',
'role', 'is_bot', 'realm_id', 'timezone',
'date_joined', 'bot_owner_id'
'date_joined', 'bot_owner_id', 'delivery_email'
] # type: List[str]
def realm_user_dicts_cache_key(realm_id: int) -> str:

View File

@ -56,7 +56,8 @@ from zproject.backends import email_auth_enabled, password_auth_enabled
from version import ZULIP_VERSION
from zerver.lib.external_accounts import DEFAULT_EXTERNAL_ACCOUNTS
def get_raw_user_data(realm: Realm, client_gravatar: bool) -> Dict[int, Dict[str, str]]:
def get_raw_user_data(realm: Realm, user_profile: UserProfile,
client_gravatar: bool) -> Dict[int, Dict[str, str]]:
user_dicts = get_realm_user_dicts(realm.id)
# TODO: Consider optimizing this query away with caching.
@ -102,6 +103,11 @@ def get_raw_user_data(realm: Realm, client_gravatar: bool) -> Dict[int, Dict[str
is_active = row['is_active'],
date_joined = row['date_joined'].isoformat(),
)
if (realm.email_address_visibility == Realm.EMAIL_ADDRESS_VISIBILITY_ADMINS and
user_profile.is_realm_admin):
result['delivery_email'] = row['delivery_email']
if is_bot:
if row['email'] in settings.CROSS_REALM_BOT_EMAILS:
result['is_cross_realm_bot'] = True
@ -248,6 +254,7 @@ def fetch_initial_state_data(user_profile: UserProfile,
if want('realm_user'):
state['raw_users'] = get_raw_user_data(
realm=realm,
user_profile=user_profile,
client_gravatar=client_gravatar,
)

View File

@ -2825,7 +2825,7 @@ class FetchInitialStateDataTest(ZulipTestCase):
self.assertNotIn(api_key, str(result))
# Admin users have access to all bots in the realm_bots field
def test_realm_bots_admin(self) -> None:
def test_realm_bots_e(self) -> None:
user_profile = self.example_user('hamlet')
do_change_is_admin(user_profile, True)
self.assertTrue(user_profile.is_realm_admin)
@ -2839,6 +2839,39 @@ class FetchInitialStateDataTest(ZulipTestCase):
result = fetch_initial_state_data(user_profile, None, "", client_gravatar=False)
self.assertEqual(result['max_message_id'], -1)
def test_delivery_email_presence_for_non_admins(self) -> None:
user_profile = self.example_user('aaron')
self.assertFalse(user_profile.is_realm_admin)
do_set_realm_property(user_profile.realm, "email_address_visibility",
Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE)
result = fetch_initial_state_data(user_profile, None, "", client_gravatar=False)
for key, value in result['raw_users'].items():
self.assertNotIn('delivery_email', value)
do_set_realm_property(user_profile.realm, "email_address_visibility",
Realm.EMAIL_ADDRESS_VISIBILITY_ADMINS)
result = fetch_initial_state_data(user_profile, None, "", client_gravatar=False)
for key, value in result['raw_users'].items():
self.assertNotIn('delivery_email', value)
def test_delivery_email_presence_for_admins(self) -> None:
user_profile = self.example_user('iago')
self.assertTrue(user_profile.is_realm_admin)
do_set_realm_property(user_profile.realm, "email_address_visibility",
Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE)
result = fetch_initial_state_data(user_profile, None, "", client_gravatar=False)
for key, value in result['raw_users'].items():
self.assertNotIn('delivery_email', value)
do_set_realm_property(user_profile.realm, "email_address_visibility",
Realm.EMAIL_ADDRESS_VISIBILITY_ADMINS)
result = fetch_initial_state_data(user_profile, None, "", client_gravatar=False)
for key, value in result['raw_users'].items():
self.assertIn('delivery_email', value)
class GetUnreadMsgsTest(ZulipTestCase):
def mute_stream(self, user_profile: UserProfile, stream: Stream) -> None:
recipient = Recipient.objects.get(type_id=stream.id, type=Recipient.STREAM)
@ -3585,7 +3618,7 @@ class TestEventsRegisterNarrowDefaults(ZulipTestCase):
class TestGetRawUserDataSystemBotRealm(ZulipTestCase):
def test_get_raw_user_data_on_system_bot_realm(self) -> None:
result = get_raw_user_data(get_realm("zulipinternal"), True)
result = get_raw_user_data(get_realm("zulipinternal"), self.example_user('hamlet'), True)
for bot_email in settings.CROSS_REALM_BOT_EMAILS:
bot_profile = get_system_bot(bot_email)