-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_comprehensive.py
More file actions
429 lines (370 loc) · 12.4 KB
/
Copy pathtest_comprehensive.py
File metadata and controls
429 lines (370 loc) · 12.4 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#!/usr/bin/env python3
import requests
import json
import sys
from decimal import Decimal
BASE_URL = "http://127.0.0.1:8000"
TENANT_ID = "test_tenant_001"
def test_endpoint(method, url, data=None, files=None, headers=None, expected_status=200, description=""):
"""Test an endpoint and return the response"""
try:
if method.upper() == "GET":
response = requests.get(url, headers=headers)
elif method.upper() == "POST":
if files:
response = requests.post(url, data=data, files=files, headers=headers)
else:
response = requests.post(url, json=data, headers=headers)
elif method.upper() == "PUT":
if files:
response = requests.put(url, data=data, files=files, headers=headers)
else:
response = requests.put(url, json=data, headers=headers)
elif method.upper() == "PATCH":
if files:
response = requests.patch(url, data=data, files=files, headers=headers)
else:
response = requests.patch(url, json=data, headers=headers)
elif method.upper() == "DELETE":
response = requests.delete(url, headers=headers)
status_ok = response.status_code == expected_status
print(f"{'✓' if status_ok else '✗'} {description}")
print(f" {method} {url}")
print(f" Status: {response.status_code} (expected: {expected_status})")
if not status_ok:
print(f" Response: {response.text[:200]}...")
print()
return response, status_ok
except Exception as e:
print(f"✗ {description}")
print(f" Error: {e}")
print()
return None, False
def main():
print("Comprehensive DoxiiCMS Backend API Testing")
print("=" * 60)
results = {"passed": 0, "failed": 0}
# Test 1: Basic endpoints
print("\n1. BASIC ENDPOINTS")
print("-" * 20)
_, ok = test_endpoint("GET", f"{BASE_URL}/", description="Root endpoint")
if ok:
results["passed"] += 1
else:
results["failed"] += 1
_, ok = test_endpoint("GET", f"{BASE_URL}/health", description="Health check")
if ok:
results["passed"] += 1
else:
results["failed"] += 1
_, ok = test_endpoint("OPTIONS", f"{BASE_URL}/any-path", expected_status=400, description="CORS OPTIONS")
if ok:
results["passed"] += 1
else:
results["failed"] += 1
# Test 2: Tenant middleware
print("\n2. TENANT MIDDLEWARE")
print("-" * 20)
_, ok = test_endpoint(
"GET", f"{BASE_URL}/tenant/nonexistent/products", expected_status=404, description="Nonexistent tenant"
)
if ok:
results["passed"] += 1
else:
results["failed"] += 1
_, ok = test_endpoint("GET", f"{BASE_URL}/invalid-path", expected_status=400, description="Invalid path format")
if ok:
results["passed"] += 1
else:
results["failed"] += 1
# Test 3: Categories API
print("\n3. CATEGORIES API")
print("-" * 20)
# List categories
_, ok = test_endpoint("GET", f"{BASE_URL}/tenant/{TENANT_ID}/categories", description="List categories")
if ok:
results["passed"] += 1
else:
results["failed"] += 1
# Create parent category
parent_data = {
"name": "Electronics",
"slug": "electronics",
"description": "Electronic devices",
"is_active": True,
"sort_order": 10,
}
resp, ok = test_endpoint(
"POST", f"{BASE_URL}/tenant/{TENANT_ID}/categories", data=parent_data, description="Create parent category"
)
if ok:
results["passed"] += 1
else:
results["failed"] += 1
parent_id = resp.json()["id"] if resp and resp.status_code == 201 else None
# Create child category
child_data = {
"name": "Smartphones",
"slug": "smartphones",
"description": "Mobile phones",
"parent_id": parent_id,
"is_active": True,
"sort_order": 1,
}
resp, ok = test_endpoint(
"POST", f"{BASE_URL}/tenant/{TENANT_ID}/categories", data=child_data, description="Create child category"
)
if ok:
results["passed"] += 1
else:
results["failed"] += 1
child_id = resp.json()["id"] if resp and resp.status_code == 201 else None
# Test category tree
_, ok = test_endpoint("GET", f"{BASE_URL}/tenant/{TENANT_ID}/categories/tree", description="Category tree")
if ok:
results["passed"] += 1
else:
results["failed"] += 1
# Test filtering
_, ok = test_endpoint(
"GET", f"{BASE_URL}/tenant/{TENANT_ID}/categories?is_active=eq.true", description="Filter active categories"
)
if ok:
results["passed"] += 1
else:
results["failed"] += 1
# Test ordering
_, ok = test_endpoint(
"GET", f"{BASE_URL}/tenant/{TENANT_ID}/categories?order=name.asc", description="Order categories by name"
)
if ok:
results["passed"] += 1
else:
results["failed"] += 1
# Test pagination
_, ok = test_endpoint(
"GET", f"{BASE_URL}/tenant/{TENANT_ID}/categories?limit=5&offset=0", description="Paginate categories"
)
if ok:
results["passed"] += 1
else:
results["failed"] += 1
# Test 4: Products API
print("\n4. PRODUCTS API")
print("-" * 20)
# List products (empty)
_, ok = test_endpoint("GET", f"{BASE_URL}/tenant/{TENANT_ID}/products", description="List products (empty)")
if ok:
results["passed"] += 1
else:
results["failed"] += 1
# Create product with multipart data
product_data = {
"name": "iPhone 15",
"slug": "iphone-15",
"description": "Latest iPhone model",
"short_description": "New iPhone",
"sku": "IPH15-001",
"price": 999.99,
"compare_at_price": 1099.99,
"cost_price": 600.00,
"inventory_quantity": 50,
"track_inventory": True,
"continue_selling_when_out_of_stock": False,
"weight": 0.2,
"weight_unit": "kg",
"status": "active",
"is_featured": True,
"seo_title": "iPhone 15 - Latest Model",
"seo_description": "Buy the latest iPhone 15",
"category_id": child_id,
}
# Create product with multipart form data
files = {
"product_data": (None, json.dumps(product_data), "application/json"),
}
# Note: For testing without actual files, we'll create product without media
resp, ok = test_endpoint(
"POST",
f"{BASE_URL}/tenant/{TENANT_ID}/products",
data=files,
files=files,
description="Create product without media",
)
if ok:
results["passed"] += 1
else:
results["failed"] += 1
product_id = resp.json()["id"] if resp and resp.status_code == 201 else None
# Get product by ID
_, ok = test_endpoint(
"GET", f"{BASE_URL}/tenant/{TENANT_ID}/products/{product_id}", description="Get product by ID"
)
if ok:
results["passed"] += 1
else:
results["failed"] += 1
# Test product filtering
_, ok = test_endpoint(
"GET", f"{BASE_URL}/tenant/{TENANT_ID}/products?status=eq.active", description="Filter active products"
)
if ok:
results["passed"] += 1
else:
results["failed"] += 1
# Test product search
_, ok = test_endpoint(
"GET", f"{BASE_URL}/tenant/{TENANT_ID}/products?name=ilike.*iPhone*", description="Search products by name"
)
if ok:
results["passed"] += 1
else:
results["failed"] += 1
# Test 5: Orders API
print("\n5. ORDERS API")
print("-" * 20)
# Create customer first (using auth register as customer)
customer_data = {
"username": "customer1",
"email": "customer1@example.com",
"password": "password123",
"first_name": "John",
"last_name": "Doe",
}
resp, ok = test_endpoint(
"POST", f"{BASE_URL}/tenant/{TENANT_ID}/auth/register", data=customer_data, description="Register customer"
)
if ok:
results["passed"] += 1
else:
results["failed"] += 1
customer_id = resp.json()["data"]["id"] if resp and resp.status_code == 201 else 1
# Create order
order_data = {
"customer_id": customer_id,
"order_number": "ORD-001",
"status": "pending",
"payment_status": "pending",
"fulfillment_status": "unfulfilled",
"notes": "Test order",
"items": [{"product_id": product_id, "quantity": 2, "unit_price": 999.99}],
}
resp, ok = test_endpoint(
"POST", f"{BASE_URL}/tenant/{TENANT_ID}/orders", data=order_data, description="Create order"
)
if ok:
results["passed"] += 1
else:
results["failed"] += 1
order_id = resp.json()["id"] if resp and resp.status_code == 201 else None
# List orders
_, ok = test_endpoint("GET", f"{BASE_URL}/tenant/{TENANT_ID}/orders", description="List orders")
if ok:
results["passed"] += 1
else:
results["failed"] += 1
# Get order by ID
_, ok = test_endpoint("GET", f"{BASE_URL}/tenant/{TENANT_ID}/orders/{order_id}", description="Get order by ID")
if ok:
results["passed"] += 1
else:
results["failed"] += 1
# Test order filtering
_, ok = test_endpoint(
"GET", f"{BASE_URL}/tenant/{TENANT_ID}/orders?status=eq.pending", description="Filter orders by status"
)
if ok:
results["passed"] += 1
else:
results["failed"] += 1
# Test 6: Auth API
print("\n6. AUTH API")
print("-" * 20)
# Login
login_data = {"username": "customer1", "password": "password123"}
resp, ok = test_endpoint(
"POST", f"{BASE_URL}/tenant/{TENANT_ID}/auth/login", data=login_data, description="Login user"
)
if ok:
results["passed"] += 1
else:
results["failed"] += 1
token = resp.json()["data"]["token"] if resp and resp.status_code == 200 else None
# Test authenticated endpoint
headers = {"Authorization": f"Bearer {token}"} if token else None
_, ok = test_endpoint(
"GET", f"{BASE_URL}/tenant/{TENANT_ID}/auth/me", headers=headers, description="Get current user"
)
if ok:
results["passed"] += 1
else:
results["failed"] += 1
# Test 7: Edge cases and error handling
print("\n7. EDGE CASES & ERROR HANDLING")
print("-" * 20)
# Duplicate category slug
_, ok = test_endpoint(
"POST",
f"{BASE_URL}/tenant/{TENANT_ID}/categories",
data=parent_data,
expected_status=400,
description="Duplicate category slug",
)
if ok:
results["passed"] += 1
else:
results["failed"] += 1
# Duplicate product SKU
_, ok = test_endpoint(
"POST",
f"{BASE_URL}/tenant/{TENANT_ID}/products",
data=files,
files=files,
expected_status=400,
description="Duplicate product SKU",
)
if ok:
results["passed"] += 1
else:
results["failed"] += 1
# Invalid product ID
_, ok = test_endpoint(
"GET", f"{BASE_URL}/tenant/{TENANT_ID}/products/99999", expected_status=404, description="Invalid product ID"
)
if ok:
results["passed"] += 1
else:
results["failed"] += 1
# Invalid order ID
_, ok = test_endpoint(
"GET", f"{BASE_URL}/tenant/{TENANT_ID}/orders/99999", expected_status=404, description="Invalid order ID"
)
if ok:
results["passed"] += 1
else:
results["failed"] += 1
# Test 8: Internal API
print("\n8. INTERNAL API")
print("-" * 20)
# List database configs
_, ok = test_endpoint("GET", f"{BASE_URL}/internal/database/configs", description="List database configs")
if ok:
results["passed"] += 1
else:
results["failed"] += 1
# Get database status
_, ok = test_endpoint("GET", f"{BASE_URL}/internal/database/{TENANT_ID}/status", description="Get database status")
if ok:
results["passed"] += 1
else:
results["failed"] += 1
# Summary
print("\n" + "=" * 60)
print("TEST SUMMARY")
print("=" * 60)
print(f"Passed: {results['passed']}")
print(f"Failed: {results['failed']}")
print(f"Total: {results['passed'] + results['failed']}")
print(f"Success Rate: {(results['passed'] / (results['passed'] + results['failed']) * 100):.1f}%")
if __name__ == "__main__":
main()