0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Add unit tests for DbWhitelister, including ability to override the whitelist via hooks

This commit is contained in:
Matt Westcott 2014-06-20 14:50:10 +01:00
parent 2afb13667f
commit 9bc770d0a6
2 changed files with 52 additions and 0 deletions

View File

@ -1,4 +1,5 @@
from wagtail.wagtailadmin import hooks
from wagtail.wagtailcore.whitelist import attribute_rule, check_url, allow_without_attributes
def editor_css():
return """<link rel="stylesheet" href="/path/to/my/custom.css">"""
@ -8,3 +9,11 @@ hooks.register('insert_editor_css', editor_css)
def editor_js():
return """<script src="/path/to/my/custom.js"></script>"""
hooks.register('insert_editor_js', editor_js)
def whitelister_element_rules():
return {
'blockquote': allow_without_attributes,
'a': attribute_rule({'href': check_url, 'target': True}),
}
hooks.register('construct_whitelister_element_rules', whitelister_element_rules)

View File

@ -0,0 +1,43 @@
from django.test import TestCase
from wagtail.wagtailcore.rich_text import DbWhitelister
from bs4 import BeautifulSoup
class TestDbWhitelister(TestCase):
def assertHtmlEqual(self, str1, str2):
"""
Assert that two HTML strings are equal at the DOM level
(necessary because we can't guarantee the order that attributes are output in)
"""
self.assertEqual(BeautifulSoup(str1), BeautifulSoup(str2))
def test_page_link_is_rewritten(self):
input_html = '<p>Look at the <a data-linktype="page" data-id="2" href="/">lovely homepage</a> of my <a href="http://wagtail.io/">Wagtail</a> site</p>'
output_html = DbWhitelister.clean(input_html)
expected = '<p>Look at the <a linktype="page" id="2">lovely homepage</a> of my <a href="http://wagtail.io/">Wagtail</a> site</p>'
self.assertHtmlEqual(expected, output_html)
def test_document_link_is_rewritten(self):
input_html = '<p>Look at our <a data-linktype="document" data-id="1" href="/documents/1/brochure.pdf">horribly oversized brochure</a></p>'
output_html = DbWhitelister.clean(input_html)
expected = '<p>Look at our <a linktype="document" id="1">horribly oversized brochure</a></p>'
self.assertHtmlEqual(expected, output_html)
def test_image_embed_is_rewritten(self):
input_html = '<p>OMG look at this picture of a kitten: <figure data-embedtype="image" data-id="5" data-format="image-with-caption" data-alt="A cute kitten" class="fancy-image"><img src="/media/images/kitten.jpg" width="320" height="200" alt="A cute kitten" /><figcaption>A kitten, yesterday.</figcaption></figure></p>'
output_html = DbWhitelister.clean(input_html)
expected = '<p>OMG look at this picture of a kitten: <embed embedtype="image" id="5" format="image-with-caption" alt="A cute kitten" /></p>'
self.assertHtmlEqual(expected, output_html)
def test_media_embed_is_rewritten(self):
input_html = '<p>OMG look at this video of a kitten: <iframe data-embedtype="media" data-url="https://www.youtube.com/watch?v=dQw4w9WgXcQ" width="640" height="480" src="//www.youtube.com/embed/dQw4w9WgXcQ" frameborder="0" allowfullscreen></iframe></p>'
output_html = DbWhitelister.clean(input_html)
expected = '<p>OMG look at this video of a kitten: <embed embedtype="media" url="https://www.youtube.com/watch?v=dQw4w9WgXcQ" /></p>'
self.assertHtmlEqual(expected, output_html)
def test_whitelist_hooks(self):
# wagtail.tests.wagtail_hooks overrides the whitelist to permit <blockquote> and <a target="...">
input_html = '<blockquote>I would put a tax on all people who <a href="https://twitter.com/DMReporter/status/432914941201223680/photo/1" target="_blank" tea="darjeeling">stand in water</a>.</blockquote><p>- <character>Gumby</character>'
output_html = DbWhitelister.clean(input_html)
expected = '<blockquote>I would put a tax on all people who <a href="https://twitter.com/DMReporter/status/432914941201223680/photo/1" target="_blank">stand in water</a>.</blockquote><p>- Gumby'
self.assertHtmlEqual(expected, output_html)