Skip to content

Commit

Permalink
Merge pull request #51 from tigran-saatchyan/fix/last_fixes
Browse files Browse the repository at this point in the history
[FIX] README.md update and file structure minor fix
  • Loading branch information
tigran-saatchyan authored Nov 19, 2023
2 parents e4e3f34 + 9040001 commit 32c7da6
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 34 deletions.
21 changes: 14 additions & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand All @@ -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")
Expand All @@ -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.
Expand Down
17 changes: 8 additions & 9 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
15 changes: 7 additions & 8 deletions tests/test_cart.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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",
Expand All @@ -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"],
},
)

Expand All @@ -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:
Expand All @@ -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"],
},
)

Expand Down
15 changes: 8 additions & 7 deletions tests/test_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -17,20 +15,23 @@ 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(
"/products/",
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"]
7 changes: 4 additions & 3 deletions tests/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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"],
},
)

Expand Down

0 comments on commit 32c7da6

Please sign in to comment.