/
ZeroFox_test.py
1172 lines (1035 loc) · 39.4 KB
/
ZeroFox_test.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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
from dateparser import parse as parse_date
from datetime import timedelta
from ZeroFox import (
ZFClient,
fetch_incidents,
get_modified_remote_data_command,
get_remote_data_command,
get_alert_command,
alert_user_assignment_command,
close_alert_command,
open_alert_command,
alert_request_takedown_command,
alert_cancel_takedown_command,
modify_alert_tags_command,
create_entity_command,
list_alerts_command,
list_entities_command,
get_entity_types_command,
get_policy_types_command,
modify_alert_notes_command,
submit_threat_command,
compromised_domain_command,
compromised_email_command,
malicious_ip_command,
malicious_hash_command,
search_exploits_command,
)
BASE_URL = "https://api.zerofox.com"
OK_CODES = (200, 201)
FETCH_LIMIT = 10
DATE_FORMAT = "%Y-%m-%dT%H:%M:%S.%f"
def load_json(file: str):
with open(file) as f:
return json.load(f)
def build_zf_client() -> ZFClient:
return ZFClient(
base_url=BASE_URL,
ok_codes=OK_CODES,
username='',
password='',
fetch_limit=FETCH_LIMIT,
)
def get_delayed_formatted_date(str_date: str, delay=timedelta(milliseconds=1)):
formatted_date = parse_date(str_date, date_formats=(DATE_FORMAT,),)
if formatted_date is None:
raise ValueError("date must be a valid string date")
delayed_date = formatted_date + delay
return delayed_date.strftime(DATE_FORMAT)
def test_fetch_incidents_first_time_with_no_data(requests_mock, mocker):
"""
Given
There is 0 alerts
And last_run is empty
When
Calling fetch_incidents
Then
It should list alerts with first_fetch_time as min_timestamp
And offset equals to 0
And return last_fetch equals to first_fetch_time
And last last_offset equals to 0
And 0 incidents
"""
alerts_response = load_json("test_data/alerts/list_no_records.json")
requests_mock.post("/1.0/api-token-auth/", json={"token": ""})
requests_mock.get("/1.0/alerts/", json=alerts_response)
client = build_zf_client()
last_run: dict = {}
first_fetch_time = "2023-06-01T00:00:00.000000"
first_fetch_time_parsed = parse_date(
first_fetch_time,
date_formats=(DATE_FORMAT,),
)
expected_offset = 0
spy = mocker.spy(client, "list_alerts")
next_run, incidents = fetch_incidents(
client,
last_run,
first_fetch_time,
)
spy.assert_called_once()
list_alert_params = spy.call_args[0][0]
assert list_alert_params.get("min_timestamp") == first_fetch_time_parsed
assert list_alert_params.get("sort_direction") == "asc"
assert list_alert_params.get("offset") == expected_offset
assert next_run["last_fetched"] == first_fetch_time
assert next_run["last_offset"] == str(expected_offset)
assert len(incidents) == 0
def test_fetch_incidents_first_time(requests_mock, mocker):
"""
Given
There are alerts (less than the fetch limit)
And there is no last_fetched in last_run
When
Calling fetch_incidents
Then
It should list alerts with first_fetch_time as min_timestamp
And offset equals to 0
And return last_fetch equals to last alert timestamp + 1 millisecond
And last last_offset equals to 0
And 10 incidents correctly formatted
"""
alerts_response = load_json("test_data/alerts/list_10_records.json")
last_alert_timestamp = alerts_response["alerts"][-1]["timestamp"]
requests_mock.post("/1.0/api-token-auth/", json={"token": ""})
requests_mock.get("/1.0/alerts/", json=alerts_response)
client = build_zf_client()
last_run: dict = {}
first_fetch_time = "2023-06-01T00:00:00.000000"
first_fetch_time_parsed = parse_date(
first_fetch_time,
date_formats=(DATE_FORMAT,),
)
last_alert_timestamp_formatted = get_delayed_formatted_date(
last_alert_timestamp,
)
expected_offset = 0
spy = mocker.spy(client, "list_alerts")
next_run, incidents = fetch_incidents(
client,
last_run,
first_fetch_time,
)
spy.assert_called_once()
list_alert_params = spy.call_args[0][0]
assert list_alert_params.get("min_timestamp") == first_fetch_time_parsed
assert list_alert_params.get("sort_direction") == "asc"
assert list_alert_params.get("offset") == expected_offset
assert next_run["last_fetched"] == last_alert_timestamp_formatted
assert next_run["last_offset"] == str(expected_offset)
assert len(incidents) == 10
for incident in incidents:
assert "mirror_instance" in incident["rawJSON"]
assert "mirror_direction" in incident["rawJSON"]
def test_fetch_incidents_no_first_time(requests_mock, mocker):
"""
Given
There are alerts
And there are more in the next page
And last_fetched is set in last_run
And last_offset is set in last_run
When
Calling fetch_incidents
Then
It should list alerts with the last_fetched set in last_run
And with the last_offset set in last_run
And return last_fetch equals to last_fetched set
And last_offset equals to the offset set in the "next" link of the response
And 10 incidents correctly formatted
"""
alerts_response = load_json("test_data/alerts/list_10_records_and_more.json")
alerts_response["alerts"][-1]["timestamp"]
requests_mock.post("/1.0/api-token-auth/", json={"token": ""})
requests_mock.get("/1.0/alerts/", json=alerts_response)
client = build_zf_client()
last_offset_saved = 10
last_run = {
"last_fetched": "2023-07-01T12:34:56.000000",
"last_offset": str(last_offset_saved),
}
first_fetch_time = "2023-06-01T00:00:00.000000"
last_offset_expected = 20
spy = mocker.spy(client, "list_alerts")
next_run, incidents = fetch_incidents(
client,
last_run,
first_fetch_time,
)
spy.assert_called_once()
list_alert_params = spy.call_args[0][0]
min_timestamp_called = list_alert_params.get(
"min_timestamp"
).strftime(DATE_FORMAT)
assert min_timestamp_called == last_run["last_fetched"]
assert list_alert_params.get("sort_direction") == "asc"
assert list_alert_params.get("offset") == last_offset_saved
assert next_run["last_fetched"] == last_run["last_fetched"]
assert next_run["last_offset"] == str(last_offset_expected)
assert len(incidents) == 10
for incident in incidents:
assert "mirror_instance" in incident["rawJSON"]
assert "mirror_direction" in incident["rawJSON"]
def test_get_modified_remote_data_command_with_no_data(requests_mock, mocker):
"""
Given
There are no modified alerts
When
Calling get_modified_remote_data_command
Then
It should list alerts with the last_fetched set in last_run
And return an empty list
"""
alerts_response = load_json("test_data/alerts/list_no_records.json")
requests_mock.post("/1.0/api-token-auth/", json={"token": ""})
requests_mock.get("/1.0/alerts/", json=alerts_response)
client = build_zf_client()
spy = mocker.spy(client, "list_alerts")
args = {"lastUpdate": "2023-07-01T12:34:56"}
results = get_modified_remote_data_command(client, args)
spy.assert_called_once()
list_alerts_call_args = spy.call_args[0][0]
assert list_alerts_call_args["last_modified_min_date"] == args["lastUpdate"]
assert len(results.modified_incident_ids) == 0
def test_get_modified_remote_data_command(requests_mock, mocker):
"""
Given
There are modified alerts
When
Calling get_modified_remote_data_command
Then
It should list alerts with the last_fetched set in last_run
And return a list with the ids of the modified alerts as strings
"""
alerts_response = load_json("test_data/alerts/list_10_records.json")
requests_mock.post("/1.0/api-token-auth/", json={"token": ""})
requests_mock.get("/1.0/alerts/", json=alerts_response)
client = build_zf_client()
spy = mocker.spy(client, "list_alerts")
args = {"lastUpdate": "2023-07-01T12:34:56"}
results = get_modified_remote_data_command(client, args)
spy.assert_called_once()
list_alerts_call_args = spy.call_args[0][0]
assert list_alerts_call_args["last_modified_min_date"] == args["lastUpdate"]
assert len(results.modified_incident_ids) == 10
for modified_incident_id in results.modified_incident_ids:
assert isinstance(modified_incident_id, str)
def test_get_remote_data_command_with_opened_alert(requests_mock, mocker):
"""
Given
There is an opened alert id
When
Calling get_remote_data_command
Then
It should call fetch alert with the given id
And return the alert content
And no entries in entries list
"""
alert_id = 123
alert_response = load_json("test_data/alerts/opened_alert.json")
requests_mock.post("/1.0/api-token-auth/", json={"token": ""})
requests_mock.get(f"/1.0/alerts/{alert_id}/", json=alert_response)
client = build_zf_client()
spy = mocker.spy(client, "get_alert")
args = {"id": alert_id, "lastUpdate": ""}
results = get_remote_data_command(client, args)
spy.assert_called_once()
get_alert_call_arg = spy.call_args[0][0]
assert get_alert_call_arg == args["id"]
assert len(results.entries) == 0
def test_get_remote_data_command_with_closed_alert(requests_mock, mocker):
"""
Given
There is an opened alert id
When
Calling get_remote_data_command
Then
It should call fetch alert with the given id
And return the alert content
And one entry in the entries list
"""
alert_id = "123"
alert_response = load_json("test_data/alerts/closed_alert.json")
requests_mock.post("/1.0/api-token-auth/", json={"token": ""})
requests_mock.get(f"/1.0/alerts/{alert_id}/", json=alert_response)
client = build_zf_client()
spy = mocker.spy(client, "get_alert")
args = {"id": alert_id, "lastUpdate": ""}
results = get_remote_data_command(client, args)
spy.assert_called_once()
get_alert_call_arg = spy.call_args[0][0]
assert get_alert_call_arg == args["id"]
assert len(results.entries) == 1
def test_get_alert_command(requests_mock, mocker):
"""
Given
There is an alert id
When
Calling get_alert_command
Then
It should call fetch alert with the given id
And return the alert as output
And with the correct output prefix
"""
alert_id = 123
alert_response = load_json("test_data/alerts/closed_alert.json")
requests_mock.post("/1.0/api-token-auth/", json={"token": ""})
requests_mock.get(f"/1.0/alerts/{alert_id}/", json=alert_response)
client = build_zf_client()
spy = mocker.spy(client, "get_alert")
args = {"alert_id": alert_id}
results = get_alert_command(client, args)
spy.assert_called_once()
get_alert_call_arg = spy.call_args[0][0]
assert get_alert_call_arg == args["alert_id"]
assert isinstance(results.outputs, dict)
assert results.outputs_prefix == "ZeroFox.Alert"
def test_alert_user_assignment_command(requests_mock, mocker):
"""
Given
There is a username
And an alert id
When
Calling alert_user_assignment_command
Then
It should call the assign user to alert with correct data
And call fetch alert with the alert id
And return the alert as output
And with the correct output prefix
"""
alert_id = "123"
username = "user123"
alert_response = load_json("test_data/alerts/closed_alert.json")
requests_mock.post("/1.0/api-token-auth/", json={"token": ""})
requests_mock.post(f"/1.0/alerts/{alert_id}/assign/")
requests_mock.get(f"/1.0/alerts/{alert_id}/", json=alert_response)
client = build_zf_client()
spy_assignment = mocker.spy(client, "alert_user_assignment")
spy_fetch = mocker.spy(client, "get_alert")
args = {"alert_id": alert_id, "username": username}
results = alert_user_assignment_command(client, args)
spy_assignment.assert_called_once()
alert_id_called_in_assignment, username_called = spy_assignment.call_args[0]
assert int(alert_id) == alert_id_called_in_assignment
assert username == username_called
spy_fetch.assert_called_once()
alert_id_called_in_fetch, = spy_fetch.call_args[0]
assert int(alert_id) == alert_id_called_in_fetch
assert isinstance(results.outputs, dict)
assert results.outputs_prefix == "ZeroFox.Alert"
def test_close_alert_command(requests_mock, mocker):
"""
Given
There is an alert id
When
Calling close_alert_command
Then
It should call the close alert with the alert id
And call fetch alert with the alert id
And return the alert as output
And with the correct output prefix
"""
alert_id = "123"
alert_response = load_json("test_data/alerts/closed_alert.json")
requests_mock.post("/1.0/api-token-auth/", json={"token": ""})
requests_mock.post(f"/1.0/alerts/{alert_id}/close/")
requests_mock.get(f"/1.0/alerts/{alert_id}/", json=alert_response)
client = build_zf_client()
spy_close = mocker.spy(client, "close_alert")
spy_fetch = mocker.spy(client, "get_alert")
args = {"alert_id": alert_id}
results = close_alert_command(client, args)
spy_close.assert_called_once()
alert_id_called_in_close, = spy_close.call_args[0]
assert int(alert_id) == alert_id_called_in_close
spy_fetch.assert_called_once()
alert_id_called_in_fetch, = spy_fetch.call_args[0]
assert int(alert_id) == alert_id_called_in_fetch
assert isinstance(results.outputs, dict)
assert results.outputs_prefix == "ZeroFox.Alert"
def test_open_alert_command(requests_mock, mocker):
"""
Given
There is an alert id
When
Calling open_alert_command
Then
It should call the open alert with the alert id
And call fetch alert with the alert id
And return the alert as output
And with the correct output prefix
"""
alert_id = "123"
alert_response = load_json("test_data/alerts/opened_alert.json")
requests_mock.post("/1.0/api-token-auth/", json={"token": ""})
requests_mock.post(f"/1.0/alerts/{alert_id}/open/")
requests_mock.get(f"/1.0/alerts/{alert_id}/", json=alert_response)
client = build_zf_client()
spy_open = mocker.spy(client, "open_alert")
spy_fetch = mocker.spy(client, "get_alert")
args = {"alert_id": alert_id}
results = open_alert_command(client, args)
spy_open.assert_called_once()
alert_id_called_in_open, = spy_open.call_args[0]
assert int(alert_id) == alert_id_called_in_open
spy_fetch.assert_called_once()
alert_id_called_in_fetch, = spy_fetch.call_args[0]
assert int(alert_id) == alert_id_called_in_fetch
assert isinstance(results.outputs, dict)
assert results.outputs_prefix == "ZeroFox.Alert"
def test_alert_request_takedown_command(requests_mock, mocker):
"""
Given
There is an alert id
When
Calling alert_request_takedown_command
Then
It should call the request takedown alert with the alert id
And call fetch alert with the alert id
And return the alert as output
And with the correct output prefix
"""
alert_id = "123"
alert_response = load_json("test_data/alerts/opened_alert.json")
requests_mock.post("/1.0/api-token-auth/", json={"token": ""})
requests_mock.post(f"/1.0/alerts/{alert_id}/request_takedown/")
requests_mock.get(f"/1.0/alerts/{alert_id}/", json=alert_response)
client = build_zf_client()
spy_request_takedown = mocker.spy(client, "alert_request_takedown")
spy_fetch = mocker.spy(client, "get_alert")
args = {"alert_id": alert_id}
results = alert_request_takedown_command(client, args)
spy_request_takedown.assert_called_once()
alert_id_called_in_request, = spy_request_takedown.call_args[0]
assert int(alert_id) == alert_id_called_in_request
spy_fetch.assert_called_once()
alert_id_called_in_fetch, = spy_fetch.call_args[0]
assert int(alert_id) == alert_id_called_in_fetch
assert isinstance(results.outputs, dict)
assert results.outputs_prefix == "ZeroFox.Alert"
def test_alert_cancel_takedown_command(requests_mock, mocker):
"""
Given
There is an alert id
When
Calling alert_cancel_takedown_command
Then
It should call the cancel takedown alert with the alert id
And call fetch alert with the alert id
And return the alert as output
And with the correct output prefix
"""
alert_id = "123"
alert_response = load_json("test_data/alerts/opened_alert.json")
requests_mock.post("/1.0/api-token-auth/", json={"token": ""})
requests_mock.post(f"/1.0/alerts/{alert_id}/cancel_takedown/")
requests_mock.get(f"/1.0/alerts/{alert_id}/", json=alert_response)
client = build_zf_client()
spy_cancel_takedown = mocker.spy(client, "alert_cancel_takedown")
spy_fetch = mocker.spy(client, "get_alert")
args = {"alert_id": alert_id}
results = alert_cancel_takedown_command(client, args)
spy_cancel_takedown.assert_called_once()
alert_id_called_in_cancel, = spy_cancel_takedown.call_args[0]
assert int(alert_id) == alert_id_called_in_cancel
spy_fetch.assert_called_once()
alert_id_called_in_fetch, = spy_fetch.call_args[0]
assert int(alert_id) == alert_id_called_in_fetch
assert isinstance(results.outputs, dict)
assert results.outputs_prefix == "ZeroFox.Alert"
def test_modify_alert_tags_command(requests_mock, mocker):
"""
Given
There is an alert id
When
Calling modify_alert_tags_command
Then
It should call the modify alert tags with the alert id
And the tags
And the action
And call fetch alert with the alert id
And return the alert as output
And with the correct output prefix
"""
alert_id = "123"
tags = "tag1,tag2,tag3"
action = "add"
action_in_request = "added"
tags_in_request = tags.split(",")
alert_response = load_json("test_data/alerts/opened_alert.json")
change_tags_response = load_json("test_data/alerts/change_tags.json")
requests_mock.post("/1.0/api-token-auth/", json={"token": ""})
requests_mock.post("/1.0/alerttagchangeset/", json=change_tags_response)
requests_mock.get(f"/1.0/alerts/{alert_id}/", json=alert_response)
client = build_zf_client()
spy_modify = mocker.spy(client, "modify_alert_tags")
spy_fetch = mocker.spy(client, "get_alert")
args = {"alert_id": alert_id, "tags": tags, "action": action}
results = modify_alert_tags_command(client, args)
spy_modify.assert_called_once()
alert_id_called, action_called, tags_called = spy_modify.call_args[0]
assert int(alert_id) == alert_id_called
assert action_in_request == action_called
assert tags_in_request == tags_called
spy_fetch.assert_called_once()
alert_id_called_in_fetch, = spy_fetch.call_args[0]
assert int(alert_id) == alert_id_called_in_fetch
assert isinstance(results.outputs, dict)
assert results.outputs_prefix == "ZeroFox.Alert"
def test_create_entity_command_with_true_flag(requests_mock, mocker):
"""
Given
There is an entity name
and its strict name matching flag
and its tags
and its policy id
and its organization
to create an entity
When
Calling create_entity_command
Then
It should call the create entity with the name
And the strict_name_matching flag
And the tags
And the policy_id
And the organization
And return the entity as output
And with the correct output prefix
"""
entity_name = "name"
strict_name_matching = "true"
tags = "tag1,tag2,tag3"
policy_id = 1
organization = "org"
strict_name_matching_request = True
tags_request = tags.split(",")
entity_response = load_json("test_data/entities/create_entity.json")
requests_mock.post("/1.0/api-token-auth/", json={"token": ""})
requests_mock.post("/1.0/entities/", json=entity_response)
client = build_zf_client()
spy_create_entity = mocker.spy(client, "create_entity")
args = {
"name": entity_name,
"strict_name_matching": strict_name_matching,
"tags": tags,
"policy_id": policy_id,
"organization": organization,
}
results = create_entity_command(client, args)
spy_create_entity.assert_called_once()
called_args = spy_create_entity.call_args[0]
entity_name_called = called_args[0]
strict_name_matching_called = called_args[1]
tags_called = called_args[2]
policy_id_called = called_args[3]
organization_called = called_args[4]
assert entity_name_called == entity_name
assert strict_name_matching_called == strict_name_matching_request
assert tags_called == tags_request
assert policy_id_called == policy_id
assert organization_called == organization
assert isinstance(results.outputs, dict)
assert results.outputs_prefix == 'ZeroFox.Entity'
def test_create_entity_command_with_false_flag(requests_mock, mocker):
"""
Given
There is an entity name
and its strict name matching flag
and its tags
and its policy id
and its organization
to create an entity
When
Calling create_entity_command
Then
It should call the create entity with the name
And the strict_name_matching flag
And the tags
And the policy_id
And the organization
And return the entity as output
And with the correct output prefix
"""
entity_name = "name"
strict_name_matching = "false"
tags = "tag1,tag2,tag3"
policy_id = 1
organization = "org"
strict_name_matching_request = False
tags_request = tags.split(",")
entity_response = load_json("test_data/entities/create_entity.json")
requests_mock.post("/1.0/api-token-auth/", json={"token": ""})
requests_mock.post("/1.0/entities/", json=entity_response)
client = build_zf_client()
spy_create_entity = mocker.spy(client, "create_entity")
args = {
"name": entity_name,
"strict_name_matching": strict_name_matching,
"tags": tags,
"policy_id": policy_id,
"organization": organization,
}
results = create_entity_command(client, args)
spy_create_entity.assert_called_once()
called_args = spy_create_entity.call_args[0]
entity_name_called = called_args[0]
strict_name_matching_called = called_args[1]
tags_called = called_args[2]
policy_id_called = called_args[3]
organization_called = called_args[4]
assert entity_name_called == entity_name
assert strict_name_matching_called == strict_name_matching_request
assert tags_called == tags_request
assert policy_id_called == policy_id
assert organization_called == organization
assert isinstance(results.outputs, dict)
assert results.outputs_prefix == 'ZeroFox.Entity'
def test_list_alerts_command_with_no_records(requests_mock, mocker):
"""
Given
There is no alerts
When
Calling list_alerts_command
Then
It should call fetch alerts
And return an empty list as output
And with the correct output prefix
"""
alerts_response = load_json("test_data/alerts/list_no_records.json")
requests_mock.post("/1.0/api-token-auth/", json={"token": ""})
requests_mock.get("/1.0/alerts/", json=alerts_response)
client = build_zf_client()
spy = mocker.spy(client, "list_alerts")
args: dict = {}
results = list_alerts_command(client, args)
spy.assert_called_once()
assert len(results.outputs) == 0
assert results.outputs_prefix == "ZeroFox.Alert"
def test_list_alerts_command_with_records(requests_mock, mocker):
"""
Given
There are alerts
When
Calling list_alerts_command
Then
It should call fetch alerts
And return a list with alerts as output
And with the correct output prefix
"""
alerts_response = load_json("test_data/alerts/list_10_records.json")
requests_mock.post("/1.0/api-token-auth/", json={"token": ""})
requests_mock.get("/1.0/alerts/", json=alerts_response)
client = build_zf_client()
spy = mocker.spy(client, "list_alerts")
args: dict = {}
results = list_alerts_command(client, args)
spy.assert_called_once()
assert len(results.outputs) == 10
assert results.outputs_prefix == "ZeroFox.Alert"
def test_list_entities_command_with_no_records(requests_mock, mocker):
"""
Given
There is no entities
When
Calling list_entities_command
Then
It should call fetch entities
And return an empty list as output
And with the correct output prefix
"""
entities_response = load_json("test_data/entities/entities_no_records.json")
requests_mock.post("/1.0/api-token-auth/", json={"token": ""})
requests_mock.get("/1.0/entities/", json=entities_response)
client = build_zf_client()
spy = mocker.spy(client, "list_entities")
args: dict = {}
results = list_entities_command(client, args)
spy.assert_called_once()
assert len(results.outputs) == 0
assert results.outputs_prefix == "ZeroFox.Entity"
def test_list_entities_command_with_records(requests_mock, mocker):
"""
Given
There are entities
When
Calling list_entities_command
Then
It should call fetch entities
And return a list with entities as output
And with the correct output prefix
"""
entities_response = load_json("test_data/entities/entities_8_records.json")
requests_mock.post("/1.0/api-token-auth/", json={"token": ""})
requests_mock.get("/1.0/entities/", json=entities_response)
client = build_zf_client()
spy = mocker.spy(client, "list_entities")
args: dict = {}
results = list_entities_command(client, args)
spy.assert_called_once()
assert len(results.outputs) == 8
assert results.outputs_prefix == "ZeroFox.Entity"
def test_get_entity_types_command_with_no_records(requests_mock, mocker):
"""
Given
There is no entity types
When
Calling get_entity_types_command
Then
It should call fetch entity types
And return an empty list as output
And with the correct output prefix
"""
entity_types_response = load_json(
"test_data/entities/entity_types_no_records.json",
)
requests_mock.post("/1.0/api-token-auth/", json={"token": ""})
requests_mock.get("/1.0/entities/types/", json=entity_types_response)
client = build_zf_client()
spy = mocker.spy(client, "get_entity_types")
args: dict = {}
results = get_entity_types_command(client, args)
spy.assert_called_once()
assert len(results.outputs) == 0
assert results.outputs_prefix == "ZeroFox.EntityTypes"
def test_get_entity_types_command_with_records(requests_mock, mocker):
"""
Given
There are entity types
When
Calling get_entity_types_command
Then
It should call fetch entity types
And return a list with entity types as output
And with the correct output prefix
"""
entity_types_response = load_json(
"test_data/entities/entity_types_10_records.json",
)
requests_mock.post("/1.0/api-token-auth/", json={"token": ""})
requests_mock.get("/1.0/entities/types/", json=entity_types_response)
client = build_zf_client()
spy = mocker.spy(client, "get_entity_types")
args: dict = {}
results = get_entity_types_command(client, args)
spy.assert_called_once()
assert len(results.outputs) == 10
assert results.outputs_prefix == "ZeroFox.EntityTypes"
def test_get_policy_types_command_with_no_records(requests_mock, mocker):
"""
Given
There is no policy types
When
Calling get_policy_types_command
Then
It should call fetch policy types
And return an empty list as output
And with the correct output prefix
"""
policy_types_response = load_json(
"test_data/policies/policy_types_no_records.json",
)
requests_mock.post("/1.0/api-token-auth/", json={"token": ""})
requests_mock.get("/1.0/policies/", json=policy_types_response)
client = build_zf_client()
spy = mocker.spy(client, "get_policy_types")
args: dict = {}
results = get_policy_types_command(client, args)
spy.assert_called_once()
assert len(results.outputs) == 0
assert results.outputs_prefix == "ZeroFox.PolicyTypes"
def test_get_policy_types_command_with_records(requests_mock, mocker):
"""
Given
There are policy types
When
Calling get_policy_types_command
Then
It should call fetch policy types
And return a list with policy types as output
And with the correct output prefix
"""
policy_types_response = load_json(
"test_data/policies/policy_types_13_records.json",
)
requests_mock.post("/1.0/api-token-auth/", json={"token": ""})
requests_mock.get("/1.0/policies/", json=policy_types_response)
client = build_zf_client()
spy = mocker.spy(client, "get_policy_types")
args: dict = {}
results = get_policy_types_command(client, args)
spy.assert_called_once()
assert len(results.outputs) == 13
assert results.outputs_prefix == "ZeroFox.PolicyTypes"
def test_modify_alert_notes_command(requests_mock, mocker):
"""
Given
There is an alert id
When
Calling modify_alert_notes_command
Then
It should call the modify alert notes with the alert id
And the notes
And the action
And call fetch alert with the alert id
And return the alert as output
And with the correct output prefix
"""
alert_id = "123"
notes = "some notes"
alert_response = load_json("test_data/alerts/opened_alert.json")
requests_mock.post("/1.0/api-token-auth/", json={"token": ""})
requests_mock.post(f"/1.0/alerts/{alert_id}/")
requests_mock.get(f"/1.0/alerts/{alert_id}/", json=alert_response)
client = build_zf_client()
spy_modify = mocker.spy(client, "modify_alert_notes")
spy_fetch = mocker.spy(client, "get_alert")
args = {"alert_id": alert_id, "notes": notes}
results = modify_alert_notes_command(client, args)
spy_modify.assert_called_once()
alert_id_called, notes_called, = spy_modify.call_args[0]
assert int(alert_id) == alert_id_called
assert notes_called == notes
spy_fetch.assert_called_once()
alert_id_called_in_fetch, = spy_fetch.call_args[0]
assert int(alert_id) == alert_id_called_in_fetch
assert isinstance(results.outputs, dict)
assert results.outputs_prefix == "ZeroFox.Alert"
def test_submit_threat_command(requests_mock, mocker):
"""
Given
There is a source
And an alert type
And a violation
And an entity id
When
Calling submit_threat_command
Then
It should call the submit threat with the source
And the alert type
And the violation
And the entity id
And return the alert id as output
And with the correct output prefix
"""
source = "abc@test.com"
alert_type = "email"
violation = "phishing"
entity_id = "123"
alert_id = "123"
submit_response = load_json("test_data/alerts/submit_threat.json")
alert_response = load_json("test_data/alerts/opened_alert.json")
requests_mock.post("/1.0/api-token-auth/", json={"token": ""})
requests_mock.post("/2.0/threat_submit/", json=submit_response)
requests_mock.get(f"/1.0/alerts/{alert_id}/", json=alert_response)
client = build_zf_client()
spy_submit = mocker.spy(client, "submit_threat")
args = {
"source": source,
"alert_type": alert_type,
"violation": violation,
"entity_id": entity_id,
}
results = submit_threat_command(client, args)
spy_submit.assert_called_once()
submit_threat_args = spy_submit.call_args[0]
source_called = submit_threat_args[0]
alert_type_called = submit_threat_args[1]
violation_called = submit_threat_args[2]
entity_id_called = submit_threat_args[3]
assert source == source_called
assert alert_type == alert_type_called
assert violation == violation_called
assert entity_id == entity_id_called
assert isinstance(results.outputs, dict)
assert results.outputs_prefix == "ZeroFox.Alert"
def test_compromised_domain_command(requests_mock, mocker):
"""
Given
There is a domain
When
Calling compromised_domain_command
Then
It should call c2-domain endpoint
And phishing endpoint
And return a list with threats as output
And with the correct output prefix
"""
domain = "abc.xyz"
c2_domains_response = load_json("test_data/cti/c2-domains.json")
phishing_response = load_json("test_data/cti/phishing.json")
requests_mock.post("/auth/token/verify/")
requests_mock.post("/auth/token/", json={"access": "token"})
requests_mock.get("/cti/c2-domains/", json=c2_domains_response)
requests_mock.get("/cti/phishing/", json=phishing_response)
client = build_zf_client()
spy_c2_domains = mocker.spy(client, "get_cti_c2_domains")
spy_phishing = mocker.spy(client, "get_cti_phishing")
args = {"domain": domain}
results = compromised_domain_command(client, args)
spy_c2_domains.assert_called_once()
c2_domains_domain_arg, = spy_c2_domains.call_args[0]
assert c2_domains_domain_arg == domain
spy_phishing.assert_called_once()
phishing_domain_arg = spy_phishing.call_args.kwargs.get("domain")