tests: Clean up and extend topic links tests in test_messages.

This is a follow-up to b69213808a.
We now actually send messages from the notification_bot, which
is the real usecase for this code.

Also, this cleans up the code and removes needless asserts like
`assertNotEqual(zulip_realm, lear_realm)` making the test easier
to read.
This commit is contained in:
Rohitt Vashishtha 2019-10-15 20:53:28 +00:00 committed by Tim Abbott
parent 2504d9ffc9
commit 5fd0a121ea
3 changed files with 23 additions and 41 deletions

View File

@ -2046,9 +2046,9 @@ def extract_emails(emails: Iterable[str]) -> List[str]:
return recipients return recipients
def check_send_stream_message(sender: UserProfile, client: Client, stream_name: str, def check_send_stream_message(sender: UserProfile, client: Client, stream_name: str,
topic: str, body: str) -> int: topic: str, body: str, realm: Optional[Realm]=None) -> int:
addressee = Addressee.for_stream_name(stream_name, topic) addressee = Addressee.for_stream_name(stream_name, topic)
message = check_message(sender, client, addressee, body) message = check_message(sender, client, addressee, body, realm)
return do_send_messages([message])[0] return do_send_messages([message])[0]

View File

@ -463,7 +463,8 @@ class ZulipTestCase(TestCase):
) )
def send_stream_message(self, sender_email: str, stream_name: str, content: str="test content", def send_stream_message(self, sender_email: str, stream_name: str, content: str="test content",
topic_name: str="test", sender_realm: str="zulip") -> int: topic_name: str="test", sender_realm: str="zulip",
recipient_realm: Optional[Realm]=None) -> int:
sender = get_user(sender_email, get_realm(sender_realm)) sender = get_user(sender_email, get_realm(sender_realm))
(sending_client, _) = Client.objects.get_or_create(name="test suite") (sending_client, _) = Client.objects.get_or_create(name="test suite")
@ -474,6 +475,7 @@ class ZulipTestCase(TestCase):
stream_name=stream_name, stream_name=stream_name,
topic=topic_name, topic=topic_name,
body=content, body=content,
realm=recipient_realm,
) )
def get_messages_response(self, anchor: int=1, num_before: int=100, num_after: int=100, def get_messages_response(self, anchor: int=1, num_before: int=100, num_after: int=100,

View File

@ -1282,58 +1282,38 @@ class MessageDictTest(ZulipTestCase):
def test_topic_links_use_stream_realm(self) -> None: def test_topic_links_use_stream_realm(self) -> None:
# Set up a realm filter on 'zulip' and assert that messages # Set up a realm filter on 'zulip' and assert that messages
# sent to a stream on 'zulip' have the topic linkified from # sent to a stream on 'zulip' have the topic linkified from
# senders in both the 'zulip' and 'lear' realms. This test is # senders in both the 'zulip' and 'lear' realms as well as
# a bit artificial; we really should be sending the message # the notification bot.
# from `notification_bot`, no the lear realm, since that's the
# actual use case.
zulip_realm = get_realm('zulip') zulip_realm = get_realm('zulip')
lear_realm = get_realm('lear')
url_format_string = r"https://trac.zulip.net/ticket/%(id)s" url_format_string = r"https://trac.zulip.net/ticket/%(id)s"
url = 'https://trac.zulip.net/ticket/123' url = 'https://trac.zulip.net/ticket/123'
othello = self.example_user('othello')
cordelia = self.lear_user('cordelia')
stream = get_stream('Denmark', zulip_realm)
topic_name = 'test #123' topic_name = 'test #123'
recipient = get_stream_recipient(stream.id)
sending_client = make_client(name="test suite")
realm_filter = RealmFilter(realm=zulip_realm, realm_filter = RealmFilter(realm=zulip_realm,
pattern=r"#(?P<id>[0-9]{2,8})", pattern=r"#(?P<id>[0-9]{2,8})",
url_format_string=url_format_string) url_format_string=url_format_string)
realm_filter.save()
self.assertEqual( self.assertEqual(
realm_filter.__str__(), realm_filter.__str__(),
'<RealmFilter(zulip): #(?P<id>[0-9]{2,8})' '<RealmFilter(zulip): #(?P<id>[0-9]{2,8})'
' https://trac.zulip.net/ticket/%(id)s>') ' https://trac.zulip.net/ticket/%(id)s>')
message_from_zulip = Message( def get_message(sender: UserProfile) -> Message:
sender=othello, msg_id = self.send_stream_message(sender.email, 'Denmark', 'hello world', topic_name,
recipient=recipient, sender.realm.string_id, zulip_realm)
content='hello world', return Message.objects.get(id=msg_id)
date_sent=timezone_now(),
sending_client=sending_client,
last_edit_time=timezone_now(),
edit_history='[]'
)
message_from_zulip.set_topic_name(topic_name)
message_from_zulip.save()
message_from_lear = Message(
sender=cordelia,
recipient=recipient,
content='hello world',
date_sent=timezone_now(),
sending_client=sending_client,
last_edit_time=timezone_now(),
edit_history='[]'
)
message_from_lear.set_topic_name(topic_name)
message_from_lear.save()
dct = MessageDict.to_dict_uncached_helper(message_from_zulip) def assert_topic_links(links: List[str], msg: Message) -> None:
self.assertEqual(dct[TOPIC_LINKS], [url]) dct = MessageDict.to_dict_uncached_helper(msg)
dct = MessageDict.to_dict_uncached_helper(message_from_lear) self.assertEqual(dct[TOPIC_LINKS], links)
self.assertEqual(dct[TOPIC_LINKS], [url])
self.assertNotEqual(lear_realm, zulip_realm) # Send messages before and after saving the realm filter from each user.
assert_topic_links([], get_message(self.example_user('othello')))
assert_topic_links([], get_message(self.lear_user('cordelia')))
assert_topic_links([], get_message(self.notification_bot()))
realm_filter.save()
assert_topic_links([url], get_message(self.example_user('othello')))
assert_topic_links([url], get_message(self.lear_user('cordelia')))
assert_topic_links([url], get_message(self.notification_bot()))
def test_reaction(self) -> None: def test_reaction(self) -> None:
sender = self.example_user('othello') sender = self.example_user('othello')