summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavide Ceretti <dav.ceretti@gmail.com>2014-10-11 17:53:29 +0100
committerTim Graham <timograham@gmail.com>2014-11-03 14:31:23 -0500
commit2d06e3155a13e3ca9e63b97c7c9499a1e1ffd654 (patch)
tree54e53edae8aba7884263adb6c9556c09ec1aa58f
parentca88aa34ad2bf088130ceb329cdbe91c907b8248 (diff)
Fixed #23300 -- Made assertTemplateUsed throw an error on responses not fetched using the test client.
Thanks zags for the report and bmispelon for the patch.
-rw-r--r--django/test/testcases.py6
-rw-r--r--tests/test_utils/tests.py12
2 files changed, 18 insertions, 0 deletions
diff --git a/django/test/testcases.py b/django/test/testcases.py
index 9b25a0b937..8d3265a5b9 100644
--- a/django/test/testcases.py
+++ b/django/test/testcases.py
@@ -518,6 +518,12 @@ class SimpleTestCase(unittest.TestCase):
if msg_prefix:
msg_prefix += ": "
+ if template_name is not None and response is not None and not hasattr(response, 'templates'):
+ raise ValueError(
+ "assertTemplateUsed() and assertTemplateNotUsed() are only "
+ "usable on responses fetched using the Django test Client."
+ )
+
if not hasattr(response, 'templates') or (response is None and template_name):
if response:
template_name = response
diff --git a/tests/test_utils/tests.py b/tests/test_utils/tests.py
index 99ddaf87ef..ddef92c193 100644
--- a/tests/test_utils/tests.py
+++ b/tests/test_utils/tests.py
@@ -385,6 +385,18 @@ class AssertTemplateUsedContextManagerTests(TestCase):
with self.assertTemplateUsed('template_used/base.html'):
render_to_string('template_used/alternative.html')
+ def test_assert_used_on_http_response(self):
+ response = HttpResponse()
+ error_msg = (
+ 'assertTemplateUsed() and assertTemplateNotUsed() are only '
+ 'usable on responses fetched using the Django test Client.'
+ )
+ with self.assertRaisesMessage(ValueError, error_msg):
+ self.assertTemplateUsed(response, 'template.html')
+
+ with self.assertRaisesMessage(ValueError, error_msg):
+ self.assertTemplateNotUsed(response, 'template.html')
+
class HTMLEqualTests(TestCase):
def test_html_parser(self):