fix(webui): surface OAuth model catalog authorization failures

This commit is contained in:
Xubin Ren committed 2026-09-21 00:13:46 +08:00
1 parent a478c96bde
commit 024fe8f636
26 files changed
+585 -34

No files matched your search

+83
View File
@@ -2120,6 +2120,89 @@ def test_provider_models_payload_returns_online_openai_codex_models(
}
@pytest.mark.parametrize("source", ["stale", "fallback"])
@pytest.mark.parametrize("error_kind", ["auth_required", "unavailable"])
def test_provider_models_payload_exposes_catalog_failure_with_usable_models(
monkeypatch, source, error_kind,
):
monkeypatch.setattr(
"nanobot.webui.settings_models.get_oauth_model_catalog",
lambda *_args, **_kwargs: OAuthModelCatalogSnapshot(
models=(ProviderModelSpec(id="openai-codex/offline-model"),),
source=source, fetched_at=123, error_kind=error_kind,
),
)
payload = provider_models_payload({"provider": ["openai_codex"]})
assert payload["status"] == "available"
assert payload["source"] == source
assert payload["error_kind"] == error_kind
assert payload["models"][0]["id"] == "openai-codex/offline-model"
def test_copilot_explicit_login_does_not_reuse_revoked_credentials(monkeypatch, oauth_flows):
from unittest.mock import Mock
login = Mock(return_value=SimpleNamespace(access="new-token"))
invalidate = Mock()
monkeypatch.setattr(
"nanobot.providers.github_copilot_provider.get_github_copilot_login_status",
lambda: SimpleNamespace(access="revoked-token"),
)
monkeypatch.setattr("nanobot.providers.github_copilot_provider.login_github_copilot", login)
monkeypatch.setattr("nanobot.webui.settings_models.invalidate_oauth_model_catalog", invalidate)
monkeypatch.setattr("nanobot.webui.settings_api.settings_payload", lambda **_: {"ready": True})
assert login_oauth_provider({"provider": ["github-copilot"]}, oauth_flows=oauth_flows) == {"ready": True}
login.assert_called_once()
invalidate.assert_called_once_with("github_copilot")
@pytest.mark.parametrize("provider", ["openai_codex", "xai_grok"])
def test_oauth_completion_clears_same_account_catalog_failure(monkeypatch, oauth_flows, provider):
from nanobot.providers.oauth_model_catalog import OAuthModelCatalog
signed_in = [False]
def fetch(_proxy):
if not signed_in[0]:
request = httpx.Request("GET", "https://example.com/models")
raise httpx.HTTPStatusError("revoked", request=request, response=httpx.Response(401))
return (ProviderModelSpec(id="provider/new-model"),)
catalog = OAuthModelCatalog(fallback_models=(), fetch=fetch)
module = f"nanobot.providers.{provider}_provider"
monkeypatch.setattr(f"{module}._{provider.upper()}_MODEL_CATALOG", catalog)
monkeypatch.setattr(
"nanobot.webui.settings_models.get_oauth_model_catalog",
lambda *_args, **_kwargs: catalog.get(cache_key="same-account"),
)
monkeypatch.setattr("nanobot.webui.settings_api.settings_payload", lambda **_: {"ready": True})
class Flow:
expired = False
def cancel(self):
pass
def complete(*_args):
signed_in[0] = True
return SimpleNamespace(access="fixture")
owner = "openai_codex_oauth" if provider == "openai_codex" else "xai_oauth"
completion = "complete_openai_codex_oauth_login" if provider == "openai_codex" else "complete_xai_oauth_login"
monkeypatch.setattr(f"nanobot.providers.{owner}.{completion}", complete)
oauth_flows.register(provider, "flow-test", Flow())
before = provider_models_payload({"provider": [provider]})
assert before["error_kind"] == "auth_required"
assert complete_oauth_provider(
{"provider": [provider], "flow_id": ["flow-test"]},
"synthetic-callback", oauth_flows=oauth_flows,
) == {"ready": True}
after = provider_models_payload({"provider": [provider]})
assert after["source"] == "remote"
assert after["error_kind"] is None
assert after["models"][0]["id"] == "provider/new-model"
def test_provider_models_payload_returns_online_github_copilot_models(
monkeypatch: pytest.MonkeyPatch,
) -> None: