예제 #1
0
def test_get_incidents(mocker, requests_mock):
    """Unit test
    Given
    - an incident from and to time, with a limit of 3
    When
    - we mock the incidents get api call
    Then
    - Validate that the correct responses are returned
    """
    from GuardiCoreV2 import Client, get_incidents, INCIDENT_COLUMNS, \
        filter_human_readable

    requests_mock.post(
        'https://api.guardicoreexample.com/api/v3.0/authenticate',
        json={'access_token': TEST_API_KEY})
    client = Client(base_url='https://api.guardicoreexample.com/api/v3.0',
                    verify=False, proxy=False, username='******', password='******')
    args = {'from_time': '2021-07-07T15:31:17Z',
            'to_time': '2022-07-07T15:31:17Z', 'limit': 3}
    mock_response = util_load_json('test_data/get_incidents_response.json')
    mocker.patch.object(client, '_http_request', return_value=mock_response)
    response = get_incidents(client, args)

    # Transform the raw results to be more readable
    hr = []
    for res in response.raw_response:
        row = filter_human_readable(res, human_columns=INCIDENT_COLUMNS)
        row['start_time'] = timestamp_to_datestring(row['start_time'])
        row['end_time'] = timestamp_to_datestring(row['end_time'])
        hr.append(row)

    assert response.outputs == hr
    assert response.raw_response == mock_response.get('objects')
예제 #2
0
def test_filter_human_readable(input, columns, output):
    """Unit test
    Given
    - an empty results dict
    - a valid result dict
    - a valid result dict and a filter column
    - a valid result dict and two filter columns
    When
    - we filter the relevant columns.
    Then
    - return an empty filtered result dict
    - return an empty filtered result dict
    - return a filtered result dict with one column
    - return a filtered result dict with two columns
    Validate that the filter human readable returns correct values.
    """
    from GuardiCoreV2 import filter_human_readable
    assert filter_human_readable(input, human_columns=columns) == output