mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 04:00:25 +08:00
Replace in-request backoff with a monotonic cache, shield shared status probes, bound SDK startup and flow waits, retain child task ownership, and fence stale probe/login results across start, cancel, and logout (35 passed).
239 lines
9.1 KiB
Python
239 lines
9.1 KiB
Python
"""Tests for reading the shared CodeBuddy login state."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import base64
|
|
import gzip
|
|
import json
|
|
from pathlib import Path
|
|
import time
|
|
|
|
import pytest
|
|
|
|
from deeptutor.services import codebuddy_credentials
|
|
from deeptutor.services.codebuddy_credentials import (
|
|
INTERNAL_ENDPOINT,
|
|
OVERSEAS_ENDPOINT,
|
|
CodeBuddyAuthUnavailable,
|
|
CodeBuddyCredentials,
|
|
cached_model_catalog,
|
|
load_credentials,
|
|
refresh_credentials,
|
|
resolve_api_base,
|
|
)
|
|
|
|
|
|
def _write_auth_file(tmp_path: Path, monkeypatch, **auth_overrides) -> Path:
|
|
auth = {
|
|
"accessToken": "access-token",
|
|
"refreshToken": "refresh-token",
|
|
"tokenType": "Bearer",
|
|
"expiresAt": int((time.time() + 3600) * 1000),
|
|
"refreshExpiresAt": int((time.time() + 30 * 24 * 3600) * 1000),
|
|
"domain": "www.codebuddy.cn",
|
|
}
|
|
auth.update(auth_overrides)
|
|
path = tmp_path / "Tencent-Cloud.coding-copilot.info"
|
|
path.write_text(
|
|
json.dumps({"account": {"uid": "uid-1", "nickname": "tester"}, "auth": auth}),
|
|
encoding="utf-8",
|
|
)
|
|
monkeypatch.setenv("DEEPTUTOR_CODEBUDDY_AUTH_FILE", str(path))
|
|
return path
|
|
|
|
|
|
def test_load_credentials_parses_session(tmp_path, monkeypatch) -> None:
|
|
expires_at_ms = int((time.time() + 3600) * 1000)
|
|
_write_auth_file(tmp_path, monkeypatch, expiresAt=expires_at_ms)
|
|
|
|
credentials = load_credentials()
|
|
|
|
assert credentials is not None
|
|
assert credentials.access_token == "access-token"
|
|
assert credentials.refresh_token == "refresh-token"
|
|
assert credentials.user_id == "uid-1"
|
|
assert credentials.user_label == "tester"
|
|
# Epoch milliseconds in the file, seconds in the dataclass.
|
|
assert credentials.expires_at == pytest.approx(expires_at_ms / 1000)
|
|
assert credentials.is_expired() is False
|
|
|
|
|
|
def test_load_credentials_returns_none_when_signed_out() -> None:
|
|
assert load_credentials() is None
|
|
|
|
|
|
def test_load_credentials_rejects_file_without_token(tmp_path, monkeypatch) -> None:
|
|
path = tmp_path / "Tencent-Cloud.coding-copilot.info"
|
|
path.write_text(json.dumps({"auth": {"refreshToken": "only-refresh"}}), encoding="utf-8")
|
|
monkeypatch.setenv("DEEPTUTOR_CODEBUDDY_AUTH_FILE", str(path))
|
|
|
|
assert load_credentials() is None
|
|
|
|
|
|
def test_resolve_api_base_follows_account_domain() -> None:
|
|
assert resolve_api_base("www.codebuddy.cn") == INTERNAL_ENDPOINT + "/v2"
|
|
assert resolve_api_base("www.codebuddy.ai") == OVERSEAS_ENDPOINT + "/v2"
|
|
|
|
|
|
def test_resolve_api_base_honours_env_overrides(monkeypatch) -> None:
|
|
monkeypatch.setenv("CODEBUDDY_BASE_URL", "https://proxy.example.com")
|
|
assert resolve_api_base("www.codebuddy.ai") == "https://proxy.example.com/v2"
|
|
|
|
monkeypatch.delenv("CODEBUDDY_BASE_URL")
|
|
monkeypatch.setenv("CODEBUDDY_INTERNET_ENVIRONMENT", "internal")
|
|
assert resolve_api_base("www.codebuddy.ai") == INTERNAL_ENDPOINT + "/v2"
|
|
|
|
|
|
def test_cached_model_catalog_reads_cli_cache(tmp_path, monkeypatch) -> None:
|
|
config = {"endpoint": INTERNAL_ENDPOINT, "models": [{"id": "default"}, {"id": "glm-5.2"}]}
|
|
blob = base64.b64encode(gzip.compress(json.dumps(config).encode())).decode()
|
|
(tmp_path / "entry_abc.info").write_text(json.dumps(blob), encoding="utf-8")
|
|
monkeypatch.setattr(codebuddy_credentials, "_local_storage_dir", lambda: tmp_path)
|
|
|
|
assert cached_model_catalog() == ["default", "glm-5.2"]
|
|
assert resolve_api_base("www.codebuddy.ai") == INTERNAL_ENDPOINT + "/v2"
|
|
|
|
|
|
def test_expired_session_without_refresh_token_is_reported(tmp_path, monkeypatch) -> None:
|
|
_write_auth_file(
|
|
tmp_path,
|
|
monkeypatch,
|
|
refreshToken="",
|
|
expiresAt=int((time.time() - 60) * 1000),
|
|
)
|
|
|
|
credentials = load_credentials()
|
|
|
|
assert credentials is not None
|
|
assert credentials.is_expired() is True
|
|
assert credentials.can_refresh() is False
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_refresh_credentials_requires_a_refresh_token() -> None:
|
|
credentials = CodeBuddyCredentials(access_token="stale", refresh_token="")
|
|
|
|
with pytest.raises(CodeBuddyAuthUnavailable):
|
|
await refresh_credentials(credentials)
|
|
|
|
|
|
def test_a_cached_endpoint_cannot_redirect_the_bearer_token(tmp_path, monkeypatch) -> None:
|
|
"""The product cache belongs to another application on this host.
|
|
|
|
Anything able to write under the service account's home could otherwise
|
|
name the host that receives the CodeBuddy access token.
|
|
"""
|
|
import base64
|
|
import gzip
|
|
import json
|
|
|
|
from deeptutor.services import codebuddy_credentials as creds
|
|
|
|
storage = tmp_path / ".codebuddy" / "local_storage"
|
|
storage.mkdir(parents=True)
|
|
blob = {"models": [{"id": "default"}], "endpoint": "http://attacker.example"}
|
|
packed = base64.b64encode(gzip.compress(json.dumps(blob).encode())).decode()
|
|
(storage / "entry_1.info").write_text(json.dumps(packed), encoding="utf-8")
|
|
monkeypatch.setattr(creds, "_local_storage_dir", lambda: storage)
|
|
monkeypatch.delenv("CODEBUDDY_BASE_URL", raising=False)
|
|
monkeypatch.delenv("CODEBUDDY_INTERNET_ENVIRONMENT", raising=False)
|
|
|
|
assert creds.cached_product_endpoint() is None
|
|
# The untrusted host must not reach the request either.
|
|
assert "attacker.example" not in creds.resolve_api_base("")
|
|
|
|
|
|
def test_a_trusted_cached_endpoint_is_still_honoured(tmp_path, monkeypatch) -> None:
|
|
import base64
|
|
import gzip
|
|
import json
|
|
|
|
from deeptutor.services import codebuddy_credentials as creds
|
|
|
|
storage = tmp_path / ".codebuddy" / "local_storage"
|
|
storage.mkdir(parents=True)
|
|
blob = {"models": [{"id": "default"}], "endpoint": creds.INTERNAL_ENDPOINT}
|
|
packed = base64.b64encode(gzip.compress(json.dumps(blob).encode())).decode()
|
|
(storage / "entry_1.info").write_text(json.dumps(packed), encoding="utf-8")
|
|
monkeypatch.setattr(creds, "_local_storage_dir", lambda: storage)
|
|
monkeypatch.delenv("CODEBUDDY_BASE_URL", raising=False)
|
|
monkeypatch.delenv("CODEBUDDY_INTERNET_ENVIRONMENT", raising=False)
|
|
|
|
assert creds.cached_product_endpoint() == creds.INTERNAL_ENDPOINT
|
|
|
|
|
|
def test_find_auth_file_locates_the_workbuddy_desktop_session(tmp_path, monkeypatch) -> None:
|
|
"""Regression: WorkBuddy 5.9.3 renamed the shared session file.
|
|
|
|
The lookup only knew ``Tencent-Cloud.coding-copilot.info``, so on current
|
|
WorkBuddy installs ``find_auth_file`` returned ``None`` and the SDK-free
|
|
login path was dead even though the session existed on disk.
|
|
"""
|
|
from deeptutor.services import codebuddy_credentials as creds
|
|
|
|
auth_dir = tmp_path / "CodeBuddyExtension" / "Data" / "Public" / "auth"
|
|
auth_dir.mkdir(parents=True)
|
|
session = auth_dir / "workbuddy-desktop.info"
|
|
session.write_text(json.dumps({"access_token": "wb-token"}), encoding="utf-8")
|
|
monkeypatch.delenv("DEEPTUTOR_CODEBUDDY_AUTH_FILE", raising=False)
|
|
monkeypatch.setattr(creds, "_auth_search_dirs", lambda: [auth_dir])
|
|
|
|
assert creds.find_auth_file() == session
|
|
|
|
|
|
def test_load_credentials_reads_the_workbuddy_flat_envelope(tmp_path, monkeypatch) -> None:
|
|
"""The WorkBuddy envelope carries the access token at the top level."""
|
|
from deeptutor.services import codebuddy_credentials as creds
|
|
|
|
path = tmp_path / "workbuddy-desktop.info"
|
|
path.write_text(
|
|
json.dumps({"account": {"uid": "uid-9"}, "access_token": "wb-access-token"}),
|
|
encoding="utf-8",
|
|
)
|
|
monkeypatch.setenv("DEEPTUTOR_CODEBUDDY_AUTH_FILE", str(path))
|
|
|
|
credentials = creds.load_credentials()
|
|
|
|
assert credentials is not None
|
|
assert credentials.access_token == "wb-access-token"
|
|
assert credentials.user_id == "uid-9"
|
|
|
|
|
|
def test_an_encrypted_workbuddy_token_is_never_sent_as_a_bearer(tmp_path, monkeypatch) -> None:
|
|
"""An at-rest encrypted token is not a bearer token, so it is rejected."""
|
|
from deeptutor.services import codebuddy_credentials as creds
|
|
|
|
path = tmp_path / "workbuddy-desktop.info"
|
|
path.write_text(json.dumps({"access_token": "$wbEncrypted:opaque"}), encoding="utf-8")
|
|
monkeypatch.setenv("DEEPTUTOR_CODEBUDDY_AUTH_FILE", str(path))
|
|
|
|
assert creds.load_credentials() is None
|
|
|
|
|
|
def test_find_auth_file_prefers_workbuddy_desktop_over_legacy(tmp_path, monkeypatch) -> None:
|
|
"""Regression: workbuddy-desktop.info should be checked before the legacy file.
|
|
|
|
Both files may exist; the legacy one might be stale while the desktop file
|
|
is current. The desktop file must win.
|
|
"""
|
|
from deeptutor.services import codebuddy_credentials as creds
|
|
|
|
auth_dir = tmp_path / "CodeBuddyExtension" / "Data" / "Public" / "auth"
|
|
auth_dir.mkdir(parents=True)
|
|
|
|
legacy = auth_dir / "Tencent-Cloud.coding-copilot.info"
|
|
legacy.write_text(json.dumps({"auth": {"accessToken": "legacy-token"}}), encoding="utf-8")
|
|
|
|
desktop = auth_dir / "workbuddy-desktop.info"
|
|
desktop.write_text(json.dumps({"access_token": "desktop-token"}), encoding="utf-8")
|
|
|
|
monkeypatch.delenv("DEEPTUTOR_CODEBUDDY_AUTH_FILE", raising=False)
|
|
monkeypatch.setattr(creds, "_auth_search_dirs", lambda: [auth_dir])
|
|
|
|
found = creds.find_auth_file()
|
|
assert found == desktop, "Desktop file should be preferred over legacy"
|
|
|
|
credentials = creds.load_credentials()
|
|
assert credentials is not None
|
|
assert credentials.access_token == "desktop-token"
|