test_s1_status.py 2.59 KB
"""S1 RED: Test PUT /api/learn/<id>/status endpoint"""
import requests
import sys, os

BASE = "http://127.0.0.1:5000"

errors = []
def check(label, ok, detail=""):
    if not ok:
        errors.append(f"FAIL: {label}: {detail}")
        print(f"  ❌ {label}: {detail}")
    else:
        print(f"  ✅ {label}")

print("=== S1: PUT /api/learn/<id>/status ===")

# Get a known learn item from the queue
r = requests.get(f"{BASE}/api/learn-queue?status=pending_review")
data = r.json()
items = (data.get("data", []) or [])
if not items:
    # Try other statuses
    for s in ["pending_learn", "applied", "approved"]:
        r = requests.get(f"{BASE}/api/learn-queue?status={s}")
        items = (r.json().get("data", []) or [])
        if items:
            break

if not items:
    print("⚠️  No learn items found, using mock test only")
    test_id = 99999
else:
    test_id = items[0]["id"]
    test_status = items[0].get("status", "?")
    print(f"Using learn item id={test_id}, current status={test_status}")

# TC1: Valid status update
print("\nTC1: Valid status update")
new_status = "pending_review"
r = requests.put(
    f"{BASE}/api/learn/{test_id}/status",
    json={"status": new_status},
    headers={"Content-Type": "application/json"}
)
check("HTTP 200", r.status_code == 200, f"Got {r.status_code}: {r.text[:200]}")
resp = r.json()
check("ok == True", resp.get("ok") == True, str(resp))
check("status matches", resp.get("data", {}).get("status") == new_status,
      f"Expected {new_status}, got {resp.get('data', {}).get('status')}")

# TC2: Invalid status value
print("\nTC2: Invalid status value")
r = requests.put(
    f"{BASE}/api/learn/{test_id}/status",
    json={"status": "invalid_value_xyz"},
    headers={"Content-Type": "application/json"}
)
check("HTTP 400", r.status_code == 400, f"Got {r.status_code}: {r.text[:200]}")
resp2 = r.json()
check("ok == False", resp2.get("ok") == False, str(resp2))

# TC3: Non-existent ID
print("\nTC3: Non-existent ID")
r = requests.put(
    f"{BASE}/api/learn/99999/status",
    json={"status": "applied"},
    headers={"Content-Type": "application/json"}
)
check("HTTP 404", r.status_code == 404, f"Got {r.status_code}: {r.text[:200]}")

# TC4: Missing status field
print("\nTC4: Missing status field in request body")
r = requests.put(
    f"{BASE}/api/learn/{test_id}/status",
    json={},
    headers={"Content-Type": "application/json"}
)
check("HTTP 400 or 422", r.status_code in (400, 422), f"Got {r.status_code}")

print(f"\n=== Results: {len(errors)} failures ===")
if errors:
    print("\n".join(errors))
    sys.exit(1)
else:
    print("ALL TESTS PASSED ✅")
    sys.exit(0)