Files
cmshoppe/tests/test_editor_login.py
T
chengma 8fe979942c fix: 调整登录检测入口URL
无 Shopee tab 时,登录检测改为打开卖家中心根地址 https://<region_host>/,默认 https://seller.shopee.tw/。

商品编辑 URL 仍保持 /portal/product 路径不变;更新登录检测单测与 API/架构/当前状态/progress 文档。
2026-06-27 12:05:33 +08:00

139 lines
4.1 KiB
Python

import os
import sys
import unittest
from unittest import mock
sys.path.insert(0, os.path.dirname(__file__))
from _helpers import REPO_ROOT
if str(REPO_ROOT) not in sys.path:
sys.path.insert(0, str(REPO_ROOT))
from app import editor
class FakeCDP:
instances = []
def __init__(self, ws, url="", cookies=None):
self.ws = ws
self.url = url
self.cookies = cookies or []
self.closed = False
FakeCDP.instances.append(self)
def val(self, expr):
if expr == "location.href":
return self.url
return None
def send(self, method, params=None):
if method == "Network.getAllCookies":
return {"cookies": self.cookies}
return {}
def close(self):
self.closed = True
def cookie(name, domain=".shopee.tw"):
return {"name": name, "domain": domain}
class EditorLoginTests(unittest.TestCase):
def setUp(self):
FakeCDP.instances = []
def patch_cdp(self, url="", cookies=None):
def factory(ws):
return FakeCDP(ws, url=url, cookies=cookies)
return mock.patch("app.editor.CDP", side_effect=factory)
def test_login_status_false_for_login_page_url(self):
with mock.patch(
"app.editor.http_get",
return_value=[
{
"type": "page",
"url": "https://seller.shopee.tw/account/signin",
"webSocketDebuggerUrl": "ws-login",
}
],
):
status = editor.login_status({"debug_port": 9222}, timeout=0)
self.assertFalse(status["logged_in"])
self.assertEqual("LOGIN_PAGE", status["reason"])
self.assertEqual([], FakeCDP.instances)
def test_login_status_true_with_session_cookie(self):
with mock.patch(
"app.editor.http_get",
return_value=[
{
"type": "page",
"url": "https://seller.shopee.tw/portal/",
"webSocketDebuggerUrl": "ws-portal",
}
],
), self.patch_cdp(
url="https://seller.shopee.tw/portal/",
cookies=[cookie("SPC_ST"), cookie("csrftoken")],
):
status = editor.login_status({"debug_port": 9222}, timeout=1)
self.assertTrue(status["logged_in"])
self.assertIsNone(status["reason"])
self.assertIn("SPC_ST", status["cookie_names"])
self.assertTrue(FakeCDP.instances[0].closed)
def test_login_status_false_without_session_cookie(self):
with mock.patch(
"app.editor.http_get",
return_value=[
{
"type": "page",
"url": "https://seller.shopee.tw/portal/",
"webSocketDebuggerUrl": "ws-portal",
}
],
), self.patch_cdp(
url="https://seller.shopee.tw/portal/",
cookies=[cookie("csrftoken")],
):
status = editor.login_status({"debug_port": 9222}, timeout=0)
self.assertFalse(status["logged_in"])
self.assertEqual("NO_SESSION_COOKIE", status["reason"])
def test_login_status_opens_seller_home_when_no_shopee_page(self):
with mock.patch("app.editor.http_get", return_value=[]), mock.patch(
"app.editor.create_tab", return_value="ws-new"
) as create_tab, self.patch_cdp(
url="https://seller.shopee.tw/",
cookies=[cookie("SPC_U")],
):
status = editor.login_status(
{"debug_port": 9222, "region_host": "seller.shopee.tw"},
timeout=1,
)
self.assertTrue(status["logged_in"])
create_tab.assert_called_once_with(
"https://seller.shopee.tw/",
host="127.0.0.1:9222",
)
def test_is_logged_in_returns_boolean(self):
with mock.patch(
"app.editor.login_status",
return_value={"logged_in": True},
):
self.assertTrue(editor.is_logged_in({"debug_port": 9222}))
if __name__ == "__main__":
unittest.main()