File size: 3,470 Bytes
96f792c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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!")