2026-02-05 15:57:52 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
import time
|
|
|
|
|
from types import SimpleNamespace
|
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
from src.api.handlers.base.request_builder import get_provider_auth
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_get_provider_auth_oauth_returns_decrypted_auth_config() -> None:
|
|
|
|
|
now = int(time.time())
|
|
|
|
|
token_meta = {
|
|
|
|
|
"provider_type": "antigravity",
|
|
|
|
|
"expires_at": now + 3600,
|
|
|
|
|
"refresh_token": "rt-1",
|
|
|
|
|
"project_id": "project-1",
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-07 02:59:02 +08:00
|
|
|
endpoint = SimpleNamespace(api_format="gemini:chat")
|
2026-02-05 15:57:52 +08:00
|
|
|
key = SimpleNamespace(
|
|
|
|
|
id="k1",
|
|
|
|
|
auth_type="oauth",
|
|
|
|
|
api_key="enc_access",
|
|
|
|
|
auth_config="enc_cfg",
|
|
|
|
|
provider=None,
|
|
|
|
|
)
|
|
|
|
|
|
2026-02-06 16:37:06 +08:00
|
|
|
def _decrypt(v: str) -> str:
|
2026-02-05 15:57:52 +08:00
|
|
|
if v == "enc_access":
|
|
|
|
|
return "access-token"
|
|
|
|
|
if v == "enc_cfg":
|
|
|
|
|
return json.dumps(token_meta)
|
|
|
|
|
return ""
|
|
|
|
|
|
2026-02-06 16:37:06 +08:00
|
|
|
with patch(
|
|
|
|
|
"src.api.handlers.base.request_builder.crypto_service.decrypt", side_effect=_decrypt
|
|
|
|
|
):
|
2026-02-05 15:57:52 +08:00
|
|
|
auth = await get_provider_auth(endpoint, key) # type: ignore[arg-type]
|
|
|
|
|
|
|
|
|
|
assert auth is not None
|
|
|
|
|
assert auth.auth_header == "Authorization"
|
|
|
|
|
assert auth.auth_value == "Bearer access-token"
|
|
|
|
|
assert auth.decrypted_auth_config == token_meta
|