diff --git a/src/npg_porch_cli/api.py b/src/npg_porch_cli/api.py index f46611d..f5927e3 100644 --- a/src/npg_porch_cli/api.py +++ b/src/npg_porch_cli/api.py @@ -251,14 +251,25 @@ def update_task(action: PorchAction, pipeline: Pipeline): } -def send_request(validate_ca_cert: bool, url: str, method: str, data: dict = None): +def send_request( + validate_ca_cert: bool, + url: str, + method: str, + data: dict = None, + auth_type: str | None = "token", +): """Sends an HTTPS request.""" headers = { - "Authorization": "Bearer " + get_token(), "Content-Type": "application/json", "Accept": "application/json", } + if auth_type is not None: + if auth_type == "token": + headers["Authorization"] = "Bearer " + get_token() + else: + raise ValueError(f"Authorization type {auth_type} is not implemented") + request_args = { "headers": headers, "timeout": CLIENT_TIMEOUT, @@ -271,7 +282,6 @@ def send_request(validate_ca_cert: bool, url: str, method: str, data: dict = Non if not response.ok: action_name = inspect.stack()[1].function raise ServerErrorException( - f"Action {action_name} failed. " f'Status code {response.status_code} "{response.reason}" ' f"received from {response.url}" ) diff --git a/tests/test_send_request.py b/tests/test_send_request.py new file mode 100644 index 0000000..70e4d82 --- /dev/null +++ b/tests/test_send_request.py @@ -0,0 +1,73 @@ +import pytest +import requests + +from npg_porch_cli import send_request +from npg_porch_cli.api import AuthException, ServerErrorException + +url = "http://some.com" +var_name = "NPG_PORCH_TOKEN" +json_data = {"some_data": "delivered"} + + +class MockResponseOK: + def __init__(self): + self.status_code = 200 + self.reason = "OK" + self.url = url + self.ok = True + + def json(self): + return json_data + + +class MockResponseNotFound: + def __init__(self): + self.status_code = 404 + self.reason = "NOT FOUND" + self.url = url + self.ok = False + + def json(self): + return {"Error": "Not found"} + + +def mock_get_200(*args, **kwargs): + return MockResponseOK() + + +def mock_get_404(*args, **kwargs): + return MockResponseNotFound() + + +def test_sending_request(monkeypatch): + + monkeypatch.delenv(var_name, raising=False) + + with pytest.raises(ValueError) as e: + send_request(validate_ca_cert=True, url=url, method="GET", auth_type="unknown") + assert e.value.args[0] == "Authorization type unknown is not implemented" + + with pytest.raises(AuthException) as e: + send_request(validate_ca_cert=True, url=url, method="GET") + assert e.value.args[0] == "Authorization token is needed" + + with monkeypatch.context() as m: + m.setattr(requests, "request", mock_get_200) + assert ( + send_request(validate_ca_cert=True, url=url, method="GET", auth_type=None) + == json_data + ) + + monkeypatch.setenv(var_name, "token_xyz") + + with monkeypatch.context() as m: + m.setattr(requests, "request", mock_get_200) + assert send_request(validate_ca_cert=False, url=url, method="GET") == json_data + + with monkeypatch.context() as m: + m.setattr(requests, "request", mock_get_404) + with pytest.raises(ServerErrorException) as e: + send_request(validate_ca_cert=False, url=url, method="POST", data=json_data) + assert e.value.args[0] == f'Status code 404 "NOT FOUND" received from {url}' + + monkeypatch.undo()