Skip to content

Commit

Permalink
Unit tests for API failures and app exception
Browse files Browse the repository at this point in the history
  • Loading branch information
kapil-metron committed Dec 10, 2024
1 parent 55fa9fc commit 9acea93
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 5 deletions.
60 changes: 55 additions & 5 deletions Packs/Doppel/Integrations/Doppel/Doppel_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import demistomock as demisto
import requests_mock

from CommonServerPython import DemistoException
from Doppel import Client, main


Expand All @@ -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'))

Check failure on line 24 in Packs/Doppel/Integrations/Doppel/Doppel_test.py

View workflow job for this annotation

GitHub Actions / pre-commit / pre-commit

Ruff (E501)

Packs/Doppel/Integrations/Doppel/Doppel_test.py:24:131: E501 Line too long (167 > 130 characters)
]
)
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):

Check failure on line 27 in Packs/Doppel/Integrations/Doppel/Doppel_test.py

View workflow job for this annotation

GitHub Actions / pre-commit / pre-commit

Ruff (F811)

Packs/Doppel/Integrations/Doppel/Doppel_test.py:27:34: F811 Redefinition of unused `requests_mock` from line 11
"""Tests the current command
"""
mocker.patch.object(demisto, 'params', return_value={"url": "https://api.doppel.com", "credentials": {"password": "<API-KEY>"}})

Check failure on line 30 in Packs/Doppel/Integrations/Doppel/Doppel_test.py

View workflow job for this annotation

GitHub Actions / pre-commit / pre-commit

Ruff (E501)

Packs/Doppel/Integrations/Doppel/Doppel_test.py:30:131: E501 Line too long (132 > 130 characters)
Expand All @@ -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')),

Check failure on line 48 in Packs/Doppel/Integrations/Doppel/Doppel_test.py

View workflow job for this annotation

GitHub Actions / pre-commit / pre-commit

Ruff (E501)

Packs/Doppel/Integrations/Doppel/Doppel_test.py:48:131: E501 Line too long (184 > 130 characters)
("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')),

Check failure on line 49 in Packs/Doppel/Integrations/Doppel/Doppel_test.py

View workflow job for this annotation

GitHub Actions / pre-commit / pre-commit

Ruff (E501)

Packs/Doppel/Integrations/Doppel/Doppel_test.py:49:131: E501 Line too long (180 > 130 characters)
]
)
def test_command_failure(mocker, requests_mock, command, args, api_path, status_code, api_response):

Check failure on line 52 in Packs/Doppel/Integrations/Doppel/Doppel_test.py

View workflow job for this annotation

GitHub Actions / pre-commit / pre-commit

Ruff (F811)

Packs/Doppel/Integrations/Doppel/Doppel_test.py:52:34: F811 Redefinition of unused `requests_mock` from line 11
"""Tests the current command
"""
mocker.patch.object(demisto, 'params', return_value={"url": "https://api.doppel.com", "credentials": {"password": "<API-KEY>"}})

Check failure on line 55 in Packs/Doppel/Integrations/Doppel/Doppel_test.py

View workflow job for this annotation

GitHub Actions / pre-commit / pre-commit

Ruff (E501)

Packs/Doppel/Integrations/Doppel/Doppel_test.py:55:131: E501 Line too long (132 > 130 characters)
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")

Check failure on line 75 in Packs/Doppel/Integrations/Doppel/Doppel_test.py

View workflow job for this annotation

GitHub Actions / pre-commit / pre-commit

Ruff (E501)

Packs/Doppel/Integrations/Doppel/Doppel_test.py:75:131: E501 Line too long (298 > 130 characters)
]
)
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": "<API-KEY>"}})
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

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"message": "Must provide exactly one of 'id' or 'entity'."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"message": "Invalid alert id: 1234"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"message": "Invalid entity: 123. Needs to be url, phone number or email."
}

0 comments on commit 9acea93

Please sign in to comment.