diff --git a/zerver/lib/test_classes.py b/zerver/lib/test_classes.py index 183dfe710a..64a3160aee 100644 --- a/zerver/lib/test_classes.py +++ b/zerver/lib/test_classes.py @@ -462,39 +462,35 @@ class ZulipTestCase(TestCase): ) return [cast(str, get_display_recipient(sub.recipient)) for sub in subs] - def send_personal_message(self, from_email: str, to_email: str, content: str="test content", - sender_realm: str="zulip", + def send_personal_message(self, from_user: UserProfile, to_user: UserProfile, content: str="test content", sending_client_name: str="test suite") -> int: - sender = get_user(from_email, get_realm(sender_realm)) - - recipient_list = [to_email] + recipient_list = [to_user.email] (sending_client, _) = Client.objects.get_or_create(name=sending_client_name) return check_send_message( - sender, sending_client, 'private', recipient_list, None, + from_user, sending_client, 'private', recipient_list, None, content ) - def send_huddle_message(self, from_email: str, to_emails: List[str], content: str="test content", - sender_realm: str="zulip", + def send_huddle_message(self, + from_user: UserProfile, + to_users: List[UserProfile], + content: str="test content", sending_client_name: str="test suite") -> int: - sender = get_user(from_email, get_realm(sender_realm)) - + to_emails = [u.email for u in to_users] assert(len(to_emails) >= 2) (sending_client, _) = Client.objects.get_or_create(name=sending_client_name) return check_send_message( - sender, sending_client, 'private', to_emails, None, + from_user, sending_client, 'private', to_emails, None, content ) - def send_stream_message(self, sender_email: str, stream_name: str, content: str="test content", - topic_name: str="test", sender_realm: str="zulip", + def send_stream_message(self, sender: UserProfile, stream_name: str, content: str="test content", + topic_name: str="test", recipient_realm: Optional[Realm]=None, sending_client_name: str="test suite") -> int: - sender = get_user(sender_email, get_realm(sender_realm)) - (sending_client, _) = Client.objects.get_or_create(name=sending_client_name) return check_send_stream_message( diff --git a/zerver/openapi/curl_param_value_generators.py b/zerver/openapi/curl_param_value_generators.py index 4aa872ba7f..be09072db4 100644 --- a/zerver/openapi/curl_param_value_generators.py +++ b/zerver/openapi/curl_param_value_generators.py @@ -60,7 +60,7 @@ def patch_openapi_example_values(entry: str, params: List[Dict[str, Any]], "/messages/{message_id}:patch", "/messages/{message_id}:delete"]) def iago_message_id() -> Dict[str, int]: return { - "message_id": helpers.send_stream_message(helpers.example_email("iago"), "Denmark") + "message_id": helpers.send_stream_message(helpers.example_user("iago"), "Denmark") } @openapi_param_value_generator(["/messages/flags:post"]) @@ -70,7 +70,7 @@ def update_flags_message_ids() -> Dict[str, List[int]]: messages = [] for _ in range(3): - messages.append(helpers.send_stream_message(helpers.example_email("iago"), stream_name)) + messages.append(helpers.send_stream_message(helpers.example_user("iago"), stream_name)) return { "messages": messages, } @@ -101,7 +101,7 @@ def get_denmark_stream_id_and_topic() -> Dict[str, Any]: topic_name = "Tivoli Gardens" helpers.subscribe(helpers.example_user("iago"), stream_name) - helpers.send_stream_message(helpers.example_email("hamlet"), stream_name, topic_name=topic_name) + helpers.send_stream_message(helpers.example_user("hamlet"), stream_name, topic_name=topic_name) return { "stream_id": helpers.get_stream_id(stream_name), @@ -136,7 +136,7 @@ def get_events() -> Dict[str, Any]: helpers.subscribe(profile, "Verona") client = Client.objects.create(name="curl-test-client-1") response = do_events_register(profile, client, event_types=['message', 'realm_emoji']) - helpers.send_stream_message(helpers.example_email("hamlet"), "Verona") + helpers.send_stream_message(helpers.example_user("hamlet"), "Verona") return { "queue_id": response["queue_id"], "last_event_id": response["last_event_id"], diff --git a/zerver/tests/test_alert_words.py b/zerver/tests/test_alert_words.py index 83ac423bf8..b37d31f78c 100644 --- a/zerver/tests/test_alert_words.py +++ b/zerver/tests/test_alert_words.py @@ -133,7 +133,7 @@ class AlertWordTests(ZulipTestCase): def message_does_alert(self, user_profile: UserProfile, message: str) -> bool: """Send a bunch of messages as othello, so Hamlet is notified""" - self.send_stream_message(self.example_email("othello"), "Denmark", message) + self.send_stream_message(self.example_user("othello"), "Denmark", message) user_message = most_recent_usermessage(user_profile) return 'has_alert_word' in user_message.flags_list() @@ -172,7 +172,7 @@ class AlertWordTests(ZulipTestCase): result = self.client_post('/json/users/me/alert_words', {'alert_words': ujson.dumps(['ALERT'])}) content = 'this is an ALERT for you' - self.send_stream_message(me_email, "Denmark", content) + self.send_stream_message(user_profile, "Denmark", content) self.assert_json_success(result) original_message = most_recent_message(user_profile) diff --git a/zerver/tests/test_archive.py b/zerver/tests/test_archive.py index 2bf06298f6..f1ba505c06 100644 --- a/zerver/tests/test_archive.py +++ b/zerver/tests/test_archive.py @@ -34,7 +34,7 @@ class GlobalPublicStreamTest(ZulipTestCase): def send_msg_and_get_result(msg: str) -> HttpResponse: self.send_stream_message( - self.example_email("iago"), + self.example_user("iago"), "Test Public Archives", msg, 'TopicGlobal' @@ -92,7 +92,7 @@ class WebPublicTopicHistoryTest(ZulipTestCase): test_stream = self.make_stream('Test Public Archives') self.send_stream_message( - self.example_email("iago"), + self.example_user("iago"), "Test Public Archives", 'Test Message', 'TopicGlobal' @@ -110,31 +110,31 @@ class WebPublicTopicHistoryTest(ZulipTestCase): do_change_stream_web_public(test_stream, True) self.send_stream_message( - self.example_email("iago"), + self.example_user("iago"), "Test Public Archives", 'Test Message 3', topic_name='first_topic' ) self.send_stream_message( - self.example_email("iago"), + self.example_user("iago"), "Test Public Archives", 'Test Message', topic_name='TopicGlobal' ) self.send_stream_message( - self.example_email("iago"), + self.example_user("iago"), "Test Public Archives", 'Test Message 2', topic_name='topicglobal' ) self.send_stream_message( - self.example_email("iago"), + self.example_user("iago"), "Test Public Archives", 'Test Message 3', topic_name='second_topic' ) self.send_stream_message( - self.example_email("iago"), + self.example_user("iago"), "Test Public Archives", 'Test Message 4', topic_name='TopicGlobal' diff --git a/zerver/tests/test_bugdown.py b/zerver/tests/test_bugdown.py index 6523b82eaa..d02f652407 100644 --- a/zerver/tests/test_bugdown.py +++ b/zerver/tests/test_bugdown.py @@ -2005,7 +2005,7 @@ class BugdownErrorTests(ZulipTestCase): # We don't use assertRaisesRegex because it seems to not # handle i18n properly here on some systems. with self.assertRaises(JsonableError): - self.send_stream_message(self.example_email("othello"), "Denmark", message) + self.send_stream_message(self.example_user("othello"), "Denmark", message) def test_ultra_long_rendering(self) -> None: """A rendered message with an ultra-long lenght (> 10 * MAX_MESSAGE_LENGTH) diff --git a/zerver/tests/test_digest.py b/zerver/tests/test_digest.py index e183ab76f0..1af70f91d2 100644 --- a/zerver/tests/test_digest.py +++ b/zerver/tests/test_digest.py @@ -271,9 +271,9 @@ class TestDigestEmailMessages(ZulipTestCase): sending_client = get_client(client) message_ids = [] # List[int] for sender_name in senders: - email = self.example_email(sender_name) - content = 'some content for {} from {}'.format(stream, email) - message_id = self.send_stream_message(email, stream, content) + sender = self.example_user(sender_name) + content = 'some content for {} from {}'.format(stream, sender_name) + message_id = self.send_stream_message(sender, stream, content) message_ids.append(message_id) Message.objects.filter(id__in=message_ids).update(sending_client=sending_client) return message_ids diff --git a/zerver/tests/test_email_notifications.py b/zerver/tests/test_email_notifications.py index 014103ad24..c435948d83 100644 --- a/zerver/tests/test_email_notifications.py +++ b/zerver/tests/test_email_notifications.py @@ -173,8 +173,8 @@ class TestMissedMessages(ZulipTestCase): def _realm_name_in_missed_message_email_subject(self, realm_name_in_notifications: bool) -> None: msg_id = self.send_personal_message( - self.example_email('othello'), - self.example_email('hamlet'), + self.example_user('othello'), + self.example_user('hamlet'), 'Extremely personal message!', ) verify_body_include = ['Extremely personal message!'] @@ -187,12 +187,12 @@ class TestMissedMessages(ZulipTestCase): def _extra_context_in_missed_stream_messages_mention(self, send_as_user: bool, show_message_content: bool=True) -> None: for i in range(0, 11): - self.send_stream_message(self.example_email('othello'), "Denmark", content=str(i)) + self.send_stream_message(self.example_user('othello'), "Denmark", content=str(i)) self.send_stream_message( - self.example_email('othello'), "Denmark", + self.example_user('othello'), "Denmark", '11', topic_name='test2') msg_id = self.send_stream_message( - self.example_email('othello'), "denmark", + self.example_user('othello'), "denmark", '@**King Hamlet**') if show_message_content: @@ -223,12 +223,12 @@ class TestMissedMessages(ZulipTestCase): def _extra_context_in_missed_stream_messages_wildcard_mention(self, send_as_user: bool, show_message_content: bool=True) -> None: for i in range(1, 6): - self.send_stream_message(self.example_email('othello'), "Denmark", content=str(i)) + self.send_stream_message(self.example_user('othello'), "Denmark", content=str(i)) self.send_stream_message( - self.example_email('othello'), "Denmark", + self.example_user('othello'), "Denmark", '11', topic_name='test2') msg_id = self.send_stream_message( - self.example_email('othello'), "denmark", + self.example_user('othello'), "denmark", '@**all**') if show_message_content: @@ -257,12 +257,12 @@ class TestMissedMessages(ZulipTestCase): def _extra_context_in_missed_stream_messages_email_notify(self, send_as_user: bool) -> None: for i in range(0, 11): - self.send_stream_message(self.example_email('othello'), "Denmark", content=str(i)) + self.send_stream_message(self.example_user('othello'), "Denmark", content=str(i)) self.send_stream_message( - self.example_email('othello'), "Denmark", + self.example_user('othello'), "Denmark", '11', topic_name='test2') msg_id = self.send_stream_message( - self.example_email('othello'), "denmark", + self.example_user('othello'), "denmark", '12') verify_body_include = [ "Othello, the Moor of Venice: 1 2 3 4 5 6 7 8 9 10 12 -- ", @@ -273,9 +273,9 @@ class TestMissedMessages(ZulipTestCase): def _extra_context_in_missed_stream_messages_mention_two_senders(self, send_as_user: bool) -> None: for i in range(0, 3): - self.send_stream_message(self.example_email('cordelia'), "Denmark", str(i)) + self.send_stream_message(self.example_user('cordelia'), "Denmark", str(i)) msg_id = self.send_stream_message( - self.example_email('othello'), "Denmark", + self.example_user('othello'), "Denmark", '@**King Hamlet**') verify_body_include = [ "Cordelia Lear: 0 1 2 Othello, the Moor of Venice: @**King Hamlet** -- ", @@ -289,8 +289,8 @@ class TestMissedMessages(ZulipTestCase): message_content_disabled_by_user: bool=False, message_content_disabled_by_realm: bool=False) -> None: msg_id = self.send_personal_message( - self.example_email('othello'), - self.example_email('hamlet'), + self.example_user('othello'), + self.example_user('hamlet'), 'Extremely personal message!', ) @@ -322,8 +322,8 @@ class TestMissedMessages(ZulipTestCase): def _reply_to_email_in_personal_missed_stream_messages(self, send_as_user: bool) -> None: msg_id = self.send_personal_message( - self.example_email('othello'), - self.example_email('hamlet'), + self.example_user('othello'), + self.example_user('hamlet'), 'Extremely personal message!', ) verify_body_include = ['Reply to this email directly, or view it in Zulip'] @@ -332,8 +332,8 @@ class TestMissedMessages(ZulipTestCase): def _reply_warning_in_personal_missed_stream_messages(self, send_as_user: bool) -> None: msg_id = self.send_personal_message( - self.example_email('othello'), - self.example_email('hamlet'), + self.example_user('othello'), + self.example_user('hamlet'), 'Extremely personal message!', ) verify_body_include = ['Do not reply to this email.'] @@ -343,10 +343,10 @@ class TestMissedMessages(ZulipTestCase): def _extra_context_in_huddle_missed_stream_messages_two_others(self, send_as_user: bool, show_message_content: bool=True) -> None: msg_id = self.send_huddle_message( - self.example_email('othello'), + self.example_user('othello'), [ - self.example_email('hamlet'), - self.example_email('iago'), + self.example_user('hamlet'), + self.example_user('iago'), ], 'Group personal message!', ) @@ -372,11 +372,11 @@ class TestMissedMessages(ZulipTestCase): def _extra_context_in_huddle_missed_stream_messages_three_others(self, send_as_user: bool) -> None: msg_id = self.send_huddle_message( - self.example_email('othello'), + self.example_user('othello'), [ - self.example_email('hamlet'), - self.example_email('iago'), - self.example_email('cordelia'), + self.example_user('hamlet'), + self.example_user('iago'), + self.example_user('cordelia'), ], 'Group personal message!', ) @@ -386,11 +386,11 @@ class TestMissedMessages(ZulipTestCase): self._test_cases(msg_id, verify_body_include, email_subject, send_as_user) def _extra_context_in_huddle_missed_stream_messages_many_others(self, send_as_user: bool) -> None: - msg_id = self.send_huddle_message(self.example_email('othello'), - [self.example_email('hamlet'), - self.example_email('iago'), - self.example_email('cordelia'), - self.example_email('prospero')], + msg_id = self.send_huddle_message(self.example_user('othello'), + [self.example_user('hamlet'), + self.example_user('iago'), + self.example_user('cordelia'), + self.example_user('prospero')], 'Group personal message!') verify_body_include = ['Othello, the Moor of Venice: Group personal message! -- Reply'] @@ -399,7 +399,7 @@ class TestMissedMessages(ZulipTestCase): def _deleted_message_in_missed_stream_messages(self, send_as_user: bool) -> None: msg_id = self.send_stream_message( - self.example_email('othello'), "denmark", + self.example_user('othello'), "denmark", '@**King Hamlet** to be deleted') hamlet = self.example_user('hamlet') @@ -412,8 +412,8 @@ class TestMissedMessages(ZulipTestCase): self.assertEqual(len(mail.outbox), 0) def _deleted_message_in_personal_missed_stream_messages(self, send_as_user: bool) -> None: - msg_id = self.send_personal_message(self.example_email('othello'), - self.example_email('hamlet'), + msg_id = self.send_personal_message(self.example_user('othello'), + self.example_user('hamlet'), 'Extremely personal message! to be deleted!') hamlet = self.example_user('hamlet') @@ -427,10 +427,10 @@ class TestMissedMessages(ZulipTestCase): def _deleted_message_in_huddle_missed_stream_messages(self, send_as_user: bool) -> None: msg_id = self.send_huddle_message( - self.example_email('othello'), + self.example_user('othello'), [ - self.example_email('hamlet'), - self.example_email('iago'), + self.example_user('hamlet'), + self.example_user('iago'), ], 'Group personal message!', ) @@ -597,7 +597,7 @@ class TestMissedMessages(ZulipTestCase): realm = get_realm("zulip") msg_id = self.send_personal_message( - self.example_email('othello'), self.example_email('hamlet'), + self.example_user('othello'), self.example_user('hamlet'), 'Extremely personal message with a realm emoji :green_tick:!') realm_emoji_id = realm.get_active_emoji()['green_tick']['id'] realm_emoji_url = "http://zulip.testserver/user_avatars/%s/emoji/images/%s.png" % ( @@ -611,7 +611,7 @@ class TestMissedMessages(ZulipTestCase): hamlet.emojiset = 'twitter' hamlet.save(update_fields=['emojiset']) msg_id = self.send_personal_message( - self.example_email('othello'), self.example_email('hamlet'), + self.example_user('othello'), self.example_user('hamlet'), 'Extremely personal message with a hamburger :hamburger:!') verify_body_include = [':hamburger:'] email_subject = 'PMs with Othello, the Moor of Venice' @@ -619,7 +619,7 @@ class TestMissedMessages(ZulipTestCase): def test_stream_link_in_missed_message(self) -> None: msg_id = self.send_personal_message( - self.example_email('othello'), self.example_email('hamlet'), + self.example_user('othello'), self.example_user('hamlet'), 'Come and join us in #**Verona**.') stream_id = get_stream('Verona', get_realm('zulip')).id href = "http://zulip.testserver/#narrow/stream/{stream_id}-Verona".format(stream_id=stream_id) @@ -629,14 +629,14 @@ class TestMissedMessages(ZulipTestCase): def test_sender_name_in_missed_message(self) -> None: hamlet = self.example_user('hamlet') - msg_id_1 = self.send_stream_message(self.example_email('iago'), + msg_id_1 = self.send_stream_message(self.example_user('iago'), "Denmark", '@**King Hamlet**') - msg_id_2 = self.send_stream_message(self.example_email('iago'), + msg_id_2 = self.send_stream_message(self.example_user('iago'), "Verona", '* 1\n *2') - msg_id_3 = self.send_personal_message(self.example_email('iago'), - hamlet.email, + msg_id_3 = self.send_personal_message(self.example_user('iago'), + hamlet, 'Hello') handle_missedmessage_emails(hamlet.id, [ @@ -661,11 +661,11 @@ class TestMissedMessages(ZulipTestCase): def test_multiple_missed_personal_messages(self) -> None: hamlet = self.example_user('hamlet') - msg_id_1 = self.send_personal_message(self.example_email('othello'), - hamlet.email, + msg_id_1 = self.send_personal_message(self.example_user('othello'), + hamlet, 'Personal Message 1') - msg_id_2 = self.send_personal_message(self.example_email('iago'), - hamlet.email, + msg_id_2 = self.send_personal_message(self.example_user('iago'), + hamlet, 'Personal Message 2') handle_missedmessage_emails(hamlet.id, [ @@ -680,10 +680,10 @@ class TestMissedMessages(ZulipTestCase): def test_multiple_stream_messages(self) -> None: hamlet = self.example_user('hamlet') - msg_id_1 = self.send_stream_message(self.example_email('othello'), + msg_id_1 = self.send_stream_message(self.example_user('othello'), "Denmark", 'Message1') - msg_id_2 = self.send_stream_message(self.example_email('iago'), + msg_id_2 = self.send_stream_message(self.example_user('iago'), "Denmark", 'Message2') @@ -698,10 +698,10 @@ class TestMissedMessages(ZulipTestCase): def test_multiple_stream_messages_and_mentions(self) -> None: """Subject should be stream name and topic as usual.""" hamlet = self.example_user('hamlet') - msg_id_1 = self.send_stream_message(self.example_email('iago'), + msg_id_1 = self.send_stream_message(self.example_user('iago'), "Denmark", 'Regular message') - msg_id_2 = self.send_stream_message(self.example_email('othello'), + msg_id_2 = self.send_stream_message(self.example_user('othello'), "Denmark", '@**King Hamlet**') @@ -723,17 +723,17 @@ class TestMissedMessages(ZulipTestCase): self.subscribe(user, stream_name) late_subscribed_user = self.example_user('hamlet') - self.send_stream_message(user.email, + self.send_stream_message(user, stream_name, 'Before subscribing') self.subscribe(late_subscribed_user, stream_name) - self.send_stream_message(user.email, + self.send_stream_message(user, stream_name, "After subscribing") - mention_msg_id = self.send_stream_message(user.email, + mention_msg_id = self.send_stream_message(user, stream_name, '@**King Hamlet**') @@ -751,13 +751,13 @@ class TestMissedMessages(ZulipTestCase): def test_stream_mentions_multiple_people(self) -> None: """Subject should be stream name and topic as usual.""" hamlet = self.example_user('hamlet') - msg_id_1 = self.send_stream_message(self.example_email('iago'), + msg_id_1 = self.send_stream_message(self.example_user('iago'), "Denmark", '@**King Hamlet**') - msg_id_2 = self.send_stream_message(self.example_email('othello'), + msg_id_2 = self.send_stream_message(self.example_user('othello'), "Denmark", '@**King Hamlet**') - msg_id_3 = self.send_stream_message(self.example_email('cordelia'), + msg_id_3 = self.send_stream_message(self.example_user('cordelia'), "Denmark", 'Regular message') @@ -773,10 +773,10 @@ class TestMissedMessages(ZulipTestCase): def test_multiple_stream_messages_different_topics(self) -> None: """Should receive separate emails for each topic within a stream.""" hamlet = self.example_user('hamlet') - msg_id_1 = self.send_stream_message(self.example_email('othello'), + msg_id_1 = self.send_stream_message(self.example_user('othello'), "Denmark", 'Message1') - msg_id_2 = self.send_stream_message(self.example_email('iago'), + msg_id_2 = self.send_stream_message(self.example_user('iago'), "Denmark", 'Message2', topic_name="test2") diff --git a/zerver/tests/test_embedded_bot_system.py b/zerver/tests/test_embedded_bot_system.py index a144f0415f..0131e2176e 100644 --- a/zerver/tests/test_embedded_bot_system.py +++ b/zerver/tests/test_embedded_bot_system.py @@ -23,7 +23,7 @@ class TestEmbeddedBotMessaging(ZulipTestCase): def test_pm_to_embedded_bot(self) -> None: assert self.bot_profile is not None - self.send_personal_message(self.user_profile.email, self.bot_profile.email, + self.send_personal_message(self.user_profile, self.bot_profile, content="help") last_message = self.get_last_message() self.assertEqual(last_message.content, "beep boop") @@ -37,7 +37,7 @@ class TestEmbeddedBotMessaging(ZulipTestCase): def test_stream_message_to_embedded_bot(self) -> None: assert self.bot_profile is not None - self.send_stream_message(self.user_profile.email, "Denmark", + self.send_stream_message(self.user_profile, "Denmark", content="@**{}** foo".format(self.bot_profile.full_name), topic_name="bar") last_message = self.get_last_message() @@ -48,7 +48,7 @@ class TestEmbeddedBotMessaging(ZulipTestCase): self.assertEqual(display_recipient, "Denmark") def test_stream_message_not_to_embedded_bot(self) -> None: - self.send_stream_message(self.user_profile.email, "Denmark", + self.send_stream_message(self.user_profile, "Denmark", content="foo", topic_name="bar") last_message = self.get_last_message() self.assertEqual(last_message.content, "foo") @@ -57,7 +57,7 @@ class TestEmbeddedBotMessaging(ZulipTestCase): assert self.bot_profile is not None with patch('zulip_bots.bots.helloworld.helloworld.HelloWorldHandler.initialize', create=True) as mock_initialize: - self.send_stream_message(self.user_profile.email, "Denmark", + self.send_stream_message(self.user_profile, "Denmark", content="@**{}** foo".format(self.bot_profile.full_name), topic_name="bar") mock_initialize.assert_called_once() @@ -67,7 +67,7 @@ class TestEmbeddedBotMessaging(ZulipTestCase): with patch('zulip_bots.bots.helloworld.helloworld.HelloWorldHandler.handle_message', side_effect=EmbeddedBotQuitException("I'm quitting!")): with patch('logging.warning') as mock_logging: - self.send_stream_message(self.user_profile.email, "Denmark", + self.send_stream_message(self.user_profile, "Denmark", content="@**{}** foo".format(self.bot_profile.full_name), topic_name="bar") mock_logging.assert_called_once_with("I'm quitting!") @@ -84,7 +84,7 @@ class TestEmbeddedBotFailures(ZulipTestCase): service_profile.name = 'invalid' service_profile.save() with patch('logging.error') as logging_error_mock: - self.send_stream_message(user_profile.email, "Denmark", + self.send_stream_message(user_profile, "Denmark", content="@**{}** foo".format(bot_profile.full_name), topic_name="bar") logging_error_mock.assert_called_once_with( diff --git a/zerver/tests/test_event_queue.py b/zerver/tests/test_event_queue.py index 2927c4a8d0..75e1aa3cf1 100644 --- a/zerver/tests/test_event_queue.py +++ b/zerver/tests/test_event_queue.py @@ -200,7 +200,7 @@ class MissedMessageNotificationsTest(ZulipTestCase): client = allocate_client_descriptor(queue_data) - self.send_stream_message(cordelia.email, stream_name) + self.send_stream_message(cordelia, stream_name) self.assertEqual(len(client.event_queue.contents()), 1) @@ -253,7 +253,7 @@ class MissedMessageNotificationsTest(ZulipTestCase): client_descriptor = allocate_event_queue() with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue: # To test the missed_message hook, we first need to send a message - msg_id = self.send_stream_message(self.example_email("iago"), "Denmark") + msg_id = self.send_stream_message(self.example_user("iago"), "Denmark") # Verify that nothing happens if you call it as not the # "last client descriptor", in which case the function @@ -275,7 +275,7 @@ class MissedMessageNotificationsTest(ZulipTestCase): # Test the hook with a private message; this should trigger notifications client_descriptor = allocate_event_queue() self.assertTrue(client_descriptor.event_queue.empty()) - msg_id = self.send_personal_message(self.example_email("iago"), email) + msg_id = self.send_personal_message(self.example_user("iago"), user_profile) with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue: missedmessage_hook(user_profile.id, client_descriptor, True) mock_enqueue.assert_called_once() @@ -289,7 +289,7 @@ class MissedMessageNotificationsTest(ZulipTestCase): # Test the hook with a mention; this should trigger notifications client_descriptor = allocate_event_queue() self.assertTrue(client_descriptor.event_queue.empty()) - msg_id = self.send_stream_message(self.example_email("iago"), "Denmark", + msg_id = self.send_stream_message(self.example_user("iago"), "Denmark", content="@**King Hamlet** what's up?") with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue: missedmessage_hook(user_profile.id, client_descriptor, True) @@ -304,7 +304,7 @@ class MissedMessageNotificationsTest(ZulipTestCase): # Test the hook with a wildcard mention; this should trigger notifications client_descriptor = allocate_event_queue() self.assertTrue(client_descriptor.event_queue.empty()) - msg_id = self.send_stream_message(self.example_email("iago"), "Denmark", + msg_id = self.send_stream_message(self.example_user("iago"), "Denmark", content="@**all** what's up?") with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue: missedmessage_hook(user_profile.id, client_descriptor, True) @@ -320,7 +320,7 @@ class MissedMessageNotificationsTest(ZulipTestCase): # themself using a human client; should not notify. client_descriptor = allocate_event_queue() self.assertTrue(client_descriptor.event_queue.empty()) - msg_id = self.send_stream_message(self.example_email("hamlet"), "Denmark", + msg_id = self.send_stream_message(self.example_user("hamlet"), "Denmark", content="@**all** what's up?", sending_client_name="website") with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue: @@ -337,7 +337,7 @@ class MissedMessageNotificationsTest(ZulipTestCase): change_subscription_properties(user_profile, stream, sub, {'is_muted': True}) client_descriptor = allocate_event_queue() self.assertTrue(client_descriptor.event_queue.empty()) - msg_id = self.send_stream_message(self.example_email("iago"), "Denmark", + msg_id = self.send_stream_message(self.example_user("iago"), "Denmark", content="@**all** what's up?") with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue: missedmessage_hook(user_profile.id, client_descriptor, True) @@ -355,7 +355,7 @@ class MissedMessageNotificationsTest(ZulipTestCase): user_profile.save() client_descriptor = allocate_event_queue() self.assertTrue(client_descriptor.event_queue.empty()) - msg_id = self.send_stream_message(self.example_email("iago"), "Denmark", + msg_id = self.send_stream_message(self.example_user("iago"), "Denmark", content="@**all** what's up?") with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue: missedmessage_hook(user_profile.id, client_descriptor, True) @@ -377,7 +377,7 @@ class MissedMessageNotificationsTest(ZulipTestCase): sub.save() client_descriptor = allocate_event_queue() self.assertTrue(client_descriptor.event_queue.empty()) - msg_id = self.send_stream_message(self.example_email("iago"), "Denmark", + msg_id = self.send_stream_message(self.example_user("iago"), "Denmark", content="@**all** what's up?") with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue: missedmessage_hook(user_profile.id, client_descriptor, True) @@ -397,7 +397,7 @@ class MissedMessageNotificationsTest(ZulipTestCase): change_subscription_properties(user_profile, stream, sub, {'push_notifications': True}) client_descriptor = allocate_event_queue() self.assertTrue(client_descriptor.event_queue.empty()) - msg_id = self.send_stream_message(self.example_email("iago"), "Denmark", + msg_id = self.send_stream_message(self.example_user("iago"), "Denmark", content="what's up everyone?") with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue: missedmessage_hook(user_profile.id, client_descriptor, True) @@ -415,7 +415,7 @@ class MissedMessageNotificationsTest(ZulipTestCase): {'push_notifications': False, 'email_notifications': True}) self.assertTrue(client_descriptor.event_queue.empty()) - msg_id = self.send_stream_message(self.example_email("iago"), "Denmark", + msg_id = self.send_stream_message(self.example_user("iago"), "Denmark", content="what's up everyone?") with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue: missedmessage_hook(user_profile.id, client_descriptor, True) @@ -436,7 +436,7 @@ class MissedMessageNotificationsTest(ZulipTestCase): self.assertTrue(client_descriptor.event_queue.empty()) do_mute_topic(user_profile, stream, sub.recipient, "mutingtest") - msg_id = self.send_stream_message(self.example_email("iago"), "Denmark", + msg_id = self.send_stream_message(self.example_user("iago"), "Denmark", content="what's up everyone?", topic_name="mutingtest") with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue: missedmessage_hook(user_profile.id, client_descriptor, True) @@ -457,7 +457,7 @@ class MissedMessageNotificationsTest(ZulipTestCase): self.assertTrue(client_descriptor.event_queue.empty()) change_subscription_properties(user_profile, stream, sub, {'is_muted': True}) - msg_id = self.send_stream_message(self.example_email("iago"), "Denmark", + msg_id = self.send_stream_message(self.example_user("iago"), "Denmark", content="what's up everyone?") with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue: missedmessage_hook(user_profile.id, client_descriptor, True) diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py index 993eb5398d..b646a3141e 100644 --- a/zerver/tests/test_events.py +++ b/zerver/tests/test_events.py @@ -396,8 +396,7 @@ class GetEventsTest(ZulipTestCase): def test_get_events_narrow(self) -> None: user_profile = self.example_user('hamlet') - email = user_profile.email - self.login(email) + self.login(user_profile.email) def get_message(apply_markdown: bool, client_gravatar: bool) -> Dict[str, Any]: result = self.tornado_call( @@ -426,8 +425,8 @@ class GetEventsTest(ZulipTestCase): self.assert_json_success(result) self.assert_length(events, 0) - self.send_personal_message(email, self.example_email("othello"), "hello") - self.send_stream_message(email, "Denmark", "**hello**") + self.send_personal_message(user_profile, self.example_user("othello"), "hello") + self.send_stream_message(user_profile, "Denmark", "**hello**") result = self.tornado_call(get_events, user_profile, {"queue_id": queue_id, @@ -621,7 +620,7 @@ class EventsRegisterTest(ZulipTestCase): for i in range(3): content = 'mentioning... @**' + user.full_name + '** hello ' + str(i) self.do_test( - lambda: self.send_stream_message(self.example_email('cordelia'), + lambda: self.send_stream_message(self.example_user('cordelia'), "Verona", content) @@ -631,7 +630,7 @@ class EventsRegisterTest(ZulipTestCase): for i in range(3): content = 'mentioning... @**all** hello ' + str(i) self.do_test( - lambda: self.send_stream_message(self.example_email('cordelia'), + lambda: self.send_stream_message(self.example_user('cordelia'), "Verona", content) @@ -639,19 +638,19 @@ class EventsRegisterTest(ZulipTestCase): def test_pm_send_message_events(self) -> None: self.do_test( - lambda: self.send_personal_message(self.example_email('cordelia'), - self.example_email('hamlet'), + lambda: self.send_personal_message(self.example_user('cordelia'), + self.example_user('hamlet'), 'hola') ) def test_huddle_send_message_events(self) -> None: huddle = [ - self.example_email('hamlet'), - self.example_email('othello'), + self.example_user('hamlet'), + self.example_user('othello'), ] self.do_test( - lambda: self.send_huddle_message(self.example_email('cordelia'), + lambda: self.send_huddle_message(self.example_user('cordelia'), huddle, 'hola') @@ -688,7 +687,7 @@ class EventsRegisterTest(ZulipTestCase): return schema_checker events = self.do_test( - lambda: self.send_stream_message(self.example_email("hamlet"), "Verona", "hello"), + lambda: self.send_stream_message(self.example_user("hamlet"), "Verona", "hello"), client_gravatar=False, ) schema_checker = get_checker(check_gravatar=check_string) @@ -696,7 +695,7 @@ class EventsRegisterTest(ZulipTestCase): self.assert_on_error(error) events = self.do_test( - lambda: self.send_stream_message(self.example_email("hamlet"), "Verona", "hello"), + lambda: self.send_stream_message(self.example_user("hamlet"), "Verona", "hello"), client_gravatar=True, ) schema_checker = get_checker(check_gravatar=equals(None)) @@ -784,8 +783,8 @@ class EventsRegisterTest(ZulipTestCase): ]) message = self.send_personal_message( - self.example_email("cordelia"), - self.example_email("hamlet"), + self.example_user("cordelia"), + self.example_user("hamlet"), "hello", ) user_profile = self.example_user('hamlet') @@ -816,7 +815,7 @@ class EventsRegisterTest(ZulipTestCase): for content in ['hello', mention]: message = self.send_stream_message( - self.example_email('cordelia'), + self.example_user('cordelia'), "Verona", content ) @@ -827,13 +826,14 @@ class EventsRegisterTest(ZulipTestCase): ) def test_send_message_to_existing_recipient(self) -> None: + sender = self.example_user('cordelia') self.send_stream_message( - self.example_email('cordelia'), + sender, "Verona", "hello 1" ) self.do_test( - lambda: self.send_stream_message("cordelia@zulip.com", "Verona", "hello 2"), + lambda: self.send_stream_message(sender, "Verona", "hello 2"), state_change_expected=True, ) @@ -852,7 +852,7 @@ class EventsRegisterTest(ZulipTestCase): ])), ]) - message_id = self.send_stream_message(self.example_email("hamlet"), "Verona", "hello") + message_id = self.send_stream_message(self.example_user("hamlet"), "Verona", "hello") message = Message.objects.get(id=message_id) events = self.do_test( lambda: do_add_reaction_legacy( @@ -877,7 +877,7 @@ class EventsRegisterTest(ZulipTestCase): ])), ]) - message_id = self.send_stream_message(self.example_email("hamlet"), "Verona", "hello") + message_id = self.send_stream_message(self.example_user("hamlet"), "Verona", "hello") message = Message.objects.get(id=message_id) do_add_reaction_legacy(self.user_profile, message, "tada") events = self.do_test( @@ -903,7 +903,7 @@ class EventsRegisterTest(ZulipTestCase): ])), ]) - message_id = self.send_stream_message(self.example_email("hamlet"), "Verona", "hello") + message_id = self.send_stream_message(self.example_user("hamlet"), "Verona", "hello") message = Message.objects.get(id=message_id) events = self.do_test( lambda: do_add_reaction( @@ -926,7 +926,7 @@ class EventsRegisterTest(ZulipTestCase): cordelia = self.example_user('cordelia') stream_name = 'Verona' message_id = self.send_stream_message( - sender_email=cordelia.email, + sender=cordelia, stream_name=stream_name, ) events = self.do_test( @@ -957,7 +957,7 @@ class EventsRegisterTest(ZulipTestCase): ])), ]) - message_id = self.send_stream_message(self.example_email("hamlet"), "Verona", "hello") + message_id = self.send_stream_message(self.example_user("hamlet"), "Verona", "hello") message = Message.objects.get(id=message_id) do_add_reaction(self.user_profile, message, "tada", "1f389", "unicode_emoji") events = self.do_test( @@ -2663,7 +2663,8 @@ class EventsRegisterTest(ZulipTestCase): ('stream_id', check_int), ('topic', check_string), ]) - msg_id = self.send_stream_message("hamlet@zulip.com", "Verona") + hamlet = self.example_user('hamlet') + msg_id = self.send_stream_message(hamlet, "Verona") message = Message.objects.get(id=msg_id) events = self.do_test( lambda: do_delete_messages(self.user_profile.realm, [message]), @@ -2682,8 +2683,8 @@ class EventsRegisterTest(ZulipTestCase): ('recipient_id', check_int), ]) msg_id = self.send_personal_message( - self.example_email("cordelia"), - self.user_profile.email, + self.example_user("cordelia"), + self.user_profile, "hello", ) message = Message.objects.get(id=msg_id) @@ -2699,7 +2700,7 @@ class EventsRegisterTest(ZulipTestCase): # Delete all historical messages for this user user_profile = self.example_user('hamlet') UserMessage.objects.filter(user_profile=user_profile).delete() - msg_id = self.send_stream_message("hamlet@zulip.com", "Verona") + msg_id = self.send_stream_message(user_profile, "Verona") message = Message.objects.get(id=msg_id) self.do_test( lambda: do_delete_messages(self.user_profile.realm, [message]), @@ -2773,7 +2774,7 @@ class EventsRegisterTest(ZulipTestCase): self.subscribe(hamlet, "Denmark") body = "First message ...[zulip.txt](http://{}".format(hamlet.realm.host) + data['uri'] + ")" events = self.do_test( - lambda: self.send_stream_message(self.example_email("hamlet"), "Denmark", body, "test"), + lambda: self.send_stream_message(self.example_user("hamlet"), "Denmark", body, "test"), num_events=2) error = schema_checker('events[0]', events[0]) self.assert_on_error(error) @@ -2945,7 +2946,7 @@ class GetUnreadMsgsTest(ZulipTestCase): for stream_name, topic_name in tups: message_ids[topic_name] = [ self.send_stream_message( - sender_email=cordelia.email, + sender=cordelia, stream_name=stream_name, topic_name=topic_name, ) for i in range(3) @@ -2998,16 +2999,16 @@ class GetUnreadMsgsTest(ZulipTestCase): huddle1_message_ids = [ self.send_huddle_message( - cordelia.email, - [hamlet.email, othello.email] + cordelia, + [hamlet, othello] ) for i in range(3) ] huddle2_message_ids = [ self.send_huddle_message( - cordelia.email, - [hamlet.email, prospero.email] + cordelia, + [hamlet, prospero] ) for i in range(3) ] @@ -3039,12 +3040,12 @@ class GetUnreadMsgsTest(ZulipTestCase): hamlet = self.example_user('hamlet') cordelia_pm_message_ids = [ - self.send_personal_message(cordelia.email, hamlet.email) + self.send_personal_message(cordelia, hamlet) for i in range(3) ] othello_pm_message_ids = [ - self.send_personal_message(othello.email, hamlet.email) + self.send_personal_message(othello, hamlet) for i in range(3) ] @@ -3065,9 +3066,9 @@ class GetUnreadMsgsTest(ZulipTestCase): ) def test_unread_msgs(self) -> None: - cordelia = self.example_user('cordelia') - sender_id = cordelia.id - sender_email = cordelia.email + sender = self.example_user('cordelia') + sender_id = sender.id + sender_email = sender.email user_profile = self.example_user('hamlet') othello = self.example_user('othello') @@ -3075,25 +3076,25 @@ class GetUnreadMsgsTest(ZulipTestCase): assert(sender_email < user_profile.email) assert(user_profile.email < othello.email) - pm1_message_id = self.send_personal_message(sender_email, user_profile.email, "hello1") - pm2_message_id = self.send_personal_message(sender_email, user_profile.email, "hello2") + pm1_message_id = self.send_personal_message(sender, user_profile, "hello1") + pm2_message_id = self.send_personal_message(sender, user_profile, "hello2") muted_stream = self.subscribe(user_profile, 'Muted Stream') self.mute_stream(user_profile, muted_stream) self.mute_topic(user_profile, 'Denmark', 'muted-topic') - stream_message_id = self.send_stream_message(sender_email, "Denmark", "hello") - muted_stream_message_id = self.send_stream_message(sender_email, "Muted Stream", "hello") + stream_message_id = self.send_stream_message(sender, "Denmark", "hello") + muted_stream_message_id = self.send_stream_message(sender, "Muted Stream", "hello") muted_topic_message_id = self.send_stream_message( - sender_email, + sender, "Denmark", topic_name="muted-topic", content="hello", ) huddle_message_id = self.send_huddle_message( - sender_email, - [user_profile.email, othello.email], + sender, + [user_profile, othello], 'hello3', ) diff --git a/zerver/tests/test_home.py b/zerver/tests/test_home.py index a4caacbf4c..cf395baec7 100644 --- a/zerver/tests/test_home.py +++ b/zerver/tests/test_home.py @@ -811,7 +811,7 @@ class HomeTest(ZulipTestCase): def send_test_message(self, content: str, sender_name: str='iago', stream_name: str='Denmark', topic_name: str='foo') -> None: - sender = self.example_email(sender_name) + sender = self.example_user(sender_name) self.send_stream_message(sender, stream_name, content=content, topic_name=topic_name) diff --git a/zerver/tests/test_import_export.py b/zerver/tests/test_import_export.py index 23108a790b..8ff50d062f 100644 --- a/zerver/tests/test_import_export.py +++ b/zerver/tests/test_import_export.py @@ -98,11 +98,10 @@ from zerver.lib.test_helpers import ( class QueryUtilTest(ZulipTestCase): def _create_messages(self) -> None: - for email in [self.example_email('cordelia'), - self.example_email('hamlet'), - self.example_email('iago')]: + for name in ['cordelia', 'hamlet', 'iago']: + user = self.example_user(name) for _ in range(5): - self.send_personal_message(email, self.example_email('othello')) + self.send_personal_message(user, self.example_user('othello')) @slow('creates lots of data') def test_query_chunker(self) -> None: @@ -458,9 +457,10 @@ class ImportExportTest(ZulipTestCase): def test_zulip_realm(self) -> None: realm = Realm.objects.get(string_id='zulip') - pm_a_msg_id = self.send_personal_message(self.example_email("AARON"), "default-bot@zulip.com") - pm_b_msg_id = self.send_personal_message("default-bot@zulip.com", self.example_email("iago")) - pm_c_msg_id = self.send_personal_message(self.example_email("othello"), self.example_email("hamlet")) + default_bot = self.example_user('default_bot') + pm_a_msg_id = self.send_personal_message(self.example_user("AARON"), default_bot) + pm_b_msg_id = self.send_personal_message(default_bot, self.example_user("iago")) + pm_c_msg_id = self.send_personal_message(self.example_user("othello"), self.example_user("hamlet")) realm_emoji = RealmEmoji.objects.get(realm=realm) realm_emoji.delete() @@ -502,10 +502,10 @@ class ImportExportTest(ZulipTestCase): hamlet = self.example_user('hamlet') user_ids = set([cordelia.id, hamlet.id]) - pm_a_msg_id = self.send_personal_message(self.example_email("AARON"), self.example_email("othello")) - pm_b_msg_id = self.send_personal_message(self.example_email("cordelia"), self.example_email("iago")) - pm_c_msg_id = self.send_personal_message(self.example_email("hamlet"), self.example_email("othello")) - pm_d_msg_id = self.send_personal_message(self.example_email("iago"), self.example_email("hamlet")) + pm_a_msg_id = self.send_personal_message(self.example_user("AARON"), self.example_user("othello")) + pm_b_msg_id = self.send_personal_message(self.example_user("cordelia"), self.example_user("iago")) + pm_c_msg_id = self.send_personal_message(self.example_user("hamlet"), self.example_user("othello")) + pm_d_msg_id = self.send_personal_message(self.example_user("iago"), self.example_user("hamlet")) realm_emoji = RealmEmoji.objects.get(realm=realm) realm_emoji.delete() @@ -542,42 +542,42 @@ class ImportExportTest(ZulipTestCase): create_stream_if_needed(realm, "Private A", invite_only=True) self.subscribe(self.example_user("iago"), "Private A") self.subscribe(self.example_user("othello"), "Private A") - self.send_stream_message(self.example_email("iago"), "Private A", "Hello Stream A") + self.send_stream_message(self.example_user("iago"), "Private A", "Hello Stream A") create_stream_if_needed(realm, "Private B", invite_only=True) self.subscribe(self.example_user("prospero"), "Private B") - stream_b_message_id = self.send_stream_message(self.example_email("prospero"), + stream_b_message_id = self.send_stream_message(self.example_user("prospero"), "Private B", "Hello Stream B") self.subscribe(self.example_user("hamlet"), "Private B") create_stream_if_needed(realm, "Private C", invite_only=True) self.subscribe(self.example_user("othello"), "Private C") self.subscribe(self.example_user("prospero"), "Private C") - stream_c_message_id = self.send_stream_message(self.example_email("othello"), + stream_c_message_id = self.send_stream_message(self.example_user("othello"), "Private C", "Hello Stream C") # Create huddles - self.send_huddle_message(self.example_email("iago"), [self.example_email("cordelia"), - self.example_email("AARON")]) + self.send_huddle_message(self.example_user("iago"), [self.example_user("cordelia"), + self.example_user("AARON")]) huddle_a = Huddle.objects.last() - self.send_huddle_message(self.example_email("ZOE"), [self.example_email("hamlet"), - self.example_email("AARON"), - self.example_email("othello")]) + self.send_huddle_message(self.example_user("ZOE"), [self.example_user("hamlet"), + self.example_user("AARON"), + self.example_user("othello")]) huddle_b = Huddle.objects.last() huddle_c_message_id = self.send_huddle_message( - self.example_email("AARON"), [self.example_email("cordelia"), - self.example_email("ZOE"), - self.example_email("othello")]) + self.example_user("AARON"), [self.example_user("cordelia"), + self.example_user("ZOE"), + self.example_user("othello")]) # Create PMs - pm_a_msg_id = self.send_personal_message(self.example_email("AARON"), self.example_email("othello")) - pm_b_msg_id = self.send_personal_message(self.example_email("cordelia"), self.example_email("iago")) - pm_c_msg_id = self.send_personal_message(self.example_email("hamlet"), self.example_email("othello")) - pm_d_msg_id = self.send_personal_message(self.example_email("iago"), self.example_email("hamlet")) + pm_a_msg_id = self.send_personal_message(self.example_user("AARON"), self.example_user("othello")) + pm_b_msg_id = self.send_personal_message(self.example_user("cordelia"), self.example_user("iago")) + pm_c_msg_id = self.send_personal_message(self.example_user("hamlet"), self.example_user("othello")) + pm_d_msg_id = self.send_personal_message(self.example_user("iago"), self.example_user("hamlet")) # Send message advertising export and make users react - self.send_stream_message(self.example_email("othello"), "Verona", + self.send_stream_message(self.example_user("othello"), "Verona", topic_name="Export", content="Thumbs up for export") message = Message.objects.last() @@ -698,24 +698,24 @@ class ImportExportTest(ZulipTestCase): RealmEmoji.objects.get(realm=original_realm).delete() # data to test import of huddles huddle = [ - self.example_email('hamlet'), - self.example_email('othello') + self.example_user('hamlet'), + self.example_user('othello') ] self.send_huddle_message( - self.example_email('cordelia'), huddle, 'test huddle message' + self.example_user('cordelia'), huddle, 'test huddle message' ) user_mention_message = '@**King Hamlet** Hello' - self.send_stream_message(self.example_email("iago"), "Verona", user_mention_message) + self.send_stream_message(self.example_user("iago"), "Verona", user_mention_message) stream_mention_message = 'Subscribe to #**Denmark**' - self.send_stream_message(self.example_email("hamlet"), "Verona", stream_mention_message) + self.send_stream_message(self.example_user("hamlet"), "Verona", stream_mention_message) user_group_mention_message = 'Hello @*hamletcharacters*' - self.send_stream_message(self.example_email("othello"), "Verona", user_group_mention_message) + self.send_stream_message(self.example_user("othello"), "Verona", user_group_mention_message) special_characters_message = "```\n'\n```\n@**Polonius**" - self.send_stream_message(self.example_email("iago"), "Denmark", special_characters_message) + self.send_stream_message(self.example_user("iago"), "Denmark", special_characters_message) sample_user = self.example_user('hamlet') diff --git a/zerver/tests/test_link_embed.py b/zerver/tests/test_link_embed.py index 2bf7577058..5fa3b7ac46 100644 --- a/zerver/tests/test_link_embed.py +++ b/zerver/tests/test_link_embed.py @@ -7,7 +7,7 @@ from requests.exceptions import ConnectionError from django.test import override_settings from django.utils.html import escape -from zerver.models import Message, Realm +from zerver.models import Message, Realm, UserProfile from zerver.lib.actions import queue_json_publish from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_helpers import MockPythonResponse @@ -269,9 +269,9 @@ class PreviewTestCase(ZulipTestCase): @override_settings(INLINE_URL_EMBED_PREVIEW=True) def test_edit_message_history(self) -> None: - email = self.example_email('hamlet') - self.login(email) - msg_id = self.send_stream_message(email, "Scotland", + user = self.example_user('hamlet') + self.login(user.email) + msg_id = self.send_stream_message(user, "Scotland", topic_name="editing", content="original") url = 'http://test.org/' @@ -296,13 +296,13 @@ class PreviewTestCase(ZulipTestCase): self.assertIn(embedded_link, msg.rendered_content) @override_settings(INLINE_URL_EMBED_PREVIEW=True) - def _send_message_with_test_org_url(self, sender_email: str, queue_should_run: bool=True, + def _send_message_with_test_org_url(self, sender: UserProfile, queue_should_run: bool=True, relative_url: bool=False) -> Message: url = 'http://test.org/' with mock.patch('zerver.lib.actions.queue_json_publish') as patched: msg_id = self.send_personal_message( - sender_email, - self.example_email('cordelia'), + sender, + self.example_user('cordelia'), content=url, ) if queue_should_run: @@ -334,12 +334,12 @@ class PreviewTestCase(ZulipTestCase): @override_settings(INLINE_URL_EMBED_PREVIEW=True) def test_message_update_race_condition(self) -> None: - email = self.example_email('hamlet') - self.login(email) + user = self.example_user('hamlet') + self.login(user.email) original_url = 'http://test.org/' edited_url = 'http://edited.org/' with mock.patch('zerver.lib.actions.queue_json_publish') as patched: - msg_id = self.send_stream_message(email, "Scotland", + msg_id = self.send_stream_message(user, "Scotland", topic_name="foo", content=original_url) patched.assert_called_once() queue = patched.call_args[0][0] @@ -383,30 +383,30 @@ class PreviewTestCase(ZulipTestCase): embedded_link = 'The Rock'.format(url) # When humans send, we should get embedded content. - msg = self._send_message_with_test_org_url(sender_email=self.example_email('hamlet')) + msg = self._send_message_with_test_org_url(sender=self.example_user('hamlet')) self.assertIn(embedded_link, msg.rendered_content) # We don't want embedded content for bots. - msg = self._send_message_with_test_org_url(sender_email='webhook-bot@zulip.com', + msg = self._send_message_with_test_org_url(sender=self.example_user('webhook_bot'), queue_should_run=False) self.assertNotIn(embedded_link, msg.rendered_content) # Try another human to make sure bot failure was due to the # bot sending the message and not some other reason. - msg = self._send_message_with_test_org_url(sender_email=self.example_email('prospero')) + msg = self._send_message_with_test_org_url(sender=self.example_user('prospero')) self.assertIn(embedded_link, msg.rendered_content) def test_inline_url_embed_preview(self) -> None: with_preview = '

http://test.org/

\n
Description text
' without_preview = '

http://test.org/

' - msg = self._send_message_with_test_org_url(sender_email=self.example_email('hamlet')) + msg = self._send_message_with_test_org_url(sender=self.example_user('hamlet')) self.assertEqual(msg.rendered_content, with_preview) realm = msg.get_realm() setattr(realm, 'inline_url_embed_preview', False) realm.save() - msg = self._send_message_with_test_org_url(sender_email=self.example_email('prospero'), queue_should_run=False) + msg = self._send_message_with_test_org_url(sender=self.example_user('prospero'), queue_should_run=False) self.assertEqual(msg.rendered_content, without_preview) @override_settings(INLINE_URL_EMBED_PREVIEW=True) @@ -414,8 +414,8 @@ class PreviewTestCase(ZulipTestCase): # Relative urls should not be sent for url preview. with mock.patch('zerver.lib.actions.queue_json_publish') as patched: self.send_personal_message( - self.example_email('prospero'), - self.example_email('cordelia'), + self.example_user('prospero'), + self.example_user('cordelia'), content="http://zulip.testserver/api/", ) patched.assert_not_called() @@ -423,14 +423,14 @@ class PreviewTestCase(ZulipTestCase): def test_inline_url_embed_preview_with_relative_image_url(self) -> None: with_preview_relative = '

http://test.org/

\n
Description text
' # Try case where the opengraph image is a relative url. - msg = self._send_message_with_test_org_url(sender_email=self.example_email('prospero'), relative_url=True) + msg = self._send_message_with_test_org_url(sender=self.example_user('prospero'), relative_url=True) self.assertEqual(msg.rendered_content, with_preview_relative) def test_http_error_get_data(self) -> None: url = 'http://test.org/' msg_id = self.send_personal_message( - self.example_email('hamlet'), - self.example_email('cordelia'), + self.example_user('hamlet'), + self.example_user('cordelia'), content=url, ) msg = Message.objects.select_related("sender").get(id=msg_id) @@ -466,11 +466,11 @@ class PreviewTestCase(ZulipTestCase): @override_settings(INLINE_URL_EMBED_PREVIEW=True) def test_link_preview_non_html_data(self) -> None: - email = self.example_email('hamlet') - self.login(email) + user = self.example_user('hamlet') + self.login(user.email) url = 'http://test.org/audio.mp3' with mock.patch('zerver.lib.actions.queue_json_publish') as patched: - msg_id = self.send_stream_message(email, "Scotland", topic_name="foo", content=url) + msg_id = self.send_stream_message(user, "Scotland", topic_name="foo", content=url) patched.assert_called_once() queue = patched.call_args[0][0] self.assertEqual(queue, "embed_links") @@ -494,11 +494,11 @@ class PreviewTestCase(ZulipTestCase): @override_settings(INLINE_URL_EMBED_PREVIEW=True) def test_link_preview_no_open_graph_image(self) -> None: - email = self.example_email('hamlet') - self.login(email) + user = self.example_user('hamlet') + self.login(user.email) url = 'http://test.org/foo.html' with mock.patch('zerver.lib.actions.queue_json_publish') as patched: - msg_id = self.send_stream_message(email, "Scotland", topic_name="foo", content=url) + msg_id = self.send_stream_message(user, "Scotland", topic_name="foo", content=url) patched.assert_called_once() queue = patched.call_args[0][0] self.assertEqual(queue, "embed_links") @@ -522,11 +522,11 @@ class PreviewTestCase(ZulipTestCase): @override_settings(INLINE_URL_EMBED_PREVIEW=True) def test_link_preview_open_graph_image_missing_content(self) -> None: - email = self.example_email('hamlet') - self.login(email) + user = self.example_user('hamlet') + self.login(user.email) url = 'http://test.org/foo.html' with mock.patch('zerver.lib.actions.queue_json_publish') as patched: - msg_id = self.send_stream_message(email, "Scotland", topic_name="foo", content=url) + msg_id = self.send_stream_message(user, "Scotland", topic_name="foo", content=url) patched.assert_called_once() queue = patched.call_args[0][0] self.assertEqual(queue, "embed_links") @@ -551,11 +551,11 @@ class PreviewTestCase(ZulipTestCase): @override_settings(INLINE_URL_EMBED_PREVIEW=True) def test_link_preview_no_content_type_header(self) -> None: - email = self.example_email('hamlet') - self.login(email) + user = self.example_user('hamlet') + self.login(user.email) url = 'http://test.org/' with mock.patch('zerver.lib.actions.queue_json_publish') as patched: - msg_id = self.send_stream_message(email, "Scotland", topic_name="foo", content=url) + msg_id = self.send_stream_message(user, "Scotland", topic_name="foo", content=url) patched.assert_called_once() queue = patched.call_args[0][0] self.assertEqual(queue, "embed_links") @@ -580,8 +580,8 @@ class PreviewTestCase(ZulipTestCase): url = 'http://test.org/' with mock.patch('zerver.lib.actions.queue_json_publish'): msg_id = self.send_personal_message( - self.example_email('hamlet'), - self.example_email('cordelia'), + self.example_user('hamlet'), + self.example_user('cordelia'), content=url, ) msg = Message.objects.select_related("sender").get(id=msg_id) @@ -611,8 +611,8 @@ class PreviewTestCase(ZulipTestCase): error_url = 'http://test.org/x' with mock.patch('zerver.lib.actions.queue_json_publish'): msg_id = self.send_personal_message( - self.example_email('hamlet'), - self.example_email('cordelia'), + self.example_user('hamlet'), + self.example_user('cordelia'), content=error_url, ) msg = Message.objects.select_related("sender").get(id=msg_id) @@ -640,8 +640,8 @@ class PreviewTestCase(ZulipTestCase): url = 'http://test.org/' with mock.patch('zerver.lib.actions.queue_json_publish'): msg_id = self.send_personal_message( - self.example_email('hamlet'), - self.example_email('cordelia'), + self.example_user('hamlet'), + self.example_user('cordelia'), content=url, ) msg = Message.objects.select_related("sender").get(id=msg_id) @@ -670,8 +670,8 @@ class PreviewTestCase(ZulipTestCase): url = 'https://www.youtube.com/watch?v=eSJTXC7Ixgg' with mock.patch('zerver.lib.actions.queue_json_publish'): msg_id = self.send_personal_message( - self.example_email('hamlet'), - self.example_email('cordelia'), + self.example_user('hamlet'), + self.example_user('cordelia'), content=url, ) msg = Message.objects.select_related("sender").get(id=msg_id) diff --git a/zerver/tests/test_management_commands.py b/zerver/tests/test_management_commands.py index 74e88f5349..a8c77098ce 100644 --- a/zerver/tests/test_management_commands.py +++ b/zerver/tests/test_management_commands.py @@ -406,7 +406,7 @@ class TestExport(ZulipTestCase): def test_command_with_consented_message_id(self) -> None: realm = get_realm("zulip") - self.send_stream_message(self.example_email("othello"), "Verona", + self.send_stream_message(self.example_user("othello"), "Verona", topic_name="Export", content="Thumbs up for export") message = Message.objects.last() diff --git a/zerver/tests/test_message_edit_notifications.py b/zerver/tests/test_message_edit_notifications.py index 5f3c951c8c..60b7039dd7 100644 --- a/zerver/tests/test_message_edit_notifications.py +++ b/zerver/tests/test_message_edit_notifications.py @@ -45,8 +45,8 @@ class EditMessageSideEffectsTest(ZulipTestCase): self.login(hamlet.email) message_id = self.send_personal_message( - hamlet.email, - cordelia.email, + hamlet, + cordelia, content='no mention' ) @@ -75,7 +75,7 @@ class EditMessageSideEffectsTest(ZulipTestCase): self.subscribe(cordelia, 'Scotland') message_id = self.send_stream_message( - hamlet.email, + hamlet, 'Scotland', content=content, ) diff --git a/zerver/tests/test_messages.py b/zerver/tests/test_messages.py index 6db4faf682..ed9f88842f 100644 --- a/zerver/tests/test_messages.py +++ b/zerver/tests/test_messages.py @@ -135,8 +135,8 @@ class TopicHistoryTest(ZulipTestCase): # Send a message to this new stream from another user self.subscribe(self.mit_user("starnine"), stream_name) stream = get_stream(stream_name, user_profile.realm) - self.send_stream_message(self.mit_email("starnine"), stream_name, - topic_name="secret topic", sender_realm="zephyr") + self.send_stream_message(self.mit_user("starnine"), stream_name, + topic_name="secret topic") # Now subscribe this MIT user to the new stream and verify # that the new topic is not accessible @@ -297,8 +297,8 @@ class TopicDeleteTest(ZulipTestCase): # Send message stream = get_stream(stream_name, user_profile.realm) - self.send_stream_message(user_profile.email, stream_name, topic_name=topic_name) - last_msg_id = self.send_stream_message(user_profile.email, stream_name, topic_name=topic_name) + self.send_stream_message(user_profile, stream_name, topic_name=topic_name) + last_msg_id = self.send_stream_message(user_profile, stream_name, topic_name=topic_name) # Deleting the topic self.login(user_profile.email, realm=user_profile.realm) @@ -317,7 +317,7 @@ class TopicDeleteTest(ZulipTestCase): user_profile = self.example_user('iago') self.subscribe(user_profile, stream_name) self.login(user_profile.email, realm=user_profile.realm) - new_last_msg_id = self.send_stream_message(user_profile.email, stream_name, topic_name=topic_name) + new_last_msg_id = self.send_stream_message(user_profile, stream_name, topic_name=topic_name) # Now admin deletes all messages in topic -- which should only # delete new_last_msg_id, i.e. the one sent since they joined. @@ -392,7 +392,7 @@ class TestCrossRealmPMs(ZulipTestCase): user1 = self.create_user(user1_email) user1a = self.create_user(user1a_email) user2 = self.create_user(user2_email) - self.create_user(user3_email) + user3 = self.create_user(user3_email) notification_bot = get_system_bot(notification_bot_email) with self.settings(CROSS_REALM_BOT_EMAILS=['notification-bot@zulip.com', 'welcome-bot@zulip.com']): # HACK: We should probably be creating this "bot" user another @@ -401,11 +401,11 @@ class TestCrossRealmPMs(ZulipTestCase): support_bot = self.create_user(support_email) # Users can PM themselves - self.send_personal_message(user1_email, user1_email, sender_realm="1.example.com") + self.send_personal_message(user1, user1) assert_message_received(user1, user1) # Users on the same realm can PM each other - self.send_personal_message(user1_email, user1a_email, sender_realm="1.example.com") + self.send_personal_message(user1, user1a) assert_message_received(user1a, user1) # Cross-realm bots in the zulip.com realm can PM any realm @@ -419,13 +419,13 @@ class TestCrossRealmPMs(ZulipTestCase): assert_message_received(user2, notification_bot) # All users can PM cross-realm bots in the zulip.com realm - self.send_personal_message(user1_email, notification_bot_email, sender_realm="1.example.com") + self.send_personal_message(user1, notification_bot) assert_message_received(notification_bot, user1) # Users can PM cross-realm bots on non-zulip realms. # (The support bot represents some theoretical bot that we may # create in the future that does not have zulip.com as its realm.) - self.send_personal_message(user1_email, support_email, sender_realm="1.example.com") + self.send_personal_message(user1, support_bot) assert_message_received(support_bot, user1) # Allow sending PMs to two different cross-realm bots simultaneously. @@ -433,39 +433,34 @@ class TestCrossRealmPMs(ZulipTestCase): # already individually send PMs to cross-realm bots, we shouldn't # prevent them from sending multiple bots at once. We may revisit # this if it's a nuisance for huddles.) - self.send_huddle_message(user1_email, [notification_bot_email, support_email], - sender_realm="1.example.com") + self.send_huddle_message(user1, [notification_bot, support_bot]) assert_message_received(notification_bot, user1) assert_message_received(support_bot, user1) # Prevent old loophole where I could send PMs to other users as long # as I copied a cross-realm bot from the same realm. with assert_invalid_email(): - self.send_huddle_message(user1_email, [user3_email, support_email], - sender_realm="1.example.com") + self.send_huddle_message(user1, [user3, support_bot]) # Users on three different realms can't PM each other, # even if one of the users is a cross-realm bot. with assert_invalid_email(): - self.send_huddle_message(user1_email, [user2_email, notification_bot_email], - sender_realm="1.example.com") + self.send_huddle_message(user1, [user2, notification_bot]) with assert_invalid_email(): - self.send_huddle_message(notification_bot_email, [user1_email, user2_email], - sender_realm=settings.SYSTEM_BOT_REALM) + self.send_huddle_message(notification_bot, [user1, user2]) # Users on the different realms cannot PM each other with assert_invalid_email(): - self.send_personal_message(user1_email, user2_email, sender_realm="1.example.com") + self.send_personal_message(user1, user2) # Users on non-zulip realms can't PM "ordinary" Zulip users with assert_invalid_email(): - self.send_personal_message(user1_email, "hamlet@zulip.com", sender_realm="1.example.com") + self.send_personal_message(user1, self.example_user('hamlet')) # Users on three different realms cannot PM each other with assert_invalid_email(): - self.send_huddle_message(user1_email, [user2_email, user3_email], - sender_realm="1.example.com") + self.send_huddle_message(user1, [user2, user3]) class TestAddressee(ZulipTestCase): def test_addressee_for_user_ids(self) -> None: @@ -760,8 +755,8 @@ class PersonalMessagesTest(ZulipTestCase): Make sure `is_private` flag is not leaked to the API. """ self.login(self.example_email("hamlet")) - self.send_personal_message(self.example_email("hamlet"), - self.example_email("cordelia"), + self.send_personal_message(self.example_user("hamlet"), + self.example_user("cordelia"), "test") for msg in self.get_messages(): @@ -776,7 +771,7 @@ class PersonalMessagesTest(ZulipTestCase): self.register(test_email, "test") user_profile = self.nonreg_user('test') old_messages_count = message_stream_count(user_profile) - self.send_personal_message(test_email, test_email) + self.send_personal_message(user_profile, user_profile) new_messages_count = message_stream_count(user_profile) self.assertEqual(new_messages_count, old_messages_count + 1) @@ -808,7 +803,8 @@ class PersonalMessagesTest(ZulipTestCase): for user_profile in old_user_profiles: old_messages.append(message_stream_count(user_profile)) - self.send_personal_message(test_email, test_email) + user_profile = self.nonreg_user('test1') + self.send_personal_message(user_profile, user_profile) new_messages = [] for user_profile in old_user_profiles: @@ -838,7 +834,7 @@ class PersonalMessagesTest(ZulipTestCase): for user_profile in other_user_profiles: old_other_messages.append(message_stream_count(user_profile)) - self.send_personal_message(sender_email, receiver_email, content) + self.send_personal_message(sender, receiver, content) # Users outside the conversation don't get the message. new_other_messages = [] @@ -873,12 +869,12 @@ class PersonalMessagesTest(ZulipTestCase): do_set_realm_property(user_profile.realm, "private_message_policy", Realm.PRIVATE_MESSAGE_POLICY_DISABLED) with self.assertRaises(JsonableError): - self.send_personal_message(user_profile.email, self.example_email("cordelia")) + self.send_personal_message(user_profile, self.example_user("cordelia")) bot_profile = self.create_test_bot("testbot", user_profile) - self.send_personal_message(user_profile.email, settings.NOTIFICATION_BOT) - self.send_personal_message(user_profile.email, bot_profile.email) - self.send_personal_message(bot_profile.email, user_profile.email) + self.send_personal_message(user_profile, get_system_bot(settings.NOTIFICATION_BOT)) + self.send_personal_message(user_profile, bot_profile) + self.send_personal_message(bot_profile, user_profile) def test_non_ascii_personal(self) -> None: """ @@ -913,9 +909,9 @@ class StreamMessagesTest(ZulipTestCase): non_bot_subscribers = [user_profile for user_profile in subscribers if not user_profile.is_bot] - a_subscriber_email = non_bot_subscribers[0].email - self.login(a_subscriber_email) - self.send_stream_message(a_subscriber_email, stream_name, + a_subscriber = non_bot_subscribers[0] + self.login(a_subscriber.email) + self.send_stream_message(a_subscriber, stream_name, content=content, topic_name=topic_name) # Did all of the subscribers get the message? @@ -1028,7 +1024,7 @@ class StreamMessagesTest(ZulipTestCase): def test_stream_message_dict(self) -> None: user_profile = self.example_user('iago') self.subscribe(user_profile, "Denmark") - self.send_stream_message(self.example_email("hamlet"), "Denmark", + self.send_stream_message(self.example_user("hamlet"), "Denmark", content="whatever", topic_name="my topic") message = most_recent_message(user_profile) row = MessageDict.get_raw_db_rows([message.id])[0] @@ -1043,7 +1039,7 @@ class StreamMessagesTest(ZulipTestCase): receiving_user_profile = self.example_user('iago') sending_user_profile = self.example_user('hamlet') self.subscribe(receiving_user_profile, "Denmark") - self.send_stream_message(sending_user_profile.email, "Denmark", + self.send_stream_message(sending_user_profile, "Denmark", content="whatever", topic_name="my topic") message = most_recent_message(receiving_user_profile) self.assertEqual(str(message), @@ -1053,7 +1049,7 @@ class StreamMessagesTest(ZulipTestCase): def test_message_mentions(self) -> None: user_profile = self.example_user('iago') self.subscribe(user_profile, "Denmark") - self.send_stream_message(self.example_email("hamlet"), "Denmark", + self.send_stream_message(self.example_user("hamlet"), "Denmark", content="test @**Iago** rules") message = most_recent_message(user_profile) assert(UserMessage.objects.get(user_profile=user_profile, message=message).flags.mentioned.is_set) @@ -1062,20 +1058,20 @@ class StreamMessagesTest(ZulipTestCase): user_profile = self.example_user('iago') self.subscribe(user_profile, "Denmark") - self.send_stream_message(self.example_email("hamlet"), "Denmark", + self.send_stream_message(self.example_user("hamlet"), "Denmark", content="test") message = most_recent_message(user_profile) self.assertFalse(UserMessage.objects.get(user_profile=user_profile, message=message).flags.is_private.is_set) - self.send_personal_message(self.example_email("hamlet"), user_profile.email, + self.send_personal_message(self.example_user("hamlet"), user_profile, content="test") message = most_recent_message(user_profile) self.assertTrue(UserMessage.objects.get(user_profile=user_profile, message=message).flags.is_private.is_set) - def _send_stream_message(self, email: str, stream_name: str, content: str) -> Set[int]: + def _send_stream_message(self, user: UserProfile, stream_name: str, content: str) -> Set[int]: with mock.patch('zerver.lib.actions.send_event') as m: self.send_stream_message( - email, + user, stream_name, content=content ) @@ -1100,7 +1096,7 @@ class StreamMessagesTest(ZulipTestCase): content = 'test @**Cordelia Lear** rules' user_ids = self._send_stream_message( - email=hamlet.email, + user=hamlet, stream_name=stream_name, content=content ) @@ -1145,7 +1141,7 @@ class StreamMessagesTest(ZulipTestCase): content = 'test @**Normal Bot** rules' user_ids = self._send_stream_message( - email=hamlet.email, + user=hamlet, stream_name=stream_name, content=content ) @@ -1213,12 +1209,8 @@ class StreamMessagesTest(ZulipTestCase): self.example_user('othello'), ] - message1_id = self.send_huddle_message(users[0].email, - [user.email for user in users], - "test content 1") - message2_id = self.send_huddle_message(users[0].email, - [user.email for user in users], - "test content 2") + message1_id = self.send_huddle_message(users[0], users, "test content 1") + message2_id = self.send_huddle_message(users[0], users, "test content 2") msg_data = get_raw_unread_data(users[1]) @@ -1365,8 +1357,8 @@ class MessageDictTest(ZulipTestCase): ' https://trac.zulip.net/ticket/%(id)s>') def get_message(sender: UserProfile) -> Message: - msg_id = self.send_stream_message(sender.email, 'Denmark', 'hello world', topic_name, - sender.realm.string_id, zulip_realm) + msg_id = self.send_stream_message(sender, 'Denmark', 'hello world', topic_name, + zulip_realm) return Message.objects.get(id=msg_id) def assert_topic_links(links: List[str], msg: Message) -> None: @@ -1472,14 +1464,14 @@ class SewMessageAndReactionTest(ZulipTestCase): class MessagePOSTTest(ZulipTestCase): - def _send_and_verify_message(self, email: str, stream_name: str, error_msg: str=None) -> None: + def _send_and_verify_message(self, user: UserProfile, stream_name: str, error_msg: str=None) -> None: if error_msg is None: - msg_id = self.send_stream_message(email, stream_name) - result = self.api_get(email, '/json/messages/' + str(msg_id)) + msg_id = self.send_stream_message(user, stream_name) + result = self.api_get(user.email, '/json/messages/' + str(msg_id)) self.assert_json_success(result) else: with self.assertRaisesRegex(JsonableError, error_msg): - self.send_stream_message(email, stream_name) + self.send_stream_message(user, stream_name) def test_message_to_self(self) -> None: """ @@ -1558,26 +1550,26 @@ class MessagePOSTTest(ZulipTestCase): do_change_stream_post_policy(stream, Stream.STREAM_POST_POLICY_ADMINS) # Admins and their owned bots can send to STREAM_POST_POLICY_ADMINS streams - self._send_and_verify_message(admin_profile.email, stream_name) + self._send_and_verify_message(admin_profile, stream_name) admin_owned_bot = self.create_test_bot( short_name='whatever1', full_name='whatever1', user_profile=admin_profile, ) - self._send_and_verify_message(admin_owned_bot.email, stream_name) + self._send_and_verify_message(admin_owned_bot, stream_name) non_admin_profile = self.example_user("hamlet") self.login(non_admin_profile.email) # Non admins and their owned bots cannot send to STREAM_POST_POLICY_ADMINS streams - self._send_and_verify_message(non_admin_profile.email, stream_name, + self._send_and_verify_message(non_admin_profile, stream_name, "Only organization administrators can send to this stream.") non_admin_owned_bot = self.create_test_bot( short_name='whatever2', full_name='whatever2', user_profile=non_admin_profile, ) - self._send_and_verify_message(non_admin_owned_bot.email, stream_name, + self._send_and_verify_message(non_admin_owned_bot, stream_name, "Only organization administrators can send to this stream.") # Bots without owner (except cross realm bot) cannot send to announcement only streams @@ -1589,7 +1581,7 @@ class MessagePOSTTest(ZulipTestCase): short_name='freebot', bot_type=UserProfile.DEFAULT_BOT, ) - self._send_and_verify_message(bot_without_owner.email, stream_name, + self._send_and_verify_message(bot_without_owner, stream_name, "Only organization administrators can send to this stream.") # Cross realm bots should be allowed @@ -1617,13 +1609,13 @@ class MessagePOSTTest(ZulipTestCase): # Admins and their owned bots can send to STREAM_POST_POLICY_RESTRICT_NEW_MEMBERS streams, # even if the admin is a new user - self._send_and_verify_message(admin_profile.email, stream_name) + self._send_and_verify_message(admin_profile, stream_name) admin_owned_bot = self.create_test_bot( short_name='whatever1', full_name='whatever1', user_profile=admin_profile, ) - self._send_and_verify_message(admin_owned_bot.email, stream_name) + self._send_and_verify_message(admin_owned_bot, stream_name) non_admin_profile = self.example_user("hamlet") self.login(non_admin_profile.email) @@ -1635,14 +1627,14 @@ class MessagePOSTTest(ZulipTestCase): # Non admins and their owned bots can send to STREAM_POST_POLICY_RESTRICT_NEW_MEMBERS streams, # if the user is not a new member - self._send_and_verify_message(non_admin_profile.email, stream_name, + self._send_and_verify_message(non_admin_profile, stream_name, "New members cannot send to this stream.") non_admin_owned_bot = self.create_test_bot( short_name='whatever2', full_name='whatever2', user_profile=non_admin_profile, ) - self._send_and_verify_message(non_admin_owned_bot.email, stream_name, + self._send_and_verify_message(non_admin_owned_bot, stream_name, "New members cannot send to this stream.") # Bots without owner (except cross realm bot) cannot send to announcement only stream @@ -1654,7 +1646,7 @@ class MessagePOSTTest(ZulipTestCase): short_name='freebot', bot_type=UserProfile.DEFAULT_BOT, ) - self._send_and_verify_message(bot_without_owner.email, stream_name, + self._send_and_verify_message(bot_without_owner, stream_name, "New members cannot send to this stream.") # Cross realm bots should be allowed @@ -2483,7 +2475,7 @@ class EditMessageTest(ZulipTestCase): """This is also tested by a client test, but here we can verify the cache against the database""" self.login(self.example_email("hamlet")) - msg_id = self.send_stream_message(self.example_email("hamlet"), "Scotland", + msg_id = self.send_stream_message(self.example_user("hamlet"), "Scotland", topic_name="editing", content="before edit") result = self.client_patch("/json/messages/" + str(msg_id), { 'message_id': msg_id, @@ -2502,8 +2494,8 @@ class EditMessageTest(ZulipTestCase): def test_fetch_raw_message(self) -> None: self.login(self.example_email("hamlet")) msg_id = self.send_personal_message( - from_email=self.example_email("hamlet"), - to_email=self.example_email("cordelia"), + from_user=self.example_user("hamlet"), + to_user=self.example_user("cordelia"), content="**before** edit", ) result = self.client_get('/json/messages/' + str(msg_id)) @@ -2527,7 +2519,7 @@ class EditMessageTest(ZulipTestCase): self.login(user_profile.email) stream = self.make_stream('public_stream') self.subscribe(user_profile, stream.name) - msg_id = self.send_stream_message(user_profile.email, stream.name, + msg_id = self.send_stream_message(user_profile, stream.name, topic_name="test", content="test") result = self.client_get('/json/messages/' + str(msg_id)) self.assert_json_success(result) @@ -2541,7 +2533,7 @@ class EditMessageTest(ZulipTestCase): self.login(user_profile.email) stream = self.make_stream('private_stream', invite_only=True) self.subscribe(user_profile, stream.name) - msg_id = self.send_stream_message(user_profile.email, stream.name, + msg_id = self.send_stream_message(user_profile, stream.name, topic_name="test", content="test") result = self.client_get('/json/messages/' + str(msg_id)) self.assert_json_success(result) @@ -2551,7 +2543,7 @@ class EditMessageTest(ZulipTestCase): def test_edit_message_no_permission(self) -> None: self.login(self.example_email("hamlet")) - msg_id = self.send_stream_message(self.example_email("iago"), "Scotland", + msg_id = self.send_stream_message(self.example_user("iago"), "Scotland", topic_name="editing", content="before edit") result = self.client_patch("/json/messages/" + str(msg_id), { 'message_id': msg_id, @@ -2561,7 +2553,7 @@ class EditMessageTest(ZulipTestCase): def test_edit_message_no_changes(self) -> None: self.login(self.example_email("hamlet")) - msg_id = self.send_stream_message(self.example_email("hamlet"), "Scotland", + msg_id = self.send_stream_message(self.example_user("hamlet"), "Scotland", topic_name="editing", content="before edit") result = self.client_patch("/json/messages/" + str(msg_id), { 'message_id': msg_id, @@ -2570,7 +2562,7 @@ class EditMessageTest(ZulipTestCase): def test_edit_message_no_topic(self) -> None: self.login(self.example_email("hamlet")) - msg_id = self.send_stream_message(self.example_email("hamlet"), "Scotland", + msg_id = self.send_stream_message(self.example_user("hamlet"), "Scotland", topic_name="editing", content="before edit") result = self.client_patch("/json/messages/" + str(msg_id), { 'message_id': msg_id, @@ -2580,7 +2572,7 @@ class EditMessageTest(ZulipTestCase): def test_edit_message_no_content(self) -> None: self.login(self.example_email("hamlet")) - msg_id = self.send_stream_message(self.example_email("hamlet"), "Scotland", + msg_id = self.send_stream_message(self.example_user("hamlet"), "Scotland", topic_name="editing", content="before edit") result = self.client_patch("/json/messages/" + str(msg_id), { 'message_id': msg_id, @@ -2596,7 +2588,7 @@ class EditMessageTest(ZulipTestCase): self.login(self.example_email("hamlet")) # Single-line edit - msg_id_1 = self.send_stream_message(self.example_email("hamlet"), + msg_id_1 = self.send_stream_message(self.example_user("hamlet"), "Denmark", topic_name="editing", content="content before edit") @@ -2626,7 +2618,7 @@ class EditMessageTest(ZulipTestCase): # Single-line edit msg_id_1 = self.send_stream_message( - self.example_email("hamlet"), + self.example_user("hamlet"), "Scotland", topic_name="editing", content="content before edit") @@ -2658,7 +2650,7 @@ class EditMessageTest(ZulipTestCase): # Edits on new lines msg_id_2 = self.send_stream_message( - self.example_email("hamlet"), + self.example_user("hamlet"), "Scotland", topic_name="editing", content=('content before edit, line 1\n' @@ -2697,7 +2689,7 @@ class EditMessageTest(ZulipTestCase): # Link editing self.login(self.example_email("hamlet")) msg_id_1 = self.send_stream_message( - self.example_email("hamlet"), + self.example_user("hamlet"), "Scotland", topic_name="editing", content="Here is a link to [zulip](www.zulip.org).") @@ -2731,7 +2723,7 @@ class EditMessageTest(ZulipTestCase): self.login(self.example_email('hamlet')) msg_id = self.send_stream_message( - self.example_email('hamlet'), + self.example_user('hamlet'), 'Scotland', topic_name='editing', content='This message has not been edited.') @@ -2751,7 +2743,7 @@ class EditMessageTest(ZulipTestCase): self.subscribe(hamlet, 'Scotland') self.subscribe(cordelia, 'Scotland') - msg_id = self.send_stream_message(hamlet.email, 'Scotland', + msg_id = self.send_stream_message(hamlet, 'Scotland', content='@**Cordelia Lear**') user_info = get_user_info_for_message_updates(msg_id) @@ -2767,7 +2759,7 @@ class EditMessageTest(ZulipTestCase): history data structures.""" self.login(self.example_email("hamlet")) hamlet = self.example_user('hamlet') - msg_id = self.send_stream_message(self.example_email("hamlet"), "Scotland", + msg_id = self.send_stream_message(self.example_user("hamlet"), "Scotland", topic_name="topic 1", content="content 1") result = self.client_patch("/json/messages/" + str(msg_id), { 'message_id': msg_id, @@ -2914,7 +2906,7 @@ class EditMessageTest(ZulipTestCase): self.login(self.example_email("iago")) # send a message in the past - id_ = self.send_stream_message(self.example_email("iago"), "Scotland", + id_ = self.send_stream_message(self.example_user("iago"), "Scotland", content="content", topic_name="topic") message = Message.objects.get(id=id_) message.date_sent = message.date_sent - datetime.timedelta(seconds=180) @@ -2976,7 +2968,7 @@ class EditMessageTest(ZulipTestCase): self.login(self.example_email("iago")) # send a message in the past - id_ = self.send_stream_message(self.example_email("hamlet"), "Scotland", + id_ = self.send_stream_message(self.example_user("hamlet"), "Scotland", content="content", topic_name="topic") message = Message.objects.get(id=id_) message.date_sent = message.date_sent - datetime.timedelta(seconds=180) @@ -3024,7 +3016,7 @@ class EditMessageTest(ZulipTestCase): self.make_stream(stream_name, history_public_to_subscribers=True) self.subscribe(hamlet, stream_name) self.login(hamlet.email) - message_id = self.send_stream_message(hamlet.email, stream_name, "Where am I?") + message_id = self.send_stream_message(hamlet, stream_name, "Where am I?") self.login(cordelia.email) self.subscribe(cordelia, stream_name) @@ -3097,7 +3089,7 @@ class EditMessageTest(ZulipTestCase): self.subscribe(hamlet, stream_name) self.subscribe(cordelia, stream_name) self.login(hamlet.email) - message_id = self.send_stream_message(hamlet.email, stream_name, "Hello everyone") + message_id = self.send_stream_message(hamlet, stream_name, "Hello everyone") def notify(user_id: int) -> Dict[str, Any]: return { @@ -3126,15 +3118,15 @@ class EditMessageTest(ZulipTestCase): def test_propagate_topic_forward(self) -> None: self.login(self.example_email("hamlet")) - id1 = self.send_stream_message(self.example_email("hamlet"), "Scotland", + id1 = self.send_stream_message(self.example_user("hamlet"), "Scotland", topic_name="topic1") - id2 = self.send_stream_message(self.example_email("iago"), "Scotland", + id2 = self.send_stream_message(self.example_user("iago"), "Scotland", topic_name="topic1") - id3 = self.send_stream_message(self.example_email("iago"), "Rome", + id3 = self.send_stream_message(self.example_user("iago"), "Rome", topic_name="topic1") - id4 = self.send_stream_message(self.example_email("hamlet"), "Scotland", + id4 = self.send_stream_message(self.example_user("hamlet"), "Scotland", topic_name="topic2") - id5 = self.send_stream_message(self.example_email("iago"), "Scotland", + id5 = self.send_stream_message(self.example_user("iago"), "Scotland", topic_name="topic1") result = self.client_patch("/json/messages/" + str(id1), { @@ -3152,17 +3144,17 @@ class EditMessageTest(ZulipTestCase): def test_propagate_all_topics(self) -> None: self.login(self.example_email("hamlet")) - id1 = self.send_stream_message(self.example_email("hamlet"), "Scotland", + id1 = self.send_stream_message(self.example_user("hamlet"), "Scotland", topic_name="topic1") - id2 = self.send_stream_message(self.example_email("hamlet"), "Scotland", + id2 = self.send_stream_message(self.example_user("hamlet"), "Scotland", topic_name="topic1") - id3 = self.send_stream_message(self.example_email("iago"), "Rome", + id3 = self.send_stream_message(self.example_user("iago"), "Rome", topic_name="topic1") - id4 = self.send_stream_message(self.example_email("hamlet"), "Scotland", + id4 = self.send_stream_message(self.example_user("hamlet"), "Scotland", topic_name="topic2") - id5 = self.send_stream_message(self.example_email("iago"), "Scotland", + id5 = self.send_stream_message(self.example_user("iago"), "Scotland", topic_name="topic1") - id6 = self.send_stream_message(self.example_email("iago"), "Scotland", + id6 = self.send_stream_message(self.example_user("iago"), "Scotland", topic_name="topic3") result = self.client_patch("/json/messages/" + str(id2), { @@ -3327,8 +3319,8 @@ class MirroredMessageUsersTest(ZulipTestCase): class MessageAccessTests(ZulipTestCase): def test_update_invalid_flags(self) -> None: message = self.send_personal_message( - self.example_email("cordelia"), - self.example_email("hamlet"), + self.example_user("cordelia"), + self.example_user("hamlet"), "hello", ) @@ -3370,8 +3362,8 @@ class MessageAccessTests(ZulipTestCase): POST /json/messages/flags. """ self.login(self.example_email("hamlet")) - message_ids = [self.send_personal_message(self.example_email("hamlet"), - self.example_email("hamlet"), + message_ids = [self.send_personal_message(self.example_user("hamlet"), + self.example_user("hamlet"), "test")] # Star a message. @@ -3401,16 +3393,16 @@ class MessageAccessTests(ZulipTestCase): self.subscribe(self.example_user("hamlet"), stream_name) self.login(self.example_email("hamlet")) message_ids = [ - self.send_stream_message(self.example_email("hamlet"), stream_name, "test"), + self.send_stream_message(self.example_user("hamlet"), stream_name, "test"), ] # Send a second message so we can verify it isn't modified other_message_ids = [ - self.send_stream_message(self.example_email("hamlet"), stream_name, "test_unused"), + self.send_stream_message(self.example_user("hamlet"), stream_name, "test_unused"), ] received_message_ids = [ self.send_personal_message( - self.example_email("hamlet"), - self.example_email("cordelia"), + self.example_user("hamlet"), + self.example_user("cordelia"), "test_received" ), ] @@ -3420,8 +3412,8 @@ class MessageAccessTests(ZulipTestCase): # Send a message to yourself to make sure we have at least one with the read flag sent_message_ids = [ self.send_personal_message( - self.example_email("cordelia"), - self.example_email("cordelia"), + self.example_user("cordelia"), + self.example_user("cordelia"), "test_read_message", ), ] @@ -3470,8 +3462,8 @@ class MessageAccessTests(ZulipTestCase): self.login(self.example_email("hamlet")) message_ids = [ self.send_personal_message( - self.example_email("hamlet"), - self.example_email("hamlet"), + self.example_user("hamlet"), + self.example_user("hamlet"), "test", ), ] @@ -3487,7 +3479,7 @@ class MessageAccessTests(ZulipTestCase): self.subscribe(self.example_user("hamlet"), stream_name) self.login(self.example_email("hamlet")) message_ids = [ - self.send_stream_message(self.example_email("hamlet"), stream_name, "test"), + self.send_stream_message(self.example_user("hamlet"), stream_name, "test"), ] # Starring private stream messages you received works @@ -3505,7 +3497,7 @@ class MessageAccessTests(ZulipTestCase): self.subscribe(self.example_user("hamlet"), stream_name) self.login(self.example_email("hamlet")) message_ids = [ - self.send_stream_message(self.example_email("hamlet"), stream_name, "test"), + self.send_stream_message(self.example_user("hamlet"), stream_name, "test"), ] # With stream.history_public_to_subscribers = True, you still @@ -3524,10 +3516,10 @@ class MessageAccessTests(ZulipTestCase): """ New messages aren't starred. """ - test_email = self.example_email('hamlet') - self.login(test_email) + sender = self.example_user('hamlet') + self.login(sender.email) content = "Test message for star" - self.send_stream_message(test_email, "Verona", + self.send_stream_message(sender, "Verona", content=content) sent_message = UserMessage.objects.filter( @@ -3545,7 +3537,7 @@ class MessageAccessTests(ZulipTestCase): self.login(normal_user.email) message_id = [ - self.send_stream_message(normal_user.email, stream_name, "test 1") + self.send_stream_message(normal_user, stream_name, "test 1") ] guest_user = self.example_user('polonius') @@ -3561,7 +3553,7 @@ class MessageAccessTests(ZulipTestCase): # And messages sent after they join self.login(normal_user.email) message_id = [ - self.send_stream_message(normal_user.email, stream_name, "test 2") + self.send_stream_message(normal_user, stream_name, "test 2") ] self.login(guest_user.email) result = self.change_star(message_id) @@ -3576,7 +3568,7 @@ class MessageAccessTests(ZulipTestCase): self.login(normal_user.email) message_id = [ - self.send_stream_message(normal_user.email, stream_name, "test 1") + self.send_stream_message(normal_user, stream_name, "test 1") ] guest_user = self.example_user('polonius') @@ -3600,7 +3592,7 @@ class MessageAccessTests(ZulipTestCase): do_change_stream_invite_only(stream, True, history_public_to_subscribers=False) self.login(normal_user.email) message_id = [ - self.send_stream_message(normal_user.email, stream_name, "test 2") + self.send_stream_message(normal_user, stream_name, "test 2") ] self.login(guest_user.email) result = self.change_star(message_id) @@ -3616,7 +3608,7 @@ class MessageAccessTests(ZulipTestCase): self.subscribe(user, stream_name) # Send a message before subscribing a new user to stream - message_one_id = self.send_stream_message(user.email, + message_one_id = self.send_stream_message(user, stream_name, "Message one") later_subscribed_user = self.example_user("cordelia") @@ -3624,7 +3616,7 @@ class MessageAccessTests(ZulipTestCase): self.subscribe(later_subscribed_user, stream_name) # Send a message after subscribing a new user to stream - message_two_id = self.send_stream_message(user.email, + message_two_id = self.send_stream_message(user, stream_name, "Message two") message_ids = [message_one_id, message_two_id] @@ -3660,14 +3652,14 @@ class MessageAccessTests(ZulipTestCase): # Testing messages accessiblity including a public stream message stream_name = "public_stream" self.subscribe(user, stream_name) - message_one_id = self.send_stream_message(user.email, + message_one_id = self.send_stream_message(user, stream_name, "Message one") later_subscribed_user = self.example_user("cordelia") self.subscribe(later_subscribed_user, stream_name) # Send a message after subscribing a new user to stream - message_two_id = self.send_stream_message(user.email, + message_two_id = self.send_stream_message(user, stream_name, "Message two") message_ids = [message_one_id, message_two_id] @@ -3717,24 +3709,24 @@ class MessageHasKeywordsTest(ZulipTestCase): body = ("Some files here ...[zulip.txt]({})" + "{}.... Some more...." + "{}").format(dummy_urls[0], dummy_urls[1], dummy_urls[1]) - self.send_stream_message(user_profile.email, "Denmark", body, "test") + self.send_stream_message(user_profile, "Denmark", body, "test") assert_attachment_claimed(dummy_path_ids[0], True) assert_attachment_claimed(dummy_path_ids[1], False) # This message tries to claim the third attachment but fails because # Bugdown would not set has_attachments = True here. body = "Link in code: `{}`".format(dummy_urls[2]) - self.send_stream_message(user_profile.email, "Denmark", body, "test") + self.send_stream_message(user_profile, "Denmark", body, "test") assert_attachment_claimed(dummy_path_ids[2], False) # Another scenario where we wouldn't parse the link. body = "Link to not parse: .{}.`".format(dummy_urls[2]) - self.send_stream_message(user_profile.email, "Denmark", body, "test") + self.send_stream_message(user_profile, "Denmark", body, "test") assert_attachment_claimed(dummy_path_ids[2], False) # Finally, claim attachment 3. body = "Link: {}".format(dummy_urls[2]) - self.send_stream_message(user_profile.email, "Denmark", body, "test") + self.send_stream_message(user_profile, "Denmark", body, "test") assert_attachment_claimed(dummy_path_ids[2], True) assert_attachment_claimed(dummy_path_ids[1], False) @@ -3742,7 +3734,7 @@ class MessageHasKeywordsTest(ZulipTestCase): msg_ids = [] msg_contents = ["foo.org", "[bar](baz.gov)", "http://quux.ca"] for msg_content in msg_contents: - msg_ids.append(self.send_stream_message(self.example_email('hamlet'), + msg_ids.append(self.send_stream_message(self.example_user('hamlet'), 'Denmark', content=msg_content)) msgs = [Message.objects.get(id=id) for id in msg_ids] self.assertTrue(all([msg.has_link for msg in msgs])) @@ -3751,7 +3743,7 @@ class MessageHasKeywordsTest(ZulipTestCase): msg_ids = [] msg_contents = ["`example.org`", '``example.org```', '$$https://example.org$$', "foo"] for msg_content in msg_contents: - msg_ids.append(self.send_stream_message(self.example_email('hamlet'), + msg_ids.append(self.send_stream_message(self.example_user('hamlet'), 'Denmark', content=msg_content)) msgs = [Message.objects.get(id=id) for id in msg_ids] self.assertFalse(all([msg.has_link for msg in msgs])) @@ -3765,7 +3757,7 @@ class MessageHasKeywordsTest(ZulipTestCase): def test_finds_link_after_edit(self) -> None: hamlet = self.example_user('hamlet') - msg_id = self.send_stream_message(hamlet.email, 'Denmark', content='a') + msg_id = self.send_stream_message(hamlet, 'Denmark', content='a') msg = Message.objects.get(id=msg_id) self.assertFalse(msg.has_link) @@ -3786,7 +3778,7 @@ class MessageHasKeywordsTest(ZulipTestCase): "Image: https://www.google.com/images/srpr/logo4w.pdf", "[Google Link](https://www.google.com/images/srpr/logo4w.png)"] for msg_content in msg_contents: - msg_ids.append(self.send_stream_message(self.example_email('hamlet'), + msg_ids.append(self.send_stream_message(self.example_user('hamlet'), 'Denmark', content=msg_content)) msgs = [Message.objects.get(id=id) for id in msg_ids] self.assertEqual([False, True, False, True], [msg.has_image for msg in msgs]) @@ -3804,7 +3796,7 @@ class MessageHasKeywordsTest(ZulipTestCase): body = ("Files ...[zulip.txt]({}) {} {}").format(dummy_urls[0], dummy_urls[1], dummy_urls[2]) - msg_id = self.send_stream_message(hamlet.email, "Denmark", body, "test") + msg_id = self.send_stream_message(hamlet, "Denmark", body, "test") msg = Message.objects.get(id=msg_id) self.assertTrue(msg.has_attachment) self.update_message(msg, 'No Attachments') @@ -3838,7 +3830,7 @@ class MessageHasKeywordsTest(ZulipTestCase): dummy_path_ids = self.setup_dummy_attachments(hamlet) body = "Hello" - msg_id = self.send_stream_message(hamlet.email, "Denmark", body, "test") + msg_id = self.send_stream_message(hamlet, "Denmark", body, "test") msg = Message.objects.get(id=msg_id) with mock.patch("zerver.lib.actions.do_claim_attachments", @@ -3915,12 +3907,12 @@ class MissedMessageTest(ZulipTestCase): class LogDictTest(ZulipTestCase): def test_to_log_dict(self) -> None: - email = self.example_email('hamlet') + user = self.example_user('hamlet') stream_name = 'Denmark' topic_name = 'Copenhagen' content = 'find me some good coffee shops' # self.login(self.example_email("hamlet")) - message_id = self.send_stream_message(email, stream_name, + message_id = self.send_stream_message(user, stream_name, topic_name=topic_name, content=content) message = Message.objects.get(id=message_id) @@ -4029,7 +4021,8 @@ class CheckMessageTest(ZulipTestCase): class DeleteMessageTest(ZulipTestCase): def test_delete_message_invalid_request_format(self) -> None: self.login("iago@zulip.com") - msg_id = self.send_stream_message("hamlet@zulip.com", "Scotland") + hamlet = self.example_user('hamlet') + msg_id = self.send_stream_message(hamlet, "Scotland") result = self.client_delete('/json/messages/{msg_id}'.format(msg_id=msg_id + 1), {'message_id': msg_id}) self.assert_json_error(result, "Invalid message(s)") @@ -4063,8 +4056,9 @@ class DeleteMessageTest(ZulipTestCase): # Test if message deleting is not allowed(default). set_message_deleting_params(False, 0) - self.login("hamlet@zulip.com") - msg_id = self.send_stream_message("hamlet@zulip.com", "Scotland") + hamlet = self.example_user('hamlet') + self.login(hamlet.email) + msg_id = self.send_stream_message(hamlet, "Scotland") result = test_delete_message_by_owner(msg_id=msg_id) self.assert_json_error(result, "You don't have permission to delete this message") @@ -4078,7 +4072,7 @@ class DeleteMessageTest(ZulipTestCase): # Test if message deleting is allowed. # Test if time limit is zero(no limit). set_message_deleting_params(True, 0) - msg_id = self.send_stream_message("hamlet@zulip.com", "Scotland") + msg_id = self.send_stream_message(hamlet, "Scotland") message = Message.objects.get(id=msg_id) message.date_sent = message.date_sent - datetime.timedelta(seconds=600) message.save() @@ -4091,12 +4085,12 @@ class DeleteMessageTest(ZulipTestCase): # Test if time limit is non-zero. set_message_deleting_params(True, 240) - msg_id_1 = self.send_stream_message("hamlet@zulip.com", "Scotland") + msg_id_1 = self.send_stream_message(hamlet, "Scotland") message = Message.objects.get(id=msg_id_1) message.date_sent = message.date_sent - datetime.timedelta(seconds=120) message.save() - msg_id_2 = self.send_stream_message("hamlet@zulip.com", "Scotland") + msg_id_2 = self.send_stream_message(hamlet, "Scotland") message = Message.objects.get(id=msg_id_2) message.date_sent = message.date_sent - datetime.timedelta(seconds=360) message.save() @@ -4114,7 +4108,7 @@ class DeleteMessageTest(ZulipTestCase): self.assert_json_success(result) # Test mulitple delete requests with no latency issues - msg_id = self.send_stream_message("hamlet@zulip.com", "Scotland") + msg_id = self.send_stream_message(hamlet, "Scotland") result = test_delete_message_by_owner(msg_id=msg_id) self.assert_json_success(result) result = test_delete_message_by_owner(msg_id=msg_id) @@ -4139,7 +4133,7 @@ class SoftDeactivationMessageTest(ZulipTestCase): for user_profile in recipient_list: self.subscribe(user_profile, "Denmark") - sender = self.example_email('iago') + sender = self.example_user('iago') stream_name = 'Denmark' topic_name = 'foo' @@ -4151,7 +4145,7 @@ class SoftDeactivationMessageTest(ZulipTestCase): long_term_idle_user = self.example_user('hamlet') # We are sending this message to ensure that long_term_idle_user has # at least one UserMessage row. - self.send_stream_message(long_term_idle_user.email, stream_name) + self.send_stream_message(long_term_idle_user, stream_name) do_soft_deactivate_users([long_term_idle_user]) message = 'Test Message 1' @@ -4196,7 +4190,7 @@ class SoftDeactivationMessageTest(ZulipTestCase): return message long_term_idle_user = self.example_user('hamlet') - self.send_stream_message(long_term_idle_user.email, stream_name) + self.send_stream_message(long_term_idle_user, stream_name) do_soft_deactivate_users([long_term_idle_user]) # Test that add_missing_messages() in simplest case of adding a @@ -4296,7 +4290,7 @@ class SoftDeactivationMessageTest(ZulipTestCase): self.subscribe(long_term_idle_user, stream_name) # Send a real message to update last_active_message_id sent_message_id = self.send_stream_message( - sender.email, stream_name, 'Test Message 9') + sender, stream_name, 'Test Message 9') self.unsubscribe(long_term_idle_user, stream_name) # Soft deactivate and send another message to the unsubscribed stream. do_soft_deactivate_users([long_term_idle_user]) @@ -4353,13 +4347,13 @@ class SoftDeactivationMessageTest(ZulipTestCase): sender = self.example_user('iago') long_term_idle_user = self.example_user('hamlet') - self.send_stream_message(long_term_idle_user.email, stream_name) + self.send_stream_message(long_term_idle_user, stream_name) do_soft_deactivate_users([long_term_idle_user]) num_new_messages = 5 message_ids = [] for _ in range(num_new_messages): - message_id = self.send_stream_message(sender.email, stream_name) + message_id = self.send_stream_message(sender, stream_name) message_ids.append(message_id) idle_user_msg_list = get_user_messages(long_term_idle_user) @@ -4385,7 +4379,7 @@ class SoftDeactivationMessageTest(ZulipTestCase): self.subscribe(user_profile, "Denmark") cordelia = self.example_user('cordelia') - sender = self.example_email('iago') + sender = self.example_user('iago') stream_name = 'Denmark' topic_name = 'foo' @@ -4394,10 +4388,10 @@ class SoftDeactivationMessageTest(ZulipTestCase): content, topic_name) def send_personal_message(content: str) -> None: - self.send_personal_message(sender, self.example_email("hamlet"), content) + self.send_personal_message(sender, self.example_user("hamlet"), content) long_term_idle_user = self.example_user('hamlet') - self.send_stream_message(long_term_idle_user.email, stream_name) + self.send_stream_message(long_term_idle_user, stream_name) do_soft_deactivate_users([long_term_idle_user]) def assert_um_count(user: UserProfile, count: int) -> None: @@ -4593,12 +4587,12 @@ class MessageHydrationTest(ZulipTestCase): stream_name = 'test stream' self.subscribe(cordelia, stream_name) - old_message_id = self.send_stream_message(cordelia.email, stream_name, content='foo') + old_message_id = self.send_stream_message(cordelia, stream_name, content='foo') self.subscribe(hamlet, stream_name) content = 'hello @**King Hamlet**' - new_message_id = self.send_stream_message(cordelia.email, stream_name, content=content) + new_message_id = self.send_stream_message(cordelia, stream_name, content=content) user_message_flags = { old_message_id: ['read', 'historical'], @@ -4639,7 +4633,7 @@ class MessageHydrationTest(ZulipTestCase): hamlet = self.example_user('hamlet') cordelia = self.example_user('cordelia') - message_id = self.send_personal_message(hamlet.email, cordelia.email, 'test') + message_id = self.send_personal_message(hamlet, cordelia, 'test') cordelia_recipient = cordelia.recipient # Cause the display_recipient to get cached: @@ -4693,8 +4687,8 @@ class TestMessageForIdsDisplayRecipientFetching(ZulipTestCase): cordelia = self.example_user('cordelia') othello = self.example_user('othello') message_ids = [ - self.send_personal_message(hamlet.email, cordelia.email, 'test'), - self.send_personal_message(cordelia.email, othello.email, 'test') + self.send_personal_message(hamlet, cordelia, 'test'), + self.send_personal_message(cordelia, othello, 'test') ] messages = messages_for_ids( @@ -4712,8 +4706,8 @@ class TestMessageForIdsDisplayRecipientFetching(ZulipTestCase): def test_display_recipient_stream(self) -> None: cordelia = self.example_user('cordelia') message_ids = [ - self.send_stream_message(cordelia.email, "Verona", content='test'), - self.send_stream_message(cordelia.email, "Denmark", content='test') + self.send_stream_message(cordelia, "Verona", content='test'), + self.send_stream_message(cordelia, "Denmark", content='test') ] messages = messages_for_ids( @@ -4734,8 +4728,8 @@ class TestMessageForIdsDisplayRecipientFetching(ZulipTestCase): othello = self.example_user('othello') iago = self.example_user('iago') message_ids = [ - self.send_huddle_message(hamlet.email, [cordelia.email, othello.email], 'test'), - self.send_huddle_message(cordelia.email, [hamlet.email, othello.email, iago.email], 'test') + self.send_huddle_message(hamlet, [cordelia, othello], 'test'), + self.send_huddle_message(cordelia, [hamlet, othello, iago], 'test') ] messages = messages_for_ids( @@ -4756,12 +4750,12 @@ class TestMessageForIdsDisplayRecipientFetching(ZulipTestCase): othello = self.example_user('othello') iago = self.example_user('iago') message_ids = [ - self.send_huddle_message(hamlet.email, [cordelia.email, othello.email], 'test'), - self.send_stream_message(cordelia.email, "Verona", content='test'), - self.send_personal_message(hamlet.email, cordelia.email, 'test'), - self.send_stream_message(cordelia.email, "Denmark", content='test'), - self.send_huddle_message(cordelia.email, [hamlet.email, othello.email, iago.email], 'test'), - self.send_personal_message(cordelia.email, othello.email, 'test') + self.send_huddle_message(hamlet, [cordelia, othello], 'test'), + self.send_stream_message(cordelia, "Verona", content='test'), + self.send_personal_message(hamlet, cordelia, 'test'), + self.send_stream_message(cordelia, "Denmark", content='test'), + self.send_huddle_message(cordelia, [hamlet, othello, iago], 'test'), + self.send_personal_message(cordelia, othello, 'test') ] messages = messages_for_ids( @@ -4783,7 +4777,7 @@ class TestMessageForIdsDisplayRecipientFetching(ZulipTestCase): class MessageVisibilityTest(ZulipTestCase): def test_update_first_visible_message_id(self) -> None: Message.objects.all().delete() - message_ids = [self.send_stream_message(self.example_email("othello"), "Scotland") for i in range(15)] + message_ids = [self.send_stream_message(self.example_user("othello"), "Scotland") for i in range(15)] # If message_visibility_limit is None update_first_visible_message_id # should set first_visible_message_id to 0 @@ -4845,8 +4839,8 @@ class TestBulkGetHuddleUserIds(ZulipTestCase): othello = self.example_user('othello') iago = self.example_user('iago') message_ids = [ - self.send_huddle_message(hamlet.email, [cordelia.email, othello.email], 'test'), - self.send_huddle_message(cordelia.email, [hamlet.email, othello.email, iago.email], 'test') + self.send_huddle_message(hamlet, [cordelia, othello], 'test'), + self.send_huddle_message(cordelia, [hamlet, othello, iago], 'test') ] messages = Message.objects.filter(id__in=message_ids).order_by("id") diff --git a/zerver/tests/test_narrow.py b/zerver/tests/test_narrow.py index 0dae4dd776..85299ad6b8 100644 --- a/zerver/tests/test_narrow.py +++ b/zerver/tests/test_narrow.py @@ -323,8 +323,8 @@ class NarrowBuilderTest(ZulipTestCase): self._do_add_term_test(term, 'WHERE 1 != 1') # Test with at least one such group PM thread existing - self.send_huddle_message(self.user_profile.email, [self.example_email("othello"), - self.example_email("cordelia")]) + self.send_huddle_message(self.user_profile, [self.example_user("othello"), + self.example_user("cordelia")]) term = dict(operator='group-pm-with', operand=self.example_email("othello")) self._do_add_term_test(term, 'WHERE recipient_id IN (%(recipient_id_1)s)') @@ -1164,7 +1164,7 @@ class GetOldMessagesTest(ZulipTestCase): hamlet = self.example_user('hamlet') self.login(hamlet.email) - self.send_personal_message(hamlet.email, self.example_email("iago")) + self.send_personal_message(hamlet, self.example_user("iago")) result = self.get_and_check_messages({}) message = result['messages'][0] @@ -1186,30 +1186,30 @@ class GetOldMessagesTest(ZulipTestCase): A request for old messages with a narrow by pm-with only returns conversations with that user. """ - me = self.example_email('hamlet') + me = self.example_user('hamlet') def dr_emails(dr: DisplayRecipientT) -> str: assert isinstance(dr, list) - return ','.join(sorted(set([r['email'] for r in dr] + [me]))) + return ','.join(sorted(set([r['email'] for r in dr] + [me.email]))) def dr_ids(dr: DisplayRecipientT) -> List[int]: assert isinstance(dr, list) return list(sorted(set([r['id'] for r in dr] + [self.example_user('hamlet').id]))) - self.send_personal_message(me, self.example_email("iago")) + self.send_personal_message(me, self.example_user("iago")) self.send_huddle_message( me, - [self.example_email("iago"), self.example_email("cordelia")], + [self.example_user("iago"), self.example_user("cordelia")], ) # Send a 1:1 and group PM containing Aaron. # Then deactivate aaron to test pm-with narrow includes messages # from deactivated users also. - self.send_personal_message(me, self.example_email("aaron")) + self.send_personal_message(me, self.example_user("aaron")) self.send_huddle_message( me, - [self.example_email("iago"), self.example_email("aaron")], + [self.example_user("iago"), self.example_user("aaron")], ) aaron = self.example_user("aaron") do_deactivate_user(aaron) @@ -1219,7 +1219,7 @@ class GetOldMessagesTest(ZulipTestCase): if not m.is_stream_message()] for personal in personals: emails = dr_emails(get_display_recipient(personal.recipient)) - self.login(me) + self.login(me.email) narrow = [dict(operator='pm-with', operand=emails)] # type: List[Dict[str, Any]] result = self.get_and_check_messages(dict(narrow=ujson.dumps(narrow))) @@ -1235,13 +1235,13 @@ class GetOldMessagesTest(ZulipTestCase): self.assertEqual(dr_emails(message['display_recipient']), emails) def test_get_visible_messages_with_narrow_pm_with(self) -> None: - me = self.example_email('hamlet') - self.login(me) + me = self.example_user('hamlet') + self.login(me.email) self.subscribe(self.example_user("hamlet"), 'Scotland') message_ids = [] for i in range(5): - message_ids.append(self.send_personal_message(me, self.example_email("iago"))) + message_ids.append(self.send_personal_message(me, self.example_user("iago"))) narrow = [dict(operator='pm-with', operand=self.example_email("iago"))] self.message_visibility_test(narrow, message_ids, 2) @@ -1251,7 +1251,7 @@ class GetOldMessagesTest(ZulipTestCase): A request for old messages with a narrow by group-pm-with only returns group-private conversations with that user. """ - me = self.example_email("hamlet") + me = self.example_user("hamlet") matching_message_ids = [] @@ -1259,9 +1259,9 @@ class GetOldMessagesTest(ZulipTestCase): self.send_huddle_message( me, [ - self.example_email("iago"), - self.example_email("cordelia"), - self.example_email("othello"), + self.example_user("iago"), + self.example_user("cordelia"), + self.example_user("othello"), ], ), ) @@ -1270,8 +1270,8 @@ class GetOldMessagesTest(ZulipTestCase): self.send_huddle_message( me, [ - self.example_email("cordelia"), - self.example_email("othello"), + self.example_user("cordelia"), + self.example_user("othello"), ], ), ) @@ -1279,30 +1279,30 @@ class GetOldMessagesTest(ZulipTestCase): non_matching_message_ids = [] non_matching_message_ids.append( - self.send_personal_message(me, self.example_email("cordelia")), + self.send_personal_message(me, self.example_user("cordelia")), ) non_matching_message_ids.append( self.send_huddle_message( me, [ - self.example_email("iago"), - self.example_email("othello"), + self.example_user("iago"), + self.example_user("othello"), ], ), ) non_matching_message_ids.append( self.send_huddle_message( - self.example_email("cordelia"), + self.example_user("cordelia"), [ - self.example_email("iago"), - self.example_email("othello"), + self.example_user("iago"), + self.example_user("othello"), ], ), ) - self.login(me) + self.login(me.email) test_operands = [self.example_email("cordelia"), self.example_user("cordelia").id] for operand in test_operands: narrow = [dict(operator='group-pm-with', operand=operand)] @@ -1312,17 +1312,17 @@ class GetOldMessagesTest(ZulipTestCase): self.assertNotIn(message["id"], non_matching_message_ids) def test_get_visible_messages_with_narrow_group_pm_with(self) -> None: - me = self.example_email('hamlet') - self.login(me) + me = self.example_user('hamlet') + self.login(me.email) message_ids = [] message_ids.append( self.send_huddle_message( me, [ - self.example_email("iago"), - self.example_email("cordelia"), - self.example_email("othello"), + self.example_user("iago"), + self.example_user("cordelia"), + self.example_user("othello"), ], ), ) @@ -1330,8 +1330,8 @@ class GetOldMessagesTest(ZulipTestCase): self.send_huddle_message( me, [ - self.example_email("cordelia"), - self.example_email("othello"), + self.example_user("cordelia"), + self.example_user("othello"), ], ), ) @@ -1339,8 +1339,8 @@ class GetOldMessagesTest(ZulipTestCase): self.send_huddle_message( me, [ - self.example_email("cordelia"), - self.example_email("iago"), + self.example_user("cordelia"), + self.example_user("iago"), ], ), ) @@ -1355,12 +1355,12 @@ class GetOldMessagesTest(ZulipTestCase): stream_name = 'test stream' self.subscribe(cordelia, stream_name) - old_message_id = self.send_stream_message(cordelia.email, stream_name, content='foo') + old_message_id = self.send_stream_message(cordelia, stream_name, content='foo') self.subscribe(hamlet, stream_name) content = 'hello @**King Hamlet**' - new_message_id = self.send_stream_message(cordelia.email, stream_name, content=content) + new_message_id = self.send_stream_message(cordelia, stream_name, content=content) self.login(hamlet.email) narrow = [ @@ -1399,7 +1399,7 @@ class GetOldMessagesTest(ZulipTestCase): # it to ensure that we actually have a stream message in this # narrow view. self.subscribe(self.example_user("hamlet"), 'Scotland') - self.send_stream_message(self.example_email("hamlet"), "Scotland") + self.send_stream_message(self.example_user("hamlet"), "Scotland") messages = get_user_messages(self.example_user('hamlet')) stream_messages = [msg for msg in messages if msg.is_stream_message()] stream_name = get_display_recipient(stream_messages[0].recipient) @@ -1421,7 +1421,7 @@ class GetOldMessagesTest(ZulipTestCase): message_ids = [] for i in range(5): - message_ids.append(self.send_stream_message(self.example_email("iago"), "Scotland")) + message_ids.append(self.send_stream_message(self.example_user("iago"), "Scotland")) narrow = [dict(operator='stream', operand="Scotland")] self.message_visibility_test(narrow, message_ids, 2) @@ -1442,8 +1442,8 @@ class GetOldMessagesTest(ZulipTestCase): lambda_stream_d_name = u"\u03bb-stream.d" self.subscribe(self.mit_user("starnine"), lambda_stream_d_name) - self.send_stream_message(self.mit_email("starnine"), u"\u03bb-stream", sender_realm="zephyr") - self.send_stream_message(self.mit_email("starnine"), u"\u03bb-stream.d", sender_realm="zephyr") + self.send_stream_message(self.mit_user("starnine"), u"\u03bb-stream") + self.send_stream_message(self.mit_user("starnine"), u"\u03bb-stream.d") narrow = [dict(operator='stream', operand=u'\u03bb-stream')] result = self.get_and_check_messages(dict(num_after=2, @@ -1471,16 +1471,11 @@ class GetOldMessagesTest(ZulipTestCase): # it to ensure that we actually have a stream message in this # narrow view. self.subscribe(mit_user_profile, "Scotland") - self.send_stream_message(email, "Scotland", topic_name=u"\u03bb-topic", - sender_realm="zephyr") - self.send_stream_message(email, "Scotland", topic_name=u"\u03bb-topic.d", - sender_realm="zephyr") - self.send_stream_message(email, "Scotland", topic_name=u"\u03bb-topic.d.d", - sender_realm="zephyr") - self.send_stream_message(email, "Scotland", topic_name=u"\u03bb-topic.d.d.d", - sender_realm="zephyr") - self.send_stream_message(email, "Scotland", topic_name=u"\u03bb-topic.d.d.d.d", - sender_realm="zephyr") + self.send_stream_message(mit_user_profile, "Scotland", topic_name=u"\u03bb-topic") + self.send_stream_message(mit_user_profile, "Scotland", topic_name=u"\u03bb-topic.d") + self.send_stream_message(mit_user_profile, "Scotland", topic_name=u"\u03bb-topic.d.d") + self.send_stream_message(mit_user_profile, "Scotland", topic_name=u"\u03bb-topic.d.d.d") + self.send_stream_message(mit_user_profile, "Scotland", topic_name=u"\u03bb-topic.d.d.d.d") narrow = [dict(operator='topic', operand=u'\u03bb-topic')] result = self.get_and_check_messages( @@ -1508,20 +1503,13 @@ class GetOldMessagesTest(ZulipTestCase): self.login(email, realm=mit_user_profile.realm) self.subscribe(mit_user_profile, "Scotland") - self.send_stream_message(email, "Scotland", topic_name=u".d.d", - sender_realm="zephyr") - self.send_stream_message(email, "Scotland", topic_name=u"PERSONAL", - sender_realm="zephyr") - self.send_stream_message(email, "Scotland", topic_name=u'(instance "").d', - sender_realm="zephyr") - self.send_stream_message(email, "Scotland", topic_name=u".d.d.d", - sender_realm="zephyr") - self.send_stream_message(email, "Scotland", topic_name=u"personal.d", - sender_realm="zephyr") - self.send_stream_message(email, "Scotland", topic_name=u'(instance "")', - sender_realm="zephyr") - self.send_stream_message(email, "Scotland", topic_name=u".d.d.d.d", - sender_realm="zephyr") + self.send_stream_message(mit_user_profile, "Scotland", topic_name=u".d.d") + self.send_stream_message(mit_user_profile, "Scotland", topic_name=u"PERSONAL") + self.send_stream_message(mit_user_profile, "Scotland", topic_name=u'(instance "").d') + self.send_stream_message(mit_user_profile, "Scotland", topic_name=u".d.d.d") + self.send_stream_message(mit_user_profile, "Scotland", topic_name=u"personal.d") + self.send_stream_message(mit_user_profile, "Scotland", topic_name=u'(instance "")') + self.send_stream_message(mit_user_profile, "Scotland", topic_name=u".d.d.d.d") narrow = [dict(operator='topic', operand=u'personal.d.d')] result = self.get_and_check_messages( @@ -1546,10 +1534,10 @@ class GetOldMessagesTest(ZulipTestCase): self.login(self.example_email("hamlet")) # We need to send a message here to ensure that we actually # have a stream message in this narrow view. - self.send_stream_message(self.example_email("hamlet"), "Scotland") - self.send_stream_message(self.example_email("othello"), "Scotland") - self.send_personal_message(self.example_email("othello"), self.example_email("hamlet")) - self.send_stream_message(self.example_email("iago"), "Scotland") + self.send_stream_message(self.example_user("hamlet"), "Scotland") + self.send_stream_message(self.example_user("othello"), "Scotland") + self.send_personal_message(self.example_user("othello"), self.example_user("hamlet")) + self.send_stream_message(self.example_user("iago"), "Scotland") test_operands = [self.example_email("othello"), self.example_user("othello").id] for operand in test_operands: @@ -1573,12 +1561,12 @@ class GetOldMessagesTest(ZulipTestCase): @override_settings(USING_PGROONGA=False) def test_messages_in_narrow(self) -> None: - email = self.example_email("cordelia") - self.login(email) + user = self.example_user("cordelia") + self.login(user.email) def send(content: str) -> int: msg_id = self.send_stream_message( - sender_email=email, + sender=user, stream_name="Verona", content=content, ) @@ -1626,7 +1614,7 @@ class GetOldMessagesTest(ZulipTestCase): for topic, content in messages_to_search: self.send_stream_message( - sender_email=self.example_email("cordelia"), + sender=self.example_user("cordelia"), stream_name="Verona", content=content, topic_name=topic, @@ -1748,7 +1736,7 @@ class GetOldMessagesTest(ZulipTestCase): message_ids = [] for topic, content in messages_to_search: - message_ids.append(self.send_stream_message(self.example_email("iago"), "Scotland", + message_ids.append(self.send_stream_message(self.example_user("iago"), "Scotland", topic_name=topic, content=content)) self._update_tsvector_index() narrow = [dict(operator='search', operand="Hogwart's")] @@ -1759,7 +1747,7 @@ class GetOldMessagesTest(ZulipTestCase): """Verify support for searching a stream you're not subscribed to""" self.subscribe(self.example_user("hamlet"), "newstream") self.send_stream_message( - sender_email=self.example_email("hamlet"), + sender=self.example_user("hamlet"), stream_name="newstream", content="Public special content!", topic_name="new", @@ -1800,7 +1788,7 @@ class GetOldMessagesTest(ZulipTestCase): for topic, content in messages_to_search: self.send_stream_message( - sender_email=self.example_email("cordelia"), + sender=self.example_user("cordelia"), stream_name="Verona", content=content, topic_name=topic, @@ -1921,12 +1909,12 @@ class GetOldMessagesTest(ZulipTestCase): '

chalk & cheese

') def test_messages_in_narrow_for_non_search(self) -> None: - email = self.example_email("cordelia") - self.login(email) + user = self.example_user("cordelia") + self.login(user.email) def send(content: str) -> int: msg_id = self.send_stream_message( - sender_email=email, + sender=user, stream_name="Verona", topic_name='test_topic', content=content, @@ -1959,7 +1947,7 @@ class GetOldMessagesTest(ZulipTestCase): returns at most 1 message. """ self.login(self.example_email("cordelia")) - anchor = self.send_stream_message(self.example_email("cordelia"), "Verona") + anchor = self.send_stream_message(self.example_user("cordelia"), "Verona") narrow = [dict(operator='sender', operand=self.example_email("cordelia"))] result = self.get_and_check_messages(dict(narrow=ujson.dumps(narrow), @@ -1985,7 +1973,7 @@ class GetOldMessagesTest(ZulipTestCase): message_ids = [] for i in range(10): - message_ids.append(self.send_stream_message(self.example_email("cordelia"), "Verona")) + message_ids.append(self.send_stream_message(self.example_user("cordelia"), "Verona")) data = self.get_messages_response(anchor=message_ids[9], num_before=9, num_after=0) @@ -2412,8 +2400,8 @@ class GetOldMessagesTest(ZulipTestCase): self.make_stream('England') # Send a few messages that Hamlet won't have UserMessage rows for. - unsub_message_id = self.send_stream_message(cordelia.email, 'England') - self.send_personal_message(cordelia.email, othello.email) + unsub_message_id = self.send_stream_message(cordelia, 'England') + self.send_personal_message(cordelia, othello) self.subscribe(hamlet, 'England') @@ -2423,14 +2411,14 @@ class GetOldMessagesTest(ZulipTestCase): set_topic_mutes(hamlet, muted_topics) # send a muted message - muted_message_id = self.send_stream_message(cordelia.email, 'England', topic_name='muted') + muted_message_id = self.send_stream_message(cordelia, 'England', topic_name='muted') # finally send Hamlet a "normal" message - first_message_id = self.send_stream_message(cordelia.email, 'England') + first_message_id = self.send_stream_message(cordelia, 'England') # send a few more messages - extra_message_id = self.send_stream_message(cordelia.email, 'England') - self.send_personal_message(cordelia.email, hamlet.email) + extra_message_id = self.send_stream_message(cordelia, 'England') + self.send_personal_message(cordelia, hamlet) sa_conn = get_sqlalchemy_connection() @@ -2471,16 +2459,16 @@ class GetOldMessagesTest(ZulipTestCase): # Have Othello send messages to Hamlet that he hasn't read. # Here, Hamlet isn't subscribed to the stream Scotland - self.send_stream_message(self.example_email("othello"), "Scotland") + self.send_stream_message(self.example_user("othello"), "Scotland") first_unread_message_id = self.send_personal_message( - self.example_email("othello"), - self.example_email("hamlet"), + self.example_user("othello"), + self.example_user("hamlet"), ) # Add a few messages that help us test that our query doesn't # look at messages that are irrelevant to Hamlet. - self.send_personal_message(self.example_email("othello"), self.example_email("cordelia")) - self.send_personal_message(self.example_email("othello"), self.example_email("iago")) + self.send_personal_message(self.example_user("othello"), self.example_user("cordelia")) + self.send_personal_message(self.example_user("othello"), self.example_user("iago")) query_params = dict( anchor="first_unread", @@ -2516,18 +2504,18 @@ class GetOldMessagesTest(ZulipTestCase): # Have Othello send messages to Hamlet that he hasn't read. self.subscribe(self.example_user("hamlet"), 'Scotland') - first_unread_message_id = self.send_stream_message(self.example_email("othello"), "Scotland") - self.send_stream_message(self.example_email("othello"), "Scotland") - self.send_stream_message(self.example_email("othello"), "Scotland") + first_unread_message_id = self.send_stream_message(self.example_user("othello"), "Scotland") + self.send_stream_message(self.example_user("othello"), "Scotland") + self.send_stream_message(self.example_user("othello"), "Scotland") self.send_personal_message( - self.example_email("othello"), - self.example_email("hamlet"), + self.example_user("othello"), + self.example_user("hamlet"), ) # Add a few messages that help us test that our query doesn't # look at messages that are irrelevant to Hamlet. - self.send_personal_message(self.example_email("othello"), self.example_email("cordelia")) - self.send_personal_message(self.example_email("othello"), self.example_email("iago")) + self.send_personal_message(self.example_user("othello"), self.example_user("cordelia")) + self.send_personal_message(self.example_user("othello"), self.example_user("iago")) query_params = dict( anchor="first_unread", @@ -2889,7 +2877,7 @@ WHERE user_profile_id = {hamlet_id} AND (content ILIKE '%jumping%' OR subject IL for topic, content in messages_to_search: self.send_stream_message( - sender_email=self.example_email("cordelia"), + sender=self.example_user("cordelia"), stream_name="Verona", content=content, topic_name=topic, diff --git a/zerver/tests/test_outgoing_webhook_interfaces.py b/zerver/tests/test_outgoing_webhook_interfaces.py index a4e4ac96c3..8677903118 100644 --- a/zerver/tests/test_outgoing_webhook_interfaces.py +++ b/zerver/tests/test_outgoing_webhook_interfaces.py @@ -21,7 +21,7 @@ class TestGenericOutgoingWebhookService(ZulipTestCase): # TODO: Ideally, this test would use the full flow, rather # than making a mock message like this. - message_id = self.send_stream_message(self.example_email('othello'), + message_id = self.send_stream_message(self.example_user('othello'), "Denmark", content="@**test**") message = Message.objects.get(id=message_id) wide_message_dict = MessageDict.wide_dict(message) diff --git a/zerver/tests/test_outgoing_webhook_system.py b/zerver/tests/test_outgoing_webhook_system.py index b91e377844..52186738d1 100644 --- a/zerver/tests/test_outgoing_webhook_system.py +++ b/zerver/tests/test_outgoing_webhook_system.py @@ -149,7 +149,7 @@ class TestOutgoingWebhookMessaging(ZulipTestCase): @mock.patch('requests.request', return_value=ResponseMock(200, {"response_string": "Hidley ho, I'm a webhook responding!"})) def test_pm_to_outgoing_webhook_bot(self, mock_requests_request: mock.Mock) -> None: - self.send_personal_message(self.user_profile.email, self.bot_profile.email, + self.send_personal_message(self.user_profile, self.bot_profile, content="foo") last_message = self.get_last_message() self.assertEqual(last_message.content, "Hidley ho, I'm a webhook responding!") @@ -163,7 +163,7 @@ class TestOutgoingWebhookMessaging(ZulipTestCase): @mock.patch('requests.request', return_value=ResponseMock(200, {"response_string": "Hidley ho, I'm a webhook responding!"})) def test_stream_message_to_outgoing_webhook_bot(self, mock_requests_request: mock.Mock) -> None: - self.send_stream_message(self.user_profile.email, "Denmark", + self.send_stream_message(self.user_profile, "Denmark", content="@**{}** foo".format(self.bot_profile.full_name), topic_name="bar") last_message = self.get_last_message() diff --git a/zerver/tests/test_push_notifications.py b/zerver/tests/test_push_notifications.py index c6eacc1c25..94defbac6c 100644 --- a/zerver/tests/test_push_notifications.py +++ b/zerver/tests/test_push_notifications.py @@ -957,7 +957,8 @@ class HandlePushNotificationTest(PushNotificationTest): not long-term idle; we fake it, though, in the sense that the user should not have received the message in the first place""" self.make_stream('public_stream') - message_id = self.send_stream_message("iago@zulip.com", "public_stream", "test") + sender = self.example_user('iago') + message_id = self.send_stream_message(sender, "public_stream", "test") missed_message = {'message_id': message_id} with mock.patch('zerver.lib.push_notifications.logger.error') as mock_logger, \ mock.patch('zerver.lib.push_notifications.push_notifications_enabled', return_value = True) as mock_push_notifications: @@ -977,7 +978,8 @@ class HandlePushNotificationTest(PushNotificationTest): self.subscribe(self.user_profile, 'public_stream') do_soft_deactivate_users([self.user_profile]) - message_id = self.send_stream_message("iago@zulip.com", "public_stream", "test") + sender = self.example_user('iago') + message_id = self.send_stream_message(sender, "public_stream", "test") missed_message = { 'message_id': message_id, 'trigger': 'stream_push_notify', @@ -1136,8 +1138,8 @@ class TestGetAPNsPayload(PushNotificationTest): def test_get_message_payload_apns_personal_message(self) -> None: user_profile = self.example_user("othello") message_id = self.send_personal_message( - self.sender.email, - user_profile.email, + self.sender, + user_profile, 'Content of personal message', ) message = Message.objects.get(id=message_id) @@ -1170,8 +1172,8 @@ class TestGetAPNsPayload(PushNotificationTest): def test_get_message_payload_apns_huddle_message(self, mock_push_notifications: mock.MagicMock) -> None: user_profile = self.example_user("othello") message_id = self.send_huddle_message( - self.sender.email, - [self.example_email('othello'), self.example_email('cordelia')]) + self.sender, + [self.example_user('othello'), self.example_user('cordelia')]) message = Message.objects.get(id=message_id) message.trigger = 'private_message' payload = get_message_payload_apns(user_profile, message) @@ -1306,8 +1308,8 @@ class TestGetAPNsPayload(PushNotificationTest): def test_get_message_payload_apns_redacted_content(self) -> None: user_profile = self.example_user("othello") message_id = self.send_huddle_message( - self.sender.email, - [self.example_email('othello'), self.example_email('cordelia')]) + self.sender, + [self.example_user('othello'), self.example_user('cordelia')]) message = Message.objects.get(id=message_id) message.trigger = 'private_message' payload = get_message_payload_apns(user_profile, message) @@ -1756,7 +1758,7 @@ class TestClearOnRead(ZulipTestCase): hamlet.save() stream = self.subscribe(hamlet, "Denmark") - message_ids = [self.send_stream_message(self.example_email("iago"), + message_ids = [self.send_stream_message(self.example_user("iago"), stream.name, "yo {}".format(i)) for i in range(n_msgs)] diff --git a/zerver/tests/test_queue_worker.py b/zerver/tests/test_queue_worker.py index 2256406e1f..19932a8799 100644 --- a/zerver/tests/test_queue_worker.py +++ b/zerver/tests/test_queue_worker.py @@ -173,26 +173,26 @@ class WorkerTest(ZulipTestCase): othello = self.example_user('othello') hamlet1_msg_id = self.send_personal_message( - from_email=cordelia.email, - to_email=hamlet.email, + from_user=cordelia, + to_user=hamlet, content='hi hamlet', ) hamlet2_msg_id = self.send_personal_message( - from_email=cordelia.email, - to_email=hamlet.email, + from_user=cordelia, + to_user=hamlet, content='goodbye hamlet', ) hamlet3_msg_id = self.send_personal_message( - from_email=cordelia.email, - to_email=hamlet.email, + from_user=cordelia, + to_user=hamlet, content='hello again hamlet', ) othello_msg_id = self.send_personal_message( - from_email=cordelia.email, - to_email=othello.email, + from_user=cordelia, + to_user=othello, content='where art thou, othello?', ) diff --git a/zerver/tests/test_reactions.py b/zerver/tests/test_reactions.py index ed66560a0f..87d5f90675 100644 --- a/zerver/tests/test_reactions.py +++ b/zerver/tests/test_reactions.py @@ -90,7 +90,7 @@ class ReactionEmojiTest(ZulipTestCase): """ stream_name = "Saxony" self.subscribe(self.example_user("cordelia"), stream_name) - message_id = self.send_stream_message(self.example_email("cordelia"), stream_name) + message_id = self.send_stream_message(self.example_user("cordelia"), stream_name) user_profile = self.example_user('hamlet') sender = user_profile.email @@ -650,7 +650,7 @@ class DefaultEmojiReactionTests(EmojiReactionBase): """ stream_name = "Saxony" self.subscribe(self.example_user("cordelia"), stream_name) - message_id = self.send_stream_message(self.example_email("cordelia"), stream_name) + message_id = self.send_stream_message(self.example_user("cordelia"), stream_name) user_profile = self.example_user('hamlet') @@ -814,7 +814,7 @@ class ReactionAPIEventTest(EmojiReactionBase): pm_sender = self.example_user('hamlet') pm_recipient = self.example_user('othello') reaction_sender = pm_recipient - pm_id = self.send_personal_message(pm_sender.email, pm_recipient.email) + pm_id = self.send_personal_message(pm_sender, pm_recipient) expected_recipient_ids = set([pm_sender.id, pm_recipient.id]) reaction_info = { 'emoji_name': 'hamburger', @@ -851,7 +851,7 @@ class ReactionAPIEventTest(EmojiReactionBase): pm_sender = self.example_user('hamlet') pm_recipient = self.example_user('othello') reaction_sender = pm_recipient - pm_id = self.send_personal_message(pm_sender.email, pm_recipient.email) + pm_id = self.send_personal_message(pm_sender, pm_recipient) expected_recipient_ids = set([pm_sender.id, pm_recipient.id]) reaction_info = { 'emoji_name': 'hamburger', diff --git a/zerver/tests/test_realm.py b/zerver/tests/test_realm.py index ba237619d2..1397205e23 100644 --- a/zerver/tests/test_realm.py +++ b/zerver/tests/test_realm.py @@ -848,10 +848,10 @@ class ScrubRealmTest(ZulipTestCase): UserMessage.objects.all().delete() for i in range(5): - self.send_stream_message(iago.email, "Scotland") - self.send_stream_message(othello.email, "Scotland") - self.send_stream_message(cordelia.email, "Shakespeare", sender_realm="lear") - self.send_stream_message(king.email, "Shakespeare", sender_realm="lear") + self.send_stream_message(iago, "Scotland") + self.send_stream_message(othello, "Scotland") + self.send_stream_message(cordelia, "Shakespeare") + self.send_stream_message(king, "Shakespeare") Attachment.objects.filter(realm=zulip).delete() Attachment.objects.create(realm=zulip, owner=iago, path_id="a/b/temp1.txt") diff --git a/zerver/tests/test_retention.py b/zerver/tests/test_retention.py index 644933facb..e9ebb8a987 100644 --- a/zerver/tests/test_retention.py +++ b/zerver/tests/test_retention.py @@ -99,8 +99,7 @@ class ArchiveMessagesTestingBase(RetentionTestingBase): # send messages from mit.edu realm and change messages pub date sender = self.mit_user('espuser') recipient = self.mit_user('starnine') - msg_ids = [self.send_personal_message(sender.email, recipient.email, - sender_realm='zephyr') + msg_ids = [self.send_personal_message(sender, recipient) for i in range(message_quantity)] self._change_messages_date_sent(msg_ids, date_sent) @@ -132,7 +131,6 @@ class ArchiveMessagesTestingBase(RetentionTestingBase): def _send_messages_with_attachments(self) -> Dict[str, int]: user_profile = self.example_user("hamlet") - sender_email = user_profile.email sample_size = 10 host = user_profile.realm.host realm_id = get_realm("zulip").id @@ -150,9 +148,11 @@ class ArchiveMessagesTestingBase(RetentionTestingBase): " http://{host}/user_uploads/{id}/31/4CBjtTLYZhk66pZrF8hnYGwc/temp_file.py.... Some more...." + " http://{host}/user_uploads/{id}/31/4CBjtTLYZhk66pZrF8hnYGwc/abc.py").format(id=realm_id, host=host) - expired_message_id = self.send_stream_message(sender_email, "Denmark", body) - actual_message_id = self.send_stream_message(sender_email, "Denmark", body) - other_message_id = self.send_stream_message("othello@zulip.com", "Denmark", body) + expired_message_id = self.send_stream_message(user_profile, "Denmark", body) + actual_message_id = self.send_stream_message(user_profile, "Denmark", body) + + othello = self.example_user('othello') + other_message_id = self.send_stream_message(othello, "Denmark", body) self._change_messages_date_sent([expired_message_id], timezone_now() - timedelta(days=MIT_REALM_DAYS + 1)) return {'expired_message_id': expired_message_id, 'actual_message_id': actual_message_id, 'other_user_message_id': other_message_id} @@ -227,7 +227,7 @@ class TestArchiveMessagesGeneral(ArchiveMessagesTestingBase): def test_different_stream_realm_policies(self) -> None: verona = get_stream("Verona", self.zulip_realm) - hamlet = self.example_email("hamlet") + hamlet = self.example_user("hamlet") msg_id = self.send_stream_message(hamlet, "Verona", "test") usermsg_ids = self._get_usermessage_ids([msg_id]) @@ -476,8 +476,8 @@ class TestArchivingReactions(ArchiveMessagesTestingBase, EmojiReactionBase): class MoveMessageToArchiveBase(RetentionTestingBase): def setUp(self) -> None: super().setUp() - self.sender = 'hamlet@zulip.com' - self.recipient = 'cordelia@zulip.com' + self.sender = self.example_user('hamlet') + self.recipient = self.example_user('cordelia') def _create_attachments(self) -> None: sample_size = 10 @@ -613,8 +613,8 @@ class MoveMessageToArchiveGeneral(MoveMessageToArchiveBase): msg_id = self.send_personal_message(self.sender, self.recipient, body) # Simulate a reply with the same contents. reply_msg_id = self.send_personal_message( - from_email=self.recipient, - to_email=self.sender, + from_user=self.recipient, + to_user=self.sender, content=body, ) @@ -749,8 +749,8 @@ class TestCleaningArchive(ArchiveMessagesTestingBase): class TestDoDeleteMessages(ZulipTestCase): def test_do_delete_messages_multiple(self) -> None: realm = get_realm("zulip") - cordelia_email = self.example_email('cordelia') - message_ids = [self.send_stream_message(cordelia_email, "Denmark", str(i)) for i in range(0, 10)] + cordelia = self.example_user('cordelia') + message_ids = [self.send_stream_message(cordelia, "Denmark", str(i)) for i in range(0, 10)] messages = Message.objects.filter(id__in=message_ids) with queries_captured() as queries: @@ -771,7 +771,7 @@ class TestDoDeleteMessages(ZulipTestCase): realm = get_realm("zulip") cordelia = self.example_user('cordelia') hamlet = self.example_user('hamlet') - message_id = self.send_personal_message(cordelia.delivery_email, hamlet.delivery_email) + message_id = self.send_personal_message(cordelia, hamlet) message = Message.objects.get(id=message_id) event = { diff --git a/zerver/tests/test_service_bot_system.py b/zerver/tests/test_service_bot_system.py index c02b5ea602..ee0c3d1c35 100644 --- a/zerver/tests/test_service_bot_system.py +++ b/zerver/tests/test_service_bot_system.py @@ -449,15 +449,15 @@ class TestServiceBotEventTriggers(ZulipTestCase): mock_queue_json_publish.side_effect = check_values_passed self.send_stream_message( - self.user_profile.email, + self.user_profile, 'Denmark', content) self.assertTrue(mock_queue_json_publish.called) @mock.patch('zerver.lib.actions.queue_json_publish') def test_no_trigger_on_stream_message_without_mention(self, mock_queue_json_publish: mock.Mock) -> None: - sender_email = self.user_profile.email - self.send_stream_message(sender_email, "Denmark") + sender = self.user_profile + self.send_stream_message(sender, "Denmark") self.assertFalse(mock_queue_json_publish.called) @mock.patch('zerver.lib.actions.queue_json_publish') @@ -467,7 +467,7 @@ class TestServiceBotEventTriggers(ZulipTestCase): self.bot_profile.save() self.send_stream_message( - self.second_bot_profile.email, + self.second_bot_profile, 'Denmark', u'@**FooBot** foo bar!!!') self.assertFalse(mock_queue_json_publish.called) @@ -478,8 +478,8 @@ class TestServiceBotEventTriggers(ZulipTestCase): self.bot_profile.bot_type = bot_type self.bot_profile.save() - sender_email = self.user_profile.email - recipient_email = self.bot_profile.email + sender = self.user_profile + recipient = self.bot_profile def check_values_passed(queue_name: Any, trigger_event: Union[Mapping[Any, Any], Any], @@ -487,16 +487,16 @@ class TestServiceBotEventTriggers(ZulipTestCase): self.assertEqual(queue_name, expected_queue_name) self.assertEqual(trigger_event["user_profile_id"], self.bot_profile.id) self.assertEqual(trigger_event["trigger"], "private_message") - self.assertEqual(trigger_event["message"]["sender_email"], sender_email) + self.assertEqual(trigger_event["message"]["sender_email"], sender.email) display_recipients = [ trigger_event["message"]["display_recipient"][0]["email"], trigger_event["message"]["display_recipient"][1]["email"], ] - self.assertTrue(sender_email in display_recipients) - self.assertTrue(recipient_email in display_recipients) + self.assertTrue(sender.email in display_recipients) + self.assertTrue(recipient.email in display_recipients) mock_queue_json_publish.side_effect = check_values_passed - self.send_personal_message(sender_email, recipient_email, 'test') + self.send_personal_message(sender, recipient, 'test') self.assertTrue(mock_queue_json_publish.called) @mock.patch('zerver.lib.actions.queue_json_publish') @@ -505,9 +505,9 @@ class TestServiceBotEventTriggers(ZulipTestCase): self.bot_profile.bot_type = bot_type self.bot_profile.save() - sender_email = self.second_bot_profile.email - recipient_email = self.bot_profile.email - self.send_personal_message(sender_email, recipient_email) + sender = self.second_bot_profile + recipient = self.bot_profile + self.send_personal_message(sender, recipient) self.assertFalse(mock_queue_json_publish.called) @mock.patch('zerver.lib.actions.queue_json_publish') @@ -519,8 +519,8 @@ class TestServiceBotEventTriggers(ZulipTestCase): self.second_bot_profile.bot_type = bot_type self.second_bot_profile.save() - sender_email = self.user_profile.email - recipient_emails = [self.bot_profile.email, self.second_bot_profile.email] + sender = self.user_profile + recipients = [self.bot_profile, self.second_bot_profile] profile_ids = [self.bot_profile.id, self.second_bot_profile.id] def check_values_passed(queue_name: Any, @@ -530,11 +530,11 @@ class TestServiceBotEventTriggers(ZulipTestCase): self.assertIn(trigger_event["user_profile_id"], profile_ids) profile_ids.remove(trigger_event["user_profile_id"]) self.assertEqual(trigger_event["trigger"], "private_message") - self.assertEqual(trigger_event["message"]["sender_email"], sender_email) + self.assertEqual(trigger_event["message"]["sender_email"], sender.email) self.assertEqual(trigger_event["message"]["type"], u'private') mock_queue_json_publish.side_effect = check_values_passed - self.send_huddle_message(sender_email, recipient_emails, 'test') + self.send_huddle_message(sender, recipients, 'test') self.assertEqual(mock_queue_json_publish.call_count, 2) mock_queue_json_publish.reset_mock() @@ -544,7 +544,7 @@ class TestServiceBotEventTriggers(ZulipTestCase): self.bot_profile.bot_type = bot_type self.bot_profile.save() - sender_email = self.second_bot_profile.email - recipient_emails = [self.user_profile.email, self.bot_profile.email] - self.send_huddle_message(sender_email, recipient_emails) + sender = self.second_bot_profile + recipients = [self.user_profile, self.bot_profile] + self.send_huddle_message(sender, recipients) self.assertFalse(mock_queue_json_publish.called) diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.py index 5b1060eb0f..c8a482e4ee 100644 --- a/zerver/tests/test_signup.py +++ b/zerver/tests/test_signup.py @@ -156,9 +156,9 @@ class AddNewUserHistoryTest(ZulipTestCase): stream = Stream.objects.get(realm=realm, name='Denmark') DefaultStream.objects.create(stream=stream, realm=realm) # Make sure at least 3 messages are sent to Denmark and it's a default stream. - message_id = self.send_stream_message(self.example_email('hamlet'), stream.name, "test 1") - self.send_stream_message(self.example_email('hamlet'), stream.name, "test 2") - self.send_stream_message(self.example_email('hamlet'), stream.name, "test 3") + message_id = self.send_stream_message(self.example_user('hamlet'), stream.name, "test 1") + self.send_stream_message(self.example_user('hamlet'), stream.name, "test 2") + self.send_stream_message(self.example_user('hamlet'), stream.name, "test 3") with patch("zerver.lib.actions.add_new_user_history"): self.register(self.nonreg_email('test'), "test") @@ -169,7 +169,7 @@ class AddNewUserHistoryTest(ZulipTestCase): # Sent a message afterwards to trigger a race between message # sending and `add_new_user_history`. - race_message_id = self.send_stream_message(self.example_email('hamlet'), + race_message_id = self.send_stream_message(self.example_user('hamlet'), streams[0].name, "test") # Overwrite ONBOARDING_UNREAD_MESSAGES to 2 @@ -1043,13 +1043,13 @@ class InviteUserTest(InviteUserBase): self.make_stream(private_stream_name, invite_only=True) self.subscribe(user_profile, private_stream_name) public_msg_id = self.send_stream_message( - self.example_email("hamlet"), + self.example_user("hamlet"), "Denmark", topic_name="Public topic", content="Public message", ) secret_msg_id = self.send_stream_message( - self.example_email("hamlet"), + self.example_user("hamlet"), private_stream_name, topic_name="Secret topic", content="Secret message", diff --git a/zerver/tests/test_soft_deactivation.py b/zerver/tests/test_soft_deactivation.py index 6b05bab665..3f2c7fac36 100644 --- a/zerver/tests/test_soft_deactivation.py +++ b/zerver/tests/test_soft_deactivation.py @@ -41,8 +41,7 @@ class UserSoftDeactivationTests(ZulipTestCase): # We are sending this message to ensure that users have at least # one UserMessage row. - self.send_huddle_message(users[0].email, - [user.email for user in users]) + self.send_huddle_message(users[0], users) with mock.patch('logging.info'): do_soft_deactivate_users(users) @@ -87,8 +86,8 @@ class UserSoftDeactivationTests(ZulipTestCase): self.example_user('iago'), self.example_user('cordelia'), ] - self.send_huddle_message(users[0].email, - [user.email for user in users]) + self.send_huddle_message(users[0], users) + with mock.patch('logging.info'): do_soft_deactivate_users(users) for user in users: @@ -139,7 +138,7 @@ class UserSoftDeactivationTests(ZulipTestCase): for user in users: self.assertTrue(user.long_term_idle) - message_id = self.send_stream_message(hamlet.email, stream, 'Hello world!') + message_id = self.send_stream_message(hamlet, stream, 'Hello world!') already_received = UserMessage.objects.filter(message_id=message_id).count() with mock.patch('logging.info'): do_catch_up_soft_deactivated_users(users) @@ -188,7 +187,7 @@ class UserSoftDeactivationTests(ZulipTestCase): # Verify that deactivated users are caught up automatically - message_id = self.send_stream_message(sender.email, stream_name) + message_id = self.send_stream_message(sender, stream_name) received_count = UserMessage.objects.filter(user_profile__in=users, message_id=message_id).count() self.assertEqual(0, received_count) @@ -204,7 +203,7 @@ class UserSoftDeactivationTests(ZulipTestCase): # Verify that deactivated users are NOT caught up if # AUTO_CATCH_UP_SOFT_DEACTIVATED_USERS is off - message_id = self.send_stream_message(sender.email, stream_name) + message_id = self.send_stream_message(sender, stream_name) received_count = UserMessage.objects.filter(user_profile__in=users, message_id=message_id).count() self.assertEqual(0, received_count) diff --git a/zerver/tests/test_submessage.py b/zerver/tests/test_submessage.py index b199b4cdf7..a85b7a8d88 100644 --- a/zerver/tests/test_submessage.py +++ b/zerver/tests/test_submessage.py @@ -20,7 +20,7 @@ class TestBasics(ZulipTestCase): stream_name = 'Verona' message_id = self.send_stream_message( - sender_email=cordelia.email, + sender=cordelia, stream_name=stream_name, ) @@ -80,7 +80,7 @@ class TestBasics(ZulipTestCase): cordelia = self.example_user('cordelia') stream_name = 'Verona' message_id = self.send_stream_message( - sender_email=cordelia.email, + sender=cordelia, stream_name=stream_name, ) self.login(cordelia.email) @@ -95,8 +95,8 @@ class TestBasics(ZulipTestCase): hamlet = self.example_user('hamlet') bad_message_id = self.send_personal_message( - from_email=hamlet.email, - to_email=hamlet.email, + from_user=hamlet, + to_user=hamlet, ) payload = dict( message_id=bad_message_id, @@ -111,7 +111,7 @@ class TestBasics(ZulipTestCase): hamlet = self.example_user('hamlet') stream_name = 'Verona' message_id = self.send_stream_message( - sender_email=cordelia.email, + sender=cordelia, stream_name=stream_name, ) self.login(cordelia.email) diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.py index 5bfeb25f42..3e129dad2e 100644 --- a/zerver/tests/test_subs.py +++ b/zerver/tests/test_subs.py @@ -3122,9 +3122,9 @@ class SubscriptionAPITest(ZulipTestCase): self.subscribe(random_user, "stream2") self.subscribe(random_user, "private_stream") - self.send_stream_message(random_user.email, "stream1", "test", "test") - self.send_stream_message(random_user.email, "stream2", "test", "test") - self.send_stream_message(random_user.email, "private_stream", "test", "test") + self.send_stream_message(random_user, "stream1", "test", "test") + self.send_stream_message(random_user, "stream2", "test", "test") + self.send_stream_message(random_user, "private_stream", "test", "test") def get_unread_stream_data() -> List[Dict[str, Any]]: raw_unread_data = get_raw_unread_data(user) @@ -3445,15 +3445,16 @@ class InviteOnlyStreamTest(ZulipTestCase): If you try to send a message to an invite-only stream to which you aren't subscribed, you'll get a 400. """ - self.login(self.example_email("hamlet")) + user = self.example_user('hamlet') + self.login(user.email) # Create Saxony as an invite-only stream. self.assert_json_success( - self.common_subscribe_to_streams(self.example_email("hamlet"), ["Saxony"], + self.common_subscribe_to_streams(user.email, ["Saxony"], invite_only=True)) - email = self.example_email("cordelia") + cordelia = self.example_user("cordelia") with self.assertRaises(JsonableError): - self.send_stream_message(email, "Saxony") + self.send_stream_message(cordelia, "Saxony") def test_list_respects_invite_only_bit(self) -> None: """ diff --git a/zerver/tests/test_tutorial.py b/zerver/tests/test_tutorial.py index 347aa768ef..1741a17a26 100644 --- a/zerver/tests/test_tutorial.py +++ b/zerver/tests/test_tutorial.py @@ -4,7 +4,7 @@ from django.conf import settings from zerver.lib.actions import internal_send_private_message from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_helpers import message_stream_count, most_recent_message -from zerver.models import get_realm, get_user, get_system_bot, UserProfile +from zerver.models import get_system_bot, UserProfile import ujson @@ -35,33 +35,30 @@ class TutorialTests(ZulipTestCase): self.assertEqual(user.tutorial_status, expected_db_status) def test_single_response_to_pm(self) -> None: - realm = get_realm('zulip') user_email = 'hamlet@zulip.com' - bot_email = 'welcome-bot@zulip.com' + user = self.example_user('hamlet') + bot = get_system_bot(settings.WELCOME_BOT) content = 'whatever' self.login(user_email) - self.send_personal_message(user_email, bot_email, content) - user = get_user(user_email, realm) + self.send_personal_message(user, bot, content) user_messages = message_stream_count(user) expected_response = ("Congratulations on your first reply! :tada:\n\n" "Feel free to continue using this space to practice your new messaging " "skills. Or, try clicking on some of the stream names to your left!") self.assertEqual(most_recent_message(user).content, expected_response) # Welcome bot shouldn't respond to further PMs. - self.send_personal_message(user_email, bot_email, content) + self.send_personal_message(user, bot, content) self.assertEqual(message_stream_count(user), user_messages+1) def test_no_response_to_group_pm(self) -> None: - realm = get_realm('zulip') # Assume realm is always 'zulip' - user1_email = self.example_email('hamlet') - user2_email = self.example_email('cordelia') - bot_email = self.example_email('welcome_bot') + user1 = self.example_user('hamlet') + user2 = self.example_user('cordelia') + bot = get_system_bot(settings.WELCOME_BOT) content = "whatever" - self.login(user1_email) - self.send_huddle_message(user1_email, [bot_email, user2_email], content) - user1 = get_user(user1_email, realm) + self.login(user1.email) + self.send_huddle_message(user1, [bot, user2], content) user1_messages = message_stream_count(user1) self.assertEqual(most_recent_message(user1).content, content) # Welcome bot should still respond to initial PM after group PM. - self.send_personal_message(user1_email, bot_email, content) + self.send_personal_message(user1, bot, content) self.assertEqual(message_stream_count(user1), user1_messages+2) diff --git a/zerver/tests/test_unread.py b/zerver/tests/test_unread.py index 97eaae23cb..18fa35fc45 100644 --- a/zerver/tests/test_unread.py +++ b/zerver/tests/test_unread.py @@ -39,7 +39,7 @@ class PointerTest(ZulipTestCase): """ self.login(self.example_email("hamlet")) self.assertEqual(self.example_user('hamlet').pointer, -1) - msg_id = self.send_stream_message(self.example_email("othello"), "Verona") + msg_id = self.send_stream_message(self.example_user("othello"), "Verona") result = self.client_post("/json/users/me/pointer", {"pointer": msg_id}) self.assert_json_success(result) self.assertEqual(self.example_user('hamlet').pointer, msg_id) @@ -51,7 +51,7 @@ class PointerTest(ZulipTestCase): user = self.example_user('hamlet') email = user.email self.assertEqual(user.pointer, -1) - msg_id = self.send_stream_message(self.example_email("othello"), "Verona") + msg_id = self.send_stream_message(self.example_user("othello"), "Verona") result = self.api_post(email, "/api/v1/users/me/pointer", {"pointer": msg_id}) self.assert_json_success(result) self.assertEqual(get_user(email, user.realm).pointer, msg_id) @@ -104,7 +104,7 @@ class PointerTest(ZulipTestCase): self.assert_json_success(result) # Send a new message (this will be unread) - new_message_id = self.send_stream_message(self.example_email("othello"), "Verona", + new_message_id = self.send_stream_message(self.example_user("othello"), "Verona", "test") # If we call get_messages with use_first_unread_anchor=True, we @@ -183,7 +183,7 @@ class PointerTest(ZulipTestCase): result = self.client_post("/json/mark_all_as_read") self.assert_json_success(result) - new_message_id = self.send_stream_message(self.example_email("othello"), "Verona", + new_message_id = self.send_stream_message(self.example_user("othello"), "Verona", "test") messages_response = self.get_messages_response( @@ -215,16 +215,16 @@ class UnreadCountTests(ZulipTestCase): return_value = True) as mock_push_notifications_enabled: self.unread_msg_ids = [ self.send_personal_message( - self.example_email("iago"), self.example_email("hamlet"), "hello"), + self.example_user("iago"), self.example_user("hamlet"), "hello"), self.send_personal_message( - self.example_email("iago"), self.example_email("hamlet"), "hello2")] + self.example_user("iago"), self.example_user("hamlet"), "hello2")] mock_push_notifications_enabled.assert_called() # Sending a new message results in unread UserMessages being created def test_new_message(self) -> None: self.login(self.example_email("hamlet")) content = "Test message for unset read bit" - last_msg = self.send_stream_message(self.example_email("hamlet"), "Verona", content) + last_msg = self.send_stream_message(self.example_user("hamlet"), "Verona", content) user_messages = list(UserMessage.objects.filter(message=last_msg)) self.assertEqual(len(user_messages) > 0, True) for um in user_messages: @@ -267,8 +267,8 @@ class UnreadCountTests(ZulipTestCase): stream = self.subscribe(user_profile, "test_stream") self.subscribe(self.example_user("cordelia"), "test_stream") - message_id = self.send_stream_message(self.example_email("hamlet"), "test_stream", "hello") - unrelated_message_id = self.send_stream_message(self.example_email("hamlet"), "Denmark", "hello") + message_id = self.send_stream_message(self.example_user("hamlet"), "test_stream", "hello") + unrelated_message_id = self.send_stream_message(self.example_user("hamlet"), "Denmark", "hello") events = [] # type: List[Mapping[str, Any]] with tornado_redirected_to_list(events): @@ -323,8 +323,8 @@ class UnreadCountTests(ZulipTestCase): user_profile = self.example_user('hamlet') self.subscribe(user_profile, "test_stream") - message_id = self.send_stream_message(self.example_email("hamlet"), "test_stream", "hello", "test_topic") - unrelated_message_id = self.send_stream_message(self.example_email("hamlet"), "Denmark", "hello", "Denmark2") + message_id = self.send_stream_message(self.example_user("hamlet"), "test_stream", "hello", "test_topic") + unrelated_message_id = self.send_stream_message(self.example_user("hamlet"), "Denmark", "hello", "Denmark2") events = [] # type: List[Mapping[str, Any]] with tornado_redirected_to_list(events): result = self.client_post("/json/mark_topic_as_read", { @@ -371,7 +371,7 @@ class FixUnreadTests(ZulipTestCase): def send_message(stream_name: str, topic_name: str) -> int: msg_id = self.send_stream_message( - self.example_email("othello"), + self.example_user("othello"), stream_name, topic_name=topic_name) um = UserMessage.objects.get( @@ -507,9 +507,9 @@ class PushNotificationMarkReadFlowsTest(ZulipTestCase): self.assert_json_success(result) self.assertEqual(self.get_mobile_push_notification_ids(user_profile), []) - message_id = self.send_stream_message(self.example_email("cordelia"), "test_stream", "hello", "test_topic") - second_message_id = self.send_stream_message(self.example_email("cordelia"), "test_stream", "hello", "other_topic") - third_message_id = self.send_stream_message(self.example_email("cordelia"), "second_stream", "hello", "test_topic") + message_id = self.send_stream_message(self.example_user("cordelia"), "test_stream", "hello", "test_topic") + second_message_id = self.send_stream_message(self.example_user("cordelia"), "test_stream", "hello", "other_topic") + third_message_id = self.send_stream_message(self.example_user("cordelia"), "second_stream", "hello", "test_topic") self.assertEqual(self.get_mobile_push_notification_ids(user_profile), [message_id, second_message_id, third_message_id]) @@ -530,7 +530,7 @@ class PushNotificationMarkReadFlowsTest(ZulipTestCase): self.assertEqual(self.get_mobile_push_notification_ids(user_profile), [third_message_id]) - fourth_message_id = self.send_stream_message(self.example_email("cordelia"), "test_stream", "hello", "test_topic") + fourth_message_id = self.send_stream_message(self.example_user("cordelia"), "test_stream", "hello", "test_topic") self.assertEqual(self.get_mobile_push_notification_ids(user_profile), [third_message_id, fourth_message_id]) diff --git a/zerver/tests/test_upload.py b/zerver/tests/test_upload.py index eb7e9c851d..fabda45623 100644 --- a/zerver/tests/test_upload.py +++ b/zerver/tests/test_upload.py @@ -199,7 +199,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase): self.subscribe(self.example_user("hamlet"), "Denmark") body = "First message ...[zulip.txt](http://localhost:9991" + uri + ")" - self.send_stream_message(self.example_email("hamlet"), "Denmark", body, "test") + self.send_stream_message(self.example_user("hamlet"), "Denmark", body, "test") self.assertIn('title="zulip.txt"', self.get_last_message().rendered_content) def test_file_download_unauthed(self) -> None: @@ -264,7 +264,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase): # Send message referring only dummy_1 self.subscribe(self.example_user("hamlet"), "Denmark") body = "Some files here ...[zulip.txt](http://localhost:9991/user_uploads/" + d1_path_id + ")" - self.send_stream_message(self.example_email("hamlet"), "Denmark", body, "test") + self.send_stream_message(self.example_user("hamlet"), "Denmark", body, "test") # dummy_2 should not exist in database or the uploads folder do_delete_old_unclaimed_attachments(2) @@ -276,7 +276,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase): self.login(hamlet.email) body = "Test message ...[zulip.txt](http://localhost:9991/user_uploads/%s/64/fake_path_id.txt)" % ( hamlet.realm_id,) - self.send_stream_message(self.example_email("hamlet"), "Denmark", body, "test") + self.send_stream_message(self.example_user("hamlet"), "Denmark", body, "test") self.assertFalse(Attachment.objects.filter(path_id = "1/64/fake_path_id.txt").exists()) def test_multiple_claim_attachments(self) -> None: @@ -293,9 +293,9 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase): self.subscribe(self.example_user("hamlet"), "Denmark") host = self.example_user('hamlet').realm.host body = "First message ...[zulip.txt](http://{}/user_uploads/".format(host) + d1_path_id + ")" - self.send_stream_message(self.example_email("hamlet"), "Denmark", body, "test") + self.send_stream_message(self.example_user("hamlet"), "Denmark", body, "test") body = "Second message ...[zulip.txt](http://{}/user_uploads/".format(host) + d1_path_id + ")" - self.send_stream_message(self.example_email("hamlet"), "Denmark", body, "test") + self.send_stream_message(self.example_user("hamlet"), "Denmark", body, "test") self.assertEqual(Attachment.objects.get(path_id=d1_path_id).messages.count(), 2) @@ -314,25 +314,25 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase): # First, send the message to the new private stream. body = "First message ...[zulip.txt](http://{}/user_uploads/".format(host) + d1_path_id + ")" - self.send_stream_message(self.example_email("hamlet"), "private_stream", body, "test") + self.send_stream_message(self.example_user("hamlet"), "private_stream", body, "test") self.assertFalse(Attachment.objects.get(path_id=d1_path_id).is_realm_public) self.assertEqual(Attachment.objects.get(path_id=d1_path_id).messages.count(), 1) # Then, try having a user who didn't receive the message try to publish it, and fail body = "Illegal message ...[zulip.txt](http://{}/user_uploads/".format(host) + d1_path_id + ")" - self.send_stream_message(self.example_email("cordelia"), "Denmark", body, "test") + self.send_stream_message(self.example_user("cordelia"), "Denmark", body, "test") self.assertEqual(Attachment.objects.get(path_id=d1_path_id).messages.count(), 1) self.assertFalse(Attachment.objects.get(path_id=d1_path_id).is_realm_public) # Then, have the owner PM it to another user, giving that other user access. body = "Second message ...[zulip.txt](http://{}/user_uploads/".format(host) + d1_path_id + ")" - self.send_personal_message(self.example_email("hamlet"), self.example_email("othello"), body) + self.send_personal_message(self.example_user("hamlet"), self.example_user("othello"), body) self.assertEqual(Attachment.objects.get(path_id=d1_path_id).messages.count(), 2) self.assertFalse(Attachment.objects.get(path_id=d1_path_id).is_realm_public) # Then, have that new recipient user publish it. body = "Third message ...[zulip.txt](http://{}/user_uploads/".format(host) + d1_path_id + ")" - self.send_stream_message(self.example_email("othello"), "Denmark", body, "test") + self.send_stream_message(self.example_user("othello"), "Denmark", body, "test") self.assertEqual(Attachment.objects.get(path_id=d1_path_id).messages.count(), 3) self.assertTrue(Attachment.objects.get(path_id=d1_path_id).is_realm_public) @@ -356,7 +356,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase): self.subscribe(hamlet, "test") body = ("[f1.txt](http://{}/user_uploads/".format(host) + f1_path_id + ") " "[f2.txt](http://{}/user_uploads/".format(host) + f2_path_id + ")") - msg_id = self.send_stream_message(hamlet.email, "test", body, "test") + msg_id = self.send_stream_message(hamlet, "test", body, "test") result = self.client_post("/json/user_uploads", {'file': f3}) f3_path_id = re.sub('/user_uploads/', '', result.json()['uri']) @@ -511,7 +511,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase): uri = result.json()['uri'] fp_path_id = re.sub('/user_uploads/', '', uri) body = "First message ...[zulip.txt](http://{}/user_uploads/".format(user.realm.host) + fp_path_id + ")" - self.send_stream_message(user.email, stream_name, body, "test") + self.send_stream_message(user, stream_name, body, "test") self.logout() # Owner user should be able to view file @@ -564,7 +564,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase): uri = result.json()['uri'] fp_path_id = re.sub('/user_uploads/', '', uri) body = "First message ...[zulip.txt](http://{}/user_uploads/".format(user.realm.host) + fp_path_id + ")" - self.send_stream_message(user.email, stream_name, body, "test") + self.send_stream_message(user, stream_name, body, "test") self.logout() # Add aaron as a subscribed after the message was sent @@ -632,7 +632,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase): fp_path_id = re.sub('/user_uploads/', '', uri) for i in range(20): body = "First message ...[zulip.txt](http://{}/user_uploads/".format(hamlet.realm.host) + fp_path_id + ")" - self.send_stream_message(self.example_email("hamlet"), "test-subscribe %s" % (i % 5,), body, "test") + self.send_stream_message(self.example_user("hamlet"), "test-subscribe %s" % (i % 5,), body, "test") self.logout() user = self.example_user("aaron") @@ -674,7 +674,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase): uri = result.json()['uri'] fp_path_id = re.sub('/user_uploads/', '', uri) body = "First message ...[zulip.txt](http://{}/user_uploads/".format(realm.host) + fp_path_id + ")" - self.send_stream_message(self.example_email("hamlet"), "test-subscribe", body, "test") + self.send_stream_message(self.example_user("hamlet"), "test-subscribe", body, "test") self.logout() # Now all users should be able to access the files @@ -1513,7 +1513,7 @@ class S3Test(ZulipTestCase): self.subscribe(self.example_user("hamlet"), "Denmark") body = "First message ...[zulip.txt](http://localhost:9991" + uri + ")" - self.send_stream_message(self.example_email("hamlet"), "Denmark", body, "test") + self.send_stream_message(self.example_user("hamlet"), "Denmark", body, "test") self.assertIn('title="dummy.txt"', self.get_last_message().rendered_content) @use_s3_backend @@ -1567,7 +1567,7 @@ class S3Test(ZulipTestCase): self.subscribe(self.example_user("hamlet"), "Denmark") body = "First message ...[zulip.txt](http://localhost:9991" + uri + ")" - self.send_stream_message(self.example_email("hamlet"), "Denmark", body, "test") + self.send_stream_message(self.example_user("hamlet"), "Denmark", body, "test") self.assertIn('title="zulip.txt"', self.get_last_message().rendered_content) @use_s3_backend diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.py index 3eeb18547b..73cec4b983 100644 --- a/zerver/tests/test_users.py +++ b/zerver/tests/test_users.py @@ -1283,7 +1283,7 @@ class GetProfileTest(ZulipTestCase): def common_get_profile(self, user_id: str) -> Dict[str, Any]: # Assumes all users are example users in realm 'zulip' user_profile = self.example_user(user_id) - self.send_stream_message(user_profile.email, "Verona", "hello") + self.send_stream_message(user_profile, "Verona", "hello") result = self.api_get(user_profile.email, "/api/v1/users/me") @@ -1365,8 +1365,8 @@ class GetProfileTest(ZulipTestCase): Ensure GET /users/me returns a proper pointer id after the pointer is updated """ - id1 = self.send_stream_message(self.example_email("othello"), "Verona") - id2 = self.send_stream_message(self.example_email("othello"), "Verona") + id1 = self.send_stream_message(self.example_user("othello"), "Verona") + id2 = self.send_stream_message(self.example_user("othello"), "Verona") json = self.common_get_profile("hamlet")