125 lines
5.6 KiB
Python
125 lines
5.6 KiB
Python
from __future__ import annotations
|
|
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
from pixiv_app.core.plugin.base import PluginParseError
|
|
from pixiv_app.core.plugin.manager import PluginManager
|
|
from pixiv_app.core import paths as app_paths
|
|
from plugins.taobao.plugin import TaobaoPlugin, _extract_item_id
|
|
|
|
|
|
PRODUCT_HTML = """
|
|
<html><head>
|
|
<meta property="og:title" content="测试商品 - 淘宝网" />
|
|
<meta property="og:image" content="https://img.alicdn.com/main.jpg" />
|
|
<meta name="description" content="商品描述" />
|
|
<script id="__NEXT_DATA__" type="application/json">
|
|
{"item":{"itemId":"123456789012","title":"测试商品","price":"99.00","images":[
|
|
"https://img.alicdn.com/a.jpg","//img.alicdn.com/b.webp"],"videoUrl":"https://cloud.video.taobao.com/demo.mp4"},
|
|
"seller":{"shopName":"樱花小店"}}
|
|
</script>
|
|
</head><body><img data-src="//img.alicdn.com/detail.jpg" /></body></html>
|
|
"""
|
|
|
|
|
|
class FakeResponse:
|
|
def __init__(self, url: str, *, text: str = "", content: bytes = b"media" * 100, content_type: str = "image/jpeg") -> None:
|
|
self.url = url
|
|
self.text = text
|
|
self.content = content
|
|
self.headers = {"content-type": content_type}
|
|
|
|
def raise_for_status(self) -> None:
|
|
return None
|
|
|
|
|
|
class FakeClient:
|
|
def __init__(self, *args, **kwargs) -> None:
|
|
self.headers = kwargs.get("headers", {})
|
|
|
|
def __enter__(self) -> "FakeClient":
|
|
return self
|
|
|
|
def __exit__(self, *args) -> None:
|
|
return None
|
|
|
|
def get(self, url: str, *args, **kwargs) -> FakeResponse:
|
|
if "item.taobao.com" in url:
|
|
return FakeResponse("https://item.taobao.com/item.htm?id=123456789012", text=PRODUCT_HTML, content_type="text/html")
|
|
content_type = "video/mp4" if url.endswith(".mp4") else "image/jpeg"
|
|
return FakeResponse(url, content_type=content_type)
|
|
|
|
|
|
class TaobaoPluginTests(unittest.TestCase):
|
|
def test_url_recognition_is_strict(self) -> None:
|
|
plugin = TaobaoPlugin()
|
|
self.assertTrue(plugin.can_handle("https://item.taobao.com/item.htm?id=123456789012"))
|
|
self.assertTrue(plugin.can_handle("https://detail.tmall.com/item.htm?id=123456789012"))
|
|
self.assertTrue(plugin.can_handle("https://m.tb.cn/h.example"))
|
|
self.assertFalse(plugin.can_handle("https://example.com/item.htm?id=123456789012"))
|
|
self.assertFalse(plugin.can_handle("https://taobao.com.evil.example/item.htm?id=1"))
|
|
|
|
def test_item_id_extraction(self) -> None:
|
|
self.assertEqual(_extract_item_id("https://item.taobao.com/item.htm?id=123456789012"), "123456789012")
|
|
|
|
def test_cookie_is_injected_from_local_file(self) -> None:
|
|
plugin = TaobaoPlugin()
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
cookie_file = Path(directory) / "cookies.txt"
|
|
cookie_file.write_text(".taobao.com\tTRUE\t/\tTRUE\t0\ttest_cookie\ttest_value\n", encoding="utf-8")
|
|
plugin.config.cookie_file = str(cookie_file)
|
|
self.assertIn("test_cookie=test_value", plugin.get_headers().get("Cookie", ""))
|
|
|
|
@patch("plugins.taobao.plugin.httpx.Client", FakeClient)
|
|
def test_parse_product_metadata_and_media(self) -> None:
|
|
resource = TaobaoPlugin().parse("https://item.taobao.com/item.htm?id=123456789012")[0]
|
|
self.assertEqual(resource.id, "123456789012")
|
|
self.assertEqual(resource.title, "测试商品")
|
|
self.assertEqual(resource.author, "樱花小店")
|
|
self.assertEqual(resource.metadata["price"], "99.00")
|
|
self.assertGreaterEqual(len(resource.files), 4)
|
|
self.assertIn("video", {item["type"] for item in resource.files})
|
|
|
|
@patch("plugins.taobao.plugin.httpx.Client", FakeClient)
|
|
def test_download_entry(self) -> None:
|
|
plugin = TaobaoPlugin()
|
|
resource = plugin.parse("https://item.taobao.com/item.htm?id=123456789012")[0]
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
paths = plugin.download(resource, Path(directory))
|
|
self.assertEqual(len(paths), len(resource.files))
|
|
self.assertTrue(all(path.is_file() for path in paths))
|
|
|
|
def test_login_challenge_is_reported(self) -> None:
|
|
plugin = TaobaoPlugin()
|
|
with patch.object(plugin, "_fetch_page", return_value=("https://item.taobao.com/item.htm?id=123456789012", "请登录并完成滑动验证")):
|
|
with self.assertRaisesRegex(PluginParseError, "登录或安全验证"):
|
|
plugin.parse("https://item.taobao.com/item.htm?id=123456789012")
|
|
|
|
def test_plugin_registration(self) -> None:
|
|
root = Path(__file__).resolve().parents[1]
|
|
manager = PluginManager([root / "plugins"])
|
|
plugin = manager.load_plugin(root / "plugins" / "taobao" / "plugin.py")
|
|
self.assertIsNotNone(plugin)
|
|
assert plugin is not None
|
|
self.assertIs(manager.get_plugin_for_url("https://detail.tmall.com/item.htm?id=123456789012"), plugin)
|
|
|
|
def test_external_plugin_root_is_scanned(self) -> None:
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
root = Path(directory)
|
|
builtin = root / "bundle" / "plugins"
|
|
external = root / "dist" / "plugins"
|
|
user = root / "user" / "plugins"
|
|
with (
|
|
patch.object(app_paths, "plugins_root", return_value=builtin),
|
|
patch.object(app_paths, "external_plugins_root", return_value=external),
|
|
patch.object(app_paths, "user_plugins_root", return_value=user),
|
|
):
|
|
self.assertEqual(app_paths.plugin_roots(), [builtin, external, user])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|