~ubuntu-branches/debian/sid/python-django/sid

« back to all changes in this revision

Viewing changes to tests/regressiontests/utils/html.py

  • Committer: Package Import Robot
  • Author(s): Luke Faraone
  • Date: 2013-11-07 15:33:49 UTC
  • mfrom: (1.3.12)
  • Revision ID: package-import@ubuntu.com-20131107153349-e31sc149l2szs3jb
Tags: 1.6-1
* New upstream version. Closes: #557474, #724637.
* python-django now also suggests the installation of ipython,
  bpython, python-django-doc, and libgdal1.
  Closes: #636511, #686333, #704203
* Set package maintainer to Debian Python Modules Team.
* Bump standards version to 3.9.5, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
from __future__ import unicode_literals
3
 
 
4
 
import unittest
5
 
 
6
 
from django.utils import html
7
 
 
8
 
class TestUtilsHtml(unittest.TestCase):
9
 
 
10
 
    def check_output(self, function, value, output=None):
11
 
        """
12
 
        Check that function(value) equals output.  If output is None,
13
 
        check that function(value) equals value.
14
 
        """
15
 
        if output is None:
16
 
            output = value
17
 
        self.assertEqual(function(value), output)
18
 
 
19
 
    def test_escape(self):
20
 
        f = html.escape
21
 
        items = (
22
 
            ('&','&'),
23
 
            ('<', '&lt;'),
24
 
            ('>', '&gt;'),
25
 
            ('"', '&quot;'),
26
 
            ("'", '&#39;'),
27
 
        )
28
 
        # Substitution patterns for testing the above items.
29
 
        patterns = ("%s", "asdf%sfdsa", "%s1", "1%sb")
30
 
        for value, output in items:
31
 
            for pattern in patterns:
32
 
                self.check_output(f, pattern % value, pattern % output)
33
 
            # Check repeated values.
34
 
            self.check_output(f, value * 2, output * 2)
35
 
        # Verify it doesn't double replace &.
36
 
        self.check_output(f, '<&', '&lt;&amp;')
37
 
 
38
 
    def test_format_html(self):
39
 
        self.assertEqual(
40
 
            html.format_html("{0} {1} {third} {fourth}",
41
 
                             "< Dangerous >",
42
 
                             html.mark_safe("<b>safe</b>"),
43
 
                             third="< dangerous again",
44
 
                             fourth=html.mark_safe("<i>safe again</i>")
45
 
                             ),
46
 
            "&lt; Dangerous &gt; <b>safe</b> &lt; dangerous again <i>safe again</i>"
47
 
            )
48
 
 
49
 
    def test_linebreaks(self):
50
 
        f = html.linebreaks
51
 
        items = (
52
 
            ("para1\n\npara2\r\rpara3", "<p>para1</p>\n\n<p>para2</p>\n\n<p>para3</p>"),
53
 
            ("para1\nsub1\rsub2\n\npara2", "<p>para1<br />sub1<br />sub2</p>\n\n<p>para2</p>"),
54
 
            ("para1\r\n\r\npara2\rsub1\r\rpara4", "<p>para1</p>\n\n<p>para2<br />sub1</p>\n\n<p>para4</p>"),
55
 
            ("para1\tmore\n\npara2", "<p>para1\tmore</p>\n\n<p>para2</p>"),
56
 
        )
57
 
        for value, output in items:
58
 
            self.check_output(f, value, output)
59
 
 
60
 
    def test_strip_tags(self):
61
 
        f = html.strip_tags
62
 
        items = (
63
 
            ('<adf>a', 'a'),
64
 
            ('</adf>a', 'a'),
65
 
            ('<asdf><asdf>e', 'e'),
66
 
            ('<f', '<f'),
67
 
            ('</fe', '</fe'),
68
 
            ('<x>b<y>', 'b'),
69
 
            ('a<p a >b</p>c', 'abc'),
70
 
            ('d<a:b c:d>e</p>f', 'def'),
71
 
        )
72
 
        for value, output in items:
73
 
            self.check_output(f, value, output)
74
 
 
75
 
    def test_strip_spaces_between_tags(self):
76
 
        f = html.strip_spaces_between_tags
77
 
        # Strings that should come out untouched.
78
 
        items = (' <adf>', '<adf> ', ' </adf> ', ' <f> x</f>')
79
 
        for value in items:
80
 
            self.check_output(f, value)
81
 
        # Strings that have spaces to strip.
82
 
        items = (
83
 
            ('<d> </d>', '<d></d>'),
84
 
            ('<p>hello </p>\n<p> world</p>', '<p>hello </p><p> world</p>'),
85
 
            ('\n<p>\t</p>\n<p> </p>\n', '\n<p></p><p></p>\n'),
86
 
        )
87
 
        for value, output in items:
88
 
            self.check_output(f, value, output)
89
 
 
90
 
    def test_strip_entities(self):
91
 
        f = html.strip_entities
92
 
        # Strings that should come out untouched.
93
 
        values = ("&", "&a", "&a", "a&#a")
94
 
        for value in values:
95
 
            self.check_output(f, value)
96
 
        # Valid entities that should be stripped from the patterns.
97
 
        entities = ("&#1;", "&#12;", "&a;", "&fdasdfasdfasdf;")
98
 
        patterns = (
99
 
            ("asdf %(entity)s ", "asdf  "),
100
 
            ("%(entity)s%(entity)s", ""),
101
 
            ("&%(entity)s%(entity)s", "&"),
102
 
            ("%(entity)s3", "3"),
103
 
        )
104
 
        for entity in entities:
105
 
            for in_pattern, output in patterns:
106
 
                self.check_output(f, in_pattern % {'entity': entity}, output)
107
 
 
108
 
    def test_fix_ampersands(self):
109
 
        f = html.fix_ampersands
110
 
        # Strings without ampersands or with ampersands already encoded.
111
 
        values = ("a&#1;", "b", "&a;", "&amp; &x; ", "asdf")
112
 
        patterns = (
113
 
            ("%s", "%s"),
114
 
            ("&%s", "&amp;%s"),
115
 
            ("&%s&", "&amp;%s&amp;"),
116
 
        )
117
 
        for value in values:
118
 
            for in_pattern, out_pattern in patterns:
119
 
                self.check_output(f, in_pattern % value, out_pattern % value)
120
 
        # Strings with ampersands that need encoding.
121
 
        items = (
122
 
            ("&#;", "&amp;#;"),
123
 
            ("&#875 ;", "&amp;#875 ;"),
124
 
            ("&#4abc;", "&amp;#4abc;"),
125
 
        )
126
 
        for value, output in items:
127
 
            self.check_output(f, value, output)
128
 
 
129
 
    def test_escapejs(self):
130
 
        f = html.escapejs
131
 
        items = (
132
 
            ('"double quotes" and \'single quotes\'', '\\u0022double quotes\\u0022 and \\u0027single quotes\\u0027'),
133
 
            (r'\ : backslashes, too', '\\u005C : backslashes, too'),
134
 
            ('and lots of whitespace: \r\n\t\v\f\b', 'and lots of whitespace: \\u000D\\u000A\\u0009\\u000B\\u000C\\u0008'),
135
 
            (r'<script>and this</script>', '\\u003Cscript\\u003Eand this\\u003C/script\\u003E'),
136
 
            ('paragraph separator:\u2029and line separator:\u2028', 'paragraph separator:\\u2029and line separator:\\u2028'),
137
 
        )
138
 
        for value, output in items:
139
 
            self.check_output(f, value, output)
140
 
 
141
 
    def test_clean_html(self):
142
 
        f = html.clean_html
143
 
        items = (
144
 
            ('<p>I <i>believe</i> in <b>semantic markup</b>!</p>', '<p>I <em>believe</em> in <strong>semantic markup</strong>!</p>'),
145
 
            ('I escape & I don\'t <a href="#" target="_blank">target</a>', 'I escape &amp; I don\'t <a href="#" >target</a>'),
146
 
            ('<p>I kill whitespace</p><br clear="all"><p>&nbsp;</p>', '<p>I kill whitespace</p>'),
147
 
            # also a regression test for #7267: this used to raise an UnicodeDecodeError
148
 
            ('<p>* foo</p><p>* bar</p>', '<ul>\n<li> foo</li><li> bar</li>\n</ul>'),
149
 
        )
150
 
        for value, output in items:
151
 
            self.check_output(f, value, output)
152
 
 
153
 
    def test_remove_tags(self):
154
 
        f = html.remove_tags
155
 
        items = (
156
 
            ("<b><i>Yes</i></b>", "b i", "Yes"),
157
 
            ("<a>x</a> <p><b>y</b></p>", "a b", "x <p>y</p>"),
158
 
        )
159
 
        for value, tags, output in items:
160
 
            self.assertEqual(f(value, tags), output)
161
 
 
162
 
    def test_smart_urlquote(self):
163
 
        quote = html.smart_urlquote
164
 
        # Ensure that IDNs are properly quoted
165
 
        self.assertEqual(quote('http://öäü.com/'), 'http://xn--4ca9at.com/')
166
 
        self.assertEqual(quote('http://öäü.com/öäü/'), 'http://xn--4ca9at.com/%C3%B6%C3%A4%C3%BC/')
167
 
        # Ensure that everything unsafe is quoted, !*'();:@&=+$,/?#[]~ is considered safe as per RFC
168
 
        self.assertEqual(quote('http://example.com/path/öäü/'), 'http://example.com/path/%C3%B6%C3%A4%C3%BC/')
169
 
        self.assertEqual(quote('http://example.com/%C3%B6/ä/'), 'http://example.com/%C3%B6/%C3%A4/')
170
 
        self.assertEqual(quote('http://example.com/?x=1&y=2'), 'http://example.com/?x=1&y=2')