diff --git a/users/tests/conftest.py b/users/tests/conftest.py index 3728bcc..2462c30 100644 --- a/users/tests/conftest.py +++ b/users/tests/conftest.py @@ -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() @@ -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() +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}"} diff --git a/users/tests/test_home.py b/users/tests/test_home.py index 71ef38f..ce1cba9 100644 --- a/users/tests/test_home.py +++ b/users/tests/test_home.py @@ -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!"