diff --git a/embed_video/templates/embed_video/embed_code.html b/embed_video/templates/embed_video/embed_code.html index 9349fd0..797e43b 100644 --- a/embed_video/templates/embed_video/embed_code.html +++ b/embed_video/templates/embed_video/embed_code.html @@ -1,2 +1 @@ - + diff --git a/embed_video/tests/backends/__init__.py b/embed_video/tests/backends/__init__.py new file mode 100644 index 0000000..74e349b --- /dev/null +++ b/embed_video/tests/backends/__init__.py @@ -0,0 +1,36 @@ +from unittest import TestCase + +from embed_video.backends import detect_backend, UnknownBackendException, \ + VideoBackend + + +class BackendTestMixin(object): + urls = [] + instance = None + + def test_detect(self): + for url in self.urls: + backend = detect_backend(url[0]) + self.assertIsInstance(backend, self.instance) + + def test_code(self): + for url in self.urls: + backend = self.instance(url[0]) + self.assertEqual(backend.code, url[1]) + + +class VideoBackendTestCase(TestCase): + unknown_backend_urls = ( + 'http://myurl.com/?video=http://www.youtube.com/watch?v=jsrRJyHBvzw', + 'http://myurl.com/?video=www.youtube.com/watch?v=jsrRJyHBvzw', + 'http://youtube.com.myurl.com/watch?v=jsrRJyHBvzw', + 'http://vimeo.com.myurl.com/72304002', + ) + + def test_detect_bad_urls(self): + for url in self.unknown_backend_urls: + self.assertRaises(UnknownBackendException, detect_backend, url) + + def test_not_implemented_get_info(self): + backend = VideoBackend('http://www.example.com') + self.assertRaises(NotImplementedError, backend.get_info) diff --git a/embed_video/tests/tests_custom_backend.py b/embed_video/tests/backends/tests_custom_backend.py similarity index 68% rename from embed_video/tests/tests_custom_backend.py rename to embed_video/tests/backends/tests_custom_backend.py index 3246a50..2ffa587 100644 --- a/embed_video/tests/tests_custom_backend.py +++ b/embed_video/tests/backends/tests_custom_backend.py @@ -1,8 +1,15 @@ +import re from unittest import TestCase -from embed_video.backends import detect_backend +from embed_video.backends import VideoBackend, detect_backend -from .custom_backend import CustomBackend + +class CustomBackend(VideoBackend): + re_detect = re.compile(r'http://myvideo\.com/[0-9]+') + re_code = re.compile(r'http://myvideo\.com/(?P[0-9]+)') + + pattern_url = '{protocol}://play.myvideo.com/c/{code}/' + pattern_thumbnail_url = '{protocol}://thumb.myvideo.com/c/{code}/' class CustomBackendTestCase(TestCase): @@ -27,4 +34,3 @@ class CustomBackendTestCase(TestCase): def test_thumbnail(self): self.assertEqual(self.backend.get_thumbnail_url(), 'http://thumb.myvideo.com/c/1530/') - diff --git a/embed_video/tests/backends/tests_soundcloud.py b/embed_video/tests/backends/tests_soundcloud.py new file mode 100644 index 0000000..24e4d9b --- /dev/null +++ b/embed_video/tests/backends/tests_soundcloud.py @@ -0,0 +1,55 @@ +import requests +from mock import patch +from unittest import TestCase + +from . import BackendTestMixin +from embed_video.backends import SoundCloudBackend, VideoDoesntExistException + + +class SoundCloudBackendTestCase(BackendTestMixin, TestCase): + urls = ( + ('https://soundcloud.com/community/soundcloud-case-study-wildlife', '82244706'), + ('https://soundcloud.com/matej-roman/jaromir-nohavica-karel-plihal-mikymauz', '7834701'), + ('https://soundcloud.com/beny97/sets/jaromir-nohavica-prazska', '960591'), + ('https://soundcloud.com/corbel-keep/norah-jones-come-away-with', '22485933'), + ) + + instance = SoundCloudBackend + + def setUp(self): + class FooBackend(SoundCloudBackend): + url = 'foobar' + + def get_info(self): + return { + 'width': 123, + 'height': 321, + 'thumbnail_url': 'xyz', + 'html': u'\u003Ciframe width=\"100%\" height=\"400\" ' + u'scrolling=\"no\" frameborder=\"no\" ' + u'src=\"{0}\"\u003E\u003C/iframe\u003E'.format(self.url) + } + + self.foo = FooBackend('abcd') + + def test_width(self): + self.assertEqual(self.foo.width, 123) + + def test_height(self): + self.assertEqual(self.foo.height, 321) + + def test_get_thumbnail_url(self): + self.assertEqual(self.foo.get_thumbnail_url(), 'xyz') + + def test_get_url(self): + self.assertEqual(self.foo.get_url(), self.foo.url) + + @patch('embed_video.backends.EMBED_VIDEO_TIMEOUT', 0.000001) + def test_timeout_in_get_info(self): + backend = SoundCloudBackend('https://soundcloud.com/community/soundcloud-case-study-wildlife') + self.assertRaises(requests.Timeout, backend.get_info) + + def test_invalid_url(self): + """ Check if bug #21 is fixed. """ + backend = SoundCloudBackend('https://soundcloud.com/xyz/foo') + self.assertRaises(VideoDoesntExistException, backend.get_info) diff --git a/embed_video/tests/backends/tests_vimeo.py b/embed_video/tests/backends/tests_vimeo.py new file mode 100644 index 0000000..ea3e0ca --- /dev/null +++ b/embed_video/tests/backends/tests_vimeo.py @@ -0,0 +1,34 @@ +import requests +from mock import patch +from unittest import TestCase + +from . import BackendTestMixin +from embed_video.backends import VimeoBackend, VideoDoesntExistException + + +class VimeoBackendTestCase(BackendTestMixin, TestCase): + urls = ( + ('http://vimeo.com/72304002', '72304002'), + ('https://vimeo.com/72304002', '72304002'), + ('http://www.vimeo.com/72304002', '72304002'), + ('https://www.vimeo.com/72304002', '72304002'), + ('http://player.vimeo.com/video/72304002', '72304002'), + ('https://player.vimeo.com/video/72304002', '72304002'), + ) + + instance = VimeoBackend + + def test_vimeo_get_info_exception(self): + with self.assertRaises(VideoDoesntExistException): + backend = VimeoBackend('http://vimeo.com/123') + backend.get_info() + + def test_get_thumbnail_url(self): + backend = VimeoBackend('http://vimeo.com/72304002') + self.assertEqual(backend.get_thumbnail_url(), + 'http://i.vimeocdn.com/video/446150690_640.jpg') + + @patch('embed_video.backends.EMBED_VIDEO_TIMEOUT', 0.000001) + def test_timeout_in_get_info(self): + backend = VimeoBackend('http://vimeo.com/72304002') + self.assertRaises(requests.Timeout, backend.get_info) diff --git a/embed_video/tests/backends/tests_youtube.py b/embed_video/tests/backends/tests_youtube.py new file mode 100644 index 0000000..c48732f --- /dev/null +++ b/embed_video/tests/backends/tests_youtube.py @@ -0,0 +1,42 @@ +from unittest import TestCase + +from . import BackendTestMixin +from embed_video.backends import YoutubeBackend, UnknownIdException + + +class YoutubeBackendTestCase(BackendTestMixin, TestCase): + urls = ( + ('http://youtu.be/jsrRJyHBvzw', 'jsrRJyHBvzw'), + ('http://youtu.be/n17B_uFF4cA', 'n17B_uFF4cA'), + ('http://youtu.be/t-ZRX8984sc', 't-ZRX8984sc'), + ('https://youtu.be/t-ZRX8984sc', 't-ZRX8984sc'), + ('http://youtube.com/watch?v=jsrRJyHBvzw', 'jsrRJyHBvzw'), + ('https://youtube.com/watch?v=jsrRJyHBvzw', 'jsrRJyHBvzw'), + ('http://www.youtube.com/v/0zM3nApSvMg?rel=0', '0zM3nApSvMg'), + ('https://www.youtube.com/v/0zM3nApSvMg?rel=0', '0zM3nApSvMg'), + ('http://www.youtube.com/embed/0zM3nApSvMg?rel=0', '0zM3nApSvMg'), + ('https://www.youtube.com/embed/0zM3nApSvMg?rel=0', '0zM3nApSvMg'), + ('http://www.youtube.com/watch?v=jsrRJyHBvzw', 'jsrRJyHBvzw'), + ('https://www.youtube.com/watch?v=t-ZRX8984sc', 't-ZRX8984sc'), + ('http://www.youtube.com/watch?v=iwGFalTRHDA&feature=related', 'iwGFalTRHDA'), + ('https://www.youtube.com/watch?v=iwGFalTRHDA&feature=related', 'iwGFalTRHDA'), + ('http://www.youtube.com/watch?feature=player_embedded&v=2NpZbaAIXag', '2NpZbaAIXag'), + ('https://www.youtube.com/watch?feature=player_embedded&v=2NpZbaAIXag', '2NpZbaAIXag'), + ('https://www.youtube.com/watch?v=XPk521voaOE&feature=youtube_gdata_player', 'XPk521voaOE'), + ('http://www.youtube.com/watch?v=6xu00J3-g2s&list=PLb5n6wzDlPakFKvJ69rJ9AJW24Aaaki2z', '6xu00J3-g2s'), + ('https://m.youtube.com/#/watch?v=IAooXLAPoBQ', 'IAooXLAPoBQ'), + ('https://m.youtube.com/watch?v=IAooXLAPoBQ', 'IAooXLAPoBQ'), + ('http://www.youtube.com/edit?video_id=eBea01qmnOE', 'eBea01qmnOE') + ) + + instance = YoutubeBackend + + def test_youtube_keyerror(self): + """ Test for issue #7 """ + backend = self.instance('http://youtube.com/watch?id=5') + self.assertRaises(UnknownIdException, backend.get_code) + + def test_thumbnail(self): + for url in self.urls: + backend = self.instance(url[0]) + self.assertIn(url[1], backend.thumbnail) diff --git a/embed_video/tests/custom_backend.py b/embed_video/tests/custom_backend.py deleted file mode 100644 index be14bb8..0000000 --- a/embed_video/tests/custom_backend.py +++ /dev/null @@ -1,11 +0,0 @@ -import re - -from embed_video.backends import VideoBackend - - -class CustomBackend(VideoBackend): - re_detect = re.compile(r'http://myvideo\.com/[0-9]+') - re_code = re.compile(r'http://myvideo\.com/(?P[0-9]+)') - - pattern_url = '{protocol}://play.myvideo.com/c/{code}/' - pattern_thumbnail_url = '{protocol}://thumb.myvideo.com/c/{code}/' diff --git a/embed_video/tests/django_settings.py b/embed_video/tests/django_settings.py index 00c05a8..e9b53cb 100644 --- a/embed_video/tests/django_settings.py +++ b/embed_video/tests/django_settings.py @@ -17,7 +17,7 @@ EMBED_VIDEO_BACKENDS = ( 'embed_video.backends.YoutubeBackend', 'embed_video.backends.VimeoBackend', 'embed_video.backends.SoundCloudBackend', - 'embed_video.tests.custom_backend.CustomBackend', + 'embed_video.tests.backends.tests_custom_backend.CustomBackend', ) diff --git a/embed_video/tests/templatetags/__init__.py b/embed_video/tests/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/embed_video/tests/templatetags/tests_embed_video_tags.py b/embed_video/tests/templatetags/tests_embed_video_tags.py new file mode 100644 index 0000000..2c50295 --- /dev/null +++ b/embed_video/tests/templatetags/tests_embed_video_tags.py @@ -0,0 +1,254 @@ +from unittest import TestCase +from mock import Mock, patch +import re + +try: + # Python <= 2.7 + import urlparse +except ImportError: + # Python 3 + import urllib.parse as urlparse + +from django.template import TemplateSyntaxError +from django.http import HttpRequest +from django.template.base import Template +from django.template.context import RequestContext +from django.test.utils import override_settings +from django.test.client import RequestFactory +from testfixtures import LogCapture, log_capture + +from embed_video.templatetags.embed_video_tags import VideoNode + +URL_PATTERN = re.compile(r'src="?\'?([^"\'>]*)"') + + +class EmbedTestCase(TestCase): + def assertRenderedTemplate(self, template_string, output, context=None): + response = RequestContext(HttpRequest(), context) + rendered_output = Template(template_string).render(response) + self.assertEqual(rendered_output.strip(), output.strip()) + + def test_embed(self): + template = """ + {% load embed_video_tags %} + {% video 'http://www.youtube.com/watch?v=jsrRJyHBvzw' as ytb %} + {% video ytb 'large' %} + {% endvideo %} + """ + self.assertRenderedTemplate( + template, + '' + ) + + def test_embed_invalid_url(self): + template = """ + {% load embed_video_tags %} + {% video 'http://www.youtube.com/edit?abcd=efgh' as ytb %} + {{ ytb.url }} + {% endvideo %} + """ + self.assertRenderedTemplate(template, '') + + def test_embed_with_none_instance(self): + template = """ + {% with None as my_video %} + {% load embed_video_tags %} + {% video my_video %}{% endwith %} + """ + self.assertRenderedTemplate(template, '') + + def test_embed_empty_string(self): + template = """ + {% load embed_video_tags %} + {% video '' 'large' %} + """ + self.assertRenderedTemplate(template, '') + + def test_direct_embed(self): + template = """ + {% load embed_video_tags %} + {{ 'http://www.youtube.com/watch?v=jsrRJyHBvzw'|embed:'large' }} + """ + self.assertRenderedTemplate( + template, + '''' + ) + + def test_direct_embed_tag(self): + template = """ + {% load embed_video_tags %} + {% video "http://www.youtube.com/watch?v=jsrRJyHBvzw" "large" %} + """ + self.assertRenderedTemplate( + template, + '' + ) + + def test_direct_embed_tag_with_default_size(self): + template = """ + {% load embed_video_tags %} + {% video "http://www.youtube.com/watch?v=jsrRJyHBvzw" %} + """ + self.assertRenderedTemplate( + template, + '' + ) + + def test_direct_embed_invalid_url(self): + template = """ + {% load embed_video_tags %} + {% video "https://soundcloud.com/xyz/foo" %} + """ + self.assertRenderedTemplate(template, '') + + def test_user_size(self): + template = """ + {% load embed_video_tags %} + {% video 'http://www.youtube.com/watch?v=jsrRJyHBvzw' as ytb %} + {% video ytb '800x800' %} + {% endvideo %} + """ + self.assertRenderedTemplate( + template, + '' + ) + + def test_wrong_size(self): + template = Template(""" + {% load embed_video_tags %} + {% video 'http://www.youtube.com/watch?v=jsrRJyHBvzw' 'so x huge' %} + """) + request = RequestContext(HttpRequest()) + self.assertRaises(TemplateSyntaxError, template.render, request) + + def test_tag_youtube(self): + template = """ + {% load embed_video_tags %} + {% video 'http://www.youtube.com/watch?v=jsrRJyHBvzw' as ytb %} + {{ ytb.url }} {{ ytb.backend }} + {% endvideo %} + """ + self.assertRenderedTemplate( + template, + 'http://www.youtube.com/embed/jsrRJyHBvzw?wmode=opaque ' + 'YoutubeBackend' + ) + + def test_tag_vimeo(self): + template = """ + {% load embed_video_tags %} + {% video 'https://vimeo.com/72304002' as vimeo %} + {{ vimeo.url }} {{ vimeo.backend }} + {% endvideo %} + """ + self.assertRenderedTemplate( + template, 'http://player.vimeo.com/video/72304002 VimeoBackend' + ) + + def test_tag_soundcloud(self): + template = """ + {% load embed_video_tags %} + {% video 'https://soundcloud.com/community/soundcloud-case-study-wildlife' as soundcloud %} + {{ soundcloud.url }} {{ soundcloud.backend }} + {% endvideo %} + """ + self.assertRenderedTemplate( + template, + 'https://w.soundcloud.com/player/?visual=true&url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F82244706&show_artwork=true ' + 'SoundCloudBackend' + ) + + @patch('embed_video.backends.EMBED_VIDEO_TIMEOUT', 0.000001) + @log_capture() + def test_empty_if_timeout(self, logs): + template = """ + {% load embed_video_tags %} + {% video "http://vimeo.com/72304002" as my_video %} + {{ my_video.thumbnail }} + {% endvideo %} + """ + + self.assertRenderedTemplate(template, '') + logs.check( + ('requests.packages.urllib3.connectionpool', 'INFO', 'Starting new HTTP connection (1): vimeo.com'), + ('embed_video.templatetags.embed_video_tags', 'ERROR', 'Timeout reached during rendering embed video (`http://vimeo.com/72304002`)') + ) + + def test_relative_size(self): + template = """ + {% load embed_video_tags %} + {% video "http://vimeo.com/72304002" "80%x30%" %} + """ + self.assertRenderedTemplate( + template, + '' + ) + + def test_allow_spaces_in_size(self): + template = """ + {% load embed_video_tags %} + {% video "http://vimeo.com/72304002" "80% x 300" %} + """ + self.assertRenderedTemplate( + template, + '' + ) + + + +class EmbedVideoNodeTestCase(TestCase): + def setUp(self): + self.parser = Mock() + self.token = Mock(methods=['split_contents']) + + def test_repr(self): + self.token.split_contents.return_value = ( + 'video', 'http://youtu.be/v/1234', 'as', 'myvideo' + ) + self.parser.compile_filter.return_value = u'some_url' + + node = VideoNode(self.parser, self.token) + self.assertEqual(str(node), '') + + def test_videonode_iter(self): + out = ['a', 'b', 'c', 'd'] + + class FooNode(VideoNode): + nodelist_file = out + + def __init__(self): + pass + + node = FooNode() + self.assertEqual(out, [x for x in node]) + + def test_get_backend_secure(self): + class SecureRequest(RequestFactory): + is_secure = lambda x: True + + context = {'request': SecureRequest()} + backend = VideoNode.get_backend('http://www.youtube.com/watch?v=jsrRJyHBvzw', context) + self.assertTrue(backend.is_secure) + + def test_get_backend_insecure(self): + class InsecureRequest(RequestFactory): + is_secure = lambda x: False + + context = {'request': InsecureRequest()} + backend = VideoNode.get_backend('http://www.youtube.com/watch?v=jsrRJyHBvzw', context) + self.assertFalse(backend.is_secure) + diff --git a/embed_video/tests/tests_backend.py b/embed_video/tests/tests_backend.py deleted file mode 100644 index e0f8c54..0000000 --- a/embed_video/tests/tests_backend.py +++ /dev/null @@ -1,157 +0,0 @@ -from unittest import TestCase -from mock import patch -import requests - -from ..backends import detect_backend, YoutubeBackend, VimeoBackend, \ - SoundCloudBackend, UnknownBackendException, VideoDoesntExistException, \ - UnknownIdException, VideoBackend - - -class BackendTestMixin(object): - def test_detect(self): - for url in self.urls: - backend = detect_backend(url[0]) - self.assertIsInstance(backend, self.instance) - - def test_code(self): - for url in self.urls: - backend = self.instance(url[0]) - self.assertEqual(backend.code, url[1]) - - -class VideoBackendTestCase(TestCase): - unknown_backend_urls = ( - 'http://myurl.com/?video=http://www.youtube.com/watch?v=jsrRJyHBvzw', - 'http://myurl.com/?video=www.youtube.com/watch?v=jsrRJyHBvzw', - 'http://youtube.com.myurl.com/watch?v=jsrRJyHBvzw', - 'http://vimeo.com.myurl.com/72304002', - ) - - def test_detect_bad_urls(self): - for url in self.unknown_backend_urls: - self.assertRaises(UnknownBackendException, detect_backend, url) - - def test_not_implemented_get_info(self): - backend = VideoBackend('http://www.example.com') - self.assertRaises(NotImplementedError, backend.get_info) - - -class YoutubeBackendTestCase(BackendTestMixin, TestCase): - urls = ( - ('http://youtu.be/jsrRJyHBvzw', 'jsrRJyHBvzw'), - ('http://youtu.be/n17B_uFF4cA', 'n17B_uFF4cA'), - ('http://youtu.be/t-ZRX8984sc', 't-ZRX8984sc'), - ('https://youtu.be/t-ZRX8984sc', 't-ZRX8984sc'), - ('http://youtube.com/watch?v=jsrRJyHBvzw', 'jsrRJyHBvzw'), - ('https://youtube.com/watch?v=jsrRJyHBvzw', 'jsrRJyHBvzw'), - ('http://www.youtube.com/v/0zM3nApSvMg?rel=0', '0zM3nApSvMg'), - ('https://www.youtube.com/v/0zM3nApSvMg?rel=0', '0zM3nApSvMg'), - ('http://www.youtube.com/embed/0zM3nApSvMg?rel=0', '0zM3nApSvMg'), - ('https://www.youtube.com/embed/0zM3nApSvMg?rel=0', '0zM3nApSvMg'), - ('http://www.youtube.com/watch?v=jsrRJyHBvzw', 'jsrRJyHBvzw'), - ('https://www.youtube.com/watch?v=t-ZRX8984sc', 't-ZRX8984sc'), - ('http://www.youtube.com/watch?v=iwGFalTRHDA&feature=related', 'iwGFalTRHDA'), - ('https://www.youtube.com/watch?v=iwGFalTRHDA&feature=related', 'iwGFalTRHDA'), - ('http://www.youtube.com/watch?feature=player_embedded&v=2NpZbaAIXag', '2NpZbaAIXag'), - ('https://www.youtube.com/watch?feature=player_embedded&v=2NpZbaAIXag', '2NpZbaAIXag'), - ('https://www.youtube.com/watch?v=XPk521voaOE&feature=youtube_gdata_player', 'XPk521voaOE'), - ('http://www.youtube.com/watch?v=6xu00J3-g2s&list=PLb5n6wzDlPakFKvJ69rJ9AJW24Aaaki2z', '6xu00J3-g2s'), - ('https://m.youtube.com/#/watch?v=IAooXLAPoBQ', 'IAooXLAPoBQ'), - ('https://m.youtube.com/watch?v=IAooXLAPoBQ', 'IAooXLAPoBQ'), - ('http://www.youtube.com/edit?video_id=eBea01qmnOE', 'eBea01qmnOE') - ) - - instance = YoutubeBackend - - def test_youtube_keyerror(self): - """ Test for issue #7 """ - backend = self.instance('http://youtube.com/watch?id=5') - self.assertRaises(UnknownIdException, backend.get_code) - - def test_thumbnail(self): - for url in self.urls: - backend = self.instance(url[0]) - self.assertIn(url[1], backend.thumbnail) - - -class VimeoBackendTestCase(BackendTestMixin, TestCase): - urls = ( - ('http://vimeo.com/72304002', '72304002'), - ('https://vimeo.com/72304002', '72304002'), - ('http://www.vimeo.com/72304002', '72304002'), - ('https://www.vimeo.com/72304002', '72304002'), - ('http://player.vimeo.com/video/72304002', '72304002'), - ('https://player.vimeo.com/video/72304002', '72304002'), - ) - - instance = VimeoBackend - - def test_vimeo_get_info_exception(self): - with self.assertRaises(VideoDoesntExistException): - backend = VimeoBackend('http://vimeo.com/123') - backend.get_info() - - def test_get_thumbnail_url(self): - backend = VimeoBackend('http://vimeo.com/72304002') - self.assertEqual(backend.get_thumbnail_url(), - 'http://i.vimeocdn.com/video/446150690_640.jpg') - - @patch('embed_video.backends.EMBED_VIDEO_TIMEOUT', 0.000001) - def test_timeout_in_get_info(self): - backend = VimeoBackend('http://vimeo.com/72304002') - self.assertRaises(requests.Timeout, backend.get_info) - - -class SoundCloudBackendTestCase(BackendTestMixin, TestCase): - urls = ( - ('https://soundcloud.com/community/soundcloud-case-study-wildlife', '82244706'), - ('https://soundcloud.com/matej-roman/jaromir-nohavica-karel-plihal-mikymauz', '7834701'), - ('https://soundcloud.com/beny97/sets/jaromir-nohavica-prazska', '960591'), - ('https://soundcloud.com/corbel-keep/norah-jones-come-away-with', '22485933'), - ) - - instance = SoundCloudBackend - - def setUp(self): - class FooBackend(SoundCloudBackend): - url = 'foobar' - - def get_info(self): - return { - 'width': 123, - 'height': 321, - 'thumbnail_url': 'xyz', - 'html': u'\u003Ciframe width=\"100%\" height=\"400\" ' - u'scrolling=\"no\" frameborder=\"no\" ' - u'src=\"{0}\"\u003E\u003C/iframe\u003E'.format(self.url) - } - - self.foo = FooBackend('abcd') - - def test_width(self): - self.assertEqual(self.foo.width, 123) - - def test_height(self): - self.assertEqual(self.foo.height, 321) - - def test_get_thumbnail_url(self): - self.assertEqual(self.foo.get_thumbnail_url(), 'xyz') - - def test_get_url(self): - self.assertEqual(self.foo.get_url(), self.foo.url) - - def test_get_embed_code(self): - self.assertEqual(self.foo.get_embed_code(100, 200), - u'\n') - - @patch('embed_video.backends.EMBED_VIDEO_TIMEOUT', 0.000001) - def test_timeout_in_get_info(self): - backend = SoundCloudBackend('https://soundcloud.com/community/soundcloud-case-study-wildlife') - self.assertRaises(requests.Timeout, backend.get_info) - - def test_invalid_url(self): - """ Check if bug #21 is fixed. """ - backend = SoundCloudBackend('https://soundcloud.com/xyz/foo') - self.assertRaises(VideoDoesntExistException, backend.get_info) diff --git a/embed_video/tests/tests.py b/embed_video/tests/tests_init.py similarity index 100% rename from embed_video/tests/tests.py rename to embed_video/tests/tests_init.py diff --git a/embed_video/tests/tests_tags.py b/embed_video/tests/tests_tags.py deleted file mode 100644 index 64f260f..0000000 --- a/embed_video/tests/tests_tags.py +++ /dev/null @@ -1,341 +0,0 @@ -from unittest import TestCase -from mock import Mock, patch -import re - -try: - # Python <= 2.7 - import urlparse -except ImportError: - # Python 3 - import urllib.parse as urlparse - -from django.template import TemplateSyntaxError -from django.http import HttpRequest -from django.template.base import Template -from django.template.context import RequestContext -from django.test.utils import override_settings -from django.test.client import RequestFactory -from testfixtures import LogCapture - -from embed_video.templatetags.embed_video_tags import VideoNode - -URL_PATTERN = re.compile(r'src="?\'?([^"\'>]*)"') - - -class EmbedVideoNodeTestCase(TestCase): - def setUp(self): - self.parser = Mock() - self.token = Mock(methods=['split_contents']) - - @staticmethod - def _grc(context=None): - return RequestContext(HttpRequest(), context) - - def test_embed(self): - template = Template(""" - {% load embed_video_tags %} - {% video 'http://www.youtube.com/watch?v=jsrRJyHBvzw' as ytb %} - {% video ytb 'large' %} - {% endvideo %} - """) - rendered = u'''''' - self.assertEqual(template.render(self._grc()).strip(), rendered) - - def test_direct_embed(self): - template = Template(""" - {% load embed_video_tags %} - {{ 'http://www.youtube.com/watch?v=jsrRJyHBvzw'|embed:'large' }} - """) - rendered = u'''''' - self.assertEqual(template.render(self._grc()).strip(), rendered) - - def test_direct_embed_tag(self): - template = Template(""" - {% load embed_video_tags %} - {% video "http://www.youtube.com/watch?v=jsrRJyHBvzw" "large" %} - """) - rendered = u'''''' - self.assertEqual(template.render(self._grc()).strip(), rendered) - - def test_direct_embed_tag_with_default_size(self): - template = Template(""" - {% load embed_video_tags %} - {% video "http://www.youtube.com/watch?v=jsrRJyHBvzw" %} - """) - rendered = u'''''' - self.assertEqual(template.render(self._grc()).strip(), rendered) - - def test_user_size(self): - template = Template(""" - {% load embed_video_tags %} - {% video 'http://www.youtube.com/watch?v=jsrRJyHBvzw' as ytb %} - {% video ytb '800x800' %} - {% endvideo %} - """) - rendered = u'''''' - self.assertEqual(template.render(self._grc()).strip(), rendered) - - def test_wrong_size(self): - template = Template(""" - {% load embed_video_tags %} - {% video 'http://www.youtube.com/watch?v=jsrRJyHBvzw' 'so x huge' %} - """) - self.assertRaises(TemplateSyntaxError, template.render, self._grc()) - - def test_tag_youtube(self): - template = Template(""" - {% load embed_video_tags %} - {% video 'http://www.youtube.com/watch?v=jsrRJyHBvzw' as ytb %} - {{ ytb.url }} - {% endvideo %} - """) - rendered = 'http://www.youtube.com/embed/jsrRJyHBvzw?wmode=opaque' - self.assertEqual(template.render(self._grc()).strip(), rendered) - - def test_tag_youtube_invalid_url(self): - template = Template(""" - {% load embed_video_tags %} - {% video 'http://www.youtube.com/edit?abcd=efgh' as ytb %} - {{ ytb.url }} - {% endvideo %} - """) - self.assertEqual(template.render(self._grc()).strip(), '') - - def test_tag_vimeo(self): - template = Template(""" - {% load embed_video_tags %} - {% video 'https://vimeo.com/72304002' as vimeo %} - {{ vimeo.url }} - {% endvideo %} - """) - rendered = 'http://player.vimeo.com/video/72304002' - self.assertEqual(template.render(self._grc()).strip(), rendered) - - def test_tag_backend_variable_vimeo(self): - template = Template(""" - {% load embed_video_tags %} - {% video 'https://vimeo.com/72304002' as vimeo %} - {{ vimeo.backend }} - {% endvideo %} - """) - rendered = 'VimeoBackend' - self.assertEqual(template.render(self._grc()).strip(), rendered) - - def test_tag_backend_variable_youtube(self): - template = Template(""" - {% load embed_video_tags %} - {% video 'http://www.youtube.com/watch?v=jsrRJyHBvz' as youtube %} - {{ youtube.backend }} - {% endvideo %} - """) - rendered = 'YoutubeBackend' - self.assertEqual(template.render(self._grc()).strip(), rendered) - - def test_tag_backend_variable_soundcloud(self): - template = Template(""" - {% load embed_video_tags %} - {% video 'https://soundcloud.com/community/soundcloud-case-study-wildlife' as soundcloud %} - {{ soundcloud.backend }} - {% endvideo %} - """) - rendered = 'SoundCloudBackend' - self.assertEqual(template.render(self._grc()).strip(), rendered) - - def test_syntax_error(self): - self.token.split_contents.return_value = [] - self.assertRaises(TemplateSyntaxError, VideoNode, self.parser, self.token) - - def test_repr(self): - self.token.split_contents.return_value = ( - 'video', 'http://youtu.be/v/1234', 'as', 'myvideo' - ) - self.parser.compile_filter.return_value = u'some_url' - - node = VideoNode(self.parser, self.token) - self.assertEqual(str(node), '') - - def test_videonode_iter(self): - out = ['a', 'b', 'c', 'd'] - - class FooNode(VideoNode): - nodelist_file = out - - def __init__(self): - pass - - node = FooNode() - self.assertEqual(out, [x for x in node]) - - def test_get_backend_secure(self): - class SecureRequest(RequestFactory): - is_secure = lambda x: True - - context = {'request': SecureRequest()} - backend = VideoNode.get_backend('http://www.youtube.com/watch?v=jsrRJyHBvzw', context) - self.assertTrue(backend.is_secure) - - def test_get_backend_insecure(self): - class InsecureRequest(RequestFactory): - is_secure = lambda x: False - - context = {'request': InsecureRequest()} - backend = VideoNode.get_backend('http://www.youtube.com/watch?v=jsrRJyHBvzw', context) - self.assertFalse(backend.is_secure) - - def test_no_video_provided(self): - template = Template(""" - {% load embed_video_tags %} - {% video '' 'large' %} - """) - self.assertEqual(template.render(self._grc()).strip(), '') - - @patch('embed_video.backends.EMBED_VIDEO_TIMEOUT', 0.000001) - def test_empty_if_timeout(self): - template = Template(""" - {% load embed_video_tags %} - {% video "http://vimeo.com/72304002" as my_video %} - {{ my_video.thumbnail }} - {% endvideo %} - """) - with LogCapture() as logs: - self.assertEqual(template.render(self._grc()).strip(), '') - log = logs.records[-1] - self.assertEqual(log.name, 'embed_video.templatetags.embed_video_tags') - self.assertEqual(log.msg, 'Timeout reached during rendering embed video (`http://vimeo.com/72304002`)') - - def test_relative_size(self): - template = Template(""" - {% load embed_video_tags %} - {% video "http://vimeo.com/72304002" "80%x30%" %} - """) - rendered = '' - self.assertEqual(template.render(self._grc()).strip(), rendered) - - def test_allow_spaces_in_size(self): - template = Template(""" - {% load embed_video_tags %} - {% video "http://vimeo.com/72304002" "80% x 300" %} - """) - rendered = '' - self.assertEqual(template.render(self._grc()).strip(), rendered) - - def test_soundcloud_invalid_url(self): - template = Template(""" - {% load embed_video_tags %} - {% video "https://soundcloud.com/xyz/foo" %} - """) - self.assertEqual(template.render(self._grc()).strip(), '') - - ########################################################################## - # Tests for adding GET query via KEY=VALUE pairs - ########################################################################## - - def _validate_GET_query(self, rendered, expected): - # Use this functioon to test the KEY=VALUE optional arguments to the - # templatetag because there's no guarantee that they will be appended - # to the URL query string in a particular order. By default the - # YouTube backend adds wmode=opaque to the query string, so be sure to - # include that in the expected querystring dict. - url = URL_PATTERN.search(rendered).group(1) - result = urlparse.urlparse(url) - qs = urlparse.parse_qs(result[4]) - self.assertEqual(qs, expected) - - @override_settings(EMBED_VIDEO_YOUTUBE_QUERY={'rel': 0, 'stop': 5}) - def test_embed_with_query_settings_override(self): - # Test KEY=VALUE argument with default values provided by settings - template = Template(""" - {% load embed_video_tags %} - {% video 'http://www.youtube.com/watch?v=jsrRJyHBvzw' %} - """) - expected = { - 'rel': ['0'], - 'stop': ['5'], - } - rendered = template.render(self._grc()) - self._validate_GET_query(rendered, expected) - - def test_embed_with_query_var(self): - # Test KEY=VALUE argument with the value resolving to a context - # variable. - template = Template(""" - {% load embed_video_tags %} - {% video 'http://www.youtube.com/watch?v=jsrRJyHBvzw' rel=show_related loop=5 %} - """) - expected = { - 'rel': ['0'], - 'loop': ['5'], - 'wmode': ['opaque'] - } - context = {'show_related': 0} - rendered = template.render(self._grc(context)) - self._validate_GET_query(rendered, expected) - - def test_embed_with_query_multiple(self): - # Test multiple KEY=VALUE arguments - template = Template(""" - {% load embed_video_tags %} - {% video 'http://www.youtube.com/watch?v=jsrRJyHBvzw' rel=0 loop=1 end=5 %} - """) - expected = { - 'rel': ['0'], - 'loop': ['1'], - 'end': ['5'], - 'wmode': ['opaque'] - } - self._validate_GET_query(template.render(self._grc()), expected) - - def test_embed_with_query_multiple_list(self): - # Test multiple KEY=VALUE arguments where the key is repeated multiple - # times (this is valid in a URL query). - template = Template(""" - {% load embed_video_tags %} - {% video 'http://www.youtube.com/watch?v=jsrRJyHBvzw' rel=0 loop=1 end=5 end=6 %} - """) - expected = { - 'rel': ['0'], - 'loop': ['1'], - 'end': ['5', '6'], - 'wmode': ['opaque'] - } - self._validate_GET_query(template.render(self._grc()), expected) - - def test_tag_youtube_with_query(self): - # Test KEY=VALUE arguments when used as a tag block. - template = Template(""" - {% load embed_video_tags %} - {% video 'http://www.youtube.com/watch?v=jsrRJyHBvzw' rel=0 as ytb %} - {{ ytb.url }} - {% endvideo %} - """) - rendered = 'http://www.youtube.com/embed/jsrRJyHBvzw?wmode=opaque&rel=0' - self.assertEqual(template.render(self._grc()).strip(), rendered) - - def test_embed_with_query_rel(self): - template = Template(""" - {% load embed_video_tags %} - {% video 'http://www.youtube.com/watch?v=jsrRJyHBvzw' rel=0 %} - """) - rendered = u'''''' - self.assertEqual(template.render(self._grc()).strip(), rendered) - - def test_none_variable_passed_to_tag(self): - """ - Checks issue #24. - """ - template = Template(""" - {% with None as my_video %} - {% load embed_video_tags %} - {% video my_video %} - {% endwith %} - """) - self.assertEqual(template.render(self._grc()).strip(), '') -