from app.core.errors import AppError
from tests.conftest import make_app, running


def _app_with_failing_routes():
    app = make_app()

    @app.get("/boom/app-error")
    async def app_error():
        raise AppError(403, "no_access", "You don't have access to Meetings")

    @app.get("/boom/unexpected")
    async def unexpected():
        raise RuntimeError("database password is hunter2")

    return app


async def test_app_error_returns_its_code_and_exact_message():
    async with running(_app_with_failing_routes()) as client:
        response = await client.get("/boom/app-error")

    assert response.status_code == 403
    assert response.json() == {
        "error": {"code": "no_access", "message": "You don't have access to Meetings"}
    }


async def test_unexpected_error_hides_internal_details():
    async with running(_app_with_failing_routes()) as client:
        response = await client.get("/boom/unexpected")

    assert response.status_code == 500
    assert response.json() == {
        "error": {"code": "internal_error", "message": "Something went wrong"}
    }
    assert "hunter2" not in response.text
