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-02-26 14:08:10 +08:00
|
|
|
SessionCreateRequest,
|
|
|
|
|
SessionRefreshRequest,
|
|
|
|
|
SessionResponse,
|
|
|
|
|
VerificationCreateRequest,
|
|
|
|
|
VerificationVerifyRequest,
|
|
|
|
|
VerificationResendRequest,
|
2026-02-05 15:13:06 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_signup_requires_valid_email() -> None:
|
|
|
|
|
with pytest.raises(ValidationError):
|
2026-02-26 14:08:10 +08:00
|
|
|
VerificationCreateRequest(
|
|
|
|
|
username="demo", email="not-an-email", password="secret123"
|
|
|
|
|
)
|
2026-02-25 10:20:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_signup_requires_username() -> None:
|
|
|
|
|
with pytest.raises(ValidationError):
|
2026-02-26 14:08:10 +08:00
|
|
|
VerificationCreateRequest.model_validate(
|
2026-02-25 10:20:43 +08:00
|
|
|
{"email": "user@example.com", "password": "secret123"}
|
|
|
|
|
)
|
2026-02-05 15:13:06 +08:00
|
|
|
|
|
|
|
|
|
2026-03-12 16:41:45 +08:00
|
|
|
def test_signup_allows_any_invite_code_input() -> None:
|
|
|
|
|
request = VerificationCreateRequest(
|
|
|
|
|
username="demo",
|
|
|
|
|
email="user@example.com",
|
|
|
|
|
password="secret123",
|
|
|
|
|
invite_code="abc123",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert request.invite_code == "abc123"
|
|
|
|
|
|
|
|
|
|
|
2026-02-25 13:34:02 +08:00
|
|
|
def test_signup_verify_requires_six_digit_token() -> None:
|
|
|
|
|
with pytest.raises(ValidationError):
|
2026-02-26 14:08:10 +08:00
|
|
|
VerificationVerifyRequest(email="user@example.com", token="abc123")
|
2026-02-25 13:34:02 +08:00
|
|
|
|
|
|
|
|
|
2026-03-07 14:55:00 +08:00
|
|
|
def test_signup_verify_disallows_new_password() -> None:
|
|
|
|
|
with pytest.raises(ValidationError):
|
|
|
|
|
VerificationVerifyRequest(
|
|
|
|
|
type="signup",
|
|
|
|
|
email="user@example.com",
|
|
|
|
|
token="123456",
|
|
|
|
|
new_password="secret123",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_recovery_verify_requires_new_password() -> None:
|
|
|
|
|
with pytest.raises(ValidationError):
|
|
|
|
|
VerificationVerifyRequest(
|
|
|
|
|
type="recovery",
|
|
|
|
|
email="user@example.com",
|
|
|
|
|
token="123456",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-02-25 13:34:02 +08:00
|
|
|
def test_signup_resend_requires_valid_email() -> None:
|
|
|
|
|
with pytest.raises(ValidationError):
|
2026-02-26 14:08:10 +08:00
|
|
|
VerificationResendRequest(email="invalid")
|
2026-02-25 13:34:02 +08:00
|
|
|
|
|
|
|
|
|
2026-02-05 15:13:06 +08:00
|
|
|
def test_login_requires_valid_email() -> None:
|
|
|
|
|
with pytest.raises(ValidationError):
|
2026-02-26 14:08:10 +08:00
|
|
|
SessionCreateRequest(email="invalid", password="secret123")
|
2026-02-05 15:13:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_refresh_requires_token() -> None:
|
|
|
|
|
with pytest.raises(ValidationError):
|
2026-02-26 14:08:10 +08:00
|
|
|
SessionRefreshRequest(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-02-05 15:13:06 +08:00
|
|
|
user = AuthUser(id="user-1", email="user@example.com")
|
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"
|
|
|
|
|
assert response.user.email == "user@example.com"
|