Imported specific functions in tests.utils_tests.test_html.

This commit is contained in:
Tim Graham 2017-02-01 18:37:16 -05:00
parent 597bfcbf8b
commit 2af8cd22a9
1 changed files with 36 additions and 37 deletions

View File

@ -2,8 +2,12 @@ import os
from datetime import datetime
from django.test import SimpleTestCase
from django.utils import html, safestring
from django.utils.functional import lazystr
from django.utils.html import (
conditional_escape, escape, escapejs, format_html, html_safe, linebreaks,
smart_urlquote, strip_spaces_between_tags, strip_tags,
)
from django.utils.safestring import mark_safe
class TestUtilsHtml(SimpleTestCase):
@ -18,7 +22,6 @@ class TestUtilsHtml(SimpleTestCase):
self.assertEqual(function(value), output)
def test_escape(self):
f = html.escape
items = (
('&', '&'),
('<', '&lt;'),
@ -30,26 +33,26 @@ class TestUtilsHtml(SimpleTestCase):
patterns = ("%s", "asdf%sfdsa", "%s1", "1%sb")
for value, output in items:
for pattern in patterns:
self.check_output(f, pattern % value, pattern % output)
self.check_output(f, lazystr(pattern % value), pattern % output)
self.check_output(escape, pattern % value, pattern % output)
self.check_output(escape, lazystr(pattern % value), pattern % output)
# Check repeated values.
self.check_output(f, value * 2, output * 2)
self.check_output(escape, value * 2, output * 2)
# Verify it doesn't double replace &.
self.check_output(f, '<&', '&lt;&amp;')
self.check_output(escape, '<&', '&lt;&amp;')
def test_format_html(self):
self.assertEqual(
html.format_html("{} {} {third} {fourth}",
"< Dangerous >",
html.mark_safe("<b>safe</b>"),
third="< dangerous again",
fourth=html.mark_safe("<i>safe again</i>")
),
format_html(
"{} {} {third} {fourth}",
"< Dangerous >",
mark_safe("<b>safe</b>"),
third="< dangerous again",
fourth=mark_safe("<i>safe again</i>"),
),
"&lt; Dangerous &gt; <b>safe</b> &lt; dangerous again <i>safe again</i>"
)
def test_linebreaks(self):
f = html.linebreaks
items = (
("para1\n\npara2\r\rpara3", "<p>para1</p>\n\n<p>para2</p>\n\n<p>para3</p>"),
("para1\nsub1\rsub2\n\npara2", "<p>para1<br />sub1<br />sub2</p>\n\n<p>para2</p>"),
@ -57,11 +60,10 @@ class TestUtilsHtml(SimpleTestCase):
("para1\tmore\n\npara2", "<p>para1\tmore</p>\n\n<p>para2</p>"),
)
for value, output in items:
self.check_output(f, value, output)
self.check_output(f, lazystr(value), output)
self.check_output(linebreaks, value, output)
self.check_output(linebreaks, lazystr(value), output)
def test_strip_tags(self):
f = html.strip_tags
items = (
('<p>See: &#39;&eacute; is an apostrophe followed by e acute</p>',
'See: &#39;&eacute; is an apostrophe followed by e acute'),
@ -83,14 +85,14 @@ class TestUtilsHtml(SimpleTestCase):
('&gotcha&#;<>', '&gotcha&#;<>'),
)
for value, output in items:
self.check_output(f, value, output)
self.check_output(f, lazystr(value), output)
self.check_output(strip_tags, value, output)
self.check_output(strip_tags, lazystr(value), output)
# Some convoluted syntax for which parsing may differ between python versions
output = html.strip_tags('<sc<!-- -->ript>test<<!-- -->/script>')
output = strip_tags('<sc<!-- -->ript>test<<!-- -->/script>')
self.assertNotIn('<script>', output)
self.assertIn('test', output)
output = html.strip_tags('<script>alert()</script>&h')
output = strip_tags('<script>alert()</script>&h')
self.assertNotIn('<script>', output)
self.assertIn('alert()', output)
@ -100,19 +102,18 @@ class TestUtilsHtml(SimpleTestCase):
with open(path, 'r') as fp:
content = fp.read()
start = datetime.now()
stripped = html.strip_tags(content)
stripped = strip_tags(content)
elapsed = datetime.now() - start
self.assertEqual(elapsed.seconds, 0)
self.assertIn("Please try again.", stripped)
self.assertNotIn('<', stripped)
def test_strip_spaces_between_tags(self):
f = html.strip_spaces_between_tags
# Strings that should come out untouched.
items = (' <adf>', '<adf> ', ' </adf> ', ' <f> x</f>')
for value in items:
self.check_output(f, value)
self.check_output(f, lazystr(value))
self.check_output(strip_spaces_between_tags, value)
self.check_output(strip_spaces_between_tags, lazystr(value))
# Strings that have spaces to strip.
items = (
('<d> </d>', '<d></d>'),
@ -120,11 +121,10 @@ class TestUtilsHtml(SimpleTestCase):
('\n<p>\t</p>\n<p> </p>\n', '\n<p></p><p></p>\n'),
)
for value, output in items:
self.check_output(f, value, output)
self.check_output(f, lazystr(value), output)
self.check_output(strip_spaces_between_tags, value, output)
self.check_output(strip_spaces_between_tags, lazystr(value), output)
def test_escapejs(self):
f = html.escapejs
items = (
('"double quotes" and \'single quotes\'', '\\u0022double quotes\\u0022 and \\u0027single quotes\\u0027'),
(r'\ : backslashes, too', '\\u005C : backslashes, too'),
@ -139,11 +139,11 @@ class TestUtilsHtml(SimpleTestCase):
),
)
for value, output in items:
self.check_output(f, value, output)
self.check_output(f, lazystr(value), output)
self.check_output(escapejs, value, output)
self.check_output(escapejs, lazystr(value), output)
def test_smart_urlquote(self):
quote = html.smart_urlquote
quote = smart_urlquote
# IDNs are properly quoted
self.assertEqual(quote('http://öäü.com/'), 'http://xn--4ca9at.com/')
self.assertEqual(quote('http://öäü.com/öäü/'), 'http://xn--4ca9at.com/%C3%B6%C3%A4%C3%BC/')
@ -159,12 +159,11 @@ class TestUtilsHtml(SimpleTestCase):
def test_conditional_escape(self):
s = '<h1>interop</h1>'
self.assertEqual(html.conditional_escape(s),
'&lt;h1&gt;interop&lt;/h1&gt;')
self.assertEqual(html.conditional_escape(safestring.mark_safe(s)), s)
self.assertEqual(conditional_escape(s), '&lt;h1&gt;interop&lt;/h1&gt;')
self.assertEqual(conditional_escape(mark_safe(s)), s)
def test_html_safe(self):
@html.html_safe
@html_safe
class HtmlClass:
def __str__(self):
return "<h1>I'm a html class!</h1>"
@ -183,7 +182,7 @@ class TestUtilsHtml(SimpleTestCase):
def __str__(self):
return 'some non html content'
@html.html_safe
@html_safe
class Subclass(BaseClass):
def __str__(self):
# overrides __str__ and is marked as html_safe
@ -195,7 +194,7 @@ class TestUtilsHtml(SimpleTestCase):
def test_html_safe_defines_html_error(self):
msg = "can't apply @html_safe to HtmlClass because it defines __html__()."
with self.assertRaisesMessage(ValueError, msg):
@html.html_safe
@html_safe
class HtmlClass:
def __html__(self):
return "<h1>I'm a html class!</h1>"
@ -203,6 +202,6 @@ class TestUtilsHtml(SimpleTestCase):
def test_html_safe_doesnt_define_str(self):
msg = "can't apply @html_safe to HtmlClass because it doesn't define __str__()."
with self.assertRaisesMessage(ValueError, msg):
@html.html_safe
@html_safe
class HtmlClass:
pass