File size: 7,657 Bytes
04aa1ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env python3
"""
Test runner for comprehensive user authentication tests

This script runs all the user authentication tests in the correct order
and provides a comprehensive report of the test results.
"""

import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

# Load environment variables
try:
    from dotenv import load_dotenv
    load_dotenv()
    print("✅ Environment variables loaded")
except ImportError:
    print("⚠️  dotenv not available - continuing without .env file loading")

import asyncio
import time
from datetime import datetime
import traceback


async def run_test_suite(test_name: str, test_function):
    """Run a test suite and capture results"""
    print(f"\n{'='*60}")
    print(f"🧪 RUNNING: {test_name}")
    print(f"{'='*60}")
    
    start_time = time.time()
    
    try:
        await test_function()
        end_time = time.time()
        duration = end_time - start_time
        
        print(f"\n✅ {test_name} PASSED ({duration:.2f}s)")
        return True, duration, None
        
    except Exception as e:
        end_time = time.time()
        duration = end_time - start_time
        error_msg = str(e)
        
        print(f"\n❌ {test_name} FAILED ({duration:.2f}s)")
        print(f"Error: {error_msg}")
        print("\nFull traceback:")
        traceback.print_exc()
        
        return False, duration, error_msg


async def main():
    """Run all user authentication tests"""
    print("🚀 COMPREHENSIVE USER AUTHENTICATION TEST SUITE")
    print("=" * 60)
    print(f"Started at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
    print("=" * 60)
    
    # Test suites to run
    test_suites = []
    
    # 1. Unit tests for user_id validation
    try:
        from test_user_id_validation import run_validation_tests
        test_suites.append(("User ID Validation Tests", run_validation_tests))
    except ImportError as e:
        print(f"⚠️  Could not import validation tests: {e}")
    
    # 2. Integration tests for chat requests
    try:
        from test_chat_integration_user_auth import run_integration_tests
        test_suites.append(("Chat Integration Tests", run_integration_tests))
    except ImportError as e:
        print(f"⚠️  Could not import integration tests: {e}")
    
    # 3. Backward compatibility tests
    try:
        from test_backward_compatibility import run_compatibility_tests
        test_suites.append(("Backward Compatibility Tests", run_compatibility_tests))
    except ImportError as e:
        print(f"⚠️  Could not import compatibility tests: {e}")
    
    # 4. Performance tests
    try:
        from test_performance_user_auth import run_performance_tests
        test_suites.append(("Performance Tests", run_performance_tests))
    except ImportError as e:
        print(f"⚠️  Could not import performance tests: {e}")
    
    # 5. Comprehensive tests
    try:
        from test_user_authentication_comprehensive import (
            run_unit_tests,
            run_analytics_tests,
            run_compatibility_tests as run_comp_tests
        )
        test_suites.append(("Comprehensive Unit Tests", run_unit_tests))
        test_suites.append(("Analytics Function Tests", run_analytics_tests))
        test_suites.append(("Comprehensive Compatibility Tests", run_comp_tests))
    except ImportError as e:
        print(f"⚠️  Could not import comprehensive tests: {e}")
    
    if not test_suites:
        print("❌ No test suites could be imported!")
        return False
    
    # Run all test suites
    results = []
    total_start_time = time.time()
    
    for test_name, test_function in test_suites:
        # Convert sync functions to async if needed
        if asyncio.iscoroutinefunction(test_function):
            success, duration, error = await run_test_suite(test_name, test_function)
        else:
            # Wrap sync function in async
            async def async_wrapper():
                test_function()
            success, duration, error = await run_test_suite(test_name, async_wrapper)
        
        results.append({
            'name': test_name,
            'success': success,
            'duration': duration,
            'error': error
        })
    
    total_duration = time.time() - total_start_time
    
    # Print summary report
    print("\n" + "="*60)
    print("📊 TEST SUMMARY REPORT")
    print("="*60)
    
    passed_tests = [r for r in results if r['success']]
    failed_tests = [r for r in results if not r['success']]
    
    print(f"Total test suites: {len(results)}")
    print(f"Passed: {len(passed_tests)}")
    print(f"Failed: {len(failed_tests)}")
    print(f"Total duration: {total_duration:.2f} seconds")
    print()
    
    # Detailed results
    for result in results:
        status = "✅ PASS" if result['success'] else "❌ FAIL"
        print(f"{status} {result['name']} ({result['duration']:.2f}s)")
        if result['error']:
            print(f"      Error: {result['error']}")
    
    print("\n" + "="*60)
    
    if failed_tests:
        print("❌ SOME TESTS FAILED")
        print("\nFailed test suites:")
        for result in failed_tests:
            print(f"  - {result['name']}: {result['error']}")
        
        print("\n🔧 TROUBLESHOOTING TIPS:")
        print("1. Ensure the server is running on localhost:7860 for integration tests")
        print("2. Check that MongoDB is accessible for database tests")
        print("3. Verify all dependencies are installed")
        print("4. Check that analytics modules are properly imported")
        
        return False
    else:
        print("🎉 ALL TESTS PASSED!")
        print("\n✨ User authentication feature is working correctly!")
        print("   - User ID validation is robust")
        print("   - Chat integration works with and without user_id")
        print("   - Backward compatibility is maintained")
        print("   - Performance is acceptable")
        print("   - Analytics functions work correctly")
        
        return True


def run_specific_test_suite(suite_name: str):
    """Run a specific test suite by name"""
    test_mapping = {
        'validation': 'test_user_id_validation.run_validation_tests',
        'integration': 'test_chat_integration_user_auth.run_integration_tests',
        'compatibility': 'test_backward_compatibility.run_compatibility_tests',
        'performance': 'test_performance_user_auth.run_performance_tests',
        'comprehensive': 'test_user_authentication_comprehensive.main'
    }
    
    if suite_name not in test_mapping:
        print(f"❌ Unknown test suite: {suite_name}")
        print(f"Available suites: {', '.join(test_mapping.keys())}")
        return False
    
    module_path = test_mapping[suite_name]
    module_name, function_name = module_path.rsplit('.', 1)
    
    try:
        module = __import__(module_name, fromlist=[function_name])
        test_function = getattr(module, function_name)
        
        if asyncio.iscoroutinefunction(test_function):
            return asyncio.run(test_function())
        else:
            test_function()
            return True
            
    except Exception as e:
        print(f"❌ Failed to run {suite_name} tests: {e}")
        traceback.print_exc()
        return False


if __name__ == "__main__":
    if len(sys.argv) > 1:
        # Run specific test suite
        suite_name = sys.argv[1].lower()
        success = run_specific_test_suite(suite_name)
    else:
        # Run all tests
        success = asyncio.run(main())
    
    sys.exit(0 if success else 1)