From 1a986bab0d9cc324c73c37f6ce4639157e342b97 Mon Sep 17 00:00:00 2001 From: Tom Talbot Date: Tue, 24 Jun 2014 10:26:44 +0100 Subject: [PATCH] Add unit tests for rich_text.py --- wagtail/tests/fixtures/test.json | 18 ++ wagtail/wagtailcore/tests/test_rich_text.py | 262 ++++++++++++++++++++ wagtail/wagtailcore/tests/tests.py | 47 ++-- wagtail/wagtailembeds/format.py | 1 - 4 files changed, 303 insertions(+), 25 deletions(-) create mode 100644 wagtail/wagtailcore/tests/test_rich_text.py diff --git a/wagtail/tests/fixtures/test.json b/wagtail/tests/fixtures/test.json index 11b82d9f1..bd4807e07 100644 --- a/wagtail/tests/fixtures/test.json +++ b/wagtail/tests/fixtures/test.json @@ -418,5 +418,23 @@ "page": 8, "submit_time": "2014-01-01T12:00:00.000Z" } +}, +{ + "pk": 1, + "model": "wagtaildocs.Document", + "fields": { + "title": "test document", + "created_at": "2014-01-01T12:00:00.000Z" + } +}, +{ + "pk": 1, + "model": "wagtailimages.Image", + "fields": { + "title": "test image", + "created_at": "2014-01-01T12:00:00.000Z", + "width": 0, + "height": 0 + } } ] diff --git a/wagtail/wagtailcore/tests/test_rich_text.py b/wagtail/wagtailcore/tests/test_rich_text.py new file mode 100644 index 000000000..9c98c6d3f --- /dev/null +++ b/wagtail/wagtailcore/tests/test_rich_text.py @@ -0,0 +1,262 @@ +from mock import patch + +from django.test import TestCase + +from wagtail.wagtailcore.rich_text import ( + ImageEmbedHandler, + MediaEmbedHandler, + PageLinkHandler, + DocumentLinkHandler, + DbWhitelister, + extract_attrs, + expand_db_html +) +from bs4 import BeautifulSoup + + +class TestImageEmbedHandler(TestCase): + fixtures = ['wagtail/tests/fixtures/test.json'] + + def test_get_db_attributes(self): + soup = BeautifulSoup( + 'foo' + ) + tag = soup.b + result = ImageEmbedHandler.get_db_attributes(tag) + self.assertEqual(result, + {'alt': 'test-alt', + 'id': 'test-id', + 'format': 'test-format'}) + + def test_expand_db_attributes_page_does_not_exist(self): + result = ImageEmbedHandler.expand_db_attributes( + {'id': 0}, + False + ) + self.assertEqual(result, '') + + @patch('wagtail.wagtailimages.models.Image') + @patch('django.core.files.File') + def test_expand_db_attributes_not_for_editor(self, mock_file, mock_image): + result = ImageEmbedHandler.expand_db_attributes( + {'id': 1, + 'alt': 'test-alt', + 'format': 'left'}, + False + ) + self.assertIn('foo' + ) + tag = soup.b + result = MediaEmbedHandler.get_db_attributes(tag) + self.assertEqual(result, + {'url': 'test-url'}) + + @patch('wagtail.wagtailembeds.embeds.oembed') + def test_expand_db_attributes_for_editor(self, oembed): + oembed.return_value = { + 'title': 'test title', + 'author_name': 'test author name', + 'provider_name': 'test provider name', + 'type': 'test type', + 'thumbnail_url': 'test thumbnail url', + 'width': 'test width', + 'height': 'test height', + 'html': 'test html' + } + result = MediaEmbedHandler.expand_db_attributes( + {'url': 'http://www.youtube.com/watch/'}, + True + ) + self.assertIn('
', result) + self.assertIn('

test title

', result) + self.assertIn('

URL: http://www.youtube.com/watch/

', result) + self.assertIn('

Provider: test provider name

', result) + self.assertIn('

Author: test author name

', result) + self.assertIn('test title', result) + + @patch('wagtail.wagtailembeds.embeds.oembed') + def test_expand_db_attributes_not_for_editor(self, oembed): + oembed.return_value = { + 'title': 'test title', + 'author_name': 'test author name', + 'provider_name': 'test provider name', + 'type': 'test type', + 'thumbnail_url': 'test thumbnail url', + 'width': 'test width', + 'height': 'test height', + 'html': 'test html' + } + result = MediaEmbedHandler.expand_db_attributes( + {'url': 'http://www.youtube.com/watch/'}, + False + ) + self.assertIn('test html', result) + + +class TestPageLinkHandler(TestCase): + fixtures = ['wagtail/tests/fixtures/test.json'] + + def test_get_db_attributes(self): + soup = BeautifulSoup( + 'foo' + ) + tag = soup.a + result = PageLinkHandler.get_db_attributes(tag) + self.assertEqual(result, + {'id': 'test-id'}) + + def test_expand_db_attributes_page_does_not_exist(self): + result = PageLinkHandler.expand_db_attributes( + {'id': 0}, + False + ) + self.assertEqual(result, '') + + def test_expand_db_attributes_for_editor(self): + result = PageLinkHandler.expand_db_attributes( + {'id': 1}, + True + ) + self.assertEqual(result, + '') + + def test_expand_db_attributes_not_for_editor(self): + result = PageLinkHandler.expand_db_attributes( + {'id': 1}, + False + ) + self.assertEqual(result, '') + + +class TestDocumentLinkHandler(TestCase): + fixtures = ['wagtail/tests/fixtures/test.json'] + + def test_get_db_attributes(self): + soup = BeautifulSoup( + 'foo' + ) + tag = soup.a + result = DocumentLinkHandler.get_db_attributes(tag) + self.assertEqual(result, + {'id': 'test-id'}) + + def test_expand_db_attributes_document_does_not_exist(self): + result = DocumentLinkHandler.expand_db_attributes( + {'id': 0}, + False + ) + self.assertEqual(result, '') + + def test_expand_db_attributes_for_editor(self): + result = DocumentLinkHandler.expand_db_attributes( + {'id': 1}, + True + ) + self.assertEqual(result, + '') + + def test_expand_db_attributes_not_for_editor(self): + result = DocumentLinkHandler.expand_db_attributes( + {'id': 1}, + False + ) + self.assertEqual(result, + '') + + +class TestDbWhiteLister(TestCase): + def test_clean_tag_node_div(self): + soup = BeautifulSoup( + '
foo
' + ) + tag = soup.div + self.assertEqual(tag.name, 'div') + DbWhitelister.clean_tag_node(soup, tag) + self.assertEqual(tag.name, 'p') + + def test_clean_tag_node_with_data_embedtype(self): + soup = BeautifulSoup( + '

foo

' + ) + tag = soup.p + DbWhitelister.clean_tag_node(soup, tag) + self.assertEqual(str(tag), + '

') + + def test_clean_tag_node_with_data_linktype(self): + soup = BeautifulSoup( + 'foo' + ) + tag = soup.a + DbWhitelister.clean_tag_node(soup, tag) + self.assertEqual(str(tag), 'foo') + + def test_clean_tag_node(self): + soup = BeautifulSoup( + 'foo' + ) + tag = soup.a + DbWhitelister.clean_tag_node(soup, tag) + self.assertEqual(str(tag), 'foo') + + +class TestExtractAttrs(TestCase): + def test_extract_attr(self): + html = 'snowman' + result = extract_attrs(html) + self.assertEqual(result, {'foo': 'bar', 'baz': 'quux'}) + + +class TestExpandDbHtml(TestCase): + def test_expand_db_html_with_linktype(self): + html = 'foo' + result = expand_db_html(html) + self.assertEqual(result, 'foo') + + def test_expand_db_html_no_linktype(self): + html = 'foo' + result = expand_db_html(html) + self.assertEqual(result, 'foo') + + @patch('wagtail.wagtailembeds.embeds.oembed') + def test_expand_db_html_with_embed(self, oembed): + oembed.return_value = { + 'title': 'test title', + 'author_name': 'test author name', + 'provider_name': 'test provider name', + 'type': 'test type', + 'thumbnail_url': 'test thumbnail url', + 'width': 'test width', + 'height': 'test height', + 'html': 'test html' + } + html = '' + result = expand_db_html(html) + self.assertIn('test html', result) diff --git a/wagtail/wagtailcore/tests/tests.py b/wagtail/wagtailcore/tests/tests.py index d10474bba..d7d9a24f9 100644 --- a/wagtail/wagtailcore/tests/tests.py +++ b/wagtail/wagtailcore/tests/tests.py @@ -1,12 +1,7 @@ -from StringIO import StringIO +from django.test import TestCase -from django.test import TestCase, Client -from django.http import HttpRequest, Http404 -from django.core import management -from django.contrib.auth.models import User - -from wagtail.wagtailcore.models import Page, Site, UserPagePermissionsProxy -from wagtail.tests.models import EventPage, EventIndex, SimplePage +from wagtail.wagtailcore.models import Page, Site +from wagtail.tests.models import SimplePage class TestPageUrlTags(TestCase): @@ -15,25 +10,28 @@ class TestPageUrlTags(TestCase): def test_pageurl_tag(self): response = self.client.get('/events/') self.assertEqual(response.status_code, 200) - self.assertContains(response, 'Christmas') + self.assertContains(response, + 'Christmas') def test_slugurl_tag(self): response = self.client.get('/events/christmas/') self.assertEqual(response.status_code, 200) - self.assertContains(response, 'Back to events index') + self.assertContains(response, + 'Back to events index') class TestIssue7(TestCase): """ - This tests for an issue where if a site root page was moved, all the page - urls in that site would change to None. + This tests for an issue where if a site root page was moved, all + the page urls in that site would change to None. - The issue was caused by the 'wagtail_site_root_paths' cache variable not being - cleared when a site root page was moved. Which left all the child pages - thinking that they are no longer in the site and return None as their url. + The issue was caused by the 'wagtail_site_root_paths' cache + variable not being cleared when a site root page was moved. Which + left all the child pages thinking that they are no longer in the + site and return None as their url. - Fix: d6cce69a397d08d5ee81a8cbc1977ab2c9db2682 - Discussion: https://github.com/torchbox/wagtail/issues/7 + Fix: d6cce69a397d08d5ee81a8cbc1977ab2c9db2682 Discussion: + https://github.com/torchbox/wagtail/issues/7 """ fixtures = ['test.json'] @@ -67,15 +65,16 @@ class TestIssue7(TestCase): class TestIssue157(TestCase): """ - This tests for an issue where if a site root pages slug was changed, all the page - urls in that site would change to None. + This tests for an issue where if a site root pages slug was + changed, all the page urls in that site would change to None. - The issue was caused by the 'wagtail_site_root_paths' cache variable not being - cleared when a site root page was changed. Which left all the child pages - thinking that they are no longer in the site and return None as their url. + The issue was caused by the 'wagtail_site_root_paths' cache + variable not being cleared when a site root page was changed. + Which left all the child pages thinking that they are no longer in + the site and return None as their url. - Fix: d6cce69a397d08d5ee81a8cbc1977ab2c9db2682 - Discussion: https://github.com/torchbox/wagtail/issues/157 + Fix: d6cce69a397d08d5ee81a8cbc1977ab2c9db2682 Discussion: + https://github.com/torchbox/wagtail/issues/157 """ fixtures = ['test.json'] diff --git a/wagtail/wagtailembeds/format.py b/wagtail/wagtailembeds/format.py index 1654be989..8a73ff524 100644 --- a/wagtail/wagtailembeds/format.py +++ b/wagtail/wagtailembeds/format.py @@ -1,6 +1,5 @@ from __future__ import division # Use true division -from django.utils.html import escape from django.template.loader import render_to_string from wagtail.wagtailembeds import get_embed