# tests/test_product.py import pytest class TestProductsAPI: def test_get_products_empty(self, client, auth_headers): """Test getting products when none exist""" response = client.get("/api/v1/product", headers=auth_headers) assert response.status_code == 200 data = response.json() assert data["products"] == [] assert data["total"] == 0 def test_get_products_with_data(self, client, auth_headers, test_product): """Test getting products with data""" response = client.get("/api/v1/product", headers=auth_headers) assert response.status_code == 200 data = response.json() assert len(data["products"]) == 1 assert data["total"] == 1 assert data["products"][0]["product_id"] == "TEST001" def test_get_products_with_filters(self, client, auth_headers, test_product): """Test filtering products""" # Test brand filter response = client.get("/api/v1/product?brand=TestBrand", headers=auth_headers) assert response.status_code == 200 assert response.json()["total"] == 1 # Test marketplace filter response = client.get("/api/v1/product?marketplace=Letzshop", headers=auth_headers) assert response.status_code == 200 assert response.json()["total"] == 1 # Test search response = client.get("/api/v1/product?search=Test", headers=auth_headers) assert response.status_code == 200 assert response.json()["total"] == 1 def test_create_product(self, client, auth_headers): """Test creating a new product""" product_data = { "product_id": "NEW001", "title": "New Product", "description": "A new product", "price": "15.99", "brand": "NewBrand", "gtin": "9876543210987", "availability": "in stock", "marketplace": "Amazon" } response = client.post("/api/v1/product", headers=auth_headers, json=product_data) assert response.status_code == 200 data = response.json() assert data["product_id"] == "NEW001" assert data["title"] == "New Product" assert data["marketplace"] == "Amazon" def test_create_product_duplicate_id(self, client, auth_headers, test_product): """Test creating product with duplicate ID""" product_data = { "product_id": test_product.product_id, "title": test_product.title, "description": "A new product", "price": "15.99", "brand": "NewBrand", "gtin": "9876543210987", "availability": "in stock", "marketplace": "Amazon" } response = client.post("/api/v1/product", headers=auth_headers, json=product_data) # Debug output print(f"Status Code: {response.status_code}") print(f"Response Content: {response.content}") try: print(f"Response JSON: {response.json()}") except: print("Could not parse response as JSON") assert response.status_code == 400 assert "already exists" in response.json()["detail"] def test_get_product_by_id(self, client, auth_headers, test_product): """Test getting specific product""" response = client.get(f"/api/v1/product/{test_product.product_id}", headers=auth_headers) assert response.status_code == 200 data = response.json() assert data["product"]["product_id"] == test_product.product_id assert data["product"]["title"] == test_product.title def test_get_nonexistent_product(self, client, auth_headers): """Test getting nonexistent product""" response = client.get("/api/v1/product/NONEXISTENT", headers=auth_headers) assert response.status_code == 404 def test_update_product(self, client, auth_headers, test_product): """Test updating product""" update_data = { "title": "Updated Product Title", "price": "25.99" } response = client.put( f"/api/v1/product/{test_product.product_id}", headers=auth_headers, json=update_data ) assert response.status_code == 200 data = response.json() assert data["title"] == "Updated Product Title" assert data["price"] == "25.99" def test_delete_product(self, client, auth_headers, test_product): """Test deleting product""" response = client.delete( f"/api/v1/product/{test_product.product_id}", headers=auth_headers ) assert response.status_code == 200 assert "deleted successfully" in response.json()["message"] def test_get_product_without_auth(self, client): """Test that product endpoints require authentication""" response = client.get("/api/v1/product") assert response.status_code == 401 # No authorization header