test_safestring.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. from __future__ import unicode_literals
  2. from django.template import Context, Template
  3. from django.test import SimpleTestCase, ignore_warnings
  4. from django.utils import html, six, text
  5. from django.utils.deprecation import RemovedInDjango20Warning
  6. from django.utils.encoding import force_bytes
  7. from django.utils.functional import lazy, lazystr
  8. from django.utils.safestring import (
  9. EscapeData, SafeData, mark_for_escaping, mark_safe,
  10. )
  11. lazybytes = lazy(force_bytes, bytes)
  12. class customescape(six.text_type):
  13. def __html__(self):
  14. # implement specific and obviously wrong escaping
  15. # in order to be able to tell for sure when it runs
  16. return self.replace('<', '<<').replace('>', '>>')
  17. class SafeStringTest(SimpleTestCase):
  18. def assertRenderEqual(self, tpl, expected, **context):
  19. context = Context(context)
  20. tpl = Template(tpl)
  21. self.assertEqual(tpl.render(context), expected)
  22. def test_mark_safe(self):
  23. s = mark_safe('a&b')
  24. self.assertRenderEqual('{{ s }}', 'a&b', s=s)
  25. self.assertRenderEqual('{{ s|force_escape }}', 'a&amp;b', s=s)
  26. def test_mark_safe_object_implementing_dunder_html(self):
  27. e = customescape('<a&b>')
  28. s = mark_safe(e)
  29. self.assertIs(s, e)
  30. self.assertRenderEqual('{{ s }}', '<<a&b>>', s=s)
  31. self.assertRenderEqual('{{ s|force_escape }}', '&lt;a&amp;b&gt;', s=s)
  32. def test_mark_safe_lazy(self):
  33. s = lazystr('a&b')
  34. b = lazybytes(b'a&b')
  35. self.assertIsInstance(mark_safe(s), SafeData)
  36. self.assertIsInstance(mark_safe(b), SafeData)
  37. self.assertRenderEqual('{{ s }}', 'a&b', s=mark_safe(s))
  38. def test_mark_safe_object_implementing_dunder_str(self):
  39. class Obj(object):
  40. def __str__(self):
  41. return '<obj>'
  42. s = mark_safe(Obj())
  43. self.assertRenderEqual('{{ s }}', '<obj>', s=s)
  44. def test_mark_safe_result_implements_dunder_html(self):
  45. self.assertEqual(mark_safe('a&b').__html__(), 'a&b')
  46. def test_mark_safe_lazy_result_implements_dunder_html(self):
  47. self.assertEqual(mark_safe(lazystr('a&b')).__html__(), 'a&b')
  48. @ignore_warnings(category=RemovedInDjango20Warning)
  49. def test_mark_for_escaping(self):
  50. s = mark_for_escaping('a&b')
  51. self.assertRenderEqual('{{ s }}', 'a&amp;b', s=s)
  52. self.assertRenderEqual('{{ s }}', 'a&amp;b', s=mark_for_escaping(s))
  53. @ignore_warnings(category=RemovedInDjango20Warning)
  54. def test_mark_for_escaping_object_implementing_dunder_html(self):
  55. e = customescape('<a&b>')
  56. s = mark_for_escaping(e)
  57. self.assertIs(s, e)
  58. self.assertRenderEqual('{{ s }}', '<<a&b>>', s=s)
  59. self.assertRenderEqual('{{ s|force_escape }}', '&lt;a&amp;b&gt;', s=s)
  60. @ignore_warnings(category=RemovedInDjango20Warning)
  61. def test_mark_for_escaping_lazy(self):
  62. s = lazystr('a&b')
  63. b = lazybytes(b'a&b')
  64. self.assertIsInstance(mark_for_escaping(s), EscapeData)
  65. self.assertIsInstance(mark_for_escaping(b), EscapeData)
  66. self.assertRenderEqual('{% autoescape off %}{{ s }}{% endautoescape %}', 'a&amp;b', s=mark_for_escaping(s))
  67. @ignore_warnings(category=RemovedInDjango20Warning)
  68. def test_mark_for_escaping_object_implementing_dunder_str(self):
  69. class Obj(object):
  70. def __str__(self):
  71. return '<obj>'
  72. s = mark_for_escaping(Obj())
  73. self.assertRenderEqual('{{ s }}', '&lt;obj&gt;', s=s)
  74. def test_add_lazy_safe_text_and_safe_text(self):
  75. s = html.escape(lazystr('a'))
  76. s += mark_safe('&b')
  77. self.assertRenderEqual('{{ s }}', 'a&b', s=s)
  78. s = html.escapejs(lazystr('a'))
  79. s += mark_safe('&b')
  80. self.assertRenderEqual('{{ s }}', 'a&b', s=s)
  81. s = text.slugify(lazystr('a'))
  82. s += mark_safe('&b')
  83. self.assertRenderEqual('{{ s }}', 'a&b', s=s)
  84. def test_mark_safe_as_decorator(self):
  85. """
  86. mark_safe used as a decorator leaves the result of a function
  87. unchanged.
  88. """
  89. def clean_string_provider():
  90. return '<html><body>dummy</body></html>'
  91. self.assertEqual(mark_safe(clean_string_provider)(), clean_string_provider())
  92. def test_mark_safe_decorator_does_not_affect_dunder_html(self):
  93. """
  94. mark_safe doesn't affect a callable that has an __html__() method.
  95. """
  96. class SafeStringContainer:
  97. def __html__(self):
  98. return '<html></html>'
  99. self.assertIs(mark_safe(SafeStringContainer), SafeStringContainer)
  100. def test_mark_safe_decorator_does_not_affect_promises(self):
  101. """
  102. mark_safe doesn't affect lazy strings (Promise objects).
  103. """
  104. def html_str():
  105. return '<html></html>'
  106. lazy_str = lazy(html_str, str)()
  107. self.assertEqual(mark_safe(lazy_str), html_str())