import os import requests import json from dotenv import load_dotenv # Load environment variables load_dotenv() # Base URL for API BASE_URL = "http://localhost:5000/api" # Global variables to store data between tests department_data = None admin_user = None token = None def test_create_department(): """Test creating a new department with an admin user""" global department_data, admin_user # Department data department_data = { "name": "Auth Test Department", "address": "456 Auth Street, Test City, TS 67890", "website": "https://auth-test.example.com", "admin_email": "admin@auth-test.example.com", "admin_name": "Auth Admin", "admin_password": "SecureTestPassword123" } # Make POST request to create department response = requests.post(f"{BASE_URL}/departments", json=department_data) # Print response details print(f"Status Code: {response.status_code}") print("Response:") print(json.dumps(response.json(), indent=2)) # Store created department and admin user result = response.json() if result.get('department') and result.get('admin_user'): department_data = result['department'] admin_user = result['admin_user'] print("\n=== Department Created Successfully ===") print(f"Department ID: {department_data['_id']}") print(f"Admin Email: {admin_user['email']}") return True return False def test_login(): """Test admin login""" global token if not admin_user: print("Error: No admin user available. Run test_create_department first.") return False # Login data login_data = { "email": department_data["admin_email"], "password": department_data["admin_password"] } # Make POST request to login response = requests.post(f"{BASE_URL}/auth/login", json=login_data) # Print response details print("\n=== Testing Admin Login ===") print(f"Status Code: {response.status_code}") print("Response:") print(json.dumps(response.json(), indent=2)) # Store token result = response.json() if result.get('token'): token = result['token'] print("\n=== Login Successful ===") print(f"Token: {token[:20]}...") return True return False def test_get_current_user(): """Test getting current user information""" if not token: print("Error: No token available. Run test_login first.") return False # Set up headers with token headers = { "Authorization": f"Bearer {token}" } # Make GET request to get current user response = requests.get(f"{BASE_URL}/auth/me", headers=headers) # Print response details print("\n=== Testing Get Current User ===") print(f"Status Code: {response.status_code}") print("Response:") print(json.dumps(response.json(), indent=2)) # Check if successful result = response.json() if result.get('user'): print("\n=== Get Current User Successful ===") return True return False def test_update_profile(): """Test updating user profile""" if not token: print("Error: No token available. Run test_login first.") return False # Set up headers with token headers = { "Authorization": f"Bearer {token}", "Content-Type": "application/json" } # Profile update data profile_data = { "name": "Updated Admin Name", "position": "Chief Administrator" } # Make PUT request to update profile response = requests.put(f"{BASE_URL}/auth/profile", headers=headers, json=profile_data) # Print response details print("\n=== Testing Update Profile ===") print(f"Status Code: {response.status_code}") print("Response:") print(json.dumps(response.json(), indent=2)) # Check if successful result = response.json() if result.get('message') == 'Profile updated successfully': print("\n=== Profile Update Successful ===") return True return False def test_update_password(): """Test updating user password""" if not token: print("Error: No token available. Run test_login first.") return False # Set up headers with token headers = { "Authorization": f"Bearer {token}", "Content-Type": "application/json" } # Password update data password_data = { "current_password": department_data["admin_password"], "new_password": "NewSecurePassword456" } # Update the stored password for future tests department_data["admin_password"] = password_data["new_password"] # Make PUT request to update password response = requests.put(f"{BASE_URL}/auth/password", headers=headers, json=password_data) # Print response details print("\n=== Testing Update Password ===") print(f"Status Code: {response.status_code}") print("Response:") print(json.dumps(response.json(), indent=2)) # Check if successful result = response.json() if result.get('message') == 'Password updated successfully': print("\n=== Password Update Successful ===") return True return False def main(): """Run test functions in sequence""" # Step 1: Create department with admin user if not test_create_department(): print("Failed to create department. Exiting tests.") return # Step 2: Login as admin if not test_login(): print("Failed to login. Exiting tests.") return # Step 3: Get current user test_get_current_user() # Step 4: Update profile test_update_profile() # Step 5: Update password test_update_password() # Step 6: Login with new password to verify print("\n=== Verifying login with new password ===") test_login() print("\n=== All authentication tests completed ===") if __name__ == "__main__": main()