| 1 | import sau_commands.facebook |
| 2 | import asyncio |
| 3 | import tempfile |
| 4 | import unittest |
| 5 | from pathlib import Path |
| 6 | from unittest.mock import AsyncMock, patch |
| 7 | |
| 8 | import sau_cli |
| 9 | from patchright.async_api import async_playwright |
| 10 | |
| 11 | from uploader.meta_uploader.facebook_uploader.main import FacebookVideo, _is_logged_out, facebook_cookie_auth |
| 12 | |
| 13 | |
| 14 | class FacebookCaptionTests(unittest.TestCase): |
| 15 | def test_caption_merges_title_description_and_tags(self): |
| 16 | uploader = FacebookVideo( |
| 17 | title="标题", |
| 18 | file_path="video.mp4", |
| 19 | description="正文", |
| 20 | tags=["#一个", " 两个 "], |
| 21 | account_file="account.json", |
| 22 | ) |
| 23 | |
| 24 | self.assertEqual(uploader._caption(), "标题\n\n正文\n\n#一个 #两个") |
| 25 | |
| 26 | def test_caption_omits_empty_optional_fields(self): |
| 27 | uploader = FacebookVideo( |
| 28 | title="标题", |
| 29 | file_path="video.mp4", |
| 30 | description="", |
| 31 | tags=[], |
| 32 | account_file="account.json", |
| 33 | ) |
| 34 | |
| 35 | self.assertEqual(uploader._caption(), "标题") |
| 36 | |
| 37 | |
| 38 | class FacebookPageStateTests(unittest.TestCase): |
| 39 | def test_logged_out_marker_is_based_on_login_form(self): |
| 40 | async def scenario(): |
| 41 | async with async_playwright() as playwright: |
| 42 | browser = await playwright.chromium.launch(headless=True, channel="chrome") |
| 43 | page = await browser.new_page() |
| 44 | await page.set_content('<h1>登录 Facebook</h1><input name="email">') |
| 45 | result = await _is_logged_out(page) |
| 46 | await browser.close() |
| 47 | return result |
| 48 | |
| 49 | self.assertTrue(asyncio.run(scenario())) |
| 50 | |
| 51 | def test_logged_in_page_without_login_form_is_not_marked_out(self): |
| 52 | async def scenario(): |
| 53 | async with async_playwright() as playwright: |
| 54 | browser = await playwright.chromium.launch(headless=True, channel="chrome") |
| 55 | page = await browser.new_page() |
| 56 | await page.set_content( |
| 57 | '<main><input type="file" accept="image/*,video/*" hidden></main>' |
| 58 | ) |
| 59 | result = await _is_logged_out(page) |
| 60 | await browser.close() |
| 61 | return result |
| 62 | |
| 63 | self.assertFalse(asyncio.run(scenario())) |
| 64 | |
| 65 | def test_two_step_verification_is_not_treated_as_logged_in(self): |
| 66 | async def scenario(): |
| 67 | class PageStub: |
| 68 | url = "https://www.facebook.com/two_step_verification/authentication/" |
| 69 | |
| 70 | return await _is_logged_out(PageStub()) |
| 71 | |
| 72 | self.assertTrue(asyncio.run(scenario())) |
| 73 | |
| 74 | def test_blank_facebook_page_is_not_treated_as_logged_in(self): |
| 75 | async def scenario(): |
| 76 | async with async_playwright() as playwright: |
| 77 | browser = await playwright.chromium.launch(headless=True, channel="chrome") |
| 78 | page = await browser.new_page() |
| 79 | await page.set_content("<body></body>") |
| 80 | result = await _is_logged_out(page) |
| 81 | await browser.close() |
| 82 | return result |
| 83 | |
| 84 | self.assertTrue(asyncio.run(scenario())) |
| 85 | |
| 86 | |
| 87 | class FacebookCliTests(unittest.TestCase): |
| 88 | def test_parser_exposes_proxy_and_headed_login(self): |
| 89 | args = sau_cli.build_parser().parse_args( |
| 90 | ["facebook", "login", "--account", "creator", "--proxy", "http://proxy.example:8080"] |
| 91 | ) |
| 92 | self.assertEqual(args.proxy, "http://proxy.example:8080") |
| 93 | self.assertFalse(args.headless) |
| 94 | |
| 95 | def test_dispatch_upload_forwards_facebook_request(self): |
| 96 | args = sau_cli.build_parser().parse_args( |
| 97 | [ |
| 98 | "facebook", |
| 99 | "upload-video", |
| 100 | "--account", |
| 101 | "creator", |
| 102 | "--file", |
| 103 | __file__, |
| 104 | "--title", |
| 105 | "标题", |
| 106 | "--desc", |
| 107 | "正文", |
| 108 | "--tags", |
| 109 | "一个,两个", |
| 110 | "--proxy", |
| 111 | "http://proxy.example:8080", |
| 112 | ] |
| 113 | ) |
| 114 | with patch("sau_commands.facebook.upload_facebook_video", new=AsyncMock()) as mock_upload: |
| 115 | result = asyncio.run(sau_cli.dispatch(args)) |
| 116 | |
| 117 | self.assertEqual(result, 0) |
| 118 | request = mock_upload.await_args.args[0] |
| 119 | self.assertEqual(request.title, "标题") |
| 120 | self.assertEqual(request.description, "正文") |
| 121 | self.assertEqual(request.tags, ["一个", "两个"]) |
| 122 | self.assertEqual(request.proxy, "http://proxy.example:8080") |
| 123 | |
| 124 | |
| 125 | class FacebookStorageStateTests(unittest.TestCase): |
| 126 | def test_missing_storage_state_is_invalid_without_network(self): |
| 127 | with tempfile.TemporaryDirectory() as directory: |
| 128 | result = asyncio.run( |
| 129 | facebook_cookie_auth(Path(directory) / "missing.json", proxy="") |
| 130 | ) |
| 131 | self.assertFalse(result) |
| 132 | |
| 133 | |
| 134 | if __name__ == "__main__": |
| 135 | unittest.main() |
| 136 |