import requests import os import sys BASE_URL = "http://localhost:8000" ADMIN_USERNAME = "admin_test" ADMIN_EMAIL = "admin_test@example.com" ADMIN_PASSWORD = "password" def create_admin_user(): # Use the existing create_admin.py logic or just call the register endpoint and then promote? # Or just rely on the create_admin.py script. # Let's try to register and then promote if possible, or just use create_admin.py # Since I can't easily import create_admin.py if it's not a module, I'll just run it via subprocess or assume it's run. # Actually, I'll just use the create_admin.py script in the main flow. pass def test_create_product_with_image(): print("Testing create product with image...") # Create a dummy image with open("test_image.jpg", "wb") as f: f.write(b"fake image content") url = f"{BASE_URL}/admin/products" headers = {"X-Username": ADMIN_USERNAME} data = { "name": "Test Product", "calories": 100, "protein": 10, "fat": 5, "carbohydrates": 20, "sodium": 50, "sugar": 5 } files = { "image": ("test_image.jpg", open("test_image.jpg", "rb"), "image/jpeg") } try: response = requests.post(url, headers=headers, data=data, files=files) if response.status_code != 201: print(f"Failed to create product: {response.status_code} {response.text}") return False product = response.json() print(f"Product created: {product}") if not product.get("image_path"): print("Error: image_path is missing in response") return False if ".tmp/test_image.jpg" not in product["image_path"]: print(f"Error: Unexpected image path: {product['image_path']}") return False # Verify file exists if not os.path.exists(product["image_path"]): print(f"Error: File does not exist at {product['image_path']}") return False print("Image upload verified successfully.") return True except Exception as e: print(f"Exception: {e}") return False finally: # Cleanup if os.path.exists("test_image.jpg"): os.remove("test_image.jpg") def test_list_products(): print("Testing list products...") url = f"{BASE_URL}/admin/products" headers = {"X-Username": ADMIN_USERNAME} response = requests.get(url, headers=headers) if response.status_code != 200: print(f"Failed to list products: {response.status_code}") return False products = response.json() # Find our test product found = False for p in products: if p["name"] == "Test Product": found = True if not p.get("image_path"): print("Error: image_path missing in list response") return False print(f"Found product in list with image_path: {p['image_path']}") break if not found: print("Error: Test product not found in list") return False print("List products verified successfully.") return True if __name__ == "__main__": if not test_create_product_with_image(): sys.exit(1) if not test_list_products(): sys.exit(1) print("All tests passed!")