Commit c4dd802cf4294f3b5a320c5207c1c41cdc43db35
1 parent
a27468eb
Exists in
master
and in
3 other branches
Adding tests to UserCreationForm
Signed-off-by: Alexandre Barbosa <alexandreab@live.com> Signed-off-by: Lucas Moura <lucas.moura128@gmail.com>
Showing
1 changed file
with
85 additions
and
0 deletions
Show diff stats
colab/accounts/tests/test_forms.py
@@ -8,6 +8,7 @@ from mock import patch | @@ -8,6 +8,7 @@ from mock import patch | ||
8 | 8 | ||
9 | from django.test import TestCase, override_settings | 9 | from django.test import TestCase, override_settings |
10 | from django.core.urlresolvers import reverse | 10 | from django.core.urlresolvers import reverse |
11 | +from django.forms import ValidationError | ||
11 | 12 | ||
12 | from colab.accounts.forms import (UserCreationForm, UserChangeForm, | 13 | from colab.accounts.forms import (UserCreationForm, UserChangeForm, |
13 | UserUpdateForm, UserForm, get_lists_choices, | 14 | UserUpdateForm, UserForm, get_lists_choices, |
@@ -252,3 +253,87 @@ class ChangePasswordFormTestCase(TestCase): | @@ -252,3 +253,87 @@ class ChangePasswordFormTestCase(TestCase): | ||
252 | form = ColabPasswordChangeForm(self.user, data=self.valid_form_data) | 253 | form = ColabPasswordChangeForm(self.user, data=self.valid_form_data) |
253 | self.assertTrue(form.is_valid()) | 254 | self.assertTrue(form.is_valid()) |
254 | validator.assert_called_with('12345') | 255 | validator.assert_called_with('12345') |
256 | + | ||
257 | + | ||
258 | +class UserCreationFormTestCase(TestCase): | ||
259 | + | ||
260 | + @classmethod | ||
261 | + def setUpClass(cls): | ||
262 | + cls.user = User.objects.create_user(username='user1234', | ||
263 | + email='teste1234@example.com', | ||
264 | + first_name='test_first_name', | ||
265 | + last_name='test_last_name') | ||
266 | + | ||
267 | + cls.user.set_password("123colab4") | ||
268 | + cls.user.save() | ||
269 | + | ||
270 | + def get_form_data(self, email, username='test_user', | ||
271 | + password1='12345', password2='12345'): | ||
272 | + return { | ||
273 | + 'first_name': 'test_first_name', | ||
274 | + 'last_name': 'test_last_name', | ||
275 | + 'username': username, | ||
276 | + 'email': email, | ||
277 | + 'password1': password1, | ||
278 | + 'password2': password2 | ||
279 | + } | ||
280 | + | ||
281 | + def test_clean_mail_error(self): | ||
282 | + creation_form = UserCreationForm( | ||
283 | + data=self.get_form_data('teste1234@example.com')) | ||
284 | + self.assertFalse(creation_form.is_valid()) | ||
285 | + self.assertTrue(creation_form.errors.has_key('email')) | ||
286 | + self.assertEqual(1, len(creation_form.errors)) | ||
287 | + | ||
288 | + def test_clean_mail(self): | ||
289 | + creation_form = UserCreationForm( | ||
290 | + data=self.get_form_data('teste12345@example.com')) | ||
291 | + self.assertTrue(creation_form.is_valid()) | ||
292 | + | ||
293 | + def test_clean_username_error(self): | ||
294 | + creation_form = UserCreationForm( | ||
295 | + data=self.get_form_data('teste12345@example.com', | ||
296 | + username='user1234')) | ||
297 | + self.assertFalse(creation_form.is_valid()) | ||
298 | + self.assertTrue(creation_form.errors.has_key('username')) | ||
299 | + self.assertEqual(1, len(creation_form.errors)) | ||
300 | + | ||
301 | + def test_clean_username(self): | ||
302 | + creation_form = UserCreationForm( | ||
303 | + data=self.get_form_data('teste12345@example.com', | ||
304 | + username='user12345')) | ||
305 | + self.assertTrue(creation_form.is_valid()) | ||
306 | + | ||
307 | + | ||
308 | + def test_clean_password2_empty_password1(self): | ||
309 | + creation_form = UserCreationForm( | ||
310 | + data=self.get_form_data('teste12345@example.com', | ||
311 | + username='user12345', | ||
312 | + password1='')) | ||
313 | + self.assertFalse(creation_form.is_valid()) | ||
314 | + self.assertTrue(creation_form.errors.has_key('password1')) | ||
315 | + self.assertEqual(1, len(creation_form.errors)) | ||
316 | + | ||
317 | + def test_clean_password2_empty_password2(self): | ||
318 | + creation_form = UserCreationForm( | ||
319 | + data=self.get_form_data('teste12345@example.com', | ||
320 | + username='user12345', | ||
321 | + password2='')) | ||
322 | + self.assertFalse(creation_form.is_valid()) | ||
323 | + self.assertTrue(creation_form.errors.has_key('password2')) | ||
324 | + | ||
325 | + def test_clean_password2_different_passwords(self): | ||
326 | + creation_form = UserCreationForm( | ||
327 | + data=self.get_form_data('teste12345@example.com', | ||
328 | + username='user12345', | ||
329 | + password1='1234')) | ||
330 | + self.assertFalse(creation_form.is_valid()) | ||
331 | + self.assertTrue(creation_form.errors.has_key('password2')) | ||
332 | + self.assertEqual(1, len(creation_form.errors)) | ||
333 | + self.assertEqual(1, len(creation_form.errors)) | ||
334 | + | ||
335 | + def test_clean_password(self): | ||
336 | + creation_form = UserCreationForm( | ||
337 | + data=self.get_form_data('teste12345@example.com', | ||
338 | + username='user12345')) | ||
339 | + self.assertTrue(creation_form.is_valid()) |