Example #1
0
    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 as well as
        # the notification bot.
        zulip_realm = get_realm('zulip')
        url_format_string = r"https://trac.example.com/ticket/%(id)s"
        url = 'https://trac.example.com/ticket/123'
        topic_name = 'test #123'

        realm_filter = RealmFilter(realm=zulip_realm,
                                   pattern=r"#(?P<id>[0-9]{2,8})",
                                   url_format_string=url_format_string)
        self.assertEqual(
            realm_filter.__str__(), '<RealmFilter(zulip): #(?P<id>[0-9]{2,8})'
            ' https://trac.example.com/ticket/%(id)s>')

        def get_message(sender: UserProfile) -> Message:
            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:
            dct = MessageDict.to_dict_uncached_helper([msg])[0]
            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()))
Example #2
0
    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,
        # and not linkified when sent to a stream in 'lear'.
        zulip_realm = get_realm("zulip")
        lear_realm = get_realm("lear")
        url_format_string = r"https://trac.example.com/ticket/%(id)s"
        links = {"url": "https://trac.example.com/ticket/123", "text": "#123"}
        topic_name = "test #123"

        linkifier = RealmFilter(realm=zulip_realm,
                                pattern=r"#(?P<id>[0-9]{2,8})",
                                url_format_string=url_format_string)
        self.assertEqual(
            linkifier.__str__(),
            "<RealmFilter(zulip): #(?P<id>[0-9]{2,8}) https://trac.example.com/ticket/%(id)s>",
        )

        def get_message(sender: UserProfile, realm: Realm) -> Message:
            stream_name = "Denmark"
            if not Stream.objects.filter(realm=realm,
                                         name=stream_name).exists():
                self.make_stream(stream_name, realm)
            self.subscribe(sender, stream_name)
            msg_id = self.send_stream_message(sender, "Denmark", "hello world",
                                              topic_name, realm)
            return Message.objects.get(id=msg_id)

        def assert_topic_links(links: List[Dict[str, str]],
                               msg: Message) -> None:
            dct = MessageDict.to_dict_uncached_helper([msg])[0]
            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"),
                                       zulip_realm))
        assert_topic_links([],
                           get_message(self.lear_user("cordelia"), lear_realm))
        assert_topic_links([],
                           get_message(self.notification_bot(zulip_realm),
                                       zulip_realm))
        linkifier.save()
        assert_topic_links([links],
                           get_message(self.example_user("othello"),
                                       zulip_realm))
        assert_topic_links([],
                           get_message(self.lear_user("cordelia"), lear_realm))
        assert_topic_links([links],
                           get_message(self.notification_bot(zulip_realm),
                                       zulip_realm))
Example #3
0
    def test_realm_patterns(self):
        realm = get_realm('zulip.com')
        url_format_string = r"https://trac.zulip.net/ticket/%(id)s"
        realm_filter = RealmFilter(realm=realm,
                                   pattern=r"#(?P<id>[0-9]{2,8})",
                                   url_format_string=url_format_string)
        realm_filter.save()
        self.assertEqual(
            str(realm_filter),
            '<RealmFilter(zulip.com): #(?P<id>[0-9]{2,8})'
            ' https://trac.zulip.net/ticket/%(id)s>')


        msg = Message(sender=get_user_profile_by_email("*****@*****.**"),
                      subject="#444")

        content = "We should fix #224 and #115, but not issue#124 or #1124z or [trac #15](https://trac.zulip.net/ticket/16) today."
        converted = bugdown.convert(content, realm_domain='zulip.com', message=msg)
        converted_subject = bugdown.subject_links(realm.domain.lower(), msg.subject)

        self.assertEqual(converted, '<p>We should fix <a href="https://trac.zulip.net/ticket/224" target="_blank" title="https://trac.zulip.net/ticket/224">#224</a> and <a href="https://trac.zulip.net/ticket/115" target="_blank" title="https://trac.zulip.net/ticket/115">#115</a>, but not issue#124 or #1124z or <a href="https://trac.zulip.net/ticket/16" target="_blank" title="https://trac.zulip.net/ticket/16">trac #15</a> today.</p>')
        self.assertEqual(converted_subject,  [u'https://trac.zulip.net/ticket/444'])
Example #4
0
    def test_realm_patterns(self):
        RealmFilter(
            realm=get_realm('zulip.com'),
            pattern=r"#(?P<id>[0-9]{2,8})",
            url_format_string=r"https://trac.zulip.net/ticket/%(id)s").save()
        msg = Message(sender=get_user_profile_by_email("*****@*****.**"))

        content = "We should fix #224 and #115, but not issue#124 or #1124z or [trac #15](https://trac.zulip.net/ticket/16) today."
        converted = bugdown.convert(content,
                                    realm_domain='zulip.com',
                                    message=msg)

        self.assertEqual(
            converted,
            '<p>We should fix <a href="https://trac.zulip.net/ticket/224" target="_blank" title="https://trac.zulip.net/ticket/224">#224</a> and <a href="https://trac.zulip.net/ticket/115" target="_blank" title="https://trac.zulip.net/ticket/115">#115</a>, but not issue#124 or #1124z or <a href="https://trac.zulip.net/ticket/16" target="_blank" title="https://trac.zulip.net/ticket/16">trac #15</a> today.</p>'
        )
Example #5
0
def do_add_linkifier(realm: Realm, pattern: str,
                     url_format_string: str) -> int:
    pattern = pattern.strip()
    url_format_string = url_format_string.strip()
    linkifier = RealmFilter(realm=realm,
                            pattern=pattern,
                            url_format_string=url_format_string)
    linkifier.full_clean()
    linkifier.save()
    notify_linkifiers(realm)

    return linkifier.id