Commit 3739e71bebec72473fa99fc7786c33c2a6fc5a39
Committed by
Matheus de Sousa Faria
1 parent
ace227ad
Exists in
master
and in
4 other branches
Testing accounts forms
Signed-off-by: Matheus Faria <matheus.sousa.faria@gmail.com> Signed-off-by: TomazMartins <tomaz.r.martins@gmail.com>
Showing
2 changed files
with
106 additions
and
5 deletions
Show diff stats
colab/accounts/tests/test_forms.py
| ... | ... | @@ -4,12 +4,16 @@ Objective: Test parameters, and behavior. |
| 4 | 4 | """ |
| 5 | 5 | |
| 6 | 6 | import datetime |
| 7 | +from mock import patch | |
| 7 | 8 | |
| 8 | 9 | from django.test import TestCase |
| 9 | 10 | from django.core.urlresolvers import reverse |
| 10 | 11 | |
| 11 | -from colab.accounts.forms import UserCreationForm, UserChangeForm | |
| 12 | +from colab.accounts.forms import UserCreationForm, UserChangeForm,\ | |
| 13 | + UserUpdateForm, UserForm, get_lists_choices | |
| 14 | +from colab.accounts import forms as accounts_forms | |
| 12 | 15 | from colab.accounts.models import User |
| 16 | +from colab.accounts.utils import mailman | |
| 13 | 17 | |
| 14 | 18 | |
| 15 | 19 | class FormTest(TestCase): |
| ... | ... | @@ -26,6 +30,9 @@ class FormTest(TestCase): |
| 26 | 30 | user.last_name = "COLAB" |
| 27 | 31 | user.save() |
| 28 | 32 | |
| 33 | + def tearDown(self): | |
| 34 | + pass | |
| 35 | + | |
| 29 | 36 | def create_form_data(self, email, username): |
| 30 | 37 | form_data = {'email': email, |
| 31 | 38 | 'first_name': 'colabName', |
| ... | ... | @@ -36,11 +43,29 @@ class FormTest(TestCase): |
| 36 | 43 | form = UserCreationForm(data=form_data) |
| 37 | 44 | return form |
| 38 | 45 | |
| 46 | + def create_update_form_data(self): | |
| 47 | + updated_data = {'username': "colab", | |
| 48 | + 'email': 'email@email.com', | |
| 49 | + 'last_login': datetime.date.today(), | |
| 50 | + 'date_joined': datetime.date.today(), | |
| 51 | + 'twitter': 'nick_twitter', | |
| 52 | + 'first_name': 'colabName', | |
| 53 | + 'last_name': 'secondName', | |
| 54 | + } | |
| 55 | + initial = {'email': 'email@email.com', | |
| 56 | + 'first_name': 'colabName', | |
| 57 | + 'last_name': 'secondName', | |
| 58 | + 'username': 'colab', | |
| 59 | + 'password': '123colab4'} | |
| 60 | + form = UserUpdateForm(initial=initial, data=updated_data) | |
| 61 | + return form | |
| 62 | + | |
| 39 | 63 | def create_change_form_data(self, username): |
| 40 | 64 | updated_data = {'username': username, |
| 41 | 65 | 'email': 'email@email.com', |
| 42 | 66 | 'last_login': datetime.date.today(), |
| 43 | 67 | 'date_joined': datetime.date.today()} |
| 68 | + | |
| 44 | 69 | initial = {'email': 'email@email.com', |
| 45 | 70 | 'first_name': 'colabName', |
| 46 | 71 | 'last_name': 'secondName', |
| ... | ... | @@ -49,10 +74,20 @@ class FormTest(TestCase): |
| 49 | 74 | form = UserChangeForm(initial=initial, data=updated_data) |
| 50 | 75 | return form |
| 51 | 76 | |
| 77 | + def create_user_form_data(self): | |
| 78 | + initial = {'email': 'email@email.com', | |
| 79 | + 'first_name': 'colabName', | |
| 80 | + 'last_name': 'secondName', | |
| 81 | + 'username': 'colab', | |
| 82 | + 'password': '123colab4'} | |
| 83 | + form = UserForm(data=initial) | |
| 84 | + return form | |
| 85 | + | |
| 52 | 86 | def test_already_registered_email(self): |
| 53 | 87 | form = self.create_form_data('usertest@colab.com.br', |
| 54 | 88 | 'colab') |
| 55 | 89 | self.assertFalse(form.is_valid()) |
| 90 | + self.assertIn('duplicate_email', form.error_messages) | |
| 56 | 91 | |
| 57 | 92 | def test_registered_email_message(self): |
| 58 | 93 | form = self.create_form_data('usertest@colab.com.br', |
| ... | ... | @@ -67,6 +102,12 @@ class FormTest(TestCase): |
| 67 | 102 | 'colab123') |
| 68 | 103 | self.assertTrue(form.is_valid()) |
| 69 | 104 | |
| 105 | + def test_already_created_username(self): | |
| 106 | + form = self.create_form_data('usertest@colab.com.br', | |
| 107 | + 'USERtestCoLaB') | |
| 108 | + self.assertFalse(form.is_valid()) | |
| 109 | + self.assertIn('duplicate_username', form.error_messages) | |
| 110 | + | |
| 70 | 111 | def test_not_valid_username(self): |
| 71 | 112 | form = self.create_form_data('user@email.com', |
| 72 | 113 | 'colab!') |
| ... | ... | @@ -80,5 +121,65 @@ class FormTest(TestCase): |
| 80 | 121 | form = self.create_change_form_data('colab!') |
| 81 | 122 | self.assertFalse(form.is_valid()) |
| 82 | 123 | |
| 83 | - def tearDown(self): | |
| 84 | - pass | |
| 124 | + @patch.object(accounts_forms, "validate_social_account") | |
| 125 | + def test_validate_social_account(self, validate_social_account): | |
| 126 | + validate_social_account.return_value = False | |
| 127 | + | |
| 128 | + form = self.create_update_form_data() | |
| 129 | + self.assertFalse(form.is_valid()) | |
| 130 | + self.assertIn("Social account does not exist", form.errors['twitter']) | |
| 131 | + | |
| 132 | + def test_required_valid_fields_user_form(self): | |
| 133 | + form_data = { | |
| 134 | + 'first_name': 'colabName', | |
| 135 | + 'last_name': 'secondName', | |
| 136 | + 'username': 'colab', | |
| 137 | + } | |
| 138 | + | |
| 139 | + form = UserForm(data=form_data) | |
| 140 | + | |
| 141 | + self.assertTrue(form.is_valid()) | |
| 142 | + | |
| 143 | + def test_required_empty_fields_user_form(self): | |
| 144 | + form_data = { | |
| 145 | + 'first_name': '', | |
| 146 | + 'last_name': '', | |
| 147 | + 'username': '', | |
| 148 | + } | |
| 149 | + | |
| 150 | + form = UserForm(data=form_data) | |
| 151 | + | |
| 152 | + self.assertFalse(form.is_valid()) | |
| 153 | + | |
| 154 | + self.assertIn('first_name', form.errors) | |
| 155 | + self.assertIn('last_name', form.errors) | |
| 156 | + self.assertIn('username', form.errors) | |
| 157 | + | |
| 158 | + def test_blank_required_fields_user_form(self): | |
| 159 | + form_data = { | |
| 160 | + 'first_name': ' ', | |
| 161 | + 'last_name': ' ', | |
| 162 | + 'username': ' ', | |
| 163 | + } | |
| 164 | + | |
| 165 | + form = UserForm(data=form_data) | |
| 166 | + | |
| 167 | + self.assertFalse(form.is_valid()) | |
| 168 | + | |
| 169 | + self.assertIn('first_name', form.errors) | |
| 170 | + self.assertIn('last_name', form.errors) | |
| 171 | + self.assertIn('username', form.errors) | |
| 172 | + | |
| 173 | + @patch.object(mailman, "all_lists") | |
| 174 | + def test_get_list_choices(self, all_lists): | |
| 175 | + all_lists.return_value = [ | |
| 176 | + {'listname': 'listA', 'description': 'A'}, | |
| 177 | + {'listname': 'listB', 'description': 'B'}, | |
| 178 | + {'listname': 'listC', 'description': 'C'}, | |
| 179 | + {'listname': 'listD', 'description': 'D'}, | |
| 180 | + ] | |
| 181 | + lists = get_lists_choices() | |
| 182 | + self.assertEqual(lists, [('listA', u'listA (A)'), | |
| 183 | + ('listB', u'listB (B)'), | |
| 184 | + ('listC', u'listC (C)'), | |
| 185 | + ('listD', u'listD (D)')]) | ... | ... |
colab/accounts/tests/test_user.py
| ... | ... | @@ -43,7 +43,7 @@ class UserTest(TestCase): |
| 43 | 43 | expected_last_name, first_name, last_name): |
| 44 | 44 | data = {'first_name': first_name, |
| 45 | 45 | 'last_name': last_name} |
| 46 | - self.client.post('/account/usertestcolab/edit', data) | |
| 46 | + self.client.post('/account/' + self.user.username + '/edit', data) | |
| 47 | 47 | user = User.objects.get(id=1) |
| 48 | 48 | self.assertEqual(expected_first_name, user.first_name) |
| 49 | 49 | self.assertEqual(expected_last_name, user.last_name) |
| ... | ... | @@ -52,7 +52,7 @@ class UserTest(TestCase): |
| 52 | 52 | data = {'first_name': 'usertestcolab', |
| 53 | 53 | 'last_name': 'colab', |
| 54 | 54 | field_name: value} |
| 55 | - self.client.post('/account/usertestcolab/edit', data) | |
| 55 | + self.client.post('/account/' + self.user.username + '/edit', data) | |
| 56 | 56 | user = User.objects.get(id=1) |
| 57 | 57 | self.assertEqual(expected_value, getattr(user, field_name)) |
| 58 | 58 | ... | ... |