From 5d98d53fab874ec21b313499a7e6aa05791bde85 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Wed, 20 Jun 2018 14:08:56 -0400 Subject: [PATCH] Refs #27398 -- Simplified some tests with assertRedirects(). --- tests/admin_views/tests.py | 45 ++++++---------------------------- tests/auth_tests/test_views.py | 3 +-- 2 files changed, 9 insertions(+), 39 deletions(-) diff --git a/tests/admin_views/tests.py b/tests/admin_views/tests.py index e64ee9b6a3..fe9a02e37a 100644 --- a/tests/admin_views/tests.py +++ b/tests/admin_views/tests.py @@ -1827,7 +1827,6 @@ class AdminViewPermissionsTest(TestCase): response = self.client.get(change_url_3) self.assertEqual(response.status_code, 200) response = self.client.post(change_url_3, {'name': 'changed'}) - self.assertEqual(response.status_code, 302) self.assertRedirects(response, self.index_url) self.assertEqual(RowLevelChangePermissionModel.objects.get(id=3).name, 'odd id mult 3') response = self.client.get(change_url_6) @@ -5559,7 +5558,7 @@ class AdminKeepChangeListFiltersTests(TestCase): def setUp(self): self.client.force_login(self.superuser) - def assertURLEqual(self, url1, url2): + def assertURLEqual(self, url1, url2, msg_prefix=''): """ Assert that two URLs are equal despite the ordering of their querystring. Refs #22360. @@ -5730,31 +5729,19 @@ class AdminKeepChangeListFiltersTests(TestCase): post_data['_save'] = 1 response = self.client.post(self.get_change_url(), data=post_data) - self.assertEqual(response.status_code, 302) - self.assertURLEqual( - response.url, - self.get_changelist_url() - ) + self.assertRedirects(response, self.get_changelist_url()) post_data.pop('_save') # Test redirect on "Save and continue". post_data['_continue'] = 1 response = self.client.post(self.get_change_url(), data=post_data) - self.assertEqual(response.status_code, 302) - self.assertURLEqual( - response.url, - self.get_change_url() - ) + self.assertRedirects(response, self.get_change_url()) post_data.pop('_continue') # Test redirect on "Save and add new". post_data['_addanother'] = 1 response = self.client.post(self.get_change_url(), data=post_data) - self.assertEqual(response.status_code, 302) - self.assertURLEqual( - response.url, - self.get_add_url() - ) + self.assertRedirects(response, self.get_add_url()) post_data.pop('_addanother') def test_add_view(self): @@ -5778,43 +5765,27 @@ class AdminKeepChangeListFiltersTests(TestCase): # Test redirect on "Save". post_data['_save'] = 1 response = self.client.post(self.get_add_url(), data=post_data) - self.assertEqual(response.status_code, 302) - self.assertURLEqual( - response.url, - self.get_change_url(User.objects.get(username='dummy').pk) - ) + self.assertRedirects(response, self.get_change_url(User.objects.get(username='dummy').pk)) post_data.pop('_save') # Test redirect on "Save and continue". post_data['username'] = 'dummy2' post_data['_continue'] = 1 response = self.client.post(self.get_add_url(), data=post_data) - self.assertEqual(response.status_code, 302) - self.assertURLEqual( - response.url, - self.get_change_url(User.objects.get(username='dummy2').pk) - ) + self.assertRedirects(response, self.get_change_url(User.objects.get(username='dummy2').pk)) post_data.pop('_continue') # Test redirect on "Save and add new". post_data['username'] = 'dummy3' post_data['_addanother'] = 1 response = self.client.post(self.get_add_url(), data=post_data) - self.assertEqual(response.status_code, 302) - self.assertURLEqual( - response.url, - self.get_add_url() - ) + self.assertRedirects(response, self.get_add_url()) post_data.pop('_addanother') def test_delete_view(self): # Test redirect on "Delete". response = self.client.post(self.get_delete_url(), {'post': 'yes'}) - self.assertEqual(response.status_code, 302) - self.assertURLEqual( - response.url, - self.get_changelist_url() - ) + self.assertRedirects(response, self.get_changelist_url()) def test_url_prefix(self): context = { diff --git a/tests/auth_tests/test_views.py b/tests/auth_tests/test_views.py index 6482f92c5d..e9f4fce89b 100644 --- a/tests/auth_tests/test_views.py +++ b/tests/auth_tests/test_views.py @@ -709,8 +709,7 @@ class LoginURLSettings(AuthViewsTestCase): """Tests for settings.LOGIN_URL.""" def assertLoginURLEquals(self, url): response = self.client.get('/login_required/') - self.assertEqual(response.status_code, 302) - self.assertURLEqual(response.url, url) + self.assertRedirects(response, url, fetch_redirect_response=False) @override_settings(LOGIN_URL='/login/') def test_standard_login_url(self):