55No aiosqlite, no async DB — all database interactions are AsyncMock.
66"""
77
8- from unittest .mock import AsyncMock , MagicMock , patch
98from datetime import datetime , timezone
9+ from unittest .mock import AsyncMock , MagicMock , patch
1010from uuid import uuid4
1111
1212import pytest
13- from fastapi import status
14- from fastapi .testclient import TestClient
15-
16- from app .main import app
1713from app .api .dependencies import get_current_user
14+ from app .main import app
1815from config .database import get_async_session
19-
16+ from fastapi import status
17+ from fastapi .testclient import TestClient
2018
2119# ── helpers ───────────────────────────────────────────────────────────────────
2220
21+
2322def _make_user (email : str = "test@example.com" , password_plain : str = "testpassword" ):
2423 """Build an in-memory User object."""
2524 from datetime import datetime , timezone
25+
2626 from models .user import User , UserStatus
2727 from passlib .context import CryptContext
2828
2929 pwd = CryptContext (schemes = ["bcrypt" ], deprecated = "auto" )
30- u = User (email = email , hashed_password = pwd .hash (password_plain ), status = UserStatus .ACTIVE , email_verified = True )
30+ u = User (
31+ email = email ,
32+ hashed_password = pwd .hash (password_plain ),
33+ status = UserStatus .ACTIVE ,
34+ email_verified = True ,
35+ )
3136 u .id = uuid4 ()
3237 u .is_deleted = False
3338 u .mfa_enabled = False
@@ -61,9 +66,12 @@ def _mock_session():
6166
6267def _tokens (user ):
6368 from services .auth .jwt_service import JWTService
69+
6470 j = JWTService ()
6571 return {
66- "access_token" : j .create_access_token ({"sub" : str (user .id ), "email" : user .email }),
72+ "access_token" : j .create_access_token (
73+ {"sub" : str (user .id ), "email" : user .email }
74+ ),
6775 "refresh_token" : j .create_refresh_token ({"sub" : str (user .id )}),
6876 "token_type" : "bearer" ,
6977 "expires_in" : 1800 ,
@@ -162,7 +170,9 @@ def test_login_success(self, client):
162170 async def _mock_auth (* a , ** kw ):
163171 return user , tokens
164172
165- with patch ("app.api.v1.endpoints.auth.auth_service.authenticate_user" , _mock_auth ):
173+ with patch (
174+ "app.api.v1.endpoints.auth.auth_service.authenticate_user" , _mock_auth
175+ ):
166176 response = c .post (
167177 "/api/v1/auth/login" ,
168178 json = {"email" : user .email , "password" : "testpassword" },
@@ -183,7 +193,9 @@ def test_login_invalid_credentials(self, client):
183193 async def _mock_auth (* a , ** kw ):
184194 raise HTTPException (status_code = 401 , detail = "Invalid credentials" )
185195
186- with patch ("app.api.v1.endpoints.auth.auth_service.authenticate_user" , _mock_auth ):
196+ with patch (
197+ "app.api.v1.endpoints.auth.auth_service.authenticate_user" , _mock_auth
198+ ):
187199 response = c .post (
188200 "/api/v1/auth/login" ,
189201 json = {"email" : "test@example.com" , "password" : "wrongpassword" },
@@ -200,7 +212,9 @@ def test_login_nonexistent_user(self, client):
200212 async def _mock_auth (* a , ** kw ):
201213 raise HTTPException (status_code = 401 , detail = "Invalid credentials" )
202214
203- with patch ("app.api.v1.endpoints.auth.auth_service.authenticate_user" , _mock_auth ):
215+ with patch (
216+ "app.api.v1.endpoints.auth.auth_service.authenticate_user" , _mock_auth
217+ ):
204218 response = c .post (
205219 "/api/v1/auth/login" ,
206220 json = {"email" : "nobody@example.com" , "password" : "testpassword" },
@@ -223,7 +237,9 @@ def test_get_current_user(self, authed_client):
223237 def test_get_current_user_invalid_token (self , client ):
224238 """Invalid token → 401."""
225239 c , _ = client
226- response = c .get ("/api/v1/auth/me" , headers = {"Authorization" : "Bearer bad_token" })
240+ response = c .get (
241+ "/api/v1/auth/me" , headers = {"Authorization" : "Bearer bad_token" }
242+ )
227243 assert response .status_code == status .HTTP_401_UNAUTHORIZED
228244
229245 def test_get_current_user_no_token (self , client ):
@@ -291,7 +307,10 @@ def test_logout_success(self, authed_client):
291307 """Valid token → successful logout."""
292308 c , user , _ = authed_client
293309 from services .auth .jwt_service import JWTService
294- token = JWTService ().create_access_token ({"sub" : str (user .id ), "email" : user .email })
310+
311+ token = JWTService ().create_access_token (
312+ {"sub" : str (user .id ), "email" : user .email }
313+ )
295314
296315 async def _mock_logout (* a , ** kw ):
297316 pass
@@ -327,7 +346,9 @@ def test_oauth2_login_form(self, client):
327346 async def _mock_auth (* a , ** kw ):
328347 return user , tokens
329348
330- with patch ("app.api.v1.endpoints.auth.auth_service.authenticate_user" , _mock_auth ):
349+ with patch (
350+ "app.api.v1.endpoints.auth.auth_service.authenticate_user" , _mock_auth
351+ ):
331352 response = c .post (
332353 "/api/v1/auth/login/form" ,
333354 data = {"username" : user .email , "password" : "testpassword" },
@@ -341,8 +362,11 @@ async def _mock_auth(*a, **kw):
341362
342363# ── tiny async helper ─────────────────────────────────────────────────────────
343364
365+
344366def _async_return (value ):
345367 """Return an async function that returns value."""
368+
346369 async def _inner (* args , ** kwargs ):
347370 return value
371+
348372 return _inner
0 commit comments