Download tests/test_feedback.py from Rsnarsna/auto: direct link, hf CLI and curl.
- Browser
- Download file 11 kB
-
https://huggingface.co/spaces/Rsnarsna/auto/resolve/main/tests/test_feedback.py
- Command line
-
hf download hf://spaces/Rsnarsna/auto/tests/test_feedback.py
-
curl -L -o test_feedback.py https://huggingface.co/spaces/Rsnarsna/auto/resolve/main/tests/test_feedback.py
11 kB
| """ | |
| 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"] | |