|
11 | 11 | import os |
12 | 12 | import uuid |
13 | 13 | from datetime import UTC, datetime, timedelta |
14 | | -from pathlib import PurePosixPath |
| 14 | +from pathlib import Path, PurePosixPath |
15 | 15 |
|
16 | 16 | import asyncpg |
17 | 17 | import pytest |
@@ -482,6 +482,39 @@ async def test_thread_artifact_preview_http_preserves_raw_download(test_client, |
482 | 482 | assert raw_response.headers["content-type"].startswith("text/markdown") |
483 | 483 |
|
484 | 484 |
|
| 485 | +async def test_office_artifact_keeps_public_pdf_and_product_source(test_client, standard_user, admin_headers): |
| 486 | + """公开预览保留 PDF,产品预览面向所属用户返回 Office 字节。""" |
| 487 | + headers = standard_user["headers"] |
| 488 | + thread_id = await _create_thread_for_user(test_client, headers) |
| 489 | + fixture = Path(__file__).resolve().parents[2] / "data" / "测试文档.docx" |
| 490 | + content = fixture.read_bytes() |
| 491 | + try: |
| 492 | + artifact_path = await _upload_project_file(test_client, headers, thread_id, "preview.docx", content, artifact_path=True) |
| 493 | + public_url = f"/api/v1/agents/sessions/{thread_id}/artifacts/{artifact_path.lstrip('/')}" |
| 494 | + product_url = f"/api/agent/threads/{thread_id}/artifacts/preview" |
| 495 | + response = await test_client.get(public_url, params={"preview": True}, headers=headers) |
| 496 | + assert response.status_code == 200, response.text |
| 497 | + assert response.headers["content-type"] == "application/pdf" |
| 498 | + assert response.content.startswith(b"%PDF-") |
| 499 | + |
| 500 | + params = {"path": artifact_path} |
| 501 | + source = await test_client.get(product_url, params=params, headers=headers) |
| 502 | + assert source.status_code == 200, source.text |
| 503 | + assert source.headers["x-yuxi-preview-type"] == "office" |
| 504 | + assert source.headers["x-yuxi-preview-filename"] == "preview.docx" |
| 505 | + assert source.content == content |
| 506 | + fallback = await test_client.get(product_url, params={**params, "office_render": "pdf"}, headers=headers) |
| 507 | + assert fallback.status_code == 200, fallback.text |
| 508 | + assert fallback.headers["content-type"] == "application/pdf" |
| 509 | + assert fallback.content.startswith(b"%PDF-") |
| 510 | + |
| 511 | + denied = await test_client.get(product_url, params=params, headers=admin_headers) |
| 512 | + assert denied.status_code == 404 |
| 513 | + finally: |
| 514 | + await archive_public_thread(test_client, headers, thread_id) |
| 515 | + await delete_created_threads(thread_id) |
| 516 | + |
| 517 | + |
485 | 518 | async def _create_thread_for_user(test_client, headers: dict[str, str]) -> str: |
486 | 519 | agents_resp = await test_client.get("/api/agent", headers=headers) |
487 | 520 | assert agents_resp.status_code == 200, agents_resp.text |
|
0 commit comments