Repository: weni-ai/bothub-engine

Issue:
Validate entity chars, no special chars



Discussion:

Resolving patch:
diff --git a/bothub/common/models.py b/bothub/common/models.py
--- a/bothub/common/models.py
+++ b/bothub/common/models.py
@@ -8,6 +8,7 @@
 from django.utils.translation import gettext as _
 from django.utils import timezone
 from django.conf import settings
+from django.core.validators import RegexValidator, _lazy_re_compile
 
 from bothub.authentication.models import User
 
@@ -18,6 +19,15 @@
 from .exceptions import DoesNotHaveTranslation
 
 
+entity_and_intent_regex = _lazy_re_compile(r'^[-a-z0-9_]+\Z')
+validate_entity_and_intent = RegexValidator(
+    entity_and_intent_regex,
+    _('Enter a valid value consisting of lowercase letters, numbers, ' +
+        'underscores or hyphens.'),
+    'invalid'
+)
+
+
 class RepositoryCategory(models.Model):
     class Meta:
         verbose_name = _('repository category')
@@ -379,7 +389,8 @@ class Meta:
         _('intent'),
         max_length=64,
         blank=True,
-        help_text=_('Example intent reference'))
+        help_text=_('Example intent reference'),
+        validators=[validate_entity_and_intent])
     created_at = models.DateTimeField(
         _('created at'),
         auto_now_add=True)
@@ -525,7 +536,8 @@ class Meta:
     entity = models.CharField(
         _('entity'),
         max_length=64,
-        help_text=_('Entity name'))
+        help_text=_('Entity name'),
+        validators=[validate_entity_and_intent])
     created_at = models.DateTimeField(
         _('created at'),
         auto_now_add=True)


Test patch:
diff --git a/bothub/api/tests/example.py b/bothub/api/tests/example.py
--- a/bothub/api/tests/example.py
+++ b/bothub/api/tests/example.py
@@ -154,6 +154,44 @@ def test_intent_or_entity_required(self):
             response.status_code,
             status.HTTP_400_BAD_REQUEST)
 
+    def test_entity_with_special_char(self):
+        response, content_data = self.request(
+            self.owner_token,
+            {
+                'repository': str(self.repository.uuid),
+                'text': 'my name is douglas',
+                'intent': '',
+                'entities': [
+                    {
+                        'start': 11,
+                        'end': 18,
+                        'entity': 'nam&',
+                    },
+                ],
+            })
+        self.assertEqual(
+            response.status_code,
+            status.HTTP_400_BAD_REQUEST)
+        self.assertEqual(
+            len(content_data.get('entities')),
+            1)
+
+    def test_intent_with_special_char(self):
+        response, content_data = self.request(
+            self.owner_token,
+            {
+                'repository': str(self.repository.uuid),
+                'text': 'my name is douglas',
+                'intent': 'nam$s',
+                'entities': [],
+            })
+        self.assertEqual(
+            response.status_code,
+            status.HTTP_400_BAD_REQUEST)
+        self.assertEqual(
+            len(content_data.get('intent')),
+            1)
+
 
 class RepositoryExampleRetrieveTestCase(TestCase):
     def setUp(self):