|
|
@@ -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
|