fix(安全培训): 修复安全培训文件预览报错不bug
This commit is contained in:
1 parent
e62e4961d4
commit
31182a83d7
2 files changed
+119
-2
No files matched your search
@@ -10,6 +10,7 @@ from __future__ import annotations
|
||||
|
||||
from datetime import date, datetime
|
||||
from io import BytesIO
|
||||
from pathlib import Path
|
||||
|
||||
from openpyxl import load_workbook
|
||||
import pytest
|
||||
@@ -19,6 +20,7 @@ from app.core.errors import ValidationError
|
||||
from app.models import Agent, TrainingRecord, User
|
||||
from app.services import training_service
|
||||
from app.services.training_service import _grade_answer_details, _normalize_question_bank
|
||||
from app.api.chat import preview_support
|
||||
|
||||
|
||||
def _create_user(
|
||||
@@ -521,6 +523,71 @@ def test_training_admin_list_stats_and_export_share_one_fact_source(client, app)
|
||||
assert {rows[1][7], rows[2][7]} == {"passed", "failed"}
|
||||
|
||||
|
||||
def test_training_preview_proxy_reads_kb_storage_without_http_callback(monkeypatch, tmp_path, app):
|
||||
"""
|
||||
培训资料预览必须直接复用知识库存储解析,不能回调前端 Nginx。
|
||||
|
||||
关键断言:匹配培训资料路径时,预览代理返回原始 PDF 内容,且不会调用 requests.get,
|
||||
防止测试环境容器经宿主机端口回环时再次出现 502。
|
||||
"""
|
||||
preview_file = tmp_path / "safety-training.pdf"
|
||||
preview_file.write_bytes(b"%PDF-training-preview")
|
||||
requested_version_ids: list[int] = []
|
||||
|
||||
def resolve_preview_path(version_id: int):
|
||||
requested_version_ids.append(version_id)
|
||||
return Path(preview_file), preview_file.name
|
||||
|
||||
def reject_http_callback(*_args, **_kwargs):
|
||||
raise AssertionError("training preview must not call requests.get")
|
||||
|
||||
monkeypatch.setattr(
|
||||
preview_support.document_service,
|
||||
"resolve_training_preview_file_path",
|
||||
resolve_preview_path,
|
||||
)
|
||||
monkeypatch.setattr(preview_support.requests, "get", reject_http_callback)
|
||||
|
||||
with app.app_context():
|
||||
content, content_type, error = preview_support._fetch_preview_source_bytes(
|
||||
"/api/kb/document-versions/1231/training-preview-file",
|
||||
)
|
||||
|
||||
assert requested_version_ids == [1231]
|
||||
assert content == b"%PDF-training-preview"
|
||||
assert content_type == "application/pdf"
|
||||
assert error is None
|
||||
|
||||
|
||||
def test_non_training_preview_url_keeps_existing_http_fetch_path(monkeypatch):
|
||||
"""
|
||||
非培训来源必须继续走原有通用预览取流逻辑。
|
||||
|
||||
关键断言:普通内网 URL 不会触发知识库培训文件解析,避免影响既有 Chat 来源文件预览。
|
||||
"""
|
||||
class FakeResponse:
|
||||
status_code = 200
|
||||
content = b"%PDF-chat-source"
|
||||
headers = {"content-type": "application/pdf"}
|
||||
|
||||
def reject_training_resolver(_version_id: int):
|
||||
raise AssertionError("non-training URL must not resolve training preview")
|
||||
|
||||
monkeypatch.setattr(
|
||||
preview_support.document_service,
|
||||
"resolve_training_preview_file_path",
|
||||
reject_training_resolver,
|
||||
)
|
||||
monkeypatch.setattr(preview_support.requests, "get", lambda *_args, **_kwargs: FakeResponse())
|
||||
|
||||
content, content_type, error = preview_support._fetch_preview_source_bytes(
|
||||
"http://192.168.100.192:9000/chat-source.pdf",
|
||||
)
|
||||
|
||||
assert content == b"%PDF-chat-source"
|
||||
assert content_type == "application/pdf"
|
||||
assert error is None
|
||||
|
||||
def test_training_admin_permission_can_be_granted_by_feature_permission(client, app):
|
||||
"""培训 HR 管理能力应支持通过功能权限显式授予普通用户。"""
|
||||
with app.app_context():
|
||||
|
||||
Reference in new issue
Block a user