refactor: remove analytics module, update tool postprocessor tests

This commit is contained in:
qzl
2026-04-23 15:55:35 +08:00
parent 1052e19134
commit f708bce585
34 changed files with 294 additions and 1490 deletions
@@ -9,8 +9,10 @@ from core.config.settings import config
from v1.auth.gateway import SupabaseAuthGateway
from v1.auth.schemas import (
AuthUser,
OtpSendRequest,
PhoneSessionCreateRequest,
SessionResponse,
SessionRefreshRequest,
)
@@ -108,6 +110,39 @@ class TestSupabaseAuthGateway:
assert exc_info.value.status_code == 401
@pytest.mark.asyncio
async def test_refresh_session_uses_dev_flow_in_dev_environment(
self,
gateway: tuple[SupabaseAuthGateway, MagicMock, MagicMock],
monkeypatch: pytest.MonkeyPatch,
) -> None:
sut, mock_client, _ = gateway
monkeypatch.setattr(config.runtime, "environment", "dev")
expected = SessionResponse(
access_token="dev-access",
refresh_token="dev-refresh",
expires_in=3600,
token_type="bearer",
user=AuthUser(id="user-dev", phone="+8613812345678"),
)
async def _fake_refresh_dev_phone_session(*, request: SessionRefreshRequest) -> SessionResponse:
assert request.refresh_token == "dev-refresh"
return expected
monkeypatch.setattr(
"v1.auth.gateway.refresh_dev_phone_session",
_fake_refresh_dev_phone_session,
)
response = await sut.refresh_session(
SessionRefreshRequest(refresh_token="dev-refresh")
)
assert response == expected
assert mock_client.auth.refresh_session.call_count == 0
@pytest.mark.asyncio
async def test_get_user_by_phone_uses_in_memory_cache(
self,
@@ -0,0 +1,57 @@
from __future__ import annotations
from uuid import UUID
import pytest
from pydantic import SecretStr
from core.config.settings import config
from core.http.errors import ApiProblemError
from v1.auth.dev_phone_session import create_dev_phone_session, refresh_dev_phone_session
from v1.auth.schemas import PhoneSessionCreateRequest, SessionRefreshRequest
_TEST_JWT_SECRET = "test-secret-key-with-32-bytes-minimum!!"
@pytest.mark.asyncio
async def test_dev_session_refresh_round_trip(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(config.runtime, "environment", "dev")
monkeypatch.setattr(config.supabase, "jwt_secret", SecretStr(_TEST_JWT_SECRET))
monkeypatch.setattr(config.supabase, "jwt_issuer", "http://localhost:8001/auth/v1")
async def _fake_find_or_create_user_by_phone(_phone: str) -> UUID:
return UUID("00000000-0000-0000-0000-000000000123")
monkeypatch.setattr(
"v1.auth.dev_phone_session._find_or_create_user_by_phone",
_fake_find_or_create_user_by_phone,
)
created = await create_dev_phone_session(
request=PhoneSessionCreateRequest(phone="+8613812345678", token="123456")
)
refreshed = await refresh_dev_phone_session(
request=SessionRefreshRequest(refresh_token=created.refresh_token)
)
assert refreshed.user.id == "00000000-0000-0000-0000-000000000123"
assert refreshed.user.phone == "+8613812345678"
assert refreshed.access_token
assert refreshed.refresh_token
@pytest.mark.asyncio
async def test_dev_session_refresh_rejects_invalid_token(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(config.runtime, "environment", "dev")
monkeypatch.setattr(config.supabase, "jwt_secret", SecretStr(_TEST_JWT_SECRET))
monkeypatch.setattr(config.supabase, "jwt_issuer", "http://localhost:8001/auth/v1")
with pytest.raises(ApiProblemError) as exc_info:
await refresh_dev_phone_session(
request=SessionRefreshRequest(refresh_token="invalid-token")
)
assert exc_info.value.status_code == 401
assert exc_info.value.code == "AUTH_REFRESH_TOKEN_INVALID"