mirror of
https://github.com/Hopiu/django-embed-video.git
synced 2026-03-16 21:30:23 +00:00
Clean up tests
This commit is contained in:
parent
1b208b9c4f
commit
0055677b84
13 changed files with 432 additions and 515 deletions
|
|
@ -1,2 +1 @@
|
|||
<iframe width="{{ width }}" height="{{ height }}" src="{{ backend.url }}"
|
||||
frameborder="0" allowfullscreen></iframe>
|
||||
<iframe width="{{ width }}" height="{{ height }}" src="{{ backend.url }}" frameborder="0" allowfullscreen></iframe>
|
||||
|
|
|
|||
36
embed_video/tests/backends/__init__.py
Normal file
36
embed_video/tests/backends/__init__.py
Normal file
|
|
@ -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)
|
||||
|
|
@ -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<code>[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/')
|
||||
|
||||
55
embed_video/tests/backends/tests_soundcloud.py
Normal file
55
embed_video/tests/backends/tests_soundcloud.py
Normal file
|
|
@ -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)
|
||||
34
embed_video/tests/backends/tests_vimeo.py
Normal file
34
embed_video/tests/backends/tests_vimeo.py
Normal file
|
|
@ -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)
|
||||
42
embed_video/tests/backends/tests_youtube.py
Normal file
42
embed_video/tests/backends/tests_youtube.py
Normal file
|
|
@ -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)
|
||||
|
|
@ -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<code>[0-9]+)')
|
||||
|
||||
pattern_url = '{protocol}://play.myvideo.com/c/{code}/'
|
||||
pattern_thumbnail_url = '{protocol}://thumb.myvideo.com/c/{code}/'
|
||||
|
|
@ -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',
|
||||
)
|
||||
|
||||
|
||||
|
|
|
|||
0
embed_video/tests/templatetags/__init__.py
Normal file
0
embed_video/tests/templatetags/__init__.py
Normal file
254
embed_video/tests/templatetags/tests_embed_video_tags.py
Normal file
254
embed_video/tests/templatetags/tests_embed_video_tags.py
Normal file
|
|
@ -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,
|
||||
'<iframe width="960" height="720" '
|
||||
'src="http://www.youtube.com/embed/jsrRJyHBvzw?wmode=opaque" '
|
||||
'frameborder="0" allowfullscreen></iframe>'
|
||||
)
|
||||
|
||||
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,
|
||||
'<iframe width="960" height="720" '
|
||||
'src="http://www.youtube.com/embed/jsrRJyHBvzw?wmode=opaque" '
|
||||
'frameborder="0" allowfullscreen></iframe>'''
|
||||
)
|
||||
|
||||
def test_direct_embed_tag(self):
|
||||
template = """
|
||||
{% load embed_video_tags %}
|
||||
{% video "http://www.youtube.com/watch?v=jsrRJyHBvzw" "large" %}
|
||||
"""
|
||||
self.assertRenderedTemplate(
|
||||
template,
|
||||
'<iframe width="960" height="720" '
|
||||
'src="http://www.youtube.com/embed/jsrRJyHBvzw?wmode=opaque" '
|
||||
'frameborder="0" allowfullscreen></iframe>'
|
||||
)
|
||||
|
||||
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,
|
||||
'<iframe width="480" height="360" '
|
||||
'src="http://www.youtube.com/embed/jsrRJyHBvzw?wmode=opaque" '
|
||||
'frameborder="0" allowfullscreen></iframe>'
|
||||
)
|
||||
|
||||
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,
|
||||
'<iframe width="800" height="800" '
|
||||
'src="http://www.youtube.com/embed/jsrRJyHBvzw?wmode=opaque" '
|
||||
'frameborder="0" allowfullscreen></iframe>'
|
||||
)
|
||||
|
||||
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,
|
||||
'<iframe width="80%" height="30%" '
|
||||
'src="http://player.vimeo.com/video/72304002" '
|
||||
'frameborder="0" allowfullscreen></iframe>'
|
||||
)
|
||||
|
||||
def test_allow_spaces_in_size(self):
|
||||
template = """
|
||||
{% load embed_video_tags %}
|
||||
{% video "http://vimeo.com/72304002" "80% x 300" %}
|
||||
"""
|
||||
self.assertRenderedTemplate(
|
||||
template,
|
||||
'<iframe width="80%" height="300" '
|
||||
'src="http://player.vimeo.com/video/72304002" '
|
||||
'frameborder="0" allowfullscreen></iframe>'
|
||||
)
|
||||
|
||||
|
||||
|
||||
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), '<VideoNode "some_url">')
|
||||
|
||||
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)
|
||||
|
||||
|
|
@ -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'<iframe width="100" height="321" src="foobar"'
|
||||
u'\n frameborder="0" allowfullscreen>'
|
||||
u'</iframe>\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)
|
||||
|
|
@ -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'''<iframe width="960" height="720" src="http://www.youtube.com/embed/jsrRJyHBvzw?wmode=opaque"
|
||||
frameborder="0" allowfullscreen></iframe>'''
|
||||
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'''<iframe width="960" height="720" src="http://www.youtube.com/embed/jsrRJyHBvzw?wmode=opaque"
|
||||
frameborder="0" allowfullscreen></iframe>'''
|
||||
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'''<iframe width="960" height="720" src="http://www.youtube.com/embed/jsrRJyHBvzw?wmode=opaque"
|
||||
frameborder="0" allowfullscreen></iframe>'''
|
||||
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'''<iframe width="480" height="360" src="http://www.youtube.com/embed/jsrRJyHBvzw?wmode=opaque"
|
||||
frameborder="0" allowfullscreen></iframe>'''
|
||||
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'''<iframe width="800" height="800" src="http://www.youtube.com/embed/jsrRJyHBvzw?wmode=opaque"
|
||||
frameborder="0" allowfullscreen></iframe>'''
|
||||
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), '<VideoNode "some_url">')
|
||||
|
||||
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 = '<iframe width="80%" height="30%" src="http://player.vimeo.com/video/72304002"' \
|
||||
'\n frameborder="0" allowfullscreen></iframe>'
|
||||
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 = '<iframe width="80%" height="300" src="http://player.vimeo.com/video/72304002"' \
|
||||
'\n frameborder="0" allowfullscreen></iframe>'
|
||||
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'''<iframe width="480" height="360" src="http://www.youtube.com/embed/jsrRJyHBvzw?wmode=opaque&rel=0"
|
||||
frameborder="0" allowfullscreen></iframe>'''
|
||||
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(), '')
|
||||
|
||||
Loading…
Reference in a new issue