hermes-agent/tests/hermes_cli/test_xai_oauth_writethrough.py
teknium1 2632229bcf fix(auth): named profiles read the root auth.json again (revert #111724)
Reverts 93889b770d ("named profiles no longer inherit the root
profile's auth.json"). After the Desktop update every bot profile that had
relied on the root OpenAI Codex login failed with "No Codex credentials
stored. Run `hermes -p <bot> auth add openai-codex --type oauth`", and users
had to re-run the device-code flow once per bot (5-6 times in the field
report). Sharing one grant across profiles is the intended design: OAuth
refresh tokens are single-use, so ONE grant lives at the root, profiles
resolve it read-only, and a refresh under a profile writes the rotated chain
back to root (Codex / xAI write-through, borrowed-row pool bookkeeping,
forked-grant heal) — never a per-profile copy.

Restored: `_global_auth_file_path` / `_load_global_auth_store` fallback in
`_load_provider_state*` / `read_credential_pool` / `_provider_state_transaction`,
Codex + xAI root write-through, `credential_pool` borrowed-root persistence,
`heal_forked_single_use_oauth_grants`, `share_auth` on profile creation
(Desktop create dialog checkbox), and the docs. `profile_credential_audit.py`
(the `hermes update` "profiles without a provider" notice) is removed with it.

Kept from after #111724: `_save_codex_tokens(set_active=...)` for image gen,
the plugin-auth `status` dispatch and the external-login notice in
`hermes auth list`, and the registry-derived env-var hint in agent_init.
2026-09-21 09:55:40 -07:00

90 lines
3.5 KiB
Python

"""Regression tests for xAI OAuth refresh write-through to the global root.
Companion to ``test_xai_oauth_profile_auth.py``. That file covers the READ
fallback (profile -> credential pool -> global root). These cover the WRITE
side: when a profile that has no own ``providers.xai-oauth`` block refreshes
the (rotating) grant it resolved from the root fallback, the rotated tokens
must be written back to the global root too. Otherwise root keeps a revoked
refresh token and every other profile reading root's stale grant dies with
``invalid_grant`` once its access token expires (issue #43589).
The tests drive the real ``_save_xai_oauth_tokens`` against real on-disk auth
stores (profile + root under ``tmp_path``) rather than mocking the save
boundary, so they exercise the actual atomic write path.
"""
import json
import pytest
from hermes_cli import auth
def _write_store(path, store):
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(store), encoding="utf-8")
def _read_store(path):
return json.loads(path.read_text(encoding="utf-8"))
@pytest.fixture
def profile_and_root(tmp_path, monkeypatch):
"""Wire a profile auth store + a distinct global-root auth store on disk.
Returns (profile_path, root_path). The pytest seat belt in
``_write_through_xai_oauth_to_global_root`` only refuses the *real* user's
``$HOME/.hermes/auth.json``; a tmp_path root is allowed, so we point HOME
away from the tmp root to keep the guard from tripping on these fixtures.
"""
profile_path = tmp_path / "profiles" / "work" / "auth.json"
root_path = tmp_path / "root" / "auth.json"
monkeypatch.setattr(auth, "_auth_file_path", lambda: profile_path)
monkeypatch.setattr(auth, "_global_auth_file_path", lambda: root_path)
# Keep the pytest write seat belt from matching our tmp root.
monkeypatch.setenv("HOME", str(tmp_path / "not-the-root"))
return profile_path, root_path
def test_write_through_is_noop_in_classic_mode(tmp_path, monkeypatch):
"""Classic mode (profile == root) already saves to root; no double write."""
profile_path = tmp_path / "auth.json"
monkeypatch.setattr(auth, "_auth_file_path", lambda: profile_path)
# Classic mode: _global_auth_file_path returns None.
monkeypatch.setattr(auth, "_global_auth_file_path", lambda: None)
_write_store(profile_path, {"version": 1, "providers": {}})
# Should not raise and should persist to the single store.
auth._save_xai_oauth_tokens(
{"access_token": "a", "refresh_token": "r"}
)
store = _read_store(profile_path)
assert store["providers"]["xai-oauth"]["tokens"]["refresh_token"] == "r"
def test_write_through_failure_does_not_break_profile_save(profile_and_root, monkeypatch):
"""A failed root write-through must not break the profile's own save."""
profile_path, root_path = profile_and_root
_write_store(profile_path, {"version": 1, "providers": {}})
_write_store(root_path, {"version": 1, "providers": {}})
# Make the root write blow up; the profile save must still succeed.
real_save = auth._save_auth_store
def _exploding_save(store, target_path=None):
if target_path is not None and target_path == root_path:
raise OSError("simulated root write failure")
return real_save(store, target_path)
monkeypatch.setattr(auth, "_save_auth_store", _exploding_save)
auth._save_xai_oauth_tokens({"access_token": "a", "refresh_token": "r"})
profile = _read_store(profile_path)
assert profile["providers"]["xai-oauth"]["tokens"]["refresh_token"] == "r"