2008-07-29 06:05:15 +00:00
|
|
|
import os.path
|
2013-07-01 12:22:27 +00:00
|
|
|
import sys
|
2015-03-02 19:31:14 +00:00
|
|
|
import tempfile
|
2013-07-01 12:22:27 +00:00
|
|
|
import unittest
|
2015-02-18 21:51:05 +00:00
|
|
|
from contextlib import contextmanager
|
2008-05-29 13:11:23 +00:00
|
|
|
|
2016-12-31 12:41:35 +00:00
|
|
|
from django.template import TemplateDoesNotExist
|
2014-11-19 22:23:58 +00:00
|
|
|
from django.template.engine import Engine
|
2016-12-31 12:41:35 +00:00
|
|
|
from django.test import SimpleTestCase, override_settings
|
2016-05-10 13:57:03 +00:00
|
|
|
from django.utils.functional import lazystr
|
2015-02-18 21:51:05 +00:00
|
|
|
|
|
|
|
from .utils import TEMPLATE_DIR
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2014-12-17 21:10:57 +00:00
|
|
|
|
2015-02-18 21:51:05 +00:00
|
|
|
class CachedLoaderTests(SimpleTestCase):
|
2015-03-03 21:48:26 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.engine = Engine(
|
|
|
|
dirs=[TEMPLATE_DIR],
|
2015-02-18 21:51:05 +00:00
|
|
|
loaders=[
|
|
|
|
(
|
|
|
|
"django.template.loaders.cached.Loader",
|
|
|
|
[
|
|
|
|
"django.template.loaders.filesystem.Loader",
|
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2014-12-17 21:10:57 +00:00
|
|
|
|
2015-03-03 21:48:26 +00:00
|
|
|
def test_get_template(self):
|
|
|
|
template = self.engine.get_template("index.html")
|
|
|
|
self.assertEqual(template.origin.name, os.path.join(TEMPLATE_DIR, "index.html"))
|
|
|
|
self.assertEqual(template.origin.template_name, "index.html")
|
|
|
|
self.assertEqual(
|
|
|
|
template.origin.loader, self.engine.template_loaders[0].loaders[0]
|
|
|
|
)
|
2008-05-29 13:11:23 +00:00
|
|
|
|
2015-03-03 21:48:26 +00:00
|
|
|
cache = self.engine.template_loaders[0].get_template_cache
|
|
|
|
self.assertEqual(cache["index.html"], template)
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2015-03-03 21:48:26 +00:00
|
|
|
# Run a second time from cache
|
|
|
|
template = self.engine.get_template("index.html")
|
|
|
|
self.assertEqual(template.origin.name, os.path.join(TEMPLATE_DIR, "index.html"))
|
|
|
|
self.assertEqual(template.origin.template_name, "index.html")
|
|
|
|
self.assertEqual(
|
|
|
|
template.origin.loader, self.engine.template_loaders[0].loaders[0]
|
|
|
|
)
|
|
|
|
|
2016-03-09 03:35:39 +00:00
|
|
|
def test_get_template_missing_debug_off(self):
|
|
|
|
"""
|
|
|
|
With template debugging disabled, the raw TemplateDoesNotExist class
|
|
|
|
should be cached when a template is missing. See ticket #26306 and
|
|
|
|
docstrings in the cached loader for details.
|
|
|
|
"""
|
|
|
|
self.engine.debug = False
|
2015-03-03 21:48:26 +00:00
|
|
|
with self.assertRaises(TemplateDoesNotExist):
|
2016-03-09 03:35:39 +00:00
|
|
|
self.engine.get_template("prod-template-missing.html")
|
|
|
|
e = self.engine.template_loaders[0].get_template_cache[
|
|
|
|
"prod-template-missing.html"
|
|
|
|
]
|
|
|
|
self.assertEqual(e, TemplateDoesNotExist)
|
|
|
|
|
|
|
|
def test_get_template_missing_debug_on(self):
|
|
|
|
"""
|
|
|
|
With template debugging enabled, a TemplateDoesNotExist instance
|
|
|
|
should be cached when a template is missing.
|
|
|
|
"""
|
|
|
|
self.engine.debug = True
|
|
|
|
with self.assertRaises(TemplateDoesNotExist):
|
|
|
|
self.engine.get_template("debug-template-missing.html")
|
|
|
|
e = self.engine.template_loaders[0].get_template_cache[
|
|
|
|
"debug-template-missing.html"
|
|
|
|
]
|
|
|
|
self.assertIsInstance(e, TemplateDoesNotExist)
|
|
|
|
self.assertEqual(e.args[0], "debug-template-missing.html")
|
|
|
|
|
|
|
|
def test_cached_exception_no_traceback(self):
|
|
|
|
"""
|
|
|
|
When a TemplateDoesNotExist instance is cached, the cached instance
|
|
|
|
should not contain the __traceback__, __context__, or __cause__
|
|
|
|
attributes that Python sets when raising exceptions.
|
|
|
|
"""
|
|
|
|
self.engine.debug = True
|
|
|
|
with self.assertRaises(TemplateDoesNotExist):
|
|
|
|
self.engine.get_template("no-traceback-in-cache.html")
|
|
|
|
e = self.engine.template_loaders[0].get_template_cache[
|
|
|
|
"no-traceback-in-cache.html"
|
|
|
|
]
|
|
|
|
|
|
|
|
error_msg = "Cached TemplateDoesNotExist must not have been thrown."
|
|
|
|
self.assertIsNone(e.__traceback__, error_msg)
|
|
|
|
self.assertIsNone(e.__context__, error_msg)
|
|
|
|
self.assertIsNone(e.__cause__, error_msg)
|
2015-03-03 21:48:26 +00:00
|
|
|
|
2016-04-24 19:28:12 +00:00
|
|
|
def test_template_name_leading_dash_caching(self):
|
|
|
|
"""
|
|
|
|
#26536 -- A leading dash in a template name shouldn't be stripped
|
|
|
|
from its cache key.
|
|
|
|
"""
|
|
|
|
self.assertEqual(
|
|
|
|
self.engine.template_loaders[0].cache_key("-template.html", []),
|
|
|
|
"-template.html",
|
|
|
|
)
|
|
|
|
|
2016-05-10 13:57:03 +00:00
|
|
|
def test_template_name_lazy_string(self):
|
|
|
|
"""
|
|
|
|
#26603 -- A template name specified as a lazy string should be forced
|
|
|
|
to text before computing its cache key.
|
|
|
|
"""
|
|
|
|
self.assertEqual(
|
|
|
|
self.engine.template_loaders[0].cache_key(lazystr("template.html"), []),
|
|
|
|
"template.html",
|
|
|
|
)
|
|
|
|
|
2020-05-17 14:10:09 +00:00
|
|
|
def test_get_dirs(self):
|
|
|
|
inner_dirs = self.engine.template_loaders[0].loaders[0].get_dirs()
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
list(self.engine.template_loaders[0].get_dirs()), list(inner_dirs)
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2020-05-17 14:10:09 +00:00
|
|
|
|
2010-09-12 21:53:35 +00:00
|
|
|
|
2015-02-18 21:51:05 +00:00
|
|
|
class FileSystemLoaderTests(SimpleTestCase):
|
2015-03-03 21:48:26 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2016-09-03 13:06:33 +00:00
|
|
|
cls.engine = Engine(
|
|
|
|
dirs=[TEMPLATE_DIR], loaders=["django.template.loaders.filesystem.Loader"]
|
|
|
|
)
|
2017-01-21 13:13:44 +00:00
|
|
|
super().setUpClass()
|
2015-03-02 19:31:14 +00:00
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def set_dirs(self, dirs):
|
|
|
|
original_dirs = self.engine.dirs
|
|
|
|
self.engine.dirs = dirs
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
self.engine.dirs = original_dirs
|
2015-02-18 21:51:05 +00:00
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def source_checker(self, dirs):
|
|
|
|
loader = self.engine.template_loaders[0]
|
|
|
|
|
|
|
|
def check_sources(path, expected_sources):
|
|
|
|
expected_sources = [os.path.abspath(s) for s in expected_sources]
|
|
|
|
self.assertEqual(
|
2015-03-03 21:48:26 +00:00
|
|
|
[origin.name for origin in loader.get_template_sources(path)],
|
2015-02-18 21:51:05 +00:00
|
|
|
expected_sources,
|
|
|
|
)
|
|
|
|
|
2015-03-02 19:31:14 +00:00
|
|
|
with self.set_dirs(dirs):
|
2015-02-24 15:17:17 +00:00
|
|
|
yield check_sources
|
2015-02-18 21:51:05 +00:00
|
|
|
|
2015-03-03 21:48:26 +00:00
|
|
|
def test_get_template(self):
|
|
|
|
template = self.engine.get_template("index.html")
|
|
|
|
self.assertEqual(template.origin.name, os.path.join(TEMPLATE_DIR, "index.html"))
|
|
|
|
self.assertEqual(template.origin.template_name, "index.html")
|
|
|
|
self.assertEqual(template.origin.loader, self.engine.template_loaders[0])
|
|
|
|
self.assertEqual(
|
|
|
|
template.origin.loader_name, "django.template.loaders.filesystem.Loader"
|
|
|
|
)
|
|
|
|
|
2016-12-13 21:50:00 +00:00
|
|
|
def test_loaders_dirs(self):
|
|
|
|
engine = Engine(
|
|
|
|
loaders=[("django.template.loaders.filesystem.Loader", [TEMPLATE_DIR])]
|
|
|
|
)
|
|
|
|
template = engine.get_template("index.html")
|
|
|
|
self.assertEqual(template.origin.name, os.path.join(TEMPLATE_DIR, "index.html"))
|
|
|
|
|
|
|
|
def test_loaders_dirs_empty(self):
|
|
|
|
"""An empty dirs list in loaders overrides top level dirs."""
|
|
|
|
engine = Engine(
|
|
|
|
dirs=[TEMPLATE_DIR],
|
|
|
|
loaders=[("django.template.loaders.filesystem.Loader", [])],
|
|
|
|
)
|
|
|
|
with self.assertRaises(TemplateDoesNotExist):
|
|
|
|
engine.get_template("index.html")
|
|
|
|
|
2015-02-18 21:51:05 +00:00
|
|
|
def test_directory_security(self):
|
|
|
|
with self.source_checker(["/dir1", "/dir2"]) as check_sources:
|
|
|
|
check_sources("index.html", ["/dir1/index.html", "/dir2/index.html"])
|
|
|
|
check_sources("/etc/passwd", [])
|
|
|
|
check_sources("etc/passwd", ["/dir1/etc/passwd", "/dir2/etc/passwd"])
|
|
|
|
check_sources("../etc/passwd", [])
|
|
|
|
check_sources("../../../etc/passwd", [])
|
|
|
|
check_sources("/dir1/index.html", ["/dir1/index.html"])
|
|
|
|
check_sources("../dir2/index.html", ["/dir2/index.html"])
|
|
|
|
check_sources("/dir1blah", [])
|
|
|
|
check_sources("../dir1blah", [])
|
|
|
|
|
|
|
|
def test_unicode_template_name(self):
|
|
|
|
with self.source_checker(["/dir1", "/dir2"]) as check_sources:
|
|
|
|
check_sources("Ångström", ["/dir1/Ångström", "/dir2/Ångström"])
|
|
|
|
|
2018-09-25 15:27:36 +00:00
|
|
|
def test_bytestring(self):
|
|
|
|
loader = self.engine.template_loaders[0]
|
|
|
|
msg = "Can't mix strings and bytes in path components"
|
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
|
|
|
list(loader.get_template_sources(b"\xc3\x85ngstr\xc3\xb6m"))
|
2015-02-18 21:51:05 +00:00
|
|
|
|
|
|
|
def test_unicode_dir_name(self):
|
2018-09-25 15:27:36 +00:00
|
|
|
with self.source_checker(["/Straße"]) as check_sources:
|
2015-02-18 21:51:05 +00:00
|
|
|
check_sources("Ångström", ["/Straße/Ångström"])
|
|
|
|
|
|
|
|
@unittest.skipUnless(
|
|
|
|
os.path.normcase("/TEST") == os.path.normpath("/test"),
|
|
|
|
"This test only runs on case-sensitive file systems.",
|
|
|
|
)
|
|
|
|
def test_case_sensitivity(self):
|
|
|
|
with self.source_checker(["/dir1", "/DIR2"]) as check_sources:
|
|
|
|
check_sources("index.html", ["/dir1/index.html", "/DIR2/index.html"])
|
|
|
|
check_sources("/DIR1/index.HTML", ["/DIR1/index.HTML"])
|
|
|
|
|
2015-03-02 19:31:14 +00:00
|
|
|
def test_file_does_not_exist(self):
|
|
|
|
with self.assertRaises(TemplateDoesNotExist):
|
|
|
|
self.engine.get_template("doesnotexist.html")
|
|
|
|
|
|
|
|
@unittest.skipIf(
|
|
|
|
sys.platform == "win32",
|
|
|
|
"Python on Windows doesn't have working os.chmod().",
|
|
|
|
)
|
|
|
|
def test_permissions_error(self):
|
|
|
|
with tempfile.NamedTemporaryFile() as tmpfile:
|
|
|
|
tmpdir = os.path.dirname(tmpfile.name)
|
|
|
|
tmppath = os.path.join(tmpdir, tmpfile.name)
|
|
|
|
os.chmod(tmppath, 0o0222)
|
|
|
|
with self.set_dirs([tmpdir]):
|
2019-01-28 15:01:35 +00:00
|
|
|
with self.assertRaisesMessage(PermissionError, "Permission denied"):
|
2015-03-02 19:31:14 +00:00
|
|
|
self.engine.get_template(tmpfile.name)
|
|
|
|
|
|
|
|
def test_notafile_error(self):
|
2019-01-29 15:05:10 +00:00
|
|
|
# Windows raises PermissionError when trying to open a directory.
|
2019-11-06 14:14:30 +00:00
|
|
|
with self.assertRaises(
|
|
|
|
PermissionError if sys.platform == "win32" else IsADirectoryError
|
|
|
|
):
|
2015-03-02 19:31:14 +00:00
|
|
|
self.engine.get_template("first")
|
|
|
|
|
2015-02-18 21:51:05 +00:00
|
|
|
|
2015-03-03 21:48:26 +00:00
|
|
|
class AppDirectoriesLoaderTests(SimpleTestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
cls.engine = Engine(
|
2015-02-18 21:51:05 +00:00
|
|
|
loaders=["django.template.loaders.app_directories.Loader"],
|
|
|
|
)
|
2017-01-21 13:13:44 +00:00
|
|
|
super().setUpClass()
|
2015-02-24 15:17:17 +00:00
|
|
|
|
|
|
|
@override_settings(INSTALLED_APPS=["template_tests"])
|
2015-03-03 21:48:26 +00:00
|
|
|
def test_get_template(self):
|
|
|
|
template = self.engine.get_template("index.html")
|
|
|
|
self.assertEqual(template.origin.name, os.path.join(TEMPLATE_DIR, "index.html"))
|
|
|
|
self.assertEqual(template.origin.template_name, "index.html")
|
|
|
|
self.assertEqual(template.origin.loader, self.engine.template_loaders[0])
|
|
|
|
|
2015-02-24 15:17:17 +00:00
|
|
|
@override_settings(INSTALLED_APPS=[])
|
|
|
|
def test_not_installed(self):
|
|
|
|
with self.assertRaises(TemplateDoesNotExist):
|
|
|
|
self.engine.get_template("index.html")
|
2015-03-03 21:48:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LocmemLoaderTests(SimpleTestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
cls.engine = Engine(
|
|
|
|
loaders=[
|
2022-02-03 19:24:19 +00:00
|
|
|
(
|
2015-03-03 21:48:26 +00:00
|
|
|
"django.template.loaders.locmem.Loader",
|
|
|
|
{
|
|
|
|
"index.html": "index",
|
|
|
|
},
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2015-03-03 21:48:26 +00:00
|
|
|
],
|
|
|
|
)
|
2017-01-21 13:13:44 +00:00
|
|
|
super().setUpClass()
|
2015-03-03 21:48:26 +00:00
|
|
|
|
|
|
|
def test_get_template(self):
|
|
|
|
template = self.engine.get_template("index.html")
|
|
|
|
self.assertEqual(template.origin.name, "index.html")
|
|
|
|
self.assertEqual(template.origin.template_name, "index.html")
|
|
|
|
self.assertEqual(template.origin.loader, self.engine.template_loaders[0])
|