2006-12-05 19:48:46 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-07-29 17:19:04 +00:00
|
|
|
from __future__ import unicode_literals
|
2011-09-16 01:16:25 +00:00
|
|
|
|
2007-07-23 04:45:01 +00:00
|
|
|
import os
|
2009-03-31 07:23:50 +00:00
|
|
|
import sys
|
2013-07-01 12:22:27 +00:00
|
|
|
import unittest
|
2007-07-23 04:45:01 +00:00
|
|
|
|
2006-08-27 13:59:47 +00:00
|
|
|
from django import template
|
2014-07-21 17:38:34 +00:00
|
|
|
from django.contrib.auth.models import Group
|
2008-08-05 14:16:13 +00:00
|
|
|
from django.core import urlresolvers
|
2014-11-10 20:40:26 +00:00
|
|
|
from django.template import (base as template_base, loader,
|
|
|
|
Context, RequestContext, Template, TemplateSyntaxError)
|
2014-11-19 22:23:58 +00:00
|
|
|
from django.template.engine import Engine
|
2014-11-15 19:58:26 +00:00
|
|
|
from django.template.loaders import app_directories, filesystem
|
2014-12-03 20:36:17 +00:00
|
|
|
from django.test import RequestFactory, SimpleTestCase
|
2014-11-15 19:41:30 +00:00
|
|
|
from django.test.utils import override_settings, extend_sys_path
|
2012-12-08 10:13:52 +00:00
|
|
|
from django.utils._os import upath
|
2006-08-27 13:59:47 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2014-12-17 21:10:57 +00:00
|
|
|
TESTS_DIR = os.path.dirname(os.path.dirname(os.path.abspath(upath(__file__))))
|
|
|
|
|
|
|
|
TEMPLATES_DIR = os.path.join(TESTS_DIR, 'templates')
|
|
|
|
|
|
|
|
|
2014-12-03 20:36:17 +00:00
|
|
|
class TemplateLoaderTests(SimpleTestCase):
|
2010-11-17 15:36:26 +00:00
|
|
|
|
2007-07-23 04:45:01 +00:00
|
|
|
def test_loaders_security(self):
|
2014-11-19 22:23:58 +00:00
|
|
|
ad_loader = app_directories.Loader(Engine.get_default())
|
|
|
|
fs_loader = filesystem.Loader(Engine.get_default())
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2007-07-23 04:45:01 +00:00
|
|
|
def test_template_sources(path, template_dirs, expected_sources):
|
2008-10-06 06:34:54 +00:00
|
|
|
if isinstance(expected_sources, list):
|
2011-05-22 23:56:42 +00:00
|
|
|
# Fix expected sources so they are abspathed
|
|
|
|
expected_sources = [os.path.abspath(s) for s in expected_sources]
|
2008-10-06 06:34:54 +00:00
|
|
|
# Test the two loaders (app_directores and filesystem).
|
2009-12-14 12:08:23 +00:00
|
|
|
func1 = lambda p, t: list(ad_loader.get_template_sources(p, t))
|
|
|
|
func2 = lambda p, t: list(fs_loader.get_template_sources(p, t))
|
2008-10-06 06:34:54 +00:00
|
|
|
for func in (func1, func2):
|
|
|
|
if isinstance(expected_sources, list):
|
|
|
|
self.assertEqual(func(path, template_dirs), expected_sources)
|
|
|
|
else:
|
|
|
|
self.assertRaises(expected_sources, func, path, template_dirs)
|
2007-07-23 04:45:01 +00:00
|
|
|
|
|
|
|
template_dirs = ['/dir1', '/dir2']
|
|
|
|
test_template_sources('index.html', template_dirs,
|
|
|
|
['/dir1/index.html', '/dir2/index.html'])
|
2008-10-06 06:34:54 +00:00
|
|
|
test_template_sources('/etc/passwd', template_dirs, [])
|
2007-07-23 04:45:01 +00:00
|
|
|
test_template_sources('etc/passwd', template_dirs,
|
|
|
|
['/dir1/etc/passwd', '/dir2/etc/passwd'])
|
2008-10-06 06:34:54 +00:00
|
|
|
test_template_sources('../etc/passwd', template_dirs, [])
|
|
|
|
test_template_sources('../../../etc/passwd', template_dirs, [])
|
2007-07-23 04:45:01 +00:00
|
|
|
test_template_sources('/dir1/index.html', template_dirs,
|
|
|
|
['/dir1/index.html'])
|
|
|
|
test_template_sources('../dir2/index.html', template_dirs,
|
|
|
|
['/dir2/index.html'])
|
2008-10-06 06:34:54 +00:00
|
|
|
test_template_sources('/dir1blah', template_dirs, [])
|
|
|
|
test_template_sources('../dir1blah', template_dirs, [])
|
|
|
|
|
|
|
|
# UTF-8 bytestrings are permitted.
|
2012-05-19 15:43:34 +00:00
|
|
|
test_template_sources(b'\xc3\x85ngstr\xc3\xb6m', template_dirs,
|
2012-06-07 16:08:47 +00:00
|
|
|
['/dir1/Ångström', '/dir2/Ångström'])
|
2008-10-06 06:34:54 +00:00
|
|
|
# Unicode strings are permitted.
|
2012-06-07 16:08:47 +00:00
|
|
|
test_template_sources('Ångström', template_dirs,
|
|
|
|
['/dir1/Ångström', '/dir2/Ångström'])
|
|
|
|
test_template_sources('Ångström', [b'/Stra\xc3\x9fe'], ['/Straße/Ångström'])
|
2012-05-19 15:43:34 +00:00
|
|
|
test_template_sources(b'\xc3\x85ngstr\xc3\xb6m', [b'/Stra\xc3\x9fe'],
|
2012-06-07 16:08:47 +00:00
|
|
|
['/Straße/Ångström'])
|
2008-10-06 06:34:54 +00:00
|
|
|
# Invalid UTF-8 encoding in bytestrings is not. Should raise a
|
|
|
|
# semi-useful error message.
|
2012-05-19 15:43:34 +00:00
|
|
|
test_template_sources(b'\xc3\xc3', template_dirs, UnicodeDecodeError)
|
2007-07-23 04:45:01 +00:00
|
|
|
|
|
|
|
# Case insensitive tests (for win32). Not run unless we're on
|
|
|
|
# a case insensitive operating system.
|
|
|
|
if os.path.normcase('/TEST') == os.path.normpath('/test'):
|
|
|
|
template_dirs = ['/dir1', '/DIR2']
|
|
|
|
test_template_sources('index.html', template_dirs,
|
2011-05-22 23:56:42 +00:00
|
|
|
['/dir1/index.html', '/DIR2/index.html'])
|
2007-07-23 04:45:01 +00:00
|
|
|
test_template_sources('/DIR1/index.HTML', template_dirs,
|
2011-05-22 23:56:42 +00:00
|
|
|
['/DIR1/index.HTML'])
|
2007-07-23 04:45:01 +00:00
|
|
|
|
2014-12-17 21:10:57 +00:00
|
|
|
@override_settings(TEMPLATES=[{
|
|
|
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
|
|
|
'DIRS': [TEMPLATES_DIR],
|
|
|
|
}])
|
2013-10-14 13:14:17 +00:00
|
|
|
# Turn TEMPLATE_DEBUG on, so that the origin file name will be kept with
|
|
|
|
# the compiled templates.
|
|
|
|
@override_settings(TEMPLATE_DEBUG=True)
|
2010-03-01 23:05:35 +00:00
|
|
|
def test_loader_debug_origin(self):
|
2013-12-12 09:59:05 +00:00
|
|
|
load_name = 'login.html'
|
2014-11-15 19:58:26 +00:00
|
|
|
|
2014-12-17 21:51:42 +00:00
|
|
|
# We rely on the fact the file system and app directories loaders both
|
|
|
|
# inherit the load_template method from the base Loader class, so we
|
|
|
|
# only need to test one of them.
|
2014-11-28 21:13:11 +00:00
|
|
|
template = loader.get_template(load_name).template
|
2013-12-12 09:59:05 +00:00
|
|
|
template_name = template.nodelist[0].source[0].name
|
|
|
|
self.assertTrue(template_name.endswith(load_name),
|
|
|
|
'Template loaded by filesystem loader has incorrect name for debug page: %s' % template_name)
|
|
|
|
|
2014-12-17 21:10:57 +00:00
|
|
|
@override_settings(TEMPLATES=[{
|
|
|
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
|
|
|
'DIRS': [TEMPLATES_DIR],
|
|
|
|
'OPTIONS': {
|
|
|
|
'loaders': [
|
|
|
|
('django.template.loaders.cached.Loader', [
|
|
|
|
'django.template.loaders.filesystem.Loader',
|
|
|
|
]),
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}])
|
2014-11-15 19:58:26 +00:00
|
|
|
@override_settings(TEMPLATE_DEBUG=True)
|
|
|
|
def test_cached_loader_debug_origin(self):
|
|
|
|
load_name = 'login.html'
|
2013-12-12 09:59:05 +00:00
|
|
|
|
2014-11-15 19:58:26 +00:00
|
|
|
# Test the cached loader separately since it overrides load_template.
|
2014-11-28 21:13:11 +00:00
|
|
|
template = loader.get_template(load_name).template
|
2013-12-12 09:59:05 +00:00
|
|
|
template_name = template.nodelist[0].source[0].name
|
|
|
|
self.assertTrue(template_name.endswith(load_name),
|
|
|
|
'Template loaded through cached loader has incorrect name for debug page: %s' % template_name)
|
|
|
|
|
2014-11-28 21:13:11 +00:00
|
|
|
template = loader.get_template(load_name).template
|
2013-12-12 09:59:05 +00:00
|
|
|
template_name = template.nodelist[0].source[0].name
|
|
|
|
self.assertTrue(template_name.endswith(load_name),
|
|
|
|
'Cached template loaded through cached loader has incorrect name for debug page: %s' % template_name)
|
2010-03-16 14:34:57 +00:00
|
|
|
|
2014-11-15 19:58:26 +00:00
|
|
|
@override_settings(TEMPLATE_DEBUG=True)
|
2013-08-30 19:08:40 +00:00
|
|
|
def test_loader_origin(self):
|
2014-11-15 19:58:26 +00:00
|
|
|
template = loader.get_template('login.html')
|
|
|
|
self.assertEqual(template.origin.loadname, 'login.html')
|
2013-08-30 19:08:40 +00:00
|
|
|
|
2014-11-15 19:58:26 +00:00
|
|
|
@override_settings(TEMPLATE_DEBUG=True)
|
2013-08-30 19:08:40 +00:00
|
|
|
def test_string_origin(self):
|
2014-11-15 19:58:26 +00:00
|
|
|
template = Template('string template')
|
|
|
|
self.assertEqual(template.origin.source, 'string template')
|
2013-08-30 19:08:40 +00:00
|
|
|
|
|
|
|
def test_debug_false_origin(self):
|
|
|
|
template = loader.get_template('login.html')
|
|
|
|
self.assertEqual(template.origin, None)
|
2011-03-03 00:41:40 +00:00
|
|
|
|
2013-10-14 13:14:17 +00:00
|
|
|
# TEMPLATE_DEBUG must be true, otherwise the exception raised
|
|
|
|
# during {% include %} processing will be suppressed.
|
|
|
|
@override_settings(TEMPLATE_DEBUG=True)
|
2013-12-12 09:59:05 +00:00
|
|
|
# Test the base loader class via the app loader. load_template
|
|
|
|
# from base is used by all shipped loaders excepting cached,
|
|
|
|
# which has its own test.
|
2014-12-17 21:10:57 +00:00
|
|
|
@override_settings(TEMPLATES=[{
|
|
|
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
|
|
|
'APP_DIRS': True,
|
|
|
|
}])
|
2011-03-03 00:41:40 +00:00
|
|
|
def test_include_missing_template(self):
|
|
|
|
"""
|
|
|
|
Tests that the correct template is identified as not existing
|
|
|
|
when {% include %} specifies a template that does not exist.
|
|
|
|
"""
|
2013-12-12 09:59:05 +00:00
|
|
|
load_name = 'test_include_error.html'
|
|
|
|
r = None
|
2011-03-03 00:41:40 +00:00
|
|
|
try:
|
2013-12-12 09:59:05 +00:00
|
|
|
tmpl = loader.select_template([load_name])
|
|
|
|
r = tmpl.render(template.Context({}))
|
|
|
|
except template.TemplateDoesNotExist as e:
|
|
|
|
self.assertEqual(e.args[0], 'missing.html')
|
|
|
|
self.assertEqual(r, None, 'Template rendering unexpectedly succeeded, produced: ->%r<-' % r)
|
2011-03-03 00:41:40 +00:00
|
|
|
|
2013-10-14 13:14:17 +00:00
|
|
|
# TEMPLATE_DEBUG must be true, otherwise the exception raised
|
|
|
|
# during {% include %} processing will be suppressed.
|
|
|
|
@override_settings(TEMPLATE_DEBUG=True)
|
2013-12-12 09:59:05 +00:00
|
|
|
# Test the base loader class via the app loader. load_template
|
|
|
|
# from base is used by all shipped loaders excepting cached,
|
|
|
|
# which has its own test.
|
2014-12-17 21:10:57 +00:00
|
|
|
@override_settings(TEMPLATES=[{
|
|
|
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
|
|
|
'APP_DIRS': True,
|
|
|
|
}])
|
2010-03-16 14:34:57 +00:00
|
|
|
def test_extends_include_missing_baseloader(self):
|
|
|
|
"""
|
2010-03-30 12:44:30 +00:00
|
|
|
Tests that the correct template is identified as not existing
|
|
|
|
when {% extends %} specifies a template that does exist, but
|
2010-03-16 14:34:57 +00:00
|
|
|
that template has an {% include %} of something that does not
|
|
|
|
exist. See #12787.
|
|
|
|
"""
|
2013-12-12 09:59:05 +00:00
|
|
|
load_name = 'test_extends_error.html'
|
|
|
|
tmpl = loader.get_template(load_name)
|
|
|
|
r = None
|
2010-03-16 14:34:57 +00:00
|
|
|
try:
|
2013-12-12 09:59:05 +00:00
|
|
|
r = tmpl.render(template.Context({}))
|
|
|
|
except template.TemplateDoesNotExist as e:
|
|
|
|
self.assertEqual(e.args[0], 'missing.html')
|
|
|
|
self.assertEqual(r, None, 'Template rendering unexpectedly succeeded, produced: ->%r<-' % r)
|
2010-03-16 14:34:57 +00:00
|
|
|
|
2014-12-17 21:10:57 +00:00
|
|
|
@override_settings(TEMPLATES=[{
|
|
|
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
|
|
|
'OPTIONS': {
|
|
|
|
'loaders': [
|
|
|
|
('django.template.loaders.cached.Loader', [
|
|
|
|
'django.template.loaders.app_directories.Loader',
|
|
|
|
]),
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}])
|
2013-10-14 13:14:17 +00:00
|
|
|
@override_settings(TEMPLATE_DEBUG=True)
|
2010-03-16 14:34:57 +00:00
|
|
|
def test_extends_include_missing_cachedloader(self):
|
|
|
|
"""
|
2010-03-30 12:44:30 +00:00
|
|
|
Same as test_extends_include_missing_baseloader, only tests
|
2014-11-15 17:35:02 +00:00
|
|
|
behavior of the cached loader instead of base loader.
|
2010-03-16 14:34:57 +00:00
|
|
|
"""
|
2014-12-17 21:10:57 +00:00
|
|
|
load_name = 'test_extends_error.html'
|
|
|
|
tmpl = loader.get_template(load_name)
|
|
|
|
r = None
|
|
|
|
try:
|
|
|
|
r = tmpl.render(template.Context({}))
|
|
|
|
except template.TemplateDoesNotExist as e:
|
|
|
|
self.assertEqual(e.args[0], 'missing.html')
|
|
|
|
self.assertEqual(r, None, 'Template rendering unexpectedly succeeded, produced: ->%r<-' % r)
|
|
|
|
|
|
|
|
# For the cached loader, repeat the test, to ensure the first attempt did not cache a
|
|
|
|
# result that behaves incorrectly on subsequent attempts.
|
|
|
|
tmpl = loader.get_template(load_name)
|
|
|
|
try:
|
|
|
|
tmpl.render(template.Context({}))
|
|
|
|
except template.TemplateDoesNotExist as e:
|
|
|
|
self.assertEqual(e.args[0], 'missing.html')
|
|
|
|
self.assertEqual(r, None, 'Template rendering unexpectedly succeeded, produced: ->%r<-' % r)
|
2010-03-01 23:05:35 +00:00
|
|
|
|
2013-08-28 12:17:20 +00:00
|
|
|
def test_include_template_argument(self):
|
|
|
|
"""
|
|
|
|
Support any render() supporting object
|
|
|
|
"""
|
|
|
|
ctx = Context({
|
|
|
|
'tmpl': Template('This worked!'),
|
|
|
|
})
|
|
|
|
outer_tmpl = Template('{% include tmpl %}')
|
|
|
|
output = outer_tmpl.render(ctx)
|
|
|
|
self.assertEqual(output, 'This worked!')
|
|
|
|
|
2013-08-29 08:58:56 +00:00
|
|
|
@override_settings(TEMPLATE_DEBUG=True)
|
|
|
|
def test_include_immediate_missing(self):
|
|
|
|
"""
|
|
|
|
Regression test for #16417 -- {% include %} tag raises TemplateDoesNotExist at compile time if TEMPLATE_DEBUG is True
|
|
|
|
|
|
|
|
Test that an {% include %} tag with a literal string referencing a
|
|
|
|
template that does not exist does not raise an exception at parse
|
|
|
|
time.
|
|
|
|
"""
|
|
|
|
tmpl = Template('{% include "this_does_not_exist.html" %}')
|
|
|
|
self.assertIsInstance(tmpl, Template)
|
|
|
|
|
|
|
|
@override_settings(TEMPLATE_DEBUG=True)
|
|
|
|
def test_include_recursive(self):
|
|
|
|
comments = [
|
|
|
|
{
|
|
|
|
'comment': 'A1',
|
|
|
|
'children': [
|
|
|
|
{'comment': 'B1', 'children': []},
|
|
|
|
{'comment': 'B2', 'children': []},
|
|
|
|
{'comment': 'B3', 'children': [
|
|
|
|
{'comment': 'C1', 'children': []}
|
|
|
|
]},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
t = loader.get_template('recursive_include.html')
|
|
|
|
self.assertEqual(
|
|
|
|
"Recursion! A1 Recursion! B1 B2 B3 Recursion! C1",
|
|
|
|
t.render(Context({'comments': comments})).replace(' ', '').replace('\n', ' ').strip(),
|
|
|
|
)
|
|
|
|
|
2013-06-06 12:27:00 +00:00
|
|
|
|
2014-12-03 20:36:17 +00:00
|
|
|
class TemplateRegressionTests(SimpleTestCase):
|
2013-06-06 12:27:00 +00:00
|
|
|
|
2008-08-31 18:28:06 +00:00
|
|
|
def test_token_smart_split(self):
|
|
|
|
# Regression test for #7027
|
2014-11-10 20:40:26 +00:00
|
|
|
token = template_base.Token(template_base.TOKEN_BLOCK, 'sometag _("Page not found") value|yesno:_("yes,no")')
|
2008-08-31 18:28:06 +00:00
|
|
|
split = token.split_contents()
|
|
|
|
self.assertEqual(split, ["sometag", '_("Page not found")', 'value|yesno:_("yes,no")'])
|
|
|
|
|
2011-09-16 01:16:25 +00:00
|
|
|
@override_settings(SETTINGS_MODULE=None, TEMPLATE_DEBUG=True)
|
2009-04-01 22:46:46 +00:00
|
|
|
def test_url_reverse_no_settings_module(self):
|
2009-04-04 19:34:52 +00:00
|
|
|
# Regression test for #9005
|
2009-04-01 22:46:46 +00:00
|
|
|
t = Template('{% url will_not_match %}')
|
|
|
|
c = Context()
|
2011-09-16 01:16:25 +00:00
|
|
|
with self.assertRaises(urlresolvers.NoReverseMatch):
|
|
|
|
t.render(c)
|
|
|
|
|
2014-12-14 22:13:03 +00:00
|
|
|
@override_settings(
|
|
|
|
TEMPLATES=[{
|
|
|
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
|
|
|
'OPTIONS': {'string_if_invalid': '%s is invalid'},
|
|
|
|
}],
|
|
|
|
SETTINGS_MODULE='also_something',
|
|
|
|
)
|
2013-02-23 16:41:30 +00:00
|
|
|
def test_url_reverse_view_name(self):
|
|
|
|
# Regression test for #19827
|
|
|
|
t = Template('{% url will_not_match %}')
|
|
|
|
c = Context()
|
|
|
|
try:
|
|
|
|
t.render(c)
|
|
|
|
except urlresolvers.NoReverseMatch:
|
|
|
|
tb = sys.exc_info()[2]
|
|
|
|
depth = 0
|
|
|
|
while tb.tb_next is not None:
|
|
|
|
tb = tb.tb_next
|
|
|
|
depth += 1
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertGreater(depth, 5,
|
2013-02-23 16:41:30 +00:00
|
|
|
"The traceback context was lost when reraising the traceback. See #19827")
|
|
|
|
|
2012-04-10 20:49:45 +00:00
|
|
|
@override_settings(DEBUG=True, TEMPLATE_DEBUG=True)
|
2011-09-16 01:16:25 +00:00
|
|
|
def test_no_wrapped_exception(self):
|
|
|
|
"""
|
|
|
|
The template system doesn't wrap exceptions, but annotates them.
|
|
|
|
Refs #16770
|
|
|
|
"""
|
|
|
|
c = Context({"coconuts": lambda: 42 / 0})
|
|
|
|
t = Template("{{ coconuts }}")
|
|
|
|
with self.assertRaises(ZeroDivisionError) as cm:
|
|
|
|
t.render(c)
|
|
|
|
|
|
|
|
self.assertEqual(cm.exception.django_template_source[1], (0, 14))
|
2009-04-04 19:34:52 +00:00
|
|
|
|
2010-02-21 23:38:33 +00:00
|
|
|
def test_invalid_block_suggestion(self):
|
|
|
|
# See #7876
|
|
|
|
try:
|
2013-10-19 12:31:38 +00:00
|
|
|
Template("{% if 1 %}lala{% endblock %}{% endif %}")
|
2012-04-28 16:09:37 +00:00
|
|
|
except TemplateSyntaxError as e:
|
2011-12-09 22:13:27 +00:00
|
|
|
self.assertEqual(e.args[0], "Invalid block tag: 'endblock', expected 'elif', 'else' or 'endif'")
|
2010-02-21 23:38:33 +00:00
|
|
|
|
2013-02-23 12:13:44 +00:00
|
|
|
def test_ifchanged_concurrency(self):
|
|
|
|
# Tests for #15849
|
|
|
|
template = Template('[0{% for x in foo %},{% with var=get_value %}{% ifchanged %}{{ var }}{% endifchanged %}{% endwith %}{% endfor %}]')
|
|
|
|
|
|
|
|
# Using generator to mimic concurrency.
|
|
|
|
# The generator is not passed to the 'for' loop, because it does a list(values)
|
|
|
|
# instead, call gen.next() in the template to control the generator.
|
|
|
|
def gen():
|
|
|
|
yield 1
|
|
|
|
yield 2
|
|
|
|
# Simulate that another thread is now rendering.
|
|
|
|
# When the IfChangeNode stores state at 'self' it stays at '3' and skip the last yielded value below.
|
|
|
|
iter2 = iter([1, 2, 3])
|
|
|
|
output2 = template.render(Context({'foo': range(3), 'get_value': lambda: next(iter2)}))
|
2014-11-27 00:41:27 +00:00
|
|
|
self.assertEqual(output2, '[0,1,2,3]', 'Expected [0,1,2,3] in second parallel template, got {}'.format(output2))
|
2013-02-23 12:13:44 +00:00
|
|
|
yield 3
|
|
|
|
|
|
|
|
gen1 = gen()
|
|
|
|
output1 = template.render(Context({'foo': range(3), 'get_value': lambda: next(gen1)}))
|
2014-11-27 00:41:27 +00:00
|
|
|
self.assertEqual(output1, '[0,1,2,3]', 'Expected [0,1,2,3] in first template, got {}'.format(output1))
|
2013-02-23 12:13:44 +00:00
|
|
|
|
2013-03-25 08:10:32 +00:00
|
|
|
def test_cache_regression_20130(self):
|
|
|
|
t = Template('{% load cache %}{% cache 1 regression_20130 %}foo{% endcache %}')
|
|
|
|
cachenode = t.nodelist[1]
|
|
|
|
self.assertEqual(cachenode.fragment_name, 'regression_20130')
|
|
|
|
|
2013-10-03 21:36:21 +00:00
|
|
|
@override_settings(CACHES={
|
|
|
|
'default': {
|
|
|
|
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
|
|
|
|
'LOCATION': 'default',
|
|
|
|
},
|
|
|
|
'template_fragments': {
|
|
|
|
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
|
|
|
|
'LOCATION': 'fragments',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
def test_cache_fragment_cache(self):
|
|
|
|
"""
|
|
|
|
When a cache called "template_fragments" is present, the cache tag
|
|
|
|
will use it in preference to 'default'
|
|
|
|
"""
|
|
|
|
t1 = Template('{% load cache %}{% cache 1 fragment %}foo{% endcache %}')
|
|
|
|
t2 = Template('{% load cache %}{% cache 1 fragment using="default" %}bar{% endcache %}')
|
|
|
|
|
|
|
|
ctx = Context()
|
|
|
|
o1 = t1.render(ctx)
|
|
|
|
o2 = t2.render(ctx)
|
|
|
|
|
|
|
|
self.assertEqual(o1, 'foo')
|
2013-10-18 22:49:24 +00:00
|
|
|
self.assertEqual(o2, 'bar')
|
2013-10-03 21:36:21 +00:00
|
|
|
|
|
|
|
def test_cache_missing_backend(self):
|
|
|
|
"""
|
|
|
|
When a cache that doesn't exist is specified, the cache tag will
|
|
|
|
raise a TemplateSyntaxError
|
|
|
|
'"""
|
|
|
|
t = Template('{% load cache %}{% cache 1 backend using="unknown" %}bar{% endcache %}')
|
|
|
|
|
|
|
|
ctx = Context()
|
|
|
|
with self.assertRaises(TemplateSyntaxError):
|
|
|
|
t.render(ctx)
|
|
|
|
|
2013-02-24 15:49:28 +00:00
|
|
|
def test_ifchanged_render_once(self):
|
|
|
|
""" Test for ticket #19890. The content of ifchanged template tag was
|
|
|
|
rendered twice."""
|
2014-03-27 20:24:19 +00:00
|
|
|
template = Template('{% ifchanged %}{% cycle "1st time" "2nd time" %}{% endifchanged %}')
|
2013-02-24 21:47:14 +00:00
|
|
|
output = template.render(Context({}))
|
|
|
|
self.assertEqual(output, '1st time')
|
2013-02-24 15:49:28 +00:00
|
|
|
|
2013-05-30 07:25:58 +00:00
|
|
|
def test_super_errors(self):
|
|
|
|
"""
|
|
|
|
Test behavior of the raise errors into included blocks.
|
|
|
|
See #18169
|
|
|
|
"""
|
|
|
|
t = loader.get_template('included_content.html')
|
|
|
|
with self.assertRaises(urlresolvers.NoReverseMatch):
|
|
|
|
t.render(Context({}))
|
|
|
|
|
2014-07-21 17:38:34 +00:00
|
|
|
def test_debug_tag_non_ascii(self):
|
|
|
|
"""
|
|
|
|
Test non-ASCII model representation in debug output (#23060).
|
|
|
|
"""
|
|
|
|
Group.objects.create(name="清風")
|
|
|
|
c1 = Context({"objs": Group.objects.all()})
|
|
|
|
t1 = Template('{% debug %}')
|
|
|
|
self.assertIn("清風", t1.render(c1))
|
|
|
|
|
2013-06-06 12:27:00 +00:00
|
|
|
|
2014-12-03 20:36:17 +00:00
|
|
|
class TemplateTagLoading(SimpleTestCase):
|
2010-04-15 19:57:09 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2012-12-08 10:13:52 +00:00
|
|
|
self.egg_dir = '%s/eggs' % os.path.dirname(upath(__file__))
|
2010-04-15 19:57:09 +00:00
|
|
|
|
|
|
|
def test_load_error(self):
|
|
|
|
ttext = "{% load broken_tag %}"
|
|
|
|
self.assertRaises(template.TemplateSyntaxError, template.Template, ttext)
|
|
|
|
try:
|
|
|
|
template.Template(ttext)
|
2012-04-28 16:09:37 +00:00
|
|
|
except template.TemplateSyntaxError as e:
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertIn('ImportError', e.args[0])
|
|
|
|
self.assertIn('Xtemplate', e.args[0])
|
2010-04-15 19:57:09 +00:00
|
|
|
|
|
|
|
def test_load_error_egg(self):
|
|
|
|
ttext = "{% load broken_egg %}"
|
|
|
|
egg_name = '%s/tagsegg.egg' % self.egg_dir
|
2014-01-26 05:50:40 +00:00
|
|
|
with extend_sys_path(egg_name):
|
|
|
|
with self.assertRaises(template.TemplateSyntaxError):
|
|
|
|
with self.settings(INSTALLED_APPS=['tagsegg']):
|
|
|
|
template.Template(ttext)
|
|
|
|
try:
|
|
|
|
with self.settings(INSTALLED_APPS=['tagsegg']):
|
|
|
|
template.Template(ttext)
|
|
|
|
except template.TemplateSyntaxError as e:
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertIn('ImportError', e.args[0])
|
|
|
|
self.assertIn('Xtemplate', e.args[0])
|
2010-04-15 19:57:09 +00:00
|
|
|
|
|
|
|
def test_load_working_egg(self):
|
|
|
|
ttext = "{% load working_egg %}"
|
|
|
|
egg_name = '%s/tagsegg.egg' % self.egg_dir
|
2014-01-26 05:50:40 +00:00
|
|
|
with extend_sys_path(egg_name):
|
|
|
|
with self.settings(INSTALLED_APPS=['tagsegg']):
|
|
|
|
template.Template(ttext)
|
2010-04-15 19:57:09 +00:00
|
|
|
|
2011-04-17 04:52:31 +00:00
|
|
|
|
2012-04-09 13:24:57 +00:00
|
|
|
class RequestContextTests(unittest.TestCase):
|
2011-04-17 04:52:31 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.fake_request = RequestFactory().get('/')
|
|
|
|
|
2014-12-17 21:10:57 +00:00
|
|
|
@override_settings(TEMPLATES=[{
|
|
|
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
|
|
|
'OPTIONS': {
|
|
|
|
'loaders': [
|
|
|
|
('django.template.loaders.locmem.Loader', {
|
|
|
|
'child': '{{ var|default:"none" }}',
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}])
|
2011-04-17 04:52:31 +00:00
|
|
|
def test_include_only(self):
|
|
|
|
"""
|
|
|
|
Regression test for #15721, ``{% include %}`` and ``RequestContext``
|
|
|
|
not playing together nicely.
|
|
|
|
"""
|
|
|
|
ctx = RequestContext(self.fake_request, {'var': 'parent'})
|
|
|
|
self.assertEqual(
|
|
|
|
template.Template('{% include "child" %}').render(ctx),
|
|
|
|
'parent'
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
template.Template('{% include "child" only %}').render(ctx),
|
|
|
|
'none'
|
|
|
|
)
|
2013-08-29 04:08:01 +00:00
|
|
|
|
|
|
|
def test_stack_size(self):
|
|
|
|
"""
|
|
|
|
Regression test for #7116, Optimize RequetsContext construction
|
|
|
|
"""
|
|
|
|
ctx = RequestContext(self.fake_request, {})
|
|
|
|
# The stack should now contain 3 items:
|
|
|
|
# [builtins, supplied context, context processor]
|
|
|
|
self.assertEqual(len(ctx.dicts), 3)
|
2013-08-27 22:50:11 +00:00
|
|
|
|
2014-02-15 12:45:52 +00:00
|
|
|
@override_settings(TEMPLATE_CONTEXT_PROCESSORS=())
|
|
|
|
def test_context_comparable(self):
|
|
|
|
test_data = {'x': 'y', 'v': 'z', 'd': {'o': object, 'a': 'b'}}
|
|
|
|
|
|
|
|
# test comparing RequestContext to prevent problems if somebody
|
|
|
|
# adds __eq__ in the future
|
|
|
|
request = RequestFactory().get('/')
|
|
|
|
|
2014-02-17 03:54:36 +00:00
|
|
|
self.assertEqual(
|
2014-02-15 12:45:52 +00:00
|
|
|
RequestContext(request, dict_=test_data),
|
|
|
|
RequestContext(request, dict_=test_data)
|
|
|
|
)
|
|
|
|
|
2013-08-27 22:50:11 +00:00
|
|
|
|
2014-12-03 20:36:17 +00:00
|
|
|
class SSITests(SimpleTestCase):
|
2013-08-27 22:50:11 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.this_dir = os.path.dirname(os.path.abspath(upath(__file__)))
|
|
|
|
self.ssi_dir = os.path.join(self.this_dir, "templates", "first")
|
2014-12-14 21:59:51 +00:00
|
|
|
self.engine = Engine(allowed_include_roots=(self.ssi_dir,))
|
2013-08-27 22:50:11 +00:00
|
|
|
|
|
|
|
def render_ssi(self, path):
|
|
|
|
# the path must exist for the test to be reliable
|
|
|
|
self.assertTrue(os.path.exists(path))
|
2014-12-14 21:59:51 +00:00
|
|
|
return self.engine.from_string('{%% ssi "%s" %%}' % path).render(Context({}))
|
2013-08-27 22:50:11 +00:00
|
|
|
|
|
|
|
def test_allowed_paths(self):
|
|
|
|
acceptable_path = os.path.join(self.ssi_dir, "..", "first", "test.html")
|
2014-12-14 21:59:51 +00:00
|
|
|
self.assertEqual(self.render_ssi(acceptable_path), 'First template\n')
|
2013-08-27 22:50:11 +00:00
|
|
|
|
|
|
|
def test_relative_include_exploit(self):
|
|
|
|
"""
|
2014-12-14 21:59:51 +00:00
|
|
|
May not bypass allowed_include_roots with relative paths
|
2013-08-27 22:50:11 +00:00
|
|
|
|
2014-12-14 21:59:51 +00:00
|
|
|
e.g. if allowed_include_roots = ("/var/www",), it should not be
|
2013-08-27 22:50:11 +00:00
|
|
|
possible to do {% ssi "/var/www/../../etc/passwd" %}
|
|
|
|
"""
|
|
|
|
disallowed_paths = [
|
|
|
|
os.path.join(self.ssi_dir, "..", "ssi_include.html"),
|
|
|
|
os.path.join(self.ssi_dir, "..", "second", "test.html"),
|
|
|
|
]
|
2014-12-14 21:59:51 +00:00
|
|
|
for disallowed_path in disallowed_paths:
|
|
|
|
self.assertEqual(self.render_ssi(disallowed_path), '')
|