#!/usr/bin/env python3 """ Test agent upload without authentication """ import requests import json import tempfile import zipfile from pathlib import Path from datetime import datetime import os def create_agent_style_session(): """Create a session package like the agent would""" temp_dir = Path(tempfile.mkdtemp()) session_id = f"sess_{datetime.now().strftime('%Y%m%dT%H%M%S')}_test" # Create session data like agent_v0 would session_data = { "schema_version": "rawsession_v1", "session_id": session_id, "agent_version": "0.1.0", "environment": { "platform": "linux", "hostname": "test-machine", "screen": { "primary_resolution": [1920, 1080], "display_scale": 1.0 } }, "user": { "id": "test_user", "label": "Test User" }, "context": { "customer": "Test Customer", "training_label": "Agent_Upload_Test", "notes": "Testing agent upload without auth" }, "started_at": datetime.now().isoformat() + "Z", "ended_at": datetime.now().isoformat() + "Z", "events": [ { "t": 1.0, "type": "mouse_click", "button": "left", "pos": [500, 300], "window": { "title": "Test Window", "app_name": "test.exe" }, "screenshot_id": "shot_0001" } ], "screenshots": [ { "screenshot_id": "shot_0001", "relative_path": "shots/shot_0001.png", "captured_at": datetime.now().isoformat() + "Z" } ] } # Create directory structure like agent_v0 session_dir = temp_dir / session_id session_dir.mkdir() shots_dir = session_dir / "shots" shots_dir.mkdir() # Write session JSON session_file = session_dir / f"{session_id}.json" with open(session_file, 'w') as f: json.dump(session_data, f, indent=2) # Create dummy screenshot screenshot_file = shots_dir / "shot_0001.png" png_data = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x02\x00\x00\x00\x90wS\xde\x00\x00\x00\tpHYs\x00\x00\x0b\x13\x00\x00\x0b\x13\x01\x00\x9a\x9c\x18\x00\x00\x00\x12IDATx\x9cc```bPPP\x00\x02\xac\x01\x00\x00\x05\x00\x01\r\n\x87\xdc\x00\x00\x00\x00IEND\xaeB`\x82' with open(screenshot_file, 'wb') as f: f.write(png_data) # Create ZIP like agent_v0 would zip_path = temp_dir / f"{session_id}.zip" with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf: for file_path in session_dir.rglob('*'): if file_path.is_file(): arcname = file_path.relative_to(temp_dir) zipf.write(file_path, arcname) return zip_path, session_id, temp_dir def test_agent_upload_no_auth(): """Test agent-style upload without authentication""" zip_path = None temp_dir = None try: # Create agent-style session zip_path, session_id, temp_dir = create_agent_style_session() print(f"Testing agent-style upload") print(f"Session ID: {session_id}") print(f"ZIP file: {zip_path.name}") print(f"ZIP size: {zip_path.stat().st_size} bytes") # Upload like agent would (but without auth headers) url = "http://127.0.0.1:8000/api/traces/upload" with open(zip_path, 'rb') as f: files = { "file": (zip_path.name, f, "application/zip") } data = { "session_id": session_id } # No authentication headers (since auth is disabled) response = requests.post( url, files=files, data=data, timeout=30 ) print(f"Status: {response.status_code}") print(f"Response: {response.text}") if response.status_code == 200: print("SUCCESS: Agent-style upload worked!") try: data = response.json() print(f"Server processed {data.get('events_count', 0)} events") print(f"Server processed {data.get('screenshots_count', 0)} screenshots") except: pass return True else: print(f"FAILED: Status {response.status_code}") return False except Exception as e: print(f"ERROR: {e}") return False finally: # Cleanup if temp_dir and temp_dir.exists(): import shutil try: shutil.rmtree(temp_dir) print(f"Cleaned up: {temp_dir}") except: pass if __name__ == "__main__": print("Testing Agent V0 Upload (No Auth)") print("=" * 40) success = test_agent_upload_no_auth() if success: print("Agent upload test passed!") print("The agent can now upload sessions without authentication.") else: print("Agent upload test failed!") exit(0 if success else 1)