mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2026-01-17 11:40:39 +03:00
refactor: clean up imports and enhance test setup for user token retrieval
This commit is contained in:
@@ -37,17 +37,28 @@ 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"}
|
# 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(
|
response = await client.post(
|
||||||
"/user/token",
|
"/user/token",
|
||||||
data=payload,
|
data=token_payload,
|
||||||
headers={"Content-Type": "application/x-www-form-urlencoded"},
|
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}
|
||||||
)
|
)
|
||||||
assert claimset["email"] == payload["email"]
|
assert claimset["email"] == token_payload["email"]
|
||||||
assert claimset["expiry"] == IsPositiveFloat()
|
assert claimset["expiry"] == IsPositiveFloat()
|
||||||
assert claimset["platform"] == "python-httpx/0.28.1"
|
assert claimset["platform"] == "python-httpx/0.28.1"
|
||||||
|
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ from inline_snapshot import snapshot
|
|||||||
from polyfactory.factories.pydantic_factory import ModelFactory
|
from polyfactory.factories.pydantic_factory import ModelFactory
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
from app.schemas.stuff import StuffSchema
|
|
||||||
from app.models import Stuff
|
from app.models import Stuff
|
||||||
|
from app.schemas.stuff import StuffSchema
|
||||||
|
|
||||||
pytestmark = pytest.mark.anyio
|
pytestmark = pytest.mark.anyio
|
||||||
|
|
||||||
@@ -16,7 +16,7 @@ class StuffFactory(ModelFactory[StuffSchema]):
|
|||||||
__model__ = 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")
|
stuff = StuffFactory.build(factory_use_constructors=True).model_dump(mode="json")
|
||||||
response = await client.post("/stuff", json=stuff)
|
response = await client.post("/stuff", json=stuff)
|
||||||
assert response.status_code == status.HTTP_201_CREATED
|
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(
|
assert response.json() == snapshot(
|
||||||
{"no_response": "The requested resource was not found"}
|
{"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")
|
stuff = StuffFactory.build(factory_use_constructors=True).model_dump(mode="json")
|
||||||
# await client.post("/stuff", json=stuff)
|
|
||||||
# name = stuff["name"]
|
|
||||||
stuff = Stuff(**stuff)
|
stuff = Stuff(**stuff)
|
||||||
name = stuff.name
|
name = stuff.name
|
||||||
db_session.add(stuff)
|
db_session.add(stuff)
|
||||||
|
|||||||
@@ -2,12 +2,11 @@ from collections.abc import AsyncGenerator
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from fastapi.exceptions import ResponseValidationError
|
|
||||||
from httpx import ASGITransport, AsyncClient
|
from httpx import ASGITransport, AsyncClient
|
||||||
from sqlalchemy import text
|
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.main import app
|
||||||
from app.models.base import Base
|
from app.models.base import Base
|
||||||
from app.redis import get_redis
|
from app.redis import get_redis
|
||||||
|
|||||||
Reference in New Issue
Block a user