Fixed #8404 -- Isolated auth password-related tests from custom templates

This commit is contained in:
Claude Paroz 2013-02-13 23:10:43 +01:00
parent ac4faa6dc3
commit 142ec8b283
1 changed files with 10 additions and 7 deletions

View File

@ -1,3 +1,4 @@
import itertools
import os import os
import re import re
@ -49,8 +50,10 @@ class AuthViewsTestCase(TestCase):
self.assertTrue(response.url.endswith(settings.LOGIN_REDIRECT_URL)) self.assertTrue(response.url.endswith(settings.LOGIN_REDIRECT_URL))
self.assertTrue(SESSION_KEY in self.client.session) self.assertTrue(SESSION_KEY in self.client.session)
def assertContainsEscaped(self, response, text, **kwargs): def assertFormError(self, response, error):
return self.assertContains(response, escape(force_text(text)), **kwargs) """Assert that error is found in response.context['form'] errors"""
form_errors = list(itertools.chain(*response.context['form'].errors.values()))
self.assertIn(force_text(text), form_errors)
@skipIfCustomUser @skipIfCustomUser
@ -87,7 +90,7 @@ class PasswordResetTest(AuthViewsTestCase):
response = self.client.get('/password_reset/') response = self.client.get('/password_reset/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = self.client.post('/password_reset/', {'email': 'not_a_real_email@email.com'}) response = self.client.post('/password_reset/', {'email': 'not_a_real_email@email.com'})
self.assertContainsEscaped(response, PasswordResetForm.error_messages['unknown']) self.assertFormError(response, PasswordResetForm.error_messages['unknown'])
self.assertEqual(len(mail.outbox), 0) self.assertEqual(len(mail.outbox), 0)
def test_email_found(self): def test_email_found(self):
@ -214,7 +217,7 @@ class PasswordResetTest(AuthViewsTestCase):
url, path = self._test_confirm_start() url, path = self._test_confirm_start()
response = self.client.post(path, {'new_password1': 'anewpassword', response = self.client.post(path, {'new_password1': 'anewpassword',
'new_password2': 'x'}) 'new_password2': 'x'})
self.assertContainsEscaped(response, SetPasswordForm.error_messages['password_mismatch']) self.assertFormError(response, SetPasswordForm.error_messages['password_mismatch'])
@override_settings(AUTH_USER_MODEL='auth.CustomUser') @override_settings(AUTH_USER_MODEL='auth.CustomUser')
@ -248,7 +251,7 @@ class ChangePasswordTest(AuthViewsTestCase):
'username': 'testclient', 'username': 'testclient',
'password': password, 'password': password,
}) })
self.assertContainsEscaped(response, AuthenticationForm.error_messages['invalid_login'] % { self.assertFormError(response, AuthenticationForm.error_messages['invalid_login'] % {
'username': User._meta.get_field('username').verbose_name 'username': User._meta.get_field('username').verbose_name
}) })
@ -262,7 +265,7 @@ class ChangePasswordTest(AuthViewsTestCase):
'new_password1': 'password1', 'new_password1': 'password1',
'new_password2': 'password1', 'new_password2': 'password1',
}) })
self.assertContainsEscaped(response, PasswordChangeForm.error_messages['password_incorrect']) self.assertFormError(response, PasswordChangeForm.error_messages['password_incorrect'])
def test_password_change_fails_with_mismatched_passwords(self): def test_password_change_fails_with_mismatched_passwords(self):
self.login() self.login()
@ -271,7 +274,7 @@ class ChangePasswordTest(AuthViewsTestCase):
'new_password1': 'password1', 'new_password1': 'password1',
'new_password2': 'donuts', 'new_password2': 'donuts',
}) })
self.assertContainsEscaped(response, SetPasswordForm.error_messages['password_mismatch']) self.assertFormError(response, SetPasswordForm.error_messages['password_mismatch'])
def test_password_change_succeeds(self): def test_password_change_succeeds(self):
self.login() self.login()