From 29b465716c78e32fcf0ea187d331af8b65ff6dc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janek=20Nouvertn=C3=A9?= <25355197+provinzkraut@users.noreply.github.com> Date: Wed, 21 Jun 2023 09:54:31 +0200 Subject: [PATCH 1/2] Add missing marker to SQLA oracle JSON tests (#1855) * Add missing marker * formatting --- .../test_repository/test_sqlalchemy_oracledb_json.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/unit/test_contrib/test_sqlalchemy/test_repository/test_sqlalchemy_oracledb_json.py b/tests/unit/test_contrib/test_sqlalchemy/test_repository/test_sqlalchemy_oracledb_json.py index af5f87acad..35432580d6 100644 --- a/tests/unit/test_contrib/test_sqlalchemy/test_repository/test_sqlalchemy_oracledb_json.py +++ b/tests/unit/test_contrib/test_sqlalchemy/test_repository/test_sqlalchemy_oracledb_json.py @@ -12,6 +12,7 @@ pytestmark = [ pytest.mark.skipif(platform.uname()[4] != "x86_64", reason="oracle not available on this platform"), + pytest.mark.sqlalchemy_integration, ] From a8498fdca41f54df33721c802e48e78a2f6b472a Mon Sep 17 00:00:00 2001 From: Peter Schutt Date: Wed, 21 Jun 2023 19:25:43 +1000 Subject: [PATCH 2/2] Fix: Internal Server Error detail (#1857) --- litestar/middleware/exceptions/middleware.py | 10 ++++++-- tests/examples/test_dto/test_example_apps.py | 1 - tests/examples/test_dto/test_tutorial.py | 1 - tests/unit/test_exceptions.py | 2 +- .../test_generator_dependencies.py | 4 ++-- .../test_base_authentication_middleware.py | 2 -- .../test_exception_handler_middleware.py | 24 ++++++++++++------- .../test_session/test_middleware.py | 2 +- tests/unit/test_signature/test_parsing.py | 2 +- tests/unit/test_template/test_built_in.py | 2 +- tests/unit/test_template/test_template.py | 2 +- 11 files changed, 30 insertions(+), 22 deletions(-) diff --git a/litestar/middleware/exceptions/middleware.py b/litestar/middleware/exceptions/middleware.py index 3f1ca7b659..06560eed9b 100644 --- a/litestar/middleware/exceptions/middleware.py +++ b/litestar/middleware/exceptions/middleware.py @@ -109,9 +109,15 @@ def create_exception_response(exc: Exception) -> Response: Returns: Response: HTTP response constructed from exception details. """ + status_code = getattr(exc, "status_code", HTTP_500_INTERNAL_SERVER_ERROR) + if status_code == HTTP_500_INTERNAL_SERVER_ERROR: + detail = "Internal Server Error" + else: + detail = getattr(exc, "detail", repr(exc)) + content = ExceptionResponseContent( - status_code=getattr(exc, "status_code", HTTP_500_INTERNAL_SERVER_ERROR), - detail=getattr(exc, "detail", repr(exc)), + status_code=status_code, + detail=detail, headers=getattr(exc, "headers", None), extra=getattr(exc, "extra", None), ) diff --git a/tests/examples/test_dto/test_example_apps.py b/tests/examples/test_dto/test_example_apps.py index ed202dea7e..6f699a6070 100644 --- a/tests/examples/test_dto/test_example_apps.py +++ b/tests/examples/test_dto/test_example_apps.py @@ -11,7 +11,6 @@ def test_dto_data_problem_statement_app() -> None: with TestClient(app) as client: response = client.post("/person", json={"name": "John", "age": 30}) assert response.status_code == 500 - assert "missing 1 required positional argument: 'id'" in response.json()["detail"] def test_dto_data_usage_app() -> None: diff --git a/tests/examples/test_dto/test_tutorial.py b/tests/examples/test_dto/test_tutorial.py index 64aebf9c06..541ba2e873 100644 --- a/tests/examples/test_dto/test_tutorial.py +++ b/tests/examples/test_dto/test_tutorial.py @@ -102,7 +102,6 @@ def test_read_only_fields(): response = client.post("/person", json={"name": "peter", "age": 40, "email": "email_of_peter@example.com"}) assert response.status_code == 500 - assert "__init__() missing 1 required positional argument: 'id'" in response.json()["detail"] def test_dto_data(): diff --git a/tests/unit/test_exceptions.py b/tests/unit/test_exceptions.py index ce08ee7871..5fe95734ee 100644 --- a/tests/unit/test_exceptions.py +++ b/tests/unit/test_exceptions.py @@ -87,7 +87,7 @@ def test_create_exception_response_utility_non_http_exception() -> None: response = create_exception_response(exc) assert response.status_code == HTTP_500_INTERNAL_SERVER_ERROR assert response.media_type == MediaType.JSON - assert response.content == {"status_code": 500, "detail": "RuntimeError('yikes')"} + assert response.content == {"status_code": 500, "detail": "Internal Server Error"} def test_missing_dependency_exception() -> None: diff --git a/tests/unit/test_kwargs/test_generator_dependencies.py b/tests/unit/test_kwargs/test_generator_dependencies.py index 8b7e0d3d9d..ed9a3b62ed 100644 --- a/tests/unit/test_kwargs/test_generator_dependencies.py +++ b/tests/unit/test_kwargs/test_generator_dependencies.py @@ -120,7 +120,7 @@ def handler(dep: str) -> Dict[str, str]: with create_test_client(route_handlers=[handler]) as client: res = client.get("/") assert res.status_code == 500 - assert res.json() == {"detail": "ValueError('foo')", "status_code": 500} + assert res.json() == {"detail": "Internal Server Error", "status_code": 500} cleanup_mock.assert_not_called() exception_mock.assert_called_once() finally_mock.assert_called_once() @@ -144,7 +144,7 @@ def handler(dep: str) -> Dict[str, str]: with create_test_client(route_handlers=[handler]) as client: res = client.get("/") assert res.status_code == 500 - assert res.json() == {"status_code": 500, "detail": "Exception('foo')"} + assert res.json() == {"status_code": 500, "detail": "Internal Server Error"} cleanup_mock.assert_called_once() finally_mock.assert_called_once() diff --git a/tests/unit/test_middleware/test_base_authentication_middleware.py b/tests/unit/test_middleware/test_base_authentication_middleware.py index 633e8ffe0d..60a5a81a04 100644 --- a/tests/unit/test_middleware/test_base_authentication_middleware.py +++ b/tests/unit/test_middleware/test_base_authentication_middleware.py @@ -72,7 +72,6 @@ def http_route_handler_user_scope(request: Request[User, None, Any]) -> None: client = create_test_client(route_handlers=[http_route_handler_user_scope]) error_response = client.get("/", headers={"Authorization": "nope"}) assert error_response.status_code == HTTP_500_INTERNAL_SERVER_ERROR - assert error_response.json()["detail"] == "'user' is not defined in scope, install an AuthMiddleware to set it" def test_authentication_middleware_not_installed_raises_for_auth_scope_http() -> None: @@ -83,7 +82,6 @@ def http_route_handler_auth_scope(request: Request[None, Auth, Any]) -> None: client = create_test_client(route_handlers=[http_route_handler_auth_scope]) error_response = client.get("/", headers={"Authorization": "nope"}) assert error_response.status_code == HTTP_500_INTERNAL_SERVER_ERROR - assert error_response.json()["detail"] == "'auth' is not defined in scope, install an AuthMiddleware to set it" @websocket(path="/") diff --git a/tests/unit/test_middleware/test_exception_handler_middleware.py b/tests/unit/test_middleware/test_exception_handler_middleware.py index 1454347fc9..88aabb5553 100644 --- a/tests/unit/test_middleware/test_exception_handler_middleware.py +++ b/tests/unit/test_middleware/test_exception_handler_middleware.py @@ -40,7 +40,7 @@ def test_default_handle_http_exception_handling_extra_object() -> None: ) assert response.status_code == HTTP_500_INTERNAL_SERVER_ERROR assert response.content == { - "detail": "litestar_exception", + "detail": "Internal Server Error", "extra": {"key": "value"}, "status_code": 500, } @@ -52,7 +52,7 @@ def test_default_handle_http_exception_handling_extra_none() -> None: HTTPException(detail="litestar_exception"), ) assert response.status_code == HTTP_500_INTERNAL_SERVER_ERROR - assert response.content == {"detail": "litestar_exception", "status_code": 500} + assert response.content == {"detail": "Internal Server Error", "status_code": 500} def test_default_handle_litestar_http_exception_handling() -> None: @@ -61,7 +61,7 @@ def test_default_handle_litestar_http_exception_handling() -> None: HTTPException(detail="litestar_exception"), ) assert response.status_code == HTTP_500_INTERNAL_SERVER_ERROR - assert response.content == {"detail": "litestar_exception", "status_code": 500} + assert response.content == {"detail": "Internal Server Error", "status_code": 500} def test_default_handle_litestar_http_exception_extra_list() -> None: @@ -71,7 +71,7 @@ def test_default_handle_litestar_http_exception_extra_list() -> None: ) assert response.status_code == HTTP_500_INTERNAL_SERVER_ERROR assert response.content == { - "detail": "litestar_exception", + "detail": "Internal Server Error", "extra": ["extra-1", "extra-2"], "status_code": 500, } @@ -84,7 +84,7 @@ def test_default_handle_starlette_http_exception_handling() -> None: ) assert response.status_code == HTTP_500_INTERNAL_SERVER_ERROR assert response.content == { - "detail": "litestar_exception", + "detail": "Internal Server Error", "status_code": 500, } @@ -95,7 +95,7 @@ def test_default_handle_python_http_exception_handling() -> None: ) assert response.status_code == HTTP_500_INTERNAL_SERVER_ERROR assert response.content == { - "detail": repr(AttributeError("oops")), + "detail": "Internal Server Error", "status_code": HTTP_500_INTERNAL_SERVER_ERROR, } @@ -164,7 +164,10 @@ def handler() -> None: client.app.logger = get_logger("litestar") response = client.get("/test") assert response.status_code == HTTP_500_INTERNAL_SERVER_ERROR - assert "Test debug exception" in response.text + if is_debug: + assert "Test debug exception" in response.text + else: + assert "Internal Server Error" in response.text if should_log: assert len(caplog.records) == 1 @@ -206,7 +209,10 @@ def handler() -> None: with TestClient(app=app) as client, capture_logs() as cap_logs: response = client.get("/test") assert response.status_code == HTTP_500_INTERNAL_SERVER_ERROR - assert "Test debug exception" in response.text + if is_debug: + assert "Test debug exception" in response.text + else: + assert "Internal Server Error" in response.text if should_log: assert len(cap_logs) == 1 @@ -233,7 +239,7 @@ def handler() -> None: client.app.logger = get_logger("litestar") response = client.get("/test") assert response.status_code == HTTP_500_INTERNAL_SERVER_ERROR - assert "Test debug exception" in response.text + assert "Internal Server Error" in response.text assert len(caplog.records) == 1 assert caplog.records[0].levelname == "ERROR" diff --git a/tests/unit/test_middleware/test_session/test_middleware.py b/tests/unit/test_middleware/test_session/test_middleware.py index 7952b03568..f206c32d0c 100644 --- a/tests/unit/test_middleware/test_session/test_middleware.py +++ b/tests/unit/test_middleware/test_session/test_middleware.py @@ -19,7 +19,7 @@ def handler(request: Request) -> None: with create_test_client(handler) as client: response = client.get("/test") assert response.status_code == HTTP_500_INTERNAL_SERVER_ERROR - assert response.json()["detail"] == "'session' is not defined in scope, install a SessionMiddleware to set it" + assert response.json()["detail"] == "Internal Server Error" def test_integration(session_backend_config: "BaseBackendConfig") -> None: diff --git a/tests/unit/test_signature/test_parsing.py b/tests/unit/test_signature/test_parsing.py index 7198ac835d..efe3da8c6d 100644 --- a/tests/unit/test_signature/test_parsing.py +++ b/tests/unit/test_signature/test_parsing.py @@ -146,7 +146,7 @@ def test(dep: int, param: int, optional_dep: Optional[int] = Dependency()) -> No response = client.get("/?param=13") assert response.json() == { - "detail": "A dependency failed validation for GET http://testserver.local/?param=13", + "detail": "Internal Server Error", "extra": error_extra, "status_code": HTTP_500_INTERNAL_SERVER_ERROR, } diff --git a/tests/unit/test_template/test_built_in.py b/tests/unit/test_template/test_built_in.py index bee82e9ea9..564f3552dc 100644 --- a/tests/unit/test_template/test_built_in.py +++ b/tests/unit/test_template/test_built_in.py @@ -93,7 +93,7 @@ def invalid_template_name_handler() -> Template: with create_test_client(route_handlers=[invalid_template_name_handler], template_config=template_config) as client: response = client.request("GET", "/") assert response.status_code == 500 - assert response.json() == {"detail": "Template invalid.html not found.", "status_code": 500} + assert response.json() == {"detail": "Internal Server Error", "status_code": 500} def test_no_context(tmp_path: Path, template_config: TemplateConfig) -> None: diff --git a/tests/unit/test_template/test_template.py b/tests/unit/test_template/test_template.py index 502f1b8d5f..91b7ad177e 100644 --- a/tests/unit/test_template/test_template.py +++ b/tests/unit/test_template/test_template.py @@ -25,7 +25,7 @@ def invalid_path() -> Template: with create_test_client(route_handlers=[invalid_path]) as client: response = client.request("GET", "/") assert response.status_code == 500 - assert response.json() == {"detail": "Template engine is not configured", "status_code": 500} + assert response.json() == {"detail": "Internal Server Error", "status_code": 500} def test_engine_passed_to_callback(tmp_path: "Path") -> None: