Skip to content

Commit 40a2edf

Browse files
committed
test handlers green
1 parent 1b2c87c commit 40a2edf

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

services/storage/tests/unit/test_utils_handlers.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
import pytest
1212
from asyncpg import PostgresError
1313
from aws_library.s3._errors import S3AccessError, S3KeyNotFoundError
14-
from fastapi import FastAPI, status
14+
from fastapi import FastAPI, HTTPException, status
15+
from fastapi.exceptions import RequestValidationError
1516
from httpx import AsyncClient
1617
from simcore_service_storage.api.rest.utils import set_exception_handlers
1718
from simcore_service_storage.exceptions.errors import (
@@ -21,6 +22,9 @@
2122
ProjectAccessRightError,
2223
ProjectNotFoundError,
2324
)
25+
from simcore_service_storage.modules.datcore_adapter.datcore_adapter_exceptions import (
26+
DatcoreAdapterTimeoutError,
27+
)
2428
from simcore_service_storage.modules.db.db_access_layer import (
2529
InvalidFileIdentifierError,
2630
)
@@ -88,6 +92,14 @@ async def client(initialized_app: FastAPI) -> AsyncIterator[AsyncClient]:
8892
S3AccessError(),
8993
status.HTTP_503_SERVICE_UNAVAILABLE,
9094
),
95+
(
96+
DatcoreAdapterTimeoutError(msg="pytest datcore adapter timeout"),
97+
status.HTTP_504_GATEWAY_TIMEOUT,
98+
),
99+
(
100+
NotImplementedError("pytest not implemented error"),
101+
status.HTTP_501_NOT_IMPLEMENTED,
102+
),
91103
],
92104
ids=str,
93105
)
@@ -104,3 +116,41 @@ async def test_endpoint():
104116
response = await client.get("/test")
105117
assert response.status_code == status_code
106118
assert response.json() == {"errors": [f"{exception}"]}
119+
120+
121+
async def test_generic_http_exception_handler(
122+
initialized_app: FastAPI, client: AsyncClient
123+
):
124+
@initialized_app.get("/test")
125+
async def test_endpoint():
126+
raise HTTPException(status_code=status.HTTP_410_GONE)
127+
128+
response = await client.get("/test")
129+
assert response.status_code == status.HTTP_410_GONE
130+
assert response.json() == {"errors": ["Gone"]}
131+
132+
133+
async def test_request_validation_error_handler(
134+
initialized_app: FastAPI, client: AsyncClient
135+
):
136+
@initialized_app.get("/test")
137+
async def test_endpoint():
138+
raise RequestValidationError(errors=["pytest request validation error"])
139+
140+
response = await client.get("/test")
141+
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
142+
assert response.json() == {"errors": ["pytest request validation error"]}
143+
144+
145+
@pytest.mark.xfail(
146+
reason="Generic exception handler is not working as expected as shown in https://github.com/ITISFoundation/osparc-simcore/blob/5732a12e07e63d5ce55010ede9b9ab543bb9b278/packages/service-library/tests/fastapi/test_exceptions_utils.py"
147+
)
148+
async def test_generic_exception_handler(initialized_app: FastAPI, client: AsyncClient):
149+
@initialized_app.get("/test")
150+
async def test_endpoint():
151+
msg = "Generic exception"
152+
raise Exception(msg)
153+
154+
response = await client.get("/test")
155+
assert response.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR
156+
assert response.json() == {"detail": "Generic exception"}

0 commit comments

Comments
 (0)