populate_db, tests: Restrict emails in zulip realm.

We now restrict emails on the zulip realm, and now
`email` and `delivery_email` will be different for
users.

This change should make it more likely to catch
errors where we leak delivery emails or use the
wrong field for lookups.
This commit is contained in:
Steve Howell 2020-03-12 12:51:54 +00:00 committed by Tim Abbott
parent f647587675
commit eb9a252ec9
6 changed files with 37 additions and 15 deletions

View File

@ -75,6 +75,10 @@ os.makedirs('var/casper', exist_ok=True)
for f in glob.glob('var/casper/casper-failure*.png'): for f in glob.glob('var/casper/casper-failure*.png'):
os.remove(f) os.remove(f)
def reset_database() -> None:
from zerver.lib.test_helpers import reset_emails_in_zulip_realm
reset_emails_in_zulip_realm()
def run_tests(files: Iterable[str], external_host: str) -> None: def run_tests(files: Iterable[str], external_host: str) -> None:
test_dir = os.path.join(ZULIP_PATH, 'frontend_tests/casper_tests') test_dir = os.path.join(ZULIP_PATH, 'frontend_tests/casper_tests')
test_files = [] test_files = []
@ -118,9 +122,12 @@ def run_tests(files: Iterable[str], external_host: str) -> None:
return 0 return 0
with test_server_running(options.force, external_host): with test_server_running(options.force, external_host):
# Important: do this next call inside the `with` block, when Django # Important: do next things inside the `with` block, when Django
# will be pointing at the test database. # will be pointing at the test database.
reset_database()
subprocess.check_call('tools/setup/generate-test-credentials') subprocess.check_call('tools/setup/generate-test-credentials')
# RUN THE TESTS!!!
if options.interactive: if options.interactive:
response = input('Press Enter to run tests, "q" to quit: ') response = input('Press Enter to run tests, "q" to quit: ')
ret = 1 ret = 1
@ -131,6 +138,7 @@ def run_tests(files: Iterable[str], external_host: str) -> None:
else: else:
ret = 1 ret = 1
ret = run_tests() ret = run_tests()
if ret != 0: if ret != 0:
print(""" print("""
The Casper frontend tests failed! For help debugging, read: The Casper frontend tests failed! For help debugging, read:

View File

@ -33,14 +33,17 @@ with test_server_running(force=options.force, external_host='zulipdev.com:9981')
from zerver.openapi.python_examples import test_the_api, test_invalid_api_key from zerver.openapi.python_examples import test_the_api, test_invalid_api_key
from zerver.openapi.test_curl_examples import test_generated_curl_examples_for_success from zerver.openapi.test_curl_examples import test_generated_curl_examples_for_success
from zerver.lib.actions import do_create_user from zerver.lib.actions import do_create_user
from zerver.lib.test_helpers import reset_emails_in_zulip_realm
from zerver.lib.users import get_api_key from zerver.lib.users import get_api_key
from zerver.models import get_user, get_realm from zerver.models import get_user, get_realm
print("Running API tests...") print("Running API tests...")
reset_emails_in_zulip_realm()
# Prepare the admin client # Prepare the admin client
email = 'iago@zulip.com' # Iago is an admin email = 'iago@zulip.com' # Iago is an admin
realm = get_realm("zulip") realm = get_realm('zulip')
user = get_user(email, realm) user = get_user(email, realm)
api_key = get_api_key(user) api_key = get_api_key(user)
site = 'http://zulip.zulipdev.com:9981' site = 'http://zulip.zulipdev.com:9981'

View File

@ -26,4 +26,4 @@ LATEST_RELEASE_ANNOUNCEMENT = "https://blog.zulip.org/2019/12/13/zulip-2-1-relea
# historical commits sharing the same major version, in which case a # historical commits sharing the same major version, in which case a
# minor version bump suffices. # minor version bump suffices.
PROVISION_VERSION = '74.1' PROVISION_VERSION = '75.0'

View File

@ -642,6 +642,7 @@ class ListCustomProfileFieldTest(CustomProfileFieldTestCase):
def test_get_custom_profile_fields_from_api(self) -> None: def test_get_custom_profile_fields_from_api(self) -> None:
iago = self.example_user("iago") iago = self.example_user("iago")
test_bot = self.create_test_bot("foo-bot", iago) test_bot = self.create_test_bot("foo-bot", iago)
self.login_user(iago)
assert(test_bot) assert(test_bot)
url = "/json/users?client_gravatar=false&include_custom_profile_fields=true" url = "/json/users?client_gravatar=false&include_custom_profile_fields=true"
@ -668,12 +669,14 @@ class ListCustomProfileFieldTest(CustomProfileFieldTestCase):
raise AssertionError("Could not find required data from the response.") raise AssertionError("Could not find required data from the response.")
expected_keys_for_iago = { expected_keys_for_iago = {
"delivery_email",
"email", "user_id", "avatar_url", "is_admin", "is_guest", "is_bot", "email", "user_id", "avatar_url", "is_admin", "is_guest", "is_bot",
"full_name", "timezone", "is_active", "date_joined", "profile_data"} "full_name", "timezone", "is_active", "date_joined", "profile_data"}
self.assertEqual(set(iago_raw_data.keys()), expected_keys_for_iago) self.assertEqual(set(iago_raw_data.keys()), expected_keys_for_iago)
self.assertNotEqual(iago_raw_data["profile_data"], {}) self.assertNotEqual(iago_raw_data["profile_data"], {})
expected_keys_for_test_bot = { expected_keys_for_test_bot = {
"delivery_email",
"email", "user_id", "avatar_url", "is_admin", "is_guest", "is_bot", "full_name", "email", "user_id", "avatar_url", "is_admin", "is_guest", "is_bot", "full_name",
"timezone", "is_active", "date_joined", "bot_type", "bot_owner_id"} "timezone", "is_active", "date_joined", "bot_type", "bot_owner_id"}
self.assertEqual(set(test_bot_raw_data.keys()), expected_keys_for_test_bot) self.assertEqual(set(test_bot_raw_data.keys()), expected_keys_for_test_bot)

View File

@ -52,7 +52,13 @@ class EmailChangeTestCase(ZulipTestCase):
def test_confirm_email_change(self) -> None: def test_confirm_email_change(self) -> None:
user_profile = self.example_user('hamlet') user_profile = self.example_user('hamlet')
old_email = user_profile.email do_set_realm_property(
user_profile.realm,
'email_address_visibility',
Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE,
)
old_email = user_profile.delivery_email
new_email = 'hamlet-new@zulip.com' new_email = 'hamlet-new@zulip.com'
new_realm = get_realm('zulip') new_realm = get_realm('zulip')
self.login('hamlet') self.login('hamlet')

View File

@ -33,7 +33,7 @@ from zerver.models import CustomProfileField, DefaultStream, Message, Realm, Rea
RealmDomain, Recipient, Service, Stream, Subscription, \ RealmDomain, Recipient, Service, Stream, Subscription, \
UserMessage, UserPresence, UserProfile, Huddle, Client, \ UserMessage, UserPresence, UserProfile, Huddle, Client, \
get_client, get_huddle, get_realm, get_stream, \ get_client, get_huddle, get_realm, get_stream, \
get_user, get_user_profile_by_id get_user, get_user_by_delivery_email, get_user_profile_by_id
from zerver.lib.types import ProfileFieldData from zerver.lib.types import ProfileFieldData
from scripts.lib.zulip_tools import get_or_create_dev_uuid_var_path from scripts.lib.zulip_tools import get_or_create_dev_uuid_var_path
@ -201,6 +201,7 @@ class Command(BaseCommand):
create_internal_realm() create_internal_realm()
zulip_realm = Realm.objects.create( zulip_realm = Realm.objects.create(
string_id="zulip", name="Zulip Dev", emails_restricted_to_domains=False, string_id="zulip", name="Zulip Dev", emails_restricted_to_domains=False,
email_address_visibility=Realm.EMAIL_ADDRESS_VISIBILITY_ADMINS,
description="The Zulip development environment default organization." description="The Zulip development environment default organization."
" It's great for testing!", " It's great for testing!",
invite_required=False, org_type=Realm.CORPORATE) invite_required=False, org_type=Realm.CORPORATE)
@ -264,12 +265,12 @@ class Command(BaseCommand):
create_users(zulip_realm, names, tos_version=settings.TOS_VERSION) create_users(zulip_realm, names, tos_version=settings.TOS_VERSION)
iago = get_user("iago@zulip.com", zulip_realm) iago = get_user_by_delivery_email("iago@zulip.com", zulip_realm)
do_change_is_admin(iago, True) do_change_is_admin(iago, True)
iago.is_staff = True iago.is_staff = True
iago.save(update_fields=['is_staff']) iago.save(update_fields=['is_staff'])
guest_user = get_user("polonius@zulip.com", zulip_realm) guest_user = get_user_by_delivery_email("polonius@zulip.com", zulip_realm)
guest_user.role = UserProfile.ROLE_GUEST guest_user.role = UserProfile.ROLE_GUEST
guest_user.save(update_fields=['role']) guest_user.save(update_fields=['role'])
@ -284,7 +285,7 @@ class Command(BaseCommand):
create_users(zulip_realm, zulip_realm_bots, bot_type=UserProfile.DEFAULT_BOT) create_users(zulip_realm, zulip_realm_bots, bot_type=UserProfile.DEFAULT_BOT)
zoe = get_user("zoe@zulip.com", zulip_realm) zoe = get_user_by_delivery_email("zoe@zulip.com", zulip_realm)
zulip_webhook_bots = [ zulip_webhook_bots = [
("Zulip Webhook Bot", "webhook-bot@zulip.com"), ("Zulip Webhook Bot", "webhook-bot@zulip.com"),
] ]
@ -293,7 +294,7 @@ class Command(BaseCommand):
# owner of the webhook bot, so bot_owner can't be None # owner of the webhook bot, so bot_owner can't be None
create_users(zulip_realm, zulip_webhook_bots, create_users(zulip_realm, zulip_webhook_bots,
bot_type=UserProfile.INCOMING_WEBHOOK_BOT, bot_owner=zoe) bot_type=UserProfile.INCOMING_WEBHOOK_BOT, bot_owner=zoe)
aaron = get_user("AARON@zulip.com", zulip_realm) aaron = get_user_by_delivery_email("AARON@zulip.com", zulip_realm)
zulip_outgoing_bots = [ zulip_outgoing_bots = [
("Outgoing Webhook", "outgoing-webhook@zulip.com") ("Outgoing Webhook", "outgoing-webhook@zulip.com")
@ -345,10 +346,11 @@ class Command(BaseCommand):
} }
for profile in profiles: for profile in profiles:
if profile.email not in subscriptions_map: email = profile.delivery_email
raise Exception('Subscriptions not listed for user %s' % (profile.email,)) if email not in subscriptions_map:
raise Exception('Subscriptions not listed for user %s' % (email,))
for stream_name in subscriptions_map[profile.email]: for stream_name in subscriptions_map[email]:
stream = Stream.objects.get(name=stream_name) stream = Stream.objects.get(name=stream_name)
r = Recipient.objects.get(type=Recipient.STREAM, type_id=stream.id) r = Recipient.objects.get(type=Recipient.STREAM, type_id=stream.id)
subscriptions_list.append((profile, r)) subscriptions_list.append((profile, r))
@ -418,7 +420,7 @@ class Command(BaseCommand):
github_profile = try_add_realm_default_custom_profile_field(zulip_realm, "github") github_profile = try_add_realm_default_custom_profile_field(zulip_realm, "github")
# Fill in values for Iago and Hamlet # Fill in values for Iago and Hamlet
hamlet = get_user("hamlet@zulip.com", zulip_realm) hamlet = get_user_by_delivery_email("hamlet@zulip.com", zulip_realm)
do_update_user_custom_profile_data_if_changed(iago, [ do_update_user_custom_profile_data_if_changed(iago, [
{"id": phone_number.id, "value": "+1-234-567-8901"}, {"id": phone_number.id, "value": "+1-234-567-8901"},
{"id": biography.id, "value": "Betrayer of Othello."}, {"id": biography.id, "value": "Betrayer of Othello."},
@ -739,7 +741,7 @@ def choose_date_sent(num_messages: int, tot_messages: int, threads: int) -> date
def create_user_groups() -> None: def create_user_groups() -> None:
zulip = get_realm('zulip') zulip = get_realm('zulip')
members = [get_user('cordelia@zulip.com', zulip), members = [get_user_by_delivery_email('cordelia@zulip.com', zulip),
get_user('hamlet@zulip.com', zulip)] get_user_by_delivery_email('hamlet@zulip.com', zulip)]
create_user_group("hamletcharacters", members, zulip, create_user_group("hamletcharacters", members, zulip,
description="Characters of Hamlet") description="Characters of Hamlet")