mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-31 19:16:29 +00:00
Second, deeper pass over tools/gateway/hermes_cli plus first pass over the trees wave 1 missed (acp, acp_adapter, skills, computer_use, docker, dashboard, conformance, monitoring, secret_sources, hermes_state, providers). Same rubric as wave 1 (AGENTS.md test policy); security, alternation/caching invariants, issue-number regressions, and E2E kept. Real test-quality fixes found and rooted out along the way: - tests/tools/test_command_guards.py made real auxiliary-LLM HTTPS calls (DEFAULT_CONFIG smart-approval leaked in) — pinned approval mode=manual via autouse fixture: 17.4s → 0.4s. - test_model_switch_custom_providers.py / test_user_providers_model_switch.py silently probed live provider catalogs (~2s/test) — stubbed cached_provider_model_ids/provider_model_ids/fetch_api_models. - test_telegram_noise_filter.py: 15-platform copy-paste matrix over shared gateway.run logic → 3 representative platforms (55s → 3.9s). - test_gateway_shutdown.py: stop()'s 5s interrupt-deadline loop spun on MagicMock agents — interrupt.side_effect now clears _running_agents (22s → 1.0s). - test_gateway_inactivity_timeout.py poll-harness timings shrunk 3-5x (24s → 1.1s); test_mcp_stability.py backoff/SIGTERM-grace sleeps patched (15.4s → 2.5s); test_async_delegation.py negative-drain wait 5s → 0.5s. - test_telegram_init_deadline.py: loop-block margin restored to 1.0s with rationale comment — the watchdog-dump assertion needs the loop blocked well past deadline+grace under parallel load (flaked once in the 40-worker verification run at a 0.2s margin). Verification: full hermetic suite via scripts/run_tests.sh — 2,438 files, 21,718 tests passed, 0 failed, 293.9s wall. Suite totals vs original baseline: 46,820 → 19,757 test functions (−57.8%), wall 583.5s → 293.9s (−50%), subprocess CPU 13,564s → 11,623s.
77 lines
3 KiB
Python
77 lines
3 KiB
Python
"""Unit tests for the generic-OIDC / Nous-Portal caller-identity token resolver.
|
|
|
|
Covers gateway.relay._resolve_relay_identity_token() — the canonical resolver
|
|
shared by the runtime self-provision path and the `hermes gateway enroll` CLI.
|
|
|
|
Two modes:
|
|
1. Generic OAuth2 client_credentials when gateway.idp.token_url (or
|
|
GATEWAY_RELAY_IDP_TOKEN_URL) is configured (air-gapped / self-hosted-IdP).
|
|
2. Nous Portal (resolve_nous_access_token) otherwise — the default.
|
|
|
|
The HTTP POST and the Nous resolver are monkeypatched; these prove the mode
|
|
SELECTION, the client_credentials request shape, and the fail-closed paths.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import io
|
|
import json
|
|
|
|
import pytest
|
|
|
|
import gateway.relay as relay
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _clean_env(monkeypatch):
|
|
for k in (
|
|
"GATEWAY_RELAY_IDP_TOKEN_URL",
|
|
"GATEWAY_RELAY_IDP_CLIENT_ID",
|
|
"GATEWAY_RELAY_IDP_CLIENT_SECRET",
|
|
"GATEWAY_RELAY_IDP_SCOPE",
|
|
):
|
|
monkeypatch.delenv(k, raising=False)
|
|
# Never read config.yaml off disk by default.
|
|
monkeypatch.setattr("gateway.run._load_gateway_config", lambda: {}, raising=False)
|
|
|
|
|
|
def test_client_credentials_via_env(monkeypatch):
|
|
monkeypatch.setenv("GATEWAY_RELAY_IDP_TOKEN_URL", "https://idp.test/token")
|
|
monkeypatch.setenv("GATEWAY_RELAY_IDP_CLIENT_ID", "agent-client")
|
|
monkeypatch.setenv("GATEWAY_RELAY_IDP_CLIENT_SECRET", "shh")
|
|
monkeypatch.setenv("GATEWAY_RELAY_IDP_SCOPE", "connector.provision")
|
|
|
|
captured = {}
|
|
|
|
def fake_urlopen(req, timeout=None):
|
|
captured["url"] = req.full_url
|
|
captured["method"] = req.get_method()
|
|
captured["body"] = req.data.decode()
|
|
captured["headers"] = {k.lower(): v for k, v in req.headers.items()}
|
|
return io.BytesIO(json.dumps({"access_token": "idp-workload-token"}).encode())
|
|
|
|
monkeypatch.setattr("urllib.request.urlopen", fake_urlopen)
|
|
|
|
token = relay._resolve_relay_identity_token()
|
|
assert token == "idp-workload-token"
|
|
assert captured["url"] == "https://idp.test/token"
|
|
assert captured["method"] == "POST"
|
|
# client_credentials grant, form-encoded, with all fields.
|
|
assert "grant_type=client_credentials" in captured["body"]
|
|
assert "client_id=agent-client" in captured["body"]
|
|
assert "client_secret=shh" in captured["body"]
|
|
assert "scope=connector.provision" in captured["body"]
|
|
assert captured["headers"]["content-type"] == "application/x-www-form-urlencoded"
|
|
|
|
|
|
def test_raises_when_no_access_token_in_response(monkeypatch):
|
|
monkeypatch.setenv("GATEWAY_RELAY_IDP_TOKEN_URL", "https://idp.test/token")
|
|
monkeypatch.setenv("GATEWAY_RELAY_IDP_CLIENT_ID", "c")
|
|
monkeypatch.setenv("GATEWAY_RELAY_IDP_CLIENT_SECRET", "s")
|
|
|
|
def fake_urlopen(req, timeout=None):
|
|
return io.BytesIO(json.dumps({"token_type": "Bearer"}).encode())
|
|
|
|
monkeypatch.setattr("urllib.request.urlopen", fake_urlopen)
|
|
with pytest.raises(RuntimeError, match="no access_token"):
|
|
relay._resolve_relay_identity_token()
|