Skip to content

Commit afc63eb

Browse files
authored
Dynamic Parsing: Add flag to indicate new test types (#10871)
* Dynamic Parsing: Add flag to indicate new test types * Add some tests
1 parent 9826e73 commit afc63eb

6 files changed

Lines changed: 43 additions & 2 deletions

File tree

dojo/api_v2/serializers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,7 @@ class TestTypeSerializer(TaggitSerializer, serializers.ModelSerializer):
14111411

14121412
class Meta:
14131413
model = Test_Type
1414-
fields = "__all__"
1414+
exclude = ("dynamically_generated",)
14151415

14161416

14171417
class TestToNotesSerializer(serializers.Serializer):
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 5.0.8 on 2024-09-04 19:23
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('dojo', '0213_system_settings_enable_ui_table_based_searching'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='test_type',
15+
name='dynamically_generated',
16+
field=models.BooleanField(default=False, help_text='Set to True for test types that are created at import time'),
17+
),
18+
]

dojo/forms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ def __init__(self, *args, **kwargs):
285285
class Test_TypeForm(forms.ModelForm):
286286
class Meta:
287287
model = Test_Type
288-
exclude = [""]
288+
exclude = ["dynamically_generated"]
289289

290290

291291
class Development_EnvironmentForm(forms.ModelForm):

dojo/importers/base_importer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,8 @@ def get_or_create_test_type(
491491
test_type, created = Test_Type.objects.get_or_create(name=test_type_name)
492492
if created:
493493
logger.info(f"Created new Test_Type with name {test_type.name} because a report is being imported")
494+
test_type.dynamically_generated = True
495+
test_type.save()
494496
return test_type
495497

496498
def verify_tool_configuration_from_test(self):

dojo/models.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,9 @@ class Test_Type(models.Model):
817817
static_tool = models.BooleanField(default=False)
818818
dynamic_tool = models.BooleanField(default=False)
819819
active = models.BooleanField(default=True)
820+
dynamically_generated = models.BooleanField(
821+
default=False,
822+
help_text=_("Set to True for test types that are created at import time"))
820823

821824
class Meta:
822825
ordering = ("name",)

unittests/test_import_reimport.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,6 +1478,24 @@ def test_import_history_reactivated_and_untouched_findings_do_not_mix(self):
14781478
self.reimport_scan_with_params(test_id, self.generic_import_1, scan_type=self.scan_type_generic)
14791479
# Passing this test means an exception does not occur
14801480

1481+
def test_dynamic_parsing_field_set_to_true(self):
1482+
# Test that a generic finding import creates a new test type
1483+
# with the dynamically_generated field set to True
1484+
import0 = self.import_scan_with_params(self.generic_import_1, scan_type=self.scan_type_generic)
1485+
test_id = import0["test"]
1486+
# Fetch the test from the DB to access the test type
1487+
test = Test.objects.get(id=test_id)
1488+
self.assertTrue(test.test_type.dynamically_generated)
1489+
1490+
def test_dynamic_parsing_field_set_to_false(self):
1491+
# Test that a ZAP import does not create a new test type
1492+
# and that the dynamically_generated field set to False
1493+
import0 = self.import_scan_with_params(self.zap_sample0_filename)
1494+
test_id = import0["test"]
1495+
# Fetch the test from the DB to access the test type
1496+
test = Test.objects.get(id=test_id)
1497+
self.assertFalse(test.test_type.dynamically_generated)
1498+
14811499

14821500
class ImportReimportTestAPI(DojoAPITestCase, ImportReimportMixin):
14831501
fixtures = ["dojo_testdata.json"]

0 commit comments

Comments
 (0)