Skip to content

Commit 876ff9c

Browse files
Maffoochclaude
andauthored
Add scan_date to import settings if overridden (#14502)
* Add scan_date to import settings if overridden * Fix datetime JSON serialization in import_settings Convert scan_date to ISO format string before storing in import_settings dict, which gets JSON-serialized. Raw datetime objects cause "TypeError: Object of type datetime is not JSON serializable". Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Add unit tests for scan_date in import_settings Tests both cases: - User supplies scan_date: verifies it's stored as ISO string - No scan_date supplied: verifies it's stored as None Both tests also verify import_settings remains JSON-serializable. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Fix timezone import in scan_date test Use datetime.timezone.utc instead of django.utils.timezone.utc which doesn't exist. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Fix ruff lint: import sorting and use datetime.UTC alias Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent d61da2c commit 876ff9c

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

dojo/importers/base_importer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ def update_import_history(
463463
import_settings["close_old_findings"] = self.close_old_findings_toggle
464464
import_settings["push_to_jira"] = self.push_to_jira
465465
import_settings["tags"] = self.tags
466+
import_settings["scan_date"] = self.scan_date.isoformat() if self.scan_date_override else None
466467
if settings.V3_FEATURE_LOCATIONS:
467468
# Add the list of locations that were added exclusively at import time
468469
if len(self.endpoints_to_add) > 0:

unittests/test_update_import_history.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import json
12
import logging
3+
from datetime import UTC, datetime
24
from unittest.mock import patch
35

46
from django.contrib.auth.models import User as DjangoUser
@@ -149,3 +151,29 @@ def test_precheck_filters_out_deleted_findings_allows_successful_bulk(self):
149151
expected = (len(new_findings) - 1) + (len(closed_findings) - 1)
150152
created = Test_Import_Finding_Action.objects.filter(test_import=test_import).count()
151153
self.assertEqual(created, expected)
154+
155+
def test_import_settings_scan_date_when_user_supplies_scan_date(self):
156+
"""When the user supplies a scan_date, import_settings should contain the ISO-formatted date."""
157+
user_scan_date = datetime(2025, 6, 15, 12, 0, 0, tzinfo=UTC)
158+
self.importer.scan_date = user_scan_date
159+
self.importer.scan_date_override = True
160+
161+
new_findings = self._create_findings(1)
162+
test_import = self.importer.update_import_history(new_findings=new_findings)
163+
164+
settings = test_import.import_settings
165+
# Verify import_settings is JSON-serializable (the original bug)
166+
json.dumps(settings)
167+
self.assertEqual(settings["scan_date"], user_scan_date.isoformat())
168+
169+
def test_import_settings_scan_date_when_no_scan_date_supplied(self):
170+
"""When no scan_date override is provided, import_settings should have scan_date as None."""
171+
self.importer.scan_date_override = False
172+
173+
new_findings = self._create_findings(1)
174+
test_import = self.importer.update_import_history(new_findings=new_findings)
175+
176+
settings = test_import.import_settings
177+
# Verify import_settings is JSON-serializable
178+
json.dumps(settings)
179+
self.assertIsNone(settings["scan_date"])

0 commit comments

Comments
 (0)