Skip to content

Commit 8dc2f4a

Browse files
fix tests
1 parent 5a371f0 commit 8dc2f4a

4 files changed

+9710
-1574
lines changed

dojo/risk_acceptance/helper.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def expire_now(risk_acceptance):
3535
# this method both saves and pushed to JIRA (no other post processing)
3636
finding.save(dedupe_option=False)
3737
if jira_helper.is_push_all_issues(finding) or jira_helper.is_keep_in_sync_with_jira(finding):
38+
logger.info("pushing finding to JIRA after expiration of risk acceptance")
3839
jira_helper.push_to_jira(finding)
3940

4041
reactivated_findings.append(finding)
@@ -76,6 +77,7 @@ def reinstate(risk_acceptance, old_expiration_date):
7677
# this method both saves and pushed to JIRA (no other post processing)
7778
finding.save(dedupe_option=False)
7879
if jira_helper.is_push_all_issues(finding) or jira_helper.is_keep_in_sync_with_jira(finding):
80+
logger.info("pushing finding to JIRA after reinstating risk acceptance")
7981
jira_helper.push_to_jira(finding)
8082
reinstated_findings.append(finding)
8183
else:
@@ -119,6 +121,7 @@ def remove_finding_from_risk_acceptance(user: Dojo_User, risk_acceptance: Risk_A
119121
# this method both saves and pushed to JIRA (no other post processing)
120122
finding.save(dedupe_option=False)
121123
if jira_helper.is_push_all_issues(finding) or jira_helper.is_keep_in_sync_with_jira(finding):
124+
logger.info("pushing finding to JIRA after removal from risk acceptance")
122125
jira_helper.push_to_jira(finding)
123126

124127
# best effort jira integration, no status changes
@@ -144,7 +147,13 @@ def add_findings_to_risk_acceptance(user: Dojo_User, risk_acceptance: Risk_Accep
144147
finding.save(dedupe_option=False)
145148
# Update any endpoint statuses on each of the findings
146149
update_endpoint_statuses(finding, accept_risk=True)
150+
147151
risk_acceptance.accepted_findings.add(finding)
152+
153+
if jira_helper.is_push_all_issues(finding) or jira_helper.is_keep_in_sync_with_jira(finding):
154+
logger.info("pushing finding to JIRA after adding to risk acceptance")
155+
jira_helper.push_to_jira(finding)
156+
148157
# Add a note to reflect that the finding was removed from the risk acceptance
149158
if user is not None:
150159
finding.notes.add(Notes.objects.create(

unittests/test_jira_import_and_pushing_api.py

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# from unittest import skip
22
import logging
3+
from unittest.mock import patch
34

45
from crum import impersonate
56
from django.urls import reverse
@@ -70,7 +71,6 @@ def setUp(self):
7071
self.testuser = User.objects.get(username="admin")
7172
self.testuser.usercontactinfo.block_execution = True
7273
self.testuser.usercontactinfo.save()
73-
7474
token = Token.objects.get(user=self.testuser)
7575
self.client = APIClient()
7676
self.client.credentials(HTTP_AUTHORIZATION="Token " + token.key)
@@ -321,7 +321,7 @@ def add_risk_acceptance(self, eid, data_risk_accceptance, fid=None):
321321
self.assertEqual(302, response.status_code, response.content[:1000])
322322
return response
323323

324-
def test_import_grouped_reopen_expired_sla(self):
324+
def test_import_grouped_reopen_expired_risk_acceptance(self):
325325
# steps
326326
# import scan, make sure they are in grouped JIRA
327327
# risk acceptance all the grouped findings, make sure they are closed in JIRA
@@ -374,6 +374,59 @@ def test_import_grouped_reopen_expired_sla(self):
374374
# by asserting full cassette is played we know all calls to JIRA have been made as expected
375375
self.assert_cassette_played()
376376

377+
@patch("dojo.decorators.we_want_async", return_value=False)
378+
def test_import_grouped_reopen_expired_risk_acceptance_with_finding_sync(self, mock):
379+
# steps
380+
# import scan, make sure they are in grouped JIRA
381+
# risk acceptance all the grouped findings, make sure they are closed in JIRA
382+
# expire risk acceptance on all grouped findings, make sure they are open in JIRA
383+
JIRA_Instance.objects.update(finding_jira_sync=True)
384+
385+
import0 = self.import_scan_with_params(self.npm_groups_sample_filename, scan_type="NPM Audit Scan", group_by="component_name+component_version", push_to_jira=True, verified=True)
386+
test_id = import0["test"]
387+
self.assert_jira_issue_count_in_test(test_id, 0)
388+
self.assert_jira_group_issue_count_in_test(test_id, 3)
389+
findings = self.get_test_findings_api(test_id)
390+
finding_id = findings["results"][0]["id"]
391+
392+
ra_data = {
393+
"name": "Accept: Unit test",
394+
"accepted_findings": [],
395+
"recommendation": "A",
396+
"recommendation_details": "recommendation 1",
397+
"decision": "A",
398+
"decision_details": "it has been decided!",
399+
"accepted_by": "pointy haired boss",
400+
"owner": 1,
401+
"expiration_date": "2024-12-31",
402+
"reactivate_expired": True,
403+
}
404+
405+
for finding in findings["results"]:
406+
ra_data["accepted_findings"].append(finding["id"])
407+
408+
pre_jira_status = self.get_jira_issue_status(finding_id)
409+
410+
response = self.add_risk_acceptance(1, data_risk_accceptance=ra_data)
411+
self.assertEqual("/engagement/1", response.url)
412+
413+
# we don't do any explicit push to JIRA here as it should happen automatically
414+
415+
post_jira_status = self.get_jira_issue_status(finding_id)
416+
self.assertNotEqual(pre_jira_status, post_jira_status)
417+
418+
pre_jira_status = post_jira_status
419+
ra = Risk_Acceptance.objects.last()
420+
ra_helper.expire_now(ra)
421+
422+
# we don't do any explicit push to JIRA here as it should happen automatically
423+
424+
post_jira_status = self.get_jira_issue_status(finding_id)
425+
self.assertNotEqual(pre_jira_status, post_jira_status)
426+
427+
# by asserting full cassette is played we know all calls to JIRA have been made as expected
428+
self.assert_cassette_played()
429+
377430
def test_import_with_groups_twice_push_to_jira(self):
378431
import0 = self.import_scan_with_params(self.npm_groups_sample_filename, scan_type="NPM Audit Scan", group_by="component_name+component_version", push_to_jira=True, verified=True)
379432
test_id = import0["test"]

0 commit comments

Comments
 (0)