def test_empty_response(requests_mock): """ Scenario: Fetch incidents but there are no incidents to return. Given: - User has provided valid credentials. - Headers and JWT token have been set. When: - Every time fetch_incident is called (either timed or by command). - There are no incidents to return. Then: - Ensure number of incidents is correct (None). - Ensure last_fetch is correctly configured according to mock response. """ from SophosCentral import fetch_incidents client = init_mock_client(requests_mock) mock_response = load_mock_response('empty.json') requests_mock.post(f'{BASE_URL}/common/v1/alerts/search', json=mock_response) last_fetch, incidents = fetch_incidents(client, {'last_fetch': 100000000}, '3 days', ['x'], ['x'], '50') assert last_fetch.get('last_fetch') == 100000001 assert len(incidents) == 0
def test_fetch_incidents_no_last_fetch(requests_mock): """ Scenario: Fetch incidents for the first time, so there is no last_fetch available. Given: - User has provided valid credentials. - Headers and JWT token have been set. - First time running fetch incidents. When: - Every time fetch_incident is called (either timed or by command). Then: - Ensure number of incidents is correct. - Ensure last_fetch is correctly configured according to mock response. """ from SophosCentral import Client, fetch_incidents mock_client_data = load_mock_response('client_data.json') requests_mock.get('https://api.central.sophos.com/whoami/v1', json=mock_client_data) client = Client(bearer_token='a', verify=False, client_id='a', client_secret='b', proxy=False) mock_response = load_mock_response('alert_list.json') requests_mock.post(f'{BASE_URL}/common/v1/alerts/search', json=mock_response) last_fetch, incidents = fetch_incidents(client, {}, '12 years', ['x'], ['x'], '50') wanted_time = datetime.timestamp(datetime.strptime('2020-11-04T09:31:19.895Z', DATE_FORMAT)) assert last_fetch.get('last_fetch') == wanted_time * 1000 assert len(incidents) == 3 assert incidents[0].get('name') == 'Sophos Central Alert 56931431-9faf-480c-ba1d-8d7541eae259'
def test_sanity(requests_mock) -> None: """ Scenario: Fetch incidents. Given: - User has provided valid credentials. - Headers and JWT token have been set. When: - Every time fetch_incident is called (either timed or by command). Then: - Ensure number of incidents is correct. - Ensure last_fetch is correctly configured according to mock response. """ from SophosCentral import fetch_incidents client = init_mock_client(requests_mock) mock_response = load_mock_response('alert_list.json') requests_mock.post(f'{BASE_URL}/common/v1/alerts/search', json=mock_response) last_fetch, incidents = fetch_incidents(client, {'last_fetch': 1}, '1 days', ['x'], ['x'], '50') wanted_time = datetime.timestamp( datetime.strptime('2020-11-04T09:31:19.895Z', DATE_FORMAT)) assert last_fetch.get('last_fetch') == wanted_time * 1000 assert len(incidents) == 3 assert incidents[0].get( 'name' ) == 'Sophos Central Alert 56931431-9faf-480c-ba1d-8d7541eae259'
def test_fetch_incidents_empty_response(requests_mock): """ Scenario: Fetch incidents but there are no incidents to return. Given: - User has provided valid credentials. - Headers and JWT token have been set. When: - Every time fetch_incident is called (either timed or by command). - There are no incidents to return. Then: - Ensure number of incidents is correct (None). - Ensure last_fetch is correctly configured according to mock response. """ from SophosCentral import Client, fetch_incidents mock_client_data = load_mock_response('client_data.json') requests_mock.get('https://api.central.sophos.com/whoami/v1', json=mock_client_data) client = Client(bearer_token='a', verify=False, client_id='a', client_secret='b', proxy=False) mock_response = load_mock_response('empty.json') requests_mock.post(f'{BASE_URL}/common/v1/alerts/search', json=mock_response) last_fetch, incidents = fetch_incidents(client, {'last_fetch': 100000000}, '3 days', ['x'], ['x'], '50') assert last_fetch.get('last_fetch') == 100000001 assert len(incidents) == 0