blob: d70101cbca405c940923f3b8d3a4136df2f8228e [file] [log] [blame]
srikanth116e6e82014-08-19 07:22:37 -07001#
2# Copyright (c) 2013 Big Switch Networks, Inc.
3#
4# Licensed under the Eclipse Public License, Version 1.0 (the
5# "License"); you may not use this file except in compliance with the
6# License. You may obtain a copy of the License at
7#
8# http://www.eclipse.org/legal/epl-v10.html
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13# implied. See the License for the specific language governing
14# permissions and limitations under the License.
15#
16
17from django.contrib.auth.models import User
18from django.contrib.auth.forms import UserCreationForm, AuthenticationForm, PasswordChangeForm, SetPasswordForm, UserChangeForm, PasswordResetForm
19from django.test import TestCase
20from django.utils.unittest import skipIf
21
22
23class UserCreationFormTest(TestCase):
24
25 fixtures = ['authtestdata.json']
26
27 def test_user_already_exists(self):
28 data = {
29 'username': 'testclient',
30 'password1': 'test123',
31 'password2': 'test123',
32 }
33 form = UserCreationForm(data)
34 self.assertFalse(form.is_valid())
35 self.assertEqual(form["username"].errors,
36 [u'A user with that username already exists.'])
37
38 def test_invalid_data(self):
39 data = {
40 'username': 'jsmith!',
41 'password1': 'test123',
42 'password2': 'test123',
43 }
44 form = UserCreationForm(data)
45 self.assertFalse(form.is_valid())
46 self.assertEqual(form["username"].errors,
47 [u'This value may contain only letters, numbers and @/./+/-/_ characters.'])
48
49
50 def test_password_verification(self):
51 # The verification password is incorrect.
52 data = {
53 'username': 'jsmith',
54 'password1': 'test123',
55 'password2': 'test',
56 }
57 form = UserCreationForm(data)
58 self.assertFalse(form.is_valid())
59 self.assertEqual(form["password2"].errors,
60 [u"The two password fields didn't match."])
61
62
63 def test_both_passwords(self):
64 # One (or both) passwords weren't given
65 data = {'username': 'jsmith'}
66 form = UserCreationForm(data)
67 self.assertFalse(form.is_valid())
68 self.assertEqual(form['password1'].errors,
69 [u'This field is required.'])
70 self.assertEqual(form['password2'].errors,
71 [u'This field is required.'])
72
73
74 data['password2'] = 'test123'
75 form = UserCreationForm(data)
76 self.assertFalse(form.is_valid())
77 self.assertEqual(form['password1'].errors,
78 [u'This field is required.'])
79
80 def test_success(self):
81 # The success case.
82
83 data = {
84 'username': 'jsmith@example.com',
85 'password1': 'test123',
86 'password2': 'test123',
87 }
88 form = UserCreationForm(data)
89 self.assertTrue(form.is_valid())
90 u = form.save()
91 self.assertEqual(repr(u), '<User: jsmith@example.com>')
92
93
94class AuthenticationFormTest(TestCase):
95
96 fixtures = ['authtestdata.json']
97
98 def test_invalid_username(self):
99 # The user submits an invalid username.
100
101 data = {
102 'username': 'jsmith_does_not_exist',
103 'password': 'test123',
104 }
105 form = AuthenticationForm(None, data)
106 self.assertFalse(form.is_valid())
107 self.assertEqual(form.non_field_errors(),
108 [u'Please enter a correct username and password. Note that both fields are case-sensitive.'])
109
110 def test_inactive_user(self):
111 # The user is inactive.
112 data = {
113 'username': 'inactive',
114 'password': 'password',
115 }
116 form = AuthenticationForm(None, data)
117 self.assertFalse(form.is_valid())
118 self.assertEqual(form.non_field_errors(),
119 [u'This account is inactive.'])
120
121
122 def test_success(self):
123 # The success case
124 data = {
125 'username': 'testclient',
126 'password': 'password',
127 }
128 form = AuthenticationForm(None, data)
129 self.assertTrue(form.is_valid())
130 self.assertEqual(form.non_field_errors(), [])
131
132
133class SetPasswordFormTest(TestCase):
134
135 fixtures = ['authtestdata.json']
136
137 def test_password_verification(self):
138 # The two new passwords do not match.
139 user = User.objects.get(username='testclient')
140 data = {
141 'new_password1': 'abc123',
142 'new_password2': 'abc',
143 }
144 form = SetPasswordForm(user, data)
145 self.assertFalse(form.is_valid())
146 self.assertEqual(form["new_password2"].errors,
147 [u"The two password fields didn't match."])
148
149 def test_success(self):
150 user = User.objects.get(username='testclient')
151 data = {
152 'new_password1': 'abc123',
153 'new_password2': 'abc123',
154 }
155 form = SetPasswordForm(user, data)
156 self.assertTrue(form.is_valid())
157
158
159class PasswordChangeFormTest(TestCase):
160
161 fixtures = ['authtestdata.json']
162
163 def test_incorrect_password(self):
164 user = User.objects.get(username='testclient')
165 data = {
166 'old_password': 'test',
167 'new_password1': 'abc123',
168 'new_password2': 'abc123',
169 }
170 form = PasswordChangeForm(user, data)
171 self.assertFalse(form.is_valid())
172 self.assertEqual(form["old_password"].errors,
173 [u'Your old password was entered incorrectly. Please enter it again.'])
174
175
176 def test_password_verification(self):
177 # The two new passwords do not match.
178 user = User.objects.get(username='testclient')
179 data = {
180 'old_password': 'password',
181 'new_password1': 'abc123',
182 'new_password2': 'abc',
183 }
184 form = PasswordChangeForm(user, data)
185 self.assertFalse(form.is_valid())
186 self.assertEqual(form["new_password2"].errors,
187 [u"The two password fields didn't match."])
188
189
190 def test_success(self):
191 # The success case.
192 user = User.objects.get(username='testclient')
193 data = {
194 'old_password': 'password',
195 'new_password1': 'abc123',
196 'new_password2': 'abc123',
197 }
198 form = PasswordChangeForm(user, data)
199 self.assertTrue(form.is_valid())
200
201 def test_field_order(self):
202 # Regression test - check the order of fields:
203 user = User.objects.get(username='testclient')
204 self.assertEqual(PasswordChangeForm(user, {}).fields.keys(),
205 ['old_password', 'new_password1', 'new_password2'])
206
207class UserChangeFormTest(TestCase):
208
209 fixtures = ['authtestdata.json']
210
211 @skipIf(True, "Do not support")
212 def test_username_validity(self):
213 user = User.objects.get(username='testclient')
214 data = {'username': 'not valid'}
215 form = UserChangeForm(data, instance=user)
216 self.assertFalse(form.is_valid())
217 self.assertEqual(form['username'].errors,
218 [u'This value may contain only letters, numbers and @/./+/-/_ characters.'])
219
220 def test_bug_14242(self):
221 # A regression test, introduce by adding an optimization for the
222 # UserChangeForm.
223
224 class MyUserForm(UserChangeForm):
225 def __init__(self, *args, **kwargs):
226 super(MyUserForm, self).__init__(*args, **kwargs)
227 self.fields['groups'].help_text = 'These groups give users different permissions'
228
229 class Meta(UserChangeForm.Meta):
230 fields = ('groups',)
231
232 # Just check we can create it
233 form = MyUserForm({})
234
235
236class PasswordResetFormTest(TestCase):
237
238 fixtures = ['authtestdata.json']
239
240 def test_invalid_email(self):
241 data = {'email':'not valid'}
242 form = PasswordResetForm(data)
243 self.assertFalse(form.is_valid())
244 self.assertEqual(form['email'].errors,
245 [u'Enter a valid e-mail address.'])
246
247 def test_nonexistant_email(self):
248 # Test nonexistant email address
249 data = {'email':'foo@bar.com'}
250 form = PasswordResetForm(data)
251 self.assertFalse(form.is_valid())
252 self.assertEqual(form.errors,
253 {'email': [u"That e-mail address doesn't have an associated user account. Are you sure you've registered?"]})
254
255 def test_cleaned_data(self):
256 # Regression test
257 user = User.objects.create_user("jsmith3", "jsmith3@example.com", "test123")
258 data = {'email':'jsmith3@example.com'}
259 form = PasswordResetForm(data)
260 self.assertTrue(form.is_valid())
261 self.assertEqual(form.cleaned_data['email'], u'jsmith3@example.com')
262
263
264 def test_bug_5605(self):
265 # bug #5605, preserve the case of the user name (before the @ in the
266 # email address) when creating a user.
267 user = User.objects.create_user('forms_test2', 'tesT@EXAMple.com', 'test')
268 self.assertEqual(user.email, 'tesT@example.com')
269 user = User.objects.create_user('forms_test3', 'tesT', 'test')
270 self.assertEqual(user.email, 'tesT')