Skip to content

Commit

Permalink
Tweaked and tested send_request method.
Browse files Browse the repository at this point in the history
For the method to be useful outside this git package,
the authorization should be optional.

Fixed the error message in the method, which used
an uninitialised variable.
  • Loading branch information
mgcam committed Jul 8, 2024
1 parent 6e1d71b commit fe6a866
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/npg_porch_cli/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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}"
)
Expand Down
73 changes: 73 additions & 0 deletions tests/test_send_request.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit fe6a866

Please sign in to comment.