Use httpx for Telegram onboarding worker calls

This commit is contained in:
Shannon Sands 2026-06-06 08:30:51 +10:00 committed by Teknium
parent e3b8b6d32c
commit ba29010902
2 changed files with 71 additions and 17 deletions

View file

@ -1134,6 +1134,54 @@ class TestWebServerEndpoints:
assert data["state"] == "not_configured"
assert "DISCORD_BOT_TOKEN" in data["message"]
def test_telegram_onboarding_worker_request_uses_httpx(self, monkeypatch):
import httpx
import hermes_cli.web_server as ws
calls = {}
def fail_urlopen(*_args, **_kwargs):
raise AssertionError("Telegram onboarding should not use urllib")
class FakeHttpxClient:
def __init__(self, *args, **kwargs):
calls["client_kwargs"] = kwargs
def __enter__(self):
return self
def __exit__(self, *_exc_info):
return False
def request(self, method, url, **kwargs):
calls["request"] = (method, url, kwargs)
return httpx.Response(
201,
json={"ok": True},
request=httpx.Request(method, url),
)
monkeypatch.setenv("TELEGRAM_ONBOARDING_URL", "https://worker.example")
monkeypatch.setattr(ws.urllib.request, "urlopen", fail_urlopen)
monkeypatch.setattr(httpx, "Client", FakeHttpxClient)
payload = ws._telegram_onboarding_request_sync(
"POST",
"/v1/telegram/pairings",
body={"bot_name": "Hermes Agent"},
bearer_token="poll-secret",
)
assert payload == {"ok": True}
method, url, kwargs = calls["request"]
assert method == "POST"
assert url == "https://worker.example/v1/telegram/pairings"
assert kwargs["json"] == {"bot_name": "Hermes Agent"}
assert kwargs["headers"]["Accept"] == "application/json"
assert kwargs["headers"]["Authorization"] == "Bearer poll-secret"
assert kwargs["headers"]["Content-Type"] == "application/json"
assert kwargs["headers"]["User-Agent"].startswith("HermesDashboard/")
def test_telegram_onboarding_start_strips_poll_token(self, monkeypatch):
import hermes_cli.web_server as ws