Simplified some auth_tests with assertRedirects().

This commit is contained in:
Tim Graham 2016-10-28 11:52:52 -04:00 committed by GitHub
parent 21dd792349
commit 20be1918e7
1 changed files with 26 additions and 53 deletions

View File

@ -274,45 +274,38 @@ class PasswordResetTest(AuthViewsTestCase):
def test_reset_redirect_default(self): def test_reset_redirect_default(self):
response = self.client.post('/password_reset/', {'email': 'staffmember@example.com'}) response = self.client.post('/password_reset/', {'email': 'staffmember@example.com'})
self.assertEqual(response.status_code, 302) self.assertRedirects(response, '/password_reset/done/', fetch_redirect_response=False)
self.assertURLEqual(response.url, '/password_reset/done/')
def test_reset_custom_redirect(self): def test_reset_custom_redirect(self):
response = self.client.post('/password_reset/custom_redirect/', {'email': 'staffmember@example.com'}) response = self.client.post('/password_reset/custom_redirect/', {'email': 'staffmember@example.com'})
self.assertEqual(response.status_code, 302) self.assertRedirects(response, '/custom/', fetch_redirect_response=False)
self.assertURLEqual(response.url, '/custom/')
def test_reset_custom_redirect_named(self): def test_reset_custom_redirect_named(self):
response = self.client.post('/password_reset/custom_redirect/named/', {'email': 'staffmember@example.com'}) response = self.client.post('/password_reset/custom_redirect/named/', {'email': 'staffmember@example.com'})
self.assertEqual(response.status_code, 302) self.assertRedirects(response, '/password_reset/', fetch_redirect_response=False)
self.assertURLEqual(response.url, '/password_reset/')
def test_confirm_redirect_default(self): def test_confirm_redirect_default(self):
url, path = self._test_confirm_start() url, path = self._test_confirm_start()
response = self.client.post(path, {'new_password1': 'anewpassword', 'new_password2': 'anewpassword'}) response = self.client.post(path, {'new_password1': 'anewpassword', 'new_password2': 'anewpassword'})
self.assertEqual(response.status_code, 302) self.assertRedirects(response, '/reset/done/', fetch_redirect_response=False)
self.assertURLEqual(response.url, '/reset/done/')
def test_confirm_redirect_custom(self): def test_confirm_redirect_custom(self):
url, path = self._test_confirm_start() url, path = self._test_confirm_start()
path = path.replace('/reset/', '/reset/custom/') path = path.replace('/reset/', '/reset/custom/')
response = self.client.post(path, {'new_password1': 'anewpassword', 'new_password2': 'anewpassword'}) response = self.client.post(path, {'new_password1': 'anewpassword', 'new_password2': 'anewpassword'})
self.assertEqual(response.status_code, 302) self.assertRedirects(response, '/custom/', fetch_redirect_response=False)
self.assertURLEqual(response.url, '/custom/')
def test_confirm_redirect_custom_named(self): def test_confirm_redirect_custom_named(self):
url, path = self._test_confirm_start() url, path = self._test_confirm_start()
path = path.replace('/reset/', '/reset/custom/named/') path = path.replace('/reset/', '/reset/custom/named/')
response = self.client.post(path, {'new_password1': 'anewpassword', 'new_password2': 'anewpassword'}) response = self.client.post(path, {'new_password1': 'anewpassword', 'new_password2': 'anewpassword'})
self.assertEqual(response.status_code, 302) self.assertRedirects(response, '/password_reset/', fetch_redirect_response=False)
self.assertURLEqual(response.url, '/password_reset/')
def test_confirm_login_post_reset(self): def test_confirm_login_post_reset(self):
url, path = self._test_confirm_start() url, path = self._test_confirm_start()
path = path.replace('/reset/', '/reset/post_reset_login/') path = path.replace('/reset/', '/reset/post_reset_login/')
response = self.client.post(path, {'new_password1': 'anewpassword', 'new_password2': 'anewpassword'}) response = self.client.post(path, {'new_password1': 'anewpassword', 'new_password2': 'anewpassword'})
self.assertEqual(response.status_code, 302) self.assertRedirects(response, '/reset/done/', fetch_redirect_response=False)
self.assertURLEqual(response.url, '/reset/done/')
self.assertIn(SESSION_KEY, self.client.session) self.assertIn(SESSION_KEY, self.client.session)
def test_confirm_display_user_from_form(self): def test_confirm_display_user_from_form(self):
@ -432,14 +425,12 @@ class ChangePasswordTest(AuthViewsTestCase):
'new_password1': 'password1', 'new_password1': 'password1',
'new_password2': 'password1', 'new_password2': 'password1',
}) })
self.assertEqual(response.status_code, 302) self.assertRedirects(response, '/password_change/done/', fetch_redirect_response=False)
self.assertURLEqual(response.url, '/password_change/done/')
@override_settings(LOGIN_URL='/login/') @override_settings(LOGIN_URL='/login/')
def test_password_change_done_fails(self): def test_password_change_done_fails(self):
response = self.client.get('/password_change/done/') response = self.client.get('/password_change/done/')
self.assertEqual(response.status_code, 302) self.assertRedirects(response, '/login/?next=/password_change/done/', fetch_redirect_response=False)
self.assertURLEqual(response.url, '/login/?next=/password_change/done/')
def test_password_change_redirect_default(self): def test_password_change_redirect_default(self):
self.login() self.login()
@ -448,8 +439,7 @@ class ChangePasswordTest(AuthViewsTestCase):
'new_password1': 'password1', 'new_password1': 'password1',
'new_password2': 'password1', 'new_password2': 'password1',
}) })
self.assertEqual(response.status_code, 302) self.assertRedirects(response, '/password_change/done/', fetch_redirect_response=False)
self.assertURLEqual(response.url, '/password_change/done/')
def test_password_change_redirect_custom(self): def test_password_change_redirect_custom(self):
self.login() self.login()
@ -458,8 +448,7 @@ class ChangePasswordTest(AuthViewsTestCase):
'new_password1': 'password1', 'new_password1': 'password1',
'new_password2': 'password1', 'new_password2': 'password1',
}) })
self.assertEqual(response.status_code, 302) self.assertRedirects(response, '/custom/', fetch_redirect_response=False)
self.assertURLEqual(response.url, '/custom/')
def test_password_change_redirect_custom_named(self): def test_password_change_redirect_custom_named(self):
self.login() self.login()
@ -468,8 +457,7 @@ class ChangePasswordTest(AuthViewsTestCase):
'new_password1': 'password1', 'new_password1': 'password1',
'new_password2': 'password1', 'new_password2': 'password1',
}) })
self.assertEqual(response.status_code, 302) self.assertRedirects(response, '/password_reset/', fetch_redirect_response=False)
self.assertURLEqual(response.url, '/password_reset/')
class SessionAuthenticationTests(AuthViewsTestCase): class SessionAuthenticationTests(AuthViewsTestCase):
@ -704,8 +692,7 @@ class LoginRedirectUrlTest(AuthViewsTestCase):
"""Tests for settings.LOGIN_REDIRECT_URL.""" """Tests for settings.LOGIN_REDIRECT_URL."""
def assertLoginRedirectURLEqual(self, url): def assertLoginRedirectURLEqual(self, url):
response = self.login() response = self.login()
self.assertEqual(response.status_code, 302) self.assertRedirects(response, url, fetch_redirect_response=False)
self.assertURLEqual(response.url, url)
def test_default(self): def test_default(self):
self.assertLoginRedirectURLEqual('/accounts/profile/') self.assertLoginRedirectURLEqual('/accounts/profile/')
@ -830,8 +817,7 @@ class LoginSuccessURLAllowedHostsTest(AuthViewsTestCase):
'next': 'https://testserver/home', 'next': 'https://testserver/home',
}) })
self.assertIn(SESSION_KEY, self.client.session) self.assertIn(SESSION_KEY, self.client.session)
self.assertEqual(response.status_code, 302) self.assertRedirects(response, 'https://testserver/home', fetch_redirect_response=False)
self.assertURLEqual(response.url, 'https://testserver/home')
def test_success_url_allowed_hosts_safe_host(self): def test_success_url_allowed_hosts_safe_host(self):
response = self.client.post('/login/allowed_hosts/', { response = self.client.post('/login/allowed_hosts/', {
@ -840,8 +826,7 @@ class LoginSuccessURLAllowedHostsTest(AuthViewsTestCase):
'next': 'https://otherserver/home', 'next': 'https://otherserver/home',
}) })
self.assertIn(SESSION_KEY, self.client.session) self.assertIn(SESSION_KEY, self.client.session)
self.assertEqual(response.status_code, 302) self.assertRedirects(response, 'https://otherserver/home', fetch_redirect_response=False)
self.assertURLEqual(response.url, 'https://otherserver/home')
def test_success_url_allowed_hosts_unsafe_host(self): def test_success_url_allowed_hosts_unsafe_host(self):
response = self.client.post('/login/allowed_hosts/', { response = self.client.post('/login/allowed_hosts/', {
@ -850,8 +835,7 @@ class LoginSuccessURLAllowedHostsTest(AuthViewsTestCase):
'next': 'https://evil/home', 'next': 'https://evil/home',
}) })
self.assertIn(SESSION_KEY, self.client.session) self.assertIn(SESSION_KEY, self.client.session)
self.assertEqual(response.status_code, 302) self.assertRedirects(response, '/accounts/profile/', fetch_redirect_response=False)
self.assertURLEqual(response.url, '/accounts/profile/')
class LogoutTest(AuthViewsTestCase): class LogoutTest(AuthViewsTestCase):
@ -884,12 +868,10 @@ class LogoutTest(AuthViewsTestCase):
# Bug 11223 # Bug 11223
self.login() self.login()
response = self.client.get('/logout/next_page/') response = self.client.get('/logout/next_page/')
self.assertEqual(response.status_code, 302) self.assertRedirects(response, '/somewhere/', fetch_redirect_response=False)
self.assertURLEqual(response.url, '/somewhere/')
response = self.client.get('/logout/next_page/?next=/login/') response = self.client.get('/logout/next_page/?next=/login/')
self.assertEqual(response.status_code, 302) self.assertRedirects(response, '/login/', fetch_redirect_response=False)
self.assertURLEqual(response.url, '/login/')
self.confirm_logged_out() self.confirm_logged_out()
@ -897,53 +879,46 @@ class LogoutTest(AuthViewsTestCase):
"Logout with next_page option given redirects to specified resource" "Logout with next_page option given redirects to specified resource"
self.login() self.login()
response = self.client.get('/logout/next_page/') response = self.client.get('/logout/next_page/')
self.assertEqual(response.status_code, 302) self.assertRedirects(response, '/somewhere/', fetch_redirect_response=False)
self.assertURLEqual(response.url, '/somewhere/')
self.confirm_logged_out() self.confirm_logged_out()
def test_logout_with_redirect_argument(self): def test_logout_with_redirect_argument(self):
"Logout with query string redirects to specified resource" "Logout with query string redirects to specified resource"
self.login() self.login()
response = self.client.get('/logout/?next=/login/') response = self.client.get('/logout/?next=/login/')
self.assertEqual(response.status_code, 302) self.assertRedirects(response, '/login/', fetch_redirect_response=False)
self.assertURLEqual(response.url, '/login/')
self.confirm_logged_out() self.confirm_logged_out()
def test_logout_with_custom_redirect_argument(self): def test_logout_with_custom_redirect_argument(self):
"Logout with custom query string redirects to specified resource" "Logout with custom query string redirects to specified resource"
self.login() self.login()
response = self.client.get('/logout/custom_query/?follow=/somewhere/') response = self.client.get('/logout/custom_query/?follow=/somewhere/')
self.assertEqual(response.status_code, 302) self.assertRedirects(response, '/somewhere/', fetch_redirect_response=False)
self.assertURLEqual(response.url, '/somewhere/')
self.confirm_logged_out() self.confirm_logged_out()
def test_logout_with_named_redirect(self): def test_logout_with_named_redirect(self):
"Logout resolves names or URLs passed as next_page." "Logout resolves names or URLs passed as next_page."
self.login() self.login()
response = self.client.get('/logout/next_page/named/') response = self.client.get('/logout/next_page/named/')
self.assertEqual(response.status_code, 302) self.assertRedirects(response, '/password_reset/', fetch_redirect_response=False)
self.assertURLEqual(response.url, '/password_reset/')
self.confirm_logged_out() self.confirm_logged_out()
def test_success_url_allowed_hosts_same_host(self): def test_success_url_allowed_hosts_same_host(self):
self.login() self.login()
response = self.client.get('/logout/allowed_hosts/?next=https://testserver/') response = self.client.get('/logout/allowed_hosts/?next=https://testserver/')
self.assertEqual(response.status_code, 302) self.assertRedirects(response, 'https://testserver/', fetch_redirect_response=False)
self.assertURLEqual(response.url, 'https://testserver/')
self.confirm_logged_out() self.confirm_logged_out()
def test_success_url_allowed_hosts_safe_host(self): def test_success_url_allowed_hosts_safe_host(self):
self.login() self.login()
response = self.client.get('/logout/allowed_hosts/?next=https://otherserver/') response = self.client.get('/logout/allowed_hosts/?next=https://otherserver/')
self.assertEqual(response.status_code, 302) self.assertRedirects(response, 'https://otherserver/', fetch_redirect_response=False)
self.assertURLEqual(response.url, 'https://otherserver/')
self.confirm_logged_out() self.confirm_logged_out()
def test_success_url_allowed_hosts_unsafe_host(self): def test_success_url_allowed_hosts_unsafe_host(self):
self.login() self.login()
response = self.client.get('/logout/allowed_hosts/?next=https://evil/') response = self.client.get('/logout/allowed_hosts/?next=https://evil/')
self.assertEqual(response.status_code, 302) self.assertRedirects(response, '/logout/allowed_hosts/', fetch_redirect_response=False)
self.assertURLEqual(response.url, '/logout/allowed_hosts/')
self.confirm_logged_out() self.confirm_logged_out()
def test_security_check(self): def test_security_check(self):
@ -999,9 +974,7 @@ class LogoutTest(AuthViewsTestCase):
} }
self.login() self.login()
response = self.client.get(url, secure=True) response = self.client.get(url, secure=True)
self.assertEqual(response.status_code, 302) self.assertRedirects(response, logout_url, fetch_redirect_response=False)
self.assertNotEqual(response.url, non_https_next_url)
self.assertEqual(response.url, logout_url)
self.confirm_logged_out() self.confirm_logged_out()
def test_logout_preserve_language(self): def test_logout_preserve_language(self):