From 9acea938c4b6df54162b2c17dd59c7feaeb75d94 Mon Sep 17 00:00:00 2001 From: Kapil Bisen Date: Tue, 10 Dec 2024 14:48:15 +0530 Subject: [PATCH] Unit tests for API failures and app exception --- .../Doppel/Integrations/Doppel/Doppel_test.py | 60 +++++++++++++++++-- ...get-alert-failure-400-both-args-given.json | 3 + ...et-alert-failure-400-invalid-alert-id.json | 3 + .../get-alert-failure-400-invalid-entity.json | 3 + ...nd-200.json => get-alert-success-200.json} | 0 5 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 Packs/Doppel/Integrations/Doppel/test_data/get-alert-failure-400-both-args-given.json create mode 100644 Packs/Doppel/Integrations/Doppel/test_data/get-alert-failure-400-invalid-alert-id.json create mode 100644 Packs/Doppel/Integrations/Doppel/test_data/get-alert-failure-400-invalid-entity.json rename Packs/Doppel/Integrations/Doppel/test_data/{get-alert-command-200.json => get-alert-success-200.json} (100%) diff --git a/Packs/Doppel/Integrations/Doppel/Doppel_test.py b/Packs/Doppel/Integrations/Doppel/Doppel_test.py index 19c1ce3686c6..9d1b8a3f08f6 100644 --- a/Packs/Doppel/Integrations/Doppel/Doppel_test.py +++ b/Packs/Doppel/Integrations/Doppel/Doppel_test.py @@ -10,6 +10,7 @@ import demistomock as demisto import requests_mock +from CommonServerPython import DemistoException from Doppel import Client, main @@ -18,13 +19,12 @@ def util_load_json(path): return json.loads(f.read()) -get_alert_mock_response_200 = util_load_json('test_data/get-alert-command-200.json') -@pytest.mark.parametrize("command, api_path, args, api_response", +@pytest.mark.parametrize("command, args, api_path, api_response", [ - ("get-alert", "https://api.doppel.com/v1/alert?id=TST-31222", {"id": "TST-31222"}, get_alert_mock_response_200) + ("get-alert", {"id": "TST-31222"}, "https://api.doppel.com/v1/alert?id=TST-31222", util_load_json('test_data/get-alert-success-200.json')) ] ) -def test_command_success(mocker, requests_mock, command, api_path, args, api_response): +def test_command_success(mocker, requests_mock, command, args, api_path, api_response): """Tests the current command """ mocker.patch.object(demisto, 'params', return_value={"url": "https://api.doppel.com", "credentials": {"password": ""}}) @@ -38,10 +38,60 @@ def test_command_success(mocker, requests_mock, command, api_path, args, api_res main() assert adapter.call_count == 1 - assert adapter.called assert api_response == json.loads(requests.get(api_path).text) assert 200 == requests.get(api_path).status_code assert sorted(results_checker.call_args.args[0].get('Contents')) == sorted(dict(api_response)) + + +@pytest.mark.parametrize("command, args, api_path, status_code, api_response", + [ + ("get-alert", {"entity": "123"}, "https://api.doppel.com/v1/alert?entity=123", 400, util_load_json('test_data/get-alert-failure-400-invalid-entity.json')), + ("get-alert", {"id": "1234"}, "https://api.doppel.com/v1/alert?id=1234", 400, util_load_json('test_data/get-alert-failure-400-invalid-alert-id.json')), + ] + ) +def test_command_failure(mocker, requests_mock, command, args, api_path, status_code, api_response): + """Tests the current command + """ + mocker.patch.object(demisto, 'params', return_value={"url": "https://api.doppel.com", "credentials": {"password": ""}}) + mocker.patch.object(demisto, 'command', return_value=command) + mocker.patch.object(demisto, 'args', return_value=args) + mocker.patch.object(demisto, 'results', return_value=None) + + adapter = requests_mock.get(api_path, status_code=status_code, json=api_response) + + # Call the main function so that the command will be called + with pytest.raises(SystemExit) as pytest_wrapped_e: + main() + + assert pytest_wrapped_e.type is SystemExit + assert pytest_wrapped_e.value.code == 0 + assert adapter.call_count == 1 + assert api_response == json.loads(requests.get(api_path).text) + assert status_code == requests.get(api_path).status_code +@pytest.mark.parametrize("command, args, api_path, status_code, exception_message", + [ + ("get-alert", {"id": "TST-31", "entity": "http://dummyrul.com"}, "https://api.doppel.com/v1/alert?id=TST-31&entity=http://dummyrul.com", 400, "Failed to execute get-alert command.\nError:\nBoth id and entity is specified. We need exactly single input for this command") + ] + ) +def test_command_exception(mocker, requests_mock, command, args, api_path, status_code, exception_message): + """Tests the current command + """ + mocker.patch.object(demisto, 'params', return_value={"url": "https://api.doppel.com", "credentials": {"password": ""}}) + mocker.patch.object(demisto, 'command', return_value=command) + mocker.patch.object(demisto, 'args', return_value=args) + results_checker = mocker.patch.object(demisto, 'results', return_value=None) + + adapter = requests_mock.get(api_path, status_code=status_code, json=None) + + # Call the main function so that the command will be called + with pytest.raises(SystemExit) as pytest_wrapped_e: + main() + + assert pytest_wrapped_e.type is SystemExit + assert pytest_wrapped_e.value.code == 0 + # Notice that the API was not called, but the app itself has raised an exception before making the API call + assert adapter.call_count == 0 + assert results_checker.call_args.args[0].get('Contents') == exception_message \ No newline at end of file diff --git a/Packs/Doppel/Integrations/Doppel/test_data/get-alert-failure-400-both-args-given.json b/Packs/Doppel/Integrations/Doppel/test_data/get-alert-failure-400-both-args-given.json new file mode 100644 index 000000000000..2a5bfef6bf25 --- /dev/null +++ b/Packs/Doppel/Integrations/Doppel/test_data/get-alert-failure-400-both-args-given.json @@ -0,0 +1,3 @@ +{ + "message": "Must provide exactly one of 'id' or 'entity'." +} \ No newline at end of file diff --git a/Packs/Doppel/Integrations/Doppel/test_data/get-alert-failure-400-invalid-alert-id.json b/Packs/Doppel/Integrations/Doppel/test_data/get-alert-failure-400-invalid-alert-id.json new file mode 100644 index 000000000000..bfe040af1de4 --- /dev/null +++ b/Packs/Doppel/Integrations/Doppel/test_data/get-alert-failure-400-invalid-alert-id.json @@ -0,0 +1,3 @@ +{ + "message": "Invalid alert id: 1234" +} \ No newline at end of file diff --git a/Packs/Doppel/Integrations/Doppel/test_data/get-alert-failure-400-invalid-entity.json b/Packs/Doppel/Integrations/Doppel/test_data/get-alert-failure-400-invalid-entity.json new file mode 100644 index 000000000000..51c97220d6ad --- /dev/null +++ b/Packs/Doppel/Integrations/Doppel/test_data/get-alert-failure-400-invalid-entity.json @@ -0,0 +1,3 @@ +{ + "message": "Invalid entity: 123. Needs to be url, phone number or email." +} \ No newline at end of file diff --git a/Packs/Doppel/Integrations/Doppel/test_data/get-alert-command-200.json b/Packs/Doppel/Integrations/Doppel/test_data/get-alert-success-200.json similarity index 100% rename from Packs/Doppel/Integrations/Doppel/test_data/get-alert-command-200.json rename to Packs/Doppel/Integrations/Doppel/test_data/get-alert-success-200.json