""" Test script to verify the complete login flow step by step. """ import os import sys from dotenv import load_dotenv # Load environment variables load_dotenv() def test_step_by_step(): """Test the login flow step by step.""" print("HealthGuard-AI Login Flow Test") print("=" * 35) # Step 1: Check environment print("\n1. Checking environment...") secret_key = os.getenv('SECRET_KEY') mongodb_uri = os.getenv('MONGODB_URI') if not secret_key: print(" ✗ SECRET_KEY not set") return False else: print(" ✓ SECRET_KEY is set") if not mongodb_uri: print(" ✗ MONGODB_URI not set") return False else: print(" ✓ MONGODB_URI is set") # Step 2: Test imports print("\n2. Testing imports...") try: from app import app, mongo print(" ✓ Flask app and mongo imported successfully") except Exception as e: print(f" ✗ Failed to import app: {e}") return False # Step 3: Test database connection print("\n3. Testing database connection...") try: if mongo.cx is None: print(" ✗ Database connection not available") return False else: print(" ✓ Database connection is available") # Test users collection user_count = mongo.db.users.count_documents({}) print(f" ✓ Users collection accessible ({user_count} users)") except Exception as e: print(f" ✗ Database test failed: {e}") return False # Step 4: Test bcrypt print("\n4. Testing bcrypt...") try: import bcrypt test_password = "test123" hashed = bcrypt.hashpw(test_password.encode('utf-8'), bcrypt.gensalt()) if bcrypt.checkpw(test_password.encode('utf-8'), hashed): print(" ✓ Bcrypt is working correctly") else: print(" ✗ Bcrypt verification failed") return False except Exception as e: print(f" ✗ Bcrypt test failed: {e}") return False # Step 5: Test a real user (if exists) print("\n5. Testing with a real user...") try: # Get first user from database first_user = mongo.db.users.find_one() if first_user: print(f" ✓ Found user: {first_user.get('name', 'Unknown')} ({first_user.get('email', 'No email')})") print(f" ✓ User role: {first_user.get('role', 'No role')}") else: print(" ⚠ No users found in database") except Exception as e: print(f" ✗ User test failed: {e}") return False print("\n✅ All tests passed! Login flow should work correctly.") return True def test_with_credentials(): """Test login with specific credentials.""" print("\nTesting login with specific credentials...") email = input("Enter email to test: ").strip() password = input("Enter password to test: ").strip() if not email or not password: print("Email and password required.") return try: from app import mongo import bcrypt # Find user user = mongo.db.users.find_one({'email': email}) if not user: print(f"✗ User with email '{email}' not found") return print(f"✓ User found: {user['name']}") # Check password if bcrypt.checkpw(password.encode('utf-8'), user['password']): print("✓ Password correct!") print("Login would be successful.") else: print("✗ Password incorrect!") except Exception as e: print(f"✗ Test failed: {e}") import traceback traceback.print_exc() if __name__ == "__main__": success = test_step_by_step() if success: print("\nDo you want to test with specific credentials? (y/n): ", end="") if input().strip().lower() == 'y': test_with_credentials() print("\nTest completed.")