From eb613f88f4c289826a985da1b6dbcb2ee2a01cc1 Mon Sep 17 00:00:00 2001 From: Cody Maffucci <46459665+Maffooch@users.noreply.github.com> Date: Mon, 12 Feb 2024 11:36:59 -0600 Subject: [PATCH 1/4] Disallow duplicate tool types --- dojo/api_v2/serializers.py | 8 ++++++++ dojo/forms.py | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/dojo/api_v2/serializers.py b/dojo/api_v2/serializers.py index 45d2707a6e0..f57e684c980 100644 --- a/dojo/api_v2/serializers.py +++ b/dojo/api_v2/serializers.py @@ -1133,6 +1133,14 @@ class Meta: model = Tool_Type fields = "__all__" + def validate(self, data): + name = data.get("name") + # Make sure this will not create a duplicate test type + if Tool_Type.objects.filter(name=name).count() > 0: + raise serializers.ValidationError('A Tool Type with the name already exists') + + return data + class RegulationSerializer(serializers.ModelSerializer): class Meta: diff --git a/dojo/forms.py b/dojo/forms.py index 558c09ae69d..e81eb0e1840 100755 --- a/dojo/forms.py +++ b/dojo/forms.py @@ -2388,6 +2388,15 @@ class Meta: model = Tool_Type exclude = ['product'] + def clean(self): + form_data = self.cleaned_data + name = form_data.get("name") + # Make sure this will not create a duplicate test type + if Tool_Type.objects.filter(name=name).count() > 0: + raise forms.ValidationError('A Tool Type with the name already exists') + + return form_data + class RegulationForm(forms.ModelForm): class Meta: From a8098d3719795f8b19fa04ecc19930cb7091cbfe Mon Sep 17 00:00:00 2001 From: Cody Maffucci <46459665+Maffooch@users.noreply.github.com> Date: Mon, 12 Feb 2024 11:40:15 -0600 Subject: [PATCH 2/4] Fix Flake8 --- dojo/forms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dojo/forms.py b/dojo/forms.py index e81eb0e1840..5daab3208fe 100755 --- a/dojo/forms.py +++ b/dojo/forms.py @@ -2395,7 +2395,7 @@ def clean(self): if Tool_Type.objects.filter(name=name).count() > 0: raise forms.ValidationError('A Tool Type with the name already exists') - return form_data + return form_data class RegulationForm(forms.ModelForm): From f3bb5c7d4f5d406f84c0185516316034b798d850 Mon Sep 17 00:00:00 2001 From: Cody Maffucci <46459665+Maffooch@users.noreply.github.com> Date: Mon, 12 Feb 2024 12:13:51 -0600 Subject: [PATCH 3/4] Only validate on new creations --- dojo/api_v2/serializers.py | 10 +++++----- dojo/forms.py | 16 ++++++++++++---- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/dojo/api_v2/serializers.py b/dojo/api_v2/serializers.py index f57e684c980..2d126115080 100644 --- a/dojo/api_v2/serializers.py +++ b/dojo/api_v2/serializers.py @@ -1134,11 +1134,11 @@ class Meta: fields = "__all__" def validate(self, data): - name = data.get("name") - # Make sure this will not create a duplicate test type - if Tool_Type.objects.filter(name=name).count() > 0: - raise serializers.ValidationError('A Tool Type with the name already exists') - + if self.context["request"].method == "POST": + name = data.get("name") + # Make sure this will not create a duplicate test type + if Tool_Type.objects.filter(name=name).count() > 0: + raise serializers.ValidationError('A Tool Type with the name already exists') return data diff --git a/dojo/forms.py b/dojo/forms.py index 5daab3208fe..27a1fb0c287 100755 --- a/dojo/forms.py +++ b/dojo/forms.py @@ -2388,12 +2388,20 @@ class Meta: model = Tool_Type exclude = ['product'] + def __init__(self, *args, **kwargs): + instance = kwargs.get('instance', None) + self.newly_created = True + if instance is not None: + self.newly_created = instance.pk is None + super().__init__(*args, **kwargs) + def clean(self): form_data = self.cleaned_data - name = form_data.get("name") - # Make sure this will not create a duplicate test type - if Tool_Type.objects.filter(name=name).count() > 0: - raise forms.ValidationError('A Tool Type with the name already exists') + if self.newly_created: + name = form_data.get("name") + # Make sure this will not create a duplicate test type + if Tool_Type.objects.filter(name=name).count() > 0: + raise forms.ValidationError('A Tool Type with the name already exists') return form_data From 0b282831502c9298f5abab2450902cf019ca01c6 Mon Sep 17 00:00:00 2001 From: Cody Maffucci <46459665+Maffooch@users.noreply.github.com> Date: Mon, 12 Feb 2024 12:41:26 -0600 Subject: [PATCH 4/4] Force new name on tool type unit test --- unittests/test_swagger_schema.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/unittests/test_swagger_schema.py b/unittests/test_swagger_schema.py index 9f1316b4d2e..b1263359374 100644 --- a/unittests/test_swagger_schema.py +++ b/unittests/test_swagger_schema.py @@ -785,6 +785,9 @@ def __init__(self, *args, **kwargs): self.viewset = ToolTypesViewSet self.model = Tool_Type self.serializer = ToolTypeSerializer + self.field_transformers = { + "name": lambda v: v + "_new" + } class UserTest(BaseClass.SchemaTest):