Added basic tests for template backends.

This commit is contained in:
Aymeric Augustin 2014-12-14 21:22:57 +01:00
parent 1eca0e95cf
commit 332154e726
12 changed files with 113 additions and 0 deletions

View File

@ -1,5 +1,6 @@
bcrypt bcrypt
docutils docutils
jinja2
# move to py2.txt when dropping Python 3.2 # move to py2.txt when dropping Python 3.2
mock mock
numpy numpy

View File

View File

@ -0,0 +1 @@
Hu ho.

View File

@ -0,0 +1 @@
{{ csrf_input }}

View File

@ -0,0 +1 @@
Hello {{ name }}!

View File

@ -0,0 +1 @@
$csrf_input

View File

@ -0,0 +1 @@
Hello $name!

View File

@ -0,0 +1 @@
{% csrf_token %}

View File

@ -0,0 +1 @@
Hello {{ name }}!

View File

@ -0,0 +1,9 @@
from django.template.backends.django import DjangoTemplates
from .test_dummy import TemplateStringsTests
class DjangoTemplatesTests(TemplateStringsTests):
engine_class = DjangoTemplates
backend_name = 'django'

View File

@ -0,0 +1,70 @@
# coding: utf-8
from __future__ import unicode_literals
from django.http import HttpRequest
from django.middleware.csrf import CsrfViewMiddleware, get_token
from django.template import TemplateDoesNotExist
from django.template.backends.dummy import TemplateStrings
from django.test import SimpleTestCase
class TemplateStringsTests(SimpleTestCase):
engine_class = TemplateStrings
backend_name = 'dummy'
options = {}
@classmethod
def setUpClass(cls):
params = {
'DIRS': [],
'APP_DIRS': True,
'NAME': cls.backend_name,
'OPTIONS': cls.options,
}
cls.engine = cls.engine_class(params)
def test_from_string(self):
template = self.engine.from_string("Hello!\n")
content = template.render()
self.assertEqual(content, "Hello!\n")
def test_get_template(self):
template = self.engine.get_template('template_backends/hello.html')
content = template.render({'name': 'world'})
self.assertEqual(content, "Hello world!\n")
def test_get_template_non_existing(self):
with self.assertRaises(TemplateDoesNotExist):
self.engine.get_template('template_backends/non_existing.html')
def test_html_escaping(self):
template = self.engine.get_template('template_backends/hello.html')
context = {'name': '<script>alert("XSS!");</script>'}
content = template.render(context)
self.assertIn('&lt;script&gt;', content)
self.assertNotIn('<script>', content)
def test_csrf_token(self):
request = HttpRequest()
CsrfViewMiddleware().process_view(request, lambda r: None, (), {})
template = self.engine.get_template('template_backends/csrf.html')
content = template.render(request=request)
expected = (
'<input type="hidden" name="csrfmiddlewaretoken" '
'value="{}" />'.format(get_token(request)))
self.assertHTMLEqual(content, expected)
def test_no_directory_traversal(self):
with self.assertRaises(TemplateDoesNotExist):
self.engine.get_template('../forbidden/template_backends/hello.html')
def test_non_ascii_characters(self):
template = self.engine.get_template('template_backends/hello.html')
content = template.render({'name': 'Jérôme'})
self.assertEqual(content, "Hello Jérôme!\n")

View File

@ -0,0 +1,26 @@
import sys
from unittest import skipIf
# Jinja2 doesn't run on Python 3.2 because it uses u-prefixed unicode strings.
if sys.version_info[:2] == (2, 7) or sys.version_info[:2] >= (3, 3):
try:
import jinja2
except ImportError:
jinja2 = None
Jinja2 = None
else:
from django.template.backends.jinja2 import Jinja2
else:
jinja2 = None
Jinja2 = None
from .test_dummy import TemplateStringsTests
@skipIf(jinja2 is None, "this test requires jinja2")
class Jinja2Tests(TemplateStringsTests):
engine_class = Jinja2
backend_name = 'jinja2'
options = {'keep_trailing_newline': True}