Admin-Desk / tests /api /test_products.py
Fred808's picture
Upload 7 files
4c5298a verified
import pytest
from httpx import AsyncClient
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.security import create_access_token
pytestmark = pytest.mark.asyncio
@pytest.fixture
async def user_token(client: AsyncClient) -> str:
"""Create a test user and return their token"""
# Register user
await client.post(
"/api/v1/auth/register",
data={
"username": "testuser@example.com",
"password": "testpass123",
"full_name": "Test User"
}
)
# Login to get token
response = await client.post(
"/api/v1/auth/login",
data={
"username": "testuser@example.com",
"password": "testpass123"
}
)
return response.json()["access_token"]
async def test_create_product(client: AsyncClient, user_token: str):
# Test creating a product
response = await client.post(
"/api/v1/products/",
json={
"name": "Test Product",
"description": "A test product",
"price": 99.99,
"category": "test",
"inventory_count": 10
},
headers={"Authorization": f"Bearer {user_token}"}
)
assert response.status_code == 200
data = response.json()
assert data["name"] == "Test Product"
assert data["price"] == 99.99
return data["id"]
async def test_get_product(client: AsyncClient, user_token: str):
# First create a product
product_id = await test_create_product(client, user_token)
# Test getting the product
response = await client.get(
f"/api/v1/products/{product_id}",
headers={"Authorization": f"Bearer {user_token}"}
)
assert response.status_code == 200
data = response.json()
assert data["id"] == product_id
assert data["name"] == "Test Product"
async def test_list_products(client: AsyncClient, user_token: str):
# Create multiple products
await test_create_product(client, user_token)
await client.post(
"/api/v1/products/",
json={
"name": "Another Product",
"description": "Another test product",
"price": 49.99,
"category": "test",
"inventory_count": 5
},
headers={"Authorization": f"Bearer {user_token}"}
)
# Test listing products
response = await client.get(
"/api/v1/products/",
headers={"Authorization": f"Bearer {user_token}"}
)
assert response.status_code == 200
data = response.json()
assert len(data) >= 2
assert any(p["name"] == "Test Product" for p in data)
assert any(p["name"] == "Another Product" for p in data)
async def test_update_product(client: AsyncClient, user_token: str):
# First create a product
product_id = await test_create_product(client, user_token)
# Test updating the product
response = await client.put(
f"/api/v1/products/{product_id}",
json={
"name": "Updated Product",
"description": "Updated description",
"price": 149.99,
"category": "test",
"inventory_count": 20
},
headers={"Authorization": f"Bearer {user_token}"}
)
assert response.status_code == 200
data = response.json()
assert data["name"] == "Updated Product"
assert data["price"] == 149.99
assert data["inventory_count"] == 20
async def test_delete_product(client: AsyncClient, user_token: str):
# First create a product
product_id = await test_create_product(client, user_token)
# Test deleting the product
response = await client.delete(
f"/api/v1/products/{product_id}",
headers={"Authorization": f"Bearer {user_token}"}
)
assert response.status_code == 200
# Verify product is deleted
response = await client.get(
f"/api/v1/products/{product_id}",
headers={"Authorization": f"Bearer {user_token}"}
)
assert response.status_code == 404