diff --git a/tests/admin_changelist/tests.py b/tests/admin_changelist/tests.py index 1d9dc61682..68aaa78996 100644 --- a/tests/admin_changelist/tests.py +++ b/tests/admin_changelist/tests.py @@ -12,7 +12,7 @@ from django.db.models import F from django.db.models.fields import Field, IntegerField from django.db.models.functions import Upper from django.db.models.lookups import Contains, Exact -from django.template import Context, Template +from django.template import Context, Template, TemplateSyntaxError from django.test import TestCase, override_settings from django.test.client import RequestFactory from django.urls import reverse @@ -893,31 +893,24 @@ class ChangeListTests(TestCase): self.assertNotIn('Add ', response.rendered_content) -class AdminLogNodeTestCase(TestCase): +class GetAdminLogTests(TestCase): - def test_get_admin_log_templatetag_custom_user(self): + def test_custom_user_pk_not_named_id(self): """ - Regression test for ticket #20088: admin log depends on User model - having id field as primary key. - - The old implementation raised an AttributeError when trying to use - the id field. + {% get_admin_log %} works if the user model's primary key isn't named + 'id'. """ context = Context({'user': CustomIdUser()}) - template_string = '{% load log %}{% get_admin_log 10 as admin_log for_user user %}' - template = Template(template_string) + template = Template('{% load log %}{% get_admin_log 10 as admin_log for_user user %}') # This template tag just logs. self.assertEqual(template.render(context), '') - def test_get_admin_log_templatetag_no_user(self): - """ - The {% get_admin_log %} tag should work without specifying a user. - """ + def test_no_user(self): + """{% get_admin_log %} works without specifying a user.""" user = User(username='jondoe', password='secret', email='super@example.com') user.save() ct = ContentType.objects.get_for_model(User) LogEntry.objects.log_action(user.pk, ct.pk, user.pk, repr(user), 1) - t = Template( '{% load log %}' '{% get_admin_log 100 as admin_log %}' @@ -927,6 +920,26 @@ class AdminLogNodeTestCase(TestCase): ) self.assertEqual(t.render(Context({})), 'Added "".') + def test_missing_args(self): + msg = "'get_admin_log' statements require two arguments" + with self.assertRaisesMessage(TemplateSyntaxError, msg): + Template('{% load log %}{% get_admin_log 10 as %}') + + def test_non_integer_limit(self): + msg = "First argument to 'get_admin_log' must be an integer" + with self.assertRaisesMessage(TemplateSyntaxError, msg): + Template('{% load log %}{% get_admin_log "10" as admin_log for_user user %}') + + def test_without_as(self): + msg = "Second argument to 'get_admin_log' must be 'as'" + with self.assertRaisesMessage(TemplateSyntaxError, msg): + Template('{% load log %}{% get_admin_log 10 ad admin_log for_user user %}') + + def test_without_for_user(self): + msg = "Fourth argument to 'get_admin_log' must be 'for_user'" + with self.assertRaisesMessage(TemplateSyntaxError, msg): + Template('{% load log %}{% get_admin_log 10 as admin_log foruser user %}') + @override_settings(ROOT_URLCONF='admin_changelist.urls') class SeleniumTests(AdminSeleniumTestCase): diff --git a/tests/template_tests/syntax_tests/test_static.py b/tests/template_tests/syntax_tests/test_static.py index 345f943caf..00f8cdbc11 100644 --- a/tests/template_tests/syntax_tests/test_static.py +++ b/tests/template_tests/syntax_tests/test_static.py @@ -1,6 +1,7 @@ from urllib.parse import urljoin from django.conf import settings +from django.template import TemplateSyntaxError from django.test import SimpleTestCase, override_settings from ..utils import setup @@ -32,6 +33,12 @@ class StaticTagTests(SimpleTestCase): output = self.engine.render_to_string('static-prefixtag04') self.assertEqual(output, settings.MEDIA_URL) + @setup({'t': '{% load static %}{% get_media_prefix ad media_prefix %}{{ media_prefix }}'}) + def test_static_prefixtag_without_as(self): + msg = "First argument in 'get_media_prefix' must be 'as'" + with self.assertRaisesMessage(TemplateSyntaxError, msg): + self.engine.render_to_string('t') + @setup({'static-statictag01': '{% load static %}{% static "admin/base.css" %}'}) def test_static_statictag01(self): output = self.engine.render_to_string('static-statictag01') @@ -56,3 +63,9 @@ class StaticTagTests(SimpleTestCase): def test_static_quotes_urls(self): output = self.engine.render_to_string('static-statictag05') self.assertEqual(output, urljoin(settings.STATIC_URL, '/static/special%3Fchars%26quoted.html')) + + @setup({'t': '{% load static %}{% static %}'}) + def test_static_statictag_without_path(self): + msg = "'static' takes at least one argument (path to file)" + with self.assertRaisesMessage(TemplateSyntaxError, msg): + self.engine.render_to_string('t')