56 lines
1.7 KiB
Python
56 lines
1.7 KiB
Python
from __future__ import annotations
|
|
|
|
from uuid import UUID
|
|
|
|
import pytest
|
|
from core.http.errors import ApiProblemError
|
|
|
|
from core.auth.jwt_verifier import TokenValidationError
|
|
import v1.users.dependencies as deps
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_current_user_falls_back_to_supabase_validation(monkeypatch) -> None:
|
|
class _BrokenVerifier:
|
|
def verify(self, token: str) -> dict[str, object]:
|
|
del token
|
|
raise TokenValidationError("Token validation failed")
|
|
|
|
monkeypatch.setattr(deps, "get_jwt_verifier", lambda: _BrokenVerifier())
|
|
|
|
async def _fallback(token: str):
|
|
del token
|
|
return deps.CurrentUser(
|
|
id=UUID("e8845a17-282b-4a63-8025-194a06235958"),
|
|
phone="dagronl@126.com",
|
|
role="authenticated",
|
|
)
|
|
|
|
monkeypatch.setattr(deps, "_verify_user_with_supabase", _fallback)
|
|
|
|
user = await deps.get_current_user(authorization="Bearer valid-token")
|
|
|
|
assert str(user.id) == "e8845a17-282b-4a63-8025-194a06235958"
|
|
assert user.phone == "dagronl@126.com"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_current_user_raises_401_when_fallback_fails(monkeypatch) -> None:
|
|
class _BrokenVerifier:
|
|
def verify(self, token: str) -> dict[str, object]:
|
|
del token
|
|
raise TokenValidationError("Token validation failed")
|
|
|
|
monkeypatch.setattr(deps, "get_jwt_verifier", lambda: _BrokenVerifier())
|
|
|
|
async def _fallback(token: str):
|
|
del token
|
|
return None
|
|
|
|
monkeypatch.setattr(deps, "_verify_user_with_supabase", _fallback)
|
|
|
|
with pytest.raises(ApiProblemError) as exc:
|
|
await deps.get_current_user(authorization="Bearer invalid-token")
|
|
|
|
assert exc.value.status_code == 401
|