mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-05 07:25:08 +00:00
fix(remote): stop with one line when a remote server rejects the API key instead of building the stub command
This commit is contained in:
@@ -14,7 +14,7 @@ import requests
|
||||
import responses
|
||||
|
||||
from unshackle.core.config import config
|
||||
from unshackle.core.remote_protocol import ClientErrorKind
|
||||
from unshackle.core.remote_protocol import APIErrorCode, ClientErrorKind
|
||||
from unshackle.core.remote_service import RemoteClient, RemoteError
|
||||
from unshackle.core.utilities import close_debug_logger, init_debug_logger
|
||||
|
||||
@@ -445,3 +445,41 @@ def test_any_other_requests_failure_is_one_remote_error(client: RemoteClient) ->
|
||||
assert e.value.kind == ClientErrorKind.UNKNOWN
|
||||
assert e.value.message == "Request to remote server failed on /api/session/s/titles: TooManyRedirects"
|
||||
assert not e.value.session_broke
|
||||
|
||||
|
||||
@responses.activate
|
||||
@pytest.mark.parametrize(
|
||||
("status", "body"),
|
||||
[(401, {"error": "unauthorized"}), (403, {"error": "unauthorized"}), (401, {"message": "Secret Key is Invalid."})],
|
||||
)
|
||||
def test_a_401_or_json_403_without_a_code_is_a_rejected_key(client: RemoteClient, status: int, body: dict) -> None:
|
||||
responses.add(responses.GET, "http://srv:8786/api/services", json=body, status=status)
|
||||
|
||||
with pytest.raises(RemoteError) as e:
|
||||
client.get("/api/services")
|
||||
|
||||
assert e.value.kind == APIErrorCode.AUTH_FAILED
|
||||
assert e.value.message == f"Remote server http://srv:8786 rejected the API key (HTTP {status})"
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_a_403_page_that_is_not_json_is_not_a_rejected_key(client: RemoteClient) -> None:
|
||||
page = "<html><head><title>Access denied | srv.example</title></head></html>"
|
||||
responses.add(responses.GET, "http://srv:8786/api/services", body=page, status=403, content_type="text/html")
|
||||
|
||||
with pytest.raises(RemoteError) as e:
|
||||
client.get("/api/services")
|
||||
|
||||
assert e.value.kind != APIErrorCode.AUTH_FAILED
|
||||
assert "Access denied" in e.value.message
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_a_serve_403_keeps_its_own_error_code(client: RemoteClient) -> None:
|
||||
body = {"error_code": "FORBIDDEN", "message": "Key not allowed"}
|
||||
responses.add(responses.GET, "http://srv:8786/api/services", json=body, status=403)
|
||||
|
||||
with pytest.raises(RemoteError) as e:
|
||||
client.get("/api/services")
|
||||
|
||||
assert e.value.kind == APIErrorCode.FORBIDDEN
|
||||
|
||||
Reference in New Issue
Block a user