Repository: modoboa/modoboa (language: python)

Issue:
Limit the size of left part in email field form to 64 characters
_From @harobed on June 25, 2015 12:47_

Limit the size of left part in email field form to 64 characters

_Copied from original issue: modoboa/modoboa-admin#29_



Discussion / hints:
The specification is defined on the RFC 5321

https://tools.ietf.org/html/rfc5321#section-4.5.3.1.1

Fix patch:
diff --git a/modoboa/admin/api/v2/serializers.py b/modoboa/admin/api/v2/serializers.py
index 26fc74906..081af930f 100644
--- a/modoboa/admin/api/v2/serializers.py
+++ b/modoboa/admin/api/v2/serializers.py
@@ -492,6 +492,14 @@ def validate(self, data):
                     )
                 except lib_exceptions.ModoboaException as inst:
                     raise serializers.ValidationError({"mailbox": str(inst)})
+            if len(self.address) > 64:
+                raise serializers.ValidationError(
+                    {
+                        "username": _(
+                            "The left part of an email address can't be more than 64 characters."
+                        )
+                    }
+                )
         if data.get("password") or not self.partial:
             password = data.get("password")
             if password:


Test patch:
diff --git a/modoboa/admin/api/v2/tests.py b/modoboa/admin/api/v2/tests.py
index 0efcd8c8e..bb7d51598 100644
--- a/modoboa/admin/api/v2/tests.py
+++ b/modoboa/admin/api/v2/tests.py
@@ -12,7 +12,6 @@
 
 
 class DomainViewSetTestCase(ModoAPITestCase):
-
     @classmethod
     def setUpTestData(cls):  # NOQA:N802
         """Create test data."""
@@ -162,7 +161,6 @@ def test_export_domains(self):
 
 
 class AccountViewSetTestCase(ModoAPITestCase):
-
     @classmethod
     def setUpTestData(cls):  # NOQA:N802
         """Create test data."""
@@ -210,6 +208,25 @@ def test_create_with_bad_password(self):
         self.assertEqual(resp.status_code, 400)
         self.assertIn("password", resp.json())
 
+    def test_create_too_long_username(self):
+        """Test that it is not possible to create too long usernames (RFC5321)."""
+        url = reverse("v2:account-list")
+        toolong_username = "".join(["a" for i in range(66)])
+        data = {
+            "username": f"{toolong_username}@test.com",
+            "role": "SimpleUsers",
+            "mailbox": {"use_domain_quota": True},
+            "password": "Toto12345",
+            "language": "fr",
+            "aliases": ["totoalias@test.com"],
+        }
+        resp = self.client.post(url, data, format="json")
+        self.assertEqual(resp.status_code, 400)
+        self.assertIn(
+            "The left part of an email address can't be more than 64 characters",
+            str(resp.json()),
+        )
+
     def test_validate(self):
         """Test validate and throttling."""
         data = {"username": "toto@test.com"}
@@ -393,7 +410,6 @@ def test_get_with_resources(self):
 
 
 class IdentityViewSetTestCase(ModoAPITestCase):
-
     @classmethod
     def setUpTestData(cls):  # NOQA:N802
         """Create test data."""
@@ -482,7 +498,6 @@ def test_export(self):
 
 
 class AliasViewSetTestCase(ModoAPITestCase):
-
     @classmethod
     def setUpTestData(cls):  # NOQA:N802
         """Create test data."""
@@ -512,7 +527,6 @@ def test_random_address(self):
 
 
 class UserAccountViewSetTestCase(ModoAPITestCase):
-
     @classmethod
     def setUpTestData(cls):  # NOQA:N802
         """Create test data."""
@@ -554,7 +568,6 @@ def test_forward(self):
 
 
 class AlarmViewSetTestCase(ModoAPITestCase):
-
     @classmethod
     def setUpTestData(cls):  # NOQA:N802
         """Create test data."""