Updated the authentication mechanism to store Codex OAuth tokens in the Hermes auth store located at ~/.hermes/auth.json instead of the previous ~/.codex/auth.json. This change includes refactoring related functions for reading and saving tokens, ensuring better management of authentication states and preventing conflicts between different applications. Adjusted tests to reflect the new storage structure and improved error handling for missing or malformed tokens.
51 lines
2.0 KiB
Python
51 lines
2.0 KiB
Python
"""Tests for detect_external_credentials() -- Phase 2 credential sync."""
|
|
|
|
import json
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
|
|
from hermes_cli.auth import detect_external_credentials
|
|
|
|
|
|
class TestDetectCodexCLI:
|
|
def test_detects_valid_codex_auth(self, tmp_path, monkeypatch):
|
|
codex_dir = tmp_path / ".codex"
|
|
codex_dir.mkdir()
|
|
auth = codex_dir / "auth.json"
|
|
auth.write_text(json.dumps({
|
|
"tokens": {"access_token": "tok-123", "refresh_token": "ref-456"}
|
|
}))
|
|
monkeypatch.setenv("CODEX_HOME", str(codex_dir))
|
|
result = detect_external_credentials()
|
|
codex_hits = [c for c in result if c["provider"] == "openai-codex"]
|
|
assert len(codex_hits) == 1
|
|
assert "Codex CLI" in codex_hits[0]["label"]
|
|
|
|
def test_skips_codex_without_access_token(self, tmp_path, monkeypatch):
|
|
codex_dir = tmp_path / ".codex"
|
|
codex_dir.mkdir()
|
|
(codex_dir / "auth.json").write_text(json.dumps({"tokens": {}}))
|
|
monkeypatch.setenv("CODEX_HOME", str(codex_dir))
|
|
result = detect_external_credentials()
|
|
assert not any(c["provider"] == "openai-codex" for c in result)
|
|
|
|
def test_skips_missing_codex_dir(self, tmp_path, monkeypatch):
|
|
monkeypatch.setenv("CODEX_HOME", str(tmp_path / "nonexistent"))
|
|
result = detect_external_credentials()
|
|
assert not any(c["provider"] == "openai-codex" for c in result)
|
|
|
|
def test_skips_malformed_codex_auth(self, tmp_path, monkeypatch):
|
|
codex_dir = tmp_path / ".codex"
|
|
codex_dir.mkdir()
|
|
(codex_dir / "auth.json").write_text("{bad json")
|
|
monkeypatch.setenv("CODEX_HOME", str(codex_dir))
|
|
result = detect_external_credentials()
|
|
assert not any(c["provider"] == "openai-codex" for c in result)
|
|
|
|
def test_returns_empty_when_nothing_found(self, tmp_path, monkeypatch):
|
|
monkeypatch.setenv("CODEX_HOME", str(tmp_path / "nonexistent"))
|
|
result = detect_external_credentials()
|
|
assert result == []
|