diff --git a/zerver/tests/test_alert_words.py b/zerver/tests/test_alert_words.py index d3ed00470a..9798027b5c 100644 --- a/zerver/tests/test_alert_words.py +++ b/zerver/tests/test_alert_words.py @@ -93,13 +93,11 @@ class AlertWordTests(ZulipTestCase): alert_words_in_realm. Alerts added for one user do not impact other users. """ - email = "cordelia@zulip.com" - user1 = get_user_profile_by_email(email) + user1 = self.example_user('cordelia') add_user_alert_words(user1, self.interesting_alert_word_list) - email = "othello@zulip.com" - user2 = get_user_profile_by_email(email) + user2 = self.example_user('othello') add_user_alert_words(user2, ['another']) realm_words = alert_words_in_realm(user2.realm) @@ -202,8 +200,8 @@ class AlertWordTests(ZulipTestCase): def test_update_alert_words(self): # type: () -> None - me_email = 'hamlet@zulip.com' - user_profile = get_user_profile_by_email(me_email) + user_profile = self.example_user('hamlet') + me_email = user_profile.email self.login(me_email) result = self.client_put('/json/users/me/alert_words', {'alert_words': ujson.dumps(['ALERT'])}) diff --git a/zerver/tests/test_auth_backends.py b/zerver/tests/test_auth_backends.py index 47680b0baa..3fbf100eb2 100644 --- a/zerver/tests/test_auth_backends.py +++ b/zerver/tests/test_auth_backends.py @@ -411,11 +411,11 @@ class SocialAuthMixinTest(ZulipTestCase): class GitHubAuthBackendTest(ZulipTestCase): def setUp(self): # type: () -> None - self.email = 'hamlet@zulip.com' + self.user_profile = self.example_user('hamlet') + self.email = self.user_profile.email self.name = 'Hamlet' self.backend = GitHubAuthBackend() self.backend.strategy = DjangoStrategy(storage=BaseDjangoStorage()) - self.user_profile = get_user_profile_by_email(self.email) self.user_profile.backend = self.backend rf = RequestFactory() @@ -1057,8 +1057,8 @@ class GoogleLoginTest(GoogleOAuthTest): class FetchAPIKeyTest(ZulipTestCase): def setUp(self): # type: () -> None - self.email = "hamlet@zulip.com" - self.user_profile = get_user_profile_by_email(self.email) + self.user_profile = self.example_user('hamlet') + self.email = self.user_profile.email def test_success(self): # type: () -> None @@ -1172,8 +1172,8 @@ class FetchAPIKeyTest(ZulipTestCase): class DevFetchAPIKeyTest(ZulipTestCase): def setUp(self): # type: () -> None - self.email = "hamlet@zulip.com" - self.user_profile = get_user_profile_by_email(self.email) + self.user_profile = self.example_user('hamlet') + self.email = self.user_profile.email def test_success(self): # type: () -> None diff --git a/zerver/tests/test_email_change.py b/zerver/tests/test_email_change.py index 5b7e1f79ed..b4b32bfd59 100644 --- a/zerver/tests/test_email_change.py +++ b/zerver/tests/test_email_change.py @@ -58,10 +58,10 @@ class EmailChangeTestCase(ZulipTestCase): def test_confirm_email_change_when_time_exceeded(self): # type: () -> None - old_email = 'hamlet@zulip.com' + user_profile = self.example_user('hamlet') + old_email = user_profile.email new_email = 'hamlet-new@zulip.com' self.login('hamlet@zulip.com') - user_profile = get_user_profile_by_email(old_email) obj = EmailChangeStatus.objects.create(new_email=new_email, old_email=old_email, user_profile=user_profile, @@ -77,10 +77,10 @@ class EmailChangeTestCase(ZulipTestCase): def test_confirm_email_change(self): # type: () -> None - old_email = 'hamlet@zulip.com' + user_profile = self.example_user('hamlet') + old_email = user_profile.email new_email = 'hamlet-new@zulip.com' self.login('hamlet@zulip.com') - user_profile = get_user_profile_by_email(old_email) obj = EmailChangeStatus.objects.create(new_email=new_email, old_email=old_email, user_profile=user_profile, diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py index 5f35fca29c..0eb7d9cfa8 100644 --- a/zerver/tests/test_events.py +++ b/zerver/tests/test_events.py @@ -220,8 +220,8 @@ class GetEventsTest(ZulipTestCase): # type: () -> None user_profile = self.example_user('hamlet') email = user_profile.email - recipient_email = "othello@zulip.com" - recipient_user_profile = get_user_profile_by_email(recipient_email) + recipient_user_profile = self.example_user('othello') + recipient_email = recipient_user_profile.email self.login(email) result = self.tornado_call(get_events_backend, user_profile, @@ -1769,11 +1769,11 @@ class EventQueueTest(TestCase): 'type': 'unknown', "timestamp": "1"}]) -class TestEventsRegisterAllPublicStreamsDefaults(TestCase): +class TestEventsRegisterAllPublicStreamsDefaults(ZulipTestCase): def setUp(self): # type: () -> None - self.email = 'hamlet@zulip.com' - self.user_profile = get_user_profile_by_email(self.email) + self.user_profile = self.example_user('hamlet') + self.email = self.user_profile.email def test_use_passed_all_public_true_default_false(self): # type: () -> None @@ -1817,11 +1817,11 @@ class TestEventsRegisterAllPublicStreamsDefaults(TestCase): result = _default_all_public_streams(self.user_profile, None) self.assertFalse(result) -class TestEventsRegisterNarrowDefaults(TestCase): +class TestEventsRegisterNarrowDefaults(ZulipTestCase): def setUp(self): # type: () -> None - self.email = 'hamlet@zulip.com' - self.user_profile = get_user_profile_by_email(self.email) + self.user_profile = self.example_user('hamlet') + self.email = self.user_profile.email self.stream = get_stream('Verona', self.user_profile.realm) def test_use_passed_narrow_no_default(self): diff --git a/zerver/tests/test_messages.py b/zerver/tests/test_messages.py index 6c71a48636..1201a5cdeb 100644 --- a/zerver/tests/test_messages.py +++ b/zerver/tests/test_messages.py @@ -446,8 +446,7 @@ class StreamMessagesTest(ZulipTestCase): for email in recipient_list: self.subscribe_to_stream(email, "Denmark") - sender_email = 'hamlet@zulip.com' - sender = get_user_profile_by_email(sender_email) + sender = self.example_user('hamlet') message_type_name = "stream" sending_client = make_client(name="test suite") stream = 'Denmark' @@ -1829,8 +1828,8 @@ class AttachmentTest(ZulipTestCase): # type: () -> None # Create dummy DB entry - sender_email = "hamlet@zulip.com" - user_profile = get_user_profile_by_email(sender_email) + user_profile = self.example_user('hamlet') + sender_email = user_profile.email sample_size = 10 dummy_files = [ ('zulip.txt', '1/31/4CBjtTLYZhk66pZrF8hnYGwc/zulip.txt', sample_size), diff --git a/zerver/tests/test_muting.py b/zerver/tests/test_muting.py index 1878ceb667..40e08b63d0 100644 --- a/zerver/tests/test_muting.py +++ b/zerver/tests/test_muting.py @@ -48,9 +48,9 @@ class MutedTopicsTests(ZulipTestCase): def test_remove_muted_topic(self): # type: () -> None - email = 'hamlet@zulip.com' + self.user_profile = self.example_user('hamlet') + email = self.user_profile.email self.login(email) - self.user_profile = get_user_profile_by_email(email) do_set_muted_topics(self.user_profile, [[u'Verona', u'Verona3']]) url = '/api/v1/users/me/subscriptions/muted_topics' @@ -63,9 +63,9 @@ class MutedTopicsTests(ZulipTestCase): def test_muted_topic_add_invalid(self): # type: () -> None - email = 'hamlet@zulip.com' + self.user_profile = self.example_user('hamlet') + email = self.user_profile.email self.login(email) - self.user_profile = get_user_profile_by_email(email) do_set_muted_topics(self.user_profile, [[u'Verona', u'Verona3']]) url = '/api/v1/users/me/subscriptions/muted_topics' @@ -75,9 +75,9 @@ class MutedTopicsTests(ZulipTestCase): def test_muted_topic_remove_invalid(self): # type: () -> None - email = 'hamlet@zulip.com' + self.user_profile = self.example_user('hamlet') + email = self.user_profile.email self.login(email) - self.user_profile = get_user_profile_by_email(email) do_set_muted_topics(self.user_profile, [[u'Denmark', u'Denmark3']]) url = '/api/v1/users/me/subscriptions/muted_topics' diff --git a/zerver/tests/test_push_notifications.py b/zerver/tests/test_push_notifications.py index c23ee01b45..30d53b4bd6 100644 --- a/zerver/tests/test_push_notifications.py +++ b/zerver/tests/test_push_notifications.py @@ -215,14 +215,13 @@ class PushBouncerNotificationTest(ZulipTestCase): # Auth on this user return self.api_auth(self.server_uuid) -class PushNotificationTest(TestCase): +class PushNotificationTest(ZulipTestCase): def setUp(self): # type: () -> None - email = 'hamlet@zulip.com' + self.user_profile = self.example_user('hamlet') apn.connection = apn.get_connection('fake-cert', 'fake-key') self.redis_client = apn.redis_client = MockRedis() # type: ignore apn.dbx_connection = apn.get_connection('fake-cert', 'fake-key') - self.user_profile = get_user_profile_by_email(email) self.tokens = [u'aaaa', u'bbbb'] for token in self.tokens: PushDeviceToken.objects.create( @@ -560,8 +559,7 @@ class GCMFailureTest(GCMTest): class TestReceivesNotificationsFunctions(ZulipTestCase): def setUp(self): # type: () -> None - email = "cordelia@zulip.com" - self.user = get_user_profile_by_email(email) + self.user = self.example_user('cordelia') def test_receivers_online_notifications_when_user_is_a_bot(self): # type: () -> None diff --git a/zerver/tests/test_reactions.py b/zerver/tests/test_reactions.py index aca765f4bb..834103d7ff 100644 --- a/zerver/tests/test_reactions.py +++ b/zerver/tests/test_reactions.py @@ -73,8 +73,8 @@ class ReactionEmojiTest(ZulipTestCase): self.subscribe_to_stream("cordelia@zulip.com", stream_name, realm=realm) message_id = self.send_message("cordelia@zulip.com", stream_name, Recipient.STREAM) - sender = 'hamlet@zulip.com' - user_profile = get_user_profile_by_email(sender) + user_profile = self.example_user('hamlet') + sender = user_profile.email # Verify that hamlet did not receive the message. self.assertFalse(UserMessage.objects.filter(user_profile=user_profile, diff --git a/zerver/tests/test_settings.py b/zerver/tests/test_settings.py index afcf279dec..c9eeb8f629 100644 --- a/zerver/tests/test_settings.py +++ b/zerver/tests/test_settings.py @@ -85,9 +85,9 @@ class ChangeSettingsTest(ZulipTestCase): def test_illegal_name_changes(self): # type: () -> None - email = 'hamlet@zulip.com' + user = self.example_user('hamlet') + email = user.email self.login(email) - user = get_user_profile_by_email(email) full_name = user.full_name with self.settings(NAME_CHANGES_DISABLED=True): @@ -265,9 +265,9 @@ class ChangeSettingsTest(ZulipTestCase): class UserChangesTest(ZulipTestCase): def test_update_api_key(self): # type: () -> None - email = "hamlet@zulip.com" + user = self.example_user('hamlet') + email = user.email self.login(email) - user = get_user_profile_by_email(email) old_api_key = user.api_key result = self.client_post('/json/users/me/api_key/regenerate') self.assert_json_success(result) diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.py index 449a35d48c..03fa684f52 100644 --- a/zerver/tests/test_subs.py +++ b/zerver/tests/test_subs.py @@ -559,8 +559,8 @@ class StreamAdminTest(ZulipTestCase): self.make_stream(stream_name, invite_only=invite_only) # Set up the principal to be unsubscribed. - other_email = "cordelia@zulip.com" - other_user_profile = get_user_profile_by_email(other_email) + other_user_profile = self.example_user('cordelia') + other_email = other_user_profile.email # Subscribe the admin and/or principal as specified in the flags. if is_subbed: @@ -693,9 +693,9 @@ class StreamAdminTest(ZulipTestCase): """ Trying to unsubscribe an invalid user from a stream fails gracefully. """ - admin_email = "hamlet@zulip.com" + user_profile = self.example_user('hamlet') + admin_email = user_profile.email self.login(admin_email) - user_profile = get_user_profile_by_email(admin_email) do_change_is_admin(user_profile, True) stream_name = u"hümbüǵ" @@ -904,10 +904,10 @@ class SubscriptionPropertiesTest(ZulipTestCase): A POST request to /json/subscriptions/property with stream_name and pin_to_top data pins the stream. """ - test_email = "hamlet@zulip.com" + user_profile = self.example_user('hamlet') + test_email = user_profile.email self.login(test_email) - user_profile = get_user_profile_by_email(test_email) old_subs, _ = gather_subscriptions(user_profile) sub = old_subs[0] stream_name = sub['name'] @@ -1194,9 +1194,9 @@ class SubscriptionAPITest(ZulipTestCase): All tests will be logged in as hamlet. Also save various useful values as attributes that tests can access. """ - self.test_email = "hamlet@zulip.com" + self.user_profile = self.example_user('hamlet') + self.test_email = self.user_profile.email self.login(self.test_email) - self.user_profile = get_user_profile_by_email(self.test_email) self.realm = self.user_profile.realm self.streams = self.get_streams(self.test_email) @@ -1614,8 +1614,8 @@ class SubscriptionAPITest(ZulipTestCase): # Finally, add othello. events = [] - email3 = 'othello@zulip.com' - user_profile = get_user_profile_by_email(email3) + user_profile = self.example_user('othello') + email3 = user_profile.email stream = get_stream('multi_user_stream', realm) with tornado_redirected_to_list(events): bulk_add_subscriptions([stream], [user_profile]) @@ -1646,8 +1646,7 @@ class SubscriptionAPITest(ZulipTestCase): stream_name = "private" (stream, _) = create_stream_if_needed(realm, stream_name, invite_only=True) - existing_email = "hamlet@zulip.com" - existing_user_profile = get_user_profile_by_email(existing_email) + existing_user_profile = self.example_user('hamlet') bulk_add_subscriptions([stream], [existing_user_profile]) # Now subscribe Cordelia to the stream, capturing events @@ -2083,8 +2082,8 @@ class SubscriptionAPITest(ZulipTestCase): settings for that stream are derived from the global notification settings. """ - invitee = "iago@zulip.com" - user_profile = get_user_profile_by_email(invitee) + user_profile = self.example_user('iago') + invitee = user_profile.email user_profile.enable_stream_desktop_notifications = True user_profile.enable_stream_sounds = True user_profile.save() @@ -2108,8 +2107,8 @@ class SubscriptionAPITest(ZulipTestCase): settings for that stream are derived from the global notification settings. """ - invitee = "iago@zulip.com" - user_profile = get_user_profile_by_email(invitee) + user_profile = self.example_user('iago') + invitee = user_profile.email user_profile.enable_stream_desktop_notifications = False user_profile.enable_stream_sounds = False user_profile.save() @@ -2168,8 +2167,8 @@ class GetPublicStreamsTest(ZulipTestCase): class StreamIdTest(ZulipTestCase): def setUp(self): # type: () -> None - self.email = "hamlet@zulip.com" - self.user_profile = get_user_profile_by_email(self.email) + self.user_profile = self.example_user('hamlet') + self.email = self.user_profile.email self.login(self.email) def test_get_stream_id(self): @@ -2284,8 +2283,8 @@ class GetSubscribersTest(ZulipTestCase): def setUp(self): # type: () -> None - self.email = "hamlet@zulip.com" - self.user_profile = get_user_profile_by_email(self.email) + self.user_profile = self.example_user('hamlet') + self.email = self.user_profile.email self.login(self.email) def check_well_formed_result(self, result, stream_name, realm): @@ -2305,7 +2304,7 @@ class GetSubscribersTest(ZulipTestCase): self.assertEqual(sorted(result["subscribers"]), sorted(true_subscribers)) def make_subscriber_request(self, stream_id, email=None): - # type: (int, Optional[str]) -> HttpResponse + # type: (int, Optional[Text]) -> HttpResponse if email is None: email = self.email return self.client_get("/api/v1/streams/%d/members" % (stream_id,), @@ -2486,8 +2485,8 @@ class GetSubscribersTest(ZulipTestCase): stream_name = "NewStream" self.common_subscribe_to_streams(self.email, [stream_name], invite_only=True) - other_email = "othello@zulip.com" - user_profile = get_user_profile_by_email(other_email) + user_profile = self.example_user('othello') + other_email = user_profile.email # Try to fetch the subscriber list as a non-member. stream_id = get_stream(stream_name, user_profile.realm).id @@ -2501,8 +2500,8 @@ class AccessStreamTest(ZulipTestCase): A comprehensive security test for the access_stream_by_* API functions. """ # Create a private stream for which Hamlet is the only subscriber. - hamlet_email = "hamlet@zulip.com" - hamlet = get_user_profile_by_email(hamlet_email) + hamlet = self.example_user('hamlet') + hamlet_email = hamlet.email stream_name = "new_private_stream" self.login(hamlet_email) @@ -2510,8 +2509,7 @@ class AccessStreamTest(ZulipTestCase): invite_only=True) stream = get_stream(stream_name, hamlet.realm) - othello_email = "othello@zulip.com" - othello = get_user_profile_by_email(othello_email) + othello = self.example_user('othello') # Nobody can access a stream that doesn't exist with self.assertRaisesRegex(JsonableError, "Invalid stream id"): diff --git a/zerver/tests/test_upload.py b/zerver/tests/test_upload.py index aca44bc6d6..f717a30b9c 100644 --- a/zerver/tests/test_upload.py +++ b/zerver/tests/test_upload.py @@ -933,8 +933,7 @@ class LocalStorageTest(UploadSerializeMixin, ZulipTestCase): def test_file_upload_local(self): # type: () -> None - sender_email = "hamlet@zulip.com" - user_profile = get_user_profile_by_email(sender_email) + user_profile = self.example_user('hamlet') uri = upload_message_image(u'dummy.txt', len(b'zulip!'), u'text/plain', b'zulip!', user_profile) base = '/user_uploads/' @@ -985,8 +984,7 @@ class S3Test(ZulipTestCase): conn = S3Connection(settings.S3_KEY, settings.S3_SECRET_KEY) bucket = conn.create_bucket(settings.S3_AUTH_UPLOADS_BUCKET) - sender_email = "hamlet@zulip.com" - user_profile = get_user_profile_by_email(sender_email) + user_profile = self.example_user('hamlet') uri = upload_message_image(u'dummy.txt', len(b'zulip!'), u'text/plain', b'zulip!', user_profile) base = '/user_uploads/' @@ -1009,8 +1007,7 @@ class S3Test(ZulipTestCase): conn = S3Connection(settings.S3_KEY, settings.S3_SECRET_KEY) conn.create_bucket(settings.S3_AUTH_UPLOADS_BUCKET) - sender_email = "hamlet@zulip.com" - user_profile = get_user_profile_by_email(sender_email) + user_profile = self.example_user('hamlet') uri = upload_message_image(u'dummy.txt', len(b'zulip!'), u'text/plain', b'zulip!', user_profile) path_id = re.sub('/user_uploads/', '', uri) diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.py index e282c5cabf..449026d6db 100644 --- a/zerver/tests/test_users.py +++ b/zerver/tests/test_users.py @@ -173,9 +173,9 @@ class AdminCreateUserTest(ZulipTestCase): # conditions, and it also does a basic test of the success # path. - admin_email = 'hamlet@zulip.com' + admin = self.example_user('hamlet') + admin_email = admin.email self.login(admin_email) - admin = get_user_profile_by_email(admin_email) do_change_is_admin(admin, True) result = self.client_post("/json/users", dict())