Files
zkxw/backend/tests/test_wechat_login_phone_bind.py
T
2026-08-11 11:13:01 +08:00

61 lines
1.7 KiB
Python

"""微信登录:openid 已绑定也须校验本次授权手机号。"""
from types import SimpleNamespace
from unittest.mock import MagicMock, patch
from app.services.wechat_login_service import resolve_wechat_login_user
from app.services.wechat_mini_auth import phones_match
def test_phones_match_equal():
assert phones_match("13800138000", "13800138000") is True
def test_phones_match_normalizes_plus86():
assert phones_match("13800138000", "+8613800138000") is True
def test_phones_match_mismatch():
assert phones_match("13866751937", "17756576757") is False
def test_phones_match_empty_account_fails():
assert phones_match(None, "17756576757") is False
assert phones_match("", "17756576757") is False
def test_resolve_openid_bound_but_phone_mismatch_rejects():
bound = SimpleNamespace(
id="admin-id",
phone_no="13866751937",
wx_openid="oid-admin",
)
db = MagicMock()
with patch(
"app.services.wechat_login_service.user_crud.get_by_wx_openid",
return_value=bound,
):
user, err, code = resolve_wechat_login_user(
db, openid="oid-admin", phone="17756576757"
)
assert user is None
assert code == 401
assert "不一致" in err
def test_resolve_openid_bound_and_phone_match_ok():
bound = SimpleNamespace(
id="admin-id",
phone_no="13866751937",
wx_openid="oid-admin",
)
db = MagicMock()
with patch(
"app.services.wechat_login_service.user_crud.get_by_wx_openid",
return_value=bound,
):
user, err, code = resolve_wechat_login_user(
db, openid="oid-admin", phone="13866751937"
)
assert err is None and code is None
assert user is bound