feat(admin): 报表反馈行内附图元数据与图片代理接口
This commit is contained in:
1 parent
6eb4c76beb
commit
b64ed9b792
3 files changed
+150
-2
No files matched your search
@@ -184,3 +184,71 @@ def test_submit_text_only_keeps_legacy(app, client, user_headers):
|
||||
with app.app_context():
|
||||
assert GeneralFeedbackImage.query.count() == 0
|
||||
assert GeneralFeedback.query.count() == 1
|
||||
|
||||
|
||||
def test_report_rows_include_images(app, client, user_headers, admin_headers):
|
||||
"""报表行含 images 元数据;无图行 images==[]。"""
|
||||
with app.app_context():
|
||||
u = db.session.query(User).filter_by(username="fbuser").first()
|
||||
a1 = _make_asset(u.id)
|
||||
# asset_no 须在 app context 内取出:context 弹出后 ORM 实例已脱管,属性不可再读
|
||||
asset_no = a1.asset_no
|
||||
resp = client.post("/api/user/feedback", headers=user_headers, json=_submit_payload(
|
||||
content="带图反馈", image_asset_nos=[asset_no],
|
||||
))
|
||||
assert resp.status_code == 200
|
||||
with_image_id = resp.get_json()["id"]
|
||||
resp = client.post("/api/user/feedback", headers=user_headers, json=_submit_payload(content="纯文本反馈"))
|
||||
assert resp.status_code == 200
|
||||
text_only_id = resp.get_json()["id"]
|
||||
|
||||
resp = client.get("/api/admin/reports/general-feedbacks", headers=admin_headers)
|
||||
assert resp.status_code == 200
|
||||
rows = resp.get_json()
|
||||
rows_by_id = {row["id"]: row for row in rows}
|
||||
# 有图行:images[0] 键集合恰为 5 个元数据键,asset_no 与提交资产一致
|
||||
with_image_row = rows_by_id[with_image_id]
|
||||
assert len(with_image_row["images"]) == 1
|
||||
assert set(with_image_row["images"][0].keys()) == {
|
||||
"image_id", "asset_no", "original_filename", "file_size", "sort_order",
|
||||
}
|
||||
assert with_image_row["images"][0]["asset_no"] == asset_no
|
||||
# 无图行:images 置空列表而非缺键
|
||||
assert rows_by_id[text_only_id]["images"] == []
|
||||
|
||||
|
||||
def test_admin_image_raw_streams(app, client, user_headers, admin_headers, monkeypatch):
|
||||
"""管理员经 raw 接口取到图片字节;错配 feedback_id → 404 FEEDBACK.IMAGE_NOT_FOUND;非管理员 → 403。"""
|
||||
# 隔离 MinIO:读取字节走本地假实现,提交仍只落库不触对象存储
|
||||
monkeypatch.setattr(
|
||||
"app.services.file_assets.service.file_asset_storage.get_bytes",
|
||||
lambda key: b"\x89PNG\r\n\x1a\nfake",
|
||||
)
|
||||
with app.app_context():
|
||||
u = db.session.query(User).filter_by(username="fbuser").first()
|
||||
a1 = _make_asset(u.id)
|
||||
asset_no = a1.asset_no
|
||||
resp = client.post("/api/user/feedback", headers=user_headers, json=_submit_payload(
|
||||
content="取图反馈", image_asset_nos=[asset_no],
|
||||
))
|
||||
assert resp.status_code == 200
|
||||
fb_id = resp.get_json()["id"]
|
||||
with app.app_context():
|
||||
img = GeneralFeedbackImage.query.filter_by(feedback_id=fb_id).first()
|
||||
image_id = img.id
|
||||
|
||||
resp = client.get(f"/api/admin/reports/general-feedbacks/{fb_id}/images/{image_id}/raw", headers=admin_headers)
|
||||
assert resp.status_code == 200
|
||||
assert resp.data == b"\x89PNG\r\n\x1a\nfake"
|
||||
assert resp.content_type.startswith("image/png")
|
||||
|
||||
# 错配 feedback_id:image 不属于该反馈 → FEEDBACK.IMAGE_NOT_FOUND(而非 FILE_ASSET.*)
|
||||
resp = client.get(
|
||||
f"/api/admin/reports/general-feedbacks/{fb_id + 99999}/images/{image_id}/raw", headers=admin_headers,
|
||||
)
|
||||
assert resp.status_code == 404
|
||||
assert parse_api_error(resp.get_json())["code"] == "FEEDBACK.IMAGE_NOT_FOUND"
|
||||
|
||||
# 非管理员访问 raw 接口 → 403
|
||||
resp = client.get(f"/api/admin/reports/general-feedbacks/{fb_id}/images/{image_id}/raw", headers=user_headers)
|
||||
assert resp.status_code == 403
|
||||
Reference in new issue
Block a user