Skip to content

Commit

Permalink
Don't allow label names which are invalid in RapidPro
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Feb 1, 2018
1 parent d1006eb commit de34c92
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion casepro/contacts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def to_parts(cls, urn):
"""
try:
scheme, path = urn.split(':', 1)
except:
except ValueError:
raise ValueError("URN strings must contain scheme and path components")

if not scheme or scheme not in cls.VALID_SCHEMES:
Expand Down
11 changes: 9 additions & 2 deletions casepro/msgs/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import unicode_literals

import regex

from django import forms
from django.utils.translation import ugettext_lazy as _

Expand Down Expand Up @@ -59,8 +61,13 @@ def clean_name(self):
name = self.cleaned_data['name'].strip()
if name.lower() == 'flagged':
raise forms.ValidationError(_("Reserved label name"))
elif name.startswith('+') or name.startswith('-'):
raise forms.ValidationError(_("Label name cannot start with + or -"))

elif len(name) > Label.MAX_NAME_LEN:
raise forms.ValidationError(_("Label name must be %d characters or less") % Label.MAX_NAME_LEN)

# first character must be a word char
elif not regex.match('\w', name[0], flags=regex.UNICODE):
raise forms.ValidationError(_("Label name must start with a letter or digit"))
return name

def clean_keywords(self):
Expand Down
2 changes: 2 additions & 0 deletions casepro/msgs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class Label(models.Model):
INBOX_COUNT_CACHE_ATTR = '_inbox_count'
ARCHIVED_COUNT_CACHE_ATTR = '_archived_count'

MAX_NAME_LEN = 64

@classmethod
def create(cls, org, name, description, tests, is_synced):
label = cls.objects.create(org=org, name=name, description=description, is_synced=is_synced)
Expand Down
7 changes: 6 additions & 1 deletion casepro/msgs/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,12 @@ def test_create(self):
# submit with name that is invalid
response = self.url_post('unicef', url, {'name': '+Ebola'})
self.assertEqual(response.status_code, 200)
self.assertFormError(response, 'form', 'name', "Label name cannot start with + or -")
self.assertFormError(response, 'form', 'name', "Label name must start with a letter or digit")

# submit with name that is too long
response = self.url_post('unicef', url, {'name': 'a' * 65})
self.assertEqual(response.status_code, 200)
self.assertFormError(response, 'form', 'name', "Label name must be 64 characters or less")

# submit with a keyword that is too short
response = self.url_post('unicef', url, {'name': 'Ebola', 'keywords': 'a, ebola'})
Expand Down

0 comments on commit de34c92

Please sign in to comment.