File size: 7,569 Bytes
95e10ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
"""

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()