Skip to content
35 changes: 31 additions & 4 deletions users/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import asyncio
from typing import Dict
from typing import Dict, Optional

import pytest
from asgi_lifespan import LifespanManager
from httpx import AsyncClient
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncConnection, AsyncSession

from app.api.deps import get_session
from app.core.security import get_password_hash
from app.core.config import settings
from app.core.database import engine
from app.main import app
from app.models.users import User


@pytest.fixture()
@pytest.fixture(scope="session")
async def connection():
async with engine.begin() as conn:
yield conn
await conn.rollback()


@pytest.fixture()
@pytest.fixture(scope="session")
async def session(connection: AsyncConnection):
async with AsyncSession(connection, expire_on_commit=False) as _session:
yield _session
Expand All @@ -38,7 +41,7 @@ def event_loop():
loop.close()


@pytest.fixture()
@pytest.fixture(scope="session")
async def client():
async with AsyncClient(app=app, base_url="http://test") as ac, LifespanManager(app):
yield ac
Expand All @@ -53,3 +56,27 @@ async def superuser_token_headers(client: AsyncClient) -> Dict[str, str]:
res = await client.post("/api/v1/login/", data=login_data)
access_token = res.json()["access_token"]
return {"Authorization": f"Bearer {access_token}"}


@pytest.fixture(scope="session")
async def create_non_superuser(session: AsyncSession) -> Dict[str, str]:
email = "[email protected]"
password = "Ksd8nASD1_Hjns!P"
hashed_password = get_password_hash(password)
result = await session.execute(select(User).where(User.email == email))
user: Optional[User] = result.scalars().first()
if user is None:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is never going to happen cause there will be a rollback on each test.

Sorry, I didn't notice before.

session.add(User(email=email, hashed_password=hashed_password, is_superuser=False))
await session.commit()
return {"email": email, "password": password}


@pytest.fixture(scope="session")
async def user_token_headers(client: AsyncClient, create_non_superuser: Dict[str, str]) -> Dict[str, str]:
login_data = {
"username": create_non_superuser["email"],
"password": create_non_superuser["password"],
}
res = await client.post("/api/v1/login/", data=login_data)
access_token = res.json()["access_token"]
return {"Authorization": f"Bearer {access_token}"}
7 changes: 7 additions & 0 deletions users/tests/test_home.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@ async def test_home(client: AsyncClient, superuser_token_headers: Dict[str, str]
res = await client.get("/api/v1/home", headers=superuser_token_headers)
assert res.status_code == 200
assert res.json() == "Hello World!"


@pytest.mark.asyncio
async def test_home_user(client: AsyncClient, user_token_headers: Dict[str, str]):
res = await client.get("/api/v1/home", headers=user_token_headers)
assert res.status_code == 200
assert res.json() == "Hello World!"