refactor: clean up imports and enhance test setup for user token retrieval

This commit is contained in:
grillazz
2026-01-11 08:37:48 +01:00
parent e9ea2c627a
commit 5080eda3ac
3 changed files with 19 additions and 10 deletions

View File

@@ -37,17 +37,28 @@ 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"}
# Create the user first
user_payload = {
"email": "joe@grillazz.com",
"first_name": "Joe",
"last_name": "Garcia",
"password": "s1lly",
}
create_user_response = await client.post("/user/", json=user_payload)
assert create_user_response.status_code == status.HTTP_201_CREATED
# Now request the token
token_payload = {"email": "joe@grillazz.com", "password": "s1lly"}
response = await client.post(
"/user/token",
data=payload,
data=token_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}
)
assert claimset["email"] == payload["email"]
assert claimset["email"] == token_payload["email"]
assert claimset["expiry"] == IsPositiveFloat()
assert claimset["platform"] == "python-httpx/0.28.1"

View File

@@ -6,8 +6,8 @@ from inline_snapshot import snapshot
from polyfactory.factories.pydantic_factory import ModelFactory
from sqlalchemy.ext.asyncio import AsyncSession
from app.schemas.stuff import StuffSchema
from app.models import Stuff
from app.schemas.stuff import StuffSchema
pytestmark = pytest.mark.anyio
@@ -16,7 +16,7 @@ class StuffFactory(ModelFactory[StuffSchema]):
__model__ = StuffSchema
async def test_add_stuff(client: AsyncClient, db_session: AsyncSession):
async def test_add_stuff(client: AsyncClient):
stuff = StuffFactory.build(factory_use_constructors=True).model_dump(mode="json")
response = await client.post("/stuff", json=stuff)
assert response.status_code == status.HTTP_201_CREATED
@@ -40,9 +40,8 @@ async def test_get_stuff(client: AsyncClient, db_session: AsyncSession):
assert response.json() == snapshot(
{"no_response": "The requested resource was not found"}
)
# test if db_session and client share the same in-memory db and rollback works
stuff = StuffFactory.build(factory_use_constructors=True).model_dump(mode="json")
# await client.post("/stuff", json=stuff)
# name = stuff["name"]
stuff = Stuff(**stuff)
name = stuff.name
db_session.add(stuff)

View File

@@ -2,12 +2,11 @@ from collections.abc import AsyncGenerator
from typing import Any
import pytest
from fastapi.exceptions import ResponseValidationError
from httpx import ASGITransport, AsyncClient
from sqlalchemy import text
from sqlalchemy.exc import ProgrammingError, SQLAlchemyError
from sqlalchemy.exc import ProgrammingError
from app.database import engine, get_db, test_engine, TestAsyncSessionFactory
from app.database import TestAsyncSessionFactory, engine, get_db, test_engine
from app.main import app
from app.models.base import Base
from app.redis import get_redis