File size: 6,322 Bytes
a1e2ff8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
"""Regression tests for the `get_db` yield-based dependency in app/db/database.py.

In FastAPI >= 0.106, exceptions raised inside a path operation (including
``HTTPException``) are propagated to yield-based dependencies. The session
manager must therefore distinguish routine HTTP 4xx flows from genuine
database errors — otherwise every 404 / 409 / 413 / 422 logs a full ERROR-level
stack trace, drowning real failures in noise across this codebase's 60+
``raise HTTPException`` sites.
"""

from __future__ import annotations

import logging
from typing import Any
from unittest.mock import AsyncMock, MagicMock

import pytest
from fastapi import HTTPException
from starlette.exceptions import HTTPException as StarletteHTTPException

from app.db import database as db_mod


class _FakeSessionCtx:
    """Async context manager that yields a single AsyncMock session.

    Records ``commit`` / ``rollback`` calls so tests can verify both behaviours.
    """

    def __init__(self) -> None:
        self.session = AsyncMock()
        self.session.commit = AsyncMock()
        self.session.rollback = AsyncMock()

    async def __aenter__(self) -> Any:
        return self.session

    async def __aexit__(self, exc_type: Any, exc: Any, tb: Any) -> bool:
        return False


@pytest.fixture(autouse=True)
def _patch_session_factory(monkeypatch: pytest.MonkeyPatch) -> _FakeSessionCtx:
    """Replace ``get_session_factory`` with a stub that yields a mock session."""
    ctx = _FakeSessionCtx()
    factory = MagicMock(return_value=ctx)
    monkeypatch.setattr(db_mod, "get_session_factory", lambda: factory)
    return ctx


async def _drive(gen: Any, send: Any = None, *, throw: BaseException | None = None) -> None:
    """Drive an async generator one step. Optionally throw an exception in."""
    try:
        if throw is not None:
            await gen.athrow(throw)
        else:
            await gen.asend(send)
    except StopAsyncIteration:
        pass


@pytest.mark.asyncio
async def test_get_db_does_not_log_error_for_http_exception(
    caplog: pytest.LogCaptureFixture,
    _patch_session_factory: _FakeSessionCtx,
) -> None:
    """Regression: routine 404/409/413/422 must not produce an ERROR log."""
    caplog.set_level(logging.DEBUG, logger="app.db.database")

    gen = db_mod.get_db()
    _ = await gen.__anext__()

    with pytest.raises(HTTPException) as ei:
        await gen.athrow(HTTPException(status_code=404, detail="not found"))
    assert ei.value.status_code == 404

    db_records = [r for r in caplog.records if r.name == "app.db.database"]
    error_records = [r for r in db_records if r.levelno >= logging.ERROR]
    assert error_records == [], (
        "HTTPException must not generate ERROR-level logs from get_db — "
        f"got: {[(r.levelname, r.getMessage()) for r in error_records]}"
    )

    _patch_session_factory.session.rollback.assert_awaited_once()
    _patch_session_factory.session.commit.assert_not_called()


@pytest.mark.asyncio
async def test_get_db_does_not_log_error_for_starlette_http_exception(
    caplog: pytest.LogCaptureFixture,
    _patch_session_factory: _FakeSessionCtx,
) -> None:
    """Filtering must catch the Starlette base class as well as fastapi.HTTPException."""
    caplog.set_level(logging.DEBUG, logger="app.db.database")

    gen = db_mod.get_db()
    _ = await gen.__anext__()

    with pytest.raises(StarletteHTTPException):
        await gen.athrow(StarletteHTTPException(status_code=413, detail="too large"))

    error_records = [
        r for r in caplog.records
        if r.name == "app.db.database" and r.levelno >= logging.ERROR
    ]
    assert error_records == []
    _patch_session_factory.session.rollback.assert_awaited_once()


@pytest.mark.asyncio
async def test_get_db_logs_genuine_database_error_at_error_level(
    caplog: pytest.LogCaptureFixture,
    _patch_session_factory: _FakeSessionCtx,
) -> None:
    """Real (non-HTTP) exceptions must still surface with full context."""
    caplog.set_level(logging.DEBUG, logger="app.db.database")

    gen = db_mod.get_db()
    _ = await gen.__anext__()

    boom = RuntimeError("connection lost")
    with pytest.raises(RuntimeError):
        await gen.athrow(boom)

    error_records = [
        r for r in caplog.records
        if r.name == "app.db.database" and r.levelno >= logging.ERROR
    ]
    assert any(
        "DB session error" in r.getMessage() and r.exc_info is not None
        for r in error_records
    ), (
        "Genuine DB errors must still log at ERROR level with traceback — "
        f"got: {[(r.levelname, r.getMessage(), bool(r.exc_info)) for r in error_records]}"
    )
    _patch_session_factory.session.rollback.assert_awaited_once()


@pytest.mark.asyncio
async def test_get_db_commits_on_success(
    _patch_session_factory: _FakeSessionCtx,
) -> None:
    """Successful path: commit fires, rollback does not."""
    gen = db_mod.get_db()
    _ = await gen.__anext__()
    with pytest.raises(StopAsyncIteration):
        await gen.__anext__()

    _patch_session_factory.session.commit.assert_awaited_once()
    _patch_session_factory.session.rollback.assert_not_called()


@pytest.mark.asyncio
async def test_get_db_quiet_rollback_failure_under_http_exception_still_logs(
    caplog: pytest.LogCaptureFixture,
    _patch_session_factory: _FakeSessionCtx,
) -> None:
    """If rollback itself fails while propagating an HTTPException, that
    secondary failure is genuine and must be visible — the original
    HTTPException must still propagate.
    """
    caplog.set_level(logging.DEBUG, logger="app.db.database")
    _patch_session_factory.session.rollback.side_effect = RuntimeError("rollback boom")

    gen = db_mod.get_db()
    _ = await gen.__anext__()

    with pytest.raises(HTTPException) as ei:
        await gen.athrow(HTTPException(status_code=409, detail="conflict"))
    assert ei.value.status_code == 409

    rollback_failure_logs = [
        r for r in caplog.records
        if r.name == "app.db.database"
        and r.levelno >= logging.ERROR
        and "Rollback failed" in r.getMessage()
    ]
    assert rollback_failure_logs, (
        "A failing rollback during HTTPException propagation must surface — "
        "we only suppress noise for normal 4xx flows, not for cascading errors."
    )