Author: lukeplant
Date: 2010-09-09 18:38:01 -0500 (Thu, 09 Sep 2010)
New Revision: 13703
Modified:
django/branches/releases/1.2.X/
django/branches/releases/1.2.X/django/contrib/auth/tests/__init__.py
django/branches/releases/1.2.X/django/contrib/auth/tests/forms.py
Log:
[1.2.X] Converted tests for contrib.auth.forms to unit tests.
Backport of [13701], needed in order to backport [13702]
Property changes on: django/branches/releases/1.2.X
___________________________________________________________________
Name: svnmerge-integrated
-
/django/trunk:1-13360,13434,13480,13574,13600,13638,13652,13664,13666,13668,13680,13683,13685,13687-13688,13690,13694,13696
+
/django/trunk:1-13360,13434,13480,13574,13600,13638,13652,13664,13666,13668,13680,13683,13685,13687-13688,13690,13694,13696,13701
Modified: django/branches/releases/1.2.X/django/contrib/auth/tests/__init__.py
===================================================================
--- django/branches/releases/1.2.X/django/contrib/auth/tests/__init__.py
2010-09-09 23:31:54 UTC (rev 13702)
+++ django/branches/releases/1.2.X/django/contrib/auth/tests/__init__.py
2010-09-09 23:38:01 UTC (rev 13703)
@@ -1,7 +1,7 @@
from django.contrib.auth.tests.auth_backends import BackendTest,
RowlevelBackendTest, AnonymousUserBackendTest, NoAnonymousUserBackendTest
from django.contrib.auth.tests.basic import BASIC_TESTS
from django.contrib.auth.tests.decorators import LoginRequiredTestCase
-from django.contrib.auth.tests.forms import FORM_TESTS
+from django.contrib.auth.tests.forms import UserCreationFormTest,
AuthenticationFormTest, SetPasswordFormTest, PasswordChangeFormTest,
UserChangeFormTest, PasswordResetFormTest
from django.contrib.auth.tests.remote_user \
import RemoteUserTest, RemoteUserNoCreateTest, RemoteUserCustomTest
from django.contrib.auth.tests.models import ProfileTestCase
@@ -13,6 +13,5 @@
__test__ = {
'BASIC_TESTS': BASIC_TESTS,
- 'FORM_TESTS': FORM_TESTS,
'TOKEN_GENERATOR_TESTS': TOKEN_GENERATOR_TESTS,
}
Modified: django/branches/releases/1.2.X/django/contrib/auth/tests/forms.py
===================================================================
--- django/branches/releases/1.2.X/django/contrib/auth/tests/forms.py
2010-09-09 23:31:54 UTC (rev 13702)
+++ django/branches/releases/1.2.X/django/contrib/auth/tests/forms.py
2010-09-09 23:38:01 UTC (rev 13703)
@@ -1,231 +1,236 @@
+from django.contrib.auth.models import User
+from django.contrib.auth.forms import UserCreationForm, AuthenticationForm,
PasswordChangeForm, SetPasswordForm, UserChangeForm, PasswordResetForm
+from django.test import TestCase
-FORM_TESTS = """
->>> from django.contrib.auth.models import User
->>> from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
->>> from django.contrib.auth.forms import PasswordChangeForm, SetPasswordForm
-# The user already exists.
+class UserCreationFormTest(TestCase):
->>> user = User.objects.create_user("jsmith", "[email protected]", "test123")
->>> data = {
-... 'username': 'jsmith',
-... 'password1': 'test123',
-... 'password2': 'test123',
-... }
->>> form = UserCreationForm(data)
->>> form.is_valid()
-False
->>> form["username"].errors
-[u'A user with that username already exists.']
+ fixtures = ['authtestdata.json']
-# The username contains invalid data.
+ def test_user_already_exists(self):
+ data = {
+ 'username': 'testclient',
+ 'password1': 'test123',
+ 'password2': 'test123',
+ }
+ form = UserCreationForm(data)
+ self.assertFalse(form.is_valid())
+ self.assertEqual(form["username"].errors,
+ [u'A user with that username already exists.'])
->>> data = {
-... 'username': 'jsmith!',
-... 'password1': 'test123',
-... 'password2': 'test123',
-... }
->>> form = UserCreationForm(data)
->>> form.is_valid()
-False
->>> form["username"].errors
-[u'This value may contain only letters, numbers and @/./+/-/_ characters.']
+ def test_invalid_data(self):
+ data = {
+ 'username': 'jsmith!',
+ 'password1': 'test123',
+ 'password2': 'test123',
+ }
+ form = UserCreationForm(data)
+ self.assertFalse(form.is_valid())
+ self.assertEqual(form["username"].errors,
+ [u'This value may contain only letters, numbers and
@/./+/-/_ characters.'])
-# The verification password is incorrect.
->>> data = {
-... 'username': 'jsmith2',
-... 'password1': 'test123',
-... 'password2': 'test',
-... }
->>> form = UserCreationForm(data)
->>> form.is_valid()
-False
->>> form["password2"].errors
-[u"The two password fields didn't match."]
+ def test_password_verification(self):
+ # The verification password is incorrect.
+ data = {
+ 'username': 'jsmith',
+ 'password1': 'test123',
+ 'password2': 'test',
+ }
+ form = UserCreationForm(data)
+ self.assertFalse(form.is_valid())
+ self.assertEqual(form["password2"].errors,
+ [u"The two password fields didn't match."])
-# One (or both) passwords weren't given
->>> data = {'username': 'jsmith2'}
->>> form = UserCreationForm(data)
->>> form.is_valid()
-False
->>> form['password1'].errors
-[u'This field is required.']
->>> form['password2'].errors
-[u'This field is required.']
+ def test_both_passwords(self):
+ # One (or both) passwords weren't given
+ data = {'username': 'jsmith'}
+ form = UserCreationForm(data)
+ self.assertFalse(form.is_valid())
+ self.assertEqual(form['password1'].errors,
+ [u'This field is required.'])
+ self.assertEqual(form['password2'].errors,
+ [u'This field is required.'])
->>> data['password2'] = 'test123'
->>> form = UserCreationForm(data)
->>> form.is_valid()
-False
->>> form['password1'].errors
-[u'This field is required.']
-# The success case.
+ data['password2'] = 'test123'
+ form = UserCreationForm(data)
+ self.assertFalse(form.is_valid())
+ self.assertEqual(form['password1'].errors,
+ [u'This field is required.'])
->>> data = {
-... 'username': '[email protected]',
-... 'password1': 'test123',
-... 'password2': 'test123',
-... }
->>> form = UserCreationForm(data)
->>> form.is_valid()
-True
->>> form.save()
-<User: [email protected]>
+ def test_success(self):
+ # The success case.
-# The user submits an invalid username.
+ data = {
+ 'username': '[email protected]',
+ 'password1': 'test123',
+ 'password2': 'test123',
+ }
+ form = UserCreationForm(data)
+ self.assertTrue(form.is_valid())
+ u = form.save()
+ self.assertEqual(repr(u), '<User: [email protected]>')
->>> data = {
-... 'username': 'jsmith_does_not_exist',
-... 'password': 'test123',
-... }
->>> form = AuthenticationForm(None, data)
->>> form.is_valid()
-False
->>> form.non_field_errors()
-[u'Please enter a correct username and password. Note that both fields are
case-sensitive.']
+class AuthenticationFormTest(TestCase):
-# The user is inactive.
+ fixtures = ['authtestdata.json']
->>> data = {
-... 'username': 'jsmith',
-... 'password': 'test123',
-... }
->>> user.is_active = False
->>> user.save()
->>> form = AuthenticationForm(None, data)
->>> form.is_valid()
-False
->>> form.non_field_errors()
-[u'This account is inactive.']
+ def test_invalid_username(self):
+ # The user submits an invalid username.
->>> user.is_active = True
->>> user.save()
+ data = {
+ 'username': 'jsmith_does_not_exist',
+ 'password': 'test123',
+ }
+ form = AuthenticationForm(None, data)
+ self.assertFalse(form.is_valid())
+ self.assertEqual(form.non_field_errors(),
+ [u'Please enter a correct username and password. Note
that both fields are case-sensitive.'])
-# The success case
+ def test_inactive_user(self):
+ # The user is inactive.
+ data = {
+ 'username': 'inactive',
+ 'password': 'password',
+ }
+ form = AuthenticationForm(None, data)
+ self.assertFalse(form.is_valid())
+ self.assertEqual(form.non_field_errors(),
+ [u'This account is inactive.'])
->>> form = AuthenticationForm(None, data)
->>> form.is_valid()
-True
->>> form.non_field_errors()
-[]
-### SetPasswordForm:
+ def test_success(self):
+ # The success case
+ data = {
+ 'username': 'testclient',
+ 'password': 'password',
+ }
+ form = AuthenticationForm(None, data)
+ self.assertTrue(form.is_valid())
+ self.assertEqual(form.non_field_errors(), [])
-# The two new passwords do not match.
->>> data = {
-... 'new_password1': 'abc123',
-... 'new_password2': 'abc',
-... }
->>> form = SetPasswordForm(user, data)
->>> form.is_valid()
-False
->>> form["new_password2"].errors
-[u"The two password fields didn't match."]
+class SetPasswordFormTest(TestCase):
-# The success case.
+ fixtures = ['authtestdata.json']
->>> data = {
-... 'new_password1': 'abc123',
-... 'new_password2': 'abc123',
-... }
->>> form = SetPasswordForm(user, data)
->>> form.is_valid()
-True
+ def test_password_verification(self):
+ # The two new passwords do not match.
+ user = User.objects.get(username='testclient')
+ data = {
+ 'new_password1': 'abc123',
+ 'new_password2': 'abc',
+ }
+ form = SetPasswordForm(user, data)
+ self.assertFalse(form.is_valid())
+ self.assertEqual(form["new_password2"].errors,
+ [u"The two password fields didn't match."])
-### PasswordChangeForm:
+ def test_success(self):
+ user = User.objects.get(username='testclient')
+ data = {
+ 'new_password1': 'abc123',
+ 'new_password2': 'abc123',
+ }
+ form = SetPasswordForm(user, data)
+ self.assertTrue(form.is_valid())
-The old password is incorrect.
->>> data = {
-... 'old_password': 'test',
-... 'new_password1': 'abc123',
-... 'new_password2': 'abc123',
-... }
->>> form = PasswordChangeForm(user, data)
->>> form.is_valid()
-False
->>> form["old_password"].errors
-[u'Your old password was entered incorrectly. Please enter it again.']
+class PasswordChangeFormTest(TestCase):
-# The two new passwords do not match.
+ fixtures = ['authtestdata.json']
->>> data = {
-... 'old_password': 'test123',
-... 'new_password1': 'abc123',
-... 'new_password2': 'abc',
-... }
->>> form = PasswordChangeForm(user, data)
->>> form.is_valid()
-False
->>> form["new_password2"].errors
-[u"The two password fields didn't match."]
+ def test_incorrect_password(self):
+ user = User.objects.get(username='testclient')
+ data = {
+ 'old_password': 'test',
+ 'new_password1': 'abc123',
+ 'new_password2': 'abc123',
+ }
+ form = PasswordChangeForm(user, data)
+ self.assertFalse(form.is_valid())
+ self.assertEqual(form["old_password"].errors,
+ [u'Your old password was entered incorrectly. Please
enter it again.'])
-# The success case.
->>> data = {
-... 'old_password': 'test123',
-... 'new_password1': 'abc123',
-... 'new_password2': 'abc123',
-... }
->>> form = PasswordChangeForm(user, data)
->>> form.is_valid()
-True
+ def test_password_verification(self):
+ # The two new passwords do not match.
+ user = User.objects.get(username='testclient')
+ data = {
+ 'old_password': 'password',
+ 'new_password1': 'abc123',
+ 'new_password2': 'abc',
+ }
+ form = PasswordChangeForm(user, data)
+ self.assertFalse(form.is_valid())
+ self.assertEqual(form["new_password2"].errors,
+ [u"The two password fields didn't match."])
-# Regression test - check the order of fields:
->>> PasswordChangeForm(user, {}).fields.keys()
-['old_password', 'new_password1', 'new_password2']
+ def test_success(self):
+ # The success case.
+ user = User.objects.get(username='testclient')
+ data = {
+ 'old_password': 'password',
+ 'new_password1': 'abc123',
+ 'new_password2': 'abc123',
+ }
+ form = PasswordChangeForm(user, data)
+ self.assertTrue(form.is_valid())
-### UserChangeForm
+ def test_field_order(self):
+ # Regression test - check the order of fields:
+ user = User.objects.get(username='testclient')
+ self.assertEqual(PasswordChangeForm(user, {}).fields.keys(),
+ ['old_password', 'new_password1', 'new_password2'])
->>> from django.contrib.auth.forms import UserChangeForm
->>> data = {'username': 'not valid'}
->>> form = UserChangeForm(data, instance=user)
->>> form.is_valid()
-False
->>> form['username'].errors
-[u'This value may contain only letters, numbers and @/./+/-/_ characters.']
+class UserChangeFormTest(TestCase):
+ fixtures = ['authtestdata.json']
-### PasswordResetForm
+ def test_username_validity(self):
+ user = User.objects.get(username='testclient')
+ data = {'username': 'not valid'}
+ form = UserChangeForm(data, instance=user)
+ self.assertFalse(form.is_valid())
+ self.assertEqual(form['username'].errors,
+ [u'This value may contain only letters, numbers and
@/./+/-/_ characters.'])
->>> from django.contrib.auth.forms import PasswordResetForm
->>> data = {'email':'not valid'}
->>> form = PasswordResetForm(data)
->>> form.is_valid()
-False
->>> form['email'].errors
-[u'Enter a valid e-mail address.']
+class PasswordResetFormTest(TestCase):
-# Test nonexistant email address
->>> data = {'email':'[email protected]'}
->>> form = PasswordResetForm(data)
->>> form.is_valid()
-False
->>> form.errors
-{'email': [u"That e-mail address doesn't have an associated user account. Are
you sure you've registered?"]}
+ fixtures = ['authtestdata.json']
-# Test cleaned_data bug fix
->>> user = User.objects.create_user("jsmith3", "[email protected]",
"test123")
->>> data = {'email':'[email protected]'}
->>> form = PasswordResetForm(data)
->>> form.is_valid()
-True
->>> form.cleaned_data['email']
-u'[email protected]'
+ def test_invalid_email(self):
+ data = {'email':'not valid'}
+ form = PasswordResetForm(data)
+ self.assertFalse(form.is_valid())
+ self.assertEqual(form['email'].errors,
+ [u'Enter a valid e-mail address.'])
-# bug #5605, preserve the case of the user name (before the @ in the email
address)
-# when creating a user.
->>> user = User.objects.create_user('forms_test2', '[email protected]', 'test')
->>> user.email
-'[email protected]'
->>> user = User.objects.create_user('forms_test3', 'tesT', 'test')
->>> user.email
-'tesT'
+ def test_nonexistant_email(self):
+ # Test nonexistant email address
+ data = {'email':'[email protected]'}
+ form = PasswordResetForm(data)
+ self.assertFalse(form.is_valid())
+ self.assertEqual(form.errors,
+ {'email': [u"That e-mail address doesn't have an
associated user account. Are you sure you've registered?"]})
-"""
+ def test_cleaned_data(self):
+ # Regression test
+ user = User.objects.create_user("jsmith3", "[email protected]",
"test123")
+ data = {'email':'[email protected]'}
+ form = PasswordResetForm(data)
+ self.assertTrue(form.is_valid())
+ self.assertEqual(form.cleaned_data['email'], u'[email protected]')
+
+
+ def test_bug_5605(self):
+ # bug #5605, preserve the case of the user name (before the @ in the
+ # email address) when creating a user.
+ user = User.objects.create_user('forms_test2', '[email protected]',
'test')
+ self.assertEqual(user.email, '[email protected]')
+ user = User.objects.create_user('forms_test3', 'tesT', 'test')
+ self.assertEqual(user.email, 'tesT')
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en.