Aniworld/tests/unit/web/test_authentication.py

127 lines
4.2 KiB
Python

"""
Test suite for authentication and session management.
This test module validates the authentication system, session management,
and security features.
"""
import unittest
import sys
import os
from unittest.mock import patch, MagicMock
# Add parent directory to path for imports
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
class TestAuthenticationSystem(unittest.TestCase):
"""Test class for authentication and session management."""
def setUp(self):
"""Set up test fixtures before each test method."""
# Mock Flask app for testing
self.mock_app = MagicMock()
self.mock_app.config = {'SECRET_KEY': 'test_secret'}
def test_session_manager_initialization(self):
"""Test SessionManager initialization."""
try:
from auth import SessionManager
manager = SessionManager()
self.assertIsNotNone(manager)
self.assertTrue(hasattr(manager, 'login'))
self.assertTrue(hasattr(manager, 'check_password'))
print('✓ SessionManager initialization successful')
except Exception as e:
self.fail(f'SessionManager initialization failed: {e}')
def test_login_method_exists(self):
"""Test that login method exists and returns proper response."""
try:
from auth import SessionManager
manager = SessionManager()
# Test login method exists
self.assertTrue(hasattr(manager, 'login'))
# Test login with invalid credentials returns dict
result = manager.login('wrong_password')
self.assertIsInstance(result, dict)
self.assertIn('success', result)
self.assertFalse(result['success'])
print('✓ SessionManager login method validated')
except Exception as e:
self.fail(f'SessionManager login method test failed: {e}')
def test_password_checking(self):
"""Test password validation functionality."""
try:
from auth import SessionManager
manager = SessionManager()
# Test check_password method exists
self.assertTrue(hasattr(manager, 'check_password'))
# Test with empty/invalid password
result = manager.check_password('')
self.assertFalse(result)
result = manager.check_password('wrong_password')
self.assertFalse(result)
print('✓ SessionManager password checking validated')
except Exception as e:
self.fail(f'SessionManager password checking test failed: {e}')
class TestConfigurationSystem(unittest.TestCase):
"""Test class for configuration management."""
def test_config_manager_initialization(self):
"""Test ConfigManager initialization."""
try:
from config import ConfigManager
manager = ConfigManager()
self.assertIsNotNone(manager)
self.assertTrue(hasattr(manager, 'anime_directory'))
print('✓ ConfigManager initialization successful')
except Exception as e:
self.fail(f'ConfigManager initialization failed: {e}')
def test_anime_directory_property(self):
"""Test anime_directory property getter and setter."""
try:
from config import ConfigManager
manager = ConfigManager()
# Test getter
initial_dir = manager.anime_directory
self.assertIsInstance(initial_dir, str)
# Test setter exists
test_dir = 'C:\\TestAnimeDir'
manager.anime_directory = test_dir
# Verify setter worked
self.assertEqual(manager.anime_directory, test_dir)
print('✓ ConfigManager anime_directory property validated')
except Exception as e:
self.fail(f'ConfigManager anime_directory property test failed: {e}')
if __name__ == '__main__':
unittest.main(verbosity=2, buffer=True)