diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index da4a90a683..c9c8a86c94 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -2046,9 +2046,9 @@ def extract_emails(emails: Iterable[str]) -> List[str]: return recipients 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) - message = check_message(sender, client, addressee, body) + message = check_message(sender, client, addressee, body, realm) return do_send_messages([message])[0] diff --git a/zerver/lib/test_classes.py b/zerver/lib/test_classes.py index 736bcdfe69..8a1b65e19f 100644 --- a/zerver/lib/test_classes.py +++ b/zerver/lib/test_classes.py @@ -463,7 +463,8 @@ class ZulipTestCase(TestCase): ) 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)) (sending_client, _) = Client.objects.get_or_create(name="test suite") @@ -474,6 +475,7 @@ class ZulipTestCase(TestCase): stream_name=stream_name, topic=topic_name, body=content, + realm=recipient_realm, ) def get_messages_response(self, anchor: int=1, num_before: int=100, num_after: int=100, diff --git a/zerver/tests/test_messages.py b/zerver/tests/test_messages.py index 25374eca04..2ea79b3816 100644 --- a/zerver/tests/test_messages.py +++ b/zerver/tests/test_messages.py @@ -1282,58 +1282,38 @@ class MessageDictTest(ZulipTestCase): def test_topic_links_use_stream_realm(self) -> None: # Set up a realm filter on 'zulip' and assert that messages # sent to a stream on 'zulip' have the topic linkified from - # senders in both the 'zulip' and 'lear' realms. This test is - # a bit artificial; we really should be sending the message - # from `notification_bot`, no the lear realm, since that's the - # actual use case. + # senders in both the 'zulip' and 'lear' realms as well as + # the notification bot. zulip_realm = get_realm('zulip') - lear_realm = get_realm('lear') url_format_string = r"https://trac.zulip.net/ticket/%(id)s" 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' - recipient = get_stream_recipient(stream.id) - sending_client = make_client(name="test suite") realm_filter = RealmFilter(realm=zulip_realm, pattern=r"#(?P[0-9]{2,8})", url_format_string=url_format_string) - realm_filter.save() self.assertEqual( realm_filter.__str__(), '[0-9]{2,8})' ' https://trac.zulip.net/ticket/%(id)s>') - message_from_zulip = Message( - sender=othello, - recipient=recipient, - content='hello world', - 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() + 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) + return Message.objects.get(id=msg_id) - dct = MessageDict.to_dict_uncached_helper(message_from_zulip) - self.assertEqual(dct[TOPIC_LINKS], [url]) - dct = MessageDict.to_dict_uncached_helper(message_from_lear) - self.assertEqual(dct[TOPIC_LINKS], [url]) - self.assertNotEqual(lear_realm, zulip_realm) + def assert_topic_links(links: List[str], msg: Message) -> None: + dct = MessageDict.to_dict_uncached_helper(msg) + self.assertEqual(dct[TOPIC_LINKS], links) + + # 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: sender = self.example_user('othello')