Commit 22f9c38f3ec4706c5319e7f9c23ad1fa30727356
1 parent
1b21cac6
Exists in
user_regex
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
| ... | ... | @@ -170,6 +170,7 @@ class UserCreationForm(UserForm): |
| 170 | 170 | error_messages={ |
| 171 | 171 | 'invalid': _(("This value may contain only" |
| 172 | 172 | " letters and numbers."))}) |
| 173 | + | |
| 173 | 174 | password1 = forms.CharField(label=_("Password"), |
| 174 | 175 | widget=forms.PasswordInput) |
| 175 | 176 | password2 = forms.CharField(label=_("Password confirmation"), |
| ... | ... | @@ -233,7 +234,7 @@ class UserCreationForm(UserForm): |
| 233 | 234 | |
| 234 | 235 | class UserChangeForm(forms.ModelForm): |
| 235 | 236 | username = forms.RegexField( |
| 236 | - label=_("Username"), max_length=30, regex=r"^[\w*]", | |
| 237 | + label=_("Username"), max_length=30, regex=r'^[\w.@+-]+$', | |
| 237 | 238 | help_text=_("Required. 30 characters or fewer. Letters and digits."), |
| 238 | 239 | error_messages={ |
| 239 | 240 | 'invalid': _("This value may contain only letters and numbers.")}) | ... | ... |
colab/accounts/models.py
| ... | ... | @@ -4,7 +4,6 @@ import urlparse |
| 4 | 4 | |
| 5 | 5 | from django.db import models |
| 6 | 6 | from django.contrib.auth.models import AbstractUser, UserManager |
| 7 | -from django.core import validators | |
| 8 | 7 | from django.core.urlresolvers import reverse |
| 9 | 8 | from django.utils.crypto import get_random_string |
| 10 | 9 | from django.utils.translation import ugettext_lazy as _ |
| ... | ... | @@ -77,8 +76,3 @@ User._meta.get_field('email')._unique = True |
| 77 | 76 | User._meta.get_field('username').help_text = _( |
| 78 | 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 | 24 | user.last_name = "COLAB" |
| 25 | 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 | 29 | 'first_name': 'colabName', |
| 30 | 30 | 'last_name': 'secondName', |
| 31 | - 'username': 'colab', | |
| 31 | + 'username': username, | |
| 32 | 32 | 'password1': '123colab4', |
| 33 | 33 | 'password2': '123colab4'} |
| 34 | 34 | form = UserCreationForm(data=form_data) |
| 35 | 35 | return form |
| 36 | 36 | |
| 37 | 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 | 40 | self.assertFalse(form.is_valid()) |
| 40 | 41 | |
| 41 | 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 | 45 | msg = form.error_messages.get('duplicate_email') % { |
| 44 | 46 | 'url': reverse('login') |
| 45 | 47 | } |
| 46 | 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 | 60 | def tearDown(self): |
| 49 | 61 | pass | ... | ... |