2014-01-31 23:13:58 +01:00
# -*- coding: utf-8 -*-
2015-11-01 17:11:06 +01:00
from __future__ import print_function
2016-03-11 10:57:29 +01:00
from __future__ import absolute_import
2014-01-31 23:13:58 +01:00
from django . conf import settings
from django . test import TestCase
from zerver . lib import bugdown
from zerver . lib . actions import (
2016-02-12 21:08:56 +01:00
check_add_realm_emoji ,
2014-01-31 23:13:58 +01:00
do_remove_realm_emoji ,
2016-06-01 05:02:43 +02:00
do_set_alert_words ,
2017-01-04 05:30:48 +01:00
get_realm ,
2014-01-31 23:13:58 +01:00
)
2016-09-14 18:02:24 +02:00
from zerver . lib . alert_words import alert_words_in_realm
2016-04-28 06:43:40 +02:00
from zerver . lib . camo import get_camo_url
2016-10-04 18:32:46 +02:00
from zerver . lib . message import render_markdown
2016-09-15 22:05:56 +02:00
from zerver . lib . request import (
JsonableError ,
)
2016-11-10 19:30:09 +01:00
from zerver . lib . test_classes import (
2016-09-15 22:05:56 +02:00
ZulipTestCase ,
)
2016-10-26 20:56:17 +02:00
from zerver . lib . str_utils import force_str
2014-01-31 23:13:58 +01:00
from zerver . models import (
2016-12-31 03:08:43 +01:00
realm_in_local_realm_filters_cache ,
2016-09-23 20:06:37 +02:00
flush_per_request_caches ,
2016-09-23 21:55:21 +02:00
flush_realm_filter ,
2016-06-01 05:02:43 +02:00
get_client ,
2017-01-04 05:30:48 +01:00
get_realm ,
2014-01-31 23:13:58 +01:00
get_user_profile_by_email ,
2016-10-26 20:56:17 +02:00
get_stream ,
2016-12-31 03:08:43 +01:00
realm_filters_for_realm ,
2014-01-31 23:13:58 +01:00
Message ,
2016-10-26 20:56:17 +02:00
Stream ,
2016-09-23 21:55:21 +02:00
Realm ,
2014-01-31 23:13:58 +01:00
RealmFilter ,
2016-09-15 22:05:56 +02:00
Recipient ,
2014-01-31 23:13:58 +01:00
)
2016-04-30 00:25:58 +02:00
import mock
2014-01-31 23:13:58 +01:00
import os
import ujson
2016-03-11 10:57:29 +01:00
import six
2014-01-31 23:13:58 +01:00
2016-10-26 20:56:17 +02:00
from six . moves import urllib
2016-10-11 11:55:03 +02:00
from zerver . lib . str_utils import NonBinaryStr
2016-12-04 18:38:56 +01:00
from typing import Any , AnyStr , Dict , List , Optional , Tuple , Text
2016-10-11 11:55:03 +02:00
2014-01-31 23:13:58 +01:00
class FencedBlockPreprocessorTest ( TestCase ) :
def test_simple_quoting ( self ) :
2016-10-11 10:51:24 +02:00
# type: () -> None
2014-01-31 23:13:58 +01:00
processor = bugdown . fenced_code . FencedBlockPreprocessor ( None )
markdown = [
' ~~~ quote ' ,
' hi ' ,
' bye ' ,
' ' ,
' '
]
expected = [
' ' ,
' > hi ' ,
' > bye ' ,
' ' ,
' ' ,
' '
]
lines = processor . run ( markdown )
self . assertEqual ( lines , expected )
def test_serial_quoting ( self ) :
2016-10-11 10:51:24 +02:00
# type: () -> None
2014-01-31 23:13:58 +01:00
processor = bugdown . fenced_code . FencedBlockPreprocessor ( None )
markdown = [
' ~~~ quote ' ,
' hi ' ,
' ~~~ ' ,
' ' ,
' ~~~ quote ' ,
' bye ' ,
' ' ,
' '
]
expected = [
' ' ,
' > hi ' ,
' ' ,
' ' ,
' ' ,
' > bye ' ,
' ' ,
' ' ,
' '
]
lines = processor . run ( markdown )
self . assertEqual ( lines , expected )
def test_serial_code ( self ) :
2016-10-11 10:51:24 +02:00
# type: () -> None
2014-01-31 23:13:58 +01:00
processor = bugdown . fenced_code . FencedBlockPreprocessor ( None )
# Simulate code formatting.
2016-10-11 11:55:03 +02:00
processor . format_code = lambda lang , code : lang + ' : ' + code # type: ignore # mypy doesn't allow monkey-patching functions
processor . placeholder = lambda s : ' ** ' + s . strip ( ' \n ' ) + ' ** ' # type: ignore # https://github.com/python/mypy/issues/708
2014-01-31 23:13:58 +01:00
markdown = [
' ``` .py ' ,
' hello() ' ,
' ``` ' ,
' ' ,
' ``` .py ' ,
' goodbye() ' ,
' ``` ' ,
' ' ,
' '
]
expected = [
' ' ,
' **py:hello()** ' ,
' ' ,
' ' ,
' ' ,
' **py:goodbye()** ' ,
' ' ,
' ' ,
' '
]
lines = processor . run ( markdown )
self . assertEqual ( lines , expected )
def test_nested_code ( self ) :
2016-10-11 10:51:24 +02:00
# type: () -> None
2014-01-31 23:13:58 +01:00
processor = bugdown . fenced_code . FencedBlockPreprocessor ( None )
# Simulate code formatting.
2016-10-11 11:55:03 +02:00
processor . format_code = lambda lang , code : lang + ' : ' + code # type: ignore # mypy doesn't allow monkey-patching functions
processor . placeholder = lambda s : ' ** ' + s . strip ( ' \n ' ) + ' ** ' # type: ignore # https://github.com/python/mypy/issues/708
2014-01-31 23:13:58 +01:00
markdown = [
' ~~~ quote ' ,
' hi ' ,
' ``` .py ' ,
' hello() ' ,
' ``` ' ,
' ' ,
' '
]
expected = [
' ' ,
' > hi ' ,
' ' ,
' > **py:hello()** ' ,
' ' ,
' ' ,
' '
]
lines = processor . run ( markdown )
self . assertEqual ( lines , expected )
def bugdown_convert ( text ) :
2016-12-04 18:38:56 +01:00
# type: (Text) -> Text
2017-01-22 06:29:11 +01:00
return bugdown . convert ( text , message_realm = get_realm ( ' zulip ' ) )
2014-01-31 23:13:58 +01:00
class BugdownTest ( TestCase ) :
def common_bugdown_test ( self , text , expected ) :
2016-12-04 18:38:56 +01:00
# type: (Text, Text) -> None
2014-01-31 23:13:58 +01:00
converted = bugdown_convert ( text )
self . assertEqual ( converted , expected )
def load_bugdown_tests ( self ) :
2016-12-04 18:38:56 +01:00
# type: () -> Tuple[Dict[Text, Any], List[List[Text]]]
2014-01-31 23:13:58 +01:00
test_fixtures = { }
2016-04-12 07:16:09 +02:00
data_file = open ( os . path . join ( os . path . dirname ( __file__ ) , ' ../fixtures/bugdown-data.json ' ) , ' r ' )
2014-01-31 23:13:58 +01:00
data = ujson . loads ( ' \n ' . join ( data_file . readlines ( ) ) )
for test in data [ ' regular_tests ' ] :
test_fixtures [ test [ ' name ' ] ] = test
return test_fixtures , data [ ' linkify_tests ' ]
def test_bugdown_fixtures ( self ) :
2016-10-11 10:51:24 +02:00
# type: () -> None
2014-01-31 23:13:58 +01:00
format_tests , linkify_tests = self . load_bugdown_tests ( )
2016-10-11 11:55:03 +02:00
self . maxDiff = None # type: Optional[int]
2016-03-11 10:57:29 +01:00
for name , test in six . iteritems ( format_tests ) :
2014-01-31 23:13:58 +01:00
converted = bugdown_convert ( test [ ' input ' ] )
2015-11-01 17:11:06 +01:00
print ( " Running Bugdown test %s " % ( name , ) )
2014-01-31 23:13:58 +01:00
self . assertEqual ( converted , test [ ' expected_output ' ] )
def replaced ( payload , url , phrase = ' ' ) :
2016-12-04 18:38:56 +01:00
# type: (Text, Text, Text) -> Text
2014-01-31 23:13:58 +01:00
target = " target= \" _blank \" "
if url [ : 4 ] == ' http ' :
href = url
elif ' @ ' in url :
href = ' mailto: ' + url
target = " "
else :
href = ' http:// ' + url
return payload % ( " <a href= \" %s \" %s title= \" %s \" > %s </a> " % ( href , target , href , url ) , )
2015-11-01 17:11:06 +01:00
print ( " Running Bugdown Linkify tests " )
2016-10-11 11:55:03 +02:00
self . maxDiff = None # type: Optional[int]
2016-10-27 12:06:44 +02:00
with mock . patch ( ' zerver.lib.url_preview.preview.link_embed_data_from_cache ' , return_value = None ) :
for inline_url , reference , url in linkify_tests :
try :
match = replaced ( reference , url , phrase = inline_url )
except TypeError :
match = reference
converted = bugdown_convert ( inline_url )
self . assertEqual ( match , converted )
2014-01-31 23:13:58 +01:00
2016-03-10 17:17:40 +01:00
def test_inline_file ( self ) :
# type: () -> None
msg = ' Check out this file file:///Volumes/myserver/Users/Shared/pi.py '
converted = bugdown_convert ( msg )
self . assertEqual ( converted , ' <p>Check out this file <a href= " file:///Volumes/myserver/Users/Shared/pi.py " target= " _blank " title= " file:///Volumes/myserver/Users/Shared/pi.py " >file:///Volumes/myserver/Users/Shared/pi.py</a></p> ' )
with self . settings ( ENABLE_FILE_LINKS = False ) :
realm = Realm . objects . create (
domain = ' file_links_test.example.com ' ,
string_id = ' file_links_test ' )
bugdown . make_md_engine (
2016-12-31 03:08:43 +01:00
realm . id ,
2016-03-10 17:17:40 +01:00
{ ' realm_filters ' : [ [ ] , u ' file_links_test.example.com ' ] , ' realm ' : [ u ' file_links_test.example.com ' , ' Realm name ' ] } )
2017-01-22 06:29:11 +01:00
converted = bugdown . convert ( msg , message_realm = realm )
2016-03-10 17:17:40 +01:00
self . assertEqual ( converted , ' <p>Check out this file file:///Volumes/myserver/Users/Shared/pi.py</p> ' )
2014-01-31 23:13:58 +01:00
def test_inline_youtube ( self ) :
2016-10-11 10:51:24 +02:00
# type: () -> None
2014-01-31 23:13:58 +01:00
msg = ' Check out the debate: http://www.youtube.com/watch?v=hx1mjT73xYE '
converted = bugdown_convert ( msg )
2016-10-17 22:02:01 +02:00
self . assertEqual ( converted , ' <p>Check out the debate: <a href= " http://www.youtube.com/watch?v=hx1mjT73xYE " target= " _blank " title= " http://www.youtube.com/watch?v=hx1mjT73xYE " >http://www.youtube.com/watch?v=hx1mjT73xYE</a></p> \n <div class= " youtube-video message_inline_image " ><a data-id= " hx1mjT73xYE " href= " http://www.youtube.com/watch?v=hx1mjT73xYE " target= " _blank " title= " http://www.youtube.com/watch?v=hx1mjT73xYE " ><img src= " https://i.ytimg.com/vi/hx1mjT73xYE/default.jpg " ></a></div> ' )
2014-01-31 23:13:58 +01:00
def test_inline_dropbox ( self ) :
2016-10-11 10:51:24 +02:00
# type: () -> None
2014-01-31 23:13:58 +01:00
msg = ' Look at how hilarious our old office was: https://www.dropbox.com/s/ymdijjcg67hv2ta/IMG_0923.JPG '
2016-04-30 00:25:58 +02:00
image_info = { ' image ' : ' https://photos-4.dropbox.com/t/2/AABIre1oReJgPYuc_53iv0IHq1vUzRaDg2rrCfTpiWMccQ/12/129/jpeg/1024x1024/2/_/0/4/IMG_0923.JPG/CIEBIAEgAiAHKAIoBw/ymdijjcg67hv2ta/AABz2uuED1ox3vpWWvMpBxu6a/IMG_0923.JPG ' , ' desc ' : ' Shared with Dropbox ' , ' title ' : ' IMG_0923.JPG ' }
with mock . patch ( ' zerver.lib.bugdown.fetch_open_graph_image ' , return_value = image_info ) :
converted = bugdown_convert ( msg )
2014-01-31 23:13:58 +01:00
2014-07-17 02:41:49 +02:00
self . assertEqual ( converted , ' <p>Look at how hilarious our old office was: <a href= " https://www.dropbox.com/s/ymdijjcg67hv2ta/IMG_0923.JPG " target= " _blank " title= " https://www.dropbox.com/s/ymdijjcg67hv2ta/IMG_0923.JPG " >https://www.dropbox.com/s/ymdijjcg67hv2ta/IMG_0923.JPG</a></p> \n <div class= " message_inline_image " ><a href= " https://www.dropbox.com/s/ymdijjcg67hv2ta/IMG_0923.JPG " target= " _blank " title= " IMG_0923.JPG " ><img src= " https://www.dropbox.com/s/ymdijjcg67hv2ta/IMG_0923.JPG?dl=1 " ></a></div> ' )
2014-01-31 23:13:58 +01:00
2014-07-17 02:41:49 +02:00
msg = ' Look at my hilarious drawing folder: https://www.dropbox.com/sh/cm39k9e04z7fhim/AAAII5NK-9daee3FcF41anEua?dl= '
2016-04-30 00:25:58 +02:00
image_info = { ' image ' : ' https://cf.dropboxstatic.com/static/images/icons128/folder_dropbox.png ' , ' desc ' : ' Shared with Dropbox ' , ' title ' : ' Saves ' }
with mock . patch ( ' zerver.lib.bugdown.fetch_open_graph_image ' , return_value = image_info ) :
converted = bugdown_convert ( msg )
2014-01-31 23:13:58 +01:00
2016-04-30 00:25:58 +02:00
self . assertEqual ( converted , ' <p>Look at my hilarious drawing folder: <a href= " https://www.dropbox.com/sh/cm39k9e04z7fhim/AAAII5NK-9daee3FcF41anEua?dl= " target= " _blank " title= " https://www.dropbox.com/sh/cm39k9e04z7fhim/AAAII5NK-9daee3FcF41anEua?dl= " >https://www.dropbox.com/sh/cm39k9e04z7fhim/AAAII5NK-9daee3FcF41anEua?dl=</a></p> \n <div class= " message_inline_ref " ><a href= " https://www.dropbox.com/sh/cm39k9e04z7fhim/AAAII5NK-9daee3FcF41anEua?dl= " target= " _blank " title= " Saves " ><img src= " https://cf.dropboxstatic.com/static/images/icons128/folder_dropbox.png " ></a><div><div class= " message_inline_image_title " >Saves</div><desc class= " message_inline_image_desc " ></desc></div></div> ' )
2014-01-31 23:13:58 +01:00
2016-04-30 00:25:58 +02:00
def test_inline_dropbox_preview ( self ) :
2016-10-11 10:51:24 +02:00
# type: () -> None
2014-02-26 21:25:27 +01:00
# Test photo album previews
msg = ' https://www.dropbox.com/sc/tditp9nitko60n5/03rEiZldy5 '
2016-04-30 00:25:58 +02:00
image_info = { ' image ' : ' https://photos-6.dropbox.com/t/2/AAAlawaeD61TyNewO5vVi-DGf2ZeuayfyHFdNTNzpGq-QA/12/271544745/jpeg/1024x1024/2/_/0/5/baby-piglet.jpg/CKnjvYEBIAIgBygCKAc/tditp9nitko60n5/AADX03VAIrQlTl28CtujDcMla/0 ' , ' desc ' : ' Shared with Dropbox ' , ' title ' : ' 1 photo ' }
with mock . patch ( ' zerver.lib.bugdown.fetch_open_graph_image ' , return_value = image_info ) :
converted = bugdown_convert ( msg )
2014-02-26 21:25:27 +01:00
2016-04-30 00:25:58 +02:00
self . assertEqual ( converted , ' <p><a href= " https://www.dropbox.com/sc/tditp9nitko60n5/03rEiZldy5 " target= " _blank " title= " https://www.dropbox.com/sc/tditp9nitko60n5/03rEiZldy5 " >https://www.dropbox.com/sc/tditp9nitko60n5/03rEiZldy5</a></p> \n <div class= " message_inline_image " ><a href= " https://www.dropbox.com/sc/tditp9nitko60n5/03rEiZldy5 " target= " _blank " title= " 1 photo " ><img src= " https://photos-6.dropbox.com/t/2/AAAlawaeD61TyNewO5vVi-DGf2ZeuayfyHFdNTNzpGq-QA/12/271544745/jpeg/1024x1024/2/_/0/5/baby-piglet.jpg/CKnjvYEBIAIgBygCKAc/tditp9nitko60n5/AADX03VAIrQlTl28CtujDcMla/0 " ></a></div> ' )
2014-01-31 23:13:58 +01:00
2016-04-30 00:25:58 +02:00
def test_inline_dropbox_negative ( self ) :
2016-10-11 10:51:24 +02:00
# type: () -> None
2014-01-31 23:13:58 +01:00
# Make sure we're not overzealous in our conversion:
msg = ' Look at the new dropbox logo: https://www.dropbox.com/static/images/home_logo.png '
2016-04-30 00:25:58 +02:00
with mock . patch ( ' zerver.lib.bugdown.fetch_open_graph_image ' , return_value = None ) :
converted = bugdown_convert ( msg )
2014-01-31 23:13:58 +01:00
self . assertEqual ( converted , ' <p>Look at the new dropbox logo: <a href= " https://www.dropbox.com/static/images/home_logo.png " target= " _blank " title= " https://www.dropbox.com/static/images/home_logo.png " >https://www.dropbox.com/static/images/home_logo.png</a></p> \n <div class= " message_inline_image " ><a href= " https://www.dropbox.com/static/images/home_logo.png " target= " _blank " title= " https://www.dropbox.com/static/images/home_logo.png " ><img src= " https://www.dropbox.com/static/images/home_logo.png " ></a></div> ' )
2016-04-30 00:25:58 +02:00
def test_inline_dropbox_bad ( self ) :
2016-10-11 10:51:24 +02:00
# type: () -> None
2014-07-17 02:41:49 +02:00
# Don't fail on bad dropbox links
2016-04-30 00:25:58 +02:00
msg = " https://zulip-test.dropbox.com/photos/cl/ROmr9K1XYtmpneM "
2016-10-27 12:06:44 +02:00
converted = bugdown_convert ( msg )
2016-04-30 00:40:52 +02:00
self . assertEqual ( converted , ' <p><a href= " https://zulip-test.dropbox.com/photos/cl/ROmr9K1XYtmpneM " target= " _blank " title= " https://zulip-test.dropbox.com/photos/cl/ROmr9K1XYtmpneM " >https://zulip-test.dropbox.com/photos/cl/ROmr9K1XYtmpneM</a></p> ' )
2014-07-17 02:41:49 +02:00
2014-01-31 23:13:58 +01:00
def test_twitter_id_extraction ( self ) :
2016-10-11 10:51:24 +02:00
# type: () -> None
2014-01-31 23:13:58 +01:00
self . assertEqual ( bugdown . get_tweet_id ( ' http://twitter.com/#!/VizzQuotes/status/409030735191097344 ' ) , ' 409030735191097344 ' )
self . assertEqual ( bugdown . get_tweet_id ( ' http://twitter.com/VizzQuotes/status/409030735191097344 ' ) , ' 409030735191097344 ' )
self . assertEqual ( bugdown . get_tweet_id ( ' http://twitter.com/VizzQuotes/statuses/409030735191097344 ' ) , ' 409030735191097344 ' )
self . assertEqual ( bugdown . get_tweet_id ( ' https://twitter.com/wdaher/status/1017581858 ' ) , ' 1017581858 ' )
self . assertEqual ( bugdown . get_tweet_id ( ' https://twitter.com/wdaher/status/1017581858/ ' ) , ' 1017581858 ' )
self . assertEqual ( bugdown . get_tweet_id ( ' https://twitter.com/windyoona/status/410766290349879296/photo/1 ' ) , ' 410766290349879296 ' )
self . assertEqual ( bugdown . get_tweet_id ( ' https://twitter.com/windyoona/status/410766290349879296/ ' ) , ' 410766290349879296 ' )
def test_inline_interesting_links ( self ) :
2016-10-11 10:51:24 +02:00
# type: () -> None
2014-01-31 23:13:58 +01:00
def make_link ( url ) :
2016-12-04 18:38:56 +01:00
# type: (Text) -> Text
2014-01-31 23:13:58 +01:00
return ' <a href= " %s " target= " _blank " title= " %s " > %s </a> ' % ( url , url , url )
2016-07-16 22:35:27 +02:00
normal_tweet_html = ( ' <a href= " https://twitter.com/twitter " target= " _blank " '
' title= " https://twitter.com/twitter " >@twitter</a> '
' meets @seepicturely at #tcdisrupt cc. '
' <a href= " https://twitter.com/boscomonkey " target= " _blank " '
' title= " https://twitter.com/boscomonkey " >@boscomonkey</a> '
' <a href= " https://twitter.com/episod " target= " _blank " '
' title= " https://twitter.com/episod " >@episod</a> '
' <a href= " http://t.co/6J2EgYM " target= " _blank " '
' title= " http://t.co/6J2EgYM " >http://instagram.com/p/MuW67/</a> ' )
2014-01-31 23:13:58 +01:00
mention_in_link_tweet_html = """ <a href= " http://t.co/@foo " target= " _blank " title= " http://t.co/@foo " >http://foo.com</a> """
2016-07-16 22:35:27 +02:00
media_tweet_html = ( ' <a href= " http://t.co/xo7pAhK6n3 " target= " _blank " title= " http://t.co/xo7pAhK6n3 " > '
' http://twitter.com/NEVNBoston/status/421654515616849920/photo/1</a> ' )
2014-01-31 23:13:58 +01:00
def make_inline_twitter_preview ( url , tweet_html , image_html = ' ' ) :
2016-12-04 18:38:56 +01:00
# type: (Text, Text, Text) -> Text
2014-01-31 23:13:58 +01:00
## As of right now, all previews are mocked to be the exact same tweet
2016-07-16 22:35:27 +02:00
return ( ' <div class= " inline-preview-twitter " > '
2016-11-30 14:17:35 +01:00
' <div class= " twitter-tweet " > '
' <a href= " %s " target= " _blank " > '
' <img class= " twitter-avatar " '
' src= " https://si0.twimg.com/profile_images/1380912173/Screen_shot_2011-06-03_at_7.35.36_PM_normal.png " > '
' </a> '
' <p> %s </p> '
' <span>- Eoin McMillan (@imeoin)</span> '
' %s '
' </div> '
2016-07-16 22:35:27 +02:00
' </div> ' ) % ( url , tweet_html , image_html )
2014-01-31 23:13:58 +01:00
msg = ' http://www.twitter.com '
converted = bugdown_convert ( msg )
self . assertEqual ( converted , ' <p> %s </p> ' % make_link ( ' http://www.twitter.com ' ) )
msg = ' http://www.twitter.com/wdaher/ '
converted = bugdown_convert ( msg )
self . assertEqual ( converted , ' <p> %s </p> ' % make_link ( ' http://www.twitter.com/wdaher/ ' ) )
msg = ' http://www.twitter.com/wdaher/status/3 '
converted = bugdown_convert ( msg )
self . assertEqual ( converted , ' <p> %s </p> ' % make_link ( ' http://www.twitter.com/wdaher/status/3 ' ) )
# id too long
msg = ' http://www.twitter.com/wdaher/status/2879779692873154569 '
converted = bugdown_convert ( msg )
self . assertEqual ( converted , ' <p> %s </p> ' % make_link ( ' http://www.twitter.com/wdaher/status/2879779692873154569 ' ) )
# id too large (i.e. tweet doesn't exist)
msg = ' http://www.twitter.com/wdaher/status/999999999999999999 '
converted = bugdown_convert ( msg )
self . assertEqual ( converted , ' <p> %s </p> ' % make_link ( ' http://www.twitter.com/wdaher/status/999999999999999999 ' ) )
msg = ' http://www.twitter.com/wdaher/status/287977969287315456 '
converted = bugdown_convert ( msg )
2016-07-16 22:35:27 +02:00
self . assertEqual ( converted , ' <p> %s </p> \n %s ' % (
make_link ( ' http://www.twitter.com/wdaher/status/287977969287315456 ' ) ,
make_inline_twitter_preview ( ' http://www.twitter.com/wdaher/status/287977969287315456 ' , normal_tweet_html ) ) )
2014-01-31 23:13:58 +01:00
msg = ' https://www.twitter.com/wdaher/status/287977969287315456 '
converted = bugdown_convert ( msg )
2016-07-16 22:35:27 +02:00
self . assertEqual ( converted , ' <p> %s </p> \n %s ' % (
make_link ( ' https://www.twitter.com/wdaher/status/287977969287315456 ' ) ,
make_inline_twitter_preview ( ' https://www.twitter.com/wdaher/status/287977969287315456 ' , normal_tweet_html ) ) )
2014-01-31 23:13:58 +01:00
msg = ' http://twitter.com/wdaher/status/287977969287315456 '
converted = bugdown_convert ( msg )
2016-07-16 22:35:27 +02:00
self . assertEqual ( converted , ' <p> %s </p> \n %s ' % (
make_link ( ' http://twitter.com/wdaher/status/287977969287315456 ' ) ,
make_inline_twitter_preview ( ' http://twitter.com/wdaher/status/287977969287315456 ' , normal_tweet_html ) ) )
2014-01-31 23:13:58 +01:00
# A max of 3 will be converted
2016-07-16 22:35:27 +02:00
msg = ( ' http://twitter.com/wdaher/status/287977969287315456 '
' http://twitter.com/wdaher/status/287977969287315457 '
' http://twitter.com/wdaher/status/287977969287315457 '
' http://twitter.com/wdaher/status/287977969287315457 ' )
2014-01-31 23:13:58 +01:00
converted = bugdown_convert ( msg )
2016-07-16 22:35:27 +02:00
self . assertEqual ( converted , ' <p> %s %s %s %s </p> \n %s %s %s ' % (
make_link ( ' http://twitter.com/wdaher/status/287977969287315456 ' ) ,
make_link ( ' http://twitter.com/wdaher/status/287977969287315457 ' ) ,
make_link ( ' http://twitter.com/wdaher/status/287977969287315457 ' ) ,
make_link ( ' http://twitter.com/wdaher/status/287977969287315457 ' ) ,
make_inline_twitter_preview ( ' http://twitter.com/wdaher/status/287977969287315456 ' , normal_tweet_html ) ,
make_inline_twitter_preview ( ' http://twitter.com/wdaher/status/287977969287315457 ' , normal_tweet_html ) ,
make_inline_twitter_preview ( ' http://twitter.com/wdaher/status/287977969287315457 ' , normal_tweet_html ) ) )
2014-01-31 23:13:58 +01:00
# Tweet has a mention in a URL, only the URL is linked
msg = ' http://twitter.com/wdaher/status/287977969287315458 '
converted = bugdown_convert ( msg )
2016-07-16 22:35:27 +02:00
self . assertEqual ( converted , ' <p> %s </p> \n %s ' % (
make_link ( ' http://twitter.com/wdaher/status/287977969287315458 ' ) ,
make_inline_twitter_preview ( ' http://twitter.com/wdaher/status/287977969287315458 ' , mention_in_link_tweet_html ) ) )
2014-01-31 23:13:58 +01:00
# Tweet with an image
msg = ' http://twitter.com/wdaher/status/287977969287315459 '
converted = bugdown_convert ( msg )
2016-07-16 22:35:27 +02:00
self . assertEqual ( converted , ' <p> %s </p> \n %s ' % (
make_link ( ' http://twitter.com/wdaher/status/287977969287315459 ' ) ,
make_inline_twitter_preview ( ' http://twitter.com/wdaher/status/287977969287315459 ' ,
media_tweet_html ,
( ' <div class= " twitter-image " > '
2016-11-30 14:17:35 +01:00
' <a href= " http://t.co/xo7pAhK6n3 " target= " _blank " title= " http://t.co/xo7pAhK6n3 " > '
' <img src= " https://pbs.twimg.com/media/BdoEjD4IEAIq86Z.jpg:small " > '
' </a> '
2016-07-16 22:35:27 +02:00
' </div> ' ) ) ) )
2014-01-31 23:13:58 +01:00
2015-09-30 09:55:56 +02:00
def test_fetch_tweet_data_settings_validation ( self ) :
2016-10-11 10:51:24 +02:00
# type: () -> None
2015-10-02 13:01:25 +02:00
with self . settings ( TEST_SUITE = False , TWITTER_CONSUMER_KEY = None ) :
2015-09-30 09:55:56 +02:00
self . assertIs ( None , bugdown . fetch_tweet_data ( ' 287977969287315459 ' ) )
2014-01-31 23:13:58 +01:00
def test_realm_emoji ( self ) :
2016-10-11 10:51:24 +02:00
# type: () -> None
2014-01-31 23:13:58 +01:00
def emoji_img ( name , url ) :
2016-12-04 18:38:56 +01:00
# type: (Text, Text) -> Text
2016-04-28 06:43:40 +02:00
return ' <img alt= " %s " class= " emoji " src= " %s " title= " %s " > ' % ( name , get_camo_url ( url ) , name )
2014-01-31 23:13:58 +01:00
2017-01-04 05:30:48 +01:00
realm = get_realm ( ' zulip ' )
2014-01-31 23:13:58 +01:00
url = " https://zulip.com/test_realm_emoji.png "
2016-12-31 03:08:43 +01:00
check_add_realm_emoji ( realm , " test " , url )
2014-01-31 23:13:58 +01:00
# Needs to mock an actual message because that's how bugdown obtains the realm
msg = Message ( sender = get_user_profile_by_email ( " hamlet@zulip.com " ) )
2017-01-22 06:29:11 +01:00
converted = bugdown . convert ( " :test: " , message_realm = realm , message = msg )
2016-11-28 23:29:01 +01:00
self . assertEqual ( converted , ' <p> %s </p> ' % ( emoji_img ( ' :test: ' , url ) ) )
2014-01-31 23:13:58 +01:00
2016-12-31 03:08:43 +01:00
do_remove_realm_emoji ( realm , ' test ' )
2017-01-22 06:29:11 +01:00
converted = bugdown . convert ( " :test: " , message_realm = realm , message = msg )
2014-01-31 23:13:58 +01:00
self . assertEqual ( converted , ' <p>:test:</p> ' )
2016-06-24 20:03:56 +02:00
def test_unicode_emoji ( self ) :
2016-10-11 10:51:24 +02:00
# type: () -> None
2016-06-24 20:03:56 +02:00
msg = u ' \u2615 ' # ☕
converted = bugdown_convert ( msg )
2016-12-28 05:07:10 +01:00
self . assertEqual ( converted , u ' <p><img alt= " \u2615 " class= " emoji " src= " /static/generated/emoji/images/emoji/unicode/2615.png " title= " \u2615 " ></p> ' )
2016-06-24 20:03:56 +02:00
msg = u ' \u2615 \u2615 ' # ☕☕
converted = bugdown_convert ( msg )
2016-12-28 05:07:10 +01:00
self . assertEqual ( converted , u ' <p><img alt= " \u2615 " class= " emoji " src= " /static/generated/emoji/images/emoji/unicode/2615.png " title= " \u2615 " ><img alt= " \u2615 " class= " emoji " src= " /static/generated/emoji/images/emoji/unicode/2615.png " title= " \u2615 " ></p> ' )
2016-06-24 20:03:56 +02:00
2014-01-31 23:13:58 +01:00
def test_realm_patterns ( self ) :
2016-10-11 10:51:24 +02:00
# type: () -> None
2017-01-04 05:30:48 +01:00
realm = get_realm ( ' zulip ' )
2016-09-19 23:30:41 +02:00
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 (
2016-02-13 19:17:15 +01:00
realm_filter . __unicode__ ( ) ,
2016-09-19 23:30:41 +02:00
' <RealmFilter(zulip.com): #(?P<id>[0-9] { 2,8}) '
' https://trac.zulip.net/ticket/ %(id)s > ' )
2016-06-01 04:45:00 +02:00
msg = Message ( sender = get_user_profile_by_email ( " othello@zulip.com " ) ,
subject = " #444 " )
2014-01-31 23:13:58 +01:00
2016-09-23 20:06:37 +02:00
flush_per_request_caches ( )
2014-01-31 23:13:58 +01:00
content = " We should fix #224 and #115, but not issue#124 or #1124z or [trac #15](https://trac.zulip.net/ticket/16) today. "
2017-01-22 06:29:11 +01:00
converted = bugdown . convert ( content , message_realm = realm , message = msg )
2016-12-31 03:08:43 +01:00
converted_subject = bugdown . subject_links ( realm . id , msg . subject )
2014-01-31 23:13:58 +01:00
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> ' )
2017-01-24 06:21:14 +01:00
self . assertEqual ( converted_subject , [ u ' https://trac.zulip.net/ticket/444 ' ] )
2014-01-31 23:13:58 +01:00
2016-12-31 03:08:43 +01:00
RealmFilter ( realm = realm , pattern = r ' #(?P<id>[a-zA-Z]+-[0-9]+) ' ,
2016-02-13 19:17:15 +01:00
url_format_string = r ' https://trac.zulip.net/ticket/ %(id)s ' ) . save ( )
msg = Message ( sender = get_user_profile_by_email ( ' hamlet@zulip.com ' ) )
content = ' #ZUL-123 was fixed and code was deployed to production, also #zul-321 was deployed to staging '
2017-01-22 06:29:11 +01:00
converted = bugdown . convert ( content , message_realm = realm , message = msg )
2016-02-13 19:17:15 +01:00
self . assertEqual ( converted , ' <p><a href= " https://trac.zulip.net/ticket/ZUL-123 " target= " _blank " title= " https://trac.zulip.net/ticket/ZUL-123 " >#ZUL-123</a> was fixed and code was deployed to production, also <a href= " https://trac.zulip.net/ticket/zul-321 " target= " _blank " title= " https://trac.zulip.net/ticket/zul-321 " >#zul-321</a> was deployed to staging</p> ' )
2016-09-23 21:02:22 +02:00
def test_maybe_update_realm_filters ( self ) :
# type: () -> None
2017-01-04 05:30:48 +01:00
realm = get_realm ( ' zulip ' )
2016-09-23 21:02:22 +02:00
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 ( )
bugdown . realm_filter_data = { }
2016-12-31 03:08:43 +01:00
bugdown . maybe_update_realm_filters ( None )
2016-09-23 21:02:22 +02:00
all_filters = bugdown . realm_filter_data
2016-12-31 03:08:43 +01:00
zulip_filters = all_filters [ realm . id ]
2016-09-23 21:02:22 +02:00
self . assertEqual ( len ( zulip_filters ) , 1 )
self . assertEqual ( zulip_filters [ 0 ] ,
2016-12-03 00:04:17 +01:00
( u ' #(?P<id>[0-9] { 2,8}) ' , u ' https://trac.zulip.net/ticket/ %(id)s ' , realm_filter . id ) )
2016-09-23 21:02:22 +02:00
2016-09-23 21:55:21 +02:00
def test_flush_realm_filter ( self ) :
# type: () -> None
2017-01-04 05:30:48 +01:00
realm = get_realm ( ' zulip ' )
2016-09-23 21:55:21 +02:00
def flush ( ) :
# type: () -> None
'''
flush_realm_filter is a post - save hook , so calling it
directly for testing is kind of awkward
'''
class Instance ( object ) :
2017-01-03 21:04:55 +01:00
realm_id = None # type: Optional[int]
2016-09-23 21:55:21 +02:00
instance = Instance ( )
2017-01-03 21:04:55 +01:00
instance . realm_id = realm . id
2016-09-23 21:55:21 +02:00
flush_realm_filter ( sender = None , instance = instance )
def save_new_realm_filter ( ) :
# type: () -> None
realm_filter = RealmFilter ( realm = realm ,
pattern = r " whatever " ,
url_format_string = ' whatever ' )
realm_filter . save ( )
2017-01-08 20:24:05 +01:00
# start fresh for our realm
2016-09-23 21:55:21 +02:00
flush ( )
2016-12-31 03:08:43 +01:00
self . assertFalse ( realm_in_local_realm_filters_cache ( realm . id ) )
2016-09-23 21:55:21 +02:00
# call this just for side effects of populating the cache
2016-12-31 03:08:43 +01:00
realm_filters_for_realm ( realm . id )
self . assertTrue ( realm_in_local_realm_filters_cache ( realm . id ) )
2016-09-23 21:55:21 +02:00
# Saving a new RealmFilter should have the side effect of
# flushing the cache.
save_new_realm_filter ( )
2016-12-31 03:08:43 +01:00
self . assertFalse ( realm_in_local_realm_filters_cache ( realm . id ) )
2016-09-23 21:55:21 +02:00
# and flush it one more time, to make sure we don't get a KeyError
flush ( )
2016-12-31 03:08:43 +01:00
self . assertFalse ( realm_in_local_realm_filters_cache ( realm . id ) )
2016-09-23 21:55:21 +02:00
2016-06-01 04:50:26 +02:00
def test_realm_patterns_negative ( self ) :
2016-10-11 10:51:24 +02:00
# type: () -> None
2017-01-04 05:30:48 +01:00
realm = get_realm ( ' zulip ' )
2016-06-01 04:50:26 +02:00
RealmFilter ( realm = realm , pattern = r " #(?P<id>[0-9] { 2,8}) " ,
url_format_string = r " https://trac.zulip.net/ticket/ %(id)s " ) . save ( )
boring_msg = Message ( sender = get_user_profile_by_email ( " othello@zulip.com " ) ,
subject = u " no match here " )
2016-12-31 03:08:43 +01:00
converted_boring_subject = bugdown . subject_links ( realm . id , boring_msg . subject )
2017-01-24 06:21:14 +01:00
self . assertEqual ( converted_boring_subject , [ ] )
2016-06-01 04:50:26 +02:00
2016-10-03 17:53:53 +02:00
def test_is_status_message ( self ) :
# type: () -> None
user_profile = get_user_profile_by_email ( " othello@zulip.com " )
msg = Message ( sender = user_profile , sending_client = get_client ( " test " ) )
content = ' /me makes a list \n * one \n * two '
2016-10-04 18:32:46 +02:00
rendered_content = render_markdown ( msg , content )
2016-10-03 17:53:53 +02:00
self . assertEqual (
rendered_content ,
' <p>/me makes a list</p> \n <ul> \n <li>one</li> \n <li>two</li> \n </ul> '
)
self . assertFalse ( Message . is_status_message ( content , rendered_content ) )
content = ' /me takes a walk '
2016-10-04 18:32:46 +02:00
rendered_content = render_markdown ( msg , content )
2016-10-03 17:53:53 +02:00
self . assertEqual (
rendered_content ,
' <p>/me takes a walk</p> '
)
self . assertTrue ( Message . is_status_message ( content , rendered_content ) )
2016-06-01 05:02:43 +02:00
def test_alert_words ( self ) :
2016-10-11 10:51:24 +02:00
# type: () -> None
2016-06-01 05:02:43 +02:00
user_profile = get_user_profile_by_email ( " othello@zulip.com " )
do_set_alert_words ( user_profile , [ " ALERTWORD " , " scaryword " ] )
msg = Message ( sender = user_profile , sending_client = get_client ( " test " ) )
2016-09-14 18:02:24 +02:00
realm_alert_words = alert_words_in_realm ( user_profile . realm )
def render ( msg , content ) :
2016-12-04 18:38:56 +01:00
# type: (Message, Text) -> Text
2016-10-04 18:32:46 +02:00
return render_markdown ( msg ,
content ,
realm_alert_words = realm_alert_words ,
message_users = { user_profile } )
2016-06-01 05:02:43 +02:00
content = " We have an ALERTWORD day today! "
2016-09-14 18:02:24 +02:00
self . assertEqual ( render ( msg , content ) , " <p>We have an ALERTWORD day today!</p> " )
2016-06-01 05:02:43 +02:00
self . assertEqual ( msg . user_ids_with_alert_words , set ( [ user_profile . id ] ) )
msg = Message ( sender = user_profile , sending_client = get_client ( " test " ) )
content = " We have a NOTHINGWORD day today! "
2016-09-14 18:02:24 +02:00
self . assertEqual ( render ( msg , content ) , " <p>We have a NOTHINGWORD day today!</p> " )
2016-06-01 05:02:43 +02:00
self . assertEqual ( msg . user_ids_with_alert_words , set ( ) )
2016-06-01 05:24:01 +02:00
def test_mention_wildcard ( self ) :
2016-10-11 10:51:24 +02:00
# type: () -> None
2016-06-01 05:24:01 +02:00
user_profile = get_user_profile_by_email ( " othello@zulip.com " )
msg = Message ( sender = user_profile , sending_client = get_client ( " test " ) )
content = " @all test "
2016-10-04 18:32:46 +02:00
self . assertEqual ( render_markdown ( msg , content ) ,
2017-01-20 18:27:30 +01:00
' <p><span class= " user-mention " data-user-id= " * " >@all</span> test</p> ' )
2016-06-01 05:24:01 +02:00
self . assertTrue ( msg . mentions_wildcard )
def test_mention_everyone ( self ) :
2016-10-11 10:51:24 +02:00
# type: () -> None
2016-06-01 05:24:01 +02:00
user_profile = get_user_profile_by_email ( " othello@zulip.com " )
msg = Message ( sender = user_profile , sending_client = get_client ( " test " ) )
content = " @everyone test "
2016-10-04 18:32:46 +02:00
self . assertEqual ( render_markdown ( msg , content ) ,
2017-01-20 18:27:30 +01:00
' <p><span class= " user-mention " data-user-id= " * " >@everyone</span> test</p> ' )
2016-06-01 05:24:01 +02:00
self . assertTrue ( msg . mentions_wildcard )
def test_mention_single ( self ) :
2016-10-11 10:51:24 +02:00
# type: () -> None
2016-06-01 05:24:01 +02:00
sender_user_profile = get_user_profile_by_email ( " othello@zulip.com " )
user_profile = get_user_profile_by_email ( " hamlet@zulip.com " )
msg = Message ( sender = sender_user_profile , sending_client = get_client ( " test " ) )
2017-01-20 18:27:30 +01:00
user_id = user_profile . id
2016-06-01 05:24:01 +02:00
content = " @**King Hamlet** "
2016-10-04 18:32:46 +02:00
self . assertEqual ( render_markdown ( msg , content ) ,
2017-01-20 18:27:30 +01:00
' <p><span class= " user-mention " data-user-id= " %s " >@King Hamlet</span></p> ' % ( user_id , ) )
2016-06-01 05:24:01 +02:00
self . assertEqual ( msg . mentions_user_ids , set ( [ user_profile . id ] ) )
def test_mention_shortname ( self ) :
2016-10-11 10:51:24 +02:00
# type: () -> None
2016-06-01 05:24:01 +02:00
sender_user_profile = get_user_profile_by_email ( " othello@zulip.com " )
user_profile = get_user_profile_by_email ( " hamlet@zulip.com " )
msg = Message ( sender = sender_user_profile , sending_client = get_client ( " test " ) )
2017-01-20 18:27:30 +01:00
user_id = user_profile . id
2016-06-01 05:24:01 +02:00
content = " @**hamlet** "
2016-10-04 18:32:46 +02:00
self . assertEqual ( render_markdown ( msg , content ) ,
2017-01-20 18:27:30 +01:00
' <p><span class= " user-mention " data-user-id= " %s " >@King Hamlet</span></p> ' % ( user_id , ) )
2016-06-01 05:24:01 +02:00
self . assertEqual ( msg . mentions_user_ids , set ( [ user_profile . id ] ) )
def test_mention_multiple ( self ) :
2016-10-11 10:51:24 +02:00
# type: () -> None
2016-06-01 05:24:01 +02:00
sender_user_profile = get_user_profile_by_email ( " othello@zulip.com " )
hamlet = get_user_profile_by_email ( " hamlet@zulip.com " )
cordelia = get_user_profile_by_email ( " cordelia@zulip.com " )
msg = Message ( sender = sender_user_profile , sending_client = get_client ( " test " ) )
content = " @**King Hamlet** and @**cordelia**, check this out "
2016-10-04 18:32:46 +02:00
self . assertEqual ( render_markdown ( msg , content ) ,
2016-06-01 05:24:01 +02:00
' <p> '
' <span class= " user-mention " '
2017-01-20 18:27:30 +01:00
' data-user-id= " %s " >@King Hamlet</span> and '
2016-06-01 05:24:01 +02:00
' <span class= " user-mention " '
2017-01-20 18:27:30 +01:00
' data-user-id= " %s " >@Cordelia Lear</span>, '
' check this out</p> ' % ( hamlet . id , cordelia . id ) )
2016-06-01 05:24:01 +02:00
self . assertEqual ( msg . mentions_user_ids , set ( [ hamlet . id , cordelia . id ] ) )
def test_mention_invalid ( self ) :
2016-10-11 10:51:24 +02:00
# type: () -> None
2016-06-01 05:24:01 +02:00
sender_user_profile = get_user_profile_by_email ( " othello@zulip.com " )
msg = Message ( sender = sender_user_profile , sending_client = get_client ( " test " ) )
content = " Hey @**Nonexistent User** "
2016-10-04 18:32:46 +02:00
self . assertEqual ( render_markdown ( msg , content ) ,
2016-06-01 05:24:01 +02:00
' <p>Hey @<strong>Nonexistent User</strong></p> ' )
self . assertEqual ( msg . mentions_user_ids , set ( ) )
2016-10-26 20:56:17 +02:00
def test_stream_single ( self ) :
# type: () -> None
2017-01-04 05:30:48 +01:00
denmark = get_stream ( ' Denmark ' , get_realm ( ' zulip ' ) )
2016-10-26 20:56:17 +02:00
sender_user_profile = get_user_profile_by_email ( " othello@zulip.com " )
msg = Message ( sender = sender_user_profile , sending_client = get_client ( " test " ) )
content = " #**Denmark** "
self . assertEqual (
render_markdown ( msg , content ) ,
' <p><a class= " stream " data-stream-id= " {d.id} " href= " /#narrow/stream/Denmark " ># {d.name} </a></p> ' . format (
d = denmark
) )
def test_stream_multiple ( self ) :
# type: () -> None
sender_user_profile = get_user_profile_by_email ( " othello@zulip.com " )
msg = Message ( sender = sender_user_profile , sending_client = get_client ( " test " ) )
2017-01-04 05:30:48 +01:00
realm = get_realm ( ' zulip ' )
2016-10-26 20:56:17 +02:00
denmark = get_stream ( ' Denmark ' , realm )
scotland = get_stream ( ' Scotland ' , realm )
content = " Look to #**Denmark** and #**Scotland**, there something "
self . assertEqual ( render_markdown ( msg , content ) ,
' <p>Look to '
' <a class= " stream " '
' data-stream-id= " {denmark.id} " '
' href= " /#narrow/stream/Denmark " ># {denmark.name} </a> and '
' <a class= " stream " '
' data-stream-id= " {scotland.id} " '
' href= " /#narrow/stream/Scotland " ># {scotland.name} </a>, '
' there something</p> ' . format ( denmark = denmark , scotland = scotland ) )
def test_stream_case_sensitivity ( self ) :
# type: () -> None
2017-01-04 05:30:48 +01:00
realm = get_realm ( ' zulip ' )
2016-10-26 20:56:17 +02:00
case_sens = Stream . objects . create ( name = ' CaseSens ' , realm = realm )
sender_user_profile = get_user_profile_by_email ( " othello@zulip.com " )
msg = Message ( sender = sender_user_profile , sending_client = get_client ( " test " ) )
content = " #**CaseSens** "
self . assertEqual (
render_markdown ( msg , content ) ,
' <p><a class= " stream " data-stream-id= " {s.id} " href= " /#narrow/stream/ {s.name} " ># {s.name} </a></p> ' . format (
s = case_sens
) )
def test_stream_case_sensitivity_nonmatching ( self ) :
2016-11-02 04:40:10 +01:00
# type: () -> None
2016-10-26 20:56:17 +02:00
""" #StreamName requires the stream be spelled with the correct case
currently . If we change that in the future , we ' ll need to change this
test . """
2017-01-04 05:30:48 +01:00
realm = get_realm ( ' zulip ' )
2016-10-26 20:56:17 +02:00
Stream . objects . create ( name = ' CaseSens ' , realm = realm )
sender_user_profile = get_user_profile_by_email ( " othello@zulip.com " )
msg = Message ( sender = sender_user_profile , sending_client = get_client ( " test " ) )
content = " #**casesens** "
self . assertEqual (
render_markdown ( msg , content ) ,
' <p>#<strong>casesens</strong></p> ' )
def test_stream_unicode ( self ) :
# type: () -> None
2017-01-04 05:30:48 +01:00
realm = get_realm ( ' zulip ' )
2016-10-26 20:56:17 +02:00
uni = Stream . objects . create ( name = u ' привет ' , realm = realm )
sender_user_profile = get_user_profile_by_email ( " othello@zulip.com " )
msg = Message ( sender = sender_user_profile , sending_client = get_client ( " test " ) )
content = u " #**привет** "
self . assertEqual (
render_markdown ( msg , content ) ,
u ' <p><a class= " stream " data-stream-id= " {s.id} " href= " /#narrow/stream/ {url} " ># {s.name} </a></p> ' . format (
s = uni ,
url = urllib . parse . quote ( force_str ( uni . name ) )
) )
def test_stream_invalid ( self ) :
# type: () -> None
sender_user_profile = get_user_profile_by_email ( " othello@zulip.com " )
msg = Message ( sender = sender_user_profile , sending_client = get_client ( " test " ) )
content = " There #**Nonexistentstream** "
self . assertEqual ( render_markdown ( msg , content ) ,
' <p>There #<strong>Nonexistentstream</strong></p> ' )
self . assertEqual ( msg . mentions_user_ids , set ( ) )
2014-01-31 23:13:58 +01:00
def test_stream_subscribe_button_simple ( self ) :
2016-10-11 10:51:24 +02:00
# type: () -> None
2014-01-31 23:13:58 +01:00
msg = ' !_stream_subscribe_button(simple) '
converted = bugdown_convert ( msg )
self . assertEqual (
converted ,
' <p> '
' <span class= " inline-subscribe " data-stream-name= " simple " > '
2014-03-05 22:28:57 +01:00
' <button class= " inline-subscribe-button btn " >Subscribe to simple</button> '
2014-01-31 23:13:58 +01:00
' <span class= " inline-subscribe-error " ></span> '
' </span> '
' </p> '
)
def test_stream_subscribe_button_in_name ( self ) :
2016-10-11 10:51:24 +02:00
# type: () -> None
2014-01-31 23:13:58 +01:00
msg = ' !_stream_subscribe_button(simple (not \\ )) '
converted = bugdown_convert ( msg )
self . assertEqual (
converted ,
' <p> '
' <span class= " inline-subscribe " data-stream-name= " simple (not) " > '
2014-03-05 22:28:57 +01:00
' <button class= " inline-subscribe-button btn " >Subscribe to simple (not)</button> '
2014-01-31 23:13:58 +01:00
' <span class= " inline-subscribe-error " ></span> '
' </span> '
' </p> '
)
def test_stream_subscribe_button_after_name ( self ) :
2016-10-11 10:51:24 +02:00
# type: () -> None
2014-01-31 23:13:58 +01:00
msg = ' !_stream_subscribe_button(simple) (not) '
converted = bugdown_convert ( msg )
self . assertEqual (
converted ,
' <p> '
' <span class= " inline-subscribe " data-stream-name= " simple " > '
2014-03-05 22:28:57 +01:00
' <button class= " inline-subscribe-button btn " >Subscribe to simple</button> '
2014-01-31 23:13:58 +01:00
' <span class= " inline-subscribe-error " ></span> '
' </span> '
' (not)</p> '
)
def test_stream_subscribe_button_slash ( self ) :
2016-10-11 10:51:24 +02:00
# type: () -> None
2014-01-31 23:13:58 +01:00
msg = ' !_stream_subscribe_button(simple \\ \\ ) '
converted = bugdown_convert ( msg )
self . assertEqual (
converted ,
' <p> '
' <span class= " inline-subscribe " data-stream-name= " simple \\ " > '
2014-03-05 22:28:57 +01:00
' <button class= " inline-subscribe-button btn " >Subscribe to simple \\ </button> '
2014-01-31 23:13:58 +01:00
' <span class= " inline-subscribe-error " ></span> '
' </span> '
' </p> '
)
def test_in_app_modal_link ( self ) :
2016-10-11 10:51:24 +02:00
# type: () -> None
2014-01-31 23:13:58 +01:00
msg = ' !modal_link(#settings, Settings page) '
converted = bugdown_convert ( msg )
self . assertEqual (
converted ,
' <p> '
' <a data-toggle= " modal " href= " #settings " title= " #settings " >Settings page</a> '
' </p> '
)
2016-09-22 22:39:24 +02:00
def test_image_preview_title ( self ) :
2016-10-11 10:51:24 +02:00
# type: () -> None
2016-09-22 22:39:24 +02:00
msg = ' [My favorite image](https://example.com/testimage.png) '
converted = bugdown_convert ( msg )
self . assertEqual (
converted ,
' <p> '
' <a href= " https://example.com/testimage.png " target= " _blank " title= " https://example.com/testimage.png " >My favorite image</a> '
' </p> \n '
' <div class= " message_inline_image " > '
' <a href= " https://example.com/testimage.png " target= " _blank " title= " My favorite image " > '
' <img src= " https://example.com/testimage.png " > '
' </a> '
' </div> '
)
2014-01-31 23:13:58 +01:00
def test_mit_rendering ( self ) :
2016-10-11 10:51:24 +02:00
# type: () -> None
2016-09-09 22:53:36 +02:00
""" Test the markdown configs for the MIT Zephyr mirroring system;
verifies almost all inline patterns are disabled , but
inline_interesting_links is still enabled """
2014-01-31 23:13:58 +01:00
msg = " **test** "
2017-01-22 06:29:11 +01:00
realm = get_realm ( " mit " )
client = get_client ( " zephyr_mirror " )
message = Message ( sending_client = client ,
sender = get_user_profile_by_email ( " sipbtest@mit.edu " ) )
converted = bugdown . convert ( msg , message_realm = realm , message = message )
2014-01-31 23:13:58 +01:00
self . assertEqual (
converted ,
" <p>**test**</p> " ,
2017-01-24 06:34:26 +01:00
)
2014-02-04 23:37:21 +01:00
msg = " * test "
2017-01-22 06:29:11 +01:00
converted = bugdown . convert ( msg , message_realm = realm , message = message )
2014-02-04 23:37:21 +01:00
self . assertEqual (
converted ,
" <p>* test</p> " ,
2017-01-24 06:34:26 +01:00
)
2014-02-07 22:05:19 +01:00
msg = " https://lists.debian.org/debian-ctte/2014/02/msg00173.html "
2017-01-22 06:29:11 +01:00
converted = bugdown . convert ( msg , message_realm = realm , message = message )
2014-02-07 22:05:19 +01:00
self . assertEqual (
converted ,
' <p><a href= " https://lists.debian.org/debian-ctte/2014/02/msg00173.html " target= " _blank " title= " https://lists.debian.org/debian-ctte/2014/02/msg00173.html " >https://lists.debian.org/debian-ctte/2014/02/msg00173.html</a></p> ' ,
2017-01-24 06:34:26 +01:00
)
2014-01-31 23:13:58 +01:00
2016-09-19 20:44:15 +02:00
class BugdownApiTests ( ZulipTestCase ) :
def test_render_message_api ( self ) :
# type: () -> None
content = ' That is a **bold** statement '
2017-01-13 01:10:19 +01:00
result = self . client_post (
2016-09-19 20:44:15 +02:00
' /api/v1/messages/render ' ,
dict ( content = content ) ,
* * self . api_auth ( ' othello@zulip.com ' )
)
self . assert_json_success ( result )
data = ujson . loads ( result . content )
self . assertEqual ( data [ ' rendered ' ] ,
2016-12-03 00:04:17 +01:00
u ' <p>That is a <strong>bold</strong> statement</p> ' )
2016-09-19 20:44:15 +02:00
2016-11-08 23:09:53 +01:00
def test_render_mention_stream_api ( self ) :
2016-11-07 20:40:40 +01:00
# type: () -> None
2016-11-08 23:09:53 +01:00
""" Determines whether we ' re correctly passing the realm context """
2016-11-07 20:40:40 +01:00
content = ' This mentions #**Denmark** and @**King Hamlet**. '
2017-01-13 01:10:19 +01:00
result = self . client_post (
2016-11-07 20:40:40 +01:00
' /api/v1/messages/render ' ,
dict ( content = content ) ,
* * self . api_auth ( ' othello@zulip.com ' )
)
self . assert_json_success ( result )
data = ujson . loads ( result . content )
2017-01-20 18:27:30 +01:00
user_id = get_user_profile_by_email ( ' hamlet@zulip.com ' ) . id
2016-11-07 20:40:40 +01:00
self . assertEqual ( data [ ' rendered ' ] ,
2017-01-20 18:27:30 +01:00
u ' <p>This mentions <a class= " stream " data-stream-id= " %s " href= " /#narrow/stream/Denmark " >#Denmark</a> and <span class= " user-mention " data-user-id= " %s " >@King Hamlet</span>.</p> ' % ( get_stream ( " Denmark " , get_realm ( " zulip " ) ) . id , user_id ) )
2016-11-07 20:40:40 +01:00
2016-09-15 22:05:56 +02:00
class BugdownErrorTests ( ZulipTestCase ) :
def test_bugdown_error_handling ( self ) :
# type: () -> None
with self . simulated_markdown_failure ( ) :
2016-09-15 22:23:25 +02:00
with self . assertRaises ( bugdown . BugdownRenderingException ) :
2016-12-31 03:08:43 +01:00
bugdown_convert ( ' ' )
2016-09-15 22:05:56 +02:00
def test_send_message_errors ( self ) :
# type: () -> None
message = ' whatever '
with self . simulated_markdown_failure ( ) :
2016-12-16 02:11:42 +01:00
# We don't use assertRaisesRegex because it seems to not
2016-09-16 20:49:30 +02:00
# handle i18n properly here on some systems.
with self . assertRaises ( JsonableError ) :
2016-09-15 22:05:56 +02:00
self . send_message ( " othello@zulip.com " , " Denmark " , Recipient . STREAM , message )