예제 #1
0
def test_fetch_incidents(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 Cyberint import Client, fetch_incidents
    mock_response = load_mock_response('csv_example.csv')
    requests_mock.get(f'{BASE_URL}/api/v1/alerts/ARG-3/attachments/123',
                      json=mock_response)
    mock_response = json.loads(load_mock_response('list_alerts.json'))
    requests_mock.post(f'{BASE_URL}/api/v1/alerts', json=mock_response)
    client = Client(base_url=BASE_URL,
                    verify_ssl=False,
                    access_token='xxx',
                    proxy=False)
    last_fetch, incidents = fetch_incidents(client, {'last_fetch': 100000000},
                                            '3 days', [], [], [], [], 50)
    wanted_time = datetime.timestamp(
        datetime.strptime('2020-12-30T00:00:57Z', DATE_FORMAT))
    assert last_fetch.get('last_fetch') == wanted_time * 1000
    assert len(incidents) == 3
    assert incidents[0].get(
        'name') == 'Cyberint alert ARG-3: Company Customer Credentials Exposed'
예제 #2
0
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 Cyberint import Client, fetch_incidents
    mock_response = json.loads(load_mock_response('empty.json'))
    requests_mock.post(f'{BASE_URL}/api/v1/alerts', json=mock_response)
    client = Client(base_url=BASE_URL,
                    verify_ssl=False,
                    access_token='xxx',
                    proxy=False)
    last_fetch, incidents = fetch_incidents(client, {'last_fetch': 100000000},
                                            '3 days', [], [], [], [], 50)
    assert last_fetch.get('last_fetch') == 100001000
    assert len(incidents) == 0