Spaces:
Runtime error
Runtime error
Download deployment_test.py from kalyan9/HealthGuard-AI: direct link, hf CLI and curl.
- Browser
- Download file 7.57 kB
-
https://huggingface.co/spaces/kalyan9/HealthGuard-AI/resolve/main/deployment_test.py
- Command line
-
hf download hf://spaces/kalyan9/HealthGuard-AI/deployment_test.py
-
curl -L -o deployment_test.py https://huggingface.co/spaces/kalyan9/HealthGuard-AI/resolve/main/deployment_test.py
7.57 kB
| """ | |
| Deployment test script for HealthGuard-AI. | |
| This script will help test the login functionality with detailed logging. | |
| """ | |
| import os | |
| import requests | |
| import time | |
| from datetime import datetime | |
| def test_endpoints(base_url): | |
| """Test various endpoints to understand the application flow.""" | |
| print("=== Testing Application Endpoints ===") | |
| # Test home page | |
| print("\n1. Testing home page...") | |
| try: | |
| response = requests.get(base_url, timeout=10) | |
| print(f" Status Code: {response.status_code}") | |
| print(f" Response Length: {len(response.text)} characters") | |
| except Exception as e: | |
| print(f" Error: {e}") | |
| # Test login page | |
| print("\n2. Testing login page...") | |
| try: | |
| response = requests.get(f"{base_url}/login", timeout=10) | |
| print(f" Status Code: {response.status_code}") | |
| print(f" Response Length: {len(response.text)} characters") | |
| # Check if it contains login form elements | |
| if 'email' in response.text.lower() and 'password' in response.text.lower(): | |
| print(" ✓ Login form elements found") | |
| else: | |
| print(" ✗ Login form elements not found") | |
| except Exception as e: | |
| print(f" Error: {e}") | |
| # Test register page | |
| print("\n3. Testing register page...") | |
| try: | |
| response = requests.get(f"{base_url}/register", timeout=10) | |
| print(f" Status Code: {response.status_code}") | |
| print(f" Response Length: {len(response.text)} characters") | |
| except Exception as e: | |
| print(f" Error: {e}") | |
| def test_session_functionality(base_url): | |
| """Test session functionality.""" | |
| print("\n=== Testing Session Functionality ===") | |
| # Create a session to maintain cookies | |
| session = requests.Session() | |
| # Test setting session | |
| print("\n1. Testing session setting...") | |
| try: | |
| response = session.get(f"{base_url}/debug/test-session", timeout=10) | |
| print(f" Status Code: {response.status_code}") | |
| if response.status_code == 200: | |
| data = response.json() | |
| print(f" Session Data: {data}") | |
| except Exception as e: | |
| print(f" Error: {e}") | |
| # Test clearing session | |
| print("\n2. Testing session clearing...") | |
| try: | |
| response = session.get(f"{base_url}/debug/clear-session", timeout=10) | |
| print(f" Status Code: {response.status_code}") | |
| if response.status_code == 200: | |
| data = response.json() | |
| print(f" Clear Result: {data}") | |
| except Exception as e: | |
| print(f" Error: {e}") | |
| def test_user_lookup(base_url, email): | |
| """Test user lookup.""" | |
| print("\n=== Testing User Lookup ===") | |
| try: | |
| response = requests.get(f"{base_url}/debug/user/{email}", timeout=10) | |
| print(f" Status Code: {response.status_code}") | |
| if response.status_code == 200: | |
| data = response.json() | |
| print(f" User Data: {data}") | |
| elif response.status_code == 404: | |
| print(" User not found") | |
| else: | |
| print(f" Unexpected response: {response.text}") | |
| except Exception as e: | |
| print(f" Error: {e}") | |
| def simulate_login_flow(base_url, email, password): | |
| """Simulate the complete login flow.""" | |
| print("\n=== Simulating Login Flow ===") | |
| # Create a session to maintain cookies | |
| session = requests.Session() | |
| # 1. Get login page first (to get any CSRF tokens if needed) | |
| print("\n1. Getting login page...") | |
| try: | |
| response = session.get(f"{base_url}/login", timeout=10) | |
| print(f" Status Code: {response.status_code}") | |
| print(f" Cookies: {dict(session.cookies)}") | |
| except Exception as e: | |
| print(f" Error: {e}") | |
| return | |
| # 2. Check login status before login | |
| print("\n2. Checking login status before login...") | |
| try: | |
| response = session.get(f"{base_url}/debug/login-status", timeout=10) | |
| print(f" Status Code: {response.status_code}") | |
| if response.status_code == 200: | |
| data = response.json() | |
| print(f" Pre-login Status: {data}") | |
| except Exception as e: | |
| print(f" Error: {e}") | |
| # 3. Perform login | |
| print("\n3. Performing login...") | |
| login_data = { | |
| 'email': email, | |
| 'password': password | |
| } | |
| try: | |
| response = session.post(f"{base_url}/login", data=login_data, timeout=10, allow_redirects=False) | |
| print(f" Status Code: {response.status_code}") | |
| print(f" Response Headers: {dict(response.headers)}") | |
| print(f" Cookies after login: {dict(session.cookies)}") | |
| # Check if it's a redirect | |
| if response.status_code in [301, 302, 303, 307, 308]: | |
| location = response.headers.get('Location', '') | |
| print(f" Redirect Location: {location}") | |
| else: | |
| print(f" Response Body: {response.text[:500]}...") | |
| except Exception as e: | |
| print(f" Error: {e}") | |
| return | |
| # 4. Check login status after login | |
| print("\n4. Checking login status after login...") | |
| try: | |
| response = session.get(f"{base_url}/debug/login-status", timeout=10) | |
| print(f" Status Code: {response.status_code}") | |
| if response.status_code == 200: | |
| data = response.json() | |
| print(f" Post-login Status: {data}") | |
| except Exception as e: | |
| print(f" Error: {e}") | |
| # 5. Try to access a protected page | |
| print("\n5. Trying to access user dashboard...") | |
| try: | |
| response = session.get(f"{base_url}/user/dashboard", timeout=10, allow_redirects=False) | |
| print(f" Status Code: {response.status_code}") | |
| print(f" Response Headers: {dict(response.headers)}") | |
| # Check if it's a redirect (which would indicate not logged in) | |
| if response.status_code in [301, 302, 303, 307, 308]: | |
| location = response.headers.get('Location', '') | |
| print(f" Redirect Location: {location}") | |
| if 'login' in location: | |
| print(" ⚠ Redirected to login page - likely not logged in") | |
| else: | |
| print(" ⚠ Other redirect") | |
| else: | |
| print(f" Response Body Length: {len(response.text)} characters") | |
| except Exception as e: | |
| print(f" Error: {e}") | |
| def main(): | |
| """Main function.""" | |
| print("HealthGuard-AI Deployment Test") | |
| print("=" * 40) | |
| print() | |
| # Get base URL from environment or use default | |
| base_url = os.getenv('APP_URL', 'http://localhost:7860') | |
| print(f"Testing against: {base_url}") | |
| print() | |
| # Test endpoints | |
| test_endpoints(base_url) | |
| # Test session functionality | |
| test_session_functionality(base_url) | |
| # Ask user for email to test | |
| print("\n" + "=" * 40) | |
| email = input("Enter email to test user lookup (or press Enter to skip): ").strip() | |
| if email: | |
| test_user_lookup(base_url, email) | |
| # Ask user for login credentials | |
| print("\n" + "=" * 40) | |
| email = input("Enter email for login test (or press Enter to skip): ").strip() | |
| if email: | |
| password = input("Enter password for login test: ").strip() | |
| if password: | |
| simulate_login_flow(base_url, email, password) | |
| if __name__ == "__main__": | |
| main() |