#!/usr/bin/env python3 """ Comprehensive test for image upload functionality Tests various scenarios to ensure robust upload handling """ import requests import sys from pathlib import Path # Add backend to path sys.path.insert(0, str(Path(__file__).parent)) from test_upload import SECRET_KEY, ALGORITHM import jwt from datetime import datetime, timezone, timedelta # Generate admin token def get_admin_token(): expires = datetime.now(timezone.utc) + timedelta(hours=24) token_data = { "sub": "0739d174-9409-4bd1-b749-69f9e5546467f", # Admin user ID "email": "admin@techzone.com", "exp": expires } return jwt.encode(token_data, SECRET_KEY, algorithm=ALGORITHM) def test_upload(filename, content, content_type=None): """Test uploading a file""" token = get_admin_token() files = {'file': (filename, content, content_type)} headers = {'Authorization': f'Bearer {token}'} print(f"\n{'='*60}") print(f"Testing upload: {filename}") print(f"Content-Type: {content_type}") print(f"Size: {len(content)} bytes") print(f"{'='*60}") try: response = requests.post( 'http://localhost:8181/api/upload/image', files=files, headers=headers ) if response.status_code == 200: print(f"✅ SUCCESS: {response.json()}") return True else: print(f"❌ FAILED ({response.status_code}): {response.json()}") return False except Exception as e: print(f"❌ ERROR: {e}") return False if __name__ == "__main__": # Test 1: Real JPEG with proper content type with open('/tmp/test-image.jpg', 'rb') as f: content = f.read() test_upload('test.jpg', content, 'image/jpeg') # Test 2: JPEG with no content type (browser might do this) test_upload('test2.jpg', content, None) # Test 3: PNG extension test_upload('test.png', content, 'image/png') # Test 4: File with uppercase extension test_upload('TEST.JPG', content, 'image/jpeg') # Test 5: No extension but has content type test_upload('image', content, 'image/jpeg') # Test 6: Invalid extension test_upload('file.txt', b'not an image', 'text/plain') print(f"\n{'='*60}") print("Test complete!") print(f"{'='*60}\n")