@@ -2728,9 +2728,14 @@ def test_medium_body_flags_kb(self):
27282728 self .assertNotIn ("降级" , out .message ) # 没试过降级不冒功
27292729
27302730 def test_small_body_no_size_section (self ):
2731+ import tempfile
2732+ from pathlib import Path
2733+ from unittest .mock import patch
27312734 from wbspy .upstream import annotate_denial
2732- out = annotate_denial (self ._err (), "短消息" , demoted = False )
2733- self .assertNotIn ("请求体" , out .message )
2735+ with tempfile .TemporaryDirectory () as tmp : # 落盘到临时目录,不污染 home
2736+ with patch ("wbspy.upstream.DENIALS_DIR" , Path (tmp )):
2737+ out = annotate_denial (self ._err (), "短消息" , demoted = False )
2738+ self .assertNotIn ("KB" , out .message )
27342739 self .assertIn ("新开会话" , out .message )
27352740
27362741 def test_chat_integrates_annotation (self ):
@@ -3251,3 +3256,67 @@ def test_lane_wipeout_alert(self):
32513256 self .assertEqual (wipe [0 ]["level" ], "error" )
32523257 self .assertIn ("重新授权" , wipe [0 ]["action" ])
32533258 self .assertEqual (out ["alive" ], 0 )
3259+
3260+
3261+ class TestDenialCapture (unittest .TestCase ):
3262+ """11128 抓现行:被拒请求体落盘 + 面板端点可查看/下载/删除。"""
3263+
3264+ def test_capture_writes_and_prunes (self ):
3265+ """落盘 JSON、仅保留最近 DENIALS_KEEP 份。"""
3266+ import tempfile
3267+ from pathlib import Path
3268+ from unittest .mock import patch
3269+ from wbspy import upstream
3270+ with tempfile .TemporaryDirectory () as tmp :
3271+ with patch .object (upstream , "DENIALS_DIR" , Path (tmp )):
3272+ name = upstream .capture_denial_body ('{"messages":[]}' , 403 )
3273+ self .assertTrue (name and "-403-" in name )
3274+ self .assertEqual ((Path (tmp ) / name ).read_text (encoding = "utf-8" ),
3275+ '{"messages":[]}' )
3276+ for i in range (upstream .DENIALS_KEEP + 3 ):
3277+ upstream .capture_denial_body (f'{{"n":{ i } }}' , 403 )
3278+ time .sleep (0.01 )
3279+ files = sorted (Path (tmp ).glob ("*.json" ))
3280+ self .assertEqual (len (files ), upstream .DENIALS_KEEP )
3281+ # 落盘失败静默:DIR 指向文件
3282+ with patch .object (upstream , "DENIALS_DIR" , Path (tmp ) / "f" ):
3283+ (Path (tmp ) / "f" ).write_text ("x" )
3284+ self .assertIsNone (upstream .capture_denial_body ("{}" , 403 ))
3285+
3286+ def test_annotate_mentions_saved_file (self ):
3287+ """annotate_denial 附上留存文件名,指引面板下载。"""
3288+ import tempfile
3289+ from pathlib import Path
3290+ from unittest .mock import patch
3291+ from wbspy import upstream
3292+ with tempfile .TemporaryDirectory () as tmp :
3293+ with patch .object (upstream , "DENIALS_DIR" , Path (tmp )):
3294+ err = upstream .annotate_denial (
3295+ upstream .UpstreamError (403 , "11128" , "denied" ), "x" * 100 , False )
3296+ self .assertIn ("~/.wbspy/denials/" , err .message )
3297+ self .assertTrue ((Path (tmp )).is_dir ())
3298+
3299+ def test_webui_denial_endpoints (self ):
3300+ """/api/denials 列表 + 下载 + 路径穿越拒绝。"""
3301+ import tempfile
3302+ from pathlib import Path
3303+ from unittest .mock import patch
3304+ from fastapi .testclient import TestClient
3305+ from wbspy import upstream
3306+ from wbspy .pool import AccountPool
3307+ from wbspy .webui import create_web_app
3308+ with tempfile .TemporaryDirectory () as tmp :
3309+ (Path (tmp ) / "20261108-114500-403.json" ).write_text ('{"a":1}' )
3310+ with patch .object (upstream , "DENIALS_DIR" , Path (tmp )):
3311+ c = TestClient (create_web_app (AccountPool ([]), api_key = "" ))
3312+ rows = c .get ("/api/denials" ).json ()["data" ]["items" ]
3313+ self .assertEqual (len (rows ), 1 )
3314+ self .assertIn ("403" , rows [0 ]["name" ])
3315+ r = c .get (f"/api/denials/{ rows [0 ]['name' ]} " )
3316+ self .assertEqual (r .status_code , 200 )
3317+ self .assertEqual (r .json (), {"a" : 1 })
3318+ self .assertEqual (c .get ("/api/denials/..%2Fetc" ).status_code , 404 )
3319+ self .assertEqual (c .get ("/api/denials/nope.json" ).status_code , 404 )
3320+ self .assertEqual (
3321+ c .delete (f"/api/denials/{ rows [0 ]['name' ]} " ).status_code , 200 )
3322+ self .assertEqual (c .get ("/api/denials" ).json ()["data" ]["items" ], [])
0 commit comments