Skip to content

Commit 34ab82f

Browse files
committed
test form data for POST
1 parent 51c20c2 commit 34ab82f

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

app/api/user.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from fastapi import APIRouter, Depends, status, Request, HTTPException
1+
from typing import Annotated
2+
3+
from fastapi import APIRouter, Depends, status, Request, HTTPException, Form
24
from sqlalchemy.ext.asyncio import AsyncSession
35

46
from app.database import get_db
@@ -29,7 +31,9 @@ async def create_user(
2931
"/token", status_code=status.HTTP_201_CREATED, response_model=TokenResponse
3032
)
3133
async def get_token_for_user(
32-
user: UserLogin, request: Request, db_session: AsyncSession = Depends(get_db)
34+
user: Annotated[UserLogin, Form()],
35+
request: Request,
36+
db_session: AsyncSession = Depends(get_db),
3337
):
3438
_user: User = await User.find(db_session, [User.email == user.email])
3539

app/database.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@
2727
async def get_db() -> AsyncGenerator:
2828
async with AsyncSessionFactory() as session:
2929
# logger.debug(f"ASYNC Pool: {engine.pool.status()}")
30-
yield session
30+
try:
31+
yield session
32+
except Exception as e:
33+
logger.error(f"Error getting database session: {e}")
34+
raise

app/services/auth.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
from fastapi import Request, HTTPException
88
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
9+
from app.utils.logging import AppLogger
10+
11+
logger = AppLogger().get_logger()
912

1013

1114
async def get_from_redis(request: Request, key: str):
@@ -37,6 +40,7 @@ async def __call__(self, request: Request):
3740
raise HTTPException(
3841
status_code=403, detail="Invalid token or expired token."
3942
)
43+
logger.info(f"Token verified: {credentials.credentials}")
4044
return credentials.credentials
4145

4246

tests/api/test_auth.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ async def test_add_user(client: AsyncClient):
3838
# TODO: parametrize test with diff urls including 404 and 401
3939
async def test_get_token(client: AsyncClient):
4040
payload = {"email": "joe@grillazz.com", "password": "s1lly"}
41-
response = await client.post("/user/token", json=payload)
41+
response = await client.post(
42+
"/user/token",
43+
data=payload,
44+
headers={"Content-Type": "application/x-www-form-urlencoded"},
45+
)
4246
assert response.status_code == status.HTTP_201_CREATED
4347
claimset = jwt.decode(
4448
response.json()["access_token"], options={"verify_signature": False}

0 commit comments

Comments
 (0)