Files
DeepTutor/tests/services/llm/test_codex_disable_ssl_verify.py
truffle 93d24f776a fix(ssl): extend DISABLE_SSL_VERIFY coverage to codex provider and four embedding adapters
v1.3.10 added the `openai_http_client.disable_ssl_verify_enabled` helper
and applied it to the SDK paths (`openai_sdk` embedding,
`openai_compat_provider`, `azure_openai_provider`, tutorbot
`openai_compat_provider`). Five raw-httpx call sites still ignored the
flag:

- `provider_core/openai_codex_provider.py:79` hardcoded `verify=True` on
  the first attempt before falling back on `CERTIFICATE_VERIFY_FAILED`.
- `embedding/adapters/openai_compatible.py:193` constructed
  `httpx.AsyncClient(timeout=...)` with no `verify` kwarg.
- `embedding/adapters/jina.py:89`, `ollama.py:61`, `cohere.py:115` same.

Each site now consults the helper and passes `verify=not
disable_ssl_verify_enabled()`. The codex provider's
`CERTIFICATE_VERIFY_FAILED` retry path is preserved unchanged.

Tests:

- `tests/services/embedding/test_disable_ssl_verify.py` (5 adapter tests
  + 1 production-guard test) asserts each adapter passes `verify=False`
  when the flag is set and `verify=True` otherwise.
- `tests/services/llm/test_codex_disable_ssl_verify.py` covers the
  default-true case, flag-flip-to-false case, and the
  retry-on-cert-failure fallback.
2026-05-10 23:08:40 +00:00

99 lines
3.4 KiB
Python

"""DISABLE_SSL_VERIFY coverage for the OpenAI Codex Responses provider.
The codex provider was previously hardcoded to ``verify=True`` on the first
attempt, with an auto-retry on ``CERTIFICATE_VERIFY_FAILED``. The flag now
short-circuits the first attempt while preserving the retry-on-cert-failure
fallback.
"""
from __future__ import annotations
from typing import Any
import pytest
from deeptutor.services.llm import openai_http_client
from deeptutor.services.llm.provider_core import openai_codex_provider
@pytest.fixture(autouse=True)
def _clean_ssl_env(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.delenv("DISABLE_SSL_VERIFY", raising=False)
monkeypatch.delenv("ENVIRONMENT", raising=False)
monkeypatch.setattr(openai_http_client, "_warning_logged", False)
def _stub_token_loader(monkeypatch: pytest.MonkeyPatch) -> None:
class _Token:
access = "test-token"
account_id = "test-account"
async def _fake_load_token(self: Any) -> _Token:
return _Token()
monkeypatch.setattr(openai_codex_provider.OpenAICodexProvider, "_load_token", _fake_load_token)
@pytest.mark.asyncio
async def test_codex_first_attempt_verify_true_by_default(monkeypatch: pytest.MonkeyPatch) -> None:
_stub_token_loader(monkeypatch)
captured: list[dict[str, Any]] = []
async def fake_request(*args: Any, **kwargs: Any) -> tuple[str, list[Any], str]:
captured.append(kwargs)
return ("ok", [], "stop")
monkeypatch.setattr(openai_codex_provider, "_request_codex", fake_request)
provider = openai_codex_provider.OpenAICodexProvider()
result = await provider.chat(messages=[{"role": "user", "content": "hi"}])
assert result.content == "ok"
assert captured[0]["verify"] is True
@pytest.mark.asyncio
async def test_codex_first_attempt_verify_false_when_flag_set(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setenv("DISABLE_SSL_VERIFY", "1")
_stub_token_loader(monkeypatch)
captured: list[dict[str, Any]] = []
async def fake_request(*args: Any, **kwargs: Any) -> tuple[str, list[Any], str]:
captured.append(kwargs)
return ("ok", [], "stop")
monkeypatch.setattr(openai_codex_provider, "_request_codex", fake_request)
provider = openai_codex_provider.OpenAICodexProvider()
result = await provider.chat(messages=[{"role": "user", "content": "hi"}])
assert result.content == "ok"
assert captured[0]["verify"] is False
@pytest.mark.asyncio
async def test_codex_retry_on_cert_failure_still_works(monkeypatch: pytest.MonkeyPatch) -> None:
"""The CERTIFICATE_VERIFY_FAILED retry fallback is preserved."""
_stub_token_loader(monkeypatch)
captured: list[dict[str, Any]] = []
call_count = {"n": 0}
async def fake_request(*args: Any, **kwargs: Any) -> tuple[str, list[Any], str]:
captured.append(kwargs)
call_count["n"] += 1
if call_count["n"] == 1:
raise RuntimeError("[SSL: CERTIFICATE_VERIFY_FAILED] cert chain")
return ("recovered", [], "stop")
monkeypatch.setattr(openai_codex_provider, "_request_codex", fake_request)
provider = openai_codex_provider.OpenAICodexProvider()
result = await provider.chat(messages=[{"role": "user", "content": "hi"}])
assert result.content == "recovered"
assert len(captured) == 2
assert captured[0]["verify"] is True
assert captured[1]["verify"] is False