From 015de477af3415e7d910b705d9f6d214f35c2503 Mon Sep 17 00:00:00 2001 From: Tom Talbot Date: Wed, 25 Jun 2014 11:13:09 +0100 Subject: [PATCH] Add unit tests for embed filters --- wagtail/wagtailembeds/tests.py | 60 +++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/wagtail/wagtailembeds/tests.py b/wagtail/wagtailembeds/tests.py index 892dde5bd..8cedf944b 100644 --- a/wagtail/wagtailembeds/tests.py +++ b/wagtail/wagtailembeds/tests.py @@ -7,6 +7,7 @@ try: except ImportError: no_embedly = True +from django import template from django.test import TestCase from wagtail.tests.utils import WagtailTestUtils, unittest @@ -18,7 +19,8 @@ from wagtail.wagtailembeds.embeds import ( AccessDeniedEmbedlyException, ) from wagtail.wagtailembeds.embeds import embedly as wagtail_embedly, oembed as wagtail_oembed - +from wagtail.wagtailembeds.templatetags.embed_filters import embed as embed_filter +from wagtail.wagtailembeds.templatetags.embed_filters import embed as embedly_filter class TestEmbeds(TestCase): @@ -245,3 +247,59 @@ class TestOembed(TestCase): 'height': 'test_height', 'html': 'test_html' }) + + +class TestEmbedFilter(TestCase): + def setUp(self): + class DummyResponse(object): + def read(self): + return "foo" + self.dummy_response = DummyResponse() + + @patch('urllib2.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, '') + + @patch('urllib2.urlopen') + @patch('json.loads') + def test_render_embed_filter(self, loads, urlopen): + urlopen.return_value = self.dummy_response + loads.return_value = {'type': 'photo', + 'url': 'http://www.example.com'} + temp = template.Template("{% load embed_filters %}{{ 'http://www.youtube.com/watch/'|embed }}") + context = template.Context() + result = temp.render(context) + self.assertEqual(result, '') + + +class TestEmbedlyFilter(TestCase): + def setUp(self): + class DummyResponse(object): + def read(self): + return "foo" + self.dummy_response = DummyResponse() + + @patch('urllib2.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 = embedly_filter('http://www.youtube.com/watch/') + self.assertEqual(result, '') + + @patch('urllib2.urlopen') + @patch('json.loads') + def test_render_embed_filter(self, loads, urlopen): + urlopen.return_value = self.dummy_response + loads.return_value = {'type': 'photo', + 'url': 'http://www.example.com'} + temp = template.Template("{% load embed_filters %}{{ 'http://www.youtube.com/watch/'|embedly }}") + context = template.Context() + result = temp.render(context) + self.assertEqual(result, '')