* feat(api): implement Google OAuth login and user management - Added Google OpenID Connect login functionality, allowing users to authenticate using their Google accounts. - Created new endpoints for Google login, including user registration and linking existing accounts. - Introduced Google token verification logic and error handling for authentication failures. - Updated environment configuration to include Google OAuth client IDs and verification settings. - Enhanced user model to support OpenID and linked Google accounts. This feature improves user experience by enabling seamless sign-in with Google, while maintaining security and integrity of user data. * fix(auth): wire staging Google token verifier * chore(deps): update expo to version 55.0.6 and adjust @expo/env dependency in pnpm-lock.yaml * chore(deps): update Babel dependencies to version 7.29.7 in package-lock.json * feat(auth): enhance phone login for China users - Updated phone login functionality to support only mainland China (+86) mobile numbers. - Added user prompts and descriptions for phone login, including confirmation and cancellation options. - Adjusted translations for both English and Chinese to reflect the new phone login requirements. - Updated Google OAuth client IDs in configuration files for production and staging environments. * chore(deps): add peer flag to use-sync-external-store in package-lock.json * chore(deps): add @emnapi/core and @emnapi/runtime to package-lock.json * fix(app-expo): align Android native dependencies * fix(app-expo): normalize lockfile for npm 10 * fix(config): update environment variable handling to use static access - Introduced a static mapping for public environment variables to ensure proper access during the release bundle. - Updated the `requirePublicEnv` and `optionalPublicEnv` functions to reference the new `PUBLIC_ENV` object instead of directly accessing `process.env`. - Added comments to clarify the necessity of static access for certain environment variables. * feat(app-expo): dark mode, FAQ i18n, eval ASR, and theme cleanup (#34) * feat(app-expo): dark mode, FAQ i18n, version CI, and theme cleanup Implement light/dark scene colors across chat, reading, and headers; remove default/brand theme picker and ThemeVariablesProvider. Localize FAQ in-app, fix dark-mode text visibility, and remove the unused /api/faqs endpoint. Align About/version with Expo config and inject APP_VERSION in CI builds. Also includes phone E164 auth/SMS updates, eval ASR page, and related API work. * revert: remove phone E.164 changes from dark-mode branch These auth/SMS internationalization updates were accidentally bundled into the dark-mode commit; restore 11-digit CN phone flow and drop related API, migration, and Expo UI work from this branch. * fix: address PR review issues for dark mode and eval ASR Use light foreground colors for sepia reading in dark mode, fix chat send button contrast, stream-limit eval ASR uploads, restore LiveTester phone validation, and remove unused AudioSegmenter code. * fix(app-expo): improve chat send button contrast in light and dark mode Add dedicated send button colors (accent fill in dark, primary fill in light), use RNText to avoid NativeWind overrides, and restore dark labels in light mode for readable composer actions. --------- Co-authored-by: Kevin <kevin@brighteng.org> --------- Co-authored-by: penghanyuan <penghanyuan@gmail.com> Co-authored-by: Kevin <kevin@brighteng.org>
121 lines
3.9 KiB
Python
121 lines
3.9 KiB
Python
"""Auth HTTP 契约:注册 / 登录 / 刷新 / 受保护 /me(依赖注入 mock)。"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from unittest.mock import AsyncMock, MagicMock
|
||
|
||
import pytest
|
||
from fastapi import FastAPI
|
||
from httpx import ASGITransport, AsyncClient
|
||
|
||
from app.core.dependencies import get_current_user
|
||
from app.features.auth.deps import get_auth_service
|
||
from app.features.auth.router import router as auth_router
|
||
from app.features.auth.service import AuthService
|
||
|
||
|
||
@pytest.fixture
|
||
def auth_app(make_test_user) -> FastAPI:
|
||
app = FastAPI()
|
||
app.include_router(auth_router)
|
||
|
||
mock_service = MagicMock(spec=AuthService)
|
||
mock_service.register = AsyncMock(
|
||
return_value={"access_token": "access-reg", "refresh_token": "refresh-reg"}
|
||
)
|
||
mock_service.login = AsyncMock(
|
||
return_value={"access_token": "access-login", "refresh_token": "refresh-login"}
|
||
)
|
||
mock_service.login_with_google = AsyncMock(
|
||
return_value={
|
||
"access_token": "access-google",
|
||
"refresh_token": "refresh-google",
|
||
}
|
||
)
|
||
mock_service.refresh_tokens = AsyncMock(
|
||
return_value={"access_token": "access-new", "refresh_token": "refresh-new"}
|
||
)
|
||
|
||
app.dependency_overrides[get_auth_service] = lambda: mock_service
|
||
app.dependency_overrides[get_current_user] = lambda: make_test_user()
|
||
|
||
app.state._mock_auth_service = mock_service
|
||
return app
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_register_login_refresh_me(auth_app: FastAPI, unique_phone: str) -> None:
|
||
transport = ASGITransport(app=auth_app)
|
||
async with AsyncClient(transport=transport, base_url="http://test") as ac:
|
||
reg = await ac.post(
|
||
"/api/auth/register",
|
||
json={
|
||
"phone": unique_phone,
|
||
"password": "secret12",
|
||
"nickname": "T",
|
||
"agreed_to_terms": True,
|
||
},
|
||
)
|
||
assert reg.status_code == 201
|
||
body = reg.json()
|
||
assert body["access_token"] == "access-reg"
|
||
|
||
login = await ac.post(
|
||
"/api/auth/login",
|
||
json={
|
||
"phone": unique_phone,
|
||
"password": "secret12",
|
||
"agreed_to_terms": True,
|
||
},
|
||
)
|
||
assert login.status_code == 200
|
||
assert login.json()["access_token"] == "access-login"
|
||
|
||
google = await ac.post(
|
||
"/api/auth/login/google",
|
||
json={
|
||
"id_token": "header.payload.signature",
|
||
"agreed_to_terms": True,
|
||
},
|
||
)
|
||
assert google.status_code == 200
|
||
assert google.json()["access_token"] == "access-google"
|
||
|
||
ref = await ac.post(
|
||
"/api/auth/refresh",
|
||
json={"refresh_token": "refresh-login"},
|
||
)
|
||
assert ref.status_code == 200
|
||
assert ref.json()["access_token"] == "access-new"
|
||
|
||
me = await ac.get(
|
||
"/api/auth/me",
|
||
headers={"Authorization": "Bearer any"},
|
||
)
|
||
assert me.status_code == 200
|
||
assert me.json()["nickname"] == "测试用户"
|
||
|
||
svc: MagicMock = auth_app.state._mock_auth_service
|
||
svc.register.assert_awaited_once()
|
||
svc.login.assert_awaited_once()
|
||
svc.login_with_google.assert_awaited_once_with(
|
||
id_token="header.payload.signature",
|
||
language=None,
|
||
)
|
||
svc.refresh_tokens.assert_awaited_once_with(refresh_token="refresh-login")
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_me_without_auth_returns_401() -> None:
|
||
app = FastAPI()
|
||
app.include_router(auth_router)
|
||
|
||
mock_service = MagicMock(spec=AuthService)
|
||
app.dependency_overrides[get_auth_service] = lambda: mock_service
|
||
# 不覆盖 get_current_user — 使用真实 OAuth2,无 header 时应 401
|
||
|
||
transport = ASGITransport(app=app)
|
||
async with AsyncClient(transport=transport, base_url="http://test") as ac:
|
||
r = await ac.get("/api/auth/me")
|
||
assert r.status_code == 401
|