| 1 | """微信公众号 uploader 的纯逻辑与 CLI 注册验证(不依赖真实浏览器)。""" |
| 2 | import tempfile |
| 3 | import unittest |
| 4 | from pathlib import Path |
| 5 | |
| 6 | from sau_cli import build_parser |
| 7 | from uploader.wechat_uploader.article import WechatArticle, build_article_url |
| 8 | from uploader.wechat_uploader.constants import WECHAT_ARTICLE_PATH |
| 9 | from uploader.wechat_uploader.session import wechat_is_login_url, wechat_token_from_url |
| 10 | |
| 11 | |
| 12 | class WechatUrlTests(unittest.TestCase): |
| 13 | def test_token_from_valid_home_url(self): |
| 14 | url = "https://mp.weixin.qq.com/cgi-bin/home?t=home/index&token=123456789&lang=zh_CN" |
| 15 | self.assertEqual(wechat_token_from_url(url), "123456789") |
| 16 | |
| 17 | def test_token_missing_or_malformed_returns_none(self): |
| 18 | self.assertIsNone(wechat_token_from_url("https://mp.weixin.qq.com/cgi-bin/home?lang=zh_CN")) |
| 19 | self.assertIsNone(wechat_token_from_url("https://mp.weixin.qq.com/cgi-bin/home?token=abc")) |
| 20 | |
| 21 | def test_article_url_contains_current_token_and_new_params(self): |
| 22 | url = build_article_url("123456789") |
| 23 | self.assertIn(WECHAT_ARTICLE_PATH, url) |
| 24 | self.assertIn("token=123456789", url) |
| 25 | self.assertIn("isNew=1", url) |
| 26 | self.assertIn("type=77", url) |
| 27 | self.assertNotIn("token=987654321", url) |
| 28 | |
| 29 | def test_login_url_detection(self): |
| 30 | self.assertTrue(wechat_is_login_url("https://mp.weixin.qq.com/")) |
| 31 | self.assertTrue(wechat_is_login_url("https://mp.weixin.qq.com/cgi-bin/loginpage")) |
| 32 | self.assertFalse(wechat_is_login_url( |
| 33 | "https://mp.weixin.qq.com/cgi-bin/home?token=123456789")) |
| 34 | |
| 35 | |
| 36 | class WechatArticleValidationTests(unittest.TestCase): |
| 37 | def setUp(self): |
| 38 | self.folder = tempfile.TemporaryDirectory() |
| 39 | self.addCleanup(self.folder.cleanup) |
| 40 | self.root = Path(self.folder.name) |
| 41 | self.content = self.root / "article.txt" |
| 42 | self.content.write_text("正文第一段,用来验证校验逻辑。", encoding="utf-8") |
| 43 | |
| 44 | def _article(self, **kwargs): |
| 45 | options = dict(title="标题", content_file=self.content, account_file=self.root / "wechat_a.json") |
| 46 | options.update(kwargs) |
| 47 | return WechatArticle(**options) |
| 48 | |
| 49 | def test_validate_rejects_overlong_title_and_digest(self): |
| 50 | with self.assertRaises(ValueError): |
| 51 | self._article(title="字" * 65).validate_upload_args() |
| 52 | with self.assertRaises(ValueError): |
| 53 | self._article(digest="字" * 121).validate_upload_args() |
| 54 | |
| 55 | def test_validate_rejects_invalid_originality(self): |
| 56 | with self.assertRaises(ValueError): |
| 57 | self._article(originality="unknown").validate_upload_args() |
| 58 | |
| 59 | def test_validate_rejects_missing_account(self): |
| 60 | with self.assertRaises(FileNotFoundError): |
| 61 | self._article().validate_upload_args() |
| 62 | |
| 63 | |
| 64 | class WechatCliTests(unittest.TestCase): |
| 65 | def test_wechat_commands_registered(self): |
| 66 | parser = build_parser() |
| 67 | args = parser.parse_args(["wechat", "login", "--account", "creator"]) |
| 68 | self.assertEqual(args.platform, "wechat") |
| 69 | self.assertEqual(args.action, "login") |
| 70 | |
| 71 | def test_upload_article_flags(self): |
| 72 | parser = build_parser() |
| 73 | with tempfile.NamedTemporaryFile("w", suffix=".txt", encoding="utf-8", delete=False) as handle: |
| 74 | handle.write("正文") |
| 75 | content_path = handle.name |
| 76 | self.addCleanup(lambda: Path(content_path).unlink(missing_ok=True)) |
| 77 | args = parser.parse_args([ |
| 78 | "wechat", "upload-article", "--account", "creator", |
| 79 | "--title", "标题", "--content-file", content_path, |
| 80 | "--no-broadcast", "--prepare-only", "--originality", "original", |
| 81 | ]) |
| 82 | self.assertTrue(args.no_broadcast) |
| 83 | self.assertTrue(args.prepare_only) |
| 84 | self.assertEqual(args.originality, "original") |
| 85 | |
| 86 | |
| 87 | if __name__ == "__main__": |
| 88 | unittest.main() |
| 89 |