Files
zeekay 1d0478e0c5 Resolve every credential through hanzo-login
A stale sk- key in ~/.hanzo/config.json beat the person's login
because the file was read before the hanzo CLI was asked. Every Python
tool had its own copy of that order, and research ran whatever `hanzo`
was first on PATH, which on Debian is the hanzo-ide GUI.

hanzo-login (stdlib only) is the one resolver: HANZO_API_KEY, then the
CLI's login at ~/.local/bin/hanzo (`auth token`, `--as` for another
identity, kept 300 s per identity), then config.json apiKey last. An
identity named per call or with use() always asks the login and never
falls through to another account. Identities are validated before
they reach argv. The CLI owns its store; nothing here reads it.

HanzoCloud resolves per request, so a refreshed or switched login is
followed, takes identity and org (X-Org-Id) per call, and sends
hanzo-tools/<version>. hanzo-research and HanzoSession call the same
resolver instead of their own.

The hanzo router answers service="auth" locally: whoami (source,
identity, email, name, org, orgs, a key's prefix only), list, use (this
process) and switch (the machine). Every call takes `as` and `org`.
params is now declared as an object or its string: FastMCP parsed the
JSON string into a dict and refused it against a str-only schema.

Claude-Session: https://claude.ai/code/session_015XYKLix9PSyn6Jw2HWKprk
2026-11-08 16:06:17 -08:00

44 lines
1.4 KiB
Python

from __future__ import annotations
import os
import sys
from typing import Iterator
import pytest
sys.path.insert(0, os.path.dirname(__file__))
import hanzo_login as login # noqa: E402
from fake import Server, jwt # noqa: E402
from hanzo_research import transport # noqa: E402
HANZO = jwt(
{
"sub": "u-1",
"owner": "hanzo",
"orgs": [{"org": "hanzo", "role": "admin"}, {"org": "lux", "role": "admin"}],
"exp": 4102444800,
}
)
SUPER = jwt({"sub": "u-0", "owner": "admin", "orgs": [{"org": "admin", "role": "admin"}], "exp": 4102444800})
ATLAS = jwt({"sub": "u-2", "owner": "hanzo", "orgs": [{"org": "hanzo"}], "project": "atlas", "exp": 4102444800})
@pytest.fixture(autouse=True)
def quiet(monkeypatch: pytest.MonkeyPatch, tmp_path) -> None:
"""No test reads the caller's own credential (key, config, hanzo CLI) or waits out a real backoff."""
monkeypatch.setenv("HOME", str(tmp_path))
monkeypatch.delenv("HANZO_API_KEY", raising=False)
monkeypatch.delenv("HANZO_KEY", raising=False)
monkeypatch.delenv("RESEARCH_BASE", raising=False)
monkeypatch.delenv("RESEARCH_PROJECT", raising=False)
monkeypatch.setattr(transport, "WAITS", (0, 0, 0))
monkeypatch.setattr(login, "cli", lambda: tmp_path / "absent")
login.forget()
@pytest.fixture
def server() -> Iterator[Server]:
with Server() as s:
yield s