def test_get_outbound_ioc_values_3(self, mocker): """Test update by not on_demand with refresh""" import CommonServerPython as CSP mocker.patch.object(CSP, 'parse_date_range', return_value=(1578383898, 1578383898)) import ExportIndicators as ei with open( 'ExportIndicators_test/TestHelperFunctions/iocs_cache_values_text.json', 'r') as iocs_text_values_f: iocs_text_dict = json.loads(iocs_text_values_f.read()) mocker.patch.object(demisto, 'getIntegrationContext', return_value={"last_output": iocs_text_dict}) mocker.patch.object(ei, 'refresh_outbound_context', return_value=iocs_text_dict) mocker.patch.object(demisto, 'getLastRun', return_value={'last_run': 1578383898000}) request_args = ei.RequestArguments(query='', out_format='text', limit=50, offset=0) ioc_list = ei.get_outbound_ioc_values( on_demand=False, request_args=request_args, cache_refresh_rate='1 minute') for ioc_row in ioc_list: assert ioc_row in iocs_text_dict
def test_refresh_outbound_context_4(self, mocker): """Test out_format=XSOAR json-seq""" import ExportIndicators as ei with open('ExportIndicators_test/TestHelperFunctions/demisto_iocs.json', 'r') as iocs_json_f: iocs_json = json.loads(iocs_json_f.read()) mocker.patch.object(ei, 'find_indicators_with_limit', return_value=iocs_json) request_args = ei.RequestArguments(query='', out_format='XSOAR json-seq', limit=38) ei_vals = ei.refresh_outbound_context(request_args) with open('ExportIndicators_test/TestHelperFunctions/iocs_out_json_seq.txt', 'r') as iocs_out_f: iocs_out = iocs_out_f.read() assert iocs_out == ei_vals
def test_refresh_outbound_context_1(self, mocker): """Test out_format=text""" import ExportIndicators as ei with open('ExportIndicators_test/TestHelperFunctions/demisto_iocs.json', 'r') as iocs_json_f: iocs_json = json.loads(iocs_json_f.read()) mocker.patch.object(ei, 'find_indicators_with_limit', return_value=iocs_json) request_args = ei.RequestArguments(query='', out_format='text', limit=38) ei_vals = ei.refresh_outbound_context(request_args) for ioc in iocs_json: ip = ioc.get('value') assert ip in ei_vals
def test_refresh_outbound_context_2(self, mocker): """Test out_format= XSOAR json""" import ExportIndicators as ei with open('ExportIndicators_test/TestHelperFunctions/demisto_iocs.json', 'r') as iocs_json_f: iocs_json = json.loads(iocs_json_f.read()) iocs_json_result = {'iocs': iocs_json, 'total': 100} mocker.patch.object(demisto, 'searchIndicators', return_value=iocs_json_result) request_args = ei.RequestArguments(query='', out_format='XSOAR json', limit=39) ei_vals = ei.refresh_outbound_context(request_args) assert isinstance(ei_vals, str) ei_vals = json.loads(ei_vals) assert iocs_json == ei_vals
def test_find_indicators_with_limit_1(self, mocker): """Test find indicators limit""" import ExportIndicators as ei with open('ExportIndicators_test/TestHelperFunctions/demisto_iocs.json', 'r') as iocs_json_f: iocs_json = json.loads(iocs_json_f.read()) limit = 30 indicator_searcher_res = [{'iocs': iocs_json[:limit]}, {'iocs': []}] indicator_searcher = ei.IndicatorsSearcher( limit=limit ) mocker.patch.object(indicator_searcher, 'search_indicators_by_version', side_effect=indicator_searcher_res) ei_vals = ei.find_indicators_with_limit(indicator_searcher) assert len(ei_vals) == limit
def test_refresh_outbound_context_6(self, mocker): """Test out_format=json-seq""" import ExportIndicators as ei with open('ExportIndicators_test/TestHelperFunctions/demisto_iocs.json', 'r') as iocs_json_f: iocs_json = json.loads(iocs_json_f.read()) iocs_json_result = {'iocs': iocs_json, 'total': 100} mocker.patch.object(demisto, 'searchIndicators', return_value=iocs_json_result) request_args = ei.RequestArguments(query='', out_format='json-seq', limit=38) ei_vals = ei.refresh_outbound_context(request_args) with open('ExportIndicators_test/TestHelperFunctions/iocs_out_json_seq_old.txt', 'r') as iocs_out_f: iocs_out = iocs_out_f.read() for iocs_out_line in iocs_out.split('\n'): assert iocs_out_line in ei_vals
def test_get_outbound_ioc_values_4(self, mocker): """Test update by request params change - limit""" import CommonServerPython as CSP mocker.patch.object(CSP, 'parse_date_range', return_value=(1578383898, 1578383898)) import ExportIndicators as ei with open( 'ExportIndicators_test/TestHelperFunctions/iocs_cache_values_text.json', 'r') as iocs_text_values_f: iocs_text_dict = json.loads(iocs_text_values_f.read()) mocker.patch.object(demisto, 'getIntegrationContext', return_value={ "last_output": iocs_text_dict, "last_limit": 1, "last_offset": 0, "last_query": "type:ip", "last_format": "text" }) mocker.patch.object(ei, 'refresh_outbound_context', return_value=iocs_text_dict) mocker.patch.object(demisto, 'getLastRun', return_value={'last_run': 1578383898000}) ioc_list = ei.get_outbound_ioc_values( out_format='text', indicator_query="type:ip", on_demand=False, limit=50, offset=0, cache_refresh_rate='1 minute') for ioc_row in ioc_list: assert ioc_row in iocs_text_dict
def test_find_indicators_with_limit_loop_1(self, mocker): """Test find indicators stops when reached last page""" import ExportIndicators as ei with open('ExportIndicators_test/TestHelperFunctions/demisto_iocs.json', 'r') as iocs_json_f: iocs_dict = {'iocs': json.loads(iocs_json_f.read())} limit = 50 mocker.patch.object(demisto, 'searchIndicators', return_value=iocs_dict) ei_vals, nxt_pg = ei.find_indicators_with_limit_loop(indicator_query='', limit=limit) assert nxt_pg == 1 # assert entered into loop
def test_find_indicators_with_limit_1(self, mocker): """Test find indicators limit""" import ExportIndicators as ei with open('ExportIndicators_test/TestHelperFunctions/demisto_iocs.json', 'r') as iocs_json_f: iocs_json = json.loads(iocs_json_f.read()) limit = 30 mocker.patch.object(ei, 'find_indicators_with_limit_loop', return_value=(iocs_json, 1)) ei_vals = ei.find_indicators_with_limit(indicator_query='', limit=limit, offset=0) assert len(ei_vals) == limit
def test_refresh_outbound_context_3(self, mocker): """Test out_format=csv""" import ExportIndicators as ei with open('ExportIndicators_test/TestHelperFunctions/demisto_iocs.json', 'r') as iocs_json_f: iocs_json = json.loads(iocs_json_f.read()) mocker.patch.object(ei, 'find_indicators_with_limit', return_value=iocs_json) ei_vals = ei.refresh_outbound_context(indicator_query='', out_format='csv') with open('ExportIndicators_test/TestHelperFunctions/iocs_out_csv.txt', 'r') as iocs_out_f: iocs_out = iocs_out_f.read() assert iocs_out == ei_vals
def test_find_indicators_with_limit_and_offset_1(self, mocker): """Test find indicators limit and offset""" import ExportIndicators as ei with open('ExportIndicators_test/TestHelperFunctions/demisto_iocs.json', 'r') as iocs_json_f: iocs_json = json.loads(iocs_json_f.read()) limit = 30 offset = 1 mocker.patch.object(ei, 'find_indicators_with_limit_loop', return_value=(iocs_json, 1)) ei_vals = ei.find_indicators_with_limit(indicator_query='', limit=limit, offset=offset) assert len(ei_vals) == limit # check that the first value is the second on the list assert ei_vals[0].get('value') == '212.115.110.19'