Skip to content

Feat/add user token test #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
29 changes: 28 additions & 1 deletion users/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
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()
Expand Down Expand Up @@ -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()
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, scope="session") -> Dict[str, str]:
email = "test_user@test.com"
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()
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!"