forked from infobyte/faraday
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_api_workspace.py
504 lines (436 loc) · 22.8 KB
/
test_api_workspace.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
'''
Faraday Penetration Test IDE
Copyright (C) 2013 Infobyte LLC (http://www.infobytesec.com/)
See the file 'doc/LICENSE' for the license information
'''
from datetime import date
import time
from urllib.parse import urljoin
import pytest
from posixpath import join
from faraday.server.models import Workspace, Scope, SeveritiesHistogram
from faraday.server.api.modules.workspaces import WorkspaceView
from tests.test_api_non_workspaced_base import ReadWriteAPITests, BulkDeleteTestsMixin
from tests import factories
class TestWorkspaceAPI(ReadWriteAPITests, BulkDeleteTestsMixin):
model = Workspace
factory = factories.WorkspaceFactory
api_endpoint = 'ws'
lookup_field = 'name'
view_class = WorkspaceView
patchable_fields = ['description']
@pytest.mark.usefixtures('ignore_nplusone')
def test_filter_restless_by_name(self, test_client):
res = test_client.get(
join(
self.url(),
f'filter?q={{"filters":[{{"name": "name", "op":"eq", "val": "{self.first_object.name}"}}]}}'
)
)
assert res.status_code == 200
assert len(res.json) == 1
assert res.json[0]['name'] == self.first_object.name
@pytest.mark.usefixtures('ignore_nplusone')
def test_filter_restless_by_name_zero_results_found(self, test_client):
res = test_client.get(
join(
self.url(),
'filter?q={"filters":[{"name": "name", "op":"eq", "val": "thiswsdoesnotexist"}]}'
)
)
assert res.status_code == 200
assert len(res.json) == 0
def test_filter_restless_by_description(self, test_client):
self.first_object.description = "this is a new description"
res = test_client.get(
join(
self.url(),
f'filter?q={{"filters":[{{"name": "description", "op":"eq", "val": "{self.first_object.description}"}}'
']}'
)
)
assert res.status_code == 200
assert len(res.json) == 1
assert res.json[0]['description'] == self.first_object.description
def test_filter_restless_with_vulns_stats(self, test_client, vulnerability_factory,
vulnerability_web_factory, session):
vulns = vulnerability_factory.create_batch(8, workspace=self.first_object,
confirmed=False, status='open', severity='informational')
vulns += vulnerability_factory.create_batch(3, workspace=self.first_object,
confirmed=True, status='closed', severity='critical')
vulns += vulnerability_web_factory.create_batch(2, workspace=self.first_object,
confirmed=True, status='open', severity='low')
vulns += vulnerability_web_factory.create_batch(2, workspace=self.first_object,
confirmed=True, status='open', severity='high')
vulns += vulnerability_web_factory.create_batch(2, workspace=self.first_object,
confirmed=True, status='open', severity='unclassified')
vulns += vulnerability_web_factory.create_batch(2, workspace=self.first_object,
confirmed=True, status='open', severity='medium')
session.add_all(vulns)
session.commit()
self.first_object.description = "this is a new description"
res = test_client.get(
join(
self.url(),
f'filter?q={{"filters":[{{"name": "description", "op":"eq", "val": "{self.first_object.description}"}}'
']}'
)
)
assert res.status_code == 200
assert len(res.json) == 1
assert res.json[0]['stats']['std_vulns'] == 11
assert res.json[0]['stats']['web_vulns'] == 8
assert res.json[0]['stats']['code_vulns'] == 0
assert res.json[0]['active_agents_count'] == 0
assert res.json[0]['description'] == self.first_object.description
assert res.json[0]['stats']['total_vulns'] == 19
assert res.json[0]['stats']['info_vulns'] == 8
assert res.json[0]['stats']['critical_vulns'] == 3
assert res.json[0]['stats']['low_vulns'] == 2
assert res.json[0]['stats']['high_vulns'] == 2
assert res.json[0]['stats']['medium_vulns'] == 2
assert res.json[0]['stats']['unclassified_vulns'] == 2
def test_host_count(self, host_factory, test_client, session):
host_factory.create(workspace=self.first_object)
session.commit()
res = test_client.get(self.url(self.first_object))
assert res.status_code == 200
assert res.json['stats']['hosts'] == 1
@pytest.mark.parametrize('querystring', [
'?confirmed=1&status=open',
'?confirmed=true&status=open'
])
def test_vuln_count_open_confirmed(self,
vulnerability_factory,
vulnerability_web_factory,
test_client,
session,
querystring):
vulns = vulnerability_factory.create_batch(8, workspace=self.first_object,
confirmed=False, status='open', severity='critical')
vulns += vulnerability_factory.create_batch(3, workspace=self.first_object,
confirmed=True, status='closed', severity='critical')
vulns += vulnerability_web_factory.create_batch(2, workspace=self.first_object,
confirmed=True, status='open', severity='informational')
session.add_all(vulns)
session.commit()
res = test_client.get(urljoin(self.url(self.first_object), querystring))
assert res.status_code == 200
assert res.json['stats']['code_vulns'] == 0
assert res.json['stats']['web_vulns'] == 2
assert res.json['stats']['std_vulns'] == 0
assert res.json['stats']['critical_vulns'] == 0
assert res.json['stats']['info_vulns'] == 2
assert res.json['stats']['total_vulns'] == 2
assert res.json['last_run_agent_date'] is None
assert res.json['stats']['opened_vulns'] == 10
assert res.json['stats']['confirmed_vulns'] == 2
@pytest.mark.skip_sql_dialect('sqlite')
@pytest.mark.usefixtures('ignore_nplusone')
def test_histogram(self,
vulnerability_factory,
vulnerability_web_factory,
second_workspace,
test_client,
session):
session.query(SeveritiesHistogram).delete()
session.commit()
vulns = vulnerability_factory.create_batch(8, workspace=self.first_object,
confirmed=False, status='open', severity='critical')
vulns += vulnerability_factory.create_batch(3, workspace=self.first_object,
confirmed=True, status='open', severity='high')
vulns += vulnerability_web_factory.create_batch(2, workspace=second_workspace,
confirmed=True, status='open', severity='medium')
vulns += vulnerability_web_factory.create_batch(2, workspace=second_workspace,
confirmed=True, status='open', severity='low')
session.add_all(vulns)
session.commit()
res = test_client.get('/v3/ws?histogram=true')
assert res.status_code == 200
firs_ws = [ws['histogram'] for ws in res.json if ws['name'] == self.first_object.name]
assert len(firs_ws[0]) == 20
ws_histogram = firs_ws[0]
for ws_date in ws_histogram:
if ws_date['date'] == date.today().strftime("%Y-%m-%d"):
assert ws_date['medium'] == 0
assert ws_date['high'] == 3
assert ws_date['critical'] == 8
assert ws_date['confirmed'] == 3
else:
assert ws_date['medium'] == 0
assert ws_date['high'] == 0
assert ws_date['critical'] == 0
assert ws_date['confirmed'] == 0
second_ws = [ws['histogram'] for ws in res.json if ws['name'] == second_workspace.name]
assert len(second_ws[0]) == 20
ws_histogram = second_ws[0]
for ws_date in ws_histogram:
if ws_date['date'] == date.today().strftime("%Y-%m-%d"):
assert ws_date['medium'] == 2
assert ws_date['high'] == 0
assert ws_date['critical'] == 0
assert ws_date['confirmed'] == 2
else:
assert ws_date['medium'] == 0
assert ws_date['high'] == 0
assert ws_date['critical'] == 0
assert ws_date['confirmed'] == 0
res = test_client.get('/v3/ws?histogram=True&histogram_days=a')
assert res.status_code == 200
firs_ws = [ws['histogram'] for ws in res.json if ws['name'] == self.first_object.name]
assert len(firs_ws[0]) == 20
res = test_client.get('/v3/ws?histogram=true&histogram_days=[asdf, "adsf"]')
assert res.status_code == 200
firs_ws = [ws['histogram'] for ws in res.json if ws['name'] == self.first_object.name]
assert len(firs_ws[0]) == 20
res = test_client.get('/v3/ws?histogram=true&histogram_days=[asdf, "adsf"]')
assert res.status_code == 200
firs_ws = [ws['histogram'] for ws in res.json if ws['name'] == self.first_object.name]
assert len(firs_ws[0]) == 20
res = test_client.get('/v3/ws?histogram=true&histogram_days=5')
assert res.status_code == 200
firs_ws = [ws['histogram'] for ws in res.json if ws['name'] == self.first_object.name]
assert len(firs_ws[0]) == 5
res = test_client.get('/v3/ws?histogram=true&histogram_days=365')
assert res.status_code == 200
firs_ws = [ws['histogram'] for ws in res.json if ws['name'] == self.first_object.name]
assert len(firs_ws[0]) == 365
res = test_client.get('/v3/ws?histogram=asdf&histogram_days=365')
assert res.status_code == 200
for ws in res.json:
assert 'histogram' not in ws
@pytest.mark.parametrize('querystring', [
'?status=closed'
])
def test_vuln_count_closed(self,
vulnerability_factory,
vulnerability_web_factory,
test_client,
session,
querystring):
vulns = vulnerability_factory.create_batch(8, workspace=self.first_object,
confirmed=False, status='open', severity='informational')
vulns += vulnerability_factory.create_batch(3, workspace=self.first_object,
confirmed=True, status='closed', severity='informational')
vulns += vulnerability_web_factory.create_batch(2, workspace=self.first_object,
confirmed=True, status='open', severity='informational')
session.add_all(vulns)
session.commit()
res = test_client.get(urljoin(self.url(self.first_object), querystring))
assert res.status_code == 200
assert res.json['stats']['code_vulns'] == 0
assert res.json['stats']['web_vulns'] == 0
assert res.json['stats']['std_vulns'] == 3
assert res.json['stats']['critical_vulns'] == 0
assert res.json['stats']['info_vulns'] == 3
assert res.json['stats']['total_vulns'] == 3
assert res.json['stats']['opened_vulns'] == 0
assert res.json['stats']['confirmed_vulns'] == 3
@pytest.mark.parametrize('querystring', [
'?status=asdfss',
'?status=close',
'?status=',
'?status=\' or 1 == 1',
])
def test_vuln_count_invalid_status(self,
vulnerability_factory,
vulnerability_web_factory,
test_client,
session,
querystring):
vulns = vulnerability_factory.create_batch(8, workspace=self.first_object,
confirmed=False, status='open')
vulns += vulnerability_factory.create_batch(3, workspace=self.first_object,
confirmed=True, status='closed')
vulns += vulnerability_web_factory.create_batch(2, workspace=self.first_object,
confirmed=True, status='open')
session.add_all(vulns)
session.commit()
res = test_client.get(urljoin(self.url(self.first_object), querystring))
assert res.status_code == 200
assert res.json['stats']['code_vulns'] == 0
assert res.json['stats']['web_vulns'] == 2
assert res.json['stats']['std_vulns'] == 11
assert res.json['stats']['total_vulns'] == 13
@pytest.mark.parametrize('querystring', [
'?confirmed=1',
'?confirmed=true'
])
def test_vuln_count_confirmed(self,
vulnerability_factory,
test_client,
session,
querystring):
vulns = vulnerability_factory.create_batch(8, workspace=self.first_object,
confirmed=False)
vulns += vulnerability_factory.create_batch(5, workspace=self.first_object,
confirmed=True)
session.add_all(vulns)
session.commit()
res = test_client.get(urljoin(self.url(self.first_object), querystring))
assert res.status_code == 200
assert res.json['stats']['total_vulns'] == 5
@pytest.mark.parametrize('querystring', [
'?confirmed=0',
'?confirmed=false'
])
def test_vuln_count_confirmed_2(self, vulnerability_factory, test_client, session, querystring):
vulns = vulnerability_factory.create_batch(8, workspace=self.first_object,
confirmed=False, severity='critical', status='open')
vulns += vulnerability_factory.create_batch(5, workspace=self.first_object,
confirmed=True, status='open')
session.add_all(vulns)
session.commit()
res = test_client.get(self.url(self.first_object) + querystring)
assert res.status_code == 200
assert res.json['stats']['std_vulns'] == 8
assert res.json['stats']['critical_vulns'] == 8
assert res.json['stats']['info_vulns'] == 0
assert res.json['stats']['opened_vulns'] == 13
assert res.json['stats']['confirmed_vulns'] == 0
assert res.json['stats']['total_vulns'] == 8
def test_create_fails_with_valid_duration(self, session, test_client):
workspace_count_previous = session.query(Workspace).count()
start_date = int(time.time()) * 1000
end_date = start_date + 86400000
duration = {'start_date': start_date, 'end_date': end_date}
raw_data = {'name': 'somethingdarkside', 'duration': duration}
res = test_client.post(self.url(), data=raw_data)
assert res.status_code == 201
assert workspace_count_previous + 1 == session.query(Workspace).count()
assert res.json['duration']['start_date'] == start_date
assert res.json['duration']['end_date'] == end_date
def test_create_fails_with_mayus(self, session, test_client):
workspace_count_previous = session.query(Workspace).count()
raw_data = {'name': 'sWtr'}
res = test_client.post(self.url(), data=raw_data)
assert res.status_code == 400
assert workspace_count_previous == session.query(Workspace).count()
def test_create_fails_with_special_character(self, session, test_client):
workspace_count_previous = session.query(Workspace).count()
raw_data = {'name': '$wtr'}
res = test_client.post(self.url(), data=raw_data)
assert res.status_code == 400
assert workspace_count_previous == session.query(Workspace).count()
def test_create_with_initial_number(self, session, test_client):
workspace_count_previous = session.query(Workspace).count()
raw_data = {'name': '2$wtr'}
res = test_client.post(self.url(), data=raw_data)
assert res.status_code == 201
assert workspace_count_previous + 1 == session.query(Workspace).count()
def test_create_fails_with_invalid_duration_start_type(self,
session,
test_client):
workspace_count_previous = session.query(Workspace).count()
start_date = 'this should clearly fail'
duration = {'start_date': start_date, 'end_date': 86400000}
raw_data = {'name': 'somethingdarkside', 'duration': duration}
res = test_client.post(self.url(), data=raw_data)
assert res.status_code == 400
assert workspace_count_previous == session.query(Workspace).count()
@pytest.mark.xfail(reason="Filter not implemented yet")
def test_create_fails_with_invalid_duration_start_after_end(self,
session,
test_client):
workspace_count_previous = session.query(Workspace).count()
start_date = int(time.time()) * 1000
duration = {'start_date': start_date, 'end_date': start_date - 86400000}
raw_data = {'name': 'somethingdarkside', 'duration': duration}
res = test_client.post(self.url(), data=raw_data)
assert res.status_code == 400
assert workspace_count_previous == session.query(Workspace).count()
def test_create_fails_with_forward_slash(self, session, test_client):
workspace_count_previous = session.query(Workspace).count()
raw_data = {'name': 'swtr/'}
res = test_client.post(self.url(), data=raw_data)
assert res.status_code == 400
assert workspace_count_previous == session.query(Workspace).count()
def test_create_with_description(self, session, test_client):
description = 'darkside'
raw_data = {'name': 'something', 'description': description}
workspace_count_previous = session.query(Workspace).count()
res = test_client.post(self.url(), data=raw_data)
assert res.status_code == 201
assert workspace_count_previous + 1 == session.query(Workspace).count()
assert res.json['description'] == description
@pytest.mark.parametrize("stat_name", [
'credentials', 'services', 'web_vulns', 'code_vulns', 'std_vulns',
'total_vulns'
])
def test_create_stat_is_zero(self, test_client, stat_name):
raw_data = {'name': 'something', 'description': ''}
res = test_client.post(self.url(), data=raw_data)
assert res.status_code == 201
assert res.json['stats'][stat_name] == 0
def test_update_stats(self, workspace, session, test_client,
vulnerability_factory,
vulnerability_web_factory):
vulns = vulnerability_factory.create_batch(10, workspace=workspace)
vulns += vulnerability_web_factory.create_batch(5, workspace=workspace)
session.add_all(vulns)
session.commit()
raw_data = {'name': 'something', 'description': ''}
res = test_client.put(self.url(obj=workspace), data=raw_data)
assert res.status_code == 200
assert res.json['stats']['web_vulns'] == 5
assert res.json['stats']['std_vulns'] == 10
assert res.json['stats']['total_vulns'] == 15
def test_create_with_scope(self, session, test_client):
desired_scope = [
'www.google.com',
'127.0.0.1'
]
raw_data = {'name': 'something', 'description': 'test',
'scope': desired_scope}
res = test_client.post(self.url(), data=raw_data)
assert res.status_code == 201
assert set(res.json['scope']) == set(desired_scope)
workspace = Workspace.query.get(res.json['id'])
assert {s.name for s in workspace.scope} == set(desired_scope)
def test_update_with_scope(self, session, test_client, workspace):
session.add(Scope(name='test.com', workspace=workspace))
session.add(Scope(name='www.google.com', workspace=workspace))
desired_scope = [
'www.google.com',
'127.0.0.1'
]
raw_data = {'name': 'something', 'description': 'test',
'scope': desired_scope}
res = test_client.put(self.url(obj=workspace), data=raw_data)
assert res.status_code == 200
assert set(res.json['scope']) == set(desired_scope)
assert {s.name for s in workspace.scope} == set(desired_scope)
@pytest.mark.skip_sql_dialect('sqlite')
def test_list_retrieves_all_items_from(self, test_client, logged_user):
super().test_list_retrieves_all_items_from(test_client, logged_user)
def test_workspace_activation(self, test_client, workspace, session):
workspace.active = False
session.add(workspace)
session.commit()
res = test_client.patch(self.url(workspace), data={'active': True})
assert res.status_code == 200
res = test_client.get(self.url(workspace))
active = res.json.get('active')
assert active
active_query = session.query(Workspace).filter_by(id=workspace.id).first().active
assert active_query
def test_workspace_deactivation(self, test_client, workspace, session):
workspace.active = True
session.add(workspace)
session.commit()
res = test_client.patch(self.url(workspace), data={'active': False})
assert res.status_code == 200
res = test_client.get(self.url(workspace))
active = res.json.get('active')
assert not active
active_query = session.query(Workspace).filter_by(id=workspace.id).first().active
assert not active_query
def test_create_fails_with_start_date_greater_than_end_date(self,
session,
test_client):
workspace_count_previous = session.query(Workspace).count()
duration = {'start_date': 1563638577, 'end_date': 1563538577}
raw_data = {'name': 'somethingdarkside', 'duration': duration}
res = test_client.post(self.url(), data=raw_data)
assert res.status_code == 400
assert workspace_count_previous == session.query(Workspace).count()