File size: 4,790 Bytes
6bf0a90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Test script for geofences API
"""

import requests
from generate_test_token import generate_test_token

# Configuration
BASE_URL = "http://localhost:8003"
MERCHANT_ID = "550e8400-e29b-41d4-a716-446655440000"
EMPLOYEE_ID = "660e8400-e29b-41d4-a716-446655440001"


def test_geofences_api():
    """Test geofences API endpoints"""
    
    print("="*70)
    print("πŸ§ͺ Testing Geofences API")
    print("="*70)
    print()
    
    # Generate JWT token
    print("πŸ”‘ Generating JWT token...")
    token = generate_test_token(
        user_id=EMPLOYEE_ID,
        merchant_id=MERCHANT_ID,
        role="employee"
    )
    print(f"βœ… Token generated\n")
    
    # Prepare headers
    headers = {
        "Authorization": f"Bearer {token}",
        "Content-Type": "application/json"
    }
    
    # Test 1: Health check
    print("1️⃣  Testing health endpoint...")
    response = requests.get(f"{BASE_URL}/tracker/geofences/health")
    print(f"   Status: {response.status_code}")
    print(f"   Response: {response.json()}\n")
    
    # Test 2: Get active geofences (default)
    print("2️⃣  Testing GET /geofences (active only)...")
    response = requests.get(
        f"{BASE_URL}/tracker/geofences",
        headers=headers
    )
    
    print(f"   Status: {response.status_code}")
    data = response.json()
    print(f"   Success: {data.get('success')}")
    print(f"   Count: {data.get('count')}")
    print(f"   Geofences:")
    
    for gf in data.get('geofences', []):
        print(f"      πŸ“ {gf['name']}")
        print(f"         ID: {gf['id']}")
        print(f"         Location: ({gf['lat']:.4f}, {gf['lon']:.4f})")
        print(f"         Radius: {gf['radius']}m")
        print(f"         Active: {gf['active']}")
    
    print()
    
    # Test 3: Get all geofences (including inactive)
    print("3️⃣  Testing GET /geofences?include_inactive=true...")
    response = requests.get(
        f"{BASE_URL}/tracker/geofences?include_inactive=true",
        headers=headers
    )
    
    print(f"   Status: {response.status_code}")
    data = response.json()
    print(f"   Success: {data.get('success')}")
    print(f"   Count: {data.get('count')}")
    print(f"   Active: {sum(1 for gf in data.get('geofences', []) if gf['active'])}")
    print(f"   Inactive: {sum(1 for gf in data.get('geofences', []) if not gf['active'])}\n")
    
    # Test 4: Unauthorized request (no token)
    print("4️⃣  Testing authentication (no token)...")
    response = requests.get(f"{BASE_URL}/tracker/geofences")
    
    print(f"   Status: {response.status_code}")
    print(f"   Response: {response.json()}\n")
    
    # Test 5: Invalid token
    print("5️⃣  Testing authentication (invalid token)...")
    invalid_headers = {
        "Authorization": "Bearer invalid_token_here",
        "Content-Type": "application/json"
    }
    
    response = requests.get(
        f"{BASE_URL}/tracker/geofences",
        headers=invalid_headers
    )
    
    print(f"   Status: {response.status_code}")
    print(f"   Response: {response.json()}\n")
    
    # Test 6: Different merchant (security test)
    print("6️⃣  Testing merchant isolation (different merchant token)...")
    different_merchant_token = generate_test_token(
        user_id=EMPLOYEE_ID,
        merchant_id="999e8400-e29b-41d4-a716-446655440099",  # Different merchant
        role="employee"
    )
    
    different_headers = {
        "Authorization": f"Bearer {different_merchant_token}",
        "Content-Type": "application/json"
    }
    
    response = requests.get(
        f"{BASE_URL}/tracker/geofences",
        headers=different_headers
    )
    
    print(f"   Status: {response.status_code}")
    data = response.json()
    print(f"   Count: {data.get('count')} (should be 0 - different merchant)")
    print(f"   Response: {data}\n")
    
    print("="*70)
    print("βœ… Geofences API Tests Complete!")
    print("="*70)
    print()
    print("πŸ“Š Summary:")
    print("   β€’ Health check: Working")
    print("   β€’ Get active geofences: Working")
    print("   β€’ Get all geofences: Working")
    print("   β€’ Authentication: Working")
    print("   β€’ Merchant isolation: Working")
    print()
    print("πŸ’‘ Next steps:")
    print("   1. Integrate with mobile app")
    print("   2. Show geofences on map")
    print("   3. Use for auto check-in/check-out")
    print("   4. Validate task locations")


if __name__ == "__main__":
    try:
        test_geofences_api()
    except requests.exceptions.ConnectionError:
        print("❌ Error: Could not connect to server")
        print("   Make sure the server is running: python3 -m uvicorn app.main:app --port 8003")
    except Exception as e:
        print(f"❌ Error: {e}")