2012-11-13 21:28:10 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
import re
|
|
|
|
|
import unittest
|
|
|
|
|
import markdown
|
2014-07-22 21:00:58 +00:00
|
|
|
from mock import patch
|
2012-11-13 21:28:10 +00:00
|
|
|
from mdx_oembed.extension import OEMBED_LINK_RE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class OEmbedPatternRegexTestCase(unittest.TestCase):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.re = re.compile(OEMBED_LINK_RE)
|
|
|
|
|
|
|
|
|
|
def test_ignore_relative_image_link(self):
|
|
|
|
|
text = ''
|
|
|
|
|
match = self.re.match(text)
|
|
|
|
|
self.assertIsNone(match)
|
|
|
|
|
|
|
|
|
|
def test_ignore_absolute_image_link(self):
|
|
|
|
|
text = ''
|
|
|
|
|
match = self.re.match(text)
|
|
|
|
|
self.assertIsNone(match)
|
|
|
|
|
|
|
|
|
|
def test_ignore_png_image_link(self):
|
|
|
|
|
text = ''
|
|
|
|
|
match = self.re.match(text)
|
|
|
|
|
self.assertIsNone(match)
|
|
|
|
|
|
|
|
|
|
def test_ignore_jpg_image_link(self):
|
|
|
|
|
text = ''
|
|
|
|
|
match = self.re.match(text)
|
|
|
|
|
self.assertIsNone(match)
|
|
|
|
|
|
|
|
|
|
def test_ignore_gif_image_link(self):
|
|
|
|
|
text = ''
|
|
|
|
|
match = self.re.match(text)
|
|
|
|
|
self.assertIsNone(match)
|
|
|
|
|
|
|
|
|
|
def test_find_youtube_link(self):
|
2014-07-22 22:12:30 +00:00
|
|
|
text = ''
|
2012-11-13 21:28:10 +00:00
|
|
|
match = self.re.match(text)
|
|
|
|
|
self.assertIsNotNone(match)
|
|
|
|
|
|
|
|
|
|
def test_find_youtube_short_link(self):
|
2014-07-22 22:12:30 +00:00
|
|
|
text = ''
|
|
|
|
|
match = self.re.match(text)
|
|
|
|
|
self.assertIsNotNone(match)
|
|
|
|
|
|
|
|
|
|
def test_find_youtube_http(self):
|
|
|
|
|
text = ''
|
|
|
|
|
match = self.re.match(text)
|
|
|
|
|
self.assertIsNotNone(match)
|
|
|
|
|
|
|
|
|
|
def test_find_youtube_https(self):
|
|
|
|
|
text = ''
|
|
|
|
|
match = self.re.match(text)
|
|
|
|
|
self.assertIsNotNone(match)
|
|
|
|
|
|
|
|
|
|
def test_find_youtube_auto(self):
|
|
|
|
|
text = ''
|
2012-11-13 21:28:10 +00:00
|
|
|
match = self.re.match(text)
|
|
|
|
|
self.assertIsNotNone(match)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class OEmbedExtensionTestCase(unittest.TestCase):
|
|
|
|
|
def setUp(self):
|
2012-11-13 23:28:04 +00:00
|
|
|
self.markdown = markdown.Markdown(extensions=['oembed'])
|
2012-11-13 21:28:10 +00:00
|
|
|
|
2014-07-22 21:00:58 +00:00
|
|
|
def assert_convert(self, text, expected):
|
|
|
|
|
with patch('oembed.OEmbedEndpoint') as MockOEmbedEndpoint:
|
|
|
|
|
MockOEmbedEndpoint.get.return_value = expected
|
|
|
|
|
output = self.markdown.convert(text)
|
|
|
|
|
self.assertEqual(output, expected)
|
|
|
|
|
|
2012-11-13 21:28:10 +00:00
|
|
|
|
|
|
|
|
class IgnoredTestCase(OEmbedExtensionTestCase):
|
|
|
|
|
"""
|
|
|
|
|
The OEmbedExtension should ignore these tags allowing markdown's image
|
|
|
|
|
processor to find and handle them.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def test_relative(self):
|
|
|
|
|
text = ''
|
|
|
|
|
expected = '<p><img alt="alt" src="image.png" /></p>'
|
|
|
|
|
output = self.markdown.convert(text)
|
|
|
|
|
self.assertEqual(output, expected)
|
|
|
|
|
|
|
|
|
|
def test_slash_relative(self):
|
|
|
|
|
text = ''
|
|
|
|
|
expected = '<p><img alt="alt" src="/image.png" /></p>'
|
|
|
|
|
output = self.markdown.convert(text)
|
|
|
|
|
self.assertEqual(output, expected)
|
|
|
|
|
|
|
|
|
|
def test_absolute(self):
|
|
|
|
|
text = ''
|
|
|
|
|
expected = '<p><img alt="Mumbo Jumbo" src="http://tannern.com/mumbo-jumbo.jpg" /></p>'
|
|
|
|
|
output = self.markdown.convert(text)
|
|
|
|
|
self.assertEqual(output, expected)
|
|
|
|
|
|
|
|
|
|
|
2014-07-22 22:12:30 +00:00
|
|
|
class ProtocolVarietyTestCase(OEmbedExtensionTestCase):
|
|
|
|
|
|
|
|
|
|
def test_http(self):
|
|
|
|
|
text = ''
|
|
|
|
|
output = self.markdown.convert(text)
|
|
|
|
|
self.assertIn('<iframe', output)
|
|
|
|
|
|
|
|
|
|
def test_https(self):
|
|
|
|
|
text = ''
|
|
|
|
|
output = self.markdown.convert(text)
|
|
|
|
|
self.assertIn('<iframe', output)
|
|
|
|
|
|
|
|
|
|
def test_auto(self):
|
|
|
|
|
text = ''
|
|
|
|
|
output = self.markdown.convert(text)
|
|
|
|
|
self.assertIn('<iframe', output)
|
|
|
|
|
|
|
|
|
|
|
2012-11-13 21:28:10 +00:00
|
|
|
class YoutubeTestCase(OEmbedExtensionTestCase):
|
|
|
|
|
"""
|
|
|
|
|
The OEmbedExtension should handle embedding for these cases.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def test_youtube_link(self):
|
|
|
|
|
"""
|
|
|
|
|
YouTube video link.
|
|
|
|
|
"""
|
2014-07-22 22:12:30 +00:00
|
|
|
text = ''
|
2012-11-13 21:28:10 +00:00
|
|
|
output = self.markdown.convert(text)
|
2014-07-22 22:12:30 +00:00
|
|
|
self.assertIn('<iframe', output)
|
2012-11-13 21:28:10 +00:00
|
|
|
|
|
|
|
|
def test_youtube_short_link(self):
|
|
|
|
|
"""
|
|
|
|
|
Short format YouTube video link.
|
|
|
|
|
"""
|
2014-07-22 22:12:30 +00:00
|
|
|
text = ''
|
2012-11-13 21:28:10 +00:00
|
|
|
output = self.markdown.convert(text)
|
2014-07-22 22:12:30 +00:00
|
|
|
self.assertIn('<iframe', output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class VimeoTestCase(OEmbedExtensionTestCase):
|
2012-11-13 21:28:10 +00:00
|
|
|
|
2013-10-21 22:37:56 +00:00
|
|
|
def test_vimeo_link(self):
|
2012-11-13 21:28:10 +00:00
|
|
|
"""
|
|
|
|
|
Vimeo video link.
|
|
|
|
|
"""
|
|
|
|
|
text = ''
|
|
|
|
|
output = self.markdown.convert(text)
|
2014-07-22 22:12:30 +00:00
|
|
|
self.assertIn('<iframe', output)
|
2012-11-13 21:28:10 +00:00
|
|
|
|
2012-11-13 23:28:04 +00:00
|
|
|
|
2014-07-22 22:12:30 +00:00
|
|
|
class LimitedOEmbedExtensionTestCase(OEmbedExtensionTestCase):
|
2012-11-13 23:28:04 +00:00
|
|
|
def setUp(self):
|
|
|
|
|
self.markdown = markdown.Markdown(
|
|
|
|
|
extensions=['oembed'],
|
|
|
|
|
extension_configs={
|
|
|
|
|
'oembed': {
|
|
|
|
|
'allowed_endpoints': ['youtube',],
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
def test_youtube_link(self):
|
|
|
|
|
"""
|
|
|
|
|
YouTube video link.
|
|
|
|
|
"""
|
2014-07-22 22:12:30 +00:00
|
|
|
text = ''
|
2012-11-13 23:28:04 +00:00
|
|
|
output = self.markdown.convert(text)
|
2014-07-22 22:12:30 +00:00
|
|
|
self.assertIn('<iframe', output)
|
2012-11-13 23:28:04 +00:00
|
|
|
|
2013-10-21 22:37:56 +00:00
|
|
|
def test_vimeo_link(self):
|
2012-11-13 23:28:04 +00:00
|
|
|
"""
|
|
|
|
|
Vimeo video link.
|
|
|
|
|
"""
|
|
|
|
|
text = ''
|
2014-07-22 22:12:30 +00:00
|
|
|
output = self.markdown.convert(text)
|
|
|
|
|
self.assertNotIn('<iframe', output)
|
2012-11-13 23:28:04 +00:00
|
|
|
|
2013-10-21 22:37:56 +00:00
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
unittest.main()
|