feat: add WeChat unlimited mini program code generator

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
zlwuandCursor committed 2026-09-18 13:48:54 +08:00
1 parent a9b6a73823
commit 9d4a11339b
2 files changed
+92

No files matched your search

+47
View File
@@ -0,0 +1,47 @@
from unittest.mock import MagicMock, patch
from app.services.wechat_mini_auth import get_unlimited_wxacode, WechatMiniAuthError
def test_get_unlimited_wxacode_returns_png_bytes():
token_resp = MagicMock()
token_resp.raise_for_status = MagicMock()
token_resp.json.return_value = {"access_token": "tok"}
code_resp = MagicMock()
code_resp.raise_for_status = MagicMock()
code_resp.headers = {"Content-Type": "image/jpeg"}
code_resp.content = b"\xff\xd8fake"
with patch("app.services.wechat_mini_auth._require_mini_credentials", return_value=("app", "sec")):
with patch("app.services.wechat_mini_auth.httpx.Client") as Client:
client = MagicMock()
Client.return_value.__enter__.return_value = client
client.get.return_value = token_resp
client.post.return_value = code_resp
raw = get_unlimited_wxacode(scene="abc", page="pages/login/web-qr/index")
assert raw.startswith(b"\xff\xd8")
def test_get_unlimited_wxacode_json_error():
token_resp = MagicMock()
token_resp.raise_for_status = MagicMock()
token_resp.json.return_value = {"access_token": "tok"}
code_resp = MagicMock()
code_resp.raise_for_status = MagicMock()
code_resp.headers = {"Content-Type": "application/json"}
code_resp.content = b'{"errcode":40001,"errmsg":"fail"}'
code_resp.json.return_value = {"errcode": 40001, "errmsg": "fail"}
with patch("app.services.wechat_mini_auth._require_mini_credentials", return_value=("app", "sec")):
with patch("app.services.wechat_mini_auth.httpx.Client") as Client:
client = MagicMock()
Client.return_value.__enter__.return_value = client
client.get.return_value = token_resp
client.post.return_value = code_resp
try:
get_unlimited_wxacode(scene="abc", page="pages/login/web-qr/index")
assert False, "expected error"
except WechatMiniAuthError:
pass