diff --git a/dojo/api_v2/serializers.py b/dojo/api_v2/serializers.py index 5bed7935f94..a0d4298b740 100644 --- a/dojo/api_v2/serializers.py +++ b/dojo/api_v2/serializers.py @@ -1411,7 +1411,7 @@ class TestTypeSerializer(TaggitSerializer, serializers.ModelSerializer): class Meta: model = Test_Type - fields = "__all__" + exclude = ("dynamically_generated",) class TestToNotesSerializer(serializers.Serializer): diff --git a/dojo/db_migrations/0214_test_type_dynamically_generated.py b/dojo/db_migrations/0214_test_type_dynamically_generated.py new file mode 100644 index 00000000000..80219377e7f --- /dev/null +++ b/dojo/db_migrations/0214_test_type_dynamically_generated.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0.8 on 2024-09-04 19:23 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('dojo', '0213_system_settings_enable_ui_table_based_searching'), + ] + + operations = [ + migrations.AddField( + model_name='test_type', + name='dynamically_generated', + field=models.BooleanField(default=False, help_text='Set to True for test types that are created at import time'), + ), + ] diff --git a/dojo/forms.py b/dojo/forms.py index 28c17082f6c..dde58a38b61 100644 --- a/dojo/forms.py +++ b/dojo/forms.py @@ -285,7 +285,7 @@ def __init__(self, *args, **kwargs): class Test_TypeForm(forms.ModelForm): class Meta: model = Test_Type - exclude = [""] + exclude = ["dynamically_generated"] class Development_EnvironmentForm(forms.ModelForm): diff --git a/dojo/importers/base_importer.py b/dojo/importers/base_importer.py index 54f18071c26..22e9ee5cbfe 100644 --- a/dojo/importers/base_importer.py +++ b/dojo/importers/base_importer.py @@ -491,6 +491,8 @@ def get_or_create_test_type( test_type, created = Test_Type.objects.get_or_create(name=test_type_name) if created: logger.info(f"Created new Test_Type with name {test_type.name} because a report is being imported") + test_type.dynamically_generated = True + test_type.save() return test_type def verify_tool_configuration_from_test(self): diff --git a/dojo/models.py b/dojo/models.py index 88fbc254e62..5048f30427f 100644 --- a/dojo/models.py +++ b/dojo/models.py @@ -817,6 +817,9 @@ class Test_Type(models.Model): static_tool = models.BooleanField(default=False) dynamic_tool = models.BooleanField(default=False) active = models.BooleanField(default=True) + dynamically_generated = models.BooleanField( + default=False, + help_text=_("Set to True for test types that are created at import time")) class Meta: ordering = ("name",) diff --git a/unittests/test_import_reimport.py b/unittests/test_import_reimport.py index 5c7ed58d1b3..1015f206d7a 100644 --- a/unittests/test_import_reimport.py +++ b/unittests/test_import_reimport.py @@ -1478,6 +1478,24 @@ def test_import_history_reactivated_and_untouched_findings_do_not_mix(self): self.reimport_scan_with_params(test_id, self.generic_import_1, scan_type=self.scan_type_generic) # Passing this test means an exception does not occur + def test_dynamic_parsing_field_set_to_true(self): + # Test that a generic finding import creates a new test type + # with the dynamically_generated field set to True + import0 = self.import_scan_with_params(self.generic_import_1, scan_type=self.scan_type_generic) + test_id = import0["test"] + # Fetch the test from the DB to access the test type + test = Test.objects.get(id=test_id) + self.assertTrue(test.test_type.dynamically_generated) + + def test_dynamic_parsing_field_set_to_false(self): + # Test that a ZAP import does not create a new test type + # and that the dynamically_generated field set to False + import0 = self.import_scan_with_params(self.zap_sample0_filename) + test_id = import0["test"] + # Fetch the test from the DB to access the test type + test = Test.objects.get(id=test_id) + self.assertFalse(test.test_type.dynamically_generated) + class ImportReimportTestAPI(DojoAPITestCase, ImportReimportMixin): fixtures = ["dojo_testdata.json"]