2026-02-05 15:13:06 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
import socket
|
|
|
|
|
import threading
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
from playwright.sync_api import sync_playwright
|
|
|
|
|
import uvicorn
|
|
|
|
|
|
|
|
|
|
from app import app
|
|
|
|
|
from v1.auth.dependencies import get_auth_service
|
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,
|
2026-02-26 14:37:51 +08:00
|
|
|
SessionRefreshRequest,
|
|
|
|
|
SessionResponse,
|
2026-02-05 15:13:06 +08:00
|
|
|
)
|
|
|
|
|
from v1.auth.service import AuthService
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FakeE2EAuthService(AuthService):
|
|
|
|
|
def __init__(self) -> None:
|
2026-03-19 18:42:59 +08:00
|
|
|
self._user = AuthUser(id="user-1", phone="+8613812345678")
|
2026-02-05 15:13:06 +08:00
|
|
|
|
2026-03-19 18:42:59 +08:00
|
|
|
async def send_otp(self, request: OtpSendRequest) -> None:
|
2026-02-26 14:37:51 +08:00
|
|
|
return None
|
2026-02-25 13:34:02 +08:00
|
|
|
|
2026-03-19 18:42:59 +08:00
|
|
|
async def create_phone_session(
|
|
|
|
|
self, request: PhoneSessionCreateRequest
|
|
|
|
|
) -> SessionResponse:
|
2026-02-26 14:37:51 +08:00
|
|
|
return SessionResponse(
|
2026-02-05 15:13:06 +08:00
|
|
|
access_token="access-2",
|
|
|
|
|
refresh_token="refresh-2",
|
|
|
|
|
expires_in=3600,
|
|
|
|
|
token_type="bearer",
|
|
|
|
|
user=self._user,
|
|
|
|
|
)
|
|
|
|
|
|
2026-02-26 14:37:51 +08:00
|
|
|
async def refresh_session(self, request: SessionRefreshRequest) -> SessionResponse:
|
|
|
|
|
return SessionResponse(
|
2026-02-05 15:13:06 +08:00
|
|
|
access_token="access-3",
|
|
|
|
|
refresh_token="refresh-3",
|
|
|
|
|
expires_in=3600,
|
|
|
|
|
token_type="bearer",
|
|
|
|
|
user=self._user,
|
|
|
|
|
)
|
|
|
|
|
|
2026-02-26 14:37:51 +08:00
|
|
|
async def delete_session(self, refresh_token: str | None) -> None:
|
2026-02-05 15:13:06 +08:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _find_free_port() -> int:
|
|
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
|
|
|
|
sock.bind(("127.0.0.1", 0))
|
|
|
|
|
return sock.getsockname()[1]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _wait_for_port(host: str, port: int, timeout: float = 5.0) -> None:
|
|
|
|
|
deadline = time.time() + timeout
|
|
|
|
|
while time.time() < deadline:
|
|
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
|
|
|
|
if sock.connect_ex((host, port)) == 0:
|
|
|
|
|
return
|
|
|
|
|
time.sleep(0.05)
|
|
|
|
|
raise RuntimeError("Server did not start in time")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _start_server(host: str, port: int):
|
|
|
|
|
config = uvicorn.Config(app, host=host, port=port, log_level="info")
|
|
|
|
|
server = uvicorn.Server(config)
|
|
|
|
|
thread = threading.Thread(target=server.run, daemon=True)
|
|
|
|
|
thread.start()
|
|
|
|
|
_wait_for_port(host, port)
|
|
|
|
|
return server, thread
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_auth_flow_e2e() -> None:
|
|
|
|
|
app.dependency_overrides[get_auth_service] = lambda: FakeE2EAuthService()
|
|
|
|
|
host = "127.0.0.1"
|
|
|
|
|
port = _find_free_port()
|
|
|
|
|
server, thread = _start_server(host, port)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
with sync_playwright() as playwright:
|
|
|
|
|
request_context = playwright.request.new_context(
|
|
|
|
|
base_url=f"http://{host}:{port}"
|
|
|
|
|
)
|
|
|
|
|
try:
|
2026-03-19 18:42:59 +08:00
|
|
|
send_code = request_context.post(
|
|
|
|
|
"/api/v1/auth/otp/send",
|
|
|
|
|
data=json.dumps({"phone": "+8613812345678"}),
|
2026-02-05 15:13:06 +08:00
|
|
|
headers={"Content-Type": "application/json"},
|
|
|
|
|
)
|
2026-03-19 18:42:59 +08:00
|
|
|
assert send_code.status == 204
|
2026-02-25 13:34:02 +08:00
|
|
|
|
2026-03-19 18:42:59 +08:00
|
|
|
login_or_register = request_context.post(
|
|
|
|
|
"/api/v1/auth/phone-session",
|
2026-02-25 13:34:02 +08:00
|
|
|
data=json.dumps(
|
|
|
|
|
{
|
2026-03-19 18:42:59 +08:00
|
|
|
"phone": "+8613812345678",
|
2026-02-25 13:34:02 +08:00
|
|
|
"token": "123456",
|
|
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
headers={"Content-Type": "application/json"},
|
|
|
|
|
)
|
2026-03-19 18:42:59 +08:00
|
|
|
assert login_or_register.status == 200
|
|
|
|
|
assert login_or_register.json()["access_token"] == "access-2"
|
2026-02-05 15:13:06 +08:00
|
|
|
|
|
|
|
|
refresh = request_context.post(
|
2026-02-26 14:37:51 +08:00
|
|
|
"/api/v1/auth/sessions/refresh",
|
2026-02-05 15:13:06 +08:00
|
|
|
data=json.dumps({"refresh_token": "refresh-2"}),
|
|
|
|
|
headers={"Content-Type": "application/json"},
|
|
|
|
|
)
|
|
|
|
|
assert refresh.status == 200
|
|
|
|
|
assert refresh.json()["access_token"] == "access-3"
|
|
|
|
|
|
2026-02-26 14:37:51 +08:00
|
|
|
logout = request_context.delete(
|
|
|
|
|
"/api/v1/auth/sessions",
|
2026-02-05 15:13:06 +08:00
|
|
|
data=json.dumps({"refresh_token": "refresh-3"}),
|
|
|
|
|
headers={"Content-Type": "application/json"},
|
|
|
|
|
)
|
|
|
|
|
assert logout.status == 204
|
|
|
|
|
finally:
|
|
|
|
|
request_context.dispose()
|
|
|
|
|
finally:
|
|
|
|
|
app.dependency_overrides = {}
|
|
|
|
|
server.should_exit = True
|
|
|
|
|
thread.join(timeout=5)
|