From 04e813cd17484ba91e3fc25d5e5118ac25f47352 Mon Sep 17 00:00:00 2001 From: Clifford Gama Date: Tue, 24 Jun 2025 22:35:45 +0200 Subject: [PATCH] Refs #4476 -- Added tests for assertRedirects() when following redirect chains. Thanks Natalia Bidart for the review. --- tests/test_client_regress/tests.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/tests/test_client_regress/tests.py b/tests/test_client_regress/tests.py index bc18b11a45..a480a2069b 100644 --- a/tests/test_client_regress/tests.py +++ b/tests/test_client_regress/tests.py @@ -385,7 +385,7 @@ class AssertTemplateUsedTests(TestDataMixin, TestCase): @override_settings(ROOT_URLCONF="test_client_regress.urls") -class AssertRedirectsTests(SimpleTestCase): +class AssertRedirectsTests(ExtraAssertMixin, SimpleTestCase): def test_redirect_page(self): "An assertion is raised if the original page couldn't be retrieved as expected" # This page will redirect with code 301, not 302 @@ -408,6 +408,34 @@ class AssertRedirectsTests(SimpleTestCase): str(e), ) + def test_followed_redirect_unexpected_initial_status_code(self): + response = self.client.get("/permanent_redirect_view/", follow=True) + msg = ( + "Initial response didn't redirect as expected: Response code was 301 " + "(expected 302)" + ) + self.assertRaisesPrefixedMessage( + self.assertRedirects, + response, + "/get_view/", + expected_msg=msg, + ) + + def test_followed_redirect_unexpected_final_status_code(self): + response = self.client.get("/redirect_view/", follow=True) + msg = ( + "Response didn't redirect as expected: Final Response code was 200 " + "(expected 403)" + ) + self.assertRaisesPrefixedMessage( + self.assertRedirects, + response, + "/get_view/", + status_code=302, + target_status_code=403, + expected_msg=msg, + ) + def test_lost_query(self): """ An assertion is raised if the redirect location doesn't preserve GET