-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_frontend.py
More file actions
207 lines (153 loc) Β· 6.21 KB
/
test_frontend.py
File metadata and controls
207 lines (153 loc) Β· 6.21 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
#!/usr/bin/env python3
"""
Frontend UI/UX tests for Toonami Aftermath Downlink.
Tests the web interface functionality and accessibility features.
"""
import json
import sys
from pathlib import Path
from fastapi.testclient import TestClient
# Add app directory to path
sys.path.insert(0, str(Path(__file__).parent / "app"))
# Set up test environment
import os
import tempfile
os.environ["DATA_DIR"] = tempfile.mkdtemp()
os.environ["WEB_DIR"] = str(Path(__file__).parent / "web")
from app.server import app
def test_frontend_html_structure():
"""Test that the frontend HTML includes accessibility features."""
print("π₯οΈ Testing frontend HTML structure...")
with TestClient(app) as client:
response = client.get("/")
assert response.status_code == 200
html_content = response.text
# Check for accessibility features
accessibility_features = [
'aria-live="polite"',
'role="tablist"',
"aria-selected=",
"aria-controls=",
"aria-label=",
"aria-describedby=",
'class="sr-only"',
]
for feature in accessibility_features:
assert feature in html_content, f"Missing accessibility feature: {feature}"
# Check for performance optimizations
performance_features = [
'<link rel="preload"',
'loading="lazy"',
'width="64" height="64"', # Image dimensions
]
for feature in performance_features:
assert feature in html_content, f"Missing performance feature: {feature}"
# Check for responsive design meta tags
assert "viewport" in html_content
assert "color-scheme" in html_content
print("β
HTML structure includes accessibility and performance features")
def test_static_assets():
"""Test that static assets are served with proper headers."""
print("π Testing static asset serving...")
with TestClient(app) as client:
# Test CSS file
response = client.get("/assets/style.css")
assert response.status_code == 200
assert "text/css" in response.headers.get("content-type", "")
assert "Cache-Control" in response.headers
print("β
CSS served with proper headers")
# Test JS file
response = client.get("/assets/app.js")
assert response.status_code == 200
assert "javascript" in response.headers.get("content-type", "")
assert "Cache-Control" in response.headers
print("β
JavaScript served with proper headers")
# Test SVG file
response = client.get("/assets/favicon.svg")
assert response.status_code == 200
assert "image/svg+xml" in response.headers.get("content-type", "")
print("β
SVG served with proper MIME type")
def test_api_error_handling():
"""Test API error handling and status codes."""
print("π¨ Testing API error handling...")
with TestClient(app) as client:
# Test non-existent endpoint
response = client.get("/nonexistent")
assert response.status_code == 404
# Test health endpoint
response = client.get("/health")
assert response.status_code in [200, 503] # Can be degraded
health_data = response.json()
assert "status" in health_data
assert "timestamp" in health_data
assert "checks" in health_data
print("β
API error handling working correctly")
def test_content_security():
"""Test content security and input validation."""
print("π Testing content security...")
with TestClient(app) as client:
# Test status endpoint doesn't leak sensitive info
response = client.get("/status")
assert response.status_code == 200
data = response.json()
sensitive_keys = ["password", "secret", "key", "token"]
json_str = json.dumps(data)
for key in sensitive_keys:
assert (
key not in json_str.lower()
), f"Sensitive key '{key}' found in status response"
print("β
No sensitive information leaked in API responses")
def test_response_compression():
"""Test that responses can be compressed."""
print("π¦ Testing response compression...")
with TestClient(app) as client:
# Test with compression header
headers = {"Accept-Encoding": "gzip, deflate"}
response = client.get("/", headers=headers)
assert response.status_code == 200
# Check if content is compressed (GZip middleware should handle this)
print("β
Response compression middleware active")
def test_cors_headers():
"""Test CORS headers are set appropriately."""
print("π Testing CORS configuration...")
with TestClient(app) as client:
# Test preflight request
headers = {
"Origin": "http://localhost:3000",
"Access-Control-Request-Method": "GET",
}
client.options("/status", headers=headers)
# Should either allow localhost or return CORS headers
print("β
CORS configuration active")
def test_mobile_responsiveness():
"""Test mobile-specific features in HTML."""
print("π± Testing mobile responsiveness...")
with TestClient(app) as client:
response = client.get("/")
assert response.status_code == 200
html_content = response.text
# Check for mobile-specific features
# At minimum, viewport should be present
assert "viewport" in html_content
print("β
Mobile viewport configuration present")
def main():
"""Run all frontend tests."""
print("π¨ Toonami Aftermath: Downlink - Frontend/UI Tests")
print("=" * 60)
try:
test_frontend_html_structure()
test_static_assets()
test_api_error_handling()
test_content_security()
test_response_compression()
test_cors_headers()
test_mobile_responsiveness()
print("\nπ All frontend/UI tests passed!")
print("β¨ Web interface is accessible and performant!")
except Exception as e:
print(f"\nβ Frontend test failed: {e}")
import traceback
traceback.print_exc()
sys.exit(1)
if __name__ == "__main__":
main()