import urllib.request
import urllib.error
import json

print("Testing POST /api/v1/auth/login on port 5005")
data = json.dumps({"username": "e2etest", "password": "Test12345!"}).encode("utf-8")
req = urllib.request.Request(
    "http://127.0.0.1:5005/api/v1/auth/login",
    data=data,
    headers={"Content-Type": "application/json", "Accept": "application/json"},
)
try:
    with urllib.request.urlopen(req) as resp:
        body = resp.read().decode()
        parsed = json.loads(body)
        print(f"[{resp.status}] SUCCESS!")
        print("  access_token:", parsed.get("access_token", "")[:50] + "...")
        print("  user:", parsed.get("user"))
except urllib.error.HTTPError as e:
    body = e.read().decode()
    print(f"[{e.code}] ERROR: {body[:300]}")
except Exception as e:
    print(f"[ERR]: {e}")
