def test_exception_in_return_error(mocker): from CommonServerPython import return_error, IntegrationLogger expected = {'EntryContext': None, 'Type': 4, 'ContentsFormat': 'text', 'Contents': 'Message'} mocker.patch.object(demisto, 'results') mocker.patch.object(IntegrationLogger, '__call__') with raises(SystemExit, match='0'): return_error("Message", error=ValueError("Error!")) results = demisto.results.call_args[0][0] assert expected == results # IntegrationLogger = LOG (2 times if exception supplied) assert IntegrationLogger.__call__.call_count == 2
def test_return_error_fetch_incidents(mocker): from CommonServerPython import return_error err_msg = "Testing unicode Ё" # Test fetch-incidents mocker.patch.object(demisto, 'command', return_value="fetch-incidents") returned_error = False try: return_error(err_msg) except Exception as e: returned_error = True assert str(e) == err_msg assert returned_error
def test_return_error_long_running_execution(mocker): from CommonServerPython import return_error err_msg = "Testing unicode Ё" # Test long-running-execution mocker.patch.object(demisto, 'command', return_value="long-running-execution") returned_error = False try: return_error(err_msg) except Exception as e: returned_error = True assert str(e) == err_msg assert returned_error
def test_return_error_script(mocker, monkeypatch): from CommonServerPython import return_error mocker.patch.object(sys, 'exit') mocker.spy(demisto, 'results') monkeypatch.delattr(demisto, 'command') err_msg = "Testing unicode Ё" outputs = {'output': 'error'} expected_error = { 'Type': entryTypes['error'], 'ContentsFormat': formats['text'], 'Contents': err_msg, "EntryContext": outputs } assert not hasattr(demisto, 'command') return_error(err_msg, '', outputs) assert str(demisto.results.call_args) == "call({})".format(expected_error)
def test_return_error_command(mocker): from CommonServerPython import return_error err_msg = "Testing unicode Ё" outputs = {'output': 'error'} expected_error = { 'Type': entryTypes['error'], 'ContentsFormat': formats['text'], 'Contents': err_msg, "EntryContext": outputs } # Test command that is not fetch-incidents mocker.patch.object(demisto, 'command', return_value="test-command") mocker.patch.object(sys, 'exit') mocker.spy(demisto, 'results') return_error(err_msg, '', outputs) assert str(demisto.results.call_args) == "call({})".format(expected_error)
'irondefense-get-alert-irondome-information': get_alert_irondome_information_command, } COOKIE_KEY = 'user_sid' LOG_PREFIX = 'IronDefense Integration: ' '''EXECUTION''' if __name__ == 'builtins': try: # Globals PARAMS = demisto.params() CREDENTIALS = PARAMS.get('credentials') HOST = PARAMS.get('ironAPIHost', 'localhost') PORT = PARAMS.get('ironAPIPort', 443) REQUEST_TIMEOUT = float(PARAMS.get('requestTimeout', 60)) LOGGER = XsoarLogger(demisto, LOG_PREFIX) # initialize the IronDefense object IRON_DEFENSE = IronDefense(demisto, requests.Session(), HOST, PORT, CREDENTIALS, LOGGER, request_timeout=REQUEST_TIMEOUT) LOGGER.debug('Invoking integration with Command: ' + demisto.command()) if demisto.command() in COMMANDS.keys(): COMMANDS[demisto.command()]() else: return_error('Command not found: ' + demisto.command()) except Exception as e: demisto.error(traceback.format_exc()) return_error(str(e))