예제 #1
0
def test_test_module_invalid_token(requests_mock):
    """
    Given:
        - the IP reputation feed

    When:
        - running test-module with a 400 on an invalid claim

    Then:
        - it tells you the test failed

    """

    base_url = "https://cyren.feed/"
    requests_mock.get(
        base_url + "data?format=jsonl&feedId=ip_reputation&offset=0&count=10",
        status_code=400,
        json=dict(statusCode=400,
                  error="unable to parse claims from token: ..."))
    client = Client(feed_name="ip_reputation",
                    base_url=base_url,
                    verify=False,
                    proxy=False)

    assert "Test failed because of an invalid API token!" in _test_module_command(
        client)
예제 #2
0
def test_fetch_indicators_rate_limiting(requests_mock, response_429):
    """
    Given:
        - the IP reputation feed

    When:
        - running fetch-indicators
        - a 429 Rate Limited response from the API

    Then:
        - a DemistoException is raised

    """

    base_url = "https://cyren.feed/"
    requests_mock.get(
        base_url + "data?format=jsonl&feedId=ip_reputation&offset=0&count=10",
        text=response_429,
        status_code=429)
    requests_mock.get(base_url + "info?format=jsonl&feedId=ip_reputation",
                      json=dict(startOffset=0, endOffset=0))
    client = Client(feed_name="ip_reputation",
                    base_url=base_url,
                    verify=False,
                    proxy=False)

    with pytest.raises(DemistoException, match=f".*{response_429}.*"):
        fetch_indicators_command(client, 0, 10, False)
예제 #3
0
def _create_instance(requests_mock,
                     feed,
                     feed_data,
                     offset_data,
                     offset=0,
                     count=2):
    base_url = "https://cyren.feed/"
    requests_mock.get(base_url +
                      "data?format=jsonl&feedId={}&offset={}&count={}".format(
                          feed, offset, count),
                      text=feed_data)
    requests_mock.get(base_url + "info?format=jsonl&feedId={}".format(feed),
                      json=offset_data)
    client = Client(feed_name=feed,
                    base_url=base_url,
                    verify=False,
                    proxy=False)

    def fetch_command(initial_count=0, max_indicators=2, update_context=False):
        return fetch_indicators_command(client, initial_count, max_indicators,
                                        update_context)

    def get_command(max_indicators):
        args = dict(max_indicators=max_indicators)
        return get_indicators_command(client, args)

    return fetch_command, get_command
예제 #4
0
def test_test_module_ok(requests_mock, ip_reputation):
    """
    Given:
        - the IP reputation feed

    When:
        - running test-module with good result

    Then:
        - it tells you the test did not fail

    """

    base_url = "https://cyren.feed/"
    requests_mock.get(base_url + "data?format=jsonl&feedId=ip_reputation&offset=0&count=10", text=ip_reputation)
    client = Client(feed_name="ip_reputation", base_url=base_url, verify=False, proxy=False)

    assert "ok" == _test_module_command(client)
예제 #5
0
def test_test_module_no_entries(requests_mock):
    """
    Given:
        - the IP reputation feed

    When:
        - running test-module with no entries being returned

    Then:
        - it tells you the test failed

    """

    base_url = "https://cyren.feed/"
    requests_mock.get(base_url + "data?format=jsonl&feedId=ip_reputation&offset=0&count=10", text="")
    client = Client(feed_name="ip_reputation", base_url=base_url, verify=False, proxy=False)

    assert "Test failed because no indicators could be fetched!" in _test_module_command(client)
예제 #6
0
def test_test_module_404(requests_mock):
    """
    Given:
        - the IP reputation feed

    When:
        - running test-module with a 404

    Then:
        - it tells you the test failed

    """

    base_url = "https://cyren.feed/"
    requests_mock.get(base_url + "data?format=jsonl&feedId=ip_reputation&offset=0&count=10", status_code=404)
    client = Client(feed_name="ip_reputation", base_url=base_url, verify=False, proxy=False)

    assert "Test failed because of an invalid API URL!" in _test_module_command(client)
예제 #7
0
def test_test_module_server_error(requests_mock):
    """
    Given:
        - the IP reputation feed

    When:
        - running test-module with a 500 Server Error

    Then:
        - it tells you the test failed

    """

    base_url = "https://cyren.feed/"
    requests_mock.get(base_url + "data?format=jsonl&feedId=ip_reputation&offset=0&count=10", status_code=500)
    client = Client(feed_name="ip_reputation", base_url=base_url, verify=False, proxy=False)

    assert "Test failed because of: Error in API call [500] - None" in _test_module_command(client)
예제 #8
0
def test_test_module_other_400(requests_mock):
    """
    Given:
        - the IP reputation feed

    When:
        - running test-module with an unknown 400

    Then:
        - it tells you the test failed

    """

    requests_mock.get(
        BASE_URL + "/data?format=jsonl&feedId=ip_reputation&offset=0&count=10",
        status_code=400)
    client = Client(feed_name="ip_reputation",
                    base_url=BASE_URL,
                    verify=False,
                    proxy=False)

    assert "Test failed because of: 400 Client Error:" in _test_module_command(
        client)
def _create_client(feed):
    return Client(feed_name=feed, api_token=API_TOKEN, base_url=BASE_URL, verify=False, proxy=False)