#!/usr/bin/env python3 """ Test script for the customers module """ import requests import pytest import json from datetime import datetime BASE_URL = "http://localhost:6920" @pytest.fixture(scope="module") def token(): """Obtain an access token from the running server, or skip if unavailable.""" try: response = requests.post(f"{BASE_URL}/api/auth/login", json={ "username": "admin", "password": "admin123" }, timeout=3) if response.status_code == 200: data = response.json() if data and data.get("access_token"): return data["access_token"] except Exception: pass pytest.skip("Auth server not available; skipping integration tests") def test_auth(): """Test authentication""" print("šŸ” Testing authentication...") # First, create an admin user if needed try: response = requests.post(f"{BASE_URL}/api/auth/register", json={ "username": "admin", "email": "admin@delphicg.local", "password": "admin123", "full_name": "System Administrator", "is_admin": True }) print(f"Registration: {response.status_code}") except Exception as e: print(f"Registration may already exist: {e}") # Login response = requests.post(f"{BASE_URL}/api/auth/login", json={ "username": "admin", "password": "admin123" }) if response.status_code == 200: token_data = response.json() token = token_data["access_token"] print(f"āœ… Login successful, token: {token[:20]}...") return token else: print(f"āŒ Login failed: {response.status_code} - {response.text}") return None def test_customers_api(token): """Test customers API endpoints""" headers = {"Authorization": f"Bearer {token}"} print("\nšŸ“‹ Testing Customers API...") # Test getting customers list (should be empty initially) response = requests.get(f"{BASE_URL}/api/customers/", headers=headers) print(f"Get customers: {response.status_code}") if response.status_code == 200: customers = response.json() print(f"Found {len(customers)} customers") # Test creating a customer test_customer = { "id": "TEST001", "last": "Doe", "first": "John", "middle": "Q", "prefix": "Mr.", "title": "Attorney", "group": "Client", "a1": "123 Main Street", "a2": "Suite 100", "city": "Dallas", "abrev": "TX", "zip": "75201", "email": "john.doe@example.com", "legal_status": "Petitioner", "memo": "Test customer created by automated test" } response = requests.post(f"{BASE_URL}/api/customers/", json=test_customer, headers=headers) print(f"Create customer: {response.status_code}") if response.status_code == 200: created_customer = response.json() print(f"āœ… Created customer: {created_customer['id']} - {created_customer['last']}") customer_id = created_customer['id'] else: print(f"āŒ Create failed: {response.text}") return # Test adding phone numbers phone1 = {"location": "Office", "phone": "(214) 555-0100"} phone2 = {"location": "Mobile", "phone": "(214) 555-0101"} for phone in [phone1, phone2]: response = requests.post(f"{BASE_URL}/api/customers/{customer_id}/phones", json=phone, headers=headers) print(f"Add phone {phone['location']}: {response.status_code}") # Test getting customer with phones response = requests.get(f"{BASE_URL}/api/customers/{customer_id}", headers=headers) if response.status_code == 200: customer = response.json() print(f"āœ… Customer has {len(customer['phone_numbers'])} phone numbers") for phone in customer['phone_numbers']: print(f" {phone['location']}: {phone['phone']}") # Test search functionality response = requests.get(f"{BASE_URL}/api/customers/?search=Doe", headers=headers) if response.status_code == 200: results = response.json() print(f"āœ… Search for 'Doe' found {len(results)} results") # Test phone search response = requests.get(f"{BASE_URL}/api/customers/search/phone?phone=214", headers=headers) if response.status_code == 200: results = response.json() print(f"āœ… Phone search for '214' found {len(results)} results") # Test stats response = requests.get(f"{BASE_URL}/api/customers/stats", headers=headers) if response.status_code == 200: stats = response.json() print(f"āœ… Stats: {stats['total_customers']} customers, {stats['total_phone_numbers']} phones") print(f" Groups: {[g['group'] + ':' + str(g['count']) for g in stats['group_breakdown']]}") # Test updating customer update_data = {"memo": f"Updated at {datetime.now().isoformat()}"} response = requests.put(f"{BASE_URL}/api/customers/{customer_id}", json=update_data, headers=headers) print(f"Update customer: {response.status_code}") print(f"\nāœ… All customer API tests completed successfully!") return customer_id def test_web_page(): """Test the web page loads""" print("\n🌐 Testing web page...") # Test health endpoint response = requests.get(f"{BASE_URL}/health") print(f"Health check: {response.status_code}") # Test customers page (will require authentication in browser) response = requests.get(f"{BASE_URL}/customers") print(f"Customers page: {response.status_code}") if response.status_code == 200: print("āœ… Customers page loads successfully") else: print(f"Note: Customers page requires authentication (status {response.status_code})") def main(): print("šŸš€ Testing Delphi Database Customers Module") print("=" * 50) # Test authentication token = test_auth() if not token: print("āŒ Cannot proceed without authentication") return # Test API endpoints customer_id = test_customers_api(token) # Test web interface test_web_page() print("\nšŸŽ‰ Customer module testing completed!") print(f"🌐 Visit http://localhost:6920/customers to see the web interface") print(f"šŸ“š API docs available at http://localhost:6920/docs") if __name__ == "__main__": main()