Spaces:
Runtime error
Runtime error
Download post_deployment_test.py from kalyan9/HealthGuard-AI: direct link, hf CLI and curl.
- Browser
- Download file 10.1 kB
-
https://huggingface.co/spaces/kalyan9/HealthGuard-AI/resolve/main/post_deployment_test.py
- Command line
-
hf download hf://spaces/kalyan9/HealthGuard-AI/post_deployment_test.py
-
curl -L -o post_deployment_test.py https://huggingface.co/spaces/kalyan9/HealthGuard-AI/resolve/main/post_deployment_test.py
10.1 kB
| """ | |
| Post-deployment test script for HealthGuard-AI. | |
| This script tests the deployed application to ensure everything is working correctly. | |
| """ | |
| import os | |
| import requests | |
| import time | |
| import sys | |
| from datetime import datetime | |
| class HealthGuardTester: | |
| def __init__(self, base_url): | |
| self.base_url = base_url.rstrip('/') | |
| self.session = requests.Session() | |
| self.test_results = [] | |
| def log_result(self, test_name, success, details=""): | |
| """Log a test result.""" | |
| status = "PASS" if success else "FAIL" | |
| result = f"[{status}] {test_name}" | |
| if details: | |
| result += f" - {details}" | |
| print(result) | |
| self.test_results.append((test_name, success, details)) | |
| return success | |
| def test_home_page(self): | |
| """Test if home page loads.""" | |
| try: | |
| response = self.session.get(self.base_url, timeout=10) | |
| success = response.status_code == 200 | |
| details = f"Status: {response.status_code}, Length: {len(response.text)} chars" | |
| return self.log_result("Home Page Load", success, details) | |
| except Exception as e: | |
| return self.log_result("Home Page Load", False, f"Error: {e}") | |
| def test_login_page(self): | |
| """Test if login page loads.""" | |
| try: | |
| response = self.session.get(f"{self.base_url}/login", timeout=10) | |
| success = response.status_code == 200 | |
| details = f"Status: {response.status_code}, Length: {len(response.text)} chars" | |
| return self.log_result("Login Page Load", success, details) | |
| except Exception as e: | |
| return self.log_result("Login Page Load", False, f"Error: {e}") | |
| def test_debug_endpoints(self): | |
| """Test debug endpoints.""" | |
| endpoints = [ | |
| "/debug/login-status", | |
| "/debug/test-session", | |
| "/debug/clear-session" | |
| ] | |
| all_success = True | |
| for endpoint in endpoints: | |
| try: | |
| response = self.session.get(f"{self.base_url}{endpoint}", timeout=10) | |
| success = response.status_code in [200, 404] # 404 might be OK if not implemented | |
| details = f"Status: {response.status_code}" | |
| if response.status_code == 200: | |
| try: | |
| data = response.json() | |
| details += f", Data keys: {list(data.keys())}" | |
| except: | |
| details += f", Response: {response.text[:100]}..." | |
| endpoint_success = self.log_result(f"Debug Endpoint {endpoint}", success, details) | |
| all_success = all_success and endpoint_success | |
| except Exception as e: | |
| endpoint_success = self.log_result(f"Debug Endpoint {endpoint}", False, f"Error: {e}") | |
| all_success = all_success and endpoint_success | |
| return all_success | |
| def test_session_functionality(self): | |
| """Test session functionality.""" | |
| try: | |
| # Clear session first | |
| self.session.get(f"{self.base_url}/debug/clear-session", timeout=10) | |
| # Set session | |
| response = self.session.get(f"{self.base_url}/debug/test-session", timeout=10) | |
| if response.status_code == 200: | |
| data = response.json() | |
| session_set = "test" in data.get("session_data", {}) | |
| details = f"Session set: {session_set}" | |
| if session_set: | |
| details += f", Session data: {data['session_data']}" | |
| return self.log_result("Session Functionality", session_set, details) | |
| else: | |
| return self.log_result("Session Functionality", False, f"Status: {response.status_code}") | |
| except Exception as e: | |
| return self.log_result("Session Functionality", False, f"Error: {e}") | |
| def test_user_lookup(self, email=None): | |
| """Test user lookup functionality.""" | |
| if not email: | |
| # Try to get a user email from environment or use a test one | |
| email = os.getenv("TEST_USER_EMAIL", "test@example.com") | |
| try: | |
| response = self.session.get(f"{self.base_url}/debug/user/{email}", timeout=10) | |
| success = response.status_code in [200, 404] | |
| details = f"Status: {response.status_code}" | |
| if response.status_code == 200: | |
| try: | |
| data = response.json() | |
| details += f", User found: {data.get('status') == 'User found'}" | |
| except: | |
| details += f", Response: {response.text[:100]}..." | |
| elif response.status_code == 404: | |
| details += ", User not found (this may be OK)" | |
| return self.log_result("User Lookup", success, details) | |
| except Exception as e: | |
| return self.log_result("User Lookup", False, f"Error: {e}") | |
| def simulate_login_flow(self, email=None, password=None): | |
| """Simulate the complete login flow.""" | |
| if not email or not password: | |
| print("Skipping login flow test - credentials not provided") | |
| return True | |
| try: | |
| # 1. Get login page | |
| response = self.session.get(f"{self.base_url}/login", timeout=10) | |
| if response.status_code != 200: | |
| return self.log_result("Login Flow", False, f"Failed to load login page: {response.status_code}") | |
| # 2. Check pre-login status | |
| response = self.session.get(f"{self.base_url}/debug/login-status", timeout=10) | |
| pre_login_status = {} | |
| if response.status_code == 200: | |
| pre_login_status = response.json() | |
| # 3. Perform login | |
| login_data = { | |
| 'email': email, | |
| 'password': password | |
| } | |
| response = self.session.post(f"{self.base_url}/login", data=login_data, timeout=10, allow_redirects=False) | |
| # 4. Check post-login status | |
| response2 = self.session.get(f"{self.base_url}/debug/login-status", timeout=10) | |
| post_login_status = {} | |
| if response2.status_code == 200: | |
| post_login_status = response2.json() | |
| # 5. Try to access dashboard | |
| response3 = self.session.get(f"{self.base_url}/user/dashboard", timeout=10, allow_redirects=False) | |
| dashboard_status = response3.status_code | |
| # Analyze results | |
| logged_in = 'user_id' in post_login_status.get('session_data', {}) | |
| redirected_to_dashboard = response.status_code in [301, 302, 303, 307, 308] and '/dashboard' in response.headers.get('Location', '') | |
| dashboard_accessible = dashboard_status == 200 | |
| success = logged_in and (redirected_to_dashboard or dashboard_accessible) | |
| details = f"Logged in: {logged_in}, Redirected: {redirected_to_dashboard}, Dashboard: {dashboard_status}" | |
| details += f", Pre-login: {pre_login_status.get('user_logged_in', False)}, Post-login: {post_login_status.get('user_logged_in', False)}" | |
| return self.log_result("Login Flow", success, details) | |
| except Exception as e: | |
| return self.log_result("Login Flow", False, f"Error: {e}") | |
| def run_all_tests(self, email=None, password=None): | |
| """Run all tests.""" | |
| print(f"HealthGuard-AI Post-Deployment Test") | |
| print(f"Testing against: {self.base_url}") | |
| print(f"Time: {datetime.now()}") | |
| print("=" * 50) | |
| tests = [ | |
| self.test_home_page, | |
| self.test_login_page, | |
| self.test_debug_endpoints, | |
| self.test_session_functionality, | |
| lambda: self.test_user_lookup(email), | |
| lambda: self.simulate_login_flow(email, password) | |
| ] | |
| for test in tests: | |
| test() | |
| print() # Empty line for readability | |
| # Summary | |
| print("=" * 50) | |
| print("TEST SUMMARY") | |
| print("=" * 50) | |
| passed = sum(1 for _, success, _ in self.test_results if success) | |
| total = len(self.test_results) | |
| for test_name, success, details in self.test_results: | |
| status = "PASS" if success else "FAIL" | |
| print(f"[{status}] {test_name}") | |
| if details: | |
| print(f" {details}") | |
| print(f"\nOverall: {passed}/{total} tests passed") | |
| if passed == total: | |
| print("✓ All tests passed! Application appears to be working correctly.") | |
| return True | |
| else: | |
| print("✗ Some tests failed. Please check the output above.") | |
| return False | |
| def main(): | |
| """Main function.""" | |
| print("HealthGuard-AI Post-Deployment Tester") | |
| print("=" * 40) | |
| # Get base URL from command line or environment | |
| if len(sys.argv) > 1: | |
| base_url = sys.argv[1] | |
| else: | |
| base_url = os.getenv('APP_URL', 'http://localhost:7860') | |
| # Get credentials if provided | |
| email = os.getenv('TEST_USER_EMAIL') | |
| password = os.getenv('TEST_USER_PASSWORD') | |
| if not email: | |
| email = input("Enter test user email (or press Enter to skip login test): ").strip() | |
| if email and not password: | |
| password = input("Enter test user password: ").strip() | |
| if not email: | |
| email = None | |
| password = None | |
| # Create tester and run tests | |
| tester = HealthGuardTester(base_url) | |
| success = tester.run_all_tests(email, password) | |
| sys.exit(0 if success else 1) | |
| if __name__ == "__main__": | |
| main() |