Files
cmshoppe/tests/test_editor_login.py
T

134 lines
4.0 KiB
Python
Raw Normal View History

2026-06-27 09:42:41 +08:00
import os
import sys
import unittest
from unittest import mock
sys.path.insert(0, os.path.dirname(__file__))
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_portal_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/portal/",
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/portal/",
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()