2026-02-05 15:13:06 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
from pydantic import ValidationError
|
|
|
|
|
|
2026-02-24 16:38:30 +08:00
|
|
|
from v1.auth.schemas import (
|
2026-02-05 15:13:06 +08:00
|
|
|
AuthUser,
|
2026-03-19 18:42:59 +08:00
|
|
|
OtpSendRequest,
|
|
|
|
|
PhoneSessionCreateRequest,
|
|
|
|
|
SessionDeleteRequest,
|
2026-02-26 14:08:10 +08:00
|
|
|
SessionRefreshRequest,
|
|
|
|
|
SessionResponse,
|
2026-02-05 15:13:06 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-03-19 18:42:59 +08:00
|
|
|
def test_send_otp_requires_valid_phone() -> None:
|
2026-02-05 15:13:06 +08:00
|
|
|
with pytest.raises(ValidationError):
|
2026-03-19 18:42:59 +08:00
|
|
|
OtpSendRequest(phone="13812345678")
|
2026-02-25 10:20:43 +08:00
|
|
|
|
|
|
|
|
|
2026-03-19 18:42:59 +08:00
|
|
|
def test_send_otp_accepts_e164_phone() -> None:
|
|
|
|
|
request = OtpSendRequest(phone="+14155552671")
|
2026-02-05 15:13:06 +08:00
|
|
|
|
2026-03-19 18:42:59 +08:00
|
|
|
assert request.phone == "+14155552671"
|
2026-03-12 16:41:45 +08:00
|
|
|
|
|
|
|
|
|
2026-03-19 18:42:59 +08:00
|
|
|
def test_phone_session_requires_six_digit_token() -> None:
|
2026-02-25 13:34:02 +08:00
|
|
|
with pytest.raises(ValidationError):
|
2026-03-19 18:42:59 +08:00
|
|
|
PhoneSessionCreateRequest(phone="+8613812345678", token="abc123")
|
2026-02-25 13:34:02 +08:00
|
|
|
|
2026-03-07 14:55:00 +08:00
|
|
|
|
2026-03-19 18:42:59 +08:00
|
|
|
def test_refresh_requires_token() -> None:
|
2026-02-05 15:13:06 +08:00
|
|
|
with pytest.raises(ValidationError):
|
2026-03-19 18:42:59 +08:00
|
|
|
SessionRefreshRequest(refresh_token="")
|
2026-02-05 15:13:06 +08:00
|
|
|
|
|
|
|
|
|
2026-03-19 18:42:59 +08:00
|
|
|
def test_logout_requires_token() -> None:
|
2026-02-05 15:13:06 +08:00
|
|
|
with pytest.raises(ValidationError):
|
2026-03-19 18:42:59 +08:00
|
|
|
SessionDeleteRequest(refresh_token="")
|
2026-02-05 15:13:06 +08:00
|
|
|
|
|
|
|
|
|
2026-02-26 14:08:10 +08:00
|
|
|
def test_session_response_maps_user() -> None:
|
2026-03-19 18:42:59 +08:00
|
|
|
user = AuthUser(id="user-1", phone="+14155552671")
|
2026-02-26 14:08:10 +08:00
|
|
|
response = SessionResponse(
|
2026-02-05 15:13:06 +08:00
|
|
|
access_token="access",
|
|
|
|
|
refresh_token="refresh",
|
|
|
|
|
expires_in=3600,
|
|
|
|
|
token_type="bearer",
|
|
|
|
|
user=user,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert response.user.id == "user-1"
|
2026-03-19 18:42:59 +08:00
|
|
|
assert response.user.phone == "+14155552671"
|