import pytest import os from unittest.mock import patch, MagicMock from config.settings import Settings class TestSettings: """Tests pour la classe Settings.""" @patch('config.settings.load_dotenv') def test_settings_with_valid_secret_key(self, mock_load_dotenv): """Test avec une clé secrète valide.""" with patch.dict(os.environ, {'SECRET_KEY': 'this-is-a-very-long-secret-key-for-testing-purposes-123'}): settings = Settings() assert len(settings.SECRET_KEY) >= 32 assert settings.SECRET_KEY == 'this-is-a-very-long-secret-key-for-testing-purposes-123' @patch('config.settings.load_dotenv') def test_settings_with_short_secret_key_raises_error(self, mock_load_dotenv): """Test avec une clé secrète trop courte.""" with patch.dict(os.environ, {'SECRET_KEY': 'short'}, clear=True): settings = Settings() with pytest.raises(ValueError, match="SECRET_KEY doit faire au moins 32 caractères"): _ = settings.SECRET_KEY @patch('config.settings.load_dotenv') def test_settings_without_secret_key_raises_error(self, mock_load_dotenv): """Test sans clé secrète.""" with patch.dict(os.environ, {}, clear=True): settings = Settings() with pytest.raises(ValueError, match="SECRET_KEY est obligatoire"): _ = settings.SECRET_KEY @patch('config.settings.load_dotenv') def test_database_url_default(self, mock_load_dotenv): """Test de l'URL de base de données par défaut.""" with patch.dict(os.environ, {'SECRET_KEY': 'this-is-a-very-long-secret-key-for-testing-purposes-123'}): settings = Settings() assert settings.DATABASE_URL == 'sqlite:///school_management.db' @patch('config.settings.load_dotenv') def test_database_url_custom(self, mock_load_dotenv): """Test avec une URL de base de données personnalisée.""" with patch.dict(os.environ, { 'SECRET_KEY': 'this-is-a-very-long-secret-key-for-testing-purposes-123', 'DATABASE_URL': 'postgresql://user:pass@localhost/test' }): settings = Settings() assert settings.DATABASE_URL == 'postgresql://user:pass@localhost/test' @patch('config.settings.load_dotenv') def test_debug_default_false(self, mock_load_dotenv): """Test que DEBUG est False par défaut.""" with patch.dict(os.environ, {'SECRET_KEY': 'this-is-a-very-long-secret-key-for-testing-purposes-123'}, clear=True): settings = Settings() assert settings.DEBUG is False @patch('config.settings.load_dotenv') def test_debug_true(self, mock_load_dotenv): """Test que DEBUG peut être activé.""" with patch.dict(os.environ, { 'SECRET_KEY': 'this-is-a-very-long-secret-key-for-testing-purposes-123', 'DEBUG': 'true' }): settings = Settings() assert settings.DEBUG is True @patch('config.settings.load_dotenv') def test_log_level_default(self, mock_load_dotenv): """Test du niveau de log par défaut.""" with patch.dict(os.environ, {'SECRET_KEY': 'this-is-a-very-long-secret-key-for-testing-purposes-123'}): settings = Settings() assert settings.LOG_LEVEL == 'INFO' @patch('config.settings.load_dotenv') def test_log_level_custom(self, mock_load_dotenv): """Test avec un niveau de log personnalisé.""" with patch.dict(os.environ, { 'SECRET_KEY': 'this-is-a-very-long-secret-key-for-testing-purposes-123', 'LOG_LEVEL': 'debug' }): settings = Settings() assert settings.LOG_LEVEL == 'DEBUG' @patch('config.settings.load_dotenv') def test_wtf_csrf_time_limit_default(self, mock_load_dotenv): """Test du timeout CSRF par défaut.""" with patch.dict(os.environ, {'SECRET_KEY': 'this-is-a-very-long-secret-key-for-testing-purposes-123'}): settings = Settings() assert settings.WTF_CSRF_TIME_LIMIT == 3600 @patch('config.settings.load_dotenv') def test_wtf_csrf_time_limit_custom(self, mock_load_dotenv): """Test avec un timeout CSRF personnalisé.""" with patch.dict(os.environ, { 'SECRET_KEY': 'this-is-a-very-long-secret-key-for-testing-purposes-123', 'WTF_CSRF_TIME_LIMIT': '7200' }): settings = Settings() assert settings.WTF_CSRF_TIME_LIMIT == 7200