From 5080eda3acf9cfe979acf9f024e3473cd5bdec8c Mon Sep 17 00:00:00 2001 From: grillazz Date: Sun, 11 Jan 2026 08:37:48 +0100 Subject: [PATCH] refactor: clean up imports and enhance test setup for user token retrieval --- tests/api/test_auth.py | 17 ++++++++++++++--- tests/api/test_stuff.py | 7 +++---- tests/conftest.py | 5 ++--- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/tests/api/test_auth.py b/tests/api/test_auth.py index 9000ef3..2d6cd84 100644 --- a/tests/api/test_auth.py +++ b/tests/api/test_auth.py @@ -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" diff --git a/tests/api/test_stuff.py b/tests/api/test_stuff.py index 985c892..3b57da0 100644 --- a/tests/api/test_stuff.py +++ b/tests/api/test_stuff.py @@ -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) diff --git a/tests/conftest.py b/tests/conftest.py index 9ad5d46..1a954d7 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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