#!/usr/bin/env python """ Test script for the 3-step avatar upload process. This script will: 1. Request an upload URL 2. Upload an image to Cloudflare 3. Save the avatar reference """ import requests # Configuration BASE_URL = "http://127.0.0.1:8000" API_BASE = f"{BASE_URL}/api/v1" # You'll need to get these from your browser's developer tools or login endpoint ACCESS_TOKEN = "your_jwt_token_here" # Replace with actual token REFRESH_TOKEN = "your_refresh_token_here" # Replace with actual token # Headers for authenticated requests HEADERS = { "Authorization": f"Bearer {ACCESS_TOKEN}", "Content-Type": "application/json", } def step1_get_upload_url(): """Step 1: Get upload URL from django-cloudflareimages-toolkit""" print("Step 1: Requesting upload URL...") url = f"{API_BASE}/cloudflare-images/api/upload-url/" data = { "metadata": { "type": "avatar", "userId": "7627" # Replace with your user ID }, "require_signed_urls": False } response = requests.post(url, json=data, headers=HEADERS) print(f"Status: {response.status_code}") print(f"Response: {response.json()}") if response.status_code == 201: result = response.json() return result["upload_url"], result["cloudflare_id"] else: raise Exception(f"Failed to get upload URL: {response.text}") def step2_upload_image(upload_url): """Step 2: Upload image directly to Cloudflare""" print("\nStep 2: Uploading image to Cloudflare...") # Create a simple test image (1x1 pixel PNG) # This is a minimal valid PNG file 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\xd2\x00\x00\x00\x05\x00\x01\r\n-\xdb\x00\x00\x00\x00IEND\xaeB`\x82' files = { 'file': ('test_avatar.png', png_data, 'image/png') } # Upload to Cloudflare (no auth headers needed for direct upload) response = requests.post(upload_url, files=files) print(f"Status: {response.status_code}") print(f"Response: {response.json()}") if response.status_code in [200, 201]: return response.json() else: raise Exception(f"Failed to upload image: {response.text}") def step3_save_avatar(cloudflare_id): """Step 3: Save avatar reference in our system""" print("\nStep 3: Saving avatar reference...") url = f"{API_BASE}/accounts/profile/avatar/save/" data = { "cloudflare_image_id": cloudflare_id } response = requests.post(url, json=data, headers=HEADERS) print(f"Status: {response.status_code}") print(f"Response: {response.json()}") if response.status_code == 200: return response.json() else: raise Exception(f"Failed to save avatar: {response.text}") def main(): """Run the complete 3-step process""" try: # Step 1: Get upload URL upload_url, cloudflare_id = step1_get_upload_url() # Step 2: Upload image upload_result = step2_upload_image(upload_url) # Step 3: Save avatar reference save_result = step3_save_avatar(cloudflare_id) print("\nāœ… Success! Avatar upload completed.") print(f"Avatar URL: {save_result.get('avatar_url')}") except Exception as e: print(f"\nāŒ Error: {e}") if __name__ == "__main__": print("šŸš€ Testing 3-step avatar upload process...") print("āš ļø Make sure to update ACCESS_TOKEN in the script!") print() if ACCESS_TOKEN == "your_jwt_token_here": print("āŒ Please update ACCESS_TOKEN in the script first!") print("You can get it from:") print("1. Browser developer tools after logging in") print("2. Or use the login endpoint to get a token") exit(1) main()