File size: 4,138 Bytes
3a32bd4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Simple API Testing Script
Test the certificate verification API locally
"""

import requests
import sys
from pathlib import Path

API_URL = "http://localhost:8000"

def test_health():
    """Test health endpoint"""
    print("\nπŸ” Testing Health Endpoint...")
    try:
        response = requests.get(f"{API_URL}/health")
        print(f"Status: {response.status_code}")
        print(f"Response: {response.json()}")
        return response.status_code == 200
    except Exception as e:
        print(f"❌ Error: {e}")
        return False

def test_root():
    """Test root endpoint"""
    print("\nπŸ” Testing Root Endpoint...")
    try:
        response = requests.get(API_URL)
        print(f"Status: {response.status_code}")
        print(f"Response: {response.json()}")
        return response.status_code == 200
    except Exception as e:
        print(f"❌ Error: {e}")
        return False

def test_api_status():
    """Test API status endpoint"""
    print("\nπŸ” Testing API Status Endpoint...")
    try:
        response = requests.get(f"{API_URL}/api/status")
        print(f"Status: {response.status_code}")
        print(f"Response: {response.json()}")
        return response.status_code == 200
    except Exception as e:
        print(f"❌ Error: {e}")
        return False

def test_verify(image_path):
    """Test certificate verification"""
    print(f"\nπŸ” Testing Verification with: {image_path}")
    
    if not Path(image_path).exists():
        print(f"❌ File not found: {image_path}")
        return False
    
    try:
        with open(image_path, 'rb') as f:
            files = {'file': (Path(image_path).name, f, 'image/jpeg')}
            response = requests.post(
                f"{API_URL}/api/verify",
                files=files,
                data={'enable_seal_verification': 'true'}
            )
        
        print(f"Status: {response.status_code}")
        result = response.json()
        
        if result.get('success'):
            print(f"\nβœ… Decision: {result['decision']}")
            print(f"πŸ“Š Confidence: {result['confidence']}")
            print(f"πŸ’¬ Reason: {result['reason']}")
            
            if 'details' in result:
                details = result['details']
                print(f"\nπŸ“‹ Details:")
                print(f"  - Registration: {details.get('registration_number', 'N/A')}")
                print(f"  - Database Match: {details.get('database_match', False)}")
                
                if 'seal_verification' in details and details['seal_verification']:
                    seal = details['seal_verification']
                    print(f"  - Seal Status: {seal.get('seal_status', 'N/A')}")
                    print(f"  - Total Seals: {seal.get('total_seals', 0)}")
        else:
            print(f"❌ Error: {result.get('error', 'Unknown error')}")
            print(f"πŸ’¬ Message: {result.get('message', 'No message')}")
        
        return response.status_code == 200
        
    except Exception as e:
        print(f"❌ Error: {e}")
        return False

def main():
    """Main test runner"""
    print("="*60)
    print("🎯 Certificate Verification API - Test Suite")
    print("="*60)
    
    # Test endpoints
    results = {
        "Root": test_root(),
        "Health": test_health(),
        "Status": test_api_status()
    }
    
    # Test verification if image provided
    if len(sys.argv) > 1:
        image_path = sys.argv[1]
        results["Verify"] = test_verify(image_path)
    else:
        print("\nπŸ’‘ Tip: Run with image path to test verification:")
        print("   python test_api.py path/to/certificate.jpg")
    
    # Summary
    print("\n" + "="*60)
    print("πŸ“Š Test Results:")
    print("="*60)
    for test, passed in results.items():
        status = "βœ… PASS" if passed else "❌ FAIL"
        print(f"{test:20s} {status}")
    print("="*60)
    
    all_passed = all(results.values())
    print(f"\n{'πŸŽ‰ All tests passed!' if all_passed else '⚠️ Some tests failed'}")
    
    return 0 if all_passed else 1

if __name__ == "__main__":
    sys.exit(main())