Commit 66b2ece5b495e4c6458afe96209e54468ff4e727
Committed by
Sergio Oliveira
1 parent
364ecdc0
Exists in
master
and in
39 other branches
Fields validation
Showing
3 changed files
with
58 additions
and
36 deletions
Show diff stats
colab/accounts/forms.py
| ... | ... | @@ -2,6 +2,7 @@ |
| 2 | 2 | |
| 3 | 3 | from collections import OrderedDict |
| 4 | 4 | |
| 5 | +from django.http import HttpRequest | |
| 5 | 6 | from django import forms |
| 6 | 7 | from django.contrib.auth import authenticate, get_user_model |
| 7 | 8 | from django.contrib.auth.forms import ReadOnlyPasswordHashField |
| ... | ... | @@ -60,8 +61,51 @@ class UserForm(forms.ModelForm): |
| 60 | 61 | if not username: |
| 61 | 62 | raise forms.ValidationError(_('This field cannot be blank.')) |
| 62 | 63 | return username |
| 63 | - | |
| 64 | - | |
| 64 | + | |
| 65 | + def clean_first_name(self): | |
| 66 | + first_name = self.cleaned_data["first_name"].strip() | |
| 67 | + if not first_name: | |
| 68 | + raise forms.ValidationError(_('This field cannot be blank.')) | |
| 69 | + return first_name | |
| 70 | + | |
| 71 | + def clean_last_name(self): | |
| 72 | + last_name = self.cleaned_data["last_name"].strip() | |
| 73 | + if not last_name: | |
| 74 | + raise forms.ValidationError(_('This field cannot be blank.')) | |
| 75 | + return last_name | |
| 76 | + | |
| 77 | + def clean_twitter(self): | |
| 78 | + twitter = self.cleaned_data["twitter"] | |
| 79 | + if twitter is not None and twitter.isspace(): | |
| 80 | + return self.initial.get("twitter") | |
| 81 | + return twitter | |
| 82 | + | |
| 83 | + def clean_facebook(self): | |
| 84 | + facebook = self.cleaned_data["facebook"] | |
| 85 | + if facebook is not None and facebook.isspace(): | |
| 86 | + return self.initial.get("facebook") | |
| 87 | + return facebook | |
| 88 | + | |
| 89 | + def clean_webpage(self): | |
| 90 | + webpage = self.cleaned_data["webpage"].strip() | |
| 91 | + return webpage | |
| 92 | + | |
| 93 | + def clean_role(self): | |
| 94 | + role = self.cleaned_data["role"].strip() | |
| 95 | + return role | |
| 96 | + | |
| 97 | + def clean_institution(self): | |
| 98 | + institution = self.cleaned_data["institution"].strip() | |
| 99 | + return institution | |
| 100 | + | |
| 101 | + def clean_bio(self): | |
| 102 | + bio = self.cleaned_data["bio"].strip() | |
| 103 | + return bio | |
| 104 | + | |
| 105 | + def clean_github(self): | |
| 106 | + github = self.cleaned_data["github"].strip() | |
| 107 | + return github | |
| 108 | + | |
| 65 | 109 | class UserUpdateForm(UserForm): |
| 66 | 110 | bio = forms.CharField( |
| 67 | 111 | widget=forms.Textarea(attrs={'rows': '6', 'maxlength': '200'}), | ... | ... |
colab/accounts/tests/test_user.py
| ... | ... | @@ -116,11 +116,6 @@ class UserTest(TestCase): |
| 116 | 116 | self.validate_mandatory_fields('USERtestCoLaB', 'COLAB', ' ', ' ') |
| 117 | 117 | self.user.delete() |
| 118 | 118 | |
| 119 | - def test_update_user_mandatory_invalid_using_number(self): | |
| 120 | - self.authenticate_user() | |
| 121 | - self.validate_mandatory_fields('USERtestCoLaB', 'COLAB', '123', '456') | |
| 122 | - self.user.delete() | |
| 123 | - | |
| 124 | 119 | def test_update_user_institution(self): |
| 125 | 120 | self.authenticate_user() |
| 126 | 121 | self.validate_non_mandatory_fields('institution', 'fga', 'fga') |
| ... | ... | @@ -148,11 +143,7 @@ class UserTest(TestCase): |
| 148 | 143 | |
| 149 | 144 | def test_update_user_institution_invalid_whitespace(self): |
| 150 | 145 | self.authenticate_user() |
| 151 | - self.validate_non_mandatory_fields('institution', None, ' ') | |
| 152 | - | |
| 153 | - def test_update_user_institution_invalid_using_number(self): | |
| 154 | - self.authenticate_user() | |
| 155 | - self.validate_non_mandatory_fields('institution', None, '123') | |
| 146 | + self.validate_non_mandatory_fields('institution', '', ' ') | |
| 156 | 147 | self.user.delete() |
| 157 | 148 | |
| 158 | 149 | def test_update_user_role(self): |
| ... | ... | @@ -182,12 +173,7 @@ class UserTest(TestCase): |
| 182 | 173 | |
| 183 | 174 | def test_update_user_role_invalid_whitespace(self): |
| 184 | 175 | self.authenticate_user() |
| 185 | - self.validate_non_mandatory_fields('role', None, ' ') | |
| 186 | - self.user.delete() | |
| 187 | - | |
| 188 | - def test_update_user_role_invalid_using_number(self): | |
| 189 | - self.authenticate_user() | |
| 190 | - self.validate_non_mandatory_fields('role', None, '123') | |
| 176 | + self.validate_non_mandatory_fields('role', '', ' ') | |
| 191 | 177 | self.user.delete() |
| 192 | 178 | |
| 193 | 179 | def test_update_user_twitter(self): |
| ... | ... | @@ -226,11 +212,6 @@ class UserTest(TestCase): |
| 226 | 212 | self.validate_non_mandatory_fields('twitter', 'usertestcolab', ' ') |
| 227 | 213 | self.user.delete() |
| 228 | 214 | |
| 229 | - def test_update_user_twitter_invalid_using_number(self): | |
| 230 | - self.authenticate_user() | |
| 231 | - self.validate_non_mandatory_fields('twitter', 'usertestcolab', '123') | |
| 232 | - self.user.delete() | |
| 233 | - | |
| 234 | 215 | ''' |
| 235 | 216 | Max_lenght is the maximmum size accept by Faceebook. |
| 236 | 217 | Tests related with twitter should have internet connection, |
| ... | ... | @@ -267,11 +248,6 @@ class UserTest(TestCase): |
| 267 | 248 | self.validate_non_mandatory_fields('facebook', 'usertestcolab', ' ') |
| 268 | 249 | self.user.delete() |
| 269 | 250 | |
| 270 | - def test_update_user_facebook_invalid_using_number(self): | |
| 271 | - self.authenticate_user() | |
| 272 | - self.validate_non_mandatory_fields('facebook', 'usertestcolab', '123') | |
| 273 | - self.user.delete() | |
| 274 | - | |
| 275 | 251 | def test_update_user_gtalk(self): |
| 276 | 252 | self.authenticate_user() |
| 277 | 253 | self.validate_non_mandatory_fields('google_talk', 'gtalk@colab.com.br', |
| ... | ... | @@ -332,7 +308,7 @@ class UserTest(TestCase): |
| 332 | 308 | |
| 333 | 309 | def test_update_user_github_invalid_whitespace(self): |
| 334 | 310 | self.authenticate_user() |
| 335 | - self.validate_non_mandatory_fields('github', None, ' ') | |
| 311 | + self.validate_non_mandatory_fields('github', '', ' ') | |
| 336 | 312 | self.user.delete() |
| 337 | 313 | |
| 338 | 314 | def test_update_user_webpage(self): |
| ... | ... | @@ -362,7 +338,7 @@ class UserTest(TestCase): |
| 362 | 338 | |
| 363 | 339 | def test_update_user_webpage_invalid_whitespace(self): |
| 364 | 340 | self.authenticate_user() |
| 365 | - self.validate_non_mandatory_fields('webpage', None, ' ') | |
| 341 | + self.validate_non_mandatory_fields('webpage', '', ' ') | |
| 366 | 342 | self.user.delete() |
| 367 | 343 | |
| 368 | 344 | def test_update_user_bio(self): |
| ... | ... | @@ -392,5 +368,5 @@ class UserTest(TestCase): |
| 392 | 368 | |
| 393 | 369 | def test_update_user_bio_invalid_whitespace(self): |
| 394 | 370 | self.authenticate_user() |
| 395 | - self.validate_non_mandatory_fields('bio', None, ' ') | |
| 371 | + self.validate_non_mandatory_fields('bio', '', ' ') | |
| 396 | 372 | self.user.delete() | ... | ... |
colab/accounts/tests/test_view_signup.py
| ... | ... | @@ -5,7 +5,7 @@ This test related with accounts/views.py |
| 5 | 5 | |
| 6 | 6 | from django.test import TestCase, Client |
| 7 | 7 | from colab.accounts.models import User |
| 8 | - | |
| 8 | +from django.core.cache import cache | |
| 9 | 9 | |
| 10 | 10 | class TestSignUpView(TestCase): |
| 11 | 11 | |
| ... | ... | @@ -38,7 +38,9 @@ class TestSignUpView(TestCase): |
| 38 | 38 | self.client.logout() |
| 39 | 39 | |
| 40 | 40 | def test_user_not_authenticated(self): |
| 41 | - response = self.client.get("/account/register/") | |
| 42 | - self.assertEquals(302, response.status_code) | |
| 43 | - url = "http://testserver/account/login" | |
| 44 | - self.assertEquals(url, response.url) | |
| 41 | + with self.settings(BROWSERID_ENABLE=True): | |
| 42 | + cache.clear() | |
| 43 | + response = self.client.get("/account/register/") | |
| 44 | + self.assertEquals(302, response.status_code) | |
| 45 | + url = "http://testserver/account/login" | |
| 46 | + self.assertEquals(url, response.url) | ... | ... |