| """
|
| Test script for MongoDB integration.
|
| Run this script to verify the MongoDB connection and user management.
|
| """
|
| import os
|
| import sys
|
| from dotenv import load_dotenv
|
| from database import db, initialize_users
|
|
|
|
|
| load_dotenv()
|
|
|
| def test_connection():
|
| """Test MongoDB connection"""
|
| try:
|
|
|
| db.client.admin.command('ping')
|
| print("β
Successfully connected to MongoDB")
|
| return True
|
| except Exception as e:
|
| print(f"β Failed to connect to MongoDB: {e}")
|
| return False
|
|
|
| def test_user_management():
|
| """Test user management functions"""
|
| test_username = "test_user_123"
|
| test_password = "test_password_123"
|
| test_role = "test_role"
|
|
|
|
|
| db.users.delete_one({"username": test_username})
|
|
|
|
|
| print("\nTesting add_user...")
|
| if db.add_user(test_username, test_password, test_role):
|
| print(f"β
Successfully added test user: {test_username}")
|
| else:
|
| print("β Failed to add test user")
|
| return False
|
|
|
|
|
| print("\nTesting verify_user with correct password...")
|
| user = db.verify_user(test_username, test_password)
|
| if user and user["username"] == test_username and user["role"] == test_role:
|
| print("β
Successfully verified user with correct password")
|
| else:
|
| print("β Failed to verify user with correct password")
|
| return False
|
|
|
|
|
| print("\nTesting verify_user with incorrect password...")
|
| user = db.verify_user(test_username, "wrong_password")
|
| if user is None:
|
| print("β
Correctly rejected incorrect password")
|
| else:
|
| print("β Incorrectly accepted wrong password")
|
| return False
|
|
|
|
|
| print("\nTesting get_user...")
|
| user = db.get_user(test_username)
|
| if user and user["username"] == test_username and user["role"] == test_role:
|
| print("β
Successfully retrieved user details")
|
| else:
|
| print("β Failed to retrieve user details")
|
| return False
|
|
|
|
|
| db.users.delete_one({"username": test_username})
|
| return True
|
|
|
| def test_initialize_users():
|
| """Test user initialization"""
|
| print("\nTesting user initialization...")
|
| try:
|
|
|
| test_usernames = ["Tony", "Bruce", "Sam", "Peter", "Sid", "Natasha"]
|
| db.users.delete_many({"username": {"$in": test_usernames}})
|
|
|
| success_count, total_users, errors = initialize_users()
|
|
|
| if errors:
|
| print(f"β οΈ Encountered {len(errors)} errors during user initialization:")
|
| for error in errors:
|
| print(f" - {error}")
|
|
|
| if success_count == total_users:
|
| print(f"β
Successfully initialized {success_count}/{total_users} users")
|
| return True
|
| else:
|
| print(f"β Only initialized {success_count}/{total_users} users")
|
| return False
|
| except Exception as e:
|
| print(f"β Error during user initialization test: {str(e)}")
|
| return False
|
|
|
| if __name__ == "__main__":
|
| print("=== Testing MongoDB Integration ===")
|
|
|
|
|
| required_vars = ['MONGO_URI']
|
| missing_vars = [var for var in required_vars if not os.getenv(var)]
|
|
|
| if missing_vars:
|
| print("β Missing required environment variables:")
|
| for var in missing_vars:
|
| print(f" - {var}")
|
| print("\nPlease create a .env file with these variables. See .env.example")
|
| sys.exit(1)
|
|
|
|
|
| connection_ok = test_connection()
|
|
|
| if connection_ok:
|
| print("\n=== Running User Management Tests ===")
|
| user_tests_ok = test_user_management()
|
|
|
| print("\n=== Running User Initialization Test ===")
|
| init_ok = test_initialize_users()
|
|
|
| if user_tests_ok and init_ok:
|
| print("\nβ
All tests passed!")
|
| sys.exit(0)
|
| else:
|
| print("\nβ Some tests failed")
|
| sys.exit(1)
|
| else:
|
| print("\nβ Connection test failed. Please check your MongoDB connection details.")
|
| sys.exit(1)
|
|
|