| 1 | from auth import CookieManager |
| 2 | |
| 3 | |
| 4 | def test_cookie_manager_validation_requires_all_keys(tmp_path): |
| 5 | cookie_file = tmp_path / ".cookies.json" |
| 6 | manager = CookieManager(str(cookie_file)) |
| 7 | |
| 8 | manager.set_cookies({"msToken": "token", "ttwid": "id"}) |
| 9 | assert manager.validate_cookies() is False |
| 10 | |
| 11 | manager.set_cookies( |
| 12 | { |
| 13 | "msToken": "token", |
| 14 | "ttwid": "id", |
| 15 | "odin_tt": "odin", |
| 16 | "passport_csrf_token": "csrf", |
| 17 | } |
| 18 | ) |
| 19 | |
| 20 | assert manager.validate_cookies() is True |
| 21 | |
| 22 | |
| 23 | def test_cookie_manager_validation_allows_missing_ms_token(tmp_path): |
| 24 | cookie_file = tmp_path / ".cookies.json" |
| 25 | manager = CookieManager(str(cookie_file)) |
| 26 | |
| 27 | manager.set_cookies( |
| 28 | { |
| 29 | "ttwid": "id", |
| 30 | "odin_tt": "odin", |
| 31 | "passport_csrf_token": "csrf", |
| 32 | } |
| 33 | ) |
| 34 | |
| 35 | assert manager.validate_cookies() is True |
| 36 | |
| 37 | |
| 38 | def test_cookie_manager_filters_illegal_cookie_keys(tmp_path): |
| 39 | cookie_file = tmp_path / ".cookies.json" |
| 40 | manager = CookieManager(str(cookie_file)) |
| 41 | |
| 42 | manager.set_cookies( |
| 43 | { |
| 44 | "": "douyin.com", |
| 45 | "ttwid": "id", |
| 46 | } |
| 47 | ) |
| 48 | |
| 49 | cookies = manager.get_cookies() |
| 50 | assert "" not in cookies |
| 51 | assert cookies["ttwid"] == "id" |
| 52 |