mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2025-08-26 16:40:40 +03:00
test form data for POST
This commit is contained in:
parent
51c20c28eb
commit
34ab82fb4c
@ -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 sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
from app.database import get_db
|
from app.database import get_db
|
||||||
@ -29,7 +31,9 @@ async def create_user(
|
|||||||
"/token", status_code=status.HTTP_201_CREATED, response_model=TokenResponse
|
"/token", status_code=status.HTTP_201_CREATED, response_model=TokenResponse
|
||||||
)
|
)
|
||||||
async def get_token_for_user(
|
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])
|
_user: User = await User.find(db_session, [User.email == user.email])
|
||||||
|
|
||||||
|
@ -27,4 +27,8 @@ AsyncSessionFactory = async_sessionmaker(
|
|||||||
async def get_db() -> AsyncGenerator:
|
async def get_db() -> AsyncGenerator:
|
||||||
async with AsyncSessionFactory() as session:
|
async with AsyncSessionFactory() as session:
|
||||||
# logger.debug(f"ASYNC Pool: {engine.pool.status()}")
|
# logger.debug(f"ASYNC Pool: {engine.pool.status()}")
|
||||||
yield session
|
try:
|
||||||
|
yield session
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error getting database session: {e}")
|
||||||
|
raise
|
||||||
|
@ -6,6 +6,9 @@ from app.models.user import User
|
|||||||
|
|
||||||
from fastapi import Request, HTTPException
|
from fastapi import Request, HTTPException
|
||||||
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
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):
|
async def get_from_redis(request: Request, key: str):
|
||||||
@ -37,6 +40,7 @@ class AuthBearer(HTTPBearer):
|
|||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=403, detail="Invalid token or expired token."
|
status_code=403, detail="Invalid token or expired token."
|
||||||
)
|
)
|
||||||
|
logger.info(f"Token verified: {credentials.credentials}")
|
||||||
return credentials.credentials
|
return credentials.credentials
|
||||||
|
|
||||||
|
|
||||||
|
@ -38,7 +38,11 @@ async def test_add_user(client: AsyncClient):
|
|||||||
# TODO: parametrize test with diff urls including 404 and 401
|
# TODO: parametrize test with diff urls including 404 and 401
|
||||||
async def test_get_token(client: AsyncClient):
|
async def test_get_token(client: AsyncClient):
|
||||||
payload = {"email": "joe@grillazz.com", "password": "s1lly"}
|
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
|
assert response.status_code == status.HTTP_201_CREATED
|
||||||
claimset = jwt.decode(
|
claimset = jwt.decode(
|
||||||
response.json()["access_token"], options={"verify_signature": False}
|
response.json()["access_token"], options={"verify_signature": False}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user