summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJake Howard <git@theorangeone.net>2025-11-12 17:41:32 +0000
committerJacob Walls <jacobtylerwalls@gmail.com>2025-12-05 10:06:48 -0500
commit0ac548635eee801d5de49bec482b7b8e1e97ef59 (patch)
treecfa0aa9b73c8d0a5e614fe8c8cc6adbc7d3a2efb /tests
parent55888655a269f41025405b9a1bff14117ae58e2a (diff)
Fixed #36728 -- Validated template tag arguments at definition time.
Before, `context` and `content` were validated at compile time.
Diffstat (limited to 'tests')
-rw-r--r--tests/admin_views/test_templatetags.py1
-rw-r--r--tests/template_tests/templatetags/custom.py56
-rw-r--r--tests/template_tests/templatetags/inclusion.py22
-rw-r--r--tests/template_tests/test_custom.py82
4 files changed, 34 insertions, 127 deletions
diff --git a/tests/admin_views/test_templatetags.py b/tests/admin_views/test_templatetags.py
index e8129cce1d..69bae27412 100644
--- a/tests/admin_views/test_templatetags.py
+++ b/tests/admin_views/test_templatetags.py
@@ -144,6 +144,7 @@ class AdminTemplateTagsTest(AdminViewBasicTestCase):
# inspect.getfullargspec(), which is not ready for deferred
# evaluation of annotations.
InclusionAdminNode(
+ "test",
parser=object(),
token=Token(token_type=TokenType.TEXT, contents="a"),
func=action,
diff --git a/tests/template_tests/templatetags/custom.py b/tests/template_tests/templatetags/custom.py
index bf201e74e9..f89ed1d9d3 100644
--- a/tests/template_tests/templatetags/custom.py
+++ b/tests/template_tests/templatetags/custom.py
@@ -268,62 +268,6 @@ def simple_unlimited_args_kwargs_block(content, one, two="hi", *args, **kwargs):
)
-@register.simple_block_tag(takes_context=True)
-def simple_block_tag_without_context_parameter(arg):
- """Expected simple_block_tag_without_context_parameter __doc__"""
- return "Expected result"
-
-
-@register.simple_block_tag
-def simple_tag_without_content_parameter(arg):
- """Expected simple_tag_without_content_parameter __doc__"""
- return "Expected result"
-
-
-@register.simple_block_tag(takes_context=True)
-def simple_tag_with_context_without_content_parameter(context, arg):
- """Expected simple_tag_with_context_without_content_parameter __doc__"""
- return "Expected result"
-
-
-@register.simple_tag(takes_context=True)
-def simple_tag_without_context_parameter(arg):
- """Expected simple_tag_without_context_parameter __doc__"""
- return "Expected result"
-
-
-simple_tag_without_context_parameter.anything = (
- "Expected simple_tag_without_context_parameter __dict__"
-)
-
-
-@register.simple_block_tag(takes_context=True)
-def simple_tag_takes_context_without_params_block():
- """Expected simple_tag_takes_context_without_params_block __doc__"""
- return "Expected result"
-
-
-@register.simple_tag(takes_context=True)
-def simple_tag_takes_context_without_params():
- """Expected simple_tag_takes_context_without_params __doc__"""
- return "Expected result"
-
-
-simple_tag_takes_context_without_params.anything = (
- "Expected simple_tag_takes_context_without_params __dict__"
-)
-
-
-@register.simple_block_tag
-def simple_block_tag_without_content():
- return "Expected result"
-
-
-@register.simple_block_tag(takes_context=True)
-def simple_block_tag_with_context_without_content():
- return "Expected result"
-
-
@register.simple_tag(takes_context=True)
def escape_naive(context):
"""A tag that doesn't even think about escaping issues"""
diff --git a/tests/template_tests/templatetags/inclusion.py b/tests/template_tests/templatetags/inclusion.py
index ea749a43b5..741eb250a2 100644
--- a/tests/template_tests/templatetags/inclusion.py
+++ b/tests/template_tests/templatetags/inclusion.py
@@ -269,28 +269,6 @@ inclusion_unlimited_args_kwargs.anything = (
)
-@register.inclusion_tag("inclusion.html", takes_context=True)
-def inclusion_tag_without_context_parameter(arg):
- """Expected inclusion_tag_without_context_parameter __doc__"""
- return {}
-
-
-inclusion_tag_without_context_parameter.anything = (
- "Expected inclusion_tag_without_context_parameter __dict__"
-)
-
-
-@register.inclusion_tag("inclusion.html", takes_context=True)
-def inclusion_tag_takes_context_without_params():
- """Expected inclusion_tag_takes_context_without_params __doc__"""
- return {}
-
-
-inclusion_tag_takes_context_without_params.anything = (
- "Expected inclusion_tag_takes_context_without_params __dict__"
-)
-
-
@register.inclusion_tag("inclusion_extends1.html")
def inclusion_extends1():
return {}
diff --git a/tests/template_tests/test_custom.py b/tests/template_tests/test_custom.py
index 424c110875..c146da5ff7 100644
--- a/tests/template_tests/test_custom.py
+++ b/tests/template_tests/test_custom.py
@@ -2,7 +2,7 @@ import os
from django.template import Context, Engine, TemplateSyntaxError
from django.template.base import Node
-from django.template.library import InvalidTemplateLibrary
+from django.template.library import InvalidTemplateLibrary, Library
from django.test import SimpleTestCase
from django.test.utils import extend_sys_path
@@ -216,10 +216,6 @@ class SimpleTagTests(TagTestCase):
self.verify_tag(
custom.simple_unlimited_args_kwargs, "simple_unlimited_args_kwargs"
)
- self.verify_tag(
- custom.simple_tag_without_context_parameter,
- "simple_tag_without_context_parameter",
- )
def test_simple_tag_missing_context(self):
# The 'context' parameter must be present when takes_context is True
@@ -228,9 +224,10 @@ class SimpleTagTests(TagTestCase):
"takes_context=True so it must have a first argument of 'context'"
)
with self.assertRaisesMessage(TemplateSyntaxError, msg):
- self.engine.from_string(
- "{% load custom %}{% simple_tag_without_context_parameter 123 %}"
- )
+
+ @Library().simple_tag(takes_context=True)
+ def simple_tag_without_context_parameter(arg):
+ return "Expected result"
def test_simple_tag_missing_context_no_params(self):
msg = (
@@ -238,9 +235,10 @@ class SimpleTagTests(TagTestCase):
"takes_context=True so it must have a first argument of 'context'"
)
with self.assertRaisesMessage(TemplateSyntaxError, msg):
- self.engine.from_string(
- "{% load custom %}{% simple_tag_takes_context_without_params %}"
- )
+
+ @Library().simple_tag(takes_context=True)
+ def simple_tag_takes_context_without_params():
+ return "Expected result"
class SimpleBlockTagTests(TagTestCase):
@@ -423,18 +421,6 @@ class SimpleBlockTagTests(TagTestCase):
"of: endsimple_one_default_block.",
"{% load custom %}{% simple_one_default_block %}Some content",
),
- (
- "'simple_tag_without_content_parameter' must have a first argument "
- "of 'content'",
- "{% load custom %}{% simple_tag_without_content_parameter %}",
- ),
- (
- "'simple_tag_with_context_without_content_parameter' is decorated with "
- "takes_context=True so it must have a first argument of 'context' and "
- "a second argument of 'content'",
- "{% load custom %}"
- "{% simple_tag_with_context_without_content_parameter %}",
- ),
]
for entry in errors:
@@ -485,10 +471,10 @@ class SimpleBlockTagTests(TagTestCase):
"takes_context=True so it must have a first argument of 'context'"
)
with self.assertRaisesMessage(TemplateSyntaxError, msg):
- self.engine.from_string(
- "{% load custom %}{% simple_block_tag_without_context_parameter 123 %}"
- "{% endsimple_block_tag_without_context_parameter %}"
- )
+
+ @Library().simple_block_tag(takes_context=True)
+ def simple_block_tag_without_context_parameter(arg):
+ return "Expected result"
def test_simple_block_tag_missing_context_no_params(self):
msg = (
@@ -496,10 +482,10 @@ class SimpleBlockTagTests(TagTestCase):
"takes_context=True so it must have a first argument of 'context'"
)
with self.assertRaisesMessage(TemplateSyntaxError, msg):
- self.engine.from_string(
- "{% load custom %}{% simple_tag_takes_context_without_params_block %}"
- "{% endsimple_tag_takes_context_without_params_block %}"
- )
+
+ @Library().simple_block_tag(takes_context=True)
+ def simple_tag_takes_context_without_params_block():
+ return "Expected result"
def test_simple_block_tag_missing_content(self):
# The 'content' parameter must be present when takes_context is True
@@ -507,10 +493,10 @@ class SimpleBlockTagTests(TagTestCase):
"'simple_block_tag_without_content' must have a first argument of 'content'"
)
with self.assertRaisesMessage(TemplateSyntaxError, msg):
- self.engine.from_string(
- "{% load custom %}{% simple_block_tag_without_content %}"
- "{% endsimple_block_tag_without_content %}"
- )
+
+ @Library().simple_block_tag
+ def simple_block_tag_without_content():
+ return "Expected result"
def test_simple_block_tag_with_context_missing_content(self):
# The 'content' parameter must be present when takes_context is True
@@ -520,10 +506,10 @@ class SimpleBlockTagTests(TagTestCase):
"second argument of 'content'"
)
with self.assertRaisesMessage(TemplateSyntaxError, msg):
- self.engine.from_string(
- "{% load custom %}{% simple_block_tag_with_context_without_content %}"
- "{% endsimple_block_tag_with_context_without_content %}"
- )
+
+ @Library().simple_block_tag(takes_context=True)
+ def simple_block_tag_with_context_without_content():
+ return "Expected result"
def test_simple_block_gets_context(self):
c = Context({"name": "Jack & Jill"})
@@ -720,9 +706,10 @@ class InclusionTagTests(TagTestCase):
"takes_context=True so it must have a first argument of 'context'"
)
with self.assertRaisesMessage(TemplateSyntaxError, msg):
- self.engine.from_string(
- "{% load inclusion %}{% inclusion_tag_without_context_parameter 123 %}"
- )
+
+ @Library().inclusion_tag("inclusion.html", takes_context=True)
+ def inclusion_tag_without_context_parameter(arg):
+ return {}
def test_include_tag_missing_context_no_params(self):
msg = (
@@ -730,9 +717,10 @@ class InclusionTagTests(TagTestCase):
"takes_context=True so it must have a first argument of 'context'"
)
with self.assertRaisesMessage(TemplateSyntaxError, msg):
- self.engine.from_string(
- "{% load inclusion %}{% inclusion_tag_takes_context_without_params %}"
- )
+
+ @Library().inclusion_tag("inclusion.html", takes_context=True)
+ def inclusion_tag_takes_context_without_params():
+ return {}
def test_inclusion_tags_from_template(self):
c = Context({"value": 42})
@@ -822,10 +810,6 @@ class InclusionTagTests(TagTestCase):
self.verify_tag(
inclusion.inclusion_only_unlimited_args, "inclusion_only_unlimited_args"
)
- self.verify_tag(
- inclusion.inclusion_tag_without_context_parameter,
- "inclusion_tag_without_context_parameter",
- )
self.verify_tag(inclusion.inclusion_tag_use_l10n, "inclusion_tag_use_l10n")
self.verify_tag(
inclusion.inclusion_unlimited_args_kwargs, "inclusion_unlimited_args_kwargs"