"""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): GEOBLOCK = ("GB",) class Fenced(Service): GEOFENCE = ("us",) class Both(Service): GEOFENCE = ("us", "gb") GEOBLOCK = ("GB",) def test_no_restrictions_allows_everything(): assert Service.is_region_allowed("xx") assert Service.is_region_allowed(None) def test_geoblock_refuses_listed_region_any_case(): assert Blocked.is_region_allowed("de") assert not Blocked.is_region_allowed("gb") assert not Blocked.is_region_allowed("GB") def test_geofence_allows_only_listed_regions(): assert Fenced.is_region_allowed("US") assert not Fenced.is_region_allowed("gb") def test_geoblock_wins_over_geofence(): assert Both.is_region_allowed("us") assert not Both.is_region_allowed("gb") 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)" ]