diff --git a/wagtail/wagtailembeds/tests.py b/wagtail/wagtailembeds/tests.py index 09ad3c9025..e7d040aa69 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 embedly as embedly_filter class TestEmbeds(TestCase): @@ -258,3 +260,81 @@ 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_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, '') + + @patch('urllib2.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 embed_filters %}{{ "http://www.youtube.com/watch/"|embed }}') + context = template.Context() + result = temp.render(context) + self.assertEqual(result, '') + + +class TestEmbedlyFilter(TestEmbedFilter): + 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_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, '') + + @patch('urllib2.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 embed_filters %}{{ "http://www.youtube.com/watch/"|embedly }}') + context = template.Context() + result = temp.render(context) + self.assertEqual(result, '') diff --git a/wagtail/wagtailimages/tests.py b/wagtail/wagtailimages/tests.py index 2fbac118c5..91ed2df6a7 100644 --- a/wagtail/wagtailimages/tests.py +++ b/wagtail/wagtailimages/tests.py @@ -1,3 +1,5 @@ +from mock import MagicMock + from django.test import TestCase from django import template from django.contrib.auth.models import User, Group, Permission @@ -6,7 +8,11 @@ from django.core.files.uploadedfile import SimpleUploadedFile from wagtail.tests.utils import unittest, WagtailTestUtils from wagtail.wagtailimages.models import get_image_model -from wagtail.wagtailimages.templatetags import image_tags +from wagtail.wagtailimages.formats import ( + Format, + get_image_format, + register_image_format +) from wagtail.wagtailimages.backends import get_image_backend from wagtail.wagtailimages.backends.pillow import PillowBackend @@ -419,3 +425,49 @@ class TestImageChooserUploadView(TestCase, WagtailTestUtils): self.assertTemplateUsed(response, 'wagtailimages/chooser/chooser.js') # TODO: Test uploading through chooser + + +class TestFormat(TestCase): + def setUp(self): + # test format + self.format = Format( + 'test name', + 'test label', + 'test classnames', + 'test filter spec' + ) + # test image + self.image = MagicMock() + self.image.id = 0 + + def test_editor_attributes(self): + result = self.format.editor_attributes( + self.image, + 'test alt text' + ) + self.assertEqual(result, + 'data-embedtype="image" data-id="0" data-format="test name" data-alt="test alt text" ') + + def test_image_to_editor_html(self): + result = self.format.image_to_editor_html( + self.image, + 'test alt text' + ) + self.assertRegexpMatches( + result, + 'test alt text', + ) + + def test_image_to_html_no_classnames(self): + self.format.classnames = None + result = self.format.image_to_html(self.image, 'test alt text') + self.assertRegexpMatches( + result, + 'test alt text' + ) + self.format.classnames = 'test classnames' + + def test_get_image_format(self): + register_image_format(self.format) + result = get_image_format('test name') + self.assertEqual(result, self.format)