Skip to content
23 changes: 23 additions & 0 deletions users/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
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()
Expand Down Expand Up @@ -53,3 +55,24 @@ 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()
Copy link
Owner

Choose a reason for hiding this comment

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

Can it be a scope="session"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Facing an issue when changing this fixture's scope to "session", the user_token_header fixture is function scoped and causes a ScopeMismatch error. If we set the scope of user_token_header as "session" too, then it causes an issue with accessing the "function" scoped fixture "client" with "session" scoped fixture "create_non_superuser".

My opinion is to create a class, and scope all related fixtures to "class". This way we can add any test related to the users into that class.

Copy link
Owner

Choose a reason for hiding this comment

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

The client fixture can be session as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That leads to making the "session" and "connection" fixtures be scoped to the session. I've made these changes already, and all tests passed.

async def create_non_superuser(session: AsyncSession) -> Dict[str, str]:
email = "test_user@test.com"
password = "randomdummypassword"
hashed_password = get_password_hash(password)
session.add(User(email=email, hashed_password=hashed_password, is_superuser=False))
await session.commit()
return {"email": email, "password": password}


@pytest.fixture()
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!"