From 904000158821b9e3e9d1297b0105c41864b8ba9f Mon Sep 17 00:00:00 2001 From: Tigran Saatchyan Date: Sun, 19 Nov 2023 13:46:31 +0400 Subject: [PATCH] [FIX] README.md update and file structure minor fix --- tests/conftest.py | 21 ++++++++++++++------- tests/test_auth.py | 17 ++++++++--------- tests/test_cart.py | 15 +++++++-------- tests/test_product.py | 15 ++++++++------- tests/test_users.py | 7 ++++--- 5 files changed, 41 insertions(+), 34 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index c7c5b70..b3f58e4 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,7 +4,6 @@ from typing import AsyncGenerator import pytest -from fastapi.testclient import TestClient from httpx import AsyncClient from sqlalchemy.ext.asyncio import ( AsyncSession, @@ -50,6 +49,17 @@ async def override_get_async_session() -> AsyncGenerator[AsyncSession, None]: app.dependency_overrides[get_async_session] = override_get_async_session +@pytest.fixture(scope="function") +async def register_user(ac: AsyncClient): + """Simulate user registration. + + Args: + ac (AsyncClient): The asynchronous HTTP client. + """ + await ac.post("/api/v1/register", json=USER_DATA) + + +@pytest.fixture(scope="function") async def login_user(ac: AsyncClient): """Simulate user registration and login, returning the authentication cookies. @@ -60,13 +70,13 @@ async def login_user(ac: AsyncClient): Returns: http.cookies: Authentication cookies. """ - await ac.post("/api/v1/register", json=USER_DATA) - response = await ac.post( "/api/v1/jwt/login", data={"username": USER_EMAIL, "password": USER_PASSWORD}, ) - return response.cookies + ac.cookies.update(response.cookies) + + return ac.cookies @pytest.fixture(autouse=True, scope="class") @@ -87,9 +97,6 @@ def event_loop(request): loop.close() -client = TestClient(app) - - @pytest.fixture(scope="session") async def ac() -> AsyncGenerator[AsyncClient, None]: """AsyncClient fixture for testing asynchronous endpoints. diff --git a/tests/test_auth.py b/tests/test_auth.py index 7b545fc..dce0482 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -23,10 +23,11 @@ async def test_register(ac: AsyncClient): ) assert response.status_code == status.HTTP_201_CREATED + assert response.json()["email"] == USER_EMAIL -class TestLogin: - async def test_login_and_logout(self, ac: AsyncClient): +class TestLoginLogout: + async def test_login(self, register_user, ac: AsyncClient): """Test user login and logout endpoints. Args: @@ -35,23 +36,21 @@ async def test_login_and_logout(self, ac: AsyncClient): Returns: None """ - await ac.post( - "/api/v1/register", - json=USER_DATA, - ) - login_response = await ac.post( "/api/v1/jwt/login", data={"username": USER_EMAIL, "password": USER_PASSWORD}, ) - auth_cookies = login_response.cookies assert login_response.status_code == status.HTTP_204_NO_CONTENT + assert "unimartcookie" in ac.cookies + @staticmethod + async def test_logout(register_user, login_user, ac: AsyncClient): logout_response = await ac.post( "/api/v1/jwt/logout", headers={ "Content-Type": "application/x-www-form-urlencoded", - "Cookie": "unimartcookie=" + auth_cookies["unimartcookie"], + "Cookie": "unimartcookie=" + ac.cookies["unimartcookie"], }, ) assert logout_response.status_code == status.HTTP_204_NO_CONTENT + assert "unimartcookie" not in ac.cookies diff --git a/tests/test_cart.py b/tests/test_cart.py index 2f1038d..796dc06 100644 --- a/tests/test_cart.py +++ b/tests/test_cart.py @@ -8,7 +8,7 @@ class TestCart: @staticmethod - async def test_add_to_cart(ac: AsyncClient): + async def test_add_to_cart(register_user, login_user, ac: AsyncClient): """Test adding a product to the user's cart. Args: @@ -17,7 +17,6 @@ async def test_add_to_cart(ac: AsyncClient): Returns: None """ - auth_cookies = await login_user(ac) test_product = { "name": "Test product", "description": "Some description", @@ -29,7 +28,7 @@ async def test_add_to_cart(ac: AsyncClient): json=test_product, headers={ "Content-Type": "application/json", - "Cookie": "unimartcookie=" + auth_cookies["unimartcookie"], + "Cookie": "unimartcookie=" + ac.cookies["unimartcookie"], }, ) @@ -38,14 +37,16 @@ async def test_add_to_cart(ac: AsyncClient): json={"product_id": int(product.text), "quantity": 1}, headers={ "Content-Type": "application/json", - "Cookie": "unimartcookie=" + auth_cookies["unimartcookie"], + "Cookie": "unimartcookie=" + ac.cookies["unimartcookie"], }, ) assert response.status_code == status.HTTP_201_CREATED @staticmethod - async def test_get_all_from_cart(ac: AsyncClient): + async def test_get_all_from_cart( + register_user, login_user, ac: AsyncClient + ): """Test retrieving all products from the user's cart. Args: @@ -54,13 +55,11 @@ async def test_get_all_from_cart(ac: AsyncClient): Returns: None """ - auth_cookies = await login_user(ac) - response = await ac.get( "/cart/get_all/", headers={ "Content-Type": "application/json", - "Cookie": "unimartcookie=" + auth_cookies["unimartcookie"], + "Cookie": "unimartcookie=" + ac.cookies["unimartcookie"], }, ) diff --git a/tests/test_product.py b/tests/test_product.py index 9ab44e1..4ed60a5 100644 --- a/tests/test_product.py +++ b/tests/test_product.py @@ -3,12 +3,10 @@ from httpx import AsyncClient from starlette import status -from tests.conftest import login_user - class TestProducts: @staticmethod - async def test_add_product(ac: AsyncClient): + async def test_add_product(register_user, login_user, ac: AsyncClient): """Test adding a new product. Args: @@ -17,11 +15,10 @@ async def test_add_product(ac: AsyncClient): Returns: None """ - auth_cookies = await login_user(ac) test_product = { "name": "Test product", "description": "Some description", - "price": 1000, + "price": 1000.0, } response = await ac.post( @@ -29,8 +26,12 @@ async def test_add_product(ac: AsyncClient): json=test_product, headers={ "Content-Type": "application/json", - "Cookie": "unimartcookie=" + auth_cookies["unimartcookie"], + "Cookie": "unimartcookie=" + ac.cookies["unimartcookie"], }, ) - + product_data = response.json() assert response.status_code == status.HTTP_201_CREATED + assert "id" in product_data + assert product_data["name"] == test_product["name"] + assert product_data["description"] == test_product["description"] + assert product_data["price"] == test_product["price"] diff --git a/tests/test_users.py b/tests/test_users.py index 9680cba..79e4afc 100644 --- a/tests/test_users.py +++ b/tests/test_users.py @@ -8,7 +8,9 @@ class TestUsers: @staticmethod - async def test_get_current_user(ac: AsyncClient): + async def test_get_current_user( + register_user, login_user, ac: AsyncClient + ): """Test retrieving information about the current user. Args: @@ -17,12 +19,11 @@ async def test_get_current_user(ac: AsyncClient): Returns: None """ - auth_cookies = await login_user(ac) response = await ac.get( "/users/me", headers={ "Content-Type": "application/x-www-form-urlencoded", - "Cookie": "unimartcookie=" + auth_cookies["unimartcookie"], + "Cookie": "unimartcookie=" + ac.cookies["unimartcookie"], }, )