Repository: zestedesavoir/zds-site

Issue:
La vérification du pseudo ne fonctionne pas
D'après le code de la fonction `validate_zds_username` du fichier `zds/member/validators.py`, le pseudo doit passer quelques tests pour pouvoir être accepté. Ces tests ne fonctionnent actuellement pas ! J'ai été en mesure de créer des pseudos avec des espaces avant et après sans soucis.


Discussion:
Je m'en occupe !

Resolving patch:
diff --git a/zds/member/forms.py b/zds/member/forms.py
--- a/zds/member/forms.py
+++ b/zds/member/forms.py
@@ -13,7 +13,7 @@
 
 from zds.member.models import Profile, KarmaNote, BannedEmailProvider
 from zds.member.validators import validate_not_empty, validate_zds_email, validate_zds_username, validate_passwords, \
-    validate_zds_password
+    validate_zds_password, validate_raw_zds_username
 from zds.utils.misc import contains_utf8mb4
 from zds.utils.models import Licence, HatRequest, Hat
 from zds.utils import get_current_user
@@ -136,6 +136,7 @@ def __init__(self, *args, **kwargs):
         self.helper.layout = layout
 
     def clean(self):
+        validate_raw_zds_username(self.data)
         cleaned_data = super(RegisterForm, self).clean()
         return validate_passwords(cleaned_data)
 
@@ -394,6 +395,7 @@ def __init__(self, user, *args, **kwargs):
         )
 
     def clean(self):
+        validate_raw_zds_username(self.data)
         cleaned_data = super(ChangeUserForm, self).clean()
         cleaned_data['previous_username'] = self.previous_username
         cleaned_data['previous_email'] = self.previous_email
diff --git a/zds/member/validators.py b/zds/member/validators.py
--- a/zds/member/validators.py
+++ b/zds/member/validators.py
@@ -80,8 +80,6 @@ def validate_zds_username(value, check_username_available=True):
     user_count = User.objects.filter(username=value).count()
     if ',' in value:
         msg = _('Le nom d\'utilisateur ne peut contenir de virgules')
-    elif value != value.strip():
-        msg = _('Le nom d\'utilisateur ne peut commencer ou finir par des espaces')
     elif contains_utf8mb4(value):
         msg = _('Le nom d\'utilisateur ne peut pas contenir des caractères utf8mb4')
     elif check_username_available and user_count > 0:
@@ -92,6 +90,21 @@ def validate_zds_username(value, check_username_available=True):
         raise ValidationError(msg)
 
 
+def validate_raw_zds_username(data):
+    """
+    Check if raw username hasn't space on left or right
+    """
+    msg = None
+    username = data.get('username', None)
+    if username is None:
+        msg = _('Le nom d\'utilisateur n\'est pas fourni')
+    elif username != username.strip():
+        msg = _('Le nom d\'utilisateur ne peut commencer ou finir par des espaces')
+
+    if msg is not None:
+        raise ValidationError(msg)
+
+
 def validate_zds_password(value):
     """
 


Test patch:
diff --git a/zds/member/tests/tests_forms.py b/zds/member/tests/tests_forms.py
--- a/zds/member/tests/tests_forms.py
+++ b/zds/member/tests/tests_forms.py
@@ -186,7 +186,6 @@ def test_email_exist_register_form(self):
         self.assertFalse(form.is_valid())
 
     def test_username_spaces_register_form(self):
-        # since Django 1.9, models.CharField is striped by default
         ProfileFactory()
         data = {
             'email': 'test@gmail.com',
@@ -195,7 +194,7 @@ def test_username_spaces_register_form(self):
             'password_confirm': 'ZePassword'
         }
         form = RegisterForm(data=data)
-        self.assertTrue(form.is_valid())
+        self.assertFalse(form.is_valid())
 
     def test_username_coma_register_form(self):
         ProfileFactory()
@@ -400,14 +399,13 @@ def test_wrong_email_user_form(self):
         self.assertFalse(form.is_valid())
 
     def test_username_spaces_register_form(self):
-        # since Django 1.9, models.CharField is striped by default
         ProfileFactory()
         data = {
             'username': '  ZeTester  ',
             'email': self.user1.user.email,
         }
         form = ChangeUserForm(data=data, user=self.user1.user)
-        self.assertTrue(form.is_valid())
+        self.assertFalse(form.is_valid())
 
     def test_username_coma_register_form(self):
         ProfileFactory()
diff --git a/zds/member/tests/tests_views.py b/zds/member/tests/tests_views.py
--- a/zds/member/tests/tests_views.py
+++ b/zds/member/tests/tests_views.py
@@ -394,6 +394,50 @@ def test_login(self):
                             '?next=' + reverse('gallery-list'),
                             count=1)
 
+    def test_register_with_not_allowed_chars(self):
+        """
+        Test register account with not allowed chars
+        :return:
+        """
+        users = [
+            # empty username
+            {
+                'username': '',
+                'password': 'flavour',
+                'password_confirm': 'flavour',
+                'email': 'firm1@zestedesavoir.com'
+            },
+            # space after username
+            {
+                'username': 'firm1 ',
+                'password': 'flavour',
+                'password_confirm': 'flavour',
+                'email': 'firm1@zestedesavoir.com'
+            },
+            # space before username
+            {
+                'username': ' firm1',
+                'password': 'flavour',
+                'password_confirm': 'flavour',
+                'email': 'firm1@zestedesavoir.com'
+            },
+            # username with utf8mb4 chars
+            {
+                'username': ' firm1',
+                'password': 'flavour',
+                'password_confirm': 'flavour',
+                'email': 'firm1@zestedesavoir.com'
+            }
+        ]
+
+        for user in users:
+            result = self.client.post(reverse('register-member'), user, follow=False)
+            self.assertEqual(result.status_code, 200)
+            # check any email has been sent.
+            self.assertEqual(len(mail.outbox), 0)
+            # user doesn't exist
+            self.assertEqual(User.objects.filter(username=user['username']).count(), 0)
+
     def test_register(self):
         """
         To test user registration.