mirror of
https://github.com/Hopiu/wagtail.git
synced 2026-05-27 16:14:01 +00:00
Tidied up embed filter tests
Less complicated and a bit more isolated now
This commit is contained in:
parent
b0df37d851
commit
9b376e4588
2 changed files with 12 additions and 32 deletions
|
|
@ -1,7 +1,7 @@
|
|||
from django import template
|
||||
from django.utils.safestring import mark_safe
|
||||
|
||||
from wagtail.wagtailembeds.embeds import get_embed
|
||||
from wagtail.wagtailembeds import embeds
|
||||
|
||||
|
||||
register = template.Library()
|
||||
|
|
@ -9,7 +9,7 @@ register = template.Library()
|
|||
|
||||
@register.filter
|
||||
def embed(url, max_width=None):
|
||||
embed = get_embed(url, max_width=max_width)
|
||||
embed = embeds.get_embed(url, max_width=max_width)
|
||||
try:
|
||||
if embed is not None:
|
||||
return mark_safe(embed.html)
|
||||
|
|
|
|||
|
|
@ -273,42 +273,22 @@ class TestOembed(TestCase):
|
|||
|
||||
|
||||
class TestEmbedFilter(TestCase):
|
||||
def setUp(self):
|
||||
class DummyResponse(object):
|
||||
def read(self):
|
||||
return b"foo"
|
||||
self.dummy_response = DummyResponse()
|
||||
@patch('wagtail.wagtailembeds.embeds.get_embed')
|
||||
def test_direct_call(self, get_embed):
|
||||
get_embed.return_value = Embed(html='<img src="http://www.example.com" />')
|
||||
|
||||
@patch('six.moves.urllib.request.urlopen')
|
||||
@patch('json.loads')
|
||||
def test_valid_embed(self, loads, urlopen):
|
||||
urlopen.return_value = self.dummy_response
|
||||
loads.return_value = {'type': 'photo',
|
||||
'url': 'http://www.example.com'}
|
||||
result = embed_filter('http://www.youtube.com/watch/')
|
||||
|
||||
self.assertEqual(result, '<img src="http://www.example.com" />')
|
||||
|
||||
@patch('six.moves.urllib.request.urlopen')
|
||||
@patch('json.loads')
|
||||
def test_render_filter(self, loads, urlopen):
|
||||
urlopen.return_value = self.dummy_response
|
||||
loads.return_value = {'type': 'photo',
|
||||
'url': 'http://www.example.com'}
|
||||
temp = template.Template('{% load wagtailembeds_tags %}{{ "http://www.youtube.com/watch/"|embed }}')
|
||||
context = template.Context()
|
||||
result = temp.render(context)
|
||||
self.assertEqual(result, '<img src="http://www.example.com" />')
|
||||
@patch('wagtail.wagtailembeds.embeds.get_embed')
|
||||
def test_call_from_template(self, get_embed):
|
||||
get_embed.return_value = Embed(html='<img src="http://www.example.com" />')
|
||||
|
||||
@patch('six.moves.urllib.request.urlopen')
|
||||
@patch('json.loads')
|
||||
def test_render_filter_nonexistent_type(self, loads, urlopen):
|
||||
urlopen.return_value = self.dummy_response
|
||||
loads.return_value = {'type': 'foo',
|
||||
'url': 'http://www.example.com'}
|
||||
temp = template.Template('{% load wagtailembeds_tags %}{{ "http://www.youtube.com/watch/"|embed }}')
|
||||
context = template.Context()
|
||||
result = temp.render(context)
|
||||
self.assertEqual(result, '')
|
||||
result = temp.render(template.Context())
|
||||
|
||||
self.assertEqual(result, '<img src="http://www.example.com" />')
|
||||
|
||||
|
||||
class TestEmbedBlock(TestCase):
|
||||
|
|
|
|||
Loading…
Reference in a new issue