Files
GitMa/backend/tests/test_config.py
panw cd6bf9bb13 feat: add configuration management
- Add Settings class using pydantic-settings
- Load config from environment variables with GM_ prefix
- Support encrypt_key and api_token (required, no defaults for security)
- Provide defaults for data_dir, host, port
- Add computed properties for db_path, ssh_keys_dir, repos_dir
- Add tests for config defaults and environment variable overrides
- Add Base class to app.models to unblock conftest.py imports

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-30 15:07:55 +08:00

33 lines
1.0 KiB
Python

import os
import pytest
import base64
from pathlib import Path
def test_config_defaults(test_env_vars, monkeypatch):
"""测试配置默认值."""
# Clear GM_DATA_DIR to test default value
monkeypatch.delenv("GM_DATA_DIR", raising=False)
# Reload config to pick up the change
from app.config import Settings
settings = Settings()
assert settings.data_dir == Path('./data')
assert settings.host == '0.0.0.0'
assert settings.port == 8000
def test_config_from_env(monkeypatch):
"""测试从环境变量读取配置."""
# Set required security fields
monkeypatch.setenv("GM_ENCRYPT_KEY", base64.b64encode(b'test-key-32-bytes-long-1234567890').decode())
monkeypatch.setenv("GM_API_TOKEN", "test-token")
monkeypatch.setenv("GM_DATA_DIR", "/custom/data")
monkeypatch.setenv("GM_PORT", "9000")
# 重新加载配置
from app.config import Settings
settings = Settings()
assert settings.data_dir == Path('/custom/data')
assert settings.port == 9000