Repository: webkom/lego

Issue:
EmailAddress case sensitivity
When creating a email address for a user (for gsuite sync) that includes capital letters, the user will be suspended. All input should be lowercased, since that is ehat google is doing.


Discussion:

Resolving patch:
diff --git a/lego/apps/email/fields.py b/lego/apps/email/fields.py
--- a/lego/apps/email/fields.py
+++ b/lego/apps/email/fields.py
@@ -20,6 +20,7 @@ def to_internal_value(self, data):
         Create email if not exists.
         """
         try:
+            data = data.lower()
             email_address, _ = self.get_queryset().get_or_create(pk=data)
             return email_address
         except ObjectDoesNotExist:
diff --git a/lego/apps/email/validators.py b/lego/apps/email/validators.py
--- a/lego/apps/email/validators.py
+++ b/lego/apps/email/validators.py
@@ -5,6 +5,8 @@
 
 
 def validate_email_address(email_address):
+    if email_address.email != email_address.email.lower():
+        raise ValidationError('Email is not lowercased')
 
     if email_address.is_assigned():
         raise ValidationError('The address is already assigned')


Test patch:
diff --git a/lego/apps/email/tests/test_views.py b/lego/apps/email/tests/test_views.py
--- a/lego/apps/email/tests/test_views.py
+++ b/lego/apps/email/tests/test_views.py
@@ -146,6 +146,19 @@ def test_set_address_on_new_user(self):
         )
         self.assertEquals(status.HTTP_201_CREATED, response.status_code)
 
+    def test_set_address_on_capitalized_internal_email(self):
+        """Set an address that is capitalized to make sure it is lowercased in input sanitation"""
+        response = self.client.post(
+            self.url, {
+                'user': 2,
+                'internal_email': 'TestEmail123',
+                'internal_email_enabled': True,
+            }
+        )
+        self.assertEquals(status.HTTP_201_CREATED, response.status_code)
+        self.assertEquals('testemail123', response.json()['internalEmail'])
+        self.assertEquals('testemail123', User.objects.get(pk=2).internal_email.email)
+
     def test_set_address_to_assigned(self):
         """Not possible to set an assigned email"""
         response = self.client.post(
diff --git a/lego/apps/users/tests/test_registration_api.py b/lego/apps/users/tests/test_registration_api.py
--- a/lego/apps/users/tests/test_registration_api.py
+++ b/lego/apps/users/tests/test_registration_api.py
@@ -36,6 +36,15 @@ def test_with_valid_token(self):
         self.assertEqual(response.status_code, 200)
         self.assertEqual(response.data.get('email'), 'test1@user.com')
 
+    def test_with_valid_token_and_capitalized_email(self):
+        response = self.client.get(
+            _get_registration_token_url(
+                Registrations.generate_registration_token('Test1@User.CoM')
+            )
+        )
+        self.assertEqual(response.status_code, 200)
+        self.assertEqual(response.data.get('email'), 'Test1@User.CoM')
+
 
 class CreateRegistrationAPITestCase(BaseAPITestCase):
 
@@ -57,6 +66,16 @@ def test_with_invalid_email(self, *args):
         )
         self.assertEqual(response.status_code, 400)
 
+    @mock.patch('lego.apps.users.serializers.registration.verify_captcha', return_value=True)
+    def test_with_capitalized_email(self, mock_verify_captcha):
+        response = self.client.post(
+            _get_list_url(), {
+                'email': 'Test1@User.CoM',
+                'captcha_response': 'testCaptcha'
+            }
+        )
+        self.assertEqual(response.status_code, 202)
+
     @mock.patch('lego.apps.users.serializers.registration.verify_captcha', return_value=False)
     def test_with_invalid_captcha(self, *args):
         response = self.client.post(_get_list_url(), self._test_registration_data)