From 286693eacc32929f380c863e3021dc6ea882f680 Mon Sep 17 00:00:00 2001 From: hasnain095 Date: Mon, 3 Apr 2023 22:04:46 +0500 Subject: [PATCH 01/11] Added test for user token --- users/tests/conftest.py | 28 ++++++++++++++++++++++++++++ users/tests/test_home.py | 7 +++++++ 2 files changed, 35 insertions(+) diff --git a/users/tests/conftest.py b/users/tests/conftest.py index 3728bcc..7394fa5 100644 --- a/users/tests/conftest.py +++ b/users/tests/conftest.py @@ -4,12 +4,15 @@ 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() @@ -53,3 +56,28 @@ 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 = "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: + 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!" From ff1fa7b2f458530b7a0eec2ab6038415eae4a3ba Mon Sep 17 00:00:00 2001 From: hasnain095 Date: Mon, 3 Apr 2023 22:18:57 +0500 Subject: [PATCH 02/11] scoped the create_non_superuser test to session --- users/tests/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/users/tests/conftest.py b/users/tests/conftest.py index 7394fa5..d17af06 100644 --- a/users/tests/conftest.py +++ b/users/tests/conftest.py @@ -59,7 +59,7 @@ async def superuser_token_headers(client: AsyncClient) -> Dict[str, str]: @pytest.fixture() -async def create_non_superuser(session: AsyncSession) -> Dict[str, str]: +async def create_non_superuser(scope="session", session: AsyncSession) -> Dict[str, str]: email = "test_user@test.com" password = "Ksd8nASD1_Hjns!P" hashed_password = get_password_hash(password) From 0b150add430927d4b3af2e418c76aa283b0e3c9a Mon Sep 17 00:00:00 2001 From: hasnain095 Date: Tue, 4 Apr 2023 20:35:45 +0500 Subject: [PATCH 03/11] fixed default argument, fixed no new line lint error --- users/tests/conftest.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/users/tests/conftest.py b/users/tests/conftest.py index d17af06..51456e5 100644 --- a/users/tests/conftest.py +++ b/users/tests/conftest.py @@ -1,5 +1,5 @@ import asyncio -from typing import Dict +from typing import Dict, Optional import pytest from asgi_lifespan import LifespanManager @@ -59,7 +59,7 @@ async def superuser_token_headers(client: AsyncClient) -> Dict[str, str]: @pytest.fixture() -async def create_non_superuser(scope="session", session: AsyncSession) -> Dict[str, str]: +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) @@ -80,4 +80,3 @@ async def user_token_headers(client: AsyncClient, create_non_superuser: Dict[str res = await client.post("/api/v1/login/", data=login_data) access_token = res.json()["access_token"] return {"Authorization": f"Bearer {access_token}"} - From 37e941ceee0d1df99b6183d884d61a42db6d6d04 Mon Sep 17 00:00:00 2001 From: hasnain095 Date: Tue, 4 Apr 2023 20:51:05 +0500 Subject: [PATCH 04/11] fixed fixture scope --- users/tests/conftest.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/users/tests/conftest.py b/users/tests/conftest.py index 51456e5..b393541 100644 --- a/users/tests/conftest.py +++ b/users/tests/conftest.py @@ -58,8 +58,8 @@ async def superuser_token_headers(client: AsyncClient) -> Dict[str, str]: return {"Authorization": f"Bearer {access_token}"} -@pytest.fixture() -async def create_non_superuser(session: AsyncSession, scope="session") -> Dict[str, str]: +@pytest.fixture(scope="session") +async def create_non_superuser(session: AsyncSession) -> Dict[str, str]: email = "test_user@test.com" password = "Ksd8nASD1_Hjns!P" hashed_password = get_password_hash(password) From 0d8fe921d969a4c91a00d0b90ca2706af9848615 Mon Sep 17 00:00:00 2001 From: hasnain095 Date: Tue, 4 Apr 2023 21:50:43 +0500 Subject: [PATCH 05/11] fix for test failing due to fixture scope mismatch --- users/tests/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/users/tests/conftest.py b/users/tests/conftest.py index b393541..3ef6d68 100644 --- a/users/tests/conftest.py +++ b/users/tests/conftest.py @@ -71,7 +71,7 @@ async def create_non_superuser(session: AsyncSession) -> Dict[str, str]: return {"email": email, "password": password} -@pytest.fixture() +@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"], From b68e839d30b8aa0f9cf4b5eb6af5bde39985eccb Mon Sep 17 00:00:00 2001 From: hasnain095 Date: Wed, 5 Apr 2023 00:10:37 +0500 Subject: [PATCH 06/11] removed fixture session scope --- users/tests/conftest.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/users/tests/conftest.py b/users/tests/conftest.py index 3ef6d68..94d4b3c 100644 --- a/users/tests/conftest.py +++ b/users/tests/conftest.py @@ -58,7 +58,7 @@ async def superuser_token_headers(client: AsyncClient) -> Dict[str, str]: return {"Authorization": f"Bearer {access_token}"} -@pytest.fixture(scope="session") +@pytest.fixture() async def create_non_superuser(session: AsyncSession) -> Dict[str, str]: email = "test_user@test.com" password = "Ksd8nASD1_Hjns!P" @@ -71,7 +71,7 @@ async def create_non_superuser(session: AsyncSession) -> Dict[str, str]: return {"email": email, "password": password} -@pytest.fixture(scope="session") +@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"], From 76e806e91469a0f75efdd7dffdb8b48fd575f796 Mon Sep 17 00:00:00 2001 From: hasnain095 Date: Wed, 5 Apr 2023 01:26:32 +0500 Subject: [PATCH 07/11] fixed fixture scoping --- users/tests/conftest.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/users/tests/conftest.py b/users/tests/conftest.py index 94d4b3c..cf9e619 100644 --- a/users/tests/conftest.py +++ b/users/tests/conftest.py @@ -15,14 +15,14 @@ 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 @@ -41,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 @@ -58,7 +58,7 @@ async def superuser_token_headers(client: AsyncClient) -> Dict[str, str]: return {"Authorization": f"Bearer {access_token}"} -@pytest.fixture() +@pytest.fixture(scope="session") async def create_non_superuser(session: AsyncSession) -> Dict[str, str]: email = "test_user@test.com" password = "Ksd8nASD1_Hjns!P" @@ -71,7 +71,7 @@ async def create_non_superuser(session: AsyncSession) -> Dict[str, str]: return {"email": email, "password": password} -@pytest.fixture() +@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"], From 43d52673ee57d2bb74e32bc9e435a85b895d9147 Mon Sep 17 00:00:00 2001 From: hasnain095 Date: Wed, 5 Apr 2023 02:09:36 +0500 Subject: [PATCH 08/11] removed fixture session scopes --- users/tests/conftest.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/users/tests/conftest.py b/users/tests/conftest.py index cf9e619..94d4b3c 100644 --- a/users/tests/conftest.py +++ b/users/tests/conftest.py @@ -15,14 +15,14 @@ from app.models.users import User -@pytest.fixture(scope="session") +@pytest.fixture() async def connection(): async with engine.begin() as conn: yield conn await conn.rollback() -@pytest.fixture(scope="session") +@pytest.fixture() async def session(connection: AsyncConnection): async with AsyncSession(connection, expire_on_commit=False) as _session: yield _session @@ -41,7 +41,7 @@ def event_loop(): loop.close() -@pytest.fixture(scope="session") +@pytest.fixture() async def client(): async with AsyncClient(app=app, base_url="http://test") as ac, LifespanManager(app): yield ac @@ -58,7 +58,7 @@ async def superuser_token_headers(client: AsyncClient) -> Dict[str, str]: return {"Authorization": f"Bearer {access_token}"} -@pytest.fixture(scope="session") +@pytest.fixture() async def create_non_superuser(session: AsyncSession) -> Dict[str, str]: email = "test_user@test.com" password = "Ksd8nASD1_Hjns!P" @@ -71,7 +71,7 @@ async def create_non_superuser(session: AsyncSession) -> Dict[str, str]: return {"email": email, "password": password} -@pytest.fixture(scope="session") +@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"], From eabf79148ef4ddd88b14bbdb3db7ff0b236bcefe Mon Sep 17 00:00:00 2001 From: hasnain095 Date: Wed, 5 Apr 2023 02:13:25 +0500 Subject: [PATCH 09/11] changed dummy password in test --- users/tests/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/users/tests/conftest.py b/users/tests/conftest.py index 94d4b3c..58e7200 100644 --- a/users/tests/conftest.py +++ b/users/tests/conftest.py @@ -61,7 +61,7 @@ async def superuser_token_headers(client: AsyncClient) -> Dict[str, str]: @pytest.fixture() async def create_non_superuser(session: AsyncSession) -> Dict[str, str]: email = "test_user@test.com" - password = "Ksd8nASD1_Hjns!P" + password = "randomdummypassword" hashed_password = get_password_hash(password) result = await session.execute(select(User).where(User.email == email)) user: Optional[User] = result.scalars().first() From 95d4bf47eb08149fe37c8e6a985fabf7bd9da22d Mon Sep 17 00:00:00 2001 From: Hasnain Ali Date: Mon, 15 May 2023 14:49:57 +0500 Subject: [PATCH 10/11] removed conditional because we roll back, its not required --- users/tests/conftest.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/users/tests/conftest.py b/users/tests/conftest.py index 58e7200..6ad49a3 100644 --- a/users/tests/conftest.py +++ b/users/tests/conftest.py @@ -63,11 +63,8 @@ async def create_non_superuser(session: AsyncSession) -> Dict[str, str]: email = "test_user@test.com" password = "randomdummypassword" 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: - session.add(User(email=email, hashed_password=hashed_password, is_superuser=False)) - await session.commit() + session.add(User(email=email, hashed_password=hashed_password, is_superuser=False)) + await session.commit() return {"email": email, "password": password} From 0cc7353984c7f224fd4ea2a418ffe2a60a41ad83 Mon Sep 17 00:00:00 2001 From: Hasnain Ali Date: Mon, 15 May 2023 16:12:24 +0500 Subject: [PATCH 11/11] removed unused imports --- users/tests/conftest.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/users/tests/conftest.py b/users/tests/conftest.py index 6ad49a3..2462c30 100644 --- a/users/tests/conftest.py +++ b/users/tests/conftest.py @@ -1,10 +1,9 @@ import asyncio -from typing import Dict, Optional +from typing import Dict 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