test_avatar_s3.py 1.95 KB
#!/usr/bin/env python3
"""GREEN test for S3: chat page avatar rendering."""
import subprocess, sys

BASE = "http://127.0.0.1:5000"
PASS = 0
FAIL = 0

def check(name, condition, detail=""):
    global PASS, FAIL
    if condition:
        print(f"  ✅ {name}"); PASS += 1
    else:
        print(f"  ❌ {name} {detail}"); FAIL += 1

print("=== S3 GREEN: Chat avatar rendering ===")

r = subprocess.run(["curl", "-s", f"{BASE}/chat/3164323182@chatroom",
    "--connect-timeout", "5", "--max-time", "10"],
    capture_output=True, text=True, timeout=15)
html = r.stdout

# 1. Avatar img tag with /media/avatar/
check("avatar <img> with /media/avatar/ ref",
      'media/avatar/' in html and 'esc(username)' in html,
      "no avatar img reference in JS")

# 2. onerror fallback creates div
check("onerror fallback creates div",
      "createElement(" in html and "msg-avatar" in html and "insertBefore" in html,
      "missing fallback logic")

# 3. No Jinja2 escaping bugs
for pattern, name in [
    ("\\\\\\\\", "quadruple backslash"),
    ('style=\\\\"', "style escaped double quote"),
    ('data-action=\\\\"', "data-action escaped quote"),
]:
    if pattern in html:
        check(f"no {name}", False, f"found bug pattern")
    else:
        check(f"no {name}", True, "")

# 4. Letter fallback still exists for no-username case
check("letter fallback for no username",
      'msg-avatar" style="background:' in html,
      "no letter fallback found")

# 5. JS syntax: check no broken strings (basic)
# The template should have balanced quotes
check("page loads without 500 error", 
      r.returncode == 0 and len(html) > 500,
      f"page size={len(html)}")

# 6. Check the onerror JS is syntactically valid (no triple escapes)
check("no triple backslash in onerror",
      "\\\\\\'" not in html,
      "triple backslash found")

print(f"\n{'='*40}")
print(f"Results: {PASS} passed, {FAIL} failed")
if FAIL > 0:
    print("FAIL!")
    sys.exit(1)
else:
    print("GREEN!")
    sys.exit(0)