chore: more tests

This commit is contained in:
Alex committed 2026-03-30 16:13:08 +01:00
1 parent dc6db847ca
commit d5c0322e2a
64 files changed
+24436 -140

No files matched your search

View File
Whitespace-only changes.
+95
View File
@@ -337,3 +337,98 @@ class TestModelRegistry:
reg = ModelRegistry()
# Should have at least docsgpt-local
assert reg.default_model_id is not None
@pytest.mark.unit
def test_default_model_from_provider_fallback(self):
"""When LLM_NAME is not set but LLM_PROVIDER and API_KEY are,
default should be first model of that provider."""
mock_settings = MagicMock()
mock_settings.OPENAI_BASE_URL = None
mock_settings.OPENAI_API_KEY = "sk-test"
mock_settings.OPENAI_API_BASE = None
mock_settings.ANTHROPIC_API_KEY = None
mock_settings.GOOGLE_API_KEY = None
mock_settings.GROQ_API_KEY = None
mock_settings.OPEN_ROUTER_API_KEY = None
mock_settings.NOVITA_API_KEY = None
mock_settings.HUGGINGFACE_API_KEY = None
mock_settings.LLM_PROVIDER = "openai"
mock_settings.LLM_NAME = None
mock_settings.API_KEY = "sk-test"
with patch("application.core.settings.settings", mock_settings):
reg = ModelRegistry()
assert reg.default_model_id is not None
@pytest.mark.unit
def test_add_google_models_no_key_with_provider(self):
with patch.object(ModelRegistry, "_load_models"):
reg = ModelRegistry()
reg.models = {}
mock_settings = MagicMock()
mock_settings.GOOGLE_API_KEY = None
mock_settings.LLM_PROVIDER = "google"
mock_settings.LLM_NAME = "nonexistent"
reg._add_google_models(mock_settings)
assert len(reg.models) > 0
@pytest.mark.unit
def test_add_groq_models_no_key_with_provider(self):
with patch.object(ModelRegistry, "_load_models"):
reg = ModelRegistry()
reg.models = {}
mock_settings = MagicMock()
mock_settings.GROQ_API_KEY = None
mock_settings.LLM_PROVIDER = "groq"
mock_settings.LLM_NAME = "nonexistent"
reg._add_groq_models(mock_settings)
assert len(reg.models) > 0
@pytest.mark.unit
def test_add_openrouter_models_no_key_with_provider(self):
with patch.object(ModelRegistry, "_load_models"):
reg = ModelRegistry()
reg.models = {}
mock_settings = MagicMock()
mock_settings.OPEN_ROUTER_API_KEY = None
mock_settings.LLM_PROVIDER = "openrouter"
mock_settings.LLM_NAME = "nonexistent"
reg._add_openrouter_models(mock_settings)
assert len(reg.models) > 0
@pytest.mark.unit
def test_add_novita_models_no_key_with_provider(self):
with patch.object(ModelRegistry, "_load_models"):
reg = ModelRegistry()
reg.models = {}
mock_settings = MagicMock()
mock_settings.NOVITA_API_KEY = None
mock_settings.LLM_PROVIDER = "novita"
mock_settings.LLM_NAME = "nonexistent"
reg._add_novita_models(mock_settings)
assert len(reg.models) > 0
@pytest.mark.unit
def test_to_dict_disabled_model(self):
model = AvailableModel(
id="disabled",
provider=ModelProvider.OPENAI,
display_name="Disabled",
enabled=False,
)
d = model.to_dict()
assert d["enabled"] is False
@pytest.mark.unit
def test_to_dict_with_attachment_types(self):
caps = ModelCapabilities(
supported_attachment_types=["image/png", "application/pdf"],
)
model = AvailableModel(
id="vision",
provider=ModelProvider.OPENAI,
display_name="Vision",
capabilities=caps,
)
d = model.to_dict()
assert d["supported_attachment_types"] == ["image/png", "application/pdf"]
+64
View File
@@ -195,3 +195,67 @@ class TestValidateUrlSafe:
is_valid, url, error = validate_url_safe("http://192.168.1.1")
assert is_valid is False
assert "private" in error.lower() or "internal" in error.lower()
def test_adds_scheme_when_missing(self):
with patch("application.core.url_validation.resolve_hostname") as mock_resolve:
mock_resolve.return_value = "93.184.216.34"
is_valid, url, error = validate_url_safe("example.com")
assert is_valid is True
assert url == "http://example.com"
class TestIsPrivateIPExtended:
"""Additional edge cases for IP classification."""
def test_multicast_ip(self):
assert is_private_ip("224.0.0.1") is True
def test_unspecified_ip(self):
assert is_private_ip("0.0.0.0") is True
def test_ipv6_loopback(self):
assert is_private_ip("::1") is True
def test_ipv6_private(self):
assert is_private_ip("fc00::1") is True
def test_ipv6_public(self):
assert is_private_ip("2607:f8b0:4004:800::200e") is False
def test_reserved_ip(self):
# 240.0.0.0/4 is reserved (future use), Python's ipaddress marks it as such
assert is_private_ip("240.0.0.1") is True
class TestValidateUrlExtended:
"""Additional URL validation tests."""
def test_blocks_metadata_hostname(self):
with pytest.raises(SSRFError):
validate_url("http://metadata")
def test_allows_localhost_with_flag(self):
with patch("application.core.url_validation.resolve_hostname") as mock_resolve:
mock_resolve.return_value = "192.168.1.1"
result = validate_url(
"http://internal.local", allow_localhost=True
)
assert result == "http://internal.local"
def test_blocks_aws_ecs_metadata_ip(self):
with pytest.raises(SSRFError, match="metadata"):
validate_url("http://169.254.170.2")
def test_blocks_aws_ipv6_metadata(self):
with pytest.raises(SSRFError, match="metadata"):
validate_url("http://[fd00:ec2::254]")
def test_blocks_hostname_resolving_to_loopback(self):
with patch("application.core.url_validation.resolve_hostname") as mock_resolve:
mock_resolve.return_value = "127.0.0.1"
with pytest.raises(SSRFError):
validate_url("http://sneaky.example.com")
def test_allows_localhost_ip_with_flag(self):
result = validate_url("http://10.0.0.1", allow_localhost=True)
assert result == "http://10.0.0.1"