forked from DefectDojo/django-DefectDojo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_risk_acceptance.py
305 lines (231 loc) · 12 KB
/
test_risk_acceptance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
from dateutil.relativedelta import relativedelta
from dojo.utils import get_system_setting
from django.utils import timezone
from django.utils.http import urlencode
from django.urls import reverse
from .dojo_test_case import DojoTestCase
from dojo.models import Risk_Acceptance, Finding, System_Settings
# from unittest.mock import patch
from django.utils.datastructures import MultiValueDict
from django.db.models import Q
import copy
# from unittest import skip
import dojo.risk_acceptance.helper as ra_helper
import logging
logger = logging.getLogger(__name__)
class RiskAcceptanceTestUI(DojoTestCase):
fixtures = ['dojo_testdata.json']
data_risk_accceptance = {
'name': 'Accept: Unit test',
'accepted_findings': [72808],
'recommendation': 'A',
'recommendation_details': 'recommendation 1',
'decision': 'A',
'decision_details': 'it has been decided!',
'accepted_by': 'pointy haired boss',
# 'path: (binary)
'owner': 1,
'expiration_date': '2021-07-15',
'reactivate_expired': True
}
data_remove_finding_from_ra = {
'remove_finding': 'Remove',
'remove_finding_id': 666,
}
def __init__(self, *args, **kwargs):
DojoTestCase.__init__(self, *args, **kwargs)
def setUp(self):
self.system_settings(enable_jira=True)
self.client.force_login(self.get_test_admin())
def add_risk_acceptance(self, eid, data_risk_accceptance, fid=None):
if fid:
args = (eid, fid, )
else:
args = (eid, )
response = self.client.post(reverse('add_risk_acceptance', args=args), data_risk_accceptance)
self.assertEqual(302, response.status_code, response.content[:1000])
return response
def assert_all_active_not_risk_accepted(self, findings):
if not all(finding.active for finding in findings):
return False
if not any(finding.risk_accepted for finding in findings):
return True
return False
def assert_all_inactive_risk_accepted(self, findings):
if any(finding.active for finding in findings):
return False
if all(finding.risk_accepted for finding in findings):
return True
return False
def test_add_risk_acceptance_single_findings_accepted(self):
ra_data = copy.copy(self.data_risk_accceptance)
ra_data['accepted_findings'] = [2]
ra_data['return_url'] = reverse('view_finding', args=(2, ))
response = self.add_risk_acceptance(1, ra_data, 2)
self.assertEqual('/finding/2', response.url)
ra = Risk_Acceptance.objects.last()
self.assert_all_active_not_risk_accepted(ra.accepted_findings.all())
def test_add_risk_acceptance_multiple_findings_accepted(self):
ra_data = copy.copy(self.data_risk_accceptance)
ra_data['accepted_findings'] = [2, 3]
response = self.add_risk_acceptance(1, ra_data)
self.assertEqual('/engagement/1', response.url)
ra = Risk_Acceptance.objects.last()
self.assert_all_active_not_risk_accepted(ra.accepted_findings.all())
def test_add_findings_to_risk_acceptance_findings_accepted(self):
# create risk acceptance first
self.test_add_risk_acceptance_multiple_findings_accepted()
ra = Risk_Acceptance.objects.last()
data_add_findings_to_ra = {
'add_findings': 'Add Selected Findings',
'accepted_findings': [4, 5]
}
response = self.client.post(reverse('view_risk_acceptance', args=(1, ra.id)),
urlencode(MultiValueDict(data_add_findings_to_ra), doseq=True),
content_type='application/x-www-form-urlencoded')
self.assertEqual(302, response.status_code, response.content[:1000])
self.assert_all_inactive_risk_accepted(Finding.objects.filter(id__in=[2, 3, 4, 5]))
def test_remove_findings_from_risk_acceptance_findings_active(self):
# create risk acceptance first
self.test_add_risk_acceptance_multiple_findings_accepted()
data = copy.copy(self.data_remove_finding_from_ra)
data['remove_finding_id'] = 2
ra = Risk_Acceptance.objects.last()
response = self.client.post(reverse('view_risk_acceptance', args=(1, ra.id)), data)
self.assertEqual(302, response.status_code, response.content[:1000])
self.assert_all_active_not_risk_accepted(Finding.objects.filter(id=2))
self.assert_all_inactive_risk_accepted(Finding.objects.filter(id=3))
def test_remove_risk_acceptance_findings_active(self):
self.test_add_risk_acceptance_multiple_findings_accepted()
ra = Risk_Acceptance.objects.last()
findings = ra.accepted_findings.all()
data = {'id': ra.id}
response = self.client.post(reverse('delete_risk_acceptance', args=(1, ra.id, )), data)
self.assert_all_active_not_risk_accepted(findings)
self.assert_all_active_not_risk_accepted(Finding.objects.filter(test__engagement=1))
def test_expire_risk_acceptance_findings_active(self):
self.test_add_risk_acceptance_multiple_findings_accepted()
ra = Risk_Acceptance.objects.last()
# ra.reactivate_expired = True # default True
# ra.save()
findings = ra.accepted_findings.all()
data = {'id': ra.id}
response = self.client.post(reverse('expire_risk_acceptance', args=(1, ra.id, )), data)
ra.refresh_from_db()
self.assert_all_active_not_risk_accepted(findings)
self.assertEqual(ra.expiration_date.date(), timezone.now().date())
self.assertEqual(ra.expiration_date_handled.date(), timezone.now().date())
self.assertIsNone(ra.expiration_date_warned)
self.assert_all_active_not_risk_accepted(Finding.objects.filter(test__engagement=1))
# findings remain in (expired) risk acceptance
self.assertTrue(all(finding in ra.accepted_findings.all() for finding in findings))
def test_expire_risk_acceptance_findings_not_active(self):
self.test_add_risk_acceptance_multiple_findings_accepted()
ra = Risk_Acceptance.objects.last()
ra.reactivate_expired = False
ra.save()
findings = ra.accepted_findings.all()
data = {'id': ra.id}
response = self.client.post(reverse('expire_risk_acceptance', args=(1, ra.id, )), data)
ra.refresh_from_db()
# no reactivation on expiry
self.assert_all_inactive_risk_accepted(findings)
self.assertEqual(ra.expiration_date.date(), timezone.now().date())
self.assertEqual(ra.expiration_date_handled.date(), timezone.now().date())
self.assertIsNone(ra.expiration_date_warned)
self.assert_all_active_not_risk_accepted(Finding.objects.filter(test__engagement=1).filter(~Q(id=2)))
# findings remain in (expired) risk acceptance
self.assertTrue(all(finding in ra.accepted_findings.all() for finding in findings))
def test_expire_risk_acceptance_sla_not_reset(self):
self.test_add_risk_acceptance_multiple_findings_accepted()
ra = Risk_Acceptance.objects.last()
# ra.restart_sla_expired = False # default is False
# ra.save()
findings = ra.accepted_findings.all()
data = {'id': ra.id}
response = self.client.post(reverse('expire_risk_acceptance', args=(1, ra.id, )), data)
ra.refresh_from_db()
self.assertTrue(all(finding.sla_start_date != timezone.now().date() for finding in findings))
def test_expire_risk_acceptance_sla_reset(self):
self.test_add_risk_acceptance_multiple_findings_accepted()
ra = Risk_Acceptance.objects.last()
ra.restart_sla_expired = True
ra.save()
findings = ra.accepted_findings.all()
data = {'id': ra.id}
response = self.client.post(reverse('expire_risk_acceptance', args=(1, ra.id, )), data)
ra.refresh_from_db()
self.assertTrue(all(finding.sla_start_date == timezone.now().date() for finding in findings))
def test_reinstate_risk_acceptance_findings_accepted(self):
# first create an expired risk acceptance
self.test_expire_risk_acceptance_findings_active()
ra = Risk_Acceptance.objects.last()
findings = ra.accepted_findings.all()
data = {'id': ra.id}
response = self.client.post(reverse('reinstate_risk_acceptance', args=(1, ra.id, )), data)
ra.refresh_from_db()
expiration_delta_days = get_system_setting('risk_acceptance_form_default_days', 90)
risk_acceptance_expiration_date = timezone.now() + relativedelta(days=expiration_delta_days)
self.assertEqual(ra.expiration_date.date(), risk_acceptance_expiration_date.date())
self.assertIsNone(ra.expiration_date_handled)
self.assertIsNone(ra.expiration_date_warned)
self.assert_all_inactive_risk_accepted(findings)
self.assert_all_active_not_risk_accepted(Finding.objects.filter(test__engagement=1).filter(~Q(id=2)))
# findings remain in (expired) risk acceptance
self.assertTrue(all(finding in ra.accepted_findings.all() for finding in findings))
def create_multiple_ras(self):
ra_data = copy.copy(self.data_risk_accceptance)
ra_data['accepted_findings'] = [2]
ra_data['return_url'] = reverse('view_finding', args=(2, ))
response = self.add_risk_acceptance(1, ra_data, 2)
ra1 = Risk_Acceptance.objects.last()
ra_data = copy.copy(self.data_risk_accceptance)
ra_data['accepted_findings'] = [7]
ra_data['return_url'] = reverse('view_finding', args=(7, ))
response = self.add_risk_acceptance(1, ra_data, 7)
ra2 = Risk_Acceptance.objects.last()
ra_data = copy.copy(self.data_risk_accceptance)
ra_data['accepted_findings'] = [22]
ra_data['return_url'] = reverse('view_finding', args=(22, ))
response = self.add_risk_acceptance(3, ra_data, 22)
ra3 = Risk_Acceptance.objects.last()
return ra1, ra2, ra3
def test_expiration_handler(self):
ra1, ra2, ra3 = self.create_multiple_ras()
system_settings = System_Settings.objects.get()
system_settings.risk_acceptance_notify_before_expiration = 10
system_settings.save()
heads_up_days = system_settings.risk_acceptance_notify_before_expiration
# ra1: expire in 9 days -> warn:yes, expire:no
# ra2: expire in 11 days -> warn:no, expire:no
# ra3: expire 5 days ago -> warn:no, expire:yes (expiration not handled yet, so expire)
ra1.expiration_date = timezone.now().date() + relativedelta(days=heads_up_days - 1)
ra2.expiration_date = timezone.now().date() + relativedelta(days=heads_up_days + 1)
ra3.expiration_date = timezone.now().date() - relativedelta(days=5)
ra1.save()
ra2.save()
ra3.save()
to_warn = ra_helper.get_almost_expired_risk_acceptances_to_handle(heads_up_days=heads_up_days)
to_expire = ra_helper.get_expired_risk_acceptances_to_handle()
self.assertTrue(ra1 in to_warn)
self.assertFalse(ra2 in to_warn)
self.assertFalse(ra3 in to_warn)
self.assertFalse(ra1 in to_expire)
self.assertFalse(ra2 in to_expire)
self.assertTrue(ra3 in to_expire)
# run job
ra_helper.expiration_handler()
ra1.refresh_from_db()
ra2.refresh_from_db()
ra3.refresh_from_db()
self.assertIsNotNone(ra1.expiration_date_warned)
self.assertIsNone(ra2.expiration_date_warned)
self.assertIsNone(ra3.expiration_date_warned)
self.assertIsNone(ra1.expiration_date_handled)
self.assertIsNone(ra2.expiration_date_handled)
self.assertIsNotNone(ra3.expiration_date_handled)
to_warn = ra_helper.get_almost_expired_risk_acceptances_to_handle(heads_up_days=heads_up_days)
to_expire = ra_helper.get_expired_risk_acceptances_to_handle()
# after handling no ra should be select for anything
self.assertFalse(any(ra in to_warn for ra in [ra1, ra2, ra3]))
self.assertFalse(any(ra in to_expire for ra in [ra1, ra2, ra3]))