APIForbidden should result in 403 status (#4943)

This commit is contained in:
Mike Degatano 2024-03-04 11:09:17 -05:00 committed by GitHub
parent bb5e138134
commit 2c7b417e25
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 11 additions and 4 deletions

View File

@ -129,12 +129,15 @@ def api_return_error(
JSON_RESULT: RESULT_ERROR, JSON_RESULT: RESULT_ERROR,
JSON_MESSAGE: message or "Unknown error, see supervisor", JSON_MESSAGE: message or "Unknown error, see supervisor",
} }
if isinstance(error, APIError) and error.job_id: status = 400
if isinstance(error, APIError):
status = error.status
if error.job_id:
result[JSON_JOB_ID] = error.job_id result[JSON_JOB_ID] = error.job_id
return web.json_response( return web.json_response(
result, result,
status=400, status=status,
dumps=json_dumps, dumps=json_dumps,
) )

View File

@ -308,6 +308,8 @@ class HostLogError(HostError):
class APIError(HassioError, RuntimeError): class APIError(HassioError, RuntimeError):
"""API errors.""" """API errors."""
status = 400
def __init__( def __init__(
self, self,
message: str | None = None, message: str | None = None,
@ -322,6 +324,8 @@ class APIError(HassioError, RuntimeError):
class APIForbidden(APIError): class APIForbidden(APIError):
"""API forbidden error.""" """API forbidden error."""
status = 403
class APIAddonNotInstalled(APIError): class APIAddonNotInstalled(APIError):
"""Not installed addon requested at addons API.""" """Not installed addon requested at addons API."""

View File

@ -25,7 +25,7 @@ async def test_api_discovery_forbidden(
with caplog.at_level(logging.ERROR): with caplog.at_level(logging.ERROR):
resp = await api_client.post("/discovery", json={"service": "mqtt"}) resp = await api_client.post("/discovery", json={"service": "mqtt"})
assert resp.status == 400 assert resp.status == 403
result = await resp.json() result = await resp.json()
assert result["result"] == "error" assert result["result"] == "error"
assert ( assert (