""" Test script to verify simulator logging functionality """ import json import requests from datetime import datetime BASE_URL = "http://localhost:8000" def test_email_simulator_logging(): """Test that email simulator logs requests to the database""" print("\n=== Testing Email Simulator Logging ===") # Send email request email_data = { "to": "test@example.com", "subject": "Test Email from Simulator", "message": "This is a test email to verify logging functionality" } response = requests.post( f"{BASE_URL}/simulator/api/email/", json=email_data, headers={"Content-Type": "application/json"} ) print(f"Status Code: {response.status_code}") print(f"Response: {json.dumps(response.json(), indent=2)}") if response.status_code == 200: print("✓ Email simulator request logged successfully") else: print("✗ Email simulator request failed") return response.status_code == 200 def test_sms_simulator_logging(): """Test that SMS simulator logs requests to the database""" print("\n=== Testing SMS Simulator Logging ===") # Send SMS request sms_data = { "to": "+966501234567", "message": "This is a test SMS to verify logging functionality" } response = requests.post( f"{BASE_URL}/simulator/api/sms/", json=sms_data, headers={"Content-Type": "application/json"} ) print(f"Status Code: {response.status_code}") print(f"Response: {json.dumps(response.json(), indent=2)}") if response.status_code == 200: print("✓ SMS simulator request logged successfully") else: print("✗ SMS simulator request failed") return response.status_code == 200 def test_his_events_logging(): """Test that HIS events handler logs requests to the database""" print("\n=== Testing HIS Events Logging ===") # Send HIS events events_data = { "events": [{ "encounter_id": "TEST-ENC-001", "mrn": "TEST-MRN-001", "national_id": "1234567890", "first_name": "Test", "last_name": "Patient", "phone": "+966501234567", "email": "testpatient@example.com", "event_type": "OPD_STAGE_1_REGISTRATION", "timestamp": datetime.now().isoformat(), "visit_type": "opd", "department": "Cardiology", "hospital_code": "ALH-main" }] } response = requests.post( f"{BASE_URL}/simulator/api/his-events/", json=events_data, headers={"Content-Type": "application/json"} ) print(f"Status Code: {response.status_code}") print(f"Response: {json.dumps(response.json(), indent=2)}") if response.status_code == 200: print("✓ HIS events logged successfully") else: print("✗ HIS events request failed") return response.status_code == 200 def test_simulator_health(): """Test simulator health endpoint""" print("\n=== Testing Simulator Health Endpoint ===") response = requests.get(f"{BASE_URL}/simulator/health/") print(f"Status Code: {response.status_code}") print(f"Response: {json.dumps(response.json(), indent=2)}") if response.status_code == 200: print("✓ Simulator health check passed") else: print("✗ Simulator health check failed") return response.status_code == 200 def test_logs_viewer(): """Test that logs can be viewed through the web interface""" print("\n=== Testing Logs Viewer ===") print(f"Visit logs viewer at: {BASE_URL}/simulator/logs/") print("Note: You need to be logged in to view the logs") print("✓ Logs viewer URL is accessible") def main(): """Run all tests""" print("="*60) print("Simulator Logging Test Suite") print("="*60) print(f"Base URL: {BASE_URL}") results = [] # Run tests results.append(("Email Simulator", test_email_simulator_logging())) results.append(("SMS Simulator", test_sms_simulator_logging())) results.append(("HIS Events", test_his_events_logging())) results.append(("Health Check", test_simulator_health())) test_logs_viewer() # Print summary print("\n" + "="*60) print("Test Summary") print("="*60) passed = 0 failed = 0 for test_name, result in results: status = "✓ PASSED" if result else "✗ FAILED" print(f"{test_name}: {status}") if result: passed += 1 else: failed += 1 print(f"\nTotal: {len(results)}") print(f"Passed: {passed}") print(f"Failed: {failed}") if failed == 0: print("\n✓ All tests passed!") else: print(f"\n✗ {failed} test(s) failed") print("\n" + "="*60) print("Next Steps:") print("="*60) print("1. Login to the application") print("2. Visit: http://localhost:8000/simulator/logs/") print("3. View the logged requests in the web interface") print("4. Click on any log entry to see detailed information") print("="*60) if __name__ == "__main__": main()