Skip to content

Commit 95b1a64

Browse files
committed
test(sns-media): 补充朋友圈图片缓存优先回归测试
- 覆盖本地缓存命中时不调用远程下载 - 覆盖本地缓存未命中时再回退到远程解密
1 parent e76c5d7 commit 95b1a64

1 file changed

Lines changed: 55 additions & 12 deletions

File tree

tests/test_sns_media_route_weflow_default.py

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,68 @@
1414

1515

1616
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):
1818
with TemporaryDirectory() as td:
1919
account_dir = Path(td) / "acc"
2020
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)
2124

2225
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+
)
3273
)
33-
)
3474

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")
3679

3780

3881
if __name__ == "__main__":

0 commit comments

Comments
 (0)