| 1 | """删除属于不可恢复操作,重点验证身份不匹配时不能走到删除动作。""" |
| 2 | |
| 3 | import asyncio |
| 4 | import unittest |
| 5 | |
| 6 | from patchright.async_api import async_playwright |
| 7 | |
| 8 | from uploader.meta_uploader.facebook_uploader.posts import facebook_post_id, find_facebook_post |
| 9 | from uploader.meta_uploader.facebook_page_uploader.posts import open_meta_post |
| 10 | |
| 11 | |
| 12 | class FacebookPostIdentityTests(unittest.TestCase): |
| 13 | def test_accepts_reel_and_photo_post_permalink(self): |
| 14 | self.assertEqual(facebook_post_id('https://www.facebook.com/reel/123/'), '123') |
| 15 | self.assertEqual(facebook_post_id('https://www.facebook.com/permalink.php?story_fbid=pfbidABC123&id=42'), 'pfbidABC123') |
| 16 | |
| 17 | def test_rejects_profile_external_site_and_ambiguous_id(self): |
| 18 | for url in ('https://www.facebook.com/profile.php?id=42', 'https://facebook.com.evil/reel/123/', |
| 19 | 'http://www.facebook.com/reel/123/', 'https://www.facebook.com/permalink.php?story_fbid=1&story_fbid=2'): |
| 20 | with self.subTest(url=url), self.assertRaises(ValueError): |
| 21 | facebook_post_id(url) |
| 22 | |
| 23 | def test_same_caption_cannot_select_another_post(self): |
| 24 | async def scenario(): |
| 25 | async with async_playwright() as p: |
| 26 | browser = await p.chromium.launch(channel='chrome', headless=True) |
| 27 | context = await browser.new_context() |
| 28 | await context.add_cookies([{'name': 'c_user', 'value': '42', 'domain': '.facebook.com', 'path': '/'}]) |
| 29 | page = await context.new_page() |
| 30 | html = '''<article role="article"><span>same caption</span><a href="/profile.php?id=42">Author</a><a href="/reel/111/">old</a></article> |
| 31 | <article role="article"><span>same caption</span><a href="/profile.php?id=42">Author</a><a href="/reel/222/">target</a></article>''' |
| 32 | await page.route('https://www.facebook.com/**', lambda route: route.fulfill(body=html, content_type='text/html')) |
| 33 | try: |
| 34 | article = await find_facebook_post(page, post_url='https://www.facebook.com/reel/222/', owner_id='42', expected_text='same caption') |
| 35 | self.assertIn('target', await article.inner_text()) |
| 36 | self.assertNotIn('old', await article.inner_text()) |
| 37 | with self.assertRaisesRegex(RuntimeError, '登录用户'): |
| 38 | await find_facebook_post(page, post_url='https://www.facebook.com/reel/222/', owner_id='99', expected_text='same caption') |
| 39 | finally: |
| 40 | await browser.close() |
| 41 | asyncio.run(scenario()) |
| 42 | |
| 43 | |
| 44 | class MetaPostIdentityTests(unittest.TestCase): |
| 45 | def test_page_content_number_mismatch_stops_before_delete(self): |
| 46 | """即使配文和 Page 正确,也必须拒绝编号不同的原作品。""" |
| 47 | async def scenario(): |
| 48 | async with async_playwright() as p: |
| 49 | browser = await p.chromium.launch(channel='chrome', headless=True) |
| 50 | page = await browser.new_page() |
| 51 | html = '''<input placeholder="按编号或配文搜索"> |
| 52 | <div role="row">Page name |
| 53 | <div role="button" aria-pressed="false" onclick="document.getElementById('detail').hidden=false">caption</div> |
| 54 | </div><div id="detail" hidden>编号:123</div>''' |
| 55 | await page.route('https://business.facebook.com/**', lambda route: route.fulfill(body=html, content_type='text/html; charset=utf-8')) |
| 56 | try: |
| 57 | with self.assertRaisesRegex(RuntimeError, '编号不匹配'): |
| 58 | await open_meta_post(page, list_url='https://business.facebook.com/latest/posts/scheduled_posts?asset_id=42', |
| 59 | page_name='Page name', expected_text='caption', content_id='999') |
| 60 | finally: |
| 61 | await browser.close() |
| 62 | asyncio.run(scenario()) |
| 63 |