bulk_create: Handle realms that hide delivery emails.

This commit is contained in:
Steve Howell 2020-03-06 16:58:06 +00:00 committed by Tim Abbott
parent ecbbc3e365
commit f647587675
2 changed files with 56 additions and 6 deletions

View File

@ -5,7 +5,8 @@ from typing import Any, Dict, Iterable, List, Optional, Set, Tuple, Union
from zerver.lib.initial_password import initial_password from zerver.lib.initial_password import initial_password
from zerver.models import Realm, Stream, UserProfile, \ from zerver.models import Realm, Stream, UserProfile, \
Subscription, Recipient, RealmAuditLog Subscription, Recipient, RealmAuditLog
from zerver.lib.create_user import create_user_profile from zerver.lib.create_user import create_user_profile, \
get_display_email_address
def bulk_create_users(realm: Realm, def bulk_create_users(realm: Realm,
users_raw: Set[Tuple[str, str, str, bool]], users_raw: Set[Tuple[str, str, str, bool]],
@ -21,10 +22,6 @@ def bulk_create_users(realm: Realm,
realm=realm).values_list('email', flat=True)) realm=realm).values_list('email', flat=True))
users = sorted([user_raw for user_raw in users_raw if user_raw[0] not in existing_users]) users = sorted([user_raw for user_raw in users_raw if user_raw[0] not in existing_users])
# If we have a different email_address_visibility mode, the code
# below doesn't have the logic to set user_profile.email properly.
assert realm.email_address_visibility == Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE
# Now create user_profiles # Now create user_profiles
profiles_to_create = [] # type: List[UserProfile] profiles_to_create = [] # type: List[UserProfile]
for (email, full_name, short_name, active) in users: for (email, full_name, short_name, active) in users:
@ -34,7 +31,19 @@ def bulk_create_users(realm: Realm,
timezone, tutorial_status=UserProfile.TUTORIAL_FINISHED, timezone, tutorial_status=UserProfile.TUTORIAL_FINISHED,
enter_sends=True) enter_sends=True)
profiles_to_create.append(profile) profiles_to_create.append(profile)
UserProfile.objects.bulk_create(profiles_to_create)
if realm.email_address_visibility == Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE:
UserProfile.objects.bulk_create(profiles_to_create)
else:
for user_profile in profiles_to_create:
user_profile.email = user_profile.delivery_email
UserProfile.objects.bulk_create(profiles_to_create)
for user_profile in profiles_to_create:
user_profile.email = get_display_email_address(user_profile, realm)
UserProfile.objects.bulk_update(profiles_to_create, ['email'])
user_ids = {user.id for user in profiles_to_create} user_ids = {user.id for user in profiles_to_create}
RealmAuditLog.objects.bulk_create( RealmAuditLog.objects.bulk_create(

View File

@ -30,6 +30,7 @@ from zerver.lib.exceptions import JsonableError
from zerver.lib.send_email import send_future_email, clear_scheduled_emails, \ from zerver.lib.send_email import send_future_email, clear_scheduled_emails, \
deliver_email deliver_email
from zerver.lib.actions import ( from zerver.lib.actions import (
create_users,
get_emails_from_user_ids, get_emails_from_user_ids,
get_recipient_info, get_recipient_info,
do_deactivate_user, do_deactivate_user,
@ -583,6 +584,46 @@ class PermissionTest(ZulipTestCase):
{'profile_data': ujson.dumps(new_profile_data)}) {'profile_data': ujson.dumps(new_profile_data)})
self.assert_json_error(result, 'Insufficient permission') self.assert_json_error(result, 'Insufficient permission')
class BulkCreateUserTest(ZulipTestCase):
def test_create_users(self) -> None:
realm = get_realm('zulip')
realm.email_address_visibility = Realm.EMAIL_ADDRESS_VISIBILITY_ADMINS
realm.save()
name_list = [
('Fred Flinstone', 'fred@zulip.com'),
('Lisa Simpson', 'lisa@zulip.com'),
]
create_users(realm, name_list)
fred = get_user_by_delivery_email('fred@zulip.com', realm)
self.assertEqual(
fred.email,
'user{}@zulip.testserver'.format(fred.id)
)
lisa = get_user_by_delivery_email('lisa@zulip.com', realm)
self.assertEqual(lisa.full_name, 'Lisa Simpson')
self.assertEqual(lisa.is_bot, False)
self.assertEqual(lisa.bot_type, None)
realm.email_address_visibility = Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE
realm.save()
name_list = [
('Bono', 'bono@zulip.com'),
('Cher', 'cher@zulip.com'),
]
create_users(realm, name_list)
bono = get_user_by_delivery_email('bono@zulip.com', realm)
self.assertEqual(bono.email, 'bono@zulip.com')
self.assertEqual(bono.delivery_email, 'bono@zulip.com')
cher = get_user_by_delivery_email('cher@zulip.com', realm)
self.assertEqual(cher.full_name, 'Cher')
class AdminCreateUserTest(ZulipTestCase): class AdminCreateUserTest(ZulipTestCase):
def test_create_user_backend(self) -> None: def test_create_user_backend(self) -> None: