Files
nanobot/tests/webui/test_copilot_device_login.py
T
Xubin Renandchengyongru a44f28e656 fix(webui): complete Copilot device sign-in in the browser
Reuse the device-flow adapter from #5498, expose its prompt in the existing OAuth dialog, and keep credential persistence scoped to uncancelled completion.

Co-authored-by: chengyongru <chengyongru.ai@gmail.com>
2026-09-21 00:13:46 +08:00

47 lines
2.0 KiB
Python

"""The WebUI exposes only user-facing device data and owns cancellation."""
import threading
import pytest
from nanobot.config.schema import Config
from nanobot.webui.settings_models import complete_oauth_provider, login_oauth_provider
from nanobot.webui.settings_services import WebUIOAuthFlowRegistry
def test_copilot_prompt_poll_and_owned_cancellation(monkeypatch):
stopped = threading.Event()
def login(*, on_authorization, cancelled, persist, **_kwargs):
assert persist is False
on_authorization("https://github.com/login/device", "ABCD-EFGH", 60)
cancelled.wait(2)
stopped.set()
raise RuntimeError("cancelled")
monkeypatch.setattr("nanobot.providers.github_copilot_oauth.login_github_copilot", login)
registry = WebUIOAuthFlowRegistry()
payload = login_oauth_provider(
Config(), {"provider": ["github_copilot"]}, oauth_flows=registry,
config_path=None, settings_payload=lambda **_: {},
)
assert set(payload) == {
"status", "provider", "flow_id", "authorization_url", "user_code",
"expires_in", "completion_input",
}
assert payload["user_code"] == "ABCD-EFGH"
assert payload["completion_input"] == "device_code"
query = {"provider": ["github_copilot"], "flow_id": [payload["flow_id"]]}
kwargs = dict(oauth_flows=registry, config_path=None, settings_payload=lambda **_: {})
try:
assert complete_oauth_provider(query, **kwargs)["status"] == "pending"
with pytest.raises(ValueError, match="expired"):
complete_oauth_provider({**query, "provider": ["openai_codex"], "cancel": ["true"]}, **kwargs)
assert registry.get("github_copilot", payload["flow_id"]) is not None
result = complete_oauth_provider({**query, "cancel": ["true"]}, **kwargs)
assert result["status"] == "cancelled"
assert registry.get("github_copilot", payload["flow_id"]) is None
assert stopped.wait(1)
finally:
registry.clear("github_copilot")