Skip to content

Commit 2ff7ed0

Browse files
authored
Merge pull request #14259 from DefectDojo/auto-create-context-patch
Auto Create Context: Fetch all objects for correct jira project associations
2 parents 84a6f8f + f011308 commit 2ff7ed0

4 files changed

+2947
-4
lines changed

dojo/api_v2/views.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2556,7 +2556,7 @@ def perform_create(self, serializer):
25562556
auto_create.process_import_meta_data_from_dict(converted_dict)
25572557
# Get an existing product
25582558
product = auto_create.get_target_product_if_exists(**converted_dict)
2559-
engagement = auto_create.get_target_engagement_if_exists(**converted_dict)
2559+
engagement = auto_create.get_target_engagement_if_exists(product=product, **converted_dict)
25602560
except (ValueError, TypeError) as e:
25612561
# Raise an explicit drf exception here
25622562
raise ValidationError(str(e))
@@ -2713,8 +2713,8 @@ def perform_create(self, serializer):
27132713
auto_create.process_import_meta_data_from_dict(converted_dict)
27142714
# Get an existing product
27152715
product = auto_create.get_target_product_if_exists(**converted_dict)
2716-
engagement = auto_create.get_target_engagement_if_exists(**converted_dict)
2717-
test = auto_create.get_target_test_if_exists(**converted_dict)
2716+
engagement = auto_create.get_target_engagement_if_exists(product=product, **converted_dict)
2717+
test = auto_create.get_target_test_if_exists(engagement=engagement, **converted_dict)
27182718
except (ValueError, TypeError) as e:
27192719
# Raise an explicit drf exception here
27202720
raise ValidationError(str(e))

unittests/test_jira_import_and_pushing_api.py

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import dojo.risk_acceptance.helper as ra_helper
1313
from dojo.jira_link import helper as jira_helper
14-
from dojo.models import Finding, Finding_Group, JIRA_Instance, Risk_Acceptance, User
14+
from dojo.models import Finding, Finding_Group, JIRA_Instance, JIRA_Project, Risk_Acceptance, Test, User
1515
from unittests.dojo_test_case import (
1616
DojoVCRAPITestCase,
1717
get_unit_tests_path,
@@ -1079,3 +1079,88 @@ def create_engagement_epic(self, engagement):
10791079
def assert_epic_issue_count(self, engagement, count):
10801080
jira_issues = self.get_epic_issues(engagement)
10811081
self.assertEqual(count, len(jira_issues))
1082+
1083+
def _test_setup_jira_project_for_engagement(self) -> dict:
1084+
import_reimport_config = {
1085+
"active": True,
1086+
"verified": True,
1087+
"product_type_name": "Some Product Type",
1088+
"product_name": "Jira Product (Not Configured)",
1089+
"engagement_name": "Jira Engagement",
1090+
"engagement": None, # This is hardcoded on the test function, so lets just null it out
1091+
"auto_create_context": True,
1092+
}
1093+
# First have a regular import create all the things
1094+
import0 = self.import_scan_with_params(
1095+
self.zap_sample5_filename,
1096+
**import_reimport_config,
1097+
)
1098+
test_id = import0["test"]
1099+
test = Test.objects.get(id=test_id)
1100+
engagement = test.engagement
1101+
# Ensure we have push to jira settings set as false here (.first fetches the most recent object)
1102+
self.assertFalse(test.test_import_set.first().import_settings["push_to_jira"], "Expected push_to_jira to be False since no Jira project is configured")
1103+
# Now set up the jira instance and project, and reimport the same report again with the same parameters, which should now fetch the jira project from the engagement and set push_to_jira to True in the import settings
1104+
JIRA_Project.objects.create(
1105+
jira_instance=JIRA_Instance.objects.first(),
1106+
project_key="TEST",
1107+
engagement=engagement,
1108+
push_all_issues=True,
1109+
)
1110+
# Double check we have no jira findings
1111+
self.assert_jira_issue_count_in_test(test_id, 0)
1112+
1113+
return import_reimport_config
1114+
1115+
# Disable deduplication here because it keeps getting in the way of us properly testing that
1116+
# findings are pushed to jira on reimport, since the same report is being imported twice in
1117+
# this test and deduplication will prevent the second import from creating any findings at all,
1118+
# which means no jira issues will be created on the second import, which is what we need to assert
1119+
# that the jira project is being fetched correctly and push_to_jira is being set to True in the import settings
1120+
@toggle_system_setting_boolean("enable_deduplication", False) # noqa: FBT003
1121+
def test_import_auto_create_context_fetches_all_objects_for_push_to_jira(self):
1122+
"""
1123+
This test is responsible for ensuring that all related objects in auto context are fetched appropriately.
1124+
To test this, we will first set up a jira instance with a project configured at the engagement level only.
1125+
It is not really important that we test that findings are pushed to jira here, but we can assert that the
1126+
import history import settings reflect that the viewset was given a "True" value for push_to_jira,
1127+
which is only possible if the engagement's jira project was correctly fetched before the serializer was invoked.
1128+
"""
1129+
import_reimport_config = self._test_setup_jira_project_for_engagement()
1130+
# Not run the import again
1131+
import1 = self.import_scan_with_params(
1132+
self.zap_sample5_filename,
1133+
**import_reimport_config,
1134+
)
1135+
test_id = import1["test"]
1136+
test = Test.objects.get(id=test_id)
1137+
# We should now have push_to_jira set to True in the import settings due to the jira project being on the engagement
1138+
self.assertTrue(test.test_import_set.first().import_settings["push_to_jira"], "Expected push_to_jira to be True since a Jira project is configured on the engagement")
1139+
# Make sure we actually pushed something to jira
1140+
self.assert_jira_issue_count_in_test(test_id, 2)
1141+
# by asserting full cassette is played we know issues have been updated in JIRA
1142+
self.assert_cassette_played()
1143+
1144+
def test_reimport_auto_create_context_fetches_all_objects_for_push_to_jira(self):
1145+
"""
1146+
This test is responsible for ensuring that all related objects in auto context are fetched appropriately.
1147+
To test this, we will first set up a jira instance with a project configured at the engagement level only.
1148+
It is not really important that we test that findings are pushed to jira here, but we can assert that the
1149+
import history import settings reflect that the viewset was given a "True" value for push_to_jira,
1150+
which is only possible if the engagement's jira project was correctly fetched before the serializer was invoked.
1151+
"""
1152+
import_reimport_config = self._test_setup_jira_project_for_engagement()
1153+
# Not run the import again
1154+
import1 = self.reimport_scan_with_params(
1155+
import_reimport_config.pop("test_id", None),
1156+
self.zap_sample5_filename,
1157+
**import_reimport_config,
1158+
)
1159+
test_id = import1["test"]
1160+
test = Test.objects.get(id=test_id)
1161+
# We should now have push_to_jira set to True in the import settings due to the jira project being on the engagement
1162+
self.assertTrue(test.test_import_set.first().import_settings["push_to_jira"], "Expected push_to_jira to be True since a Jira project is configured on the engagement")
1163+
# Make sure we actually pushed something to jira
1164+
self.assert_jira_issue_count_in_test(test_id, 2)
1165+
# by asserting full cassette is played we know issues have been updated in JIRA
1166+
self.assert_cassette_played()

0 commit comments

Comments
 (0)