Skip to content

Commit 5080eda

Browse files
committed
refactor: clean up imports and enhance test setup for user token retrieval
1 parent e9ea2c6 commit 5080eda

3 files changed

Lines changed: 19 additions & 10 deletions

File tree

tests/api/test_auth.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,28 @@ async def test_add_user(client: AsyncClient):
3737

3838
# TODO: parametrize test with diff urls including 404 and 401
3939
async def test_get_token(client: AsyncClient):
40-
payload = {"email": "joe@grillazz.com", "password": "s1lly"}
40+
# Create the user first
41+
user_payload = {
42+
"email": "joe@grillazz.com",
43+
"first_name": "Joe",
44+
"last_name": "Garcia",
45+
"password": "s1lly",
46+
}
47+
create_user_response = await client.post("/user/", json=user_payload)
48+
assert create_user_response.status_code == status.HTTP_201_CREATED
49+
50+
# Now request the token
51+
token_payload = {"email": "joe@grillazz.com", "password": "s1lly"}
4152
response = await client.post(
4253
"/user/token",
43-
data=payload,
54+
data=token_payload,
4455
headers={"Content-Type": "application/x-www-form-urlencoded"},
4556
)
4657
assert response.status_code == status.HTTP_201_CREATED
4758
claimset = jwt.decode(
4859
response.json()["access_token"], options={"verify_signature": False}
4960
)
50-
assert claimset["email"] == payload["email"]
61+
assert claimset["email"] == token_payload["email"]
5162
assert claimset["expiry"] == IsPositiveFloat()
5263
assert claimset["platform"] == "python-httpx/0.28.1"
5364

tests/api/test_stuff.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
from polyfactory.factories.pydantic_factory import ModelFactory
77
from sqlalchemy.ext.asyncio import AsyncSession
88

9-
from app.schemas.stuff import StuffSchema
109
from app.models import Stuff
10+
from app.schemas.stuff import StuffSchema
1111

1212
pytestmark = pytest.mark.anyio
1313

@@ -16,7 +16,7 @@ class StuffFactory(ModelFactory[StuffSchema]):
1616
__model__ = StuffSchema
1717

1818

19-
async def test_add_stuff(client: AsyncClient, db_session: AsyncSession):
19+
async def test_add_stuff(client: AsyncClient):
2020
stuff = StuffFactory.build(factory_use_constructors=True).model_dump(mode="json")
2121
response = await client.post("/stuff", json=stuff)
2222
assert response.status_code == status.HTTP_201_CREATED
@@ -40,9 +40,8 @@ async def test_get_stuff(client: AsyncClient, db_session: AsyncSession):
4040
assert response.json() == snapshot(
4141
{"no_response": "The requested resource was not found"}
4242
)
43+
# test if db_session and client share the same in-memory db and rollback works
4344
stuff = StuffFactory.build(factory_use_constructors=True).model_dump(mode="json")
44-
# await client.post("/stuff", json=stuff)
45-
# name = stuff["name"]
4645
stuff = Stuff(**stuff)
4746
name = stuff.name
4847
db_session.add(stuff)

tests/conftest.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
from typing import Any
33

44
import pytest
5-
from fastapi.exceptions import ResponseValidationError
65
from httpx import ASGITransport, AsyncClient
76
from sqlalchemy import text
8-
from sqlalchemy.exc import ProgrammingError, SQLAlchemyError
7+
from sqlalchemy.exc import ProgrammingError
98

10-
from app.database import engine, get_db, test_engine, TestAsyncSessionFactory
9+
from app.database import TestAsyncSessionFactory, engine, get_db, test_engine
1110
from app.main import app
1211
from app.models.base import Base
1312
from app.redis import get_redis

0 commit comments

Comments
 (0)