apac-ops-bot/backend/tests/test_config.py
2026-02-11 12:54:37 +00:00

140 lines
4.4 KiB
Python

"""
Tests for configuration management
Tests Settings loading from environment variables and validation
"""
import pytest
from pydantic import ValidationError
from app.config import Settings
def test_settings_default_values():
"""Test Settings with default values"""
settings = Settings(
SECRET_KEY="test-secret-key",
DATABASE_URL="postgresql+asyncpg://user:pass@localhost/db",
OPENAI_API_KEY="sk-test-key",
)
assert settings.APP_NAME == "The APAC OpsBot"
assert settings.APP_ENV == "development"
assert settings.DEBUG is True
assert settings.OPENAI_MODEL == "gpt-5-nano-2025-08-07"
assert settings.OPENAI_VECTOR_STORE_ID == "vs_QkOKiQCqzCHS4iFT5lP9qUxc"
def test_settings_custom_values():
"""Test Settings with custom environment values"""
settings = Settings(
APP_NAME="Custom Bot",
APP_ENV="production",
DEBUG=False,
SECRET_KEY="custom-secret",
DATABASE_URL="postgresql+asyncpg://custom:pass@db/custom_db",
OPENAI_API_KEY="sk-custom-key",
OPENAI_MODEL="gpt-4",
)
assert settings.APP_NAME == "Custom Bot"
assert settings.APP_ENV == "production"
assert settings.DEBUG is False
assert settings.OPENAI_MODEL == "gpt-4"
def test_settings_required_fields_missing():
"""Test that required fields raise ValidationError when missing"""
with pytest.raises(ValidationError) as exc_info:
Settings()
errors = exc_info.value.errors()
error_fields = {error["loc"][0] for error in errors}
# Check that required fields are in the error
assert "SECRET_KEY" in error_fields
assert "DATABASE_URL" in error_fields
assert "OPENAI_API_KEY" in error_fields
def test_settings_database_url_format():
"""Test DATABASE_URL is properly formatted"""
settings = Settings(
SECRET_KEY="test-secret",
DATABASE_URL="postgresql+asyncpg://apac_ops_bot:password@localhost:5432/apac_ops_bot",
OPENAI_API_KEY="sk-test-key",
)
assert "postgresql+asyncpg://" in settings.DATABASE_URL
assert "apac_ops_bot" in settings.DATABASE_URL
def test_settings_azure_ad_configuration():
"""Test Azure AD configuration fields"""
settings = Settings(
SECRET_KEY="test-secret",
DATABASE_URL="postgresql+asyncpg://user:pass@localhost/db",
OPENAI_API_KEY="sk-test-key",
AZURE_TENANT_ID="test-tenant-id",
AZURE_CLIENT_ID="test-client-id",
AZURE_CLIENT_SECRET="test-client-secret",
AZURE_REDIRECT_URI="http://localhost:8048/api/v1/auth/callback",
)
assert settings.AZURE_TENANT_ID == "test-tenant-id"
assert settings.AZURE_CLIENT_ID == "test-client-id"
assert settings.AZURE_CLIENT_SECRET == "test-client-secret"
assert settings.AZURE_REDIRECT_URI == "http://localhost:8048/api/v1/auth/callback"
def test_settings_redis_url():
"""Test Redis URL configuration"""
settings = Settings(
SECRET_KEY="test-secret",
DATABASE_URL="postgresql+asyncpg://user:pass@localhost/db",
OPENAI_API_KEY="sk-test-key",
REDIS_URL="redis://localhost:6399/0",
)
assert settings.REDIS_URL == "redis://localhost:6399/0"
def test_settings_cors_origins():
"""Test CORS origins configuration"""
settings = Settings(
SECRET_KEY="test-secret",
DATABASE_URL="postgresql+asyncpg://user:pass@localhost/db",
OPENAI_API_KEY="sk-test-key",
CORS_ORIGINS="http://localhost:3000,https://example.com",
)
assert "http://localhost:3000" in settings.CORS_ORIGINS
assert "https://example.com" in settings.CORS_ORIGINS
def test_settings_rate_limits():
"""Test rate limiting configuration"""
settings = Settings(
SECRET_KEY="test-secret",
DATABASE_URL="postgresql+asyncpg://user:pass@localhost/db",
OPENAI_API_KEY="sk-test-key",
RATE_LIMIT_PER_MINUTE=60,
RATE_LIMIT_PER_DAY=5000,
)
assert settings.RATE_LIMIT_PER_MINUTE == 60
assert settings.RATE_LIMIT_PER_DAY == 5000
def test_settings_token_costs():
"""Test token cost configuration"""
settings = Settings(
SECRET_KEY="test-secret",
DATABASE_URL="postgresql+asyncpg://user:pass@localhost/db",
OPENAI_API_KEY="sk-test-key",
PROMPT_TOKEN_COST=0.0001,
COMPLETION_TOKEN_COST=0.0002,
)
assert settings.PROMPT_TOKEN_COST == 0.0001
assert settings.COMPLETION_TOKEN_COST == 0.0002