File size: 4,823 Bytes
2c41dce |
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 |
"""
Simple test script for uploading images to the proof system.
"""
import requests
import base64
import json
from pathlib import Path
# API base URL
BASE_URL = "http://127.0.0.1:8000"
def test_text_proof():
"""Test creating a proof from text."""
print("\n=== Testing Text Proof ===")
response = requests.post(
f"{BASE_URL}/proof/create/text",
json={"content": "Hello, this is a test document!"}
)
result = response.json()
print(f"Success: {result.get('success')}")
print(f"Proof ID: {result.get('proof_id')}")
print(f"Hash: {result.get('hash')}")
if 'assistant' in result and result['assistant']:
print(f"\n🤖 AI: {result['assistant']['response']}")
return result.get('proof_id')
def test_image_proof(image_path):
"""Test creating a proof from an image."""
print(f"\n=== Testing Image Proof: {image_path} ===")
# Check if file exists
if not Path(image_path).exists():
print(f"❌ Error: File not found: {image_path}")
print(f"Current directory: {Path.cwd()}")
print(f"Available images: {list(Path('.').glob('*.png')) + list(Path('.').glob('*.jpg'))}")
return None
# Use FastAPI's multipart/form-data upload (not base64)
with open(image_path, "rb") as f:
files = {"file": (Path(image_path).name, f, "image/png")}
print(f"Uploading {Path(image_path).name}...")
response = requests.post(
f"{BASE_URL}/proof/create/file",
files=files
)
if response.status_code != 200:
print(f"❌ API Error {response.status_code}: {response.text}")
return None
result = response.json()
if not result.get('success'):
print(f"❌ Failed: {result.get('message')}")
if 'error' in result:
print(f"Error: {result['error']}")
return None
print(f"✅ Success: {result.get('success')}")
print(f"Proof ID: {result.get('proof_id')}")
print(f"Hash: {result.get('hash')}")
print(f"OCR Status: {result.get('ocr_status')}")
if result.get('extracted_text'):
text = result['extracted_text']
preview = text[:100] + "..." if len(text) > 100 else text
print(f"Extracted Text: {preview}")
else:
print("Extracted Text: (none - this is normal for photos)")
if 'assistant' in result and result['assistant']:
print(f"\n🤖 AI: {result['assistant']['response']}")
return result.get('proof_id'), Path(image_path).name
def test_verify_proof(proof_id, original_content, filename=None):
"""Test verifying a proof."""
print(f"\n=== Testing Verification: {proof_id} ===")
# For file uploads, we need to re-upload the file
if filename:
print("⚠️ File verification requires the original file")
print(f" API endpoint: POST /proof/verify")
print(f" Body: {{'proof_id': '{proof_id}', 'content': 'original_content_as_string'}}")
return
# For text content
if isinstance(original_content, bytes):
content_str = original_content.decode('utf-8')
else:
content_str = original_content
response = requests.post(
f"{BASE_URL}/proof/verify",
json={
"proof_id": proof_id,
"content": content_str
}
)
if response.status_code != 200:
print(f"❌ API Error {response.status_code}: {response.text}")
return
result = response.json()
print(f"Valid: {result.get('is_valid')}")
print(f"Message: {result.get('message')}")
if 'assistant' in result and result['assistant']:
print(f"\n🤖 AI: {result['assistant']['response']}")
def main():
"""Run all tests."""
print("=" * 60)
print("PROOF SYSTEM - API TESTS")
print("=" * 60)
# Test 1: Text proof
text_proof_id = test_text_proof()
# Test 2: Verify text proof
if text_proof_id:
test_verify_proof(text_proof_id, "Hello, this is a test document!")
# Test 3: Image proof
image_files = ["test1.png", "test1.jpg", "test1.jpeg"]
image_path = None
for img in image_files:
if Path(img).exists():
image_path = img
break
if image_path:
result = test_image_proof(image_path)
if result:
image_proof_id, filename = result
# Note: File verification needs special handling
print(f"\n💡 To verify the image proof, use proof ID: {image_proof_id}")
else:
print("\n⚠️ No test1 image found. Place test1.png in the backend folder.")
print("\n" + "=" * 60)
print("Tests completed!")
print("=" * 60)
if __name__ == "__main__":
main() |