| 1 |
from django.template import Context, Template |
| 2 |
from django.test import SimpleTestCase |
| 3 |
from django.utils import html, translation |
| 4 |
from django.utils.functional import Promise, lazy, lazystr |
| 5 |
from django.utils.safestring import SafeData, SafeString, mark_safe |
| 6 |
from django.utils.translation import gettext_lazy |
| 7 |
|
| 8 |
|
| 9 |
class customescape(str): |
| 10 |
def __html__(self): |
| 11 |
# Implement specific and wrong escaping in order to be able to detect |
| 12 |
# when it runs. |
| 13 |
return self.replace("<", "<<").replace(">", ">>") |
| 14 |
|
| 15 |
|
| 16 |
class SafeStringTest(SimpleTestCase): |
| 17 |
def assertRenderEqual(self, tpl, expected, **context): |
| 18 |
context = Context(context) |
| 19 |
tpl = Template(tpl) |
| 20 |
self.assertEqual(tpl.render(context), expected) |
| 21 |
|
| 22 |
def test_mark_safe(self): |
| 23 |
s = mark_safe("a&b") |
| 24 |
|
| 25 |
self.assertRenderEqual("{{ s }}", "a&b", s=s) |
| 26 |
self.assertRenderEqual("{{ s|force_escape }}", "a&b", s=s) |
| 27 |
|
| 28 |
def test_mark_safe_str(self): |
| 29 |
""" |
| 30 |
Calling str() on a SafeString instance doesn't lose the safe status. |
| 31 |
""" |
| 32 |
s = mark_safe("a&b") |
| 33 |
self.assertIsInstance(str(s), type(s)) |
| 34 |
|
| 35 |
def test_mark_safe_object_implementing_dunder_html(self): |
| 36 |
e = customescape("<a&b>") |
| 37 |
s = mark_safe(e) |
| 38 |
self.assertIs(s, e) |
| 39 |
|
| 40 |
self.assertRenderEqual("{{ s }}", "<<a&b>>", s=s) |
| 41 |
self.assertRenderEqual("{{ s|force_escape }}", "<a&b>", s=s) |
| 42 |
|
| 43 |
def test_mark_safe_lazy(self): |
| 44 |
safe_s = mark_safe(lazystr("a&b")) |
| 45 |
|
| 46 |
self.assertIsInstance(safe_s, Promise) |
| 47 |
self.assertRenderEqual("{{ s }}", "a&b", s=safe_s) |
| 48 |
self.assertIsInstance(str(safe_s), SafeData) |
| 49 |
|
| 50 |
def test_mark_safe_lazy_i18n(self): |
| 51 |
s = mark_safe(gettext_lazy("name")) |
| 52 |
tpl = Template("{{ s }}") |
| 53 |
with translation.override("fr"): |
| 54 |
self.assertEqual(tpl.render(Context({"s": s})), "nom") |
| 55 |
|
| 56 |
def test_mark_safe_object_implementing_dunder_str(self): |
| 57 |
class Obj: |
| 58 |
def __str__(self): |
| 59 |
return "<obj>" |
| 60 |
|
| 61 |
s = mark_safe(Obj()) |
| 62 |
|
| 63 |
self.assertRenderEqual("{{ s }}", "<obj>", s=s) |
| 64 |
|
| 65 |
def test_mark_safe_result_implements_dunder_html(self): |
| 66 |
self.assertEqual(mark_safe("a&b").__html__(), "a&b") |
| 67 |
|
| 68 |
def test_mark_safe_lazy_result_implements_dunder_html(self): |
| 69 |
self.assertEqual(mark_safe(lazystr("a&b")).__html__(), "a&b") |
| 70 |
|
| 71 |
def test_add_lazy_safe_text_and_safe_text(self): |
| 72 |
s = html.escape(lazystr("a")) |
| 73 |
s += mark_safe("&b") |
| 74 |
self.assertRenderEqual("{{ s }}", "a&b", s=s) |
| 75 |
|
| 76 |
s = html.escapejs(lazystr("a")) |
| 77 |
s += mark_safe("&b") |
| 78 |
self.assertRenderEqual("{{ s }}", "a&b", s=s) |
| 79 |
|
| 80 |
def test_mark_safe_as_decorator(self): |
| 81 |
""" |
| 82 |
mark_safe used as a decorator leaves the result of a function |
| 83 |
unchanged. |
| 84 |
""" |
| 85 |
|
| 86 |
def clean_string_provider(): |
| 87 |
return "<html><body>dummy</body></html>" |
| 88 |
|
| 89 |
self.assertEqual(mark_safe(clean_string_provider)(), clean_string_provider()) |
| 90 |
|
| 91 |
def test_mark_safe_decorator_does_not_affect_dunder_html(self): |
| 92 |
""" |
| 93 |
mark_safe doesn't affect a callable that has an __html__() method. |
| 94 |
""" |
| 95 |
|
| 96 |
class SafeStringContainer: |
| 97 |
def __html__(self): |
| 98 |
return "<html></html>" |
| 99 |
|
| 100 |
self.assertIs(mark_safe(SafeStringContainer), SafeStringContainer) |
| 101 |
|
| 102 |
def test_mark_safe_decorator_does_not_affect_promises(self): |
| 103 |
""" |
| 104 |
mark_safe doesn't affect lazy strings (Promise objects). |
| 105 |
""" |
| 106 |
|
| 107 |
def html_str(): |
| 108 |
return "<html></html>" |
| 109 |
|
| 110 |
lazy_str = lazy(html_str, str)() |
| 111 |
self.assertEqual(mark_safe(lazy_str), html_str()) |
| 112 |
|
| 113 |
def test_default_additional_attrs(self): |
| 114 |
s = SafeString("a&b") |
| 115 |
msg = "object has no attribute 'dynamic_attr'" |
| 116 |
with self.assertRaisesMessage(AttributeError, msg): |
| 117 |
s.dynamic_attr = True |
| 118 |
|
| 119 |
def test_default_safe_data_additional_attrs(self): |
| 120 |
s = SafeData() |
| 121 |
msg = "object has no attribute 'dynamic_attr'" |
| 122 |
with self.assertRaisesMessage(AttributeError, msg): |
| 123 |
s.dynamic_attr = True |
| 124 |
|
| 125 |
def test_add_str(self): |
| 126 |
s = SafeString("a&b") |
| 127 |
cases = [ |
| 128 |
("test", "a&btest"), |
| 129 |
("<p>unsafe</p>", "a&b<p>unsafe</p>"), |
| 130 |
(SafeString("<p>safe</p>"), SafeString("a&b<p>safe</p>")), |
| 131 |
] |
| 132 |
for case, expected in cases: |
| 133 |
with self.subTest(case=case): |
| 134 |
self.assertRenderEqual("{{ s }}", expected, s=s + case) |
| 135 |
|
| 136 |
def test_add_obj(self): |
| 137 |
|
| 138 |
base_str = "<strong>strange</strong>" |
| 139 |
add_str = "hello</br>" |
| 140 |
|
| 141 |
class Add: |
| 142 |
def __add__(self, other): |
| 143 |
return base_str + other |
| 144 |
|
| 145 |
class AddSafe: |
| 146 |
def __add__(self, other): |
| 147 |
return mark_safe(base_str) + other |
| 148 |
|
| 149 |
class Radd: |
| 150 |
def __radd__(self, other): |
| 151 |
return other + base_str |
| 152 |
|
| 153 |
class RaddSafe: |
| 154 |
def __radd__(self, other): |
| 155 |
return other + mark_safe(base_str) |
| 156 |
|
| 157 |
left_add_expected = f"{base_str}{add_str}" |
| 158 |
right_add_expected = f"{add_str}{base_str}" |
| 159 |
cases = [ |
| 160 |
# Left-add test cases. |
| 161 |
(Add(), add_str, left_add_expected, str), |
| 162 |
(Add(), mark_safe(add_str), left_add_expected, str), |
| 163 |
(AddSafe(), add_str, left_add_expected, str), |
| 164 |
(AddSafe(), mark_safe(add_str), left_add_expected, SafeString), |
| 165 |
# Right-add test cases. |
| 166 |
(add_str, Radd(), right_add_expected, str), |
| 167 |
(mark_safe(add_str), Radd(), right_add_expected, str), |
| 168 |
(add_str, Radd(), right_add_expected, str), |
| 169 |
(mark_safe(add_str), RaddSafe(), right_add_expected, SafeString), |
| 170 |
] |
| 171 |
for lhs, rhs, expected, expected_type in cases: |
| 172 |
with self.subTest(lhs=lhs, rhs=rhs): |
| 173 |
result = lhs + rhs |
| 174 |
self.assertEqual(result, expected) |
| 175 |
self.assertEqual(type(result), expected_type) |
| 176 |
|
| 177 |
cases = [ |
| 178 |
("hello", Add()), |
| 179 |
("hello", AddSafe()), |
| 180 |
(Radd(), "hello"), |
| 181 |
(RaddSafe(), "hello"), |
| 182 |
] |
| 183 |
for lhs, rhs in cases: |
| 184 |
with self.subTest(lhs=lhs, rhs=rhs), self.assertRaises(TypeError): |
| 185 |
lhs + rhs |