""" Unit tests for configuration settings module. Tests the environment variable loading and default values for application configuration. """ import os import pytest from unittest.mock import patch from app.config.settings import ( get_mongodb_url, get_mongodb_database_name, get_jwt_secret_key, get_jwt_algorithm, get_jwt_expire_hours, is_development_environment ) def test_i_can_load_mongodb_url_from_env(): """Test loading MongoDB URL from environment variable.""" test_url = "mongodb://test-server:27017" with patch.dict(os.environ, {"MONGODB_URL": test_url}): result = get_mongodb_url() assert result == test_url def test_i_can_use_default_mongodb_url(): """Test default MongoDB URL when environment variable is not set.""" with patch.dict(os.environ, {}, clear=True): result = get_mongodb_url() assert result == "mongodb://localhost:27017" def test_i_can_load_mongodb_database_name_from_env(): """Test loading MongoDB database name from environment variable.""" test_db_name = "test_database" with patch.dict(os.environ, {"MONGODB_DATABASE": test_db_name}): result = get_mongodb_database_name() assert result == test_db_name def test_i_can_use_default_mongodb_database_name(): """Test default MongoDB database name when environment variable is not set.""" with patch.dict(os.environ, {}, clear=True): result = get_mongodb_database_name() assert result == "mydocmanager" def test_i_can_load_jwt_secret_from_env(): """Test loading JWT secret from environment variable.""" test_secret = "super-secret-key-123" with patch.dict(os.environ, {"JWT_SECRET": test_secret}): result = get_jwt_secret_key() assert result == test_secret def test_i_can_use_default_jwt_secret_in_development(): """Test default JWT secret in development environment.""" with patch.dict(os.environ, {"ENVIRONMENT": "development"}, clear=True): result = get_jwt_secret_key() assert result == "dev-secret-key-change-in-production" def test_i_cannot_get_jwt_secret_in_production_without_env(): """Test that JWT secret raises error in production without environment variable.""" with patch.dict(os.environ, {"ENVIRONMENT": "production"}, clear=True): with pytest.raises(ValueError, match="JWT_SECRET environment variable must be set in production"): get_jwt_secret_key() def test_i_can_load_jwt_algorithm_from_env(): """Test loading JWT algorithm from environment variable.""" test_algorithm = "RS256" with patch.dict(os.environ, {"JWT_ALGORITHM": test_algorithm}): result = get_jwt_algorithm() assert result == test_algorithm def test_i_can_use_default_jwt_algorithm(): """Test default JWT algorithm when environment variable is not set.""" with patch.dict(os.environ, {}, clear=True): result = get_jwt_algorithm() assert result == "HS256" def test_i_can_load_jwt_expire_hours_from_env(): """Test loading JWT expiration hours from environment variable.""" test_hours = "48" with patch.dict(os.environ, {"JWT_EXPIRE_HOURS": test_hours}): result = get_jwt_expire_hours() assert result == 48 def test_i_can_use_default_jwt_expire_hours(): """Test default JWT expiration hours when environment variable is not set.""" with patch.dict(os.environ, {}, clear=True): result = get_jwt_expire_hours() assert result == 24 def test_i_can_handle_invalid_jwt_expire_hours(): """Test handling of invalid JWT expiration hours value.""" with patch.dict(os.environ, {"JWT_EXPIRE_HOURS": "invalid"}): result = get_jwt_expire_hours() assert result == 24 # Should fall back to default def test_i_can_detect_development_environment(): """Test detection of development environment.""" with patch.dict(os.environ, {"ENVIRONMENT": "development"}): result = is_development_environment() assert result is True def test_i_can_detect_production_environment(): """Test detection of production environment.""" with patch.dict(os.environ, {"ENVIRONMENT": "production"}): result = is_development_environment() assert result is False def test_i_can_use_default_development_environment(): """Test default environment is development.""" with patch.dict(os.environ, {}, clear=True): result = is_development_environment() assert result is True def test_i_can_handle_case_insensitive_environment(): """Test case insensitive environment detection.""" with patch.dict(os.environ, {"ENVIRONMENT": "DEVELOPMENT"}): result = is_development_environment() assert result is True