2
0

test_dummy.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from django.forms import CharField, Form, Media
  5. from django.http import HttpRequest
  6. from django.middleware.csrf import (
  7. CsrfViewMiddleware, _compare_salted_tokens as equivalent_tokens, get_token,
  8. )
  9. from django.template import TemplateDoesNotExist, TemplateSyntaxError
  10. from django.template.backends.dummy import TemplateStrings
  11. from django.test import SimpleTestCase
  12. class TemplateStringsTests(SimpleTestCase):
  13. engine_class = TemplateStrings
  14. backend_name = 'dummy'
  15. options = {}
  16. @classmethod
  17. def setUpClass(cls):
  18. super(TemplateStringsTests, cls).setUpClass()
  19. params = {
  20. 'DIRS': [],
  21. 'APP_DIRS': True,
  22. 'NAME': cls.backend_name,
  23. 'OPTIONS': cls.options,
  24. }
  25. cls.engine = cls.engine_class(params)
  26. def test_from_string(self):
  27. template = self.engine.from_string("Hello!\n")
  28. content = template.render()
  29. self.assertEqual(content, "Hello!\n")
  30. def test_get_template(self):
  31. template = self.engine.get_template('template_backends/hello.html')
  32. content = template.render({'name': 'world'})
  33. self.assertEqual(content, "Hello world!\n")
  34. def test_get_template_non_existing(self):
  35. with self.assertRaises(TemplateDoesNotExist) as e:
  36. self.engine.get_template('template_backends/non_existing.html')
  37. self.assertEqual(e.exception.backend, self.engine)
  38. def test_get_template_syntax_error(self):
  39. # There's no way to trigger a syntax error with the dummy backend.
  40. # The test still lives here to factor it between other backends.
  41. if self.backend_name == 'dummy':
  42. self.skipTest("test doesn't apply to dummy backend")
  43. with self.assertRaises(TemplateSyntaxError):
  44. self.engine.get_template('template_backends/syntax_error.html')
  45. def test_html_escaping(self):
  46. template = self.engine.get_template('template_backends/hello.html')
  47. context = {'name': '<script>alert("XSS!");</script>'}
  48. content = template.render(context)
  49. self.assertIn('&lt;script&gt;', content)
  50. self.assertNotIn('<script>', content)
  51. def test_django_html_escaping(self):
  52. if self.backend_name == 'dummy':
  53. self.skipTest("test doesn't apply to dummy backend")
  54. class TestForm(Form):
  55. test_field = CharField()
  56. media = Media(js=['my-script.js'])
  57. form = TestForm()
  58. template = self.engine.get_template('template_backends/django_escaping.html')
  59. content = template.render({'media': media, 'test_form': form})
  60. expected = '{}\n\n{}\n\n{}'.format(media, form, form['test_field'])
  61. self.assertHTMLEqual(content, expected)
  62. def test_csrf_token(self):
  63. request = HttpRequest()
  64. CsrfViewMiddleware().process_view(request, lambda r: None, (), {})
  65. template = self.engine.get_template('template_backends/csrf.html')
  66. content = template.render(request=request)
  67. expected = '<input type="hidden" name="csrfmiddlewaretoken" value="([^"]+)" />'
  68. match = re.match(expected, content) or re.match(expected.replace('"', "'"), content)
  69. self.assertTrue(match, "hidden csrftoken field not found in output")
  70. self.assertTrue(equivalent_tokens(match.group(1), get_token(request)))
  71. def test_no_directory_traversal(self):
  72. with self.assertRaises(TemplateDoesNotExist):
  73. self.engine.get_template('../forbidden/template_backends/hello.html')
  74. def test_non_ascii_characters(self):
  75. template = self.engine.get_template('template_backends/hello.html')
  76. content = template.render({'name': 'Jérôme'})
  77. self.assertEqual(content, "Hello Jérôme!\n")