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 = """
链接: 点击此处下载文件
""" 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()