-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathFireEyeHXv2_test.py
2027 lines (1547 loc) · 63.5 KB
/
FireEyeHXv2_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 pathlib import Path
from typing import Any
import pytest
from CommonServerPython import DemistoException
def util_load_json(path):
with open(path, encoding='utf-8') as f:
return json.loads(f.read())
"""POLICIES"""
UPSERT_COMMAND_DATA_BAD_CASES = [
(
{"policyName": "test", "policyId": "test"},
"Enter a name or ID but not both"
)
]
@pytest.mark.parametrize('demisto_args, expected_results', UPSERT_COMMAND_DATA_BAD_CASES)
def test_list_policy_command_failed(demisto_args, expected_results):
"""
Given:
- agentId or hostName
When:
- Get list of all the policies
Then:
- failing when missing required data
"""
from FireEyeHXv2 import list_policy_command
client = ""
with pytest.raises(Exception) as e:
list_policy_command(client, demisto_args)
assert str(e.value) == expected_results
UPSERT_COMMAND_DATA_BAD_CASES = [
(
{'hostSetId': "test", "policyId": "test"},
"Enter a Policy Id or Host Set Id but not both"
)
]
@pytest.mark.parametrize('demisto_args, expected_results', UPSERT_COMMAND_DATA_BAD_CASES)
def test_list_host_set_policy_command_failed(demisto_args, expected_results):
"""
Given:
- hostSetId or policyId
When:
- Get specific policy by Policy Id or policies by Host Set Id
Then:
- failing when missing required data
"""
from FireEyeHXv2 import list_host_set_policy_command
client = ""
with pytest.raises(Exception) as e:
list_host_set_policy_command(client, demisto_args)
assert str(e.value) == expected_results
UPSERT_COMMAND_DATA_BAD_CASES = [
(
# Given only one argument instead of two
{"policyId": "test"},
"policy ID and hostSetId are required"
),
(
# Given only one argument instead of two
{"hostSetId": "test"},
"policy ID and hostSetId are required"
),
(
# No arguments given
{},
"policy ID and hostSetId are required"
)
]
@pytest.mark.parametrize('demisto_args, expected_results', UPSERT_COMMAND_DATA_BAD_CASES)
def test_assign_host_set_policy_command_failed(demisto_args, expected_results):
"""
Given:
- agentId or hostName
When:
- assigning host set to the policy
Then:
- failing when missing required data
"""
from FireEyeHXv2 import assign_host_set_policy_command
client = ""
with pytest.raises(Exception) as e:
assign_host_set_policy_command(client, demisto_args)
assert str(e.value) == expected_results
UPSERT_COMMAND_DATA_CASES_LIST_POLICY = [
(
{'policyId': 'test', 'hostSetId': 'test'},
Exception('400'),
'This hostset may already be included in this policy'
),
(
{'policyId': 'test', 'hostSetId': 'test'},
'test',
'Success'
)
]
@pytest.mark.parametrize('demisto_args, return_mocker, expected_results', UPSERT_COMMAND_DATA_CASES_LIST_POLICY)
def test_assign_host_set_policy_command(mocker, demisto_args, return_mocker, expected_results):
from FireEyeHXv2 import assign_host_set_policy_command, Client
mocker.patch.object(Client, 'assign_host_set_policy_request', side_effect=return_mocker)
result = assign_host_set_policy_command(Client, demisto_args)
assert result.readable_output == expected_results
pass
UPSERT_COMMAND_DATA_CASES_LIST_POLICY = [
(
{'policyId': 11, 'enabled': 'test', 'limit': 5, 'offset': 2},
{'policy_id': 11, 'enabled': 'test', 'limit': 5, 'offset': 2, 'name': None}
),
(
{'policyName': 'test'},
{'policy_id': None, 'enabled': None, 'limit': 50, 'offset': 0, 'name': 'test'}
)
]
@pytest.mark.parametrize('demisto_args, call_args', UPSERT_COMMAND_DATA_CASES_LIST_POLICY)
def test_list_policy_command(mocker, demisto_args, call_args):
from FireEyeHXv2 import list_policy_command, Client
return_value = {
'data': {
'entries': [{'_id': 'test', 'name': 'test', 'description': 'test', 'priority': 'test', 'enabled': 'test'}]
}
}
policies = mocker.patch.object(Client, 'list_policy_request', return_value=return_value)
mocker.patch('FireEyeHXv2.tableToMarkdown', return_value='test')
list_policy_command(Client, demisto_args)
assert policies.call_args.kwargs['policy_id'] == call_args['policy_id']
assert policies.call_args.kwargs['offset'] == call_args['offset']
assert policies.call_args.kwargs['limit'] == call_args['limit']
assert policies.call_args.kwargs['name'] == call_args['name']
assert policies.call_args.kwargs['enabled'] == call_args['enabled']
UPSERT_COMMAND_DATA_CASES_LIST_HOST_SET_POLICY = [
(
{'hostSetId': 11},
{"hostSetId": 1, "hostSetPolicy": 0},
{}
),
(
{},
{"hostSetId": 0, "hostSetPolicy": 1},
{'limit': 50, 'offset': 0}
),
(
{'limit': 5, 'offset': 2},
{"hostSetId": 0, "hostSetPolicy": 1},
{'limit': 5, 'offset': 2}
)
]
@pytest.mark.parametrize('demisto_args, call_count, call_args', UPSERT_COMMAND_DATA_CASES_LIST_HOST_SET_POLICY)
def test_list_host_set_policy_command(mocker, demisto_args, call_count, call_args):
from FireEyeHXv2 import list_host_set_policy_command, Client
return_value = {'data': {'entries': [{'policy_id': 'test', 'persist_id': 'test'}]}}
hostSetId = mocker.patch.object(Client, 'list_host_set_policy_by_hostSetId_request', return_value=return_value)
hostSetPolicy = mocker.patch.object(Client, 'list_host_set_policy_request', return_value=return_value)
mocker.patch('FireEyeHXv2.tableToMarkdown', return_value='test')
list_host_set_policy_command(Client, demisto_args)
assert hostSetId.call_count == call_count['hostSetId']
assert hostSetPolicy.call_count == call_count['hostSetPolicy']
if hostSetPolicy.call_count != 0:
assert hostSetPolicy.call_args.kwargs['offset'] == call_args['offset']
assert hostSetPolicy.call_args.kwargs['limit'] == call_args['limit']
UPSERT_COMMAND_DATA_CASES_DELETE_POLICY = [
(
{'hostSetId': 11, 'policyId': 'test'},
Exception('404'),
'polisy ID - test or Host Set ID - 11 Not Found'
),
(
{'hostSetId': 11, 'policyId': 'test'},
'test',
'Success'
)
]
@pytest.mark.parametrize('demisto_args, exception, expected_results', UPSERT_COMMAND_DATA_CASES_DELETE_POLICY)
def test_delete_host_set_policy_command(mocker, demisto_args, exception, expected_results):
from FireEyeHXv2 import delete_host_set_policy_command, Client
mocker.patch.object(Client, 'delete_host_set_policy_request', side_effect=exception)
result = delete_host_set_policy_command(Client, demisto_args)
assert result.readable_output == expected_results
"""HOSTS"""
UPSERT_COMMAND_DATA_BAD_CASES = [
(
{},
"Please provide either agentId or hostName"
),
(
{'agentId': 'test'},
"agentId test is not correct"
),
(
{'hostName': 'test'},
'test is not found'
)
]
@pytest.mark.parametrize('demisto_args, expected_results', UPSERT_COMMAND_DATA_BAD_CASES)
def test_get_host_information_command_failed(mocker, demisto_args, expected_results):
from FireEyeHXv2 import get_host_information_command, Client
mocker.patch.object(Client, "get_hosts_by_agentId_request", return_value={})
mocker.patch.object(Client, "get_hosts_request", return_value={})
with pytest.raises(Exception) as e:
get_host_information_command(Client, demisto_args)
assert str(e.value) == expected_results
UPSERT_COMMAND_DATA_CASES_HOST_INFORMATION = [
(
# No arguments given
{"agentId": "test"},
{"agentId": 1, "hostName": 0},
isinstance({}, dict)
),
(
# No arguments given
{"hostName": "test"},
{"agentId": 0, "hostName": 1},
isinstance({}, dict)
)
]
@pytest.mark.parametrize('demisto_args, call_count, expected_results', UPSERT_COMMAND_DATA_CASES_HOST_INFORMATION)
def test_host_information(mocker, demisto_args, call_count, expected_results):
from FireEyeHXv2 import get_host_information_command, Client
mocker.patch.object(Client, "get_token_request", return_value="test")
client = Client(
base_url="base_url",
verify=False,
proxy=True,
auth=("userName", "password"))
host_by_agentId = mocker.patch.object(client, "get_hosts_by_agentId_request", return_value={"data": {}})
host_by_hostName = mocker.patch.object(client, "get_hosts_request", return_value={"data": {"entries": [{}]}})
result = get_host_information_command(client, demisto_args)
assert host_by_agentId.call_count == call_count["agentId"]
assert host_by_hostName.call_count == call_count["hostName"]
assert isinstance(result.outputs, dict) == expected_results
UPSERT_COMMAND_DATA_CASES_GET_ALL_HOSTS = [
(
{},
{"hosts_partial": 2},
[{"data": {"entries": [{}]}}, {"data": {"entries": []}}],
[list, dict]
),
(
{},
{"hosts_partial": 1},
[{"data": {"entries": []}}],
[list, 0]
)
]
@pytest.mark.parametrize('demisto_args, call_count, return_value_mock, expected_results', UPSERT_COMMAND_DATA_CASES_GET_ALL_HOSTS)
def test_get_all_hosts_information(mocker, demisto_args, call_count, return_value_mock, expected_results):
from FireEyeHXv2 import get_all_hosts_information_command, Client
mocker.patch.object(Client, "get_token_request", return_value="test")
client = Client(
base_url="base_url",
verify=False,
proxy=True,
auth=("userName", "password"))
hosts_partial = mocker.patch.object(client, "get_hosts_request", side_effect=return_value_mock)
result = get_all_hosts_information_command(client, demisto_args)
assert hosts_partial.call_count == call_count["hosts_partial"]
assert isinstance(result.outputs, expected_results[0])
if result.outputs:
assert isinstance(result.outputs[0], expected_results[1])
else:
assert len(result.outputs) == expected_results[1]
UPSERT_COMMAND_DATA_CASES_GET_HOST_INFORMATION = [
(
{"hostSetID": "test"},
{"data": {}},
list
),
(
{},
{"data": {"entries": []}},
list
)
]
@pytest.mark.parametrize('demisto_args, return_value_mock, expected_results', UPSERT_COMMAND_DATA_CASES_GET_HOST_INFORMATION)
def test_get_host_set_information(mocker, demisto_args, return_value_mock, expected_results):
from FireEyeHXv2 import get_host_set_information_command, Client
mocker.patch.object(Client, "get_token_request", return_value="test")
client = Client(
base_url="base_url",
verify=False,
proxy=True,
auth=("userName", "password"))
mocker.patch.object(client, "get_host_set_information_request", return_value=return_value_mock)
result = get_host_set_information_command(client, demisto_args)
assert isinstance(result.outputs, expected_results)
"""CONTAINMENT"""
UPSERT_COMMAND_DATA_BAD_CASES = [
(
{},
"Please provide either agentId or hostName"
)
]
@pytest.mark.parametrize('demisto_args, expected_results', UPSERT_COMMAND_DATA_BAD_CASES)
def test_host_containment_command_failed(demisto_args, expected_results):
"""
Given:
- agentId or hostName
When:
- Get Host information
Then:
- failing when missing required data
"""
from FireEyeHXv2 import host_containment_command
client = ""
with pytest.raises(Exception) as e:
host_containment_command(client, demisto_args)
assert str(e.value) == expected_results
UPSERT_COMMAND_DATA_BAD_CASES = [
(
{},
"Agent ID is required"
)
]
@pytest.mark.parametrize('demisto_args, expected_results', UPSERT_COMMAND_DATA_BAD_CASES)
def test_approve_containment_command_failed(demisto_args, expected_results):
"""
Given:
- agentId
When:
- approve containment to the request contain
Then:
- failing when missing required data
"""
from FireEyeHXv2 import approve_containment_command
client = ""
with pytest.raises(Exception) as e:
approve_containment_command(client, demisto_args)
assert str(e.value) == expected_results
UPSERT_COMMAND_DATA_BAD_CASES = [
(
# No arguments given
{},
"One of the following arguments is required -> [agentId, hostName]"
)
]
@pytest.mark.parametrize('demisto_args, expected_results', UPSERT_COMMAND_DATA_BAD_CASES)
def test_cancel_containment_command_failed(demisto_args, expected_results):
from FireEyeHXv2 import cancel_containment_command
client = ""
with pytest.raises(Exception) as e:
cancel_containment_command(client, demisto_args)
assert str(e.value) == expected_results
UPSERT_COMMAND_DATA_CASES_HOST_CONTAINMENT = [
(
{"hostName": "test"},
1,
None,
dict
),
(
{"agentId": "test"},
0,
None,
dict
),
(
{"agentId": "test"},
0,
Exception("422"),
"You do not have the required permissions for containment approve\nThe containment request sent, but it is not approve."
),
(
{"agentId": "test"},
0,
Exception("409"),
"This host may already in containment"
)
]
@pytest.mark.parametrize('demisto_args, call_count, return_mocker, expected_results', UPSERT_COMMAND_DATA_CASES_HOST_CONTAINMENT)
def test_host_containment(mocker, demisto_args, call_count, return_mocker, expected_results):
from FireEyeHXv2 import host_containment_command, Client
mocker.patch.object(Client, "get_token_request", return_value="test")
client = Client(
base_url="base_url",
verify=False,
proxy=True,
auth=("userName", "password"))
get_agentId = mocker.patch("FireEyeHXv2.get_agent_id_by_host_name", return_value="")
mocker.patch.object(client, "host_containmet_request", return_value=None)
mocker.patch.object(client, "approve_containment_request", side_effect=return_mocker)
mocker.patch.object(client, "get_hosts_by_agentId_request", return_value={"data": {}})
result = host_containment_command(client, demisto_args)
if not isinstance(return_mocker, Exception):
assert get_agentId.call_count == call_count
assert len(result) == 2
assert isinstance(result[1].outputs, expected_results)
else:
assert result[0].readable_output == expected_results
UPSERT_COMMAND_DATA_CASES_APPROVE_CONTAINMENT = [
(
{"agentId": "test"},
1
)
]
@pytest.mark.parametrize('demisto_args, call_count', UPSERT_COMMAND_DATA_CASES_APPROVE_CONTAINMENT)
def test_approve_containment_command(mocker, demisto_args, call_count):
from FireEyeHXv2 import approve_containment_command, Client
mocker.patch.object(Client, "get_token_request", return_value="test")
client = Client(
base_url="base_url",
verify=False,
proxy=True,
auth=("userName", "password"))
call_request = mocker.patch.object(client, "approve_containment_request", return_value=None)
approve_containment_command(client, demisto_args)
assert call_request.call_count == call_count
UPSERT_COMMAND_DATA_CASES_CANCEL_CONTAINMENT = [
(
{"hostName": "test"},
1
),
(
{"agentId": "test"},
0
)
]
@pytest.mark.parametrize('demisto_args, call_count', UPSERT_COMMAND_DATA_CASES_CANCEL_CONTAINMENT)
def test_cancel_containment_command(mocker, demisto_args, call_count):
from FireEyeHXv2 import cancel_containment_command, Client
mocker.patch.object(Client, "get_token_request", return_value="test")
client = Client(
base_url="base_url",
verify=False,
proxy=True,
auth=("userName", "password"))
get_agentId = mocker.patch("FireEyeHXv2.get_agent_id_by_host_name", return_value="")
mocker.patch.object(client, "cancel_containment_request", return_value=None)
cancel_containment_command(client, demisto_args)
assert get_agentId.call_count == call_count
UPSERT_COMMAND_DATA_CASES_LIST_CONTAINMENT = [
(
{"offset": 2, "limit": 5},
{"offset": 2, "limit": 5}
),
(
{},
{"offset": 0, "limit": 50}
)
]
@pytest.mark.parametrize('demisto_args, args_call', UPSERT_COMMAND_DATA_CASES_LIST_CONTAINMENT)
def test_get_list_containment_command(mocker, demisto_args, args_call):
from FireEyeHXv2 import get_list_containment_command, Client
request = mocker.patch.object(Client, "get_list_containment_request", return_value={"data": {"entries": []}})
mocker.patch("FireEyeHXv2.tableToMarkdown", return_value=" ")
get_list_containment_command(Client, demisto_args)
assert request.call_args.kwargs['offset'] == args_call['offset']
assert request.call_args.kwargs['limit'] == args_call['limit']
"""INDICATORS"""
UPSERT_COMMAND_DATA_BAD_CASES = [
(
# Given only one argument instead of two
{"category": "test"},
"The category and name arguments are required"
),
(
# Given only one argument instead of two
{"name": "test"},
"The category and name arguments are required"
),
(
# No arguments given
{},
"The category and name arguments are required"
)
]
@pytest.mark.parametrize('demisto_args, expected_results', UPSERT_COMMAND_DATA_BAD_CASES)
def test_get_indicator_command_failed(demisto_args, expected_results):
from FireEyeHXv2 import get_indicator_command
client = ""
with pytest.raises(Exception) as e:
get_indicator_command(client, demisto_args)
assert str(e.value) == expected_results
UPSERT_COMMAND_DATA_BAD_CASES = [
(
{"name": "test", "condition": "test"},
"All of the following arguments are required -> ['name','category','condition']"
),
(
{"category": "test", "name": "test"},
"All of the following arguments are required -> ['name','category','condition']"
),
(
{"category": "test", "condition": "test"},
"All of the following arguments are required -> ['name','category','condition']"
)
]
@pytest.mark.parametrize('demisto_args, expected_results', UPSERT_COMMAND_DATA_BAD_CASES)
def test_append_conditions_command_failed(demisto_args, expected_results):
from FireEyeHXv2 import append_conditions_command
client = ""
with pytest.raises(Exception) as e:
append_conditions_command(client, demisto_args)
assert str(e.value) == expected_results
UPSERT_COMMAND_DATA_CASES_ENABLED_CONDITIONS = [
(
"test",
"test",
[{"data": {"entries": ["test", "test"]}}, {"data": {"entries": []}}],
2
),
(
"test",
"test",
[{"data": {"entries": []}}],
0
)
]
@pytest.mark.parametrize('category, name, results_mock, expected_results', UPSERT_COMMAND_DATA_CASES_ENABLED_CONDITIONS)
def test_get_all_enabled_conditions(mocker, category, name, results_mock, expected_results):
from FireEyeHXv2 import get_all_enabled_conditions, Client
mocker.patch.object(Client, "get_indicator_conditions_request", side_effect=results_mock)
result = get_all_enabled_conditions(Client, category, name)
assert len(result) == expected_results
UPSERT_COMMAND_DATA_CASES_GET_INDICATOR = [
(
{'category': 'test', 'name': 'test'}
)
]
@pytest.mark.parametrize('demisto_args', UPSERT_COMMAND_DATA_CASES_GET_INDICATOR)
def test_get_indicator_command(mocker, demisto_args):
from FireEyeHXv2 import get_indicator_command, Client
mocker.patch.object(Client, 'get_indicator_request', return_value='test')
mocker.patch('FireEyeHXv2.tableToMarkdown', return_value='test')
mocker.patch('FireEyeHXv2.get_indicator_conditions', return_value='test')
mocker.patch('FireEyeHXv2.get_indicator_entry', return_value='test')
result = get_indicator_command(Client, demisto_args)
assert isinstance(result, list)
UPSERT_COMMAND_DATA_CASES_GET_ALL_INDICATORS = [
(
2,
[{"data": {"entries": ["test", "test"]}}, {"data": {"entries": []}}],
2
),
(
2,
[{"data": {"entries": []}}],
0
),
(
1,
[{"data": {"entries": ["test", "test"]}}, {"data": {"entries": []}}],
1
)
]
@pytest.mark.parametrize('limit, results_mocker, expected_results', UPSERT_COMMAND_DATA_CASES_GET_ALL_INDICATORS)
def test_get_all_indicators(mocker, limit, results_mocker, expected_results):
from FireEyeHXv2 import get_all_indicators, Client
mocker.patch.object(Client, "get_indicators_request", side_effect=results_mocker)
result = get_all_indicators(Client, limit=limit)
assert len(result) == expected_results
UPSERT_COMMAND_DATA_CASES_GET_INDICATORS = [
(
{'limit': 5, 'alerted': 'yes', 'sort': 'createdBy'}
)
]
@pytest.mark.parametrize('demisto_args', UPSERT_COMMAND_DATA_CASES_GET_INDICATORS)
def test_get_indicators_command(mocker, demisto_args):
from FireEyeHXv2 import get_indicators_command, Client
get_all_indicators = mocker.patch('FireEyeHXv2.get_all_indicators', return_value=[])
mocker.patch('FireEyeHXv2.tableToMarkdown', return_value=' ')
result = get_indicators_command(Client, demisto_args)
assert get_all_indicators.call_args.kwargs['limit'] == 5
assert get_all_indicators.call_args.kwargs['alerted'] is True
assert get_all_indicators.call_args.kwargs['sort'] == 'created_by'
assert isinstance(result, object)
UPSERT_COMMAND_DATA_CASES_GET_INDICATOR_RESULT = [
(
{'event_type': 'fileWriteEvent'},
'File'
),
(
{'event_type': 'ipv4NetworkEvent'},
'Ip'
),
(
{'event_type': 'Unknown'},
None
)
]
@pytest.mark.parametrize('args, expected_results', UPSERT_COMMAND_DATA_CASES_GET_INDICATOR_RESULT)
def test_get_indicator_command_result(mocker, args, expected_results):
from FireEyeHXv2 import get_indicator_command_result
mocker.patch('FireEyeHXv2.general_context_from_event', return_value='test')
mocker.patch('FireEyeHXv2.tableToMarkdown', return_value='test')
result = get_indicator_command_result(args)
assert result.outputs_prefix == expected_results
"""SEARCH"""
UPSERT_COMMAND_DATA_BAD_CASES = [
(
# No arguments given
{},
"Search Id is must be"
)
]
@pytest.mark.parametrize('demisto_args, expected_results', UPSERT_COMMAND_DATA_BAD_CASES)
def test_search_stop_command_failed(demisto_args, expected_results):
from FireEyeHXv2 import search_stop_command
client = ""
with pytest.raises(Exception) as e:
search_stop_command(client, demisto_args)
assert str(e.value) == expected_results
UPSERT_COMMAND_DATA_BAD_CASES = [
(
# No arguments given
{},
"Search Id is must be"
)
]
@pytest.mark.parametrize('demisto_args, expected_results', UPSERT_COMMAND_DATA_BAD_CASES)
def test_search_result_get_command_failed(demisto_args, expected_results):
from FireEyeHXv2 import search_result_get_command
client = ""
with pytest.raises(Exception) as e:
search_result_get_command(client, demisto_args)
assert str(e.value) == expected_results
UPSERT_COMMAND_DATA_BAD_CASES = [
(
# No arguments given
{},
"One of the following arguments is required -> [agentsIds, hostsNames, hostSet, hostSetName]"
),
(
{'hostsNames': 'WIN10X64'},
"One of the following arguments is required -> [dnsHostname, fileFullPath, fileMD5Hash, ipAddress, fieldSearchName]"
)
]
@pytest.mark.parametrize('demisto_args, expected_results', UPSERT_COMMAND_DATA_BAD_CASES)
def test_start_search_command_failed(mocker, demisto_args, expected_results):
from FireEyeHXv2 import start_search_command
client = ""
mocker.patch('FireEyeHXv2.organize_search_body_host', return_value={'hosts': [{'_id': ''}]})
with pytest.raises(Exception) as e:
start_search_command(client, demisto_args)
assert str(e.value) == expected_results
UPSERT_COMMAND_DATA_CASES_START_SEARCH = [
(
{"searchId": 12},
{"data": {"state": "RUNNING", "stats": {"search_state": {"MATCHED": 0, "PENDING": 1}}}},
[False, '12']
),
(
{"searchId": 12},
{"data": {"state": "STOPPED", "stats": {"search_state": {"MATCHED": 0, "PENDING": 1}}}},
[True, '12']
),
(
{"searchId": 12, "stopSearch": "stop"},
{"data": {"state": "RUNNING", "stats": {"search_state": {"MATCHED": 0, "PENDING": 0}}}},
[True, '12']
),
(
{"searchId": 12, "limit": 2},
{"data": {"state": "STOPPED", "stats": {"search_state": {"MATCHED": 2, "PENDING": 1}}}},
[True, '12']
)
]
@pytest.mark.parametrize('args, searchInfo, expected_results', UPSERT_COMMAND_DATA_CASES_START_SEARCH)
def test_start_search_command(mocker, args, searchInfo, expected_results):
from FireEyeHXv2 import start_search_command, Client
mocker.patch.object(Client, "get_search_by_id_request", return_value=searchInfo)
_, result_bool, result_id = start_search_command(Client, args)
assert result_bool == expected_results[0]
assert result_id == expected_results[1]
UPSERT_COMMAND_DATA_CASES_SEARCH_RESULT = [
(
{'searchId': '11,12'},
{'search_result_request': 2, 'tableToMarkdown': 2, 'search_stop': 0, 'search_delete': 0},
list
),
(
{'searchId': '11'},
{'search_result_request': 1, 'tableToMarkdown': 1, 'search_stop': 0, 'search_delete': 0},
list
),
(
{'searchId': '11', 'stopSearch': 'stop'},
{'search_result_request': 1, 'tableToMarkdown': 1, 'search_stop': 1, 'search_delete': 0},
list
),
(
{'searchId': '11', 'stopSearch': 'stopAndDelete'},
{'search_result_request': 1, 'tableToMarkdown': 1, 'search_stop': 0, 'search_delete': 1},
list
)
]
@pytest.mark.parametrize('demisto_args, call_count, expected_results', UPSERT_COMMAND_DATA_CASES_SEARCH_RESULT)
def test_search_result_get_command(mocker, demisto_args, call_count, expected_results):
from FireEyeHXv2 import search_result_get_command, Client
search_result_request = mocker.patch.object(Client, 'search_result_get_request', return_value={'data': {'entries': [{}]}})
search_stop = mocker.patch.object(Client, 'search_stop_request')
search_delete = mocker.patch.object(Client, 'delete_search_request')
tableToMarkdown = mocker.patch('FireEyeHXv2.tableToMarkdown', return_value=' ')
result = search_result_get_command(Client, demisto_args)
assert tableToMarkdown.call_count == call_count['tableToMarkdown']
assert search_result_request.call_count == call_count['search_result_request']
assert isinstance(result, expected_results)
assert search_stop.call_count == call_count['search_stop']
assert search_delete.call_count == call_count['search_delete']
UPSERT_COMMAND_DATA_CASES_SEARCH_LIST = [
(
{'searchId': '11,12'},
{'search_by_id': 2, 'search_request': 0}
),
(
{},
{'search_by_id': 0, 'search_request': 1}
)
]
@pytest.mark.parametrize('demisto_args, call_count', UPSERT_COMMAND_DATA_CASES_SEARCH_LIST)
def test_get_search_list_command(mocker, demisto_args, call_count):
from FireEyeHXv2 import get_search_list_command, Client
search_by_id = mocker.patch.object(Client, 'get_search_by_id_request', return_value={'data': {}})
search_request = mocker.patch.object(Client, 'get_search_list_request', return_value={'data': {'entries': {}}})
mocker.patch('FireEyeHXv2.tableToMarkdown', return_value='test')
result = get_search_list_command(Client, demisto_args)
assert search_by_id.call_count == call_count['search_by_id']
assert search_request.call_count == call_count['search_request']
assert isinstance(result, object)
UPSERT_COMMAND_DATA_CASES_SEARCH_DELETE = [
(
{"searchId": "2,3,4"},
[None, Exception("404"), None],
"Results\nSearch Id 2: Deleted successfully\nSearch Id 3: Not Found\nSearch Id 4: Deleted successfully"
),
(
{"searchId": "2"},
[Exception("400")],
"Results\nSearch Id 2: Failed to delete search"
)
]
@pytest.mark.parametrize('demisto_args, return_mocker, expected_results', UPSERT_COMMAND_DATA_CASES_SEARCH_DELETE)
def test_search_delete_command(mocker, demisto_args, return_mocker, expected_results):
from FireEyeHXv2 import search_delete_command, Client
mocker.patch.object(Client, "delete_search_request", side_effect=return_mocker)
result = search_delete_command(Client, demisto_args)
assert result.readable_output == expected_results
UPSERT_COMMAND_DATA_CASES_SEARCH_STOP = [
(
{"searchId": "2,3,4"},
[{"data": ""}, Exception("404"), {"data": ""}],
"Results\nSearch Id 2: Success\nSearch Id 3: Not Found\nSearch Id 4: Success"
)
]
@pytest.mark.parametrize('demisto_args, return_mocker, expected_results', UPSERT_COMMAND_DATA_CASES_SEARCH_STOP)
def test_search_stop_command(mocker, demisto_args, return_mocker, expected_results):
from FireEyeHXv2 import search_stop_command, Client
mocker.patch.object(Client, "search_stop_request", side_effect=return_mocker)
result = search_stop_command(Client, demisto_args)
assert result.readable_output == expected_results
"""ACQUISITIONS"""