1 | """ A short series of tests to check a proposed custom autoescape feature.
|
---|
2 | """
|
---|
3 |
|
---|
4 | from django.test import TestCase
|
---|
5 | from django.template import Template, Context
|
---|
6 | from django.utils.safestring import mark_safe
|
---|
7 |
|
---|
8 | def custom_escape_function(value):
|
---|
9 | return value.upper()
|
---|
10 | custom_escape_function.autoescape_context = "upper"
|
---|
11 |
|
---|
12 | def number_escape(value):
|
---|
13 | return value.replace("55", "00")
|
---|
14 | number_escape.autoescape_context = "number"
|
---|
15 |
|
---|
16 | def rtfescape(value):
|
---|
17 | return value.replace('\\', '\\\\').replace('{','\\{').replace('}', '\\}')
|
---|
18 | rtfescape.autoescape_context = "rtf"
|
---|
19 |
|
---|
20 | class CustomAutoEscape(TestCase):
|
---|
21 | def test_safe_string(self):
|
---|
22 | t = Template("{{ val|addslashes }}")
|
---|
23 | context = {'val': mark_safe("Joe's string")}
|
---|
24 | self.assertEqual(
|
---|
25 | t.render(Context(context, autoescape=rtfescape)),
|
---|
26 | u"Joe\\\\'s string"
|
---|
27 | )
|
---|
28 |
|
---|
29 | def test_unsafe_string(self):
|
---|
30 | t = Template("{{ val|addslashes }}")
|
---|
31 | context = {'val': "Joe's string"}
|
---|
32 | self.assertEqual(
|
---|
33 | t.render(Context(context, autoescape=rtfescape)),
|
---|
34 | u"Joe\\\\'s string"
|
---|
35 | )
|
---|
36 |
|
---|
37 | def test_manual_slashes(self):
|
---|
38 | t = Template("{{ val }}")
|
---|
39 | context = {'val': "Joe\\'s string"}
|
---|
40 | self.assertEqual(
|
---|
41 | t.render(Context(context, autoescape=rtfescape)),
|
---|
42 | u"Joe\\\\'s string"
|
---|
43 | )
|
---|
44 |
|
---|
45 | def test_filter_context_same(self):
|
---|
46 | t = Template("{% load custom %}{{ val|odd_letters }}")
|
---|
47 | context = {'val': "abcDEFghi"}
|
---|
48 | self.assertEqual(
|
---|
49 | t.render(Context(context, autoescape=custom_escape_function)),
|
---|
50 | u"BDFH"
|
---|
51 | )
|
---|
52 |
|
---|
53 | def test_filter_context_safe(self):
|
---|
54 | """ Data marked as safe for a safe function with the same AE context should not be escaped. """
|
---|
55 | t = Template("{% load custom %}{{ val|odd_letters }}")
|
---|
56 | context = {'val': mark_safe("abcDEFghi")}
|
---|
57 | self.assertEqual(
|
---|
58 | t.render(Context(context, autoescape=custom_escape_function)),
|
---|
59 | u"bDFh"
|
---|
60 | )
|
---|
61 |
|
---|
62 | def test_filter_context_diff(self):
|
---|
63 | """ Checks that changes from a filter marked .is_safe are not safe
|
---|
64 | when the filter is from a different context.
|
---|
65 | """
|
---|
66 | t = Template("{{ val|lower }}")
|
---|
67 | context = {'val': mark_safe("ABCDEFGHI")}
|
---|
68 | self.assertEqual(
|
---|
69 | t.render(Context(context, autoescape=custom_escape_function)),
|
---|
70 | u"ABCDEFGHI"
|
---|
71 | )
|
---|
72 |
|
---|
73 | def test_filter_context_diff_safe(self):
|
---|
74 | t = Template("{% load custom %}{{ val|lower|safe }}")
|
---|
75 | context = {'val': mark_safe("ABCDEFGHI")}
|
---|
76 | self.assertEqual(
|
---|
77 | t.render(Context(context, autoescape=custom_escape_function)),
|
---|
78 | u"abcdefghi"
|
---|
79 | )
|
---|
80 |
|
---|
81 | def test_filter_join(self):
|
---|
82 | """ Tests the changed join filter that handles escaping itself. """
|
---|
83 | t = Template('{{ val|join:", " }}')
|
---|
84 | context = {'val': ["ab", "cD", "EF", "gh", mark_safe("ij")]}
|
---|
85 | self.assertEqual(
|
---|
86 | t.render(Context(context, autoescape=custom_escape_function)),
|
---|
87 | u"AB, CD, EF, GH, ij"
|
---|
88 | )
|
---|
89 |
|
---|
90 | def test_filter_force_escape(self):
|
---|
91 | t = Template('{{ val|force_escape }}')
|
---|
92 | context = {'val': mark_safe("ab")}
|
---|
93 | self.assertEqual(
|
---|
94 | t.render(Context(context, autoescape=custom_escape_function)),
|
---|
95 | u"AB"
|
---|
96 | )
|
---|
97 |
|
---|
98 | def test_filter_linenumbers(self):
|
---|
99 | t = Template('{{ val|linenumbers }}')
|
---|
100 | context = {'val': "abcde\nfghi"}
|
---|
101 | self.assertEqual(
|
---|
102 | t.render(Context(context, autoescape=custom_escape_function)),
|
---|
103 | u"1. ABCDE\n2. FGHI"
|
---|
104 | )
|
---|
105 |
|
---|
106 | def test_filter_unordered_list(self):
|
---|
107 | t = Template('{{ val|unordered_list }}')
|
---|
108 | context = {'val': ["abcde","fghi"]}
|
---|
109 | self.assertEqual(
|
---|
110 | t.render(Context(context, autoescape=custom_escape_function)),
|
---|
111 | u"\t<li>ABCDE</li>\n\t<li>FGHI</li>"
|
---|
112 | )
|
---|
113 |
|
---|
114 | def test_filter_linebreaks(self):
|
---|
115 | t = Template('{{ val|linebreaks }}')
|
---|
116 | context = {'val': "abcde\n\nfghi\njklm"}
|
---|
117 | self.assertEqual(
|
---|
118 | t.render(Context(context, autoescape=custom_escape_function)),
|
---|
119 | u"<p>ABCDE</p>\n\n<p>FGHI<br />JKLM</p>"
|
---|
120 | )
|
---|
121 |
|
---|
122 | def test_filter_linebreaksbr(self):
|
---|
123 | t = Template('{{ val|linebreaksbr }}')
|
---|
124 | context = {'val': "abcde\n\nfghi\njklm"}
|
---|
125 | self.assertEqual(
|
---|
126 | t.render(Context(context, autoescape=custom_escape_function)),
|
---|
127 | u"ABCDE<br /><br />FGHI<br />JKLM"
|
---|
128 | )
|
---|
129 |
|
---|
130 | def test_filter_urlize(self):
|
---|
131 | t = Template('{{ val|urlize }}')
|
---|
132 | context = {'val': "a link to www.test.com la"}
|
---|
133 | self.assertEqual(
|
---|
134 | t.render(Context(context, autoescape=custom_escape_function)),
|
---|
135 | u'A LINK TO <a href="HTTP://WWW.TEST.COM" rel="nofollow">WWW.TEST.COM</a> LA'
|
---|
136 | )
|
---|
137 |
|
---|
138 | def test_filter_urlizetrunc(self):
|
---|
139 | t = Template('{{ val|urlizetrunc:5 }}')
|
---|
140 | context = {'val': "a link to www.test.com la"}
|
---|
141 | self.assertEqual(
|
---|
142 | t.render(Context(context, autoescape=custom_escape_function)),
|
---|
143 | u'A LINK TO <a href="HTTP://WWW.TEST.COM" rel="nofollow">WW...</a> LA'
|
---|
144 | )
|
---|
145 |
|
---|
146 | def test_filter_slugify(self):
|
---|
147 | t = Template('{{ val|slugify }}')
|
---|
148 | context = {'val': "my title string"}
|
---|
149 | self.assertEqual(
|
---|
150 | t.render(Context(context, autoescape=custom_escape_function)),
|
---|
151 | u'MY-TITLE-STRING'
|
---|
152 | )
|
---|
153 |
|
---|
154 | def test_filter_floatformat(self):
|
---|
155 | t = Template('{{ val|floatformat:2 }}')
|
---|
156 | context = {'val': 1559}
|
---|
157 | self.assertEqual(
|
---|
158 | t.render(Context(context, autoescape=number_escape)),
|
---|
159 | u'1009.00'
|
---|
160 | )
|
---|
161 |
|
---|
162 | def test_filter_cut(self):
|
---|
163 | """ Checks that post cut data is escaped. """
|
---|
164 | t = Template('{{ val|cut:"xyz" }}')
|
---|
165 | # Two fives will be together after xyz is removed
|
---|
166 | context = {'val': mark_safe("abcDE5xyz5Bc")}
|
---|
167 | self.assertEqual(
|
---|
168 | t.render(Context(context, autoescape=number_escape)),
|
---|
169 | u'abcDE00Bc'
|
---|
170 | )
|
---|
171 |
|
---|
172 | def test_filter_markup_textile(self):
|
---|
173 | t = Template('{% load markup %}{{ val|textile }}')
|
---|
174 | context = {'val': "abcDEF"}
|
---|
175 | self.assertEqual(
|
---|
176 | t.render(Context(context, autoescape=custom_escape_function)),
|
---|
177 | u'ABCDEF'
|
---|
178 | )
|
---|
179 |
|
---|
180 | def test_filter_markup_markdown(self):
|
---|
181 | t = Template('{% load markup %}{{ val|markdown }}')
|
---|
182 | context = {'val': "abcDEF"}
|
---|
183 | self.assertEqual(
|
---|
184 | t.render(Context(context, autoescape=custom_escape_function)),
|
---|
185 | u'ABCDEF'
|
---|
186 | )
|
---|
187 |
|
---|
188 | def test_filter_markup_restructuredtext(self):
|
---|
189 | t = Template('{% load markup %}{{ val|restructuredtext }}')
|
---|
190 | context = {'val': "abcDEF"}
|
---|
191 | self.assertEqual(
|
---|
192 | t.render(Context(context, autoescape=custom_escape_function)),
|
---|
193 | u'ABCDEF'
|
---|
194 | )
|
---|
195 |
|
---|
196 | def test_filter_mark_safe(self):
|
---|
197 | """ Checks a filter that marks its input as safe. """
|
---|
198 | t = Template('{% load custom %}{{ val|safe_lower }}')
|
---|
199 | context = {'val': "abcDEF"}
|
---|
200 | self.assertEqual(
|
---|
201 | t.render(Context(context, autoescape=custom_escape_function)),
|
---|
202 | u'abcdef'
|
---|
203 | )
|
---|