feat(service): warn when a proxy exit is outside GEOFENCE

This commit is contained in:
imSp4rky
2026-11-30 09:48:54 -07:00
parent f0163439d4
commit ac7d10abb6
7 changed files with 95 additions and 16 deletions
+72
View File
@@ -1,6 +1,14 @@
"""Region checks for GEOFENCE and GEOBLOCK on the Service base class."""
from types import SimpleNamespace
import pytest
from tests.core.test_service_proxy_env import PROXY, Dummy
from unshackle.core import service as service_mod
from unshackle.core.api.session_log import SessionLogBuffer, capture_service_logs
from unshackle.core.service import Service
from unshackle.core.utils import run_proxy
class Blocked(Service):
@@ -40,3 +48,67 @@ def test_geoblock_wins_over_geofence():
def test_unknown_region_is_allowed():
assert Blocked.is_region_allowed("")
assert Blocked.is_region_allowed(None)
def warnings_for(
monkeypatch: pytest.MonkeyPatch,
exit_region: str | None,
class_fence: tuple[str, ...] | None = (),
own_fence: tuple[str, ...] | None = None,
block: tuple[str, ...] = (),
) -> list[str]:
"""Build a service through a proxy with the given exit: the warnings a remote session gets from it."""
class EXAMPLE(Dummy):
GEOFENCE = class_fence
GEOBLOCK = block
def __init__(self, ctx): # type: ignore[no-untyped-def]
if own_fence is not None:
self.GEOFENCE = own_fence
super().__init__(ctx)
monkeypatch.setattr(
service_mod, "verify_proxy_exit", lambda session: {"country": exit_region} if exit_region else {}
)
monkeypatch.setattr(run_proxy, "watched", {})
monkeypatch.setattr(run_proxy, "proxy_hosts", set())
ctx = SimpleNamespace(
obj=SimpleNamespace(config={}, proxy_providers=[]), parent=SimpleNamespace(params={"proxy": PROXY})
)
buffer = SessionLogBuffer()
with capture_service_logs("EXAMPLE", buffer):
service = EXAMPLE(ctx)
assert service.current_region == exit_region
return [r["message"] for r in buffer.since(0) if r["level"] != "INFO"]
def test_proxy_exit_outside_the_geofence_logs_one_warning(monkeypatch):
assert warnings_for(monkeypatch, "hu", ("fr", "PL")) == [
"Proxy exit region HU is outside the regions of the service (FR, PL)"
]
@pytest.mark.parametrize(
("exit_region", "fence"),
[("Fr", ("fR", "PL")), ("hu", ()), ("hu", None), (None, ("fr",))],
ids=["inside-any-case", "no-fence", "none-fence", "unknown-exit"],
)
def test_no_warning_when_the_exit_is_allowed_or_unknown(monkeypatch, exit_region, fence):
assert warnings_for(monkeypatch, exit_region, fence) == []
def test_an_instance_geofence_decides(monkeypatch):
assert warnings_for(monkeypatch, "hu", ("hu", "fr"), own_fence=("fr",)) == [
"Proxy exit region HU is outside the regions of the service (FR)"
]
def test_an_empty_instance_geofence_gives_no_warning(monkeypatch):
assert warnings_for(monkeypatch, "hu", ("fr",), own_fence=()) == []
def test_a_blocked_region_is_not_listed_as_supported(monkeypatch):
assert warnings_for(monkeypatch, "hu", ("us", "gb"), block=("GB",)) == [
"Proxy exit region HU is outside the regions of the service (US)"
]