Skip to content

Commit

Permalink
Improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
plietar committed Nov 6, 2024
1 parent 2d9ad26 commit a37a2d8
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/pyorderly/outpack/location_packit.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def poll_access_token(
parameters: DeviceAuthorizationResponse,
) -> AccessTokenResponse:
interval = parameters.interval
if parameters.interval is None:
if parameters.interval is None: # pragma: no cover
interval = 5
else:
interval = parameters.interval
Expand All @@ -142,7 +142,7 @@ def poll_access_token(
return response
elif response.error == "authorization_pending":
pass
elif response.error == "slow_down":
elif response.error == "slow_down": # pragma: no cover
interval += 5
else:
if response.error_description is not None:
Expand Down
1 change: 1 addition & 0 deletions src/pyorderly/outpack/static.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
LOCATION_ORPHAN,
"path",
"http",
"packit",
"custom",
"ssh",
]
32 changes: 31 additions & 1 deletion tests/outpack/test_location_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

import pytest
import requests
import responses
from requests import HTTPError

from pyorderly.outpack.hash import hash_file
from pyorderly.outpack.location import outpack_location_add
from pyorderly.outpack.location_http import OutpackLocationHTTP
from pyorderly.outpack.location_http import (
OutpackHTTPClient,
OutpackLocationHTTP,
)
from pyorderly.outpack.location_pull import (
outpack_location_pull_metadata,
outpack_location_pull_packet,
Expand Down Expand Up @@ -165,3 +170,28 @@ def test_can_pull_packet(tmp_path):
assert id not in root["dst"].index.unpacked()
outpack_location_pull_packet(id, root=root["dst"])
assert id in root["dst"].index.unpacked()


@responses.activate
def test_http_client_errors():
responses.get(
"https://example.com/text-error", status=400, body="Request failed"
)
responses.get(
"https://example.com/packit-error",
status=400,
json={"error": {"detail": "Custom error message"}},
)
responses.get(
"https://example.com/outpack-error",
status=400,
json={"errors": [{"detail": "Custom error message"}]},
)

client = OutpackHTTPClient("https://example.com")
with pytest.raises(HTTPError, match="400 Client Error: Bad Request"):
client.get("/text-error")
with pytest.raises(HTTPError, match="400 Error: Custom error message"):
client.get("/packit-error")
with pytest.raises(HTTPError, match="400 Error: Custom error message"):
client.get("/outpack-error")
68 changes: 63 additions & 5 deletions tests/outpack/test_location_packit.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import pytest
import re
import responses
from responses import matchers
from responses.registries import OrderedRegistry

from pyorderly.outpack.location_pull import outpack_location_pull_metadata
from pyorderly.outpack.location import outpack_location_add
from pyorderly.outpack.location_packit import (
GITHUB_ACCESS_TOKEN_URL,
GITHUB_CLIENT_ID,
GITHUB_DEVICE_CODE_URL,
OAuthDeviceClient,
outpack_location_packit,
packit_authorisation,
)

from ..helpers import create_temporary_root


# This fixture automatically gets invoked by all tests in the file, and will
# clear the authorisation cache to avoid carrying tokens over.
Expand Down Expand Up @@ -139,7 +145,7 @@ def test_can_perform_interactive_authentication(capsys):


@responses.activate(assert_all_requests_are_fired=True)
def test_failed_authentication():
def test_oauth_failed_authentication():
responses.post(
GITHUB_DEVICE_CODE_URL,
json={
Expand All @@ -153,8 +159,60 @@ def test_failed_authentication():

responses.post(GITHUB_ACCESS_TOKEN_URL, json={"error": "access_denied"})

location = outpack_location_packit("https://example.com")
with OAuthDeviceClient(
GITHUB_CLIENT_ID,
GITHUB_DEVICE_CODE_URL,
GITHUB_ACCESS_TOKEN_URL,
) as client:
msg = "Error while fetching access token: access_denied"
with pytest.raises(Exception, match=msg):
client.authenticate("read:org")


@responses.activate(assert_all_requests_are_fired=True)
def test_oauth_failed_authentication_with_description():
responses.post(
GITHUB_DEVICE_CODE_URL,
json={
"device_code": "xxxxx",
"user_code": "1234-5678",
"verification_uri": "https://github.com/login/device",
"expires_in": 3600,
"interval": 0,
},
)

msg = "Error while fetching access token: access_denied"
with pytest.raises(Exception, match=msg):
location.list()
responses.post(
GITHUB_ACCESS_TOKEN_URL,
json={
"error": "access_denied",
"error_description": "Access was denied",
},
)

with OAuthDeviceClient(
GITHUB_CLIENT_ID,
GITHUB_DEVICE_CODE_URL,
GITHUB_ACCESS_TOKEN_URL,
) as client:
msg = "Error while fetching access token: Access was denied (access_denied)"
with pytest.raises(Exception, match=re.escape(msg)):
client.authenticate("read:org")


@responses.activate(assert_all_requests_are_fired=True)
def test_can_add_packit_location(tmp_path):
responses.get(
"https://example.com/packit/api/outpack/metadata/list",
json={"status": "success", "data": []},
match=[matchers.header_matcher({"Authorization": "Bearer mytoken"})],
)

root = create_temporary_root(tmp_path)
outpack_location_add(
"upstream",
"packit",
{"url": "https://example.com", "token": "mytoken"},
root,
)
outpack_location_pull_metadata("upstream", root=root)

0 comments on commit a37a2d8

Please sign in to comment.