141 lines
4.9 KiB
Python
141 lines
4.9 KiB
Python
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from review_agent.models import (
|
|
Conversation,
|
|
ExportedSummaryFile,
|
|
RegulatoryInfoPackageBatch,
|
|
WorkflowNodeRun,
|
|
)
|
|
|
|
|
|
pytestmark = pytest.mark.django_db
|
|
|
|
|
|
def test_regulatory_info_package_export_download_checks_owner(client, django_user_model, tmp_path):
|
|
owner = django_user_model.objects.create_user(username="owner", password="pass")
|
|
other = django_user_model.objects.create_user(username="other", password="pass")
|
|
conversation = Conversation.objects.create(user=owner, title="会话")
|
|
batch = RegulatoryInfoPackageBatch.objects.create(
|
|
conversation=conversation,
|
|
user=owner,
|
|
batch_no="RIP-20260610153300-abcdef",
|
|
)
|
|
path = tmp_path / "第1章 监管信息(预生成版).zip"
|
|
path.write_bytes(b"zip-content")
|
|
exported = ExportedSummaryFile.objects.create(
|
|
batch=None,
|
|
workflow_type="regulatory_info_package",
|
|
workflow_batch_id=batch.pk,
|
|
export_category="regulatory_info_package",
|
|
export_type=ExportedSummaryFile.ExportType.ZIP,
|
|
file_name=path.name,
|
|
storage_path=str(path),
|
|
)
|
|
|
|
client.force_login(other)
|
|
denied = client.get(f"/api/review-agent/file-summary/exports/{exported.pk}/download/")
|
|
assert denied.status_code == 404
|
|
|
|
client.force_login(owner)
|
|
allowed = client.get(f"/api/review-agent/file-summary/exports/{exported.pk}/download/")
|
|
assert allowed.status_code == 200
|
|
assert allowed["Content-Type"] == "application/zip"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("file_name", "export_type", "expected"),
|
|
[
|
|
("CH1.9 产品申报前沟通的说明.doc", ExportedSummaryFile.ExportType.WORD, "application/msword"),
|
|
(
|
|
"CH1.4 申请表.docx",
|
|
ExportedSummaryFile.ExportType.WORD,
|
|
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
),
|
|
("第1章 监管信息(预生成版).zip", ExportedSummaryFile.ExportType.ZIP, "application/zip"),
|
|
],
|
|
)
|
|
def test_regulatory_info_package_download_mime_by_extension(
|
|
client,
|
|
django_user_model,
|
|
tmp_path,
|
|
file_name,
|
|
export_type,
|
|
expected,
|
|
):
|
|
user = django_user_model.objects.create_user(username="owner", password="pass")
|
|
conversation = Conversation.objects.create(user=user, title="会话")
|
|
batch = RegulatoryInfoPackageBatch.objects.create(
|
|
conversation=conversation,
|
|
user=user,
|
|
batch_no=f"RIP-20260610153400-{Path(file_name).suffix[1:] or 'zip'}",
|
|
)
|
|
path = tmp_path / file_name
|
|
path.write_bytes(b"content")
|
|
exported = ExportedSummaryFile.objects.create(
|
|
batch=None,
|
|
workflow_type="regulatory_info_package",
|
|
workflow_batch_id=batch.pk,
|
|
export_category="generated_document",
|
|
export_type=export_type,
|
|
file_name=file_name,
|
|
storage_path=str(path),
|
|
)
|
|
client.force_login(user)
|
|
|
|
response = client.get(f"/api/review-agent/file-summary/exports/{exported.pk}/download/")
|
|
|
|
assert response.status_code == 200
|
|
assert response["Content-Type"] == expected
|
|
|
|
|
|
def test_regulatory_info_package_status_returns_nodes_and_zip_first(client, django_user_model, tmp_path):
|
|
user = django_user_model.objects.create_user(username="owner", password="pass")
|
|
conversation = Conversation.objects.create(user=user, title="会话")
|
|
batch = RegulatoryInfoPackageBatch.objects.create(
|
|
conversation=conversation,
|
|
user=user,
|
|
batch_no="RIP-20260610153500-abcdef",
|
|
status=RegulatoryInfoPackageBatch.Status.SUCCESS,
|
|
)
|
|
WorkflowNodeRun.objects.create(
|
|
workflow_type="regulatory_info_package",
|
|
workflow_batch_id=batch.pk,
|
|
node_group="regulatory_info_package",
|
|
node_code="zip_export",
|
|
node_name="打包下载",
|
|
status=WorkflowNodeRun.Status.SUCCESS,
|
|
progress=100,
|
|
)
|
|
doc = tmp_path / "CH1.4 申请表.docx"
|
|
zip_file = tmp_path / "第1章 监管信息(预生成版).zip"
|
|
doc.write_bytes(b"doc")
|
|
zip_file.write_bytes(b"zip")
|
|
ExportedSummaryFile.objects.create(
|
|
batch=None,
|
|
workflow_type="regulatory_info_package",
|
|
workflow_batch_id=batch.pk,
|
|
export_category="generated_document",
|
|
export_type=ExportedSummaryFile.ExportType.WORD,
|
|
file_name=doc.name,
|
|
storage_path=str(doc),
|
|
)
|
|
ExportedSummaryFile.objects.create(
|
|
batch=None,
|
|
workflow_type="regulatory_info_package",
|
|
workflow_batch_id=batch.pk,
|
|
export_category="regulatory_info_package",
|
|
export_type=ExportedSummaryFile.ExportType.ZIP,
|
|
file_name=zip_file.name,
|
|
storage_path=str(zip_file),
|
|
)
|
|
client.force_login(user)
|
|
|
|
response = client.get(f"/api/review-agent/regulatory-info-package/{batch.pk}/status/")
|
|
|
|
payload = response.json()
|
|
assert payload["batch"]["workflow_type"] == "regulatory_info_package"
|
|
assert payload["nodes"][0]["node_code"] == "zip_export"
|
|
assert payload["exports"][0]["export_type"] == "zip"
|