HealthGuard-AI / test_login_flow.py
kalyan9's picture
Upload 14 files
95e10ab verified
Raw History Blame Contribute Delete
4.24 kB
"""
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.")