This commit is contained in:
2026-01-29 19:53:36 +01:00
parent 1bda2013bb
commit a9708b33e2
27 changed files with 3745 additions and 4 deletions

1
tests/__init__.py Normal file
View File

@@ -0,0 +1 @@
"""Tests for AegisGitea MCP."""

27
tests/conftest.py Normal file
View File

@@ -0,0 +1,27 @@
"""Pytest configuration and fixtures."""
import os
from typing import Generator
import pytest
from aegis_gitea_mcp.config import reset_settings
from aegis_gitea_mcp.audit import reset_audit_logger
@pytest.fixture(autouse=True)
def reset_globals() -> Generator[None, None, None]:
"""Reset global singletons between tests."""
yield
reset_settings()
reset_audit_logger()
@pytest.fixture
def mock_env(monkeypatch: pytest.MonkeyPatch) -> None:
"""Set up mock environment variables for testing."""
monkeypatch.setenv("GITEA_URL", "https://gitea.example.com")
monkeypatch.setenv("GITEA_TOKEN", "test-token-12345")
monkeypatch.setenv("MCP_HOST", "0.0.0.0")
monkeypatch.setenv("MCP_PORT", "8080")
monkeypatch.setenv("LOG_LEVEL", "DEBUG")

73
tests/test_config.py Normal file
View File

@@ -0,0 +1,73 @@
"""Tests for configuration management."""
import pytest
from pydantic import ValidationError
from aegis_gitea_mcp.config import Settings, get_settings, reset_settings
def test_settings_from_env(mock_env: None) -> None:
"""Test loading settings from environment variables."""
settings = get_settings()
assert settings.gitea_base_url == "https://gitea.example.com"
assert settings.gitea_token == "test-token-12345"
assert settings.mcp_host == "0.0.0.0"
assert settings.mcp_port == 8080
assert settings.log_level == "DEBUG"
def test_settings_defaults(monkeypatch: pytest.MonkeyPatch) -> None:
"""Test default values when not specified."""
monkeypatch.setenv("GITEA_URL", "https://gitea.example.com")
monkeypatch.setenv("GITEA_TOKEN", "test-token")
settings = get_settings()
assert settings.mcp_host == "0.0.0.0"
assert settings.mcp_port == 8080
assert settings.log_level == "INFO"
assert settings.max_file_size_bytes == 1_048_576
assert settings.request_timeout_seconds == 30
def test_settings_validation_missing_required(monkeypatch: pytest.MonkeyPatch) -> None:
"""Test that missing required fields raise validation errors."""
monkeypatch.delenv("GITEA_URL", raising=False)
monkeypatch.delenv("GITEA_TOKEN", raising=False)
reset_settings()
with pytest.raises(ValidationError):
get_settings()
def test_settings_invalid_log_level(monkeypatch: pytest.MonkeyPatch) -> None:
"""Test that invalid log levels are rejected."""
monkeypatch.setenv("GITEA_URL", "https://gitea.example.com")
monkeypatch.setenv("GITEA_TOKEN", "test-token")
monkeypatch.setenv("LOG_LEVEL", "INVALID")
reset_settings()
with pytest.raises(ValidationError):
get_settings()
def test_settings_empty_token(monkeypatch: pytest.MonkeyPatch) -> None:
"""Test that empty tokens are rejected."""
monkeypatch.setenv("GITEA_URL", "https://gitea.example.com")
monkeypatch.setenv("GITEA_TOKEN", " ")
reset_settings()
with pytest.raises(ValidationError):
get_settings()
def test_settings_singleton() -> None:
"""Test that get_settings returns same instance."""
settings1 = get_settings()
settings2 = get_settings()
assert settings1 is settings2