test_safestring.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from __future__ import unicode_literals
  2. from django.template import Context, Template
  3. from django.test import SimpleTestCase
  4. from django.utils import html, six, text
  5. from django.utils.encoding import force_bytes
  6. from django.utils.functional import lazy, lazystr
  7. from django.utils.safestring import (
  8. EscapeData, SafeData, mark_for_escaping, mark_safe,
  9. )
  10. lazybytes = lazy(force_bytes, bytes)
  11. class customescape(six.text_type):
  12. def __html__(self):
  13. # implement specific and obviously wrong escaping
  14. # in order to be able to tell for sure when it runs
  15. return self.replace('<', '<<').replace('>', '>>')
  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. def test_mark_safe(self):
  22. s = mark_safe('a&b')
  23. self.assertRenderEqual('{{ s }}', 'a&b', s=s)
  24. self.assertRenderEqual('{{ s|force_escape }}', 'a&amp;b', s=s)
  25. def test_mark_safe_object_implementing_dunder_html(self):
  26. e = customescape('<a&b>')
  27. s = mark_safe(e)
  28. self.assertIs(s, e)
  29. self.assertRenderEqual('{{ s }}', '<<a&b>>', s=s)
  30. self.assertRenderEqual('{{ s|force_escape }}', '&lt;a&amp;b&gt;', s=s)
  31. def test_mark_safe_lazy(self):
  32. s = lazystr('a&b')
  33. b = lazybytes(b'a&b')
  34. self.assertIsInstance(mark_safe(s), SafeData)
  35. self.assertIsInstance(mark_safe(b), SafeData)
  36. self.assertRenderEqual('{{ s }}', 'a&b', s=mark_safe(s))
  37. def test_mark_safe_object_implementing_dunder_str(self):
  38. class Obj(object):
  39. def __str__(self):
  40. return '<obj>'
  41. s = mark_safe(Obj())
  42. self.assertRenderEqual('{{ s }}', '<obj>', s=s)
  43. def test_mark_safe_result_implements_dunder_html(self):
  44. self.assertEqual(mark_safe('a&b').__html__(), 'a&b')
  45. def test_mark_safe_lazy_result_implements_dunder_html(self):
  46. self.assertEqual(mark_safe(lazystr('a&b')).__html__(), 'a&b')
  47. def test_mark_for_escaping(self):
  48. s = mark_for_escaping('a&b')
  49. self.assertRenderEqual('{{ s }}', 'a&amp;b', s=s)
  50. self.assertRenderEqual('{{ s }}', 'a&amp;b', s=mark_for_escaping(s))
  51. def test_mark_for_escaping_object_implementing_dunder_html(self):
  52. e = customescape('<a&b>')
  53. s = mark_for_escaping(e)
  54. self.assertIs(s, e)
  55. self.assertRenderEqual('{{ s }}', '<<a&b>>', s=s)
  56. self.assertRenderEqual('{{ s|force_escape }}', '&lt;a&amp;b&gt;', s=s)
  57. def test_mark_for_escaping_lazy(self):
  58. s = lazystr('a&b')
  59. b = lazybytes(b'a&b')
  60. self.assertIsInstance(mark_for_escaping(s), EscapeData)
  61. self.assertIsInstance(mark_for_escaping(b), EscapeData)
  62. self.assertRenderEqual('{% autoescape off %}{{ s }}{% endautoescape %}', 'a&amp;b', s=mark_for_escaping(s))
  63. def test_mark_for_escaping_object_implementing_dunder_str(self):
  64. class Obj(object):
  65. def __str__(self):
  66. return '<obj>'
  67. s = mark_for_escaping(Obj())
  68. self.assertRenderEqual('{{ s }}', '&lt;obj&gt;', s=s)
  69. def test_add_lazy_safe_text_and_safe_text(self):
  70. s = html.escape(lazystr('a'))
  71. s += mark_safe('&b')
  72. self.assertRenderEqual('{{ s }}', 'a&b', s=s)
  73. s = html.escapejs(lazystr('a'))
  74. s += mark_safe('&b')
  75. self.assertRenderEqual('{{ s }}', 'a&b', s=s)
  76. s = text.slugify(lazystr('a'))
  77. s += mark_safe('&b')
  78. self.assertRenderEqual('{{ s }}', 'a&b', s=s)