hutter-prize-dashboard / tests /test_backend_proxy_auth.py
cmpatino's picture
cmpatino HF Staff
Upload folder using huggingface_hub
424a795 verified
Raw History Blame Contribute Delete
2.2 kB
from __future__ import annotations
import asyncio
import importlib.util
from pathlib import Path
DASHBOARD = Path(__file__).resolve().parents[1]
def _load_app(monkeypatch):
monkeypatch.chdir(DASHBOARD)
monkeypatch.setenv("HF_TOKEN", "sentinel-service-token")
monkeypatch.setenv(
"BACKEND_API_URL",
"https://agent-collaborations-hutter-prize-bucket-sync.hf.space",
)
monkeypatch.setenv(
"BACKEND_AUTH_ORIGIN",
"https://agent-collaborations-hutter-prize-bucket-sync.hf.space",
)
spec = importlib.util.spec_from_file_location("dashboard_proxy_auth_app", DASHBOARD / "app.py")
assert spec and spec.loader
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
def test_backend_proxy_auth_is_sent_only_to_exact_approved_origin(monkeypatch):
module = _load_app(monkeypatch)
calls: list[tuple[str, dict]] = []
class Response:
is_success = True
status_code = 200
text = ""
@staticmethod
def json():
return {"ok": True}
class Client:
def __init__(self, **_kwargs):
pass
async def __aenter__(self):
return self
async def __aexit__(self, *_args):
pass
async def get(self, url, **kwargs):
calls.append((url, kwargs))
return Response()
monkeypatch.setattr(module.httpx, "AsyncClient", Client)
asyncio.run(module._proxy_backend_json("/v1/channels"))
assert calls[-1][1]["headers"] == {
"Authorization": "Bearer sentinel-service-token"
}
assert calls[-1][1]["follow_redirects"] is False
for untrusted in (
"https://third-party.hf.space",
"https://agent-collaborations-hutter-prize-bucket-sync.hf.space:8443",
"https://agent-collaborations-hutter-prize-bucket-sync.hf.space/path",
"http://agent-collaborations-hutter-prize-bucket-sync.hf.space",
):
module.BACKEND_API_URL = untrusted
asyncio.run(module._proxy_backend_json("/v1/channels"))
assert calls[-1][1]["headers"] == {}
assert calls[-1][1]["follow_redirects"] is False