134 行
5.1 KiB
Python
134 行
5.1 KiB
Python
from __future__ import annotations
|
|
|
|
import sys
|
|
import tempfile
|
|
import types
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from tests.helpers import install_runtime_stubs
|
|
|
|
install_runtime_stubs()
|
|
|
|
from services.download import JMDownloadService # noqa: E402
|
|
from services.settings import JmProxySettings, PluginSettings # noqa: E402
|
|
|
|
|
|
class JmComicConfigTest(unittest.TestCase):
|
|
def test_default_proxy_is_not_written(self) -> None:
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
settings = PluginSettings.from_config({}, Path(temp_dir) / "data")
|
|
config = JMDownloadService(settings)._build_jmcomic_config(Path(temp_dir) / "downloads")
|
|
|
|
self.assertNotIn("postman", config["client"])
|
|
|
|
def test_proxy_modes_convert_to_jmcomic_values(self) -> None:
|
|
self.assertEqual(JmProxySettings(mode="none").to_jmcomic_value(), (True, None))
|
|
self.assertEqual(JmProxySettings(mode="system").to_jmcomic_value(), (True, "system"))
|
|
self.assertEqual(JmProxySettings(mode="clash").to_jmcomic_value(), (True, "clash"))
|
|
self.assertEqual(JmProxySettings(mode="v2ray").to_jmcomic_value(), (True, "v2ray"))
|
|
self.assertEqual(JmProxySettings(mode="custom").to_jmcomic_value(), (False, None))
|
|
self.assertEqual(
|
|
JmProxySettings(mode="custom", proxy_url="127.0.0.1:7890").to_jmcomic_value(),
|
|
(True, "127.0.0.1:7890"),
|
|
)
|
|
self.assertEqual(
|
|
JmProxySettings(
|
|
mode="custom",
|
|
http_proxy="http://127.0.0.1:7890",
|
|
https_proxy="http://127.0.0.1:7891",
|
|
).to_jmcomic_value(),
|
|
(
|
|
True,
|
|
{
|
|
"http": "http://127.0.0.1:7890",
|
|
"https": "http://127.0.0.1:7891",
|
|
},
|
|
),
|
|
)
|
|
|
|
def test_proxy_and_avs_cookie_are_written_to_postman_metadata(self) -> None:
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
settings = PluginSettings.from_config(
|
|
{
|
|
"jm_proxy": {"mode": "custom", "proxy_url": "127.0.0.1:7890"},
|
|
"jm_avs_cookie": "avs-token",
|
|
},
|
|
Path(temp_dir) / "data",
|
|
)
|
|
config = JMDownloadService(settings)._build_jmcomic_config(Path(temp_dir) / "downloads")
|
|
|
|
meta_data = config["client"]["postman"]["meta_data"]
|
|
self.assertEqual(meta_data["proxies"], "127.0.0.1:7890")
|
|
self.assertEqual(meta_data["cookies"], {"AVS": "avs-token"})
|
|
|
|
def test_account_login_is_written_as_after_init_plugin(self) -> None:
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
settings = PluginSettings.from_config(
|
|
{
|
|
"jm_login": {
|
|
"enabled": True,
|
|
"username": "user",
|
|
"password": "pass",
|
|
"impl": "html",
|
|
},
|
|
},
|
|
Path(temp_dir) / "data",
|
|
)
|
|
config = JMDownloadService(settings)._build_jmcomic_config(Path(temp_dir) / "downloads")
|
|
|
|
login_plugin = config["plugins"]["after_init"][0]
|
|
self.assertEqual(login_plugin["plugin"], "login")
|
|
self.assertEqual(
|
|
login_plugin["kwargs"],
|
|
{"username": "user", "password": "pass", "impl": "html"},
|
|
)
|
|
|
|
def test_sensitive_config_is_removed_after_option_load(self) -> None:
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
config_path = Path(temp_dir) / "jmcomic.yml"
|
|
config_path.write_text("secret", encoding="utf-8")
|
|
settings = PluginSettings.from_config(
|
|
{
|
|
"jm_login": {
|
|
"enabled": True,
|
|
"username": "user",
|
|
"password": "pass",
|
|
},
|
|
},
|
|
Path(temp_dir) / "data",
|
|
)
|
|
service = JMDownloadService(settings)
|
|
old_jmcomic = sys.modules.get("jmcomic")
|
|
fake_jmcomic = types.ModuleType("jmcomic")
|
|
|
|
class Album:
|
|
title = "Title"
|
|
|
|
class Result:
|
|
album = Album()
|
|
|
|
def create_option_by_file(path: str) -> object:
|
|
self.assertTrue(Path(path).exists())
|
|
return object()
|
|
|
|
def download_album(comic_id: str, option: object, check_exception: bool) -> Result:
|
|
self.assertEqual(comic_id, "123456")
|
|
self.assertTrue(check_exception)
|
|
self.assertFalse(config_path.exists())
|
|
return Result()
|
|
|
|
fake_jmcomic.create_option_by_file = create_option_by_file
|
|
fake_jmcomic.download_album = download_album
|
|
sys.modules["jmcomic"] = fake_jmcomic
|
|
try:
|
|
self.assertEqual(service._download_sync("123456", config_path), "Title")
|
|
finally:
|
|
if old_jmcomic is None:
|
|
sys.modules.pop("jmcomic", None)
|
|
else:
|
|
sys.modules["jmcomic"] = old_jmcomic
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|