File size: 11,013 Bytes
6eab5a6 | 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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | """
Unit and Integration Tests for ALLI Product Feedback System
Verifies:
- Feedback submission and validation
- Metadata sanitization (credential and token scrubbing)
- Screenshot upload with OWASP magic-bytes validation (PNG, JPEG, WebP)
- Rejection of spoofed/corrupted image payloads
- Rejection of oversized attachments (> 5MB)
- Authenticated attachment download
- User isolation in /mine
- Admin authorization and triage updates
"""
import pytest
from fastapi.testclient import TestClient
from main import app
from app.feedback.storage import MAX_ATTACHMENT_SIZE_BYTES
client = TestClient(app)
# Authentic binary magic headers for test fixtures
VALID_PNG_BYTES = b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06\x00\x00\x00\x1f\x15c4\x00\x00\x00\nIDATx\x9cc`\x00\x00\x00\x02\x00\x01H\xaf\xa4q\x00\x00\x00\x00IEND\xaeB`\x82"
VALID_JPEG_BYTES = b"\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x01\x00H\x00H\x00\x00\xff\xdb\x00C\x00\x08\x06\x06\x07\x06\x05\x08\x07\x07\x07\t\t\x08\n\x0c\x14\r\x0c\x0b\x0b\x0c\x19\x12\x13\x0f\x14\x1d\x1a\x1f\x1e\x1d\x1a\x1c\x1c $.' \",#\x1c\x1c(7),01444\x1f'9=82<.342\xff\xc0\x00\x0b\x08\x00\x01\x00\x01\x01\x01\x11\x00\xff\xc4\x00\x1f\x00\x00\x01\x05\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\xff\xda\x00\x08\x01\x01\x00\x00?\x00\xbf\x00\xff\xd9"
VALID_WEBP_BYTES = b"RIFF\x1a\x00\x00\x00WEBPVP8 \x0e\x00\x00\x000\x01\x00\x9d\x01*\x01\x00\x01\x00\x02\x004%\xa4%"
CORRUPT_SPOOFED_BYTES = b"<!DOCTYPE html><html><head><script>alert('xss')</script></head></html>"
def test_create_feedback_success():
"""Test standard feedback submission with valid data."""
payload = {
"title": "Canvas edge snapping misalignment",
"description": "Connecting Math node to Output node sometimes snaps to the center instead of right port.",
"expected_behavior": "Edge should snap cleanly to the input handle.",
"category": "bug",
"severity": "medium",
"page_url": "/studio",
"app_version": "2.1.0",
"client_metadata": {
"browser": "Chrome",
"screen_width": 1920,
"screen_height": 1080,
},
}
headers = {"X-User-Id": "user_123_test"}
response = client.post("/api/feedback", json=payload, headers=headers)
assert response.status_code == 201
data = response.json()
assert data["id"].startswith("fb_")
assert data["user_id"] == "user_123_test"
assert data["title"] == "Canvas edge snapping misalignment"
assert data["status"] == "new"
assert data["category"] == "bug"
assert data["severity"] == "medium"
assert data["client_metadata"]["browser"] == "Chrome"
assert len(data["attachments"]) == 0
def test_create_feedback_validation_error():
"""Verify validation triggers when title or description is too short."""
payload = {
"title": "ab", # Min length is 3
"description": "cd", # Min length is 5
}
response = client.post("/api/feedback", json=payload)
assert response.status_code == 422
def test_client_metadata_credential_sanitization():
"""Verify that credentials, auth tokens, and passwords in telemetry are scrubbed."""
payload = {
"title": "Token test feedback",
"description": "Testing that sensitive keys in metadata are automatically redacted.",
"client_metadata": {
"auth_token": "secret_session_token_12345",
"password": "my_super_secret_password",
"apiKey": "sk_test_99999",
"nested": {
"jwt": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.t-IDcSemACt8x4iTMCda8Yhe3iZaWbvV5XKSTbuAn0M",
"safe_field": "public_theme_dark",
},
},
}
response = client.post("/api/feedback", json=payload, headers={"X-User-Id": "user_security_audit"})
assert response.status_code == 201
meta = response.json()["client_metadata"]
assert meta["auth_token"] == "[REDACTED_SENSITIVE_VALUE]"
assert meta["password"] == "[REDACTED_SENSITIVE_VALUE]"
assert meta["apiKey"] == "[REDACTED_SENSITIVE_VALUE]"
assert meta["nested"]["jwt"] == "[REDACTED_SENSITIVE_VALUE]"
assert meta["nested"]["safe_field"] == "public_theme_dark"
def test_attachment_upload_valid_png():
"""Verify uploading authentic PNG screenshot."""
# First create feedback
fb_res = client.post(
"/api/feedback",
json={"title": "PNG attachment test", "description": "Attaching PNG screenshot."},
headers={"X-User-Id": "user_att_png"}
)
fb_id = fb_res.json()["id"]
# Upload attachment
files = {"file": ("screenshot.png", VALID_PNG_BYTES, "image/png")}
att_res = client.post(
f"/api/feedback/{fb_id}/attachments",
files=files,
headers={"X-User-Id": "user_att_png"}
)
assert att_res.status_code == 201
att_data = att_res.json()
assert att_data["attachment_id"].startswith("att_")
assert att_data["content_type"] == "image/png"
assert att_data["file_size"] == len(VALID_PNG_BYTES)
# Verify attachment is listed in feedback
get_res = client.get(f"/api/feedback/{fb_id}", headers={"X-User-Id": "user_att_png"})
assert get_res.status_code == 200
assert len(get_res.json()["attachments"]) == 1
def test_attachment_upload_valid_jpeg_and_webp():
"""Verify uploading authentic JPEG and WebP images."""
fb_res = client.post(
"/api/feedback",
json={"title": "Multi-format test", "description": "Testing JPEG and WebP formats."},
headers={"X-User-Id": "user_formats"}
)
fb_id = fb_res.json()["id"]
# 1. JPEG
jpeg_res = client.post(
f"/api/feedback/{fb_id}/attachments",
files={"file": ("photo.jpg", VALID_JPEG_BYTES, "image/jpeg")},
headers={"X-User-Id": "user_formats"}
)
assert jpeg_res.status_code == 201
assert jpeg_res.json()["content_type"] == "image/jpeg"
# 2. WebP
webp_res = client.post(
f"/api/feedback/{fb_id}/attachments",
files={"file": ("capture.webp", VALID_WEBP_BYTES, "image/webp")},
headers={"X-User-Id": "user_formats"}
)
assert webp_res.status_code == 201
assert webp_res.json()["content_type"] == "image/webp"
def test_attachment_upload_spoofed_file_rejected():
"""Verify that file with fraudulent .png name but non-image content is rejected."""
fb_res = client.post(
"/api/feedback",
json={"title": "Spoof test", "description": "Submitting spoofed file."},
headers={"X-User-Id": "user_spoof"}
)
fb_id = fb_res.json()["id"]
# Upload HTML payload disguised as PNG
files = {"file": ("exploit.png", CORRUPT_SPOOFED_BYTES, "image/png")}
att_res = client.post(
f"/api/feedback/{fb_id}/attachments",
files=files,
headers={"X-User-Id": "user_spoof"}
)
assert att_res.status_code == 400
assert "Security validation error" in att_res.json()["detail"]
def test_attachment_upload_oversized_rejected():
"""Verify that file exceeding 5MB is rejected."""
fb_res = client.post(
"/api/feedback",
json={"title": "Oversized test", "description": "Testing size limit."},
headers={"X-User-Id": "user_oversized"}
)
fb_id = fb_res.json()["id"]
# Generate oversized payload (5MB + 100 bytes) starting with PNG magic header
oversized = VALID_PNG_BYTES + (b"\x00" * (MAX_ATTACHMENT_SIZE_BYTES + 100))
files = {"file": ("giant.png", oversized, "image/png")}
att_res = client.post(
f"/api/feedback/{fb_id}/attachments",
files=files,
headers={"X-User-Id": "user_oversized"}
)
assert att_res.status_code == 413
def test_download_attachment_security():
"""Verify downloading attachment with secure streaming headers."""
fb_res = client.post(
"/api/feedback",
json={"title": "Download test", "description": "Testing download endpoint."},
headers={"X-User-Id": "user_dl"}
)
fb_id = fb_res.json()["id"]
att_res = client.post(
f"/api/feedback/{fb_id}/attachments",
files={"file": ("shot.png", VALID_PNG_BYTES, "image/png")},
headers={"X-User-Id": "user_dl"}
)
att_id = att_res.json()["attachment_id"]
# Download
dl_res = client.get(f"/api/feedback/{fb_id}/attachments/{att_id}", headers={"X-User-Id": "user_dl"})
assert dl_res.status_code == 200
assert dl_res.headers["content-type"] == "image/png"
assert dl_res.headers.get("x-content-type-options") == "nosniff"
assert dl_res.content == VALID_PNG_BYTES
# Attempt unauthorized download by another user
denied = client.get(f"/api/feedback/{fb_id}/attachments/{att_id}", headers={"X-User-Id": "unauthorized_user"})
assert denied.status_code == 403
def test_list_user_feedback_isolation():
"""Verify /api/feedback/mine only returns the calling user's feedback."""
client.post(
"/api/feedback",
json={"title": "User A Feedback 1", "description": "Description A1"},
headers={"X-User-Id": "user_alice"}
)
client.post(
"/api/feedback",
json={"title": "User B Feedback 1", "description": "Description B1"},
headers={"X-User-Id": "user_bob"}
)
alice_mine = client.get("/api/feedback/mine", headers={"X-User-Id": "user_alice"}).json()
bob_mine = client.get("/api/feedback/mine", headers={"X-User-Id": "user_bob"}).json()
assert all(f["user_id"] == "user_alice" for f in alice_mine)
assert all(f["user_id"] == "user_bob" for f in bob_mine)
def test_admin_triage_flow():
"""Verify admin triage queue, status filter, and patch endpoint."""
fb_res = client.post(
"/api/feedback",
json={"title": "Triage Target", "description": "To be resolved by admin.", "category": "usability"},
headers={"X-User-Id": "regular_user"}
)
fb_id = fb_res.json()["id"]
# Regular user attempting admin view is forbidden
forbidden_res = client.get("/api/feedback/admin/all", headers={"X-User-Id": "regular_user"})
assert forbidden_res.status_code == 403
# Admin access
admin_headers = {"X-User-Id": "admin_user", "X-Admin-Role": "true"}
all_res = client.get("/api/feedback/admin/all?category=usability", headers=admin_headers)
assert all_res.status_code == 200
assert any(f["id"] == fb_id for f in all_res.json())
# Admin update triage
patch_payload = {
"status": "in_progress",
"admin_notes": "Investigated by core team; fix planned for release v2.1.1.",
"assigned_to": "engineer_alex",
"priority": "P1",
}
patch_res = client.patch(f"/api/feedback/admin/{fb_id}", json=patch_payload, headers=admin_headers)
assert patch_res.status_code == 200
updated = patch_res.json()
assert updated["status"] == "in_progress"
assert updated["assigned_to"] == "engineer_alex"
assert "Investigated by core team" in updated["admin_notes"]
|