File size: 4,318 Bytes
8431187
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Simple test script for the Bank Balance API
"""
import requests
import json
import time


def test_balance_api(base_url="http://localhost:7860"):
    """Test the bank balance API endpoints"""

    print("=" * 60)
    print("Testing Bank Balance API")
    print("=" * 60)

    # Test 1: Health check
    print("\n1. Testing health endpoint...")
    try:
        response = requests.get(f"{base_url}/health")
        print(f"   Status: {response.status_code}")
        print(f"   Response: {response.json()}")
    except Exception as e:
        print(f"   Error: {e}")

    # Test 2: Root endpoint
    print("\n2. Testing root endpoint...")
    try:
        response = requests.get(base_url)
        print(f"   Status: {response.status_code}")
        print(f"   Response: {json.dumps(response.json(), indent=2)}")
    except Exception as e:
        print(f"   Error: {e}")

    # Test 3: Valid PIN (POST)
    print("\n3. Testing with valid PIN (1234) via POST...")
    try:
        response = requests.post(
            f"{base_url}/api/balance",
            json={"pin": "1234"}
        )
        print(f"   Status: {response.status_code}")
        data = response.json()
        print(f"   Response: {json.dumps(data, indent=2)}")

        if data.get("success"):
            print(f"   βœ“ Balance: {data['balance']} {data['currency']}")
            print(f"   βœ“ Account: {data['account_name']}")
    except Exception as e:
        print(f"   Error: {e}")

    # Test 4: Another valid PIN (5678 - EUR account)
    print("\n4. Testing with valid PIN (5678) - EUR account...")
    try:
        response = requests.post(
            f"{base_url}/api/balance",
            json={"pin": "5678"}
        )
        data = response.json()
        print(f"   Status: {response.status_code}")
        if data.get("success"):
            print(f"   βœ“ Balance: {data['balance']} {data['currency']}")
            print(f"   βœ“ Account: {data['account_name']}")
    except Exception as e:
        print(f"   Error: {e}")

    # Test 5: Invalid PIN
    print("\n5. Testing with invalid PIN (0000)...")
    try:
        response = requests.post(
            f"{base_url}/api/balance",
            json={"pin": "0000"}
        )
        print(f"   Status: {response.status_code}")
        data = response.json()
        print(f"   Response: {json.dumps(data, indent=2)}")

        if not data.get("success"):
            print(f"   βœ“ Correctly denied access")
    except Exception as e:
        print(f"   Error: {e}")

    # Test 6: Invalid PIN format
    print("\n6. Testing with invalid PIN format (123)...")
    try:
        response = requests.post(
            f"{base_url}/api/balance",
            json={"pin": "123"}
        )
        print(f"   Status: {response.status_code}")
        print(f"   Response: {json.dumps(response.json(), indent=2)}")

        if response.status_code == 400:
            print(f"   βœ“ Correctly rejected invalid format")
    except Exception as e:
        print(f"   Error: {e}")

    # Test 7: GET endpoint
    print("\n7. Testing GET endpoint with PIN 7890...")
    try:
        response = requests.get(f"{base_url}/api/balance/7890")
        print(f"   Status: {response.status_code}")
        data = response.json()
        if data.get("success"):
            print(f"   βœ“ Balance: {data['balance']} {data['currency']}")
            print(f"   βœ“ Account: {data['account_name']}")
    except Exception as e:
        print(f"   Error: {e}")

    # Test 8: All available test accounts
    print("\n8. Testing all available test accounts...")
    test_pins = ["1234", "5678", "9012", "3456", "7890"]
    for pin in test_pins:
        try:
            response = requests.post(
                f"{base_url}/api/balance",
                json={"pin": pin}
            )
            data = response.json()
            if data.get("success"):
                print(f"   PIN {pin}: {data['balance']} {data['currency']} ({data['account_name']})")
        except Exception as e:
            print(f"   PIN {pin}: Error - {e}")

    print("\n" + "=" * 60)
    print("Testing complete!")
    print("=" * 60)


if __name__ == "__main__":
    # Wait a moment for server to be ready if just started
    print("Waiting for server to be ready...")
    time.sleep(2)

    test_balance_api()