Commit de8b4cdcb918291c210aa0ce3cf7bf10e99de716
Committed by
Lucas Kanashiro
1 parent
54fbd9c0
Exists in
master
and in
8 other branches
Added tests for username regex
Signed-off-by: Lucas Moura <lucas.moura128@gmail.com> Signed-off-by: Matheus Faria <matheus.sousa.faria@gmail.com>
Showing
3 changed files
with
19 additions
and
12 deletions
Show diff stats
colab/accounts/forms.py
@@ -187,6 +187,7 @@ class UserCreationForm(UserForm): | @@ -187,6 +187,7 @@ class UserCreationForm(UserForm): | ||
187 | error_messages={ | 187 | error_messages={ |
188 | 'invalid': _(("This value may contain only" | 188 | 'invalid': _(("This value may contain only" |
189 | " letters and numbers."))}) | 189 | " letters and numbers."))}) |
190 | + | ||
190 | password1 = forms.CharField(label=_("Password"), | 191 | password1 = forms.CharField(label=_("Password"), |
191 | widget=forms.PasswordInput) | 192 | widget=forms.PasswordInput) |
192 | password2 = forms.CharField(label=_("Password confirmation"), | 193 | password2 = forms.CharField(label=_("Password confirmation"), |
@@ -250,7 +251,7 @@ class UserCreationForm(UserForm): | @@ -250,7 +251,7 @@ class UserCreationForm(UserForm): | ||
250 | 251 | ||
251 | class UserChangeForm(forms.ModelForm): | 252 | class UserChangeForm(forms.ModelForm): |
252 | username = forms.RegexField( | 253 | username = forms.RegexField( |
253 | - label=_("Username"), max_length=30, regex=r"^[\w*]", | 254 | + label=_("Username"), max_length=30, regex=r'^[\w.@+-]+$', |
254 | help_text=_("Required. 30 characters or fewer. Letters and digits."), | 255 | help_text=_("Required. 30 characters or fewer. Letters and digits."), |
255 | error_messages={ | 256 | error_messages={ |
256 | 'invalid': _("This value may contain only letters and numbers.")}) | 257 | 'invalid': _("This value may contain only letters and numbers.")}) |
colab/accounts/models.py
@@ -4,7 +4,6 @@ import urlparse | @@ -4,7 +4,6 @@ import urlparse | ||
4 | 4 | ||
5 | from django.db import models | 5 | from django.db import models |
6 | from django.contrib.auth.models import AbstractUser, UserManager | 6 | from django.contrib.auth.models import AbstractUser, UserManager |
7 | -from django.core import validators | ||
8 | from django.core.urlresolvers import reverse | 7 | from django.core.urlresolvers import reverse |
9 | from django.utils.crypto import get_random_string | 8 | from django.utils.crypto import get_random_string |
10 | from django.utils.translation import ugettext_lazy as _ | 9 | from django.utils.translation import ugettext_lazy as _ |
@@ -77,8 +76,3 @@ User._meta.get_field('email')._unique = True | @@ -77,8 +76,3 @@ User._meta.get_field('email')._unique = True | ||
77 | User._meta.get_field('username').help_text = _( | 76 | User._meta.get_field('username').help_text = _( |
78 | u'Required. 30 characters or fewer. Letters and digits.' | 77 | u'Required. 30 characters or fewer. Letters and digits.' |
79 | ) | 78 | ) |
80 | -User._meta.get_field('username').validators[0] = validators.RegexValidator( | ||
81 | - r'^\w+$', | ||
82 | - _('Enter a valid username.'), | ||
83 | - 'invalid' | ||
84 | -) |
colab/accounts/tests/test_forms.py
@@ -24,26 +24,38 @@ class FormTest(TestCase): | @@ -24,26 +24,38 @@ class FormTest(TestCase): | ||
24 | user.last_name = "COLAB" | 24 | user.last_name = "COLAB" |
25 | user.save() | 25 | user.save() |
26 | 26 | ||
27 | - def create_form_data(self): | ||
28 | - form_data = {'email': 'usertest@colab.com.br', | 27 | + def create_form_data(self, email, username): |
28 | + form_data = {'email': email, | ||
29 | 'first_name': 'colabName', | 29 | 'first_name': 'colabName', |
30 | 'last_name': 'secondName', | 30 | 'last_name': 'secondName', |
31 | - 'username': 'colab', | 31 | + 'username': username, |
32 | 'password1': '123colab4', | 32 | 'password1': '123colab4', |
33 | 'password2': '123colab4'} | 33 | 'password2': '123colab4'} |
34 | form = UserCreationForm(data=form_data) | 34 | form = UserCreationForm(data=form_data) |
35 | return form | 35 | return form |
36 | 36 | ||
37 | def test_already_registered_email(self): | 37 | def test_already_registered_email(self): |
38 | - form = self.create_form_data() | 38 | + form = self.create_form_data('usertest@colab.com.br', |
39 | + 'colab') | ||
39 | self.assertFalse(form.is_valid()) | 40 | self.assertFalse(form.is_valid()) |
40 | 41 | ||
41 | def test_registered_email_message(self): | 42 | def test_registered_email_message(self): |
42 | - form = self.create_form_data() | 43 | + form = self.create_form_data('usertest@colab.com.br', |
44 | + 'colab') | ||
43 | msg = form.error_messages.get('duplicate_email') % { | 45 | msg = form.error_messages.get('duplicate_email') % { |
44 | 'url': reverse('login') | 46 | 'url': reverse('login') |
45 | } | 47 | } |
46 | self.assertIn(msg, str(form)) | 48 | self.assertIn(msg, str(form)) |
47 | 49 | ||
50 | + def test_valid_username(self): | ||
51 | + form = self.create_form_data('user@email.com', | ||
52 | + 'colab123@colab-spb.com') | ||
53 | + self.assertTrue(form.is_valid()) | ||
54 | + | ||
55 | + def test_not_valid_username(self): | ||
56 | + form = self.create_form_data('user@email.com', | ||
57 | + 'colab!') | ||
58 | + self.assertFalse(form.is_valid()) | ||
59 | + | ||
48 | def tearDown(self): | 60 | def tearDown(self): |
49 | pass | 61 | pass |