Browse Source

Fixed background tests to support new authentication and user module

maziggy 4 months ago
parent
commit
dc30856238
2 changed files with 4 additions and 13 deletions
  1. 2 12
      backend/app/core/auth.py
  2. 2 1
      backend/tests/conftest.py

+ 2 - 12
backend/app/core/auth.py

@@ -2,7 +2,7 @@ from __future__ import annotations
 
 import secrets
 from datetime import datetime, timedelta
-from typing import TYPE_CHECKING, Annotated
+from typing import Annotated
 
 from fastapi import Depends, Header, HTTPException, status
 from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
@@ -12,12 +12,10 @@ from sqlalchemy import select
 from sqlalchemy.ext.asyncio import AsyncSession
 
 from backend.app.core.database import async_session, get_db
+from backend.app.models.api_key import APIKey
 from backend.app.models.settings import Settings
 from backend.app.models.user import User
 
-if TYPE_CHECKING:
-    from backend.app.models.api_key import APIKey
-
 # Password hashing
 # Use pbkdf2_sha256 instead of bcrypt to avoid 72-byte limit and passlib initialization issues
 # pbkdf2_sha256 is a secure password hashing algorithm without bcrypt's limitations
@@ -245,10 +243,6 @@ async def get_api_key(
 
     Checks both 'Authorization: Bearer <key>' and 'X-API-Key: <key>' headers.
     """
-    from fastapi import HTTPException, status
-
-    from backend.app.models.api_key import APIKey
-
     api_key_value = None
     if x_api_key:
         api_key_value = x_api_key
@@ -295,8 +289,6 @@ def check_permission(api_key: APIKey, permission: str) -> None:
     Raises:
         HTTPException: If permission is not granted
     """
-    from fastapi import HTTPException, status
-
     permission_map = {
         "queue": "can_queue",
         "control_printer": "can_control_printer",
@@ -327,8 +319,6 @@ def check_printer_access(api_key: APIKey, printer_id: int) -> None:
     Raises:
         HTTPException: If access is denied
     """
-    from fastapi import HTTPException, status
-
     # If printer_ids is None or empty, access to all printers
     if api_key.printer_ids is None or len(api_key.printer_ids) == 0:
         return

+ 2 - 1
backend/tests/conftest.py

@@ -99,9 +99,10 @@ async def async_client(test_engine, db_session) -> AsyncGenerator[AsyncClient, N
     async def mock_init_printer_connections(db):
         pass  # No-op - don't connect to real printers
 
-    # Also patch the module-level async_session used by services
+    # Also patch the module-level async_session used by services and auth
     with (
         patch("backend.app.core.database.async_session", test_async_session),
+        patch("backend.app.core.auth.async_session", test_async_session),
         patch("backend.app.main.init_printer_connections", mock_init_printer_connections),
     ):
         async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client: