From 970241e8c66fa75eb2b2f4da38a65b721f40e832 Mon Sep 17 00:00:00 2001 From: jeremystretch Date: Fri, 12 May 2023 15:59:07 -0400 Subject: [PATCH] Add test for invalid custom field names --- .../migrations/0066_customfield_name_validation.py | 2 +- netbox/extras/models/customfields.py | 2 +- netbox/extras/tests/test_customfields.py | 11 +++++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/netbox/extras/migrations/0066_customfield_name_validation.py b/netbox/extras/migrations/0066_customfield_name_validation.py index fe187d525..3d2c51399 100644 --- a/netbox/extras/migrations/0066_customfield_name_validation.py +++ b/netbox/extras/migrations/0066_customfield_name_validation.py @@ -25,7 +25,7 @@ class Migration(migrations.Migration): django.core.validators.RegexValidator( flags=re.RegexFlag['IGNORECASE'], inverse_match=True, - message='No double-underscores are allowed.', + message='Double underscores are not permitted in custom field names.', regex=r'__', ), ], diff --git a/netbox/extras/models/customfields.py b/netbox/extras/models/customfields.py index 45b60eb56..be3540f08 100644 --- a/netbox/extras/models/customfields.py +++ b/netbox/extras/models/customfields.py @@ -87,7 +87,7 @@ class CustomField(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel): ), RegexValidator( regex=r'__', - message="No double-underscores are allowed.", + message="Double underscores are not permitted in custom field names.", flags=re.IGNORECASE, inverse_match=True ), diff --git a/netbox/extras/tests/test_customfields.py b/netbox/extras/tests/test_customfields.py index 6a3a3d074..3fd0dc83e 100644 --- a/netbox/extras/tests/test_customfields.py +++ b/netbox/extras/tests/test_customfields.py @@ -29,6 +29,17 @@ class CustomFieldTest(TestCase): 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): value = 'Foobar!'