Files
DeepTutor/tests/services/codex_auth/test_contracts.py
TyrionH-is-coding c3d1dd0b15 feat(llm): add first-party OpenAI Codex OAuth provider (#690)
Replaces the oauth_cli_kit indirection with DeepTutor's own PKCE loopback
sign-in, an account-scoped live model catalog, and a Settings card that exposes
only the OAuth surface. Credentials live in `<user-root>/private/openai-codex/`
with owner-only permissions, atomic writes, and a generation guard that stops a
late refresh from resurrecting a signed-out session; `~/.codex` is never read or
modified. A CLI path shares the same service.

A Codex token authorizes one person's ChatGPT plan, so credentials anchor to the
signing-in account's own root and the managed profile is marked `owner_bound`:
`redacted_model_access` drops it for every caller, and it is not offered as an
assignable model. Signing in publishes the profile but only activates it when no
LLM is configured yet, so it never repoints a deployment that already has one.

Also from review: reschedule the status poll so one dropped response cannot
strand the card on "waiting"; defer the catalog reload when the operator has
unsaved edits instead of discarding them; log the real cause behind the generic
provider error; stop promising a retry when the token refresh actually failed;
bind both loopback stacks since `localhost` may resolve to either; and drop the
provider metadata fields nothing consumed.
2026-07-26 22:27:54 +08:00

164 lines
4.5 KiB
Python

from __future__ import annotations
import base64
import json
import pytest
from deeptutor.services.codex_auth.constants import (
CODEX_CALLBACK_PORTS,
CODEX_CLIENT_VERSION,
CODEX_DEFAULT_MODEL,
CODEX_MODELS_URL,
CODEX_OAUTH_CLIENT_ID,
)
from deeptutor.services.codex_auth.contracts import (
CatalogSnapshot,
CodexAuthError,
CodexCredentials,
CodexModel,
TokenClaims,
decode_codex_jwt,
)
def _jwt_for_test(payload: dict[str, object]) -> str:
def encode(value: dict[str, object]) -> str:
raw = json.dumps(value, separators=(",", ":")).encode("utf-8")
return base64.urlsafe_b64encode(raw).rstrip(b"=").decode("ascii")
return f"{encode({'alg': 'none'})}.{encode(payload)}."
def _model() -> CodexModel:
return CodexModel(
slug="gpt-5.6-sol",
display_name="GPT-5.6-Sol",
priority=10,
visibility="list",
default_reasoning_level="medium",
supported_reasoning_levels=("low", "medium", "high"),
supports_reasoning_summary=True,
supports_parallel_tool_calls=True,
use_responses_lite=False,
)
def test_codex_upstream_contract_is_pinned() -> None:
assert CODEX_OAUTH_CLIENT_ID == "app_EMoamEEZ73f0CkXaXp7hrann"
assert CODEX_CALLBACK_PORTS == (1455, 1457)
assert CODEX_CLIENT_VERSION == "0.145.0"
assert CODEX_MODELS_URL.endswith("/backend-api/codex/models")
assert CODEX_DEFAULT_MODEL == "gpt-5.6-sol"
def test_decode_token_claims_drops_email() -> None:
token = _jwt_for_test(
{
"exp": 2_000_000_000,
"email": "must-not-leave-backend@example.test",
"https://api.openai.com/auth": {
"chatgpt_account_id": "account-123",
"chatgpt_plan_type": "plus",
},
}
)
claims = decode_codex_jwt(token)
assert claims == TokenClaims(expires_at=2_000_000_000, account_id="account-123")
assert "email" not in repr(claims).lower()
assert "example.test" not in repr(claims)
def test_decode_token_claims_ignores_wrong_field_types() -> None:
token = _jwt_for_test(
{
"exp": "not-an-integer",
"https://api.openai.com/auth": {"chatgpt_account_id": 42},
}
)
assert decode_codex_jwt(token) == TokenClaims(expires_at=None, account_id=None)
def test_decode_token_claims_rejects_malformed_payload_without_echoing_token() -> None:
token = "header.not-base64.secret"
with pytest.raises(CodexAuthError) as exc_info:
decode_codex_jwt(token)
error = exc_info.value
assert error.code == "invalid_token"
assert error.http_status == 401
assert str(error) == error.public_message
assert token not in str(error)
def test_credentials_round_trip_and_public_token() -> None:
credentials = CodexCredentials(
schema_version=1,
access_token="access-secret",
refresh_token="refresh-secret",
id_token="id-secret",
account_id="account-123",
expires_at=2_000_000_000,
generation=7,
)
restored = CodexCredentials.from_dict(credentials.to_dict())
assert restored == credentials
assert restored.public_token().access_token == "access-secret"
assert not hasattr(restored.public_token(), "refresh_token")
@pytest.mark.parametrize(
("field", "value"),
[
("access_token", None),
("refresh_token", ""),
("id_token", 123),
("account_id", None),
("expires_at", True),
("generation", True),
("schema_version", 2),
],
)
def test_credentials_reject_corrupt_field_types(
field: str,
value: object,
) -> None:
payload = CodexCredentials(
schema_version=1,
access_token="access-secret",
refresh_token="refresh-secret",
id_token="id-secret",
account_id="account-123",
expires_at=2_000_000_000,
generation=7,
).to_dict()
payload[field] = value
with pytest.raises(CodexAuthError) as exc_info:
CodexCredentials.from_dict(payload)
assert exc_info.value.code == "credential_corrupt"
def test_catalog_snapshot_round_trip_preserves_tuples() -> None:
snapshot = CatalogSnapshot(
models=(_model(),),
source="live",
fetched_at=2_000_000_000,
etag='"catalog-v1"',
generation=7,
account_hash="account-hash",
)
restored = CatalogSnapshot.from_dict(snapshot.to_dict())
assert restored == snapshot
assert isinstance(restored.models, tuple)
assert isinstance(restored.models[0].supported_reasoning_levels, tuple)