Ejemplo n.º 1
0
def test_access_denied_unknown_error(denial_status_code, session_mock, token):
    """If a msg field is not available for an access denied a generic message is supplied."""
    session_mock.post.return_value.status_code = denial_status_code
    session_mock.post.return_value.json.side_effect = ValueError
    connection = flowclient.connect(url="DUMMY_API", token=token)
    with pytest.raises(FlowclientConnectionError, match="Unknown access denied error"):
        connection.post_json(route="DUMMY_ROUTE", data={})
Ejemplo n.º 2
0
def test_post_json_good_statuses(status_code, session_mock, token):
    """response object should be returned for OK status codes.."""
    session_mock.post.return_value.status_code = status_code
    connection = flowclient.connect(url="DUMMY_API", token=token)
    assert session_mock.post.return_value == connection.post_json(
        route="DUMMY_ROUTE", data={}
    )
Ejemplo n.º 3
0
def test_access_denied_error(denial_status_code, session_mock, token):
    """If a msg field is available for an access denied it should be used as the error message."""
    session_mock.get.return_value.status_code = denial_status_code
    session_mock.get.return_value.json.return_value = {"msg": "ERROR_MESSAGE"}
    connection = flowclient.connect(url="DUMMY_API", token=token)
    with pytest.raises(FlowclientConnectionError, match="ERROR_MESSAGE"):
        connection.get_url(route="DUMMY_ROUTE")
Ejemplo n.º 4
0
def test_update_token_raises(session_mock, token, new_token):
    """
    Test that update_token raises an error if the token is not valid or does not contain an identity.
    """
    connection = flowclient.connect(url="DUMMY_API", token=token)
    with pytest.raises(FlowclientConnectionError):
        connection.update_token(new_token)
Ejemplo n.º 5
0
def test_401_error(session_mock, token):
    """If a msg field is available for a 401, it should be used as the error message."""
    session_mock.post.return_value.status_code = 401
    session_mock.post.return_value.json.return_value = {"msg": "ERROR_MESSAGE"}
    connection = flowclient.connect("DUMMY_API", token)
    with pytest.raises(FlowclientConnectionError, match="ERROR_MESSAGE"):
        connection.post_json("DUMMY_ROUTE", {})
Ejemplo n.º 6
0
def test_404_raises_error(session_mock, token):
    """Exception should be raised for a 404 response."""
    session_mock.post.return_value.status_code = 404
    connection = flowclient.connect(url="DUMMY_API", token=token)
    with pytest.raises(FileNotFoundError,
                       match="DUMMY_API/api/0/DUMMY_ROUTE not found."):
        connection.post_json(route="DUMMY_ROUTE", data={})
Ejemplo n.º 7
0
def test_access_denied_error(denial_status_code, session_mock, token):
    """If a msg field is not available for an access denied a generic error should be used."""
    session_mock.get.return_value.status_code = denial_status_code
    session_mock.get.return_value.json.return_value = {}
    connection = flowclient.connect(url="DUMMY_API", token=token)
    with pytest.raises(FlowclientConnectionError,
                       match="Unknown access denied error"):
        connection.get_url(route="DUMMY_ROUTE")
Ejemplo n.º 8
0
def test_401_error(session_mock, token):
    """If a msg field is available for a 401, it should be used as the error message."""
    session_mock.post.return_value.status_code = 401
    session_mock.post.return_value.json.return_value = ZMQReply(
        status="error", msg="ERROR_MESSAGE").as_json()
    connection = flowclient.connect(url="DUMMY_API", token=token)
    with pytest.raises(FlowclientConnectionError, match="ERROR_MESSAGE"):
        connection.post_json(route="DUMMY_ROUTE", data={})
Ejemplo n.º 9
0
def test_401_unknown_error(session_mock, token):
    """If a msg field is not available for a 401, a generic message is used."""
    session_mock.get.return_value.status_code = 401
    session_mock.get.return_value.json.return_value = {}
    connection = flowclient.connect("DUMMY_API", token)
    with pytest.raises(FlowclientConnectionError,
                       match="Unknown access denied error"):
        connection.get_url("DUMMY_ROUTE")
Ejemplo n.º 10
0
def test_generic_status_code_no_payload(session_mock, token):
    """An error should be raised for status codes that aren't expected, with a blank payload if not given."""
    session_mock.post.return_value.status_code = 418
    session_mock.post.return_value.json.return_value = dict(msg="DUMMY_ERROR")
    connection = flowclient.connect(url="DUMMY_API", token=token)
    with pytest.raises(
        FlowclientConnectionError,
        match="Something went wrong. API returned with status code 418. Error message: 'DUMMY_ERROR'.",
    ):
        connection.post_json(route="DUMMY_ROUTE", data={})
Ejemplo n.º 11
0
def test_401_unknown_error(session_mock, token):
    """If a msg field is not available for a 401, a generic message is used."""
    session_mock.get.return_value.status_code = 401
    session_mock.get.return_value.json.return_value = ZMQReply(
        status="error", msg="Unknown access denied error",
        payload={}).as_json()
    connection = flowclient.connect(url="DUMMY_API", token=token)
    with pytest.raises(FlowclientConnectionError,
                       match="Unknown access denied error"):
        connection.get_url(route="DUMMY_ROUTE")
Ejemplo n.º 12
0
def test_update_token(session_mock, token):
    """
    Test that update_token updates all attributes appropriately.
    """
    connection = flowclient.connect(url="DUMMY_API", token=token)
    new_token = jwt.encode({"identity": "NEW_IDENTITY"}, "secret")
    connection.update_token(new_token)
    assert connection.user == "NEW_IDENTITY"
    assert connection.token == new_token
    assert session_mock.headers["Authorization"] == f"Bearer {new_token}"
Ejemplo n.º 13
0
def test_generic_status_code_unknown_error(json_failure, session_mock, token):
    """An error should be raised for status codes that aren't expected, with a default error message if not given."""
    session_mock.get.return_value.status_code = 418
    session_mock.get.return_value.json.side_effect = json_failure
    connection = flowclient.connect(url="DUMMY_API", token=token)
    with pytest.raises(
            FlowclientConnectionError,
            match=
            "Something went wrong: Unknown error. API returned with status code: 418 and status 'Unknown status'",
    ):
        connection.get_url(route="DUMMY_ROUTE")
Ejemplo n.º 14
0
def test_generic_status_code_error(session_mock, token):
    """An error should be raised for status codes that aren't expected."""
    session_mock.get.return_value.status_code = 418
    session_mock.get.return_value.json.return_value = {"msg": "I AM A TEAPOT"}
    connection = flowclient.connect(url="DUMMY_API", token=token)
    with pytest.raises(
            FlowclientConnectionError,
            match=
            "Something went wrong: I AM A TEAPOT. API returned with status code: 418",
    ):
        connection.get_url(route="DUMMY_ROUTE")
Ejemplo n.º 15
0
def test_generic_status_code_unknown_error(session_mock, token):
    """An error should be raised for status codes that aren't expected, with a default error message if not given."""
    session_mock.post.return_value.status_code = 418
    session_mock.post.return_value.json.side_effect = (
        ValueError  # indicated that the resonse body doesn't contain valid JSON
    )
    connection = flowclient.connect(url="DUMMY_API", token=token)
    with pytest.raises(
        FlowclientConnectionError,
        match="Something went wrong. API returned with status code 418. Error message: 'the response did not contain valid JSON'.",
    ):
        connection.post_json(route="DUMMY_ROUTE", data={})
Ejemplo n.º 16
0
def test_generic_status_code_error(session_mock, token):
    """An error should be raised for status codes that aren't expected."""
    session_mock.post.return_value.status_code = 418
    session_mock.post.return_value.json.return_value = ZMQReply(
        status="error", msg="I AM A TEAPOT").as_json()
    connection = flowclient.connect(url="DUMMY_API", token=token)
    with pytest.raises(
            FlowclientConnectionError,
            match=
            "Something went wrong: I AM A TEAPOT. API returned with status code: 418",
    ):
        connection.post_json(route="DUMMY_ROUTE", data={})
Ejemplo n.º 17
0
def get_available_dates(
    cdr_types: Optional[Sequence[str]] = None, ) -> List[pendulum.Date]:
    """
    Task to return a union of the dates for which data is available in FlowDB for the specified set of CDR types.

    Parameters
    ----------
    cdr_types : list of str, optional
        Subset of CDR types for which to find available dates.
        If not provided, the union of available dates for all CDR types will be returned.

    Returns
    -------
    list of pendulum.Date
        List of available dates, in chronological order
    """
    prefect.context.logger.info(
        f"Getting available dates from FlowAPI at '{prefect.config.flowapi_url}'."
    )
    conn = flowclient.connect(
        url=prefect.config.flowapi_url,
        token=environ["FLOWAPI_TOKEN"],
        ssl_certificate=getenv("SSL_CERTIFICATE_FILE"),
    )
    dates = flowclient.get_available_dates(connection=conn)
    prefect.context.logger.debug(f"Available dates: {dates}")
    if cdr_types is None:
        prefect.context.logger.debug(
            "No CDR types provided. Will return available dates for all CDR types."
        )
        cdr_types = dates.keys()
    else:
        prefect.context.logger.debug(
            f"Returning available dates for CDR types {cdr_types}.")
        unknown_cdr_types = set(cdr_types).difference(dates.keys())
        if unknown_cdr_types:
            warnings.warn(
                f"No data available for CDR types {unknown_cdr_types}.")
    dates_union = set.union(*[
        set(pendulum.parse(date, exact=True) for date in dates[cdr_type])
        for cdr_type in cdr_types if cdr_type in dates.keys()
    ])
    return sorted(list(dates_union))
Ejemplo n.º 18
0
def flowclient_connection(session_mock, dummy_route, dummy_api_url, token):
    yield flowclient.connect(url=dummy_api_url, token=token)
Ejemplo n.º 19
0
def test_get_url_reraises(session_mock, token):
    """get_url should reraise anything raises by requests."""
    session_mock.get.side_effect = ConnectionError("DUMMY_MESSAGE")
    connection = flowclient.connect(url="DUMMY_API", token=token)
    with pytest.raises(FlowclientConnectionError, match="DUMMY_MESSAGE"):
        connection.get_url(route="DUMMY_ROUTE")
Ejemplo n.º 20
0
def test_get_url_good_statuses(status_code, session_mock, token):
    """response object should be returned for OK status codes.."""
    session_mock.get.return_value.status_code = status_code
    connection = flowclient.connect("DUMMY_API", token)
    assert session_mock.get.return_value == connection.get_url("DUMMY_ROUTE")
Ejemplo n.º 21
0
def test_post_json_reraises(session_mock, token):
    """post_json should reraise anything raised by requests."""
    session_mock.post.side_effect = ConnectionError("DUMMY_MESSAGE")
    connection = flowclient.connect(url="DUMMY_API", token=token)
    with pytest.raises(FlowclientConnectionError, match="DUMMY_MESSAGE"):
        connection.post_json(route="DUMMY_ROUTE", data={})