test_avatar_s3.py
1.95 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
#!/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)