Converted tests for contrib.auth.forms to unit tests.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13701 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
9e3b327aca
commit
801bb146e8
|
@ -1,7 +1,7 @@
|
||||||
from django.contrib.auth.tests.auth_backends import BackendTest, RowlevelBackendTest, AnonymousUserBackendTest, NoAnonymousUserBackendTest
|
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.basic import BASIC_TESTS
|
||||||
from django.contrib.auth.tests.decorators import LoginRequiredTestCase
|
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 \
|
from django.contrib.auth.tests.remote_user \
|
||||||
import RemoteUserTest, RemoteUserNoCreateTest, RemoteUserCustomTest
|
import RemoteUserTest, RemoteUserNoCreateTest, RemoteUserCustomTest
|
||||||
from django.contrib.auth.tests.models import ProfileTestCase
|
from django.contrib.auth.tests.models import ProfileTestCase
|
||||||
|
@ -13,6 +13,5 @@ from django.contrib.auth.tests.views \
|
||||||
|
|
||||||
__test__ = {
|
__test__ = {
|
||||||
'BASIC_TESTS': BASIC_TESTS,
|
'BASIC_TESTS': BASIC_TESTS,
|
||||||
'FORM_TESTS': FORM_TESTS,
|
|
||||||
'TOKEN_GENERATOR_TESTS': TOKEN_GENERATOR_TESTS,
|
'TOKEN_GENERATOR_TESTS': TOKEN_GENERATOR_TESTS,
|
||||||
}
|
}
|
||||||
|
|
|
@ -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", "jsmith@example.com", "test123")
|
fixtures = ['authtestdata.json']
|
||||||
>>> 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.']
|
|
||||||
|
|
||||||
# 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 = {
|
def test_invalid_data(self):
|
||||||
... 'username': 'jsmith!',
|
data = {
|
||||||
... 'password1': 'test123',
|
'username': 'jsmith!',
|
||||||
... 'password2': 'test123',
|
'password1': 'test123',
|
||||||
... }
|
'password2': 'test123',
|
||||||
>>> form = UserCreationForm(data)
|
}
|
||||||
>>> form.is_valid()
|
form = UserCreationForm(data)
|
||||||
False
|
self.assertFalse(form.is_valid())
|
||||||
>>> form["username"].errors
|
self.assertEqual(form["username"].errors,
|
||||||
[u'This value may contain only letters, numbers and @/./+/-/_ characters.']
|
[u'This value may contain only letters, numbers and @/./+/-/_ characters.'])
|
||||||
|
|
||||||
|
|
||||||
|
def test_password_verification(self):
|
||||||
# The verification password is incorrect.
|
# 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."])
|
||||||
|
|
||||||
>>> 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_both_passwords(self):
|
||||||
# One (or both) passwords weren't given
|
# 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 = {'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.']
|
|
||||||
|
|
||||||
>>> data['password2'] = 'test123'
|
data['password2'] = 'test123'
|
||||||
>>> form = UserCreationForm(data)
|
form = UserCreationForm(data)
|
||||||
>>> form.is_valid()
|
self.assertFalse(form.is_valid())
|
||||||
False
|
self.assertEqual(form['password1'].errors,
|
||||||
>>> form['password1'].errors
|
[u'This field is required.'])
|
||||||
[u'This field is required.']
|
|
||||||
|
|
||||||
|
def test_success(self):
|
||||||
# The success case.
|
# The success case.
|
||||||
|
|
||||||
>>> data = {
|
data = {
|
||||||
... 'username': 'jsmith2@example.com',
|
'username': 'jsmith@example.com',
|
||||||
... 'password1': 'test123',
|
'password1': 'test123',
|
||||||
... 'password2': 'test123',
|
'password2': 'test123',
|
||||||
... }
|
}
|
||||||
>>> form = UserCreationForm(data)
|
form = UserCreationForm(data)
|
||||||
>>> form.is_valid()
|
self.assertTrue(form.is_valid())
|
||||||
True
|
u = form.save()
|
||||||
>>> form.save()
|
self.assertEqual(repr(u), '<User: jsmith@example.com>')
|
||||||
<User: jsmith2@example.com>
|
|
||||||
|
|
||||||
|
|
||||||
|
class AuthenticationFormTest(TestCase):
|
||||||
|
|
||||||
|
fixtures = ['authtestdata.json']
|
||||||
|
|
||||||
|
def test_invalid_username(self):
|
||||||
# The user submits an invalid username.
|
# The user submits an invalid username.
|
||||||
|
|
||||||
>>> data = {
|
data = {
|
||||||
... 'username': 'jsmith_does_not_exist',
|
'username': 'jsmith_does_not_exist',
|
||||||
... 'password': 'test123',
|
'password': 'test123',
|
||||||
... }
|
}
|
||||||
|
form = AuthenticationForm(None, data)
|
||||||
>>> form = AuthenticationForm(None, data)
|
self.assertFalse(form.is_valid())
|
||||||
>>> form.is_valid()
|
self.assertEqual(form.non_field_errors(),
|
||||||
False
|
[u'Please enter a correct username and password. Note that both fields are case-sensitive.'])
|
||||||
>>> form.non_field_errors()
|
|
||||||
[u'Please enter a correct username and password. Note that both fields are case-sensitive.']
|
|
||||||
|
|
||||||
|
def test_inactive_user(self):
|
||||||
# The user is inactive.
|
# 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.'])
|
||||||
|
|
||||||
>>> 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.']
|
|
||||||
|
|
||||||
>>> user.is_active = True
|
|
||||||
>>> user.save()
|
|
||||||
|
|
||||||
|
def test_success(self):
|
||||||
# The success case
|
# The success case
|
||||||
|
data = {
|
||||||
|
'username': 'testclient',
|
||||||
|
'password': 'password',
|
||||||
|
}
|
||||||
|
form = AuthenticationForm(None, data)
|
||||||
|
self.assertTrue(form.is_valid())
|
||||||
|
self.assertEqual(form.non_field_errors(), [])
|
||||||
|
|
||||||
>>> form = AuthenticationForm(None, data)
|
|
||||||
>>> form.is_valid()
|
|
||||||
True
|
|
||||||
>>> form.non_field_errors()
|
|
||||||
[]
|
|
||||||
|
|
||||||
### SetPasswordForm:
|
class SetPasswordFormTest(TestCase):
|
||||||
|
|
||||||
|
fixtures = ['authtestdata.json']
|
||||||
|
|
||||||
|
def test_password_verification(self):
|
||||||
# The two new passwords do not match.
|
# 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."])
|
||||||
|
|
||||||
>>> data = {
|
def test_success(self):
|
||||||
... 'new_password1': 'abc123',
|
user = User.objects.get(username='testclient')
|
||||||
... 'new_password2': 'abc',
|
data = {
|
||||||
... }
|
'new_password1': 'abc123',
|
||||||
>>> form = SetPasswordForm(user, data)
|
'new_password2': 'abc123',
|
||||||
>>> form.is_valid()
|
}
|
||||||
False
|
form = SetPasswordForm(user, data)
|
||||||
>>> form["new_password2"].errors
|
self.assertTrue(form.is_valid())
|
||||||
[u"The two password fields didn't match."]
|
|
||||||
|
|
||||||
# The success case.
|
|
||||||
|
|
||||||
>>> data = {
|
class PasswordChangeFormTest(TestCase):
|
||||||
... 'new_password1': 'abc123',
|
|
||||||
... 'new_password2': 'abc123',
|
|
||||||
... }
|
|
||||||
>>> form = SetPasswordForm(user, data)
|
|
||||||
>>> form.is_valid()
|
|
||||||
True
|
|
||||||
|
|
||||||
### PasswordChangeForm:
|
fixtures = ['authtestdata.json']
|
||||||
|
|
||||||
The old password is incorrect.
|
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.'])
|
||||||
|
|
||||||
>>> 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.']
|
|
||||||
|
|
||||||
|
def test_password_verification(self):
|
||||||
# The two new passwords do not match.
|
# 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."])
|
||||||
|
|
||||||
>>> 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_success(self):
|
||||||
# The success case.
|
# 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())
|
||||||
|
|
||||||
>>> data = {
|
def test_field_order(self):
|
||||||
... 'old_password': 'test123',
|
|
||||||
... 'new_password1': 'abc123',
|
|
||||||
... 'new_password2': 'abc123',
|
|
||||||
... }
|
|
||||||
>>> form = PasswordChangeForm(user, data)
|
|
||||||
>>> form.is_valid()
|
|
||||||
True
|
|
||||||
|
|
||||||
# Regression test - check the order of fields:
|
# 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'])
|
||||||
|
|
||||||
>>> PasswordChangeForm(user, {}).fields.keys()
|
class UserChangeFormTest(TestCase):
|
||||||
['old_password', 'new_password1', 'new_password2']
|
|
||||||
|
|
||||||
### UserChangeForm
|
fixtures = ['authtestdata.json']
|
||||||
|
|
||||||
>>> from django.contrib.auth.forms import UserChangeForm
|
def test_username_validity(self):
|
||||||
>>> data = {'username': 'not valid'}
|
user = User.objects.get(username='testclient')
|
||||||
>>> form = UserChangeForm(data, instance=user)
|
data = {'username': 'not valid'}
|
||||||
>>> form.is_valid()
|
form = UserChangeForm(data, instance=user)
|
||||||
False
|
self.assertFalse(form.is_valid())
|
||||||
>>> form['username'].errors
|
self.assertEqual(form['username'].errors,
|
||||||
[u'This value may contain only letters, numbers and @/./+/-/_ characters.']
|
[u'This value may contain only letters, numbers and @/./+/-/_ characters.'])
|
||||||
|
|
||||||
|
class PasswordResetFormTest(TestCase):
|
||||||
|
|
||||||
### PasswordResetForm
|
fixtures = ['authtestdata.json']
|
||||||
|
|
||||||
>>> from django.contrib.auth.forms import PasswordResetForm
|
def test_invalid_email(self):
|
||||||
>>> data = {'email':'not valid'}
|
data = {'email':'not valid'}
|
||||||
>>> form = PasswordResetForm(data)
|
form = PasswordResetForm(data)
|
||||||
>>> form.is_valid()
|
self.assertFalse(form.is_valid())
|
||||||
False
|
self.assertEqual(form['email'].errors,
|
||||||
>>> form['email'].errors
|
[u'Enter a valid e-mail address.'])
|
||||||
[u'Enter a valid e-mail address.']
|
|
||||||
|
|
||||||
|
def test_nonexistant_email(self):
|
||||||
# Test nonexistant email address
|
# Test nonexistant email address
|
||||||
>>> data = {'email':'foo@bar.com'}
|
data = {'email':'foo@bar.com'}
|
||||||
>>> form = PasswordResetForm(data)
|
form = PasswordResetForm(data)
|
||||||
>>> form.is_valid()
|
self.assertFalse(form.is_valid())
|
||||||
False
|
self.assertEqual(form.errors,
|
||||||
>>> form.errors
|
{'email': [u"That e-mail address doesn't have an associated user account. Are you sure you've registered?"]})
|
||||||
{'email': [u"That e-mail address doesn't have an associated user account. Are you sure you've registered?"]}
|
|
||||||
|
|
||||||
# Test cleaned_data bug fix
|
def test_cleaned_data(self):
|
||||||
>>> user = User.objects.create_user("jsmith3", "jsmith3@example.com", "test123")
|
# Regression test
|
||||||
>>> data = {'email':'jsmith3@example.com'}
|
user = User.objects.create_user("jsmith3", "jsmith3@example.com", "test123")
|
||||||
>>> form = PasswordResetForm(data)
|
data = {'email':'jsmith3@example.com'}
|
||||||
>>> form.is_valid()
|
form = PasswordResetForm(data)
|
||||||
True
|
self.assertTrue(form.is_valid())
|
||||||
>>> form.cleaned_data['email']
|
self.assertEqual(form.cleaned_data['email'], u'jsmith3@example.com')
|
||||||
u'jsmith3@example.com'
|
|
||||||
|
|
||||||
# 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', 'tesT@EXAMple.com', 'test')
|
|
||||||
>>> user.email
|
|
||||||
'tesT@example.com'
|
|
||||||
>>> user = User.objects.create_user('forms_test3', 'tesT', 'test')
|
|
||||||
>>> user.email
|
|
||||||
'tesT'
|
|
||||||
|
|
||||||
"""
|
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', 'tesT@EXAMple.com', 'test')
|
||||||
|
self.assertEqual(user.email, 'tesT@example.com')
|
||||||
|
user = User.objects.create_user('forms_test3', 'tesT', 'test')
|
||||||
|
self.assertEqual(user.email, 'tesT')
|
||||||
|
|
Loading…
Reference in New Issue