84 lines
2.4 KiB
Python
84 lines
2.4 KiB
Python
"""微信登录:openid 已绑定可无 phoneCode 静默换票。"""
|
|
from types import SimpleNamespace
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
from app.services.wechat_login_service import resolve_wechat_login_by_openid_only
|
|
|
|
|
|
def test_resolve_openid_bound_ok_without_phone():
|
|
bound = SimpleNamespace(
|
|
id="u1",
|
|
wx_openid="oid-1",
|
|
status="1",
|
|
)
|
|
db = MagicMock()
|
|
with patch(
|
|
"app.services.wechat_login_service.user_crud.get_by_wx_openid",
|
|
return_value=bound,
|
|
):
|
|
user, err, code = resolve_wechat_login_by_openid_only(db, openid="oid-1")
|
|
assert err is None and code is None
|
|
assert user is bound
|
|
|
|
|
|
def test_resolve_openid_not_bound_asks_phone():
|
|
db = MagicMock()
|
|
with patch(
|
|
"app.services.wechat_login_service.user_crud.get_by_wx_openid",
|
|
return_value=None,
|
|
):
|
|
user, err, code = resolve_wechat_login_by_openid_only(db, openid="oid-x")
|
|
assert user is None
|
|
assert code == 400
|
|
assert "请授权手机号" in err
|
|
|
|
|
|
def test_resolve_openid_bound_but_disabled():
|
|
bound = SimpleNamespace(
|
|
id="u2",
|
|
wx_openid="oid-2",
|
|
status="0",
|
|
)
|
|
db = MagicMock()
|
|
with patch(
|
|
"app.services.wechat_login_service.user_crud.get_by_wx_openid",
|
|
return_value=bound,
|
|
):
|
|
user, err, code = resolve_wechat_login_by_openid_only(db, openid="oid-2")
|
|
assert user is None
|
|
assert code == 403
|
|
assert "停用" in err
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_wechat_login_api_silent_when_openid_bound():
|
|
bound = SimpleNamespace(
|
|
id="u1",
|
|
wx_openid="oid-1",
|
|
status="1",
|
|
wx_unionid=None,
|
|
)
|
|
with (
|
|
patch(
|
|
"app.api.mini_auth.code2session",
|
|
return_value={"openid": "oid-1", "unionid": None},
|
|
),
|
|
patch(
|
|
"app.api.mini_auth.resolve_wechat_login_by_openid_only",
|
|
return_value=(bound, None, None),
|
|
) as resolve_silent,
|
|
patch(
|
|
"app.api.mini_auth.issue_tokens_for_user",
|
|
new_callable=AsyncMock,
|
|
return_value={"accessToken": "t"},
|
|
),
|
|
):
|
|
from app.api.mini_auth import wechat_login
|
|
|
|
resp = await wechat_login({"loginCode": "lc"}, db=MagicMock())
|
|
resolve_silent.assert_called_once()
|
|
assert resp["code"] == 200
|
|
assert resp["data"]["accessToken"] == "t"
|