|
0
|
1 |
|
|
|
2 |
FORM_TESTS = """ |
|
|
3 |
>>> from django.contrib.auth.models import User |
|
|
4 |
>>> from django.contrib.auth.forms import UserCreationForm, AuthenticationForm |
|
|
5 |
>>> from django.contrib.auth.forms import PasswordChangeForm, SetPasswordForm |
|
|
6 |
|
|
|
7 |
# The user already exists. |
|
|
8 |
|
|
|
9 |
>>> user = User.objects.create_user("jsmith", "jsmith@example.com", "test123") |
|
|
10 |
>>> data = { |
|
|
11 |
... 'username': 'jsmith', |
|
|
12 |
... 'password1': 'test123', |
|
|
13 |
... 'password2': 'test123', |
|
|
14 |
... } |
|
|
15 |
>>> form = UserCreationForm(data) |
|
|
16 |
>>> form.is_valid() |
|
|
17 |
False |
|
|
18 |
>>> form["username"].errors |
|
|
19 |
[u'A user with that username already exists.'] |
|
|
20 |
|
|
|
21 |
# The username contains invalid data. |
|
|
22 |
|
|
|
23 |
>>> data = { |
|
|
24 |
... 'username': 'jsmith@example.com', |
|
|
25 |
... 'password1': 'test123', |
|
|
26 |
... 'password2': 'test123', |
|
|
27 |
... } |
|
|
28 |
>>> form = UserCreationForm(data) |
|
|
29 |
>>> form.is_valid() |
|
|
30 |
False |
|
|
31 |
>>> form["username"].errors |
|
|
32 |
[u'This value must contain only letters, numbers and underscores.'] |
|
|
33 |
|
|
|
34 |
# The verification password is incorrect. |
|
|
35 |
|
|
|
36 |
>>> data = { |
|
|
37 |
... 'username': 'jsmith2', |
|
|
38 |
... 'password1': 'test123', |
|
|
39 |
... 'password2': 'test', |
|
|
40 |
... } |
|
|
41 |
>>> form = UserCreationForm(data) |
|
|
42 |
>>> form.is_valid() |
|
|
43 |
False |
|
|
44 |
>>> form["password2"].errors |
|
|
45 |
[u"The two password fields didn't match."] |
|
|
46 |
|
|
|
47 |
# One (or both) passwords weren't given |
|
|
48 |
|
|
|
49 |
>>> data = {'username': 'jsmith2'} |
|
|
50 |
>>> form = UserCreationForm(data) |
|
|
51 |
>>> form.is_valid() |
|
|
52 |
False |
|
|
53 |
>>> form['password1'].errors |
|
|
54 |
[u'This field is required.'] |
|
|
55 |
>>> form['password2'].errors |
|
|
56 |
[u'This field is required.'] |
|
|
57 |
|
|
|
58 |
>>> data['password2'] = 'test123' |
|
|
59 |
>>> form = UserCreationForm(data) |
|
|
60 |
>>> form.is_valid() |
|
|
61 |
False |
|
|
62 |
>>> form['password1'].errors |
|
|
63 |
[u'This field is required.'] |
|
|
64 |
|
|
|
65 |
# The success case. |
|
|
66 |
|
|
|
67 |
>>> data = { |
|
|
68 |
... 'username': 'jsmith2', |
|
|
69 |
... 'password1': 'test123', |
|
|
70 |
... 'password2': 'test123', |
|
|
71 |
... } |
|
|
72 |
>>> form = UserCreationForm(data) |
|
|
73 |
>>> form.is_valid() |
|
|
74 |
True |
|
|
75 |
>>> form.save() |
|
|
76 |
<User: jsmith2> |
|
|
77 |
|
|
|
78 |
# The user submits an invalid username. |
|
|
79 |
|
|
|
80 |
>>> data = { |
|
|
81 |
... 'username': 'jsmith_does_not_exist', |
|
|
82 |
... 'password': 'test123', |
|
|
83 |
... } |
|
|
84 |
|
|
|
85 |
>>> form = AuthenticationForm(None, data) |
|
|
86 |
>>> form.is_valid() |
|
|
87 |
False |
|
|
88 |
>>> form.non_field_errors() |
|
|
89 |
[u'Please enter a correct username and password. Note that both fields are case-sensitive.'] |
|
|
90 |
|
|
|
91 |
# The user is inactive. |
|
|
92 |
|
|
|
93 |
>>> data = { |
|
|
94 |
... 'username': 'jsmith', |
|
|
95 |
... 'password': 'test123', |
|
|
96 |
... } |
|
|
97 |
>>> user.is_active = False |
|
|
98 |
>>> user.save() |
|
|
99 |
>>> form = AuthenticationForm(None, data) |
|
|
100 |
>>> form.is_valid() |
|
|
101 |
False |
|
|
102 |
>>> form.non_field_errors() |
|
|
103 |
[u'This account is inactive.'] |
|
|
104 |
|
|
|
105 |
>>> user.is_active = True |
|
|
106 |
>>> user.save() |
|
|
107 |
|
|
|
108 |
# The success case |
|
|
109 |
|
|
|
110 |
>>> form = AuthenticationForm(None, data) |
|
|
111 |
>>> form.is_valid() |
|
|
112 |
True |
|
|
113 |
>>> form.non_field_errors() |
|
|
114 |
[] |
|
|
115 |
|
|
|
116 |
### SetPasswordForm: |
|
|
117 |
|
|
|
118 |
# The two new passwords do not match. |
|
|
119 |
|
|
|
120 |
>>> data = { |
|
|
121 |
... 'new_password1': 'abc123', |
|
|
122 |
... 'new_password2': 'abc', |
|
|
123 |
... } |
|
|
124 |
>>> form = SetPasswordForm(user, data) |
|
|
125 |
>>> form.is_valid() |
|
|
126 |
False |
|
|
127 |
>>> form["new_password2"].errors |
|
|
128 |
[u"The two password fields didn't match."] |
|
|
129 |
|
|
|
130 |
# The success case. |
|
|
131 |
|
|
|
132 |
>>> data = { |
|
|
133 |
... 'new_password1': 'abc123', |
|
|
134 |
... 'new_password2': 'abc123', |
|
|
135 |
... } |
|
|
136 |
>>> form = SetPasswordForm(user, data) |
|
|
137 |
>>> form.is_valid() |
|
|
138 |
True |
|
|
139 |
|
|
|
140 |
### PasswordChangeForm: |
|
|
141 |
|
|
|
142 |
The old password is incorrect. |
|
|
143 |
|
|
|
144 |
>>> data = { |
|
|
145 |
... 'old_password': 'test', |
|
|
146 |
... 'new_password1': 'abc123', |
|
|
147 |
... 'new_password2': 'abc123', |
|
|
148 |
... } |
|
|
149 |
>>> form = PasswordChangeForm(user, data) |
|
|
150 |
>>> form.is_valid() |
|
|
151 |
False |
|
|
152 |
>>> form["old_password"].errors |
|
|
153 |
[u'Your old password was entered incorrectly. Please enter it again.'] |
|
|
154 |
|
|
|
155 |
# The two new passwords do not match. |
|
|
156 |
|
|
|
157 |
>>> data = { |
|
|
158 |
... 'old_password': 'test123', |
|
|
159 |
... 'new_password1': 'abc123', |
|
|
160 |
... 'new_password2': 'abc', |
|
|
161 |
... } |
|
|
162 |
>>> form = PasswordChangeForm(user, data) |
|
|
163 |
>>> form.is_valid() |
|
|
164 |
False |
|
|
165 |
>>> form["new_password2"].errors |
|
|
166 |
[u"The two password fields didn't match."] |
|
|
167 |
|
|
|
168 |
# The success case. |
|
|
169 |
|
|
|
170 |
>>> data = { |
|
|
171 |
... 'old_password': 'test123', |
|
|
172 |
... 'new_password1': 'abc123', |
|
|
173 |
... 'new_password2': 'abc123', |
|
|
174 |
... } |
|
|
175 |
>>> form = PasswordChangeForm(user, data) |
|
|
176 |
>>> form.is_valid() |
|
|
177 |
True |
|
|
178 |
|
|
|
179 |
# Regression test - check the order of fields: |
|
|
180 |
|
|
|
181 |
>>> PasswordChangeForm(user, {}).fields.keys() |
|
|
182 |
['old_password', 'new_password1', 'new_password2'] |
|
|
183 |
|
|
|
184 |
### UserChangeForm |
|
|
185 |
|
|
|
186 |
>>> from django.contrib.auth.forms import UserChangeForm |
|
|
187 |
>>> data = {'username': 'not valid'} |
|
|
188 |
>>> form = UserChangeForm(data, instance=user) |
|
|
189 |
>>> form.is_valid() |
|
|
190 |
False |
|
|
191 |
>>> form['username'].errors |
|
|
192 |
[u'This value must contain only letters, numbers and underscores.'] |
|
|
193 |
|
|
|
194 |
|
|
|
195 |
### PasswordResetForm |
|
|
196 |
|
|
|
197 |
>>> from django.contrib.auth.forms import PasswordResetForm |
|
|
198 |
>>> data = {'email':'not valid'} |
|
|
199 |
>>> form = PasswordResetForm(data) |
|
|
200 |
>>> form.is_valid() |
|
|
201 |
False |
|
|
202 |
>>> form['email'].errors |
|
|
203 |
[u'Enter a valid e-mail address.'] |
|
|
204 |
|
|
|
205 |
# Test nonexistant email address |
|
|
206 |
>>> data = {'email':'foo@bar.com'} |
|
|
207 |
>>> form = PasswordResetForm(data) |
|
|
208 |
>>> form.is_valid() |
|
|
209 |
False |
|
|
210 |
>>> form.errors |
|
|
211 |
{'email': [u"That e-mail address doesn't have an associated user account. Are you sure you've registered?"]} |
|
|
212 |
|
|
|
213 |
# Test cleaned_data bug fix |
|
|
214 |
>>> user = User.objects.create_user("jsmith3", "jsmith3@example.com", "test123") |
|
|
215 |
>>> data = {'email':'jsmith3@example.com'} |
|
|
216 |
>>> form = PasswordResetForm(data) |
|
|
217 |
>>> form.is_valid() |
|
|
218 |
True |
|
|
219 |
>>> form.cleaned_data['email'] |
|
|
220 |
u'jsmith3@example.com' |
|
|
221 |
|
|
|
222 |
""" |