Exemplo n.º 1
0
def test_fetch_incidents_no_first(mocker, requests_mock):
    """Unit test
    Given
    - na
    When
    - we mock the fetch incidents flow
    Then
    - Validate that the last_fetch is correct (deafult of 3 past days)
    """
    from dateparser import parse
    from pytz import utc
    from GuardiCoreV2 import Client, fetch_incidents

    incidents_data = util_load_json('test_data/fetch_incidents_response.json')
    requests_mock.post(
        'https://api.guardicoreexample.com/api/v3.0/authenticate',
        json={'access_token': TEST_API_KEY})
    requests_mock.get('https://api.guardicoreexample.com/api/v3.0/incidents',
                      json=incidents_data.get('first'))

    client = Client(base_url='https://api.guardicoreexample.com/api/v3.0',
                    verify=False,
                    proxy=False,
                    username='******',
                    password='******')
    incidents, last_fetch = fetch_incidents(client, {})
    # Fetch first time, then change last fetch
    last_three = int(parse('3 days').replace(tzinfo=utc).timestamp()) * 1000
    assert last_fetch == last_three
Exemplo n.º 2
0
def test_fetch_incidents(mocker, requests_mock):
    """Unit test
    Given
    - a first_fetch time (of 40 days)
    When
    - we mock the fetch incidents flow
    - we mock the fetch incidents flow is called twice
    Then
    - Validate that the last_fetch is correct (unix time of 40 days)
    - Validate that the first incident returned has a correct id
    - Validate that the length of the incidents is correct
    - Validate that the last_fetch is the last incident fetched
    - Validate that the incidents are all fetched (only 1 new one)
    """
    from GuardiCoreV2 import Client, fetch_incidents
    from CommonServerPython import \
        demisto  # noqa # pylint: disable=unused-wildcard-importcommon
    incidents_data = util_load_json('test_data/fetch_incidents_response.json')
    requests_mock.post(
        'https://api.guardicoreexample.com/api/v3.0/authenticate',
        json={'access_token': TEST_API_KEY})
    requests_mock.get('https://api.guardicoreexample.com/api/v3.0/incidents',
                      json=incidents_data.get('first'))

    client = Client(base_url='https://api.guardicoreexample.com/api/v3.0',
                    verify=False,
                    proxy=False,
                    username='******',
                    password='******')
    incidents, last_fetch = fetch_incidents(
        client, {'first_fetch': '40 years'}
    )  # if xsoar is still here when this is a bug then we have a good problem on our hands :)
    # Fetch first time, then change last fetch
    assert last_fetch == 1611322222222
    assert incidents[0].get('name') == 'Guardicore Incident (INC-ADB636B7)'
    assert len(incidents) == 2

    mocker.patch.object(demisto,
                        'getLastRun',
                        return_value={'last_fetch': last_fetch})
    requests_mock.get('https://api.guardicoreexample.com/api/v3.0/incidents',
                      json=incidents_data.get('second'))

    incidents, last_fetch = fetch_incidents(client, {})
    # Now we should see the last fetch changed
    assert last_fetch == 1611322333333
    assert len(incidents) == 1