CaiWuXiTong/scripts/cmb-mail/test_fetch_daily.py
shanshi 39769abd26 feat(财务台账): 接入招商银行邮件账单 (#1392)
接入招商银行邮件账单下载、整目录导入和余额同步。
2026-11-25 10:11:08 +08:00

54 lines
1.7 KiB
Python

from __future__ import annotations
import io
import tempfile
import unittest
import zipfile
from pathlib import Path
from fetch_daily import CmbMailError, download_link_from_body, safe_extract
class FetchDailyTest(unittest.TestCase):
def test_extracts_download_link_and_date_range(self) -> None:
body = """
<p>链接:
<a href="s3gw.cmbimg.com/example/file.zip?filename=%E8%B4%A6%E5%8D%95-20261101-20261124.zip">
点击此处下载文件
</a>
</p>
"""
link = download_link_from_body(body)
self.assertIsNotNone(link)
self.assertEqual("20261101", link.date_start)
self.assertEqual("20261124", link.date_end)
def test_rejects_unsafe_zip_member(self) -> None:
with tempfile.TemporaryDirectory() as temporary:
root = Path(temporary)
archive = root / "statement.zip"
with zipfile.ZipFile(archive, "w") as output:
output.writestr("../outside.xlsx", b"bad")
with self.assertRaises(CmbMailError):
safe_extract(archive, root / "contents")
def test_extracts_supported_statement_file(self) -> None:
with tempfile.TemporaryDirectory() as temporary:
root = Path(temporary)
archive = root / "statement.zip"
with zipfile.ZipFile(archive, "w") as output:
output.writestr("企业账户交易明细.xlsx", b"fixture")
files = safe_extract(archive, root / "contents")
self.assertEqual(1, len(files))
self.assertEqual("企业账户交易明细.xlsx", files[0]["name"])
self.assertTrue(Path(files[0]["path"]).is_file())
if __name__ == "__main__":
unittest.main()