hermes-agent/tests/tools/test_mcp_dashboard_oauth.py
Teknium 39975613b1
test: prune wave 2 + speed fixes — 28,106 → 19,757 test functions, suite wall 315s → 294s
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.
2026-07-29 13:39:40 -07:00

113 lines
3.6 KiB
Python

"""Hosted-dashboard bridge for MCP OAuth browser callbacks."""
import asyncio
import threading
import pytest
def test_dashboard_flow_exposes_authorization_url_and_accepts_callback():
from tools.mcp_dashboard_oauth import DashboardOAuthFlow
flow = DashboardOAuthFlow(
flow_id="flow-1",
server_name="reports",
profile=None,
hermes_home="/tmp/hermes-test",
redirect_uri="https://agent.example/mcp/oauth/callback/flow-1",
)
asyncio.run(flow.publish_authorization_url("https://idp.example/authorize?state=s1"))
assert flow.snapshot() == {
"flow_id": "flow-1",
"server_name": "reports",
"status": "authorization_required",
"authorization_url": "https://idp.example/authorize?state=s1",
"error": None,
}
flow.deliver_callback(code="code-1", state="s1", error=None)
assert asyncio.run(flow.wait_for_callback()) == ("code-1", "s1")
def test_dashboard_flow_accepts_only_one_concurrent_callback():
from tools.mcp_dashboard_oauth import DashboardOAuthFlow
flow = DashboardOAuthFlow(
flow_id="flow-race",
server_name="reports",
profile=None,
hermes_home="/tmp/hermes-test",
redirect_uri="https://agent.example/mcp/oauth/callback/flow-race",
)
asyncio.run(flow.publish_authorization_url("https://idp.example/authorize?state=state"))
start = threading.Barrier(3)
outcomes: list[str] = []
def deliver(code: str) -> None:
start.wait()
try:
flow.deliver_callback(code=code, state="state", error=None)
outcomes.append("accepted")
except ValueError:
outcomes.append("rejected")
workers = [threading.Thread(target=deliver, args=(code,)) for code in ("one", "two")]
for worker in workers:
worker.start()
start.wait()
for worker in workers:
worker.join()
assert sorted(outcomes) == ["accepted", "rejected"]
def test_mcp_oauth_helpers_use_dashboard_flow_without_loopback_port():
from tools.mcp_dashboard_oauth import DashboardOAuthFlow, dashboard_oauth_flow
from tools.mcp_oauth import (
HermesTokenStorage,
_build_client_metadata,
_configure_callback_port,
_make_callback_waiter,
_make_redirect_handler,
)
flow = DashboardOAuthFlow(
flow_id="flow-4",
server_name="reports",
profile=None,
hermes_home="/tmp/hermes-test",
redirect_uri="https://agent.example/mcp/oauth/callback/flow-4",
)
cfg = {}
with dashboard_oauth_flow(flow):
assert _configure_callback_port(cfg, HermesTokenStorage("reports")) == 0
metadata = _build_client_metadata(cfg)
assert str(metadata.redirect_uris[0]) == flow.redirect_uri
asyncio.run(
_make_redirect_handler(0)(
"https://idp.example/authorize?state=state-4"
)
)
flow.deliver_callback(code="code-4", state="state-4", error=None)
assert asyncio.run(_make_callback_waiter(0)()) == ("code-4", "state-4")
assert flow.authorization_url == "https://idp.example/authorize?state=state-4"
def test_failed_reauth_rollback_preserves_newer_oauth_state(tmp_path, monkeypatch):
from tools.mcp_oauth import HermesTokenStorage
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
storage = HermesTokenStorage("reports")
storage._tokens_path().parent.mkdir(parents=True)
storage._tokens_path().write_text("OLD")
backup = storage.snapshot()
storage.remove()
storage._tokens_path().write_text("FRESH")
storage.restore(backup, only_if_absent=True)
assert storage._tokens_path().read_text() == "FRESH"