Files
shaguabijia-app-server/tests/test_oneclick.py
T
guke f161d07615 feat(auth): 后端接入阿里云一键登录 provider(门面+GetMobile换号+契约+配置)
新增 aliyun_onekey(Dypnsapi GetMobile 换号,无 RSA)+ oneclick 门面(按 provider 分派 jiguang/aliyun);jverify-login 与微信绑号两处换号改走门面 + JverifyLoginRequest 加 provider 字段 + ALIYUN_ONEKEY 配置。新增 15 单测(aliyun_onekey/oneclick/端点),微信绑号回归修 2 处桩。真机端到端验证通过。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-28 17:13:57 +08:00

75 lines
2.5 KiB
Python

"""一键登录换号门面 oneclick 单测:按 provider 分派到极光/阿里云, 统一异常。
纯函数(monkeypatch 掉两家的 verify_and_get_phone), 不碰 DB。本机可跑:
SGB_TEST_SKIP_DB=1 pytest tests/test_oneclick.py
"""
from __future__ import annotations
import pytest
from app.integrations import aliyun_onekey, jiguang, oneclick
def test_dispatch_defaults_to_jiguang_when_blank(monkeypatch):
"""provider 为空 = 老客户端 → 走极光(向后兼容)。"""
seen = {}
def fake_jg(t):
seen["jg"] = t
return "13800000000"
monkeypatch.setattr(jiguang, "verify_and_get_phone", fake_jg)
assert oneclick.verify_and_get_phone("", "tok") == "13800000000"
assert seen["jg"] == "tok"
def test_dispatch_jiguang_explicit(monkeypatch):
monkeypatch.setattr(jiguang, "verify_and_get_phone", lambda t: "13811111111")
assert oneclick.verify_and_get_phone("jiguang", "tok") == "13811111111"
def test_dispatch_aliyun(monkeypatch):
seen = {}
def fake_al(t):
seen["al"] = t
return "13822222222"
monkeypatch.setattr(aliyun_onekey, "verify_and_get_phone", fake_al)
assert oneclick.verify_and_get_phone("aliyun", "tok") == "13822222222"
assert seen["al"] == "tok"
def test_provider_is_case_insensitive(monkeypatch):
monkeypatch.setattr(aliyun_onekey, "verify_and_get_phone", lambda t: "13844444444")
assert oneclick.verify_and_get_phone("Aliyun", "tok") == "13844444444"
def test_unknown_provider_falls_back_to_jiguang(monkeypatch):
"""未知 provider 兜底走极光(主家), 不因客户端传错值而拒登。"""
monkeypatch.setattr(jiguang, "verify_and_get_phone", lambda t: "13833333333")
assert oneclick.verify_and_get_phone("weird-value", "tok") == "13833333333"
def test_jiguang_error_wrapped_as_oneclick_error(monkeypatch):
def boom(_t):
raise jiguang.JiguangError("jg down")
monkeypatch.setattr(jiguang, "verify_and_get_phone", boom)
with pytest.raises(oneclick.OneClickError):
oneclick.verify_and_get_phone("jiguang", "tok")
def test_aliyun_error_wrapped_as_oneclick_error(monkeypatch):
def boom(_t):
raise aliyun_onekey.AliyunOneClickError("aliyun down")
monkeypatch.setattr(aliyun_onekey, "verify_and_get_phone", boom)
with pytest.raises(oneclick.OneClickError):
oneclick.verify_and_get_phone("aliyun", "tok")
def test_mask_phone_reexported():
"""auth 层只依赖 oneclick, 脱敏函数从门面转出。"""
assert oneclick.mask_phone("13800138000") == "138****00"