Skip to content

Commit 24eb308

Browse files
committed
fix(tags): improve tag handling in DefaultImporter and add tests for tag imports
1 parent d4e7513 commit 24eb308

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

dojo/importers/default_importer.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,11 @@ def process_findings(
226226
# Process any endpoints on the endpoint, or added on the form
227227
self.process_endpoints(finding, self.endpoints_to_add)
228228
# Parsers must use unsaved_tags to store tags, so we can clean them
229-
finding.tags = clean_tags(finding.unsaved_tags)
229+
cleaned_tags = clean_tags(finding.unsaved_tags)
230+
if isinstance(cleaned_tags, list):
231+
finding.tags.set(cleaned_tags)
232+
elif isinstance(cleaned_tags, str):
233+
finding.tags.set([cleaned_tags])
230234
# Process any files
231235
self.process_files(finding)
232236
# Process vulnerability IDs

unittests/test_tags.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class TagTests(DojoAPITestCase):
1515
def setUp(self, *args, **kwargs):
1616
super().setUp()
1717
self.login_as_admin()
18-
self.scans_path = get_unit_tests_scans_path("zap")
19-
self.zap_sample5_filename = self.scans_path / "5_zap_sample_one.xml"
18+
self.zap_sample5_filename = get_unit_tests_scans_path("zap") / "5_zap_sample_one.xml"
19+
self.generic_sample_with_tags_filename = get_unit_tests_scans_path("generic") / "generic_report1.json"
2020

2121
def test_create_product_with_tags(self, expected_status_code: int = 201):
2222
product_id = Product.objects.all().first().id
@@ -285,6 +285,22 @@ def test_import_multipart_tags(self):
285285
for tag in success_tags:
286286
self.assertIn(tag, response["tags"])
287287

288+
def test_import_report_with_tags(self):
289+
def assert_tags_in_findings(findings: list[dict], expected_finding_count: int, desired_tags: list[str]) -> None:
290+
self.assertEqual(expected_finding_count, len(findings))
291+
for finding in findings:
292+
self.assertEqual(len(desired_tags), len(finding.get("tags")))
293+
for tag in desired_tags:
294+
self.assertIn(tag, finding["tags"])
295+
296+
# Import a report with findings that have tags
297+
import0 = self.import_scan_with_params(self.generic_sample_with_tags_filename, scan_type="Generic Findings Import")
298+
test_id = import0["test"]
299+
response = self.get_test_findings_api(test_id)
300+
findings = response["results"]
301+
# Make sure we have what we are looking for
302+
assert_tags_in_findings(findings, 2, ["security", "network"])
303+
288304

289305
class InheritedTagsTests(DojoAPITestCase):
290306
fixtures = ["dojo_testdata.json"]

0 commit comments

Comments
 (0)