refactor(api): decide the proxy of an API request in one api_proxy module

This commit is contained in:
imSp4rky
2026-12-02 20:04:07 -07:00
parent d23324e27b
commit fef6898823
8 changed files with 617 additions and 468 deletions
+9 -13
View File
@@ -131,6 +131,15 @@ def test_remote_server_rejects(remote_config, data, code):
assert e.value.error_code == code
@pytest.mark.parametrize(("entry", "proxy"), [({"server_proxy": True}, {"proxy": "ca"}), ({}, {})])
def test_a_relayed_request_gets_the_geofence_proxy_only_under_server_proxy(remote_config, monkeypatch, entry, proxy):
fenced = {**INFO, "geofence": ["ca"]}
monkeypatch.setattr(remote_service, "remote_service_list", lambda name: [fenced])
monkeypatch.setattr(handlers, "request_policy", lambda request=None: KeyPolicy(entry))
data = {"remote": "srv", "service": "example"}
assert asyncio.run(handlers.relay_target(data)) == ("srv", fenced, {**data, "service": "EXAMPLE", **proxy})
def test_remote_server_applies_the_allowlist_and_reports_an_unreachable_server(remote_config, monkeypatch):
monkeypatch.setattr(handlers, "request_policy", lambda request=None: KeyPolicy({"services": ["OTHER"]}))
with pytest.raises(APIError) as e:
@@ -198,19 +207,6 @@ def test_an_unreachable_server_list_logs_one_warning(monkeypatch, caplog):
assert [r.levelname for r in caplog.records if r.levelname in ("WARNING", "ERROR")] == ["WARNING"]
def test_a_geofenced_remote_service_gets_its_first_allowed_region_as_proxy(monkeypatch):
monkeypatch.setattr(handlers, "request_policy", lambda request=None: KeyPolicy({"server_proxy": True}))
fenced = {"tag": "EXAMPLE", "geofence": ["US", "ca"], "geoblock": ["us"]}
assert handlers.geofence_proxy({}, {"tag": "EXAMPLE", "geofence": ["US", "ca"]}) == {"proxy": "us"}
assert handlers.geofence_proxy({}, fenced) == {"proxy": "ca"}
assert handlers.geofence_proxy({"proxy": "gb"}, fenced) == {}
assert handlers.geofence_proxy({"no_proxy": True}, fenced) == {}
assert handlers.geofence_proxy({}, INFO) == {}
monkeypatch.setattr(handlers, "request_policy", lambda request=None: KeyPolicy({}))
assert handlers.geofence_proxy({}, fenced) == {}
def test_a_quiet_remote_login_shows_its_last_server_lines_as_a_notice(monkeypatch):
import logging