"""The settings picker must use the same owner-private discovery as the CLI.""" from __future__ import annotations from types import SimpleNamespace from unittest.mock import AsyncMock from fastapi import FastAPI from fastapi.testclient import TestClient import httpx import pytest from deeptutor.api.routers import settings from deeptutor.multi_user.models import CurrentUser, UserScope from deeptutor.multi_user.paths import user_context from deeptutor.services import github_copilot_auth as auth from deeptutor.services.github_copilot_storage import GitHubToken from deeptutor.services.llm import cloud_provider, local_provider from deeptutor.services.partners.scope import PARTNER_USER_PREFIX ENDPOINT = "/api/settings/fetch-models" LOGIN_COMMAND = "deeptutor provider login github-copilot" @pytest.fixture def client(tmp_path, monkeypatch): monkeypatch.setenv("DEEPTUTOR_HOME", str(tmp_path / "home")) app = FastAPI() app.include_router(settings.router, prefix="/api/settings") with TestClient(app) as test_client: yield test_client @pytest.fixture def copilot_http(monkeypatch): """Only the HTTP transport is faked; storage, exchange and filtering are real.""" state = SimpleNamespace( calls=[], rows=[ {"id": "claude-live", "supported_endpoints": ["/responses"]}, {"id": "gpt-live", "supported_endpoints": ["/chat/completions"]}, {"id": "legacy-live"}, {"id": "hidden", "model_picker_enabled": False}, {"id": "disabled", "policy": {"state": "disabled"}}, {"id": "unsupported", "supported_endpoints": ["/embeddings"]}, ], failure=None, ) def handle(request): state.calls.append(request) if state.failure: raise state.failure if request.url == auth.DEFAULT_COPILOT_TOKEN_URL: assert request.headers["Authorization"].startswith("token fixture-") return httpx.Response( 200, json={ "token": "fixture-short-lived", "expires_at": 2_000_000_000, "endpoints": {"api": "https://tenant.example/copilot"}, }, ) assert str(request.url) == "https://tenant.example/copilot/models" assert request.headers["Authorization"] == "Bearer fixture-short-lived" return httpx.Response(200, json={"data": state.rows}) real_client = httpx.AsyncClient monkeypatch.setattr( auth.httpx, "AsyncClient", lambda **kwargs: real_client(transport=httpx.MockTransport(handle), **kwargs), ) monkeypatch.delenv("DEEPTUTOR_COPILOT_TOKEN_URL", raising=False) monkeypatch.setattr( cloud_provider, "fetch_models", AsyncMock(side_effect=AssertionError("generic cloud path")) ) monkeypatch.setattr( local_provider, "fetch_models", AsyncMock(side_effect=AssertionError("generic local path")) ) return state def _login(): auth.get_github_copilot_storage().save( GitHubToken("fixture-github", 2_000_000_000_000, "fixture-account") ) @pytest.mark.parametrize("binding", ["github_copilot", "github-copilot", " GitHub_Copilot "]) @pytest.mark.parametrize("api_key", [None, "", "***"]) def test_picker_returns_live_selectable_models(client, copilot_http, monkeypatch, binding, api_key): _login() monkeypatch.setattr( settings, "get_model_catalog_service", lambda: pytest.fail("Copilot must not load API keys from the shared catalog"), ) response = client.post( ENDPOINT, json={ "binding": binding, "base_url": "", "api_key": api_key, "profile_id": "copilot-profile", "service": "llm", }, ) assert response.status_code == 200 ids = ["github-copilot/claude-live", "github-copilot/gpt-live", "github-copilot/legacy-live"] assert response.json() == {"models": [{"id": model, "name": model} for model in ids]} assert len(copilot_http.calls) == 2 assert "fixture-" not in response.text def test_task_picker_ignores_draft_endpoint_and_key(client, copilot_http): _login() response = client.post( ENDPOINT, json={ "binding": "github_copilot", "base_url": "http://localhost:1234/v1", "api_key": "untrusted-draft-key", "api_format": "anthropic", "service": "task", }, ) assert response.status_code == 200 assert len(copilot_http.calls) == 2 assert response.json()["models"][0]["id"] == "github-copilot/claude-live" def test_missing_login_is_actionable_not_empty_success(client, copilot_http): response = client.post(ENDPOINT, json={"binding": "github_copilot"}) assert response.status_code == 502 assert LOGIN_COMMAND in response.json()["detail"] assert "models" not in response.json() assert copilot_http.calls == [] @pytest.mark.parametrize("failure_kind", ["unauthorized", "forbidden", "timeout", "invalid"]) def test_provider_errors_are_safe_and_actionable(client, copilot_http, caplog, failure_kind): _login() secret = "fixture-sensitive-error" request = httpx.Request("GET", f"https://tenant.example/?token={secret}") if failure_kind in {"unauthorized", "forbidden"}: upstream = httpx.Response( 401 if failure_kind == "unauthorized" else 403, request=request, json={"error": secret}, ) failure = httpx.HTTPStatusError(secret, request=request, response=upstream) elif failure_kind == "timeout": failure = httpx.ReadTimeout(secret, request=request) else: failure = ValueError(secret) copilot_http.failure = failure response = client.post(ENDPOINT, json={"binding": "github_copilot"}) assert response.status_code == 502 assert LOGIN_COMMAND in response.json()["detail"] assert secret not in response.text assert secret not in caplog.text def test_catalog_failure_is_not_silently_swallowed(client, copilot_http, monkeypatch): _login() monkeypatch.setattr( auth, "fetch_github_copilot_models", AsyncMock(side_effect=RuntimeError("fixture-error")) ) response = client.post(ENDPOINT, json={"binding": "github_copilot"}) assert response.status_code == 502 assert LOGIN_COMMAND in response.json()["detail"] assert "fixture-error" not in response.text def test_owner_and_home_scope_are_preserved(client, copilot_http, tmp_path, monkeypatch): _login() other = CurrentUser( id="u_other", username="other", role="admin", scope=UserScope(kind="user", user_id="u_other", root=tmp_path / "other"), ) with user_context(other): response = client.post(ENDPOINT, json={"binding": "github_copilot"}) assert response.status_code == 502 assert copilot_http.calls == [] auth.get_github_copilot_storage().save( GitHubToken("fixture-other", 2_000_000_000_000, "other") ) response = client.post(ENDPOINT, json={"binding": "github_copilot"}) assert response.status_code == 200 assert copilot_http.calls[0].headers["Authorization"] == "token fixture-other" copilot_http.calls.clear() assert client.post(ENDPOINT, json={"binding": "github_copilot"}).status_code == 200 assert copilot_http.calls[0].headers["Authorization"] == "token fixture-github" monkeypatch.setenv("DEEPTUTOR_HOME", str(tmp_path / "different-home")) copilot_http.calls.clear() assert client.post(ENDPOINT, json={"binding": "github_copilot"}).status_code == 502 assert copilot_http.calls == [] @pytest.mark.parametrize("user_id", ["u_reader", f"{PARTNER_USER_PREFIX}ada"]) def test_settings_permission_gate_still_applies(client, copilot_http, tmp_path, user_id): user = CurrentUser( id=user_id, username=user_id, role="user", scope=UserScope(kind="user", user_id=user_id, root=tmp_path / "reader"), ) with user_context(user): response = client.post(ENDPOINT, json={"binding": "github_copilot"}) assert response.status_code == 403 assert copilot_http.calls == [] def test_standard_provider_keeps_key_endpoint_format_and_contract(client, monkeypatch): fetch = AsyncMock(return_value=["gpt-standard"]) monkeypatch.setattr(cloud_provider, "fetch_models", fetch) response = client.post( ENDPOINT, json={ "binding": "OpenAI", "base_url": "https://standard.example/v1", "api_key": "fixture-standard", "api_format": "responses", }, ) assert response.status_code == 200 assert response.json() == {"models": [{"id": "gpt-standard", "name": "gpt-standard"}]} fetch.assert_awaited_once_with( "https://standard.example/v1", "fixture-standard", "openai", api_format="responses" ) assert client.post(ENDPOINT, json={"binding": "openai"}).status_code == 400 def test_copilot_registry_and_probe_accept_empty_api_key(): from deeptutor.services.llm.config import LLMConfig from deeptutor.services.provider_registry import find_by_name spec = find_by_name("github_copilot") assert spec.is_oauth assert spec.auth_mode == "oauth" choices = settings._provider_choices()["llm"] assert next(row for row in choices if row["value"] == spec.name)["auth_mode"] == "oauth" config = LLMConfig( model="github-copilot/gpt-live", binding=spec.name, provider_name=spec.name, provider_mode=spec.mode, api_key="", base_url="", ) assert config.get_api_key() == "" def test_copilot_web_auth_routes_owner_isolated_and_admin_gated(client, tmp_path, monkeypatch): from deeptutor.multi_user.context import user_from_token_payload # Initially disconnected and marked no-store. res = client.get("/api/settings/providers/github-copilot/auth/status") assert res.status_code == 200 assert res.headers.get("cache-control") == "no-store" assert res.json()["connection"] == "disconnected" # CLI login for local-admin is immediately visible via web status. _login() res = client.get("/api/settings/providers/github-copilot/auth/status") assert res.status_code == 200 assert res.json()["connection"] == "connected" assert res.json()["account_id"] == "fixture-account" assert "fixture-github" not in res.text # Two distinct production admins (both receive shared admin_scope from # user_from_token_payload) must have strictly isolated Copilot auth state. admin_a = user_from_token_payload( SimpleNamespace(user_id="u_admin_a", username="alice", role="admin") ) admin_b = user_from_token_payload( SimpleNamespace(user_id="u_admin_b", username="bob", role="admin") ) assert admin_a.scope.kind == "admin" and admin_b.scope.kind == "admin" with user_context(admin_a): assert ( client.get("/api/settings/providers/github-copilot/auth/status").json()["connection"] == "disconnected" ) auth.get_github_copilot_storage().save( GitHubToken("fixture-admin-a", 2_000_000_000_000, "alice-gh") ) res_a = client.get("/api/settings/providers/github-copilot/auth/status") assert res_a.json()["connection"] == "connected" assert res_a.json()["account_id"] == "alice-gh" with user_context(admin_b): res_b = client.get("/api/settings/providers/github-copilot/auth/status") assert res_b.status_code == 200 assert res_b.json()["connection"] == "disconnected" # Admin B logging out or cancelling must not affect Admin A. out_b = client.post("/api/settings/providers/github-copilot/auth/logout") assert out_b.status_code == 200 assert out_b.headers.get("cache-control") == "no-store" with user_context(admin_a): still_a = client.get("/api/settings/providers/github-copilot/auth/status") assert still_a.json()["connection"] == "connected" assert still_a.json()["account_id"] == "alice-gh" # Non-admin user is rejected with 403 on all auth routes. reader = CurrentUser( id="u_reader", username="reader", role="user", scope=UserScope(kind="user", user_id="u_reader", root=tmp_path / "reader"), ) with user_context(reader): for method, path in ( ("get", "/api/settings/providers/github-copilot/auth/status"), ("post", "/api/settings/providers/github-copilot/auth/start"), ("post", "/api/settings/providers/github-copilot/auth/cancel"), ("post", "/api/settings/providers/github-copilot/auth/logout"), ): assert getattr(client, method)(path).status_code == 403 # Logout disconnects local-admin and clears stored token. out = client.post("/api/settings/providers/github-copilot/auth/logout") assert out.status_code == 200 assert out.headers.get("cache-control") == "no-store" assert out.json()["connection"] == "disconnected" assert auth.load_github_token() is None def test_fetch_models_reports_source_for_claude_code_and_codebuddy(client, monkeypatch): from deeptutor.services.llm.provider_core import claude_code_provider, codebuddy_models monkeypatch.setattr( claude_code_provider, "fetch_claude_code_catalog_with_source", AsyncMock( return_value=( [ {"id": "claude-code/default", "name": "Default (account setting)"}, {"id": "claude-code/opus", "name": "Opus"}, ], "fallback", ) ), ) cc_res = client.post(ENDPOINT, json={"binding": "claude_code"}) assert cc_res.status_code == 200 assert cc_res.json()["source"] == "fallback" assert len(cc_res.json()["models"]) == 2 monkeypatch.setattr( codebuddy_models, "fetch_codebuddy_models_with_source", AsyncMock(return_value=(["hy3", "glm-5.2"], "cli")), ) cb_res = client.post(ENDPOINT, json={"binding": "codebuddy"}) assert cb_res.status_code == 200 assert cb_res.json()["source"] == "cli" assert [m["id"] for m in cb_res.json()["models"]] == ["hy3", "glm-5.2"]