Files
zyt/app/tests/test_local_launcher.py
T
2026-10-23 09:26:49 +08:00

49 lines
2.0 KiB
Python

"""The explicit local launcher must validate the effective endpoint after preferences."""
from __future__ import annotations
import importlib.util
from pathlib import Path
from types import SimpleNamespace
import pytest
spec = importlib.util.spec_from_file_location(
"local_launcher", Path(__file__).parents[1] / "scripts/run_local_workstation.py"
)
launcher = importlib.util.module_from_spec(spec)
spec.loader.exec_module(launcher)
@pytest.fixture(autouse=True)
def loopback_dns(monkeypatch):
monkeypatch.setattr(launcher.socket, "getaddrinfo", lambda *_: [(None, None, None, None, ("127.0.0.1", 443))])
@pytest.mark.parametrize("url", [
"http://local.test", "https://user:password@local.test", "https://local.test?token=private",
"https://local.test#fragment", "https://local.test:invalid", "https://local.test:70000",
])
def test_local_url_rejects_unsecured_or_credential_url(url):
with pytest.raises(ValueError):
launcher.validate_local_url(url)
def test_local_url_rejects_external_dns(monkeypatch):
monkeypatch.setattr(launcher.socket, "getaddrinfo", lambda *_: [(None, None, None, None, ("192.0.2.1", 443))])
with pytest.raises(ValueError):
launcher.validate_local_url("https://local.test")
@pytest.mark.parametrize("effective", [
"http://local.test/adminapi", "https://local.test:8443/adminapi", "https://local.test/other/adminapi",
])
def test_preferences_cannot_change_validated_scheme_port_or_path(effective):
with pytest.raises(ValueError):
launcher.validate_effective_config(SimpleNamespace(api_base_url=effective, verify_ssl=True), "https://local.test")
def test_local_config_keeps_certificate_verification():
launcher.validate_effective_config(SimpleNamespace(api_base_url="https://local.test/adminapi", verify_ssl=True), "https://local.test")
with pytest.raises(ValueError):
launcher.validate_effective_config(SimpleNamespace(api_base_url="https://local.test/adminapi", verify_ssl=False), "https://local.test")