From a5ab2ec818bd4d6f0ce4a4f91954ece2bf42480d Mon Sep 17 00:00:00 2001 From: HMZ-H Date: Tue, 15 Sep 2026 16:03:21 +0300 Subject: [PATCH] Phase 2: add auth tests and register auth router - Add conftest with async test fixtures (SQLite + httpx) - Add 8 auth endpoint tests (register, login, refresh) - Register auth router in main.py - Fix Settings dependency injection via get_settings --- backend/dwf/api/deps.py | 6 +- backend/dwf/api/routers/auth.py | 8 +- backend/dwf/main.py | 3 +- backend/pyproject.toml | 1 + backend/test.db | Bin 0 -> 16384 bytes {tests => backend/tests}/__init__.py | 0 backend/tests/conftest.py | 52 ++++++++++ .../tests}/simulation/__init__.py | 0 .../tests/unit/__init__.py | 0 backend/tests/unit/test_auth.py | 97 ++++++++++++++++++ 10 files changed, 161 insertions(+), 6 deletions(-) create mode 100644 backend/test.db rename {tests => backend/tests}/__init__.py (100%) create mode 100644 backend/tests/conftest.py rename {tests => backend/tests}/simulation/__init__.py (100%) rename tests/conftest.py => backend/tests/unit/__init__.py (100%) create mode 100644 backend/tests/unit/test_auth.py diff --git a/backend/dwf/api/deps.py b/backend/dwf/api/deps.py index e665fd6..120b6ab 100644 --- a/backend/dwf/api/deps.py +++ b/backend/dwf/api/deps.py @@ -12,6 +12,10 @@ oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/auth/login") +def get_settings(request: Request) -> Settings: + return request.app.state.settings + + async def get_db(request: Request) -> AsyncGenerator[AsyncSession, None]: session_factory = request.app.state.session_factory session = session_factory() @@ -24,7 +28,7 @@ async def get_db(request: Request) -> AsyncGenerator[AsyncSession, None]: async def get_current_user( token: str = Depends(oauth2_scheme), db: AsyncSession = Depends(get_db), - settings: Settings = Depends(), + settings: Settings = Depends(get_settings), ) -> User: auth_service = AuthService(db, settings) try: diff --git a/backend/dwf/api/routers/auth.py b/backend/dwf/api/routers/auth.py index da41545..f441ddf 100644 --- a/backend/dwf/api/routers/auth.py +++ b/backend/dwf/api/routers/auth.py @@ -2,7 +2,7 @@ from pydantic import BaseModel, EmailStr from sqlalchemy.ext.asyncio import AsyncSession -from dwf.api.deps import get_db +from dwf.api.deps import get_db, get_settings from dwf.services.auth_service import AuthService from dwf.settings import Settings @@ -33,7 +33,7 @@ class RefreshRequest(BaseModel): async def register( request: RegisterRequest, db: AsyncSession = Depends(get_db), - settings: Settings = Depends(), + settings: Settings = Depends(get_settings), ): auth_service = AuthService(db, settings) try: @@ -51,7 +51,7 @@ async def register( async def login( request: LoginRequest, db: AsyncSession = Depends(get_db), - settings: Settings = Depends(), + settings: Settings = Depends(get_settings), ): auth_service = AuthService(db, settings) user = await auth_service.login(request.email, request.password) @@ -69,7 +69,7 @@ async def login( @router.post("/refresh") async def refresh( request: RefreshRequest, - settings: Settings = Depends(), + settings: Settings = Depends(get_settings), ): auth_service = AuthService(None, settings) try: diff --git a/backend/dwf/main.py b/backend/dwf/main.py index c31463f..c40aaa9 100644 --- a/backend/dwf/main.py +++ b/backend/dwf/main.py @@ -16,7 +16,6 @@ async def lifespan(app: FastAPI): settings: Settings = app.state.settings setup_logging(settings.log_level) - engine = create_engine(settings.database_url) app.state.engine = engine app.state.session_factory = create_session_factory(engine) @@ -40,8 +39,10 @@ def create_app(settings: Settings | None = None) -> FastAPI: app.state.settings = settings from dwf.api.routers.health import router as health_router + from dwf.api.routers.auth import router as auth_router app.include_router(health_router) + app.include_router(auth_router) return app diff --git a/backend/pyproject.toml b/backend/pyproject.toml index e75a707..a11bdb1 100644 --- a/backend/pyproject.toml +++ b/backend/pyproject.toml @@ -25,6 +25,7 @@ dev = [ ] [tool.pytest.ini_options] +asyncio_mode = "auto" testpaths = ["tests"] pythonpath = ["."] diff --git a/backend/test.db b/backend/test.db new file mode 100644 index 0000000000000000000000000000000000000000..6e739645db592a29f8d14ede4d6a7c67c9f7c051 GIT binary patch literal 16384 zcmeI&!3hE}5CzbQ>s8qjQh^A