-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathPassiveTotal_v2_test.py
2419 lines (1848 loc) · 85.3 KB
/
PassiveTotal_v2_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 unittest import mock
from unittest.mock import patch
import pytest
from requests.exceptions import MissingSchema, InvalidSchema, ConnectionError
import demistomock as demisto
from CommonServerPython import DemistoException
from test_data import input_data
MOCK_URL = 'http://123-fake-api.com' # NOSONAR
SSL_ARGS = {
'field': 'serialNumber',
'query': 'dummy serial number'
}
PDNS_ARGS = {
'query': 'dummy domain',
'start': '2020-01-01 00:00:00',
'end': '2020-01-31'
}
HOST_ATTRIBUTE_ARGS = {
'component_by_domain': {
'query': 'dummy domain',
'start': '2020-05-25 00:05:25'
},
'component_by_ip': {
'query': 'dummy ip',
'end': '2020-05-25 00:05:25'
},
'tracker_by_domain': {
'query': 'dummy domain',
'start': '2020-05-25 00:05:25'
},
'tracker_by_ip': {
'query': 'dummy ip',
'end': '2020-05-25 00:05:25'
},
'host_pair': {
'query': 'dummy child',
'direction': 'parents',
'start': '2020-05-25 00:05:25'
}
}
COOKIE_ARGS = {
"query": "dummy.domain",
"search_by": "get addresses by cookie domain",
"order": "desc",
"sort": "last seen",
"page": '0'
}
@pytest.fixture()
def client():
from PassiveTotal_v2 import Client
return Client(MOCK_URL, '10', False, False, ('USERNAME', 'API_SECRET'))
def mock_http_response(status=200, json_data=None, raise_for_status=None):
mock_resp = mock.Mock()
# mock raise_for_status call w/optional error
mock_resp.raise_for_status = mock.Mock()
if raise_for_status:
mock_resp.raise_for_status.side_effect = raise_for_status
# set status code
mock_resp.status_code = status
# add json data if provided
if json_data:
mock_resp.json = mock.Mock(
return_value=json_data
)
return mock_resp
@patch('PassiveTotal_v2.Client._http_request')
def test_http_request_authentication_error(mock_base_http_request, client):
"""
When http request return status code 401 then appropriate error message should display.
"""
# Configure
mock_base_http_request.return_value = mock_http_response(status=401)
# Execute
with pytest.raises(ValueError) as e:
client.http_request('GET', '/test/url/suffix')
# Assert
assert 'Unauthenticated. Check the configured Username and API secret.' == str(e.value)
@patch('PassiveTotal_v2.Client._http_request')
def test_http_request_page_not_found_error(mock_base_http_request, client):
"""
When http request return status code 404 then appropriate error message should display.
"""
# Configure
mock_base_http_request.return_value = mock_http_response(status=404)
# Execute
with pytest.raises(ValueError) as e:
client.http_request('GET', '/test/url/suffix')
# Assert
assert 'No record(s) found.' == str(e.value)
@patch('PassiveTotal_v2.Client._http_request')
def test_http_request_proxy_error_based_on_status(mock_base_http_request, client):
"""
When http request return status code 407 then appropriate error message should display.
"""
# Configure
mock_base_http_request.return_value = mock_http_response(status=407)
# Execute
with pytest.raises(ValueError) as e:
client.http_request('GET', '/test/url/suffix')
# Assert
assert 'Proxy Error - cannot connect to proxy. Either try clearing the \'Use system proxy\' check-box or check ' \
'the host, authentication details and connection details for the proxy.' == str(e.value)
@patch('PassiveTotal_v2.Client._http_request')
def test_http_request_internal_server_error(mock_base_http_request, client):
"""
When http request return status code 500 then appropriate error message should display.
"""
# Configure
mock_base_http_request.return_value = mock_http_response(status=500)
# Execute
with pytest.raises(ValueError) as e:
client.http_request('GET', '/test/url/suffix')
# Assert
assert 'The server encountered an internal error for PassiveTotal and was unable to complete your request.' == str(
e.value)
@patch('PassiveTotal_v2.Client._http_request')
def test_http_request_raise_for_status(mock_base_http_request, client):
"""
When http request return invalid status code then appropriate error message should display.
"""
# Configure
mock_raise_for_status = mock.Mock()
mock_raise_for_status.return_value = None
mock_response = mock_http_response(status=300, raise_for_status=mock_raise_for_status)
mock_base_http_request.return_value = mock_response
# Execute
client.http_request('GET', '/test/url/suffix')
# Assert
assert mock_raise_for_status.called
def test_main_success(mocker, client):
"""
When main function called test function should call.
"""
import PassiveTotal_v2
mocker.patch.object(demisto, 'command', return_value='test-module')
mocker.patch.object(PassiveTotal_v2, 'test_function', return_value='ok')
PassiveTotal_v2.main()
assert PassiveTotal_v2.test_function.called
@patch('PassiveTotal_v2.return_error')
def test_main_failure(mock_return_error, capfd, mocker):
"""
When main function get some exception then valid message should be print.
"""
import PassiveTotal_v2
mocker.patch.object(demisto, 'command', return_value='test-module')
mocker.patch.object(PassiveTotal_v2, 'test_function', side_effect=Exception)
with capfd.disabled():
PassiveTotal_v2.main()
mock_return_error.assert_called_once_with('Error: ')
@patch('PassiveTotal_v2.Client.http_request')
def test_function_success(request_mocker, client):
"""
When success response come then test_function command should pass.
"""
from PassiveTotal_v2 import test_function
mock_response = {
'results': 0,
'domains': []
}
request_mocker.return_value = mock_response
assert test_function(client) == 'ok'
def test_request_timeout_success(mocker):
"""
When provided valid request timeout then test should be passed.
"""
from PassiveTotal_v2 import get_request_timeout
request_timeout = 5
params = {
'request_timeout': str(request_timeout)
}
mocker.patch.object(demisto, 'params', return_value=params)
get_request_timeout()
assert int(request_timeout) == request_timeout
def test_request_timeout_invalid_value(mocker):
"""
When provided invalid request timeout then display error message.
"""
from PassiveTotal_v2 import get_request_timeout
# Configure
request_timeout = 'invalid_str_value'
params = {
'request_timeout': str(request_timeout)
}
mocker.patch.object(demisto, 'params', return_value=params)
# Execute
with pytest.raises(ValueError) as e:
get_request_timeout()
# Assert
assert 'HTTP(S) Request timeout parameter must be a positive integer.' == str(e.value)
def test_request_timeout_failure(mocker):
"""
When invalid input provided for request timeout then appropriate error message should display.
"""
from PassiveTotal_v2 import get_request_timeout
request_timeout = -5
params = {
'request_timeout': str(request_timeout)
}
mocker.patch.object(demisto, 'params', return_value=params)
# Execute
with pytest.raises(ValueError) as e:
get_request_timeout()
assert 'HTTP(S) Request timeout parameter must be a positive integer.' == str(e.value)
def test_request_timeout_large_value_failure(mocker):
"""
When too large value provided for request timeout then raised value error and
appropriate error message should display.
"""
from PassiveTotal_v2 import get_request_timeout
request_timeout = 990000000000000000
params = {
'request_timeout': str(request_timeout)
}
mocker.patch.object(demisto, 'params', return_value=params)
# Execute
with pytest.raises(ValueError) as e:
get_request_timeout()
assert 'Value is too large for HTTP(S) Request Timeout.' == str(e.value)
def test_get_components_command_main_success(mocker, client):
"""
When "pt-get-components" command executes the get_components_command function should be called from main.
"""
import PassiveTotal_v2
mocker.patch.object(demisto, 'command', return_value='pt-get-components')
mocker.patch.object(PassiveTotal_v2, 'get_components_command',
return_value='No component(s) were found for the given argument(s).')
PassiveTotal_v2.main()
assert PassiveTotal_v2.get_components_command.called
@patch('PassiveTotal_v2.Client.http_request')
def test_get_components_command_domain_success(mocker_http_request, client):
"""
When "pt-get-components" command executes successfully for domain then context output and response should match.
"""
from PassiveTotal_v2 import get_components_command
# Fetch the expected response from file
with open('test_data/HostAttribute/Component/component_resp.json', encoding='utf-8') as f:
json_file = json.load(f)
expected_res = json_file.get('successDomain')
mocker_http_request.return_value = expected_res
# Fetch the expected custom entry context from file
with open('test_data/HostAttribute/Component/component_ec.json', encoding='utf-8') as f:
json_file = json.load(f)
expected_custom_ec = json_file.get('successDomain')
# Fetch the expected human readable details from file
with open('test_data/HostAttribute/Component/component_domain_hr.md') as f:
expected_hr = f.read()
result = get_components_command(client, HOST_ATTRIBUTE_ARGS['component_by_domain'])
assert result[0].raw_response == expected_res
assert result[0].outputs == expected_custom_ec
assert result[0].readable_output == expected_hr
assert result[0].outputs_key_field == ''
assert result[0].outputs_prefix == 'PassiveTotal.Component'
@patch('PassiveTotal_v2.Client.http_request')
def test_get_components_command_ip_success(mocker_http_request, client):
"""
When "pt-get-components" command executes successfully for ip then context output and response should match.
"""
from PassiveTotal_v2 import get_components_command
# Fetch the expected response from file
with open('test_data/HostAttribute/Component/component_resp.json', encoding='utf-8') as f:
json_file = json.load(f)
expected_res = json_file.get('successIP')
mocker_http_request.return_value = expected_res
# Fetch the expected custom entry context from file
with open('test_data/HostAttribute/Component/component_ec.json', encoding='utf-8') as f:
json_file = json.load(f)
expected_custom_ec = json_file.get('successIP')
# Fetch the expected human readable details from file
with open('test_data/HostAttribute/Component/component_ip_hr.md') as f:
expected_hr = f.read()
result = get_components_command(client, HOST_ATTRIBUTE_ARGS['component_by_ip'])
assert result[0].raw_response == expected_res
assert result[0].outputs == expected_custom_ec
assert result[0].readable_output == expected_hr
assert result[0].outputs_key_field == ''
assert result[0].outputs_prefix == 'PassiveTotal.Component'
@patch('PassiveTotal_v2.Client.http_request')
def test_get_components_command_no_record_found(mocker_http_request, client):
"""
When no records found from Components response then result string should match.
"""
from PassiveTotal_v2 import get_components_command
# Fetch the expected response from file
with open('test_data/HostAttribute/Component/component_resp.json', encoding='utf-8') as f:
json_file = json.load(f)
expected_res = json_file.get('emptyContent')
mocker_http_request.return_value = expected_res
result = get_components_command(client, HOST_ATTRIBUTE_ARGS['component_by_domain'])
assert result == 'No component(s) were found for the given argument(s).'
def test_get_trackers_command_main_success(mocker, client):
"""
When "pt-get-trackers" command executes the get_trackers_command function should be called from main.
"""
import PassiveTotal_v2
mocker.patch.object(demisto, 'command', return_value='pt-get-trackers')
mocker.patch.object(PassiveTotal_v2, 'get_trackers_command',
return_value='No tracker(s) were found for the given argument(s).')
PassiveTotal_v2.main()
assert PassiveTotal_v2.get_trackers_command.called
@patch('PassiveTotal_v2.Client.http_request')
def test_get_trackers_command_domain_success(mocker_http_request, client):
"""
When "pt-get-trackers" command executes successfully for domain then context output and response should match.
"""
from PassiveTotal_v2 import get_trackers_command
# Fetch the expected response from file
with open('test_data/HostAttribute/Tracker/tracker_resp.json', encoding='utf-8') as f:
json_file = json.load(f)
expected_res = json_file.get('successDomain')
mocker_http_request.return_value = expected_res
# Fetch the expected custom entry context from file
with open('test_data/HostAttribute/Tracker/tracker_ec.json', encoding='utf-8') as f:
json_file = json.load(f)
expected_custom_ec = json_file.get('successDomain')
# Fetch the expected human readable details from file
with open('test_data/HostAttribute/Tracker/tracker_domain_hr.md') as f:
expected_hr = f.read()
result = get_trackers_command(client, HOST_ATTRIBUTE_ARGS['tracker_by_domain'])
assert result[0].raw_response == expected_res
assert result[0].outputs == expected_custom_ec
assert result[0].readable_output == expected_hr
assert result[0].outputs_key_field == ''
assert result[0].outputs_prefix == 'PassiveTotal.Tracker'
@patch('PassiveTotal_v2.Client.http_request')
def test_get_trackers_command_ip_success(mocker_http_request, client):
"""
When "pt-get-trackers" command executes successfully for ip then context output and response should match.
"""
from PassiveTotal_v2 import get_trackers_command
# Fetch the expected response from file
with open('test_data/HostAttribute/Tracker/tracker_resp.json', encoding='utf-8') as f:
json_file = json.load(f)
expected_res = json_file.get('successIP')
mocker_http_request.return_value = expected_res
# Fetch the expected custom entry context from file
with open('test_data/HostAttribute/Tracker/tracker_ec.json', encoding='utf-8') as f:
json_file = json.load(f)
expected_custom_ec = json_file.get('successIP')
# Fetch the expected human readable details from file
with open('test_data/HostAttribute/Tracker/tracker_ip_hr.md') as f:
expected_hr = f.read()
result = get_trackers_command(client, HOST_ATTRIBUTE_ARGS['tracker_by_ip'])
assert result[0].raw_response == expected_res
assert result[0].outputs == expected_custom_ec
assert result[0].readable_output == expected_hr
assert result[0].outputs_key_field == ''
assert result[0].outputs_prefix == 'PassiveTotal.Tracker'
@patch('PassiveTotal_v2.Client.http_request')
def test_get_trackers_command_no_record_found(mocker_http_request, client):
"""
When no records found from Trackers response then result string should match.
"""
from PassiveTotal_v2 import get_trackers_command
# Fetch the expected response from file
with open('test_data/HostAttribute/Tracker/tracker_resp.json', encoding='utf-8') as f:
json_file = json.load(f)
expected_res = json_file.get('emptyContent')
mocker_http_request.return_value = expected_res
result = get_trackers_command(client, HOST_ATTRIBUTE_ARGS['tracker_by_domain'])
assert result == 'No tracker(s) were found for the given argument(s).'
def test_get_host_pairs_command_main_success(mocker, client):
"""
When "pt-get-host-pairs" command executes the get_host_pairs_command function should be called from main.
"""
import PassiveTotal_v2
mocker.patch.object(demisto, 'command', return_value='pt-get-host-pairs')
mocker.patch.object(PassiveTotal_v2, 'get_host_pairs_command',
return_value='No host pair(s) were found for the given argument(s).')
PassiveTotal_v2.main()
assert PassiveTotal_v2.get_host_pairs_command.called
@patch('PassiveTotal_v2.Client.http_request')
def test_get_host_pairs_command_success(mocker_http_request, client):
"""
When "pt-get-host-pairs" command executes successfully then context output and response should match.
"""
from PassiveTotal_v2 import get_host_pairs_command
# Fetch the expected response from file
with open('test_data/HostAttribute/Host_Pair/host_pair_resp.json', encoding='utf-8') as f:
json_file = json.load(f)
expected_res = json_file.get('success')
mocker_http_request.return_value = expected_res
# Fetch the expected custom entry context from file
with open('test_data/HostAttribute/Host_Pair/host_pair_ec.json', encoding='utf-8') as f:
json_file = json.load(f)
expected_custom_ec = json_file.get('success')
# Fetch the expected human readable details from file
with open('test_data/HostAttribute/Host_Pair/host_pair_hr.md') as f:
expected_hr = f.read()
result = get_host_pairs_command(client, HOST_ATTRIBUTE_ARGS['host_pair'])
assert result.raw_response == expected_res
assert result.outputs == expected_custom_ec
assert result.readable_output == expected_hr
assert result.outputs_key_field == ''
assert result.outputs_prefix == 'PassiveTotal.HostPair'
@patch('PassiveTotal_v2.Client.http_request')
def test_get_host_pairs_command_no_record_found(mocker_http_request, client):
"""
When no records found from Host Pairs response then result string should match.
"""
from PassiveTotal_v2 import get_host_pairs_command
# Fetch the expected response from file
with open('test_data/HostAttribute/Host_Pair/host_pair_resp.json', encoding='utf-8') as f:
json_file = json.load(f)
expected_res = json_file.get('emptyContent')
mocker_http_request.return_value = expected_res
result = get_host_pairs_command(client, HOST_ATTRIBUTE_ARGS['host_pair'])
assert result == 'No host pair(s) were found for the given argument(s).'
@patch('PassiveTotal_v2.Client.http_request')
def test_get_host_pairs_command_invalid_value_for_direction(client):
"""
When invalid value is provided for direction argument in 'pt-get-host-pairs' then error message should match.
"""
from PassiveTotal_v2 import get_host_pairs_command
# Configure
args = {
'query': 'dummy domain',
'direction': 'invalid direction'
}
# Execute
with pytest.raises(ValueError) as e:
get_host_pairs_command(client, args)
# Assert
assert 'The given value for direction is invalid. Supported values: children, parents.' == str(e.value)
def test_get_common_arguments_invalid_value_for_query():
"""
When invalid value is provided for query argument then error message should match.
"""
from PassiveTotal_v2 import get_common_arguments
# Configure
args = {
'query': '',
'start': '2016-02-02 02:32:44'
}
# Execute
with pytest.raises(ValueError) as e:
get_common_arguments(args)
# Assert
assert 'The given value for query is invalid.' == str(e.value)
def test_get_valid_whois_search_arguments_empty_value_in_query():
"""
When empty value enter for command argument then should raise error with proper message
"""
from PassiveTotal_v2 import get_valid_whois_search_arguments
# Configure
args = {
'query': '',
'field': 'email'
}
# Execute
with pytest.raises(ValueError) as e:
get_valid_whois_search_arguments(args)
# Assert
assert 'query or field argument should not be empty.' == str(e.value)
def test_get_valid_whois_search_invalid_value_for_field_arguemnts():
"""
When invalid value for command argument field then should raise error with proper message
"""
from PassiveTotal_v2 import get_valid_whois_search_arguments
# Configure
args = {
'query': '[email protected]',
'field': 'field'
}
# Execute
with pytest.raises(ValueError) as e:
get_valid_whois_search_arguments(args)
# Assert
assert 'Invalid field type field. Valid field types are domain, email, name, organization, address, phone, ' \
'nameserver.' == str(e.value)
@patch('PassiveTotal_v2.CommandResults')
@patch('PassiveTotal_v2.Client.http_request')
def test_pt_whois_search_command_success(request_mocker, mock_cr, client):
"""
Proper Readable output and context should be set via CommonResults in case of proper response from whois-search
API endpoint
"""
from PassiveTotal_v2 import pt_whois_search_command
from PassiveTotal_v2 import get_human_readable_for_whois_commands
from PassiveTotal_v2 import get_context_for_whois_commands
# Configure
args = {
'query': '[email protected]',
'field': 'email'
}
with open('test_data/whois_command/whois_command_response.json', 'rb') as f:
dummy_response = json.load(f)
with open('test_data/whois_command/whois_custom_context.json', 'rb') as f:
dummy_custom_context = json.load(f)
with open('test_data/whois_command/whois_command_readable_output.md', 'r') as f:
dummy_readable_output = f.read()
with open('test_data/whois_command/whois_command_standard_domain_readable_output.md', 'r') as f:
dummy_standard_domain_readable_output = f.read()
request_mocker.return_value = dummy_response
# Execute
domains = dummy_response.get('results')
# get human readable via dummy response
readable_output = get_human_readable_for_whois_commands(domains)
# get custom context via dummy response
custom_context = get_context_for_whois_commands(domains)[1]
pt_whois_search_command(client, args)
# Assert
# asserts the readable output
assert readable_output == dummy_readable_output
# asserts the custom context
assert custom_context == dummy_custom_context
# assert the standard domain readable output
assert dummy_standard_domain_readable_output == mock_cr.call_args_list[0][1]['readable_output']
# assert overall command output
mock_cr.assert_called_with(
outputs_prefix='PassiveTotal.WHOIS',
outputs_key_field=['domain', 'lastLoadedAt'],
outputs=dummy_custom_context,
readable_output=dummy_readable_output,
raw_response=dummy_response
)
@patch('PassiveTotal_v2.Client.http_request')
def test_pt_whois_search_empty_response(request_mocker, client):
"""
Proper message should be display in case of empty response from whois-search API endpoint
"""
from PassiveTotal_v2 import pt_whois_search_command
# Configure
args = {
'query': '[email protected]',
'field': 'email'
}
empty_response = '{"results": []}'
dummy_response = json.loads(empty_response)
request_mocker.return_value = dummy_response
# Execute
message = pt_whois_search_command(client, args)
# Assert
assert message == 'No domain information were found for the given argument(s).'
@patch("PassiveTotal_v2.Client.http_request")
def test_ssl_cert_search_command_success(mocker_http_request, client):
"""
When "ssl-cert-search" command executes successfully then context output and response should match.
"""
from PassiveTotal_v2 import ssl_cert_search_command
# Fetching expected raw response from file
with open('test_data/SSL/ssl_cert_resp.json', encoding='utf-8') as f:
json_file = json.load(f)
expected_res = json_file.get('success')
mocker_http_request.return_value = expected_res
# Fetching expected entry context details from file
with open("test_data/SSL/ssl_cert_ec.json", encoding='utf-8') as f:
expected_ec = json.load(f)
# Fetching expected entry context details from file
with open("test_data/SSL/ssl_cert_hr.md") as f:
expected_hr = f.read()
result = ssl_cert_search_command(client, SSL_ARGS)
assert result.raw_response == expected_res
assert result.outputs == expected_ec
assert result.readable_output == expected_hr
assert result.outputs_key_field == 'sha1'
assert result.outputs_prefix == 'PassiveTotal.SSL'
@patch("PassiveTotal_v2.Client.http_request")
def test_ssl_cert_search_no_record_found(mocker_http_request, client):
"""
When no record found from SSL response then result string should match.
"""
from PassiveTotal_v2 import ssl_cert_search_command
# Fetching expected raw response from file
with open("test_data/SSL/ssl_cert_resp.json", encoding='utf-8') as f:
json_file = json.load(f)
expected_res = json_file.get('zeroRecords')
mocker_http_request.return_value = expected_res
result = ssl_cert_search_command(client, SSL_ARGS)
assert result == 'No SSL certificate(s) were found for the given argument(s).'
@patch("PassiveTotal_v2.Client.http_request")
def test_get_pdns_details_command_success(mocker_http_request, client):
"""
When "get-pdns-details" command executes successfully then context output and response should match.
"""
from PassiveTotal_v2 import get_pdns_details_command
# Fetching expected raw response from file
with open('test_data/PDNS/get_pdns_resp.json', encoding='utf-8') as f:
json_file = json.load(f)
expected_res = json_file.get('success')
mocker_http_request.return_value = expected_res
# Fetching expected entry context details from file
with open("test_data/PDNS/get_pdns_ec.json", encoding='utf-8') as f:
expected_ec = json.load(f)
# Fetching expected entry context details from file
with open("test_data/PDNS/get_pdns_hr.md") as f:
expected_hr = f.read()
result = get_pdns_details_command(client, PDNS_ARGS)
assert result[0].raw_response == expected_res
assert result[0].outputs == expected_ec
assert result[0].readable_output == expected_hr
assert result[0].outputs_prefix == 'PassiveTotal.PDNS(val.resolve == obj.resolve && val.recordType == ' \
'obj.recordType' \
' && val.resolveType == obj.resolveType)'
@patch("PassiveTotal_v2.Client.http_request")
def test_get_pdns_details_no_record_found(mocker_http_request, client):
"""
When no record found from PDNS response then result string should match.
"""
from PassiveTotal_v2 import get_pdns_details_command
# Fetching expected raw response from file
with open("test_data/PDNS/get_pdns_resp.json", encoding='utf-8') as f:
json_file = json.load(f)
expected_res = json_file.get('zeroRecords')
mocker_http_request.return_value = expected_res
result = get_pdns_details_command(client, SSL_ARGS)
assert result == 'No PDNS Record(s) were found for the given argument(s).'
@patch('PassiveTotal_v2.Client._http_request')
def test_http_request_missing_schema_error(mock_base_http_request, client):
"""
When http request return MissingSchema exception then appropriate error message should display.
"""
# Configure
mock_base_http_request.side_effect = MissingSchema
# Execute
with pytest.raises(ValueError) as e:
client.http_request('GET', '/test/url/suffix')
# Assert
assert 'Invalid API URL. No schema supplied: http(s).' == str(e.value)
@patch('PassiveTotal_v2.Client._http_request')
def test_http_request_invalid_schema_error(mock_base_http_request, client):
"""
When http request return invalid schema exception then appropriate error message should match.
"""
# Configure
mock_base_http_request.side_effect = InvalidSchema
# Execute
with pytest.raises(ValueError) as e:
client.http_request('GET', '/test/url/suffix')
# Assert
assert 'Invalid API URL. Supplied schema is invalid, supports http(s).' == str(e.value)
@patch('PassiveTotal_v2.Client._http_request')
def test_http_request_proxy_error(mock_base_http_request, client):
"""
When http request return proxy error with exception then appropriate error message should match.
"""
# Configure
mock_base_http_request.side_effect = DemistoException('Proxy Error')
# Execute
with pytest.raises(ConnectionError) as e:
client.http_request('GET', '/test/url/suffix')
# Assert
assert 'Proxy Error - cannot connect to proxy. Either try clearing the \'Use system proxy\' check-box or' \
' check the host, authentication details and connection details for the proxy.' == str(e.value)
@patch('PassiveTotal_v2.Client._http_request')
def test_http_request_connection_error(mock_base_http_request, client):
"""
When http request return connection error with Demisto exception then appropriate error message should match.
"""
# Configure
mock_base_http_request.side_effect = DemistoException('ConnectionError')
# Execute
with pytest.raises(ConnectionError) as e:
client.http_request('GET', '/test/url/suffix')
# Assert
assert 'Connectivity failed. Check your internet connection, the API URL or try increasing the HTTP(s) Request' \
' Timeout.' == str(e.value)
@patch('PassiveTotal_v2.Client._http_request')
def test_http_request_connect_timeout_error(mock_base_http_request, client):
"""
When http request return connect timeout error with Demisto exception then appropriate error message
should match.
"""
# Configure
mock_base_http_request.side_effect = DemistoException('ConnectTimeout')
# Execute
with pytest.raises(ConnectionError) as e:
client.http_request('GET', '/test/url/suffix')
# Assert
assert 'Connectivity failed. Check your internet connection, the API URL or try increasing the HTTP(s) Request' \
' Timeout.' == str(e.value)
@patch('PassiveTotal_v2.Client._http_request')
def test_http_request_other_demisto_exception(mock_base_http_request, client):
"""
When http request return other custom Demisto exception then appropriate error message should match.
"""
# Configure
mock_base_http_request.side_effect = DemistoException('custom')
# Execute
with pytest.raises(Exception) as e:
client.http_request('GET', '/test/url/suffix')
# Assert
assert 'custom' == str(e.value)
def test_init():
"""
test init function
"""
import PassiveTotal_v2
with mock.patch.object(PassiveTotal_v2, "main", return_value=42):
with mock.patch.object(PassiveTotal_v2, "__name__", "__main__"):
PassiveTotal_v2.init()
def test_domain_reputation_command_empty_domain_arguments_values(client):
"""
When multiple empty value enter for command argument then should raise error with proper message
"""
from PassiveTotal_v2 import domain_reputation_command
# Configure
args = {
'domain': ',,'
}
# Execute
with pytest.raises(ValueError) as e:
domain_reputation_command(client, args)
# Assert
assert 'domain argument should not be empty.' == str(e.value)
def test_domain_reputation_command_not_specify_domain_arguments_values(client):
"""
When no value enter for command argument then should raise error with proper message
"""
from PassiveTotal_v2 import domain_reputation_command
# Configure
args = {
'domain': ''
}
# Execute
with pytest.raises(ValueError) as e:
domain_reputation_command(client, args)
# Assert
assert 'domain(s) not specified' == str(e.value)
@patch('PassiveTotal_v2.Client.http_request')
def test_domain_reputation_command_success(mocker_http_request, client):
"""
Proper Readable output and context should be set via CommonResults in case of proper response from whois-search
and reputation API endpoint
"""
from PassiveTotal_v2 import domain_reputation_command
from PassiveTotal_v2 import get_context_for_whois_commands
# Configure
args = {
'domain': 'somedomain.com'
}
with open('test_data/domain_reputation/domain_reputation_response.json', 'rb') as f:
dummy_response = json.load(f)
with open('test_data/domain_reputation/domain_reputation_context.json', 'rb') as f:
dummy_custom_context = json.load(f)
with open('test_data/domain_reputation/domain_reputation_command_readable_output.md', 'r') as f:
dummy_readable_output = f.read()
with open('test_data/domain_reputation/domain_reputation_command_reputation_response.json', 'rb') as f:
dummy_custom_reputation_context = json.load(f)
mocker_http_request.return_value = dummy_response
_, custom_context = get_context_for_whois_commands(dummy_response.get('results'))
custom_context.append(dummy_custom_reputation_context)
final_context = [{k: v for x in custom_context for k, v in x.items()}]
assert final_context == dummy_custom_context
command_response = domain_reputation_command(client, args)
assert command_response[0].outputs_prefix == 'PassiveTotal.Domain'
assert command_response[0].outputs_key_field == 'domain'
assert command_response[0].readable_output == dummy_readable_output
@patch('PassiveTotal_v2.CommandResults')
@patch('PassiveTotal_v2.Client.http_request')
def test_domain_reputation_command_empty_response(request_mocker, mock_cr, client):
"""
Proper message should be display in case of empty response from whois-search and reputation API endpoint
"""
from PassiveTotal_v2 import domain_reputation_command
# Configure
args = {
'domain': 'somedomain.com'
}
empty_response = '{"results": []}'
dummy_response = json.loads(empty_response)
request_mocker.return_value = dummy_response
# Execute
domain_reputation_command(client, args)
# Assert
mock_cr.assert_called_with(
outputs_prefix='PassiveTotal.Domain',
outputs_key_field='domain',
outputs=[],
readable_output="### Domain(s)\n**No entries.**\nThe reputation score for 'somedomain.com' is "
"and is classified as ''.\n### Reputation Rules\n**No entries.**\n"
)
@patch('PassiveTotal_v2.Client.http_request')
def test_get_services_command_empty_response(request_mocker, client):
"""
Empty response body should not throw any error if API returns.
"""
from PassiveTotal_v2 import get_services_command
# Configure
args = {
'ip': '127.0.0.1'