Example #1
0
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'
        }
    }
Example #2
0
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
        }
    }
Example #3
0
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
        }
    })