Skip to content

Commit

Permalink
Disallow duplicate tool types (DefectDojo#9530)
Browse files Browse the repository at this point in the history
* Disallow duplicate tool types

* Fix Flake8

* Only validate on new creations

* Force new name on tool type unit test
  • Loading branch information
Maffooch authored and quirinziessler committed Feb 21, 2024
1 parent 5eb2d1b commit a3403a4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
8 changes: 8 additions & 0 deletions dojo/api_v2/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,14 @@ class Meta:
model = Tool_Type
fields = "__all__"

def validate(self, data):
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


class RegulationSerializer(serializers.ModelSerializer):
class Meta:
Expand Down
17 changes: 17 additions & 0 deletions dojo/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2404,6 +2404,23 @@ 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
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


class RegulationForm(forms.ModelForm):
class Meta:
Expand Down
3 changes: 3 additions & 0 deletions unittests/test_swagger_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit a3403a4

Please sign in to comment.