from types import SimpleNamespace import pytest from fastapi import HTTPException, status from fastapi.responses import JSONResponse from monday_com_orca_backend.api import error_handlers from monday_com_orca_backend.api.auth.exceptions import UnauthorizedException @pytest.fixture def mock_request(): return type( "MockRequest", (), {"url": "http://testserver/test", "client": SimpleNamespace(host="127.0.0.1")}, )() async def assert_json_response( handler, request, exc, expected_status, expected_content ): response = await handler(request, exc) assert isinstance(response, JSONResponse) assert response.status_code == expected_status data = response.body.decode() for value in expected_content: assert value in data class TestDefaultErrorHandler: @pytest.mark.asyncio async def test_default_error_handler_returns_500(self, mock_request): await assert_json_response( error_handlers.default_error_handler, mock_request, Exception("Something went wrong"), status.HTTP_500_INTERNAL_SERVER_ERROR, ["internal_error", error_handlers.DEFAULT_ERROR_MSG], ) class TestHttpErrorHandler: @pytest.mark.asyncio @pytest.mark.parametrize( "status_code,detail", [ (400, "Invalid input"), (403, "Forbidden!"), ], ) async def test_http_error_handler(self, mock_request, status_code, detail): await assert_json_response( error_handlers.http_error_handler, mock_request, HTTPException(status_code=status_code, detail=detail), status_code, ["bad_request", detail], ) class TestUnauthorizedExceptionHandler: @pytest.mark.asyncio async def test_unauthorized_exception_handler_returns_404(self, mock_request): await assert_json_response( error_handlers.unauthorized_exception_handler, mock_request, UnauthorizedException(detail="Unauthorized access!"), 404, # Obfuscated to avoid revealing endpoint existence ["Not Found"], )