def test_escalate_alert(mocker): """ Given: - An app client object - Alert-id = 1234 = escalation_id = 123 When: - Calling function escalate_alert Then: - Ensure the return data is correct """ mock_client = OpsGenieV3.Client(base_url="") mocker.patch.object(mock_client, 'escalate_alert', return_value=util_load_json('test_data/request.json')) mocker.patch.object(mock_client, 'get_request', return_value=util_load_json( 'test_data/escalate_alert.json', True)) res = OpsGenieV3.escalate_alert(mock_client, { "alert-id": 1234, "escalation_id": 123 }) assert ( res.raw_response == util_load_json('test_data/escalate_alert.json'))
def test_remove_tag_incident(mocker): """ Given: - An app client object - incident_id = 1234 - tags = [1, 2] When: - Calling function remove_tag_incident Then: - Ensure the return data is correct """ mocker.patch('CommonServerPython.get_demisto_version', return_value={"version": "6.2.0"}) mock_client = OpsGenieV3.Client(base_url="") mocker.patch.object(mock_client, 'remove_tag_incident', return_value=util_load_json('test_data/request.json')) mocker.patch.object(mock_client, 'get_request', return_value=util_load_json( 'test_data/remove_tag_incident.json', True)) res = OpsGenieV3.remove_tag_incident(mock_client, { "incident_id": 1234, "tags": [1, 2] }) assert (res.raw_response == util_load_json( 'test_data/remove_tag_incident.json'))
def test_add_responder_incident(mocker): """ Given: - An app client object - incident_id = 1234 - responders = ["team", "id", "name"] When: - Calling function add_responder_incident Then: - Ensure the return data is correct """ mock_client = OpsGenieV3.Client(base_url="") mocker.patch.object(mock_client, 'add_responder_incident', return_value=util_load_json('test_data/request.json')) mocker.patch.object(mock_client, 'get_request', return_value=util_load_json( 'test_data/add_responder_incident.json', True)) res = OpsGenieV3.add_responder_incident( mock_client, { "incident_id": 1234, "responders": ["team", "id", "name"] }) assert (res.raw_response == util_load_json( 'test_data/add_responder_incident.json'))
def test_get_on_call_without_args(): """ Given: - An app client object When: - Calling function get_on_call with no arguments Then: - Ensure the resulted will raise an exception. """ mock_client = OpsGenieV3.Client(base_url="") with pytest.raises(DemistoException): OpsGenieV3.get_on_call(mock_client, {})
def test_create_incident_wrong_args(): """ Given: - An app client object When: - Calling function create_incident with argument responders in the wrong format Then: - Ensure the resulted will raise an exception. """ mock_client = OpsGenieV3.Client(base_url="") with pytest.raises(DemistoException): OpsGenieV3.create_incident(mock_client, {'responders': ['team', 'id']})
def test_responders_to_json_empty_value(): """ Given: - An app client object - responders = {} When: - Calling function responders_to_json Then: - Ensure the return data is correct """ mock_client = OpsGenieV3.Client(base_url="") res = mock_client.responders_to_json(responders={}, responder_key="responder") assert (res == {})
def test_get_schedules_with_both_args(): """ Given: - An app client object When: - Calling function get_schedules with both arguments Then: - Ensure the resulted will raise an exception. """ mock_client = OpsGenieV3.Client(base_url="") with pytest.raises(DemistoException): OpsGenieV3.get_schedules(mock_client, { "schedule_id": "ID", "schedule_name": "NAME" })
def test_get_teams(mocker): """ Given: - An app client object When: - Calling function get_teams Then: - Ensure the return data is correct """ mock_client = OpsGenieV3.Client(base_url="") mocker.patch.object( mock_client, 'list_teams', return_value=util_load_json('test_data/get_teams.json')) res = OpsGenieV3.get_teams(mock_client, {}) assert len(res.outputs) == 2
def test_get_escalation(mocker): """ Given: - An app client object - escalation_id = 123 When: - Calling function get_escalations Then: - Ensure the return data is correct """ mock_client = OpsGenieV3.Client(base_url="") mocker.patch.object( mock_client, 'get_escalation', return_value=util_load_json('test_data/get_escalations.json')) res = OpsGenieV3.get_escalations(mock_client, {"escalation_id": 123}) assert len(res.outputs) == 2
def test_get_request_command_404(requests_mock, mocker): """ Given: - A call to get-retquest When: - response is 404 Then: - Scheduledcommand is returned """ mocker.patch( 'CommonServerPython.ScheduledCommand.raise_error_if_not_supported') requests_mock.get(url='http://example.com/v2/alert/requests/1', status_code=404) args = {'request_id': 1, 'request_type': 'alert'} response = OpsGenieV3.get_request_command( OpsGenieV3.Client(base_url="http://example.com"), args) assert response.scheduled_command._args == {**args, 'polled_once': True}
def test_fetch_incidents_command(mocker): """ Given: - An app client object When: - Calling function fetch_incidents_command Then: - Ensure the return data is correct """ mocker.patch('CommonServerPython.get_demisto_version', return_value={"version": "6.2.0"}) mock_client = OpsGenieV3.Client(base_url="") mocker.patch.object( mock_client, 'list_alerts', return_value=util_load_json('test_data/get_alerts.json')) mocker.patch.object( mock_client, 'list_incidents', return_value=util_load_json('test_data/get_incidents.json')) mocker.patch.object(OpsGenieV3, '_get_utc_now', return_value=datetime(2021, 11, 26)) mocker.patch.object(OpsGenieV3, '_parse_fetch_time', return_value='2021-11-23T12:19:48Z') res, last_run = OpsGenieV3.fetch_incidents_command(mock_client, {"max_fetch": 1}) assert len(res) == 2 assert last_run == { 'Alerts': { 'lastRun': '2021-11-26T00:00:00Z', 'next_page': 'https://api.opsgenie.com/v2/alerts?limit=1&sort=' 'createdAt&offset=1&order=desc' }, 'Incidents': { 'lastRun': '2021-11-26T00:00:00Z', 'next_page': 'https://api.opsgenie.com/v1/incidents?limit=1&' 'sort=insertedAt&offset=1&order=desc' } }
def test_get_on_call(mocker): """ Given: - An app client object - schedule_id = 1234 When: - Calling function get_on_call Then: - Ensure the return data is correct """ mock_client = OpsGenieV3.Client(base_url="") mocker.patch.object( mock_client, 'get_on_call', return_value=util_load_json('test_data/delete_incident.json')) res = OpsGenieV3.get_on_call(mock_client, {"schedule_id": 1234}) assert ( res.raw_response == util_load_json('test_data/delete_incident.json'))
def test_get_alert_attachments(mocker): """ Given: - An app client object - Alert-id = 1234 When: - Calling function get_alert_attachments Then: - Ensure the return data is correct """ mock_client = OpsGenieV3.Client(base_url="") mocker.patch.object( mock_client, 'get_alert_attachments', return_value=util_load_json('test_data/get_alert_attachments.json')) res = OpsGenieV3.get_alert_attachments(mock_client, {"alert-id": 1234}) assert ( res.readable_output == "### OpsGenie Attachment\n**No entries.**\n")
def test_get_request_command(requests_mock, mocker): """ Given: - A call to get-retquest When: - response is successful Then: - output is returned """ output = {'hello': 'world'} mocker.patch( 'CommonServerPython.ScheduledCommand.raise_error_if_not_supported') requests_mock.get(url='http://example.com/v2/alert/requests/1', json={'data': output}) args = {'request_id': 1, 'request_type': 'alert'} response = OpsGenieV3.get_request_command( OpsGenieV3.Client(base_url="http://example.com"), args) assert response.outputs == output
def test_get_alerts(mocker): """ Given: - An app client object - Limit = 1 When: - Calling function list_alerts Then: - Ensure the return data is correct """ mocker.patch('CommonServerPython.get_demisto_version', return_value={"version": "6.2.0"}) mock_client = OpsGenieV3.Client(base_url="") mocker.patch.object( mock_client, 'list_alerts', return_value=util_load_json('test_data/get_alerts.json')) res = OpsGenieV3.get_alerts(mock_client, {"limit": 1}) assert (len(res.outputs) == 1)
def test_get_schedules(): """ Given: - An app client object When: - Calling function get_schedules Case A: "schedule_id" = 1234 Case B: No arguments Then: - Ensure the right function was called Case A: Called get_schedule Case B: Called list_schedules """ mock_client = OpsGenieV3.Client(base_url="") mock_client.get_schedule = mock.MagicMock() OpsGenieV3.get_schedules(mock_client, {"schedule_id": 1234}) assert mock_client.get_schedule.called mock_client.list_schedules = mock.MagicMock() OpsGenieV3.get_schedules(mock_client, {}) assert mock_client.list_schedules.called
def test_build_query_not_fetch_without_query(): """ Given: - An app client object - args - is_fetch_query = False When: - Calling function build_query Then: - Ensure the return data is correct """ args = { "status": "Open", "is_fetch_query": False, "priority": "P1,P3", "tags": "1,2" } mock_client = OpsGenieV3.Client(base_url="") res = mock_client.build_query(args) assert (res == "status=open AND priority: (P1 OR P3) AND tag: (1 OR 2)")
def test_get_alerts_going_to_right_function(): """ Given: - An app client object When: - Calling function get_alerts Case A: "alert-id" = 1234 Case B: No arguments Then: - Ensure the right function was called Case A: Called get_alert Case B: Called list_alerts """ mock_client = OpsGenieV3.Client(base_url="") mock_client.get_alert = mock.MagicMock() OpsGenieV3.get_alerts(mock_client, {"alert-id": 1234}) assert mock_client.get_alert.called OpsGenieV3.list_alerts = mock.MagicMock() OpsGenieV3.get_alerts(mock_client, {}) assert OpsGenieV3.list_alerts.called
def test_fetch_incidents_command_no_result(mocker): """ Given: - An app client object - max_fetch = 1 When: - Calling function fetch_incidents_command - The list_alerts and list_incidents functions returns empty response Then: - Ensure the return data is correct """ mocker.patch('CommonServerPython.get_demisto_version', return_value={"version": "6.2.0"}) mock_client = OpsGenieV3.Client(base_url="") mocker.patch.object( mock_client, 'list_alerts', return_value=util_load_json('test_data/empty_response.json')) mocker.patch.object( mock_client, 'list_incidents', return_value=util_load_json('test_data/empty_response.json')) mocker.patch.object(OpsGenieV3, '_get_utc_now', return_value=datetime(2021, 11, 26)) mocker.patch.object(OpsGenieV3, '_parse_fetch_time', return_value='2021-11-23T12:19:48Z') res, last_run = OpsGenieV3.fetch_incidents_command(mock_client, {"max_fetch": 1}) assert len(res) == 0 assert last_run == { 'Alerts': { 'lastRun': '2021-11-26T00:00:00Z', 'next_page': None }, 'Incidents': { 'lastRun': '2021-11-26T00:00:00Z', 'next_page': None } }
def test_build_query_not_fetch(): """ Given: - An app client object - args - is_fetch_query = False When: - Calling function build_query Then: - Ensure the return data is correct """ args = { "query": "createdAt < 147039484114", "status": "Open", "is_fetch_query": False, "priority": "P1,P3", "tags": "1,2" } mock_client = OpsGenieV3.Client(base_url="") res = mock_client.build_query(args) assert (res == "createdAt < 147039484114")
def test_ack_alert(mocker): """ Given: - An app client object - Alert-id = 1234 When: - Calling function ack_alert Then: - Ensure the return data is correct """ mocker.patch('CommonServerPython.get_demisto_version', return_value={"version": "6.2.0"}) mock_client = OpsGenieV3.Client(base_url="") mocker.patch.object(mock_client, 'ack_alert', return_value=util_load_json('test_data/request.json')) mocker.patch.object(mock_client, 'get_request', return_value=util_load_json('test_data/ack_alert.json', True)) res = OpsGenieV3.ack_alert(mock_client, {"alert-id": 1234}) assert (res.raw_response == util_load_json('test_data/ack_alert.json'))
def test_create_incident(mocker): """ Given: - An app client object When: - Calling function create_incident Then: - Ensure the return data is correct """ mocker.patch('CommonServerPython.get_demisto_version', return_value={"version": "6.2.0"}) mock_client = OpsGenieV3.Client(base_url="") mocker.patch.object(mock_client, 'create_incident', return_value=util_load_json('test_data/request.json')) mocker.patch.object(mock_client, 'get_request', return_value=util_load_json( 'test_data/create_incident.json', True)) res = OpsGenieV3.create_incident(mock_client, {}) assert ( res.raw_response == util_load_json('test_data/create_incident.json'))
def test_create_alert(mocker): """ Given: - An app client object - Responders "team,id,123" When: - Calling function create_alert with argument responders in the right format Then: - Ensure the return data is correct """ mocker.patch('CommonServerPython.get_demisto_version', return_value={"version": "6.2.0"}) mock_client = OpsGenieV3.Client(base_url="") mocker.patch.object(mock_client, 'create_alert', return_value=util_load_json('test_data/request.json')) mocker.patch.object(mock_client, 'get_request', return_value=util_load_json( 'test_data/create_alert.json', True)) res = OpsGenieV3.create_alert(mock_client, {'responders': "team,id,123"}) assert (res.raw_response == util_load_json('test_data/create_alert.json'))
def test_responders_to_json(): """ Given: - An app client object - responders = ["team", "id", 1, "schedule", "name", "a"] - responder_key = 'responders' When: - Calling function responders_to_json Then: - Ensure the return data is correct """ mock_client = OpsGenieV3.Client(base_url="") res = mock_client.responders_to_json( responders=["team", "id", 1, "schedule", "name", "a"], responder_key='responders') assert (res == { 'responders': [{ 'id': 1, 'type': 'team' }, { 'name': 'a', 'type': 'schedule' }] })
def test_fetch_with_paging_only_alerts(mocker): """ Given: - An app client object - max_fetch = 2 - event_types = OpsGenieV3.ALERT_TYPE When: - Calling function fetch_incidents_command - The list_alerts function returns result with paging Then: - Ensure the return data is correct """ mocker.patch('CommonServerPython.get_demisto_version', return_value={"version": "6.2.0"}) mock_client = OpsGenieV3.Client(base_url="") mocker.patch.object( mock_client, 'list_alerts', return_value=util_load_json('test_data/get_alerts.json')) mocker.patch.object( mock_client, 'get_paged', return_value=util_load_json('test_data/get_alerts_without_next.json')) mocker.patch.object(OpsGenieV3, '_get_utc_now', return_value=datetime(2021, 11, 26)) mocker.patch.object(OpsGenieV3, '_parse_fetch_time', return_value='2021-11-23T12:19:48Z') res, last_run = OpsGenieV3.fetch_incidents_command( mock_client, { "max_fetch": 2, "event_types": OpsGenieV3.ALERT_TYPE }) assert (last_run == { 'Alerts': { 'lastRun': '2021-11-26T00:00:00Z', 'next_page': 'https://api.opsgenie.com/v2/alerts?limit=1&sort=createdAt&offset=1&order=desc' }, 'Incidents': { 'lastRun': None, 'next_page': None } }) mocker.patch.object(demisto, 'getLastRun', return_value=last_run) res, last_run = OpsGenieV3.fetch_incidents_command( mock_client, { "max_fetch": 2, "event_types": OpsGenieV3.ALERT_TYPE }, last_run) assert (last_run == { 'Alerts': { 'lastRun': '2021-11-26T00:00:00Z', 'next_page': None }, 'Incidents': { 'lastRun': None, 'next_page': None } })