Add test for invalid custom field names

This commit is contained in:
jeremystretch 2023-05-12 15:59:07 -04:00
parent 050a3b789c
commit 970241e8c6
3 changed files with 13 additions and 2 deletions

View File

@ -25,7 +25,7 @@ class Migration(migrations.Migration):
django.core.validators.RegexValidator( django.core.validators.RegexValidator(
flags=re.RegexFlag['IGNORECASE'], flags=re.RegexFlag['IGNORECASE'],
inverse_match=True, inverse_match=True,
message='No double-underscores are allowed.', message='Double underscores are not permitted in custom field names.',
regex=r'__', regex=r'__',
), ),
], ],

View File

@ -87,7 +87,7 @@ class CustomField(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel):
), ),
RegexValidator( RegexValidator(
regex=r'__', regex=r'__',
message="No double-underscores are allowed.", message="Double underscores are not permitted in custom field names.",
flags=re.IGNORECASE, flags=re.IGNORECASE,
inverse_match=True inverse_match=True
), ),

View File

@ -29,6 +29,17 @@ class CustomFieldTest(TestCase):
cls.object_type = ContentType.objects.get_for_model(Site) cls.object_type = ContentType.objects.get_for_model(Site)
def test_invalid_name(self):
"""
Try creating a CustomField with an invalid name.
"""
with self.assertRaises(ValidationError):
# Invalid character
CustomField(name='?', type=CustomFieldTypeChoices.TYPE_TEXT).full_clean()
with self.assertRaises(ValidationError):
# Double underscores not permitted
CustomField(name='foo__bar', type=CustomFieldTypeChoices.TYPE_TEXT).full_clean()
def test_text_field(self): def test_text_field(self):
value = 'Foobar!' value = 'Foobar!'