|
14 | 14 |
|
15 | 15 |
|
16 | 16 | class TestSnsMediaRouteWeFlowDefault(unittest.TestCase): |
17 | | - def test_route_stops_after_remote_miss_by_default(self): |
| 17 | + def test_route_prefers_local_cache_before_remote(self): |
18 | 18 | with TemporaryDirectory() as td: |
19 | 19 | account_dir = Path(td) / "acc" |
20 | 20 | account_dir.mkdir(parents=True, exist_ok=True) |
| 21 | + local_path = account_dir / "local.jpg" |
| 22 | + payload = b"\xff\xd8\xff\x00localjpeg" |
| 23 | + local_path.write_bytes(payload) |
21 | 24 |
|
22 | 25 | with mock.patch("wechat_decrypt_tool.routers.sns._resolve_account_dir", return_value=account_dir): |
23 | | - with mock.patch("wechat_decrypt_tool.routers.sns._try_fetch_and_decrypt_sns_remote", return_value=None): |
24 | | - with self.assertRaises(sns.HTTPException) as ctx: |
25 | | - asyncio.run( |
26 | | - sns.get_sns_media( |
27 | | - account="acc", |
28 | | - url="https://mmsns.qpic.cn/sns/test/0", |
29 | | - key="123", |
30 | | - token="tkn", |
31 | | - use_cache=1, |
| 26 | + with mock.patch("wechat_decrypt_tool.routers.sns._resolve_account_wxid_dir", return_value=Path(td) / "wxid"): |
| 27 | + with mock.patch("wechat_decrypt_tool.routers.sns._resolve_sns_cached_image_path", return_value=str(local_path)): |
| 28 | + with mock.patch("wechat_decrypt_tool.routers.sns._read_and_maybe_decrypt_media", return_value=(payload, "image/jpeg")): |
| 29 | + with mock.patch("wechat_decrypt_tool.routers.sns._try_fetch_and_decrypt_sns_remote") as remote: |
| 30 | + resp = asyncio.run( |
| 31 | + sns.get_sns_media( |
| 32 | + account="acc", |
| 33 | + create_time=1, |
| 34 | + width=1, |
| 35 | + height=1, |
| 36 | + url="https://mmsns.qpic.cn/sns/test/0", |
| 37 | + key="123", |
| 38 | + token="tkn", |
| 39 | + use_cache=1, |
| 40 | + ) |
| 41 | + ) |
| 42 | + |
| 43 | + remote.assert_not_called() |
| 44 | + self.assertEqual(resp.status_code, 200) |
| 45 | + self.assertEqual(resp.body, payload) |
| 46 | + self.assertEqual(resp.headers.get("X-SNS-Source"), "local-cache") |
| 47 | + |
| 48 | + def test_route_falls_back_to_remote_when_local_cache_misses(self): |
| 49 | + with TemporaryDirectory() as td: |
| 50 | + account_dir = Path(td) / "acc" |
| 51 | + account_dir.mkdir(parents=True, exist_ok=True) |
| 52 | + remote_resp = sns.Response(content=b"remote", media_type="image/jpeg") |
| 53 | + remote_resp.headers["X-SNS-Source"] = "remote-decrypt" |
| 54 | + |
| 55 | + with mock.patch("wechat_decrypt_tool.routers.sns._resolve_account_dir", return_value=account_dir): |
| 56 | + with mock.patch("wechat_decrypt_tool.routers.sns._resolve_account_wxid_dir", return_value=Path(td) / "wxid"): |
| 57 | + with mock.patch("wechat_decrypt_tool.routers.sns._resolve_sns_cached_image_path", return_value=None): |
| 58 | + with mock.patch( |
| 59 | + "wechat_decrypt_tool.routers.sns._try_fetch_and_decrypt_sns_remote", |
| 60 | + return_value=remote_resp, |
| 61 | + ) as remote: |
| 62 | + resp = asyncio.run( |
| 63 | + sns.get_sns_media( |
| 64 | + account="acc", |
| 65 | + create_time=1, |
| 66 | + width=1, |
| 67 | + height=1, |
| 68 | + url="https://mmsns.qpic.cn/sns/test/0", |
| 69 | + key="123", |
| 70 | + token="tkn", |
| 71 | + use_cache=1, |
| 72 | + ) |
32 | 73 | ) |
33 | | - ) |
34 | 74 |
|
35 | | - self.assertEqual(ctx.exception.status_code, 404) |
| 75 | + remote.assert_called_once() |
| 76 | + self.assertEqual(resp.status_code, 200) |
| 77 | + self.assertEqual(resp.body, b"remote") |
| 78 | + self.assertEqual(resp.headers.get("X-SNS-Source"), "remote-decrypt") |
36 | 79 |
|
37 | 80 |
|
38 | 81 | if __name__ == "__main__": |
|
0 commit comments