#!/usr/bin/env python3 """ Test runner for authentication system tests Runs both unit tests and integration tests with proper reporting """ import unittest import sys import os from io import StringIO # Add parent directory to Python path so we can import the main modules sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) def run_tests(): """Run all authentication tests and generate report""" # Create test suite loader = unittest.TestLoader() suite = unittest.TestSuite() # Load unit tests try: from test_auth_unit import ( TestUserModel, TestChatSessionModel, TestAuthenticationUtilities, TestLoginRequiredDecorator ) suite.addTests(loader.loadTestsFromTestCase(TestUserModel)) suite.addTests(loader.loadTestsFromTestCase(TestChatSessionModel)) suite.addTests(loader.loadTestsFromTestCase(TestAuthenticationUtilities)) suite.addTests(loader.loadTestsFromTestCase(TestLoginRequiredDecorator)) print("✓ Unit tests loaded successfully") except ImportError as e: print(f"✗ Failed to load unit tests: {e}") return False # Load integration tests try: from test_auth_simple import TestSimpleAuthenticationFlows suite.addTests(loader.loadTestsFromTestCase(TestSimpleAuthenticationFlows)) print("✓ Integration tests loaded successfully") except ImportError as e: print(f"✗ Failed to load integration tests: {e}") return False # Run tests with detailed output print("\n" + "="*60) print("RUNNING AUTHENTICATION SYSTEM TESTS") print("="*60) # Capture test output stream = StringIO() runner = unittest.TextTestRunner( stream=stream, verbosity=2, buffer=True, failfast=False ) result = runner.run(suite) # Print results output = stream.getvalue() print(output) # Print summary print("\n" + "="*60) print("TEST SUMMARY") print("="*60) print(f"Tests run: {result.testsRun}") print(f"Failures: {len(result.failures)}") print(f"Errors: {len(result.errors)}") print(f"Skipped: {len(result.skipped) if hasattr(result, 'skipped') else 0}") if result.failures: print(f"\n❌ FAILURES ({len(result.failures)}):") for test, traceback in result.failures: print(f" - {test}") if result.errors: print(f"\n💥 ERRORS ({len(result.errors)}):") for test, traceback in result.errors: print(f" - {test}") success_rate = ((result.testsRun - len(result.failures) - len(result.errors)) / result.testsRun * 100) if result.testsRun > 0 else 0 print(f"\n📊 Success Rate: {success_rate:.1f}%") if result.wasSuccessful(): print("\n🎉 ALL TESTS PASSED!") return True else: print(f"\n❌ {len(result.failures) + len(result.errors)} TEST(S) FAILED") return False def run_specific_test_class(test_class_name): """Run a specific test class""" # Map test class names to modules test_classes = { 'TestUserModel': 'test_auth_unit', 'TestChatSessionModel': 'test_auth_unit', 'TestAuthenticationUtilities': 'test_auth_unit', 'TestLoginRequiredDecorator': 'test_auth_unit', 'TestSimpleAuthenticationFlows': 'test_auth_simple' } if test_class_name not in test_classes: print(f"❌ Unknown test class: {test_class_name}") print(f"Available classes: {', '.join(test_classes.keys())}") return False try: module_name = test_classes[test_class_name] module = __import__(module_name) test_class = getattr(module, test_class_name) suite = unittest.TestLoader().loadTestsFromTestCase(test_class) runner = unittest.TextTestRunner(verbosity=2) result = runner.run(suite) return result.wasSuccessful() except Exception as e: print(f"❌ Error running test class {test_class_name}: {e}") return False def main(): """Main entry point""" if len(sys.argv) > 1: # Run specific test class test_class = sys.argv[1] success = run_specific_test_class(test_class) else: # Run all tests success = run_tests() # Exit with appropriate code sys.exit(0 if success else 1) if __name__ == '__main__': main()