test form data for POST

This commit is contained in:
Jakub Miazek 2024-11-11 15:10:05 +01:00
parent 51c20c28eb
commit 34ab82fb4c
4 changed files with 20 additions and 4 deletions

View File

@ -1,4 +1,6 @@
from fastapi import APIRouter, Depends, status, Request, HTTPException
from typing import Annotated
from fastapi import APIRouter, Depends, status, Request, HTTPException, Form
from sqlalchemy.ext.asyncio import AsyncSession
from app.database import get_db
@ -29,7 +31,9 @@ async def create_user(
"/token", status_code=status.HTTP_201_CREATED, response_model=TokenResponse
)
async def get_token_for_user(
user: UserLogin, request: Request, db_session: AsyncSession = Depends(get_db)
user: Annotated[UserLogin, Form()],
request: Request,
db_session: AsyncSession = Depends(get_db),
):
_user: User = await User.find(db_session, [User.email == user.email])

View File

@ -27,4 +27,8 @@ AsyncSessionFactory = async_sessionmaker(
async def get_db() -> AsyncGenerator:
async with AsyncSessionFactory() as session:
# logger.debug(f"ASYNC Pool: {engine.pool.status()}")
try:
yield session
except Exception as e:
logger.error(f"Error getting database session: {e}")
raise

View File

@ -6,6 +6,9 @@ from app.models.user import User
from fastapi import Request, HTTPException
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from app.utils.logging import AppLogger
logger = AppLogger().get_logger()
async def get_from_redis(request: Request, key: str):
@ -37,6 +40,7 @@ class AuthBearer(HTTPBearer):
raise HTTPException(
status_code=403, detail="Invalid token or expired token."
)
logger.info(f"Token verified: {credentials.credentials}")
return credentials.credentials

View File

@ -38,7 +38,11 @@ async def test_add_user(client: AsyncClient):
# TODO: parametrize test with diff urls including 404 and 401
async def test_get_token(client: AsyncClient):
payload = {"email": "joe@grillazz.com", "password": "s1lly"}
response = await client.post("/user/token", json=payload)
response = await client.post(
"/user/token",
data=payload,
headers={"Content-Type": "application/x-www-form-urlencoded"},
)
assert response.status_code == status.HTTP_201_CREATED
claimset = jwt.decode(
response.json()["access_token"], options={"verify_signature": False}