findEthics/Atlas
0
1#!/usr/bin/env python32"""3Test runner for comprehensive user authentication tests4 5This script runs all the user authentication tests in the correct order6and provides a comprehensive report of the test results.7"""8 9import sys10import os11sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))12 13# Load environment variables14try:15 from dotenv import load_dotenv16 load_dotenv()17 print("✅ Environment variables loaded")18except ImportError:19 print("⚠️ dotenv not available - continuing without .env file loading")20 21import asyncio22import time23from datetime import datetime24import traceback25 26 27async def run_test_suite(test_name: str, test_function):28 """Run a test suite and capture results"""29 print(f"\n{'='*60}")30 print(f"🧪 RUNNING: {test_name}")31 print(f"{'='*60}")32 33 start_time = time.time()34 35 try:36 await test_function()37 end_time = time.time()38 duration = end_time - start_time39 40 print(f"\n✅ {test_name} PASSED ({duration:.2f}s)")41 return True, duration, None42 43 except Exception as e:44 end_time = time.time()45 duration = end_time - start_time46 error_msg = str(e)47 48 print(f"\n❌ {test_name} FAILED ({duration:.2f}s)")49 print(f"Error: {error_msg}")50 print("\nFull traceback:")51 traceback.print_exc()52 53 return False, duration, error_msg54 55 56async def main():57 """Run all user authentication tests"""58 print("🚀 COMPREHENSIVE USER AUTHENTICATION TEST SUITE")59 print("=" * 60)60 print(f"Started at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")61 print("=" * 60)62 63 # Test suites to run64 test_suites = []65 66 # 1. Unit tests for user_id validation67 try:68 from test_user_id_validation import run_validation_tests69 test_suites.append(("User ID Validation Tests", run_validation_tests))70 except ImportError as e:71 print(f"⚠️ Could not import validation tests: {e}")72 73 # 2. Integration tests for chat requests74 try:75 from test_chat_integration_user_auth import run_integration_tests76 test_suites.append(("Chat Integration Tests", run_integration_tests))77 except ImportError as e:78 print(f"⚠️ Could not import integration tests: {e}")79 80 # 3. Backward compatibility tests81 try:82 from test_backward_compatibility import run_compatibility_tests83 test_suites.append(("Backward Compatibility Tests", run_compatibility_tests))84 except ImportError as e:85 print(f"⚠️ Could not import compatibility tests: {e}")86 87 # 4. Performance tests88 try:89 from test_performance_user_auth import run_performance_tests90 test_suites.append(("Performance Tests", run_performance_tests))91 except ImportError as e:92 print(f"⚠️ Could not import performance tests: {e}")93 94 # 5. Comprehensive tests95 try:96 from test_user_authentication_comprehensive import (97 run_unit_tests,98 run_analytics_tests,99 run_compatibility_tests as run_comp_tests100 )101 test_suites.append(("Comprehensive Unit Tests", run_unit_tests))102 test_suites.append(("Analytics Function Tests", run_analytics_tests))103 test_suites.append(("Comprehensive Compatibility Tests", run_comp_tests))104 except ImportError as e:105 print(f"⚠️ Could not import comprehensive tests: {e}")106 107 if not test_suites:108 print("❌ No test suites could be imported!")109 return False110 111 # Run all test suites112 results = []113 total_start_time = time.time()114 115 for test_name, test_function in test_suites:116 # Convert sync functions to async if needed117 if asyncio.iscoroutinefunction(test_function):118 success, duration, error = await run_test_suite(test_name, test_function)119 else:120 # Wrap sync function in async121 async def async_wrapper():122 test_function()123 success, duration, error = await run_test_suite(test_name, async_wrapper)124 125 results.append({126 'name': test_name,127 'success': success,128 'duration': duration,129 'error': error130 })131 132 total_duration = time.time() - total_start_time133 134 # Print summary report135 print("\n" + "="*60)136 print("📊 TEST SUMMARY REPORT")137 print("="*60)138 139 passed_tests = [r for r in results if r['success']]140 failed_tests = [r for r in results if not r['success']]141 142 print(f"Total test suites: {len(results)}")143 print(f"Passed: {len(passed_tests)}")144 print(f"Failed: {len(failed_tests)}")145 print(f"Total duration: {total_duration:.2f} seconds")146 print()147 148 # Detailed results149 for result in results:150 status = "✅ PASS" if result['success'] else "❌ FAIL"151 print(f"{status} {result['name']} ({result['duration']:.2f}s)")152 if result['error']:153 print(f" Error: {result['error']}")154 155 print("\n" + "="*60)156 157 if failed_tests:158 print("❌ SOME TESTS FAILED")159 print("\nFailed test suites:")160 for result in failed_tests:161 print(f" - {result['name']}: {result['error']}")162 163 print("\n🔧 TROUBLESHOOTING TIPS:")164 print("1. Ensure the server is running on localhost:7860 for integration tests")165 print("2. Check that MongoDB is accessible for database tests")166 print("3. Verify all dependencies are installed")167 print("4. Check that analytics modules are properly imported")168 169 return False170 else:171 print("🎉 ALL TESTS PASSED!")172 print("\n✨ User authentication feature is working correctly!")173 print(" - User ID validation is robust")174 print(" - Chat integration works with and without user_id")175 print(" - Backward compatibility is maintained")176 print(" - Performance is acceptable")177 print(" - Analytics functions work correctly")178 179 return True180 181 182def run_specific_test_suite(suite_name: str):183 """Run a specific test suite by name"""184 test_mapping = {185 'validation': 'test_user_id_validation.run_validation_tests',186 'integration': 'test_chat_integration_user_auth.run_integration_tests',187 'compatibility': 'test_backward_compatibility.run_compatibility_tests',188 'performance': 'test_performance_user_auth.run_performance_tests',189 'comprehensive': 'test_user_authentication_comprehensive.main'190 }191 192 if suite_name not in test_mapping:193 print(f"❌ Unknown test suite: {suite_name}")194 print(f"Available suites: {', '.join(test_mapping.keys())}")195 return False196 197 module_path = test_mapping[suite_name]198 module_name, function_name = module_path.rsplit('.', 1)199 200 try:201 module = __import__(module_name, fromlist=[function_name])202 test_function = getattr(module, function_name)203 204 if asyncio.iscoroutinefunction(test_function):205 return asyncio.run(test_function())206 else:207 test_function()208 return True209 210 except Exception as e:211 print(f"❌ Failed to run {suite_name} tests: {e}")212 traceback.print_exc()213 return False214 215 216if __name__ == "__main__":217 if len(sys.argv) > 1:218 # Run specific test suite219 suite_name = sys.argv[1].lower()220 success = run_specific_test_suite(suite_name)221 else:222 # Run all tests223 success = asyncio.run(main())224 225 sys.exit(0 if success else 1)