test_wechat_login_phone_bind.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. """微信登录:openid 已绑定也须校验本次授权手机号。"""
  2. from types import SimpleNamespace
  3. from unittest.mock import MagicMock, patch
  4. from app.services.wechat_login_service import resolve_wechat_login_user
  5. from app.services.wechat_mini_auth import phones_match
  6. def test_phones_match_equal():
  7. assert phones_match("13800138000", "13800138000") is True
  8. def test_phones_match_normalizes_plus86():
  9. assert phones_match("13800138000", "+8613800138000") is True
  10. def test_phones_match_mismatch():
  11. assert phones_match("13866751937", "17756576757") is False
  12. def test_phones_match_empty_account_fails():
  13. assert phones_match(None, "17756576757") is False
  14. assert phones_match("", "17756576757") is False
  15. def test_resolve_openid_bound_but_phone_mismatch_rejects():
  16. bound = SimpleNamespace(
  17. id="admin-id",
  18. phone_no="13866751937",
  19. wx_openid="oid-admin",
  20. )
  21. db = MagicMock()
  22. with patch(
  23. "app.services.wechat_login_service.user_crud.get_by_wx_openid",
  24. return_value=bound,
  25. ):
  26. user, err, code = resolve_wechat_login_user(
  27. db, openid="oid-admin", phone="17756576757"
  28. )
  29. assert user is None
  30. assert code == 401
  31. assert "不一致" in err
  32. def test_resolve_openid_bound_and_phone_match_ok():
  33. bound = SimpleNamespace(
  34. id="admin-id",
  35. phone_no="13866751937",
  36. wx_openid="oid-admin",
  37. )
  38. db = MagicMock()
  39. with patch(
  40. "app.services.wechat_login_service.user_crud.get_by_wx_openid",
  41. return_value=bound,
  42. ):
  43. user, err, code = resolve_wechat_login_user(
  44. db, openid="oid-admin", phone="13866751937"
  45. )
  46. assert err is None and code is None
  47. assert user is bound