Spaces:
Runtime error
Runtime error
Download test_login_flow.py from kalyan9/HealthGuard-AI: direct link, hf CLI and curl.
- Browser
- Download file 4.24 kB
-
https://huggingface.co/spaces/kalyan9/HealthGuard-AI/resolve/main/test_login_flow.py
- Command line
-
hf download hf://spaces/kalyan9/HealthGuard-AI/test_login_flow.py
-
curl -L -o test_login_flow.py https://huggingface.co/spaces/kalyan9/HealthGuard-AI/resolve/main/test_login_flow.py
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.") |