From 32b89ba2fbe2e96a8923370b820fc2a5fccc4fcd Mon Sep 17 00:00:00 2001 From: Cliff Hill Date: Wed, 3 Sep 2025 19:06:59 -0400 Subject: [PATCH] More fixes to make the tests work for the backend once more. Signed-off-by: Cliff Hill --- .bandit | 4 +++ backend/src/backend/logging.py | 2 +- backend/tests/conftest.py | 2 +- backend/tests/services/test_bookings.py | 39 +++++++++++-------------- 4 files changed, 23 insertions(+), 24 deletions(-) create mode 100644 .bandit diff --git a/.bandit b/.bandit new file mode 100644 index 00000000..9a4ba811 --- /dev/null +++ b/.bandit @@ -0,0 +1,4 @@ +[bandit] +skips: B101 +tests: true +test_file_pattern: "test_*.py,*/tests/*.py" diff --git a/backend/src/backend/logging.py b/backend/src/backend/logging.py index 64fed6b0..9cd874b6 100644 --- a/backend/src/backend/logging.py +++ b/backend/src/backend/logging.py @@ -146,4 +146,4 @@ def setup_logging(app: FastAPI) -> None: status_code=500, content={"detail": "Internal server error"} ) - app.add_exception_handler(Exception, custom_exception_handler) # type: ignore + app.add_exception_handler(Exception, custom_exception_handler) diff --git a/backend/tests/conftest.py b/backend/tests/conftest.py index 59ee540c..4beafcb7 100644 --- a/backend/tests/conftest.py +++ b/backend/tests/conftest.py @@ -22,7 +22,7 @@ from sqlalchemy.ext.asyncio import AsyncSession # Ensure .env is loaded from base or parent directory before any project # imports -def _load_env_from_base_or_parent(): +def _load_env_from_base_or_parent() -> None: base = Path(__file__).parent.parent # project root env_path = base / ".env" if env_path.exists(): diff --git a/backend/tests/services/test_bookings.py b/backend/tests/services/test_bookings.py index 2bde2064..8ceb3b30 100644 --- a/backend/tests/services/test_bookings.py +++ b/backend/tests/services/test_bookings.py @@ -28,6 +28,12 @@ from backend.services.bookings import new_booking from backend.services.bookings import update_booking +def _config_side_effect(key: str, *args: Any, **kwargs: Any) -> int | None: + if key == "BOOKING_MAX_MONTHS": + return 12 if "max_months" not in kwargs else kwargs["max_months"] + return kwargs.get("default") + + @pytest.mark.asyncio @pytest.mark.parametrize("mock_logger", ["backend.services.bookings"], indirect=True) async def test_get_bookings_for_room_success( @@ -285,14 +291,7 @@ async def test_new_booking_success( "backend.services.bookings.config", wraps=sys.modules["backend"].config ) as mock_config, ): - from typing import Any - from typing import cast - - mock_config.side_effect = lambda key, **kwargs: ( # type: ignore - 12 - if key == "BOOKING_MAX_MONTHS" - else cast(Any, kwargs.get("default")) # type: ignore - ) + mock_config.side_effect = _config_side_effect # Patch session.scalars to simulate no overlap mock_scalars_result = AsyncMock() mock_scalars_result.first = MagicMock(return_value=None) @@ -393,11 +392,13 @@ async def test_new_booking_constraints( "backend.services.bookings.config", wraps=sys.modules["backend"].config ) as mock_config, ): - mock_config.side_effect = lambda key, *args, **kwargs: ( # type: ignore - max_months - if key == "BOOKING_MAX_MONTHS" - else kwargs.get("default") # type: ignore - ) + + def _config_side_effect_max_months( + key: str, *args: Any, **kwargs: Any + ) -> int | None: + return max_months if key == "BOOKING_MAX_MONTHS" else kwargs.get("default") + + mock_config.side_effect = _config_side_effect_max_months # Patch session.scalars to simulate no overlap mock_scalars_result = AsyncMock() mock_scalars_result.first = MagicMock(return_value=None) @@ -441,9 +442,7 @@ async def test_new_booking_overlap( "backend.services.bookings.config", wraps=sys.modules["backend"].config ) as mock_config, ): - mock_config.side_effect = lambda key, **kwargs: ( # type: ignore - 12 if key == "BOOKING_MAX_MONTHS" else kwargs.get("default") # type: ignore - ) # type: ignore[no-untyped-def] + mock_config.side_effect = _config_side_effect # Patch session.scalars to simulate overlap overlap_booking: Booking = Booking( room_id=1, @@ -491,9 +490,7 @@ async def test_new_booking_attendees_exceed_capacity( "backend.services.bookings.config", wraps=sys.modules["backend"].config ) as mock_config, ): - mock_config.side_effect = lambda key, **kwargs: ( # type: ignore - 12 if key == "BOOKING_MAX_MONTHS" else kwargs.get("default") # type: ignore - ) + mock_config.side_effect = _config_side_effect # Patch session.scalars to simulate no overlap mock_scalars_result = AsyncMock() mock_scalars_result.first = MagicMock(return_value=None) @@ -552,9 +549,7 @@ async def test_new_booking_database_error( "backend.services.bookings.config", wraps=sys.modules["backend"].config ) as mock_config, ): - mock_config.side_effect = lambda key, **kwargs: ( # type: ignore - 12 if key == "BOOKING_MAX_MONTHS" else kwargs.get("default") # type: ignore - ) + mock_config.side_effect = _config_side_effect # Patch session.scalars to simulate no overlap mock_scalars_result = AsyncMock() mock_scalars_result.first = MagicMock(return_value=None)