Skip to content

Commit

Permalink
Dynamic Parsing: Add flag to indicate new test types (#10871)
Browse files Browse the repository at this point in the history
* Dynamic Parsing: Add flag to indicate new test types

* Add some tests
  • Loading branch information
Maffooch authored Sep 9, 2024
1 parent 4ea5804 commit f13afa8
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dojo/api_v2/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1411,7 +1411,7 @@ class TestTypeSerializer(TaggitSerializer, serializers.ModelSerializer):

class Meta:
model = Test_Type
fields = "__all__"
exclude = ("dynamically_generated",)


class TestToNotesSerializer(serializers.Serializer):
Expand Down
18 changes: 18 additions & 0 deletions dojo/db_migrations/0214_test_type_dynamically_generated.py
Original file line number Diff line number Diff line change
@@ -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'),
),
]
2 changes: 1 addition & 1 deletion dojo/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions dojo/importers/base_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 3 additions & 0 deletions dojo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",)
Expand Down
18 changes: 18 additions & 0 deletions unittests/test_import_reimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down

0 comments on commit f13afa8

Please sign in to comment.