- 新增 chrome.create_shortcut 使用 PowerShell WScript.Shell 生成 .lnk - 快捷方式参数复用 Chrome 启动参数,包含调试端口、allow-origins 和账号 user-data-dir - 在账号服务层和 Tab④ 增加快捷方式入口,不包含密码或密钥 - 补充 chrome/accounts/gui 测试并同步任务、API、路由、当前状态和进度文档
169 lines
6.3 KiB
Python
169 lines
6.3 KiB
Python
import os
|
|
import unittest
|
|
from unittest import mock
|
|
|
|
from _helpers import TempDirMixin
|
|
|
|
from app import accounts
|
|
from app import config as account_config
|
|
|
|
|
|
class AccountsTests(TempDirMixin, unittest.TestCase):
|
|
def make_config(self, temp_dir):
|
|
return {
|
|
"chrome_path": "chrome.exe",
|
|
"user_data_root": os.path.join(temp_dir, "chrome_user_data_dir"),
|
|
"db_path": os.path.join(temp_dir, "cmshopee.db"),
|
|
"debug_port_range": [9222, 9224],
|
|
}
|
|
|
|
def test_create_account_stores_record_and_creates_user_data_dir(self):
|
|
with self.make_temp_dir() as temp_dir:
|
|
cfg = self.make_config(temp_dir)
|
|
account = accounts.create_account(
|
|
"主店",
|
|
"alias",
|
|
"https://seller.shopee.tw/portal/",
|
|
9222,
|
|
password="secret",
|
|
note="备注",
|
|
config=cfg,
|
|
)
|
|
|
|
self.assertEqual("主店", account.account_name)
|
|
self.assertEqual("alias", account.alias)
|
|
self.assertEqual("seller.shopee.tw", account.region_host)
|
|
self.assertEqual("alias_cdb6fdbe", account.slug)
|
|
self.assertTrue(os.path.isdir(account.user_data_dir))
|
|
self.assertEqual("******", accounts.mask_password(account.password))
|
|
self.assertEqual([account], accounts.list_accounts(config=cfg))
|
|
|
|
self.assert_removed(temp_dir)
|
|
|
|
def test_update_account_can_change_alias_and_rebuild_directory(self):
|
|
with self.make_temp_dir() as temp_dir:
|
|
cfg = self.make_config(temp_dir)
|
|
accounts.create_account("主店", "alias", debug_port=9222, config=cfg)
|
|
|
|
updated = accounts.update_account(
|
|
"alias",
|
|
"新主店",
|
|
"new alias",
|
|
"seller.shopee.tw",
|
|
9333,
|
|
password="new-secret",
|
|
note="新备注",
|
|
config=cfg,
|
|
)
|
|
|
|
self.assertEqual("new alias", updated.alias)
|
|
self.assertEqual(account_config.make_slug("new alias"), updated.slug)
|
|
self.assertEqual(9333, updated.debug_port)
|
|
self.assertTrue(os.path.isdir(updated.user_data_dir))
|
|
with self.assertRaises(accounts.AccountError):
|
|
accounts.get_account("alias", config=cfg)
|
|
self.assertEqual("new-secret", accounts.get_account("new alias", config=cfg).password)
|
|
|
|
self.assert_removed(temp_dir)
|
|
|
|
def test_next_debug_port_skips_existing_accounts(self):
|
|
with self.make_temp_dir() as temp_dir:
|
|
cfg = self.make_config(temp_dir)
|
|
accounts.create_account("店1", "a1", debug_port=9222, config=cfg)
|
|
accounts.create_account("店2", "a2", debug_port=9223, config=cfg)
|
|
|
|
self.assertEqual(9224, accounts.next_debug_port(config=cfg))
|
|
|
|
self.assert_removed(temp_dir)
|
|
|
|
def test_launch_for_login_only_launches_chrome(self):
|
|
with self.make_temp_dir() as temp_dir:
|
|
cfg = self.make_config(temp_dir)
|
|
account = accounts.create_account("主店", "alias", debug_port=9222, config=cfg)
|
|
process = object()
|
|
|
|
with mock.patch("app.accounts.chrome.launch_chrome", return_value=process) as launch:
|
|
result = accounts.launch_for_login("alias", config=cfg)
|
|
|
|
self.assertIs(process, result)
|
|
launch.assert_called_once_with(account, config=cfg)
|
|
|
|
self.assert_removed(temp_dir)
|
|
|
|
def test_create_shortcut_uses_account_record(self):
|
|
with self.make_temp_dir() as temp_dir:
|
|
cfg = self.make_config(temp_dir)
|
|
account = accounts.create_account("主店", "alias", debug_port=9222, config=cfg)
|
|
shortcut_path = os.path.join(temp_dir, "Desktop", "主店.lnk")
|
|
|
|
with mock.patch(
|
|
"app.accounts.chrome.create_shortcut",
|
|
return_value=shortcut_path,
|
|
) as create_shortcut:
|
|
result = accounts.create_shortcut(
|
|
"alias",
|
|
shortcut_path=shortcut_path,
|
|
config=cfg,
|
|
)
|
|
|
|
self.assertEqual(shortcut_path, result)
|
|
create_shortcut.assert_called_once_with(
|
|
account,
|
|
shortcut_path=shortcut_path,
|
|
desktop_dir=None,
|
|
config=cfg,
|
|
)
|
|
|
|
self.assert_removed(temp_dir)
|
|
|
|
def test_detect_login_updates_last_login_at_when_logged_in(self):
|
|
with self.make_temp_dir() as temp_dir:
|
|
cfg = self.make_config(temp_dir)
|
|
accounts.create_account("主店", "alias", debug_port=9222, config=cfg)
|
|
|
|
with mock.patch(
|
|
"app.accounts.editor.login_status",
|
|
return_value={"logged_in": True, "reason": None},
|
|
) as login_status:
|
|
status = accounts.detect_login("alias", timeout=1, config=cfg)
|
|
|
|
self.assertTrue(status["logged_in"])
|
|
login_status.assert_called_once()
|
|
self.assertIsNotNone(accounts.get_account("alias", config=cfg).last_login_at)
|
|
|
|
self.assert_removed(temp_dir)
|
|
|
|
def test_duplicate_alias_raises_clear_error(self):
|
|
with self.make_temp_dir() as temp_dir:
|
|
cfg = self.make_config(temp_dir)
|
|
accounts.create_account("店1", "alias", debug_port=9222, config=cfg)
|
|
|
|
with self.assertRaises(accounts.AccountError):
|
|
accounts.create_account("店2", "alias", debug_port=9223, config=cfg)
|
|
|
|
self.assert_removed(temp_dir)
|
|
|
|
def test_duplicate_debug_port_raises_clear_error(self):
|
|
with self.make_temp_dir() as temp_dir:
|
|
cfg = self.make_config(temp_dir)
|
|
accounts.create_account("店1", "a1", debug_port=9222, config=cfg)
|
|
accounts.create_account("店2", "a2", debug_port=9223, config=cfg)
|
|
|
|
with self.assertRaises(accounts.AccountError):
|
|
accounts.create_account("店3", "a3", debug_port=9222, config=cfg)
|
|
with self.assertRaises(accounts.AccountError):
|
|
accounts.update_account(
|
|
"a2",
|
|
"店2",
|
|
"a2",
|
|
"seller.shopee.tw",
|
|
9222,
|
|
config=cfg,
|
|
)
|
|
|
|
self.assert_removed(temp_dir)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|