cuatrolabs-tracker-ms / test_geofences_api.py
Michael-Antony's picture
feat: implement geofences API with merchant-scoped security
6bf0a90
#!/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}")