146 lines
5.4 KiB
Python
146 lines
5.4 KiB
Python
"""End-to-end tests for validated source upload into metadata and storage."""
|
|
|
|
from io import BytesIO
|
|
from pathlib import Path
|
|
from typing import cast
|
|
|
|
import pytest
|
|
from sqlalchemy import create_engine, select
|
|
from sqlalchemy.orm import Session, sessionmaker
|
|
|
|
from ekp_core.database import create_session_factory
|
|
from ekp_core.document_upload import (
|
|
DocumentTooLargeError,
|
|
DocumentUploadService,
|
|
EmptyDocumentError,
|
|
InvalidDocumentFilenameError,
|
|
MediaTypeMismatchError,
|
|
UnsupportedDocumentTypeError,
|
|
)
|
|
from ekp_domain import Base, Document, DocumentStatus
|
|
from ekp_object_storage import LocalObjectStorage
|
|
|
|
DOCX_MEDIA_TYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
|
|
|
|
|
class FailingCommitSession(Session):
|
|
"""Session used to verify source cleanup after metadata failure."""
|
|
|
|
def commit(self) -> None:
|
|
raise RuntimeError("simulated metadata failure")
|
|
|
|
|
|
def _build_upload_service(
|
|
tmp_path: Path,
|
|
*,
|
|
max_upload_bytes: int = 1024,
|
|
) -> tuple[DocumentUploadService, sessionmaker[Session], LocalObjectStorage]:
|
|
engine = create_engine("sqlite+pysqlite:///:memory:")
|
|
Base.metadata.create_all(engine)
|
|
session_factory = create_session_factory(engine)
|
|
storage = LocalObjectStorage(tmp_path / "storage")
|
|
service = DocumentUploadService(session_factory, storage, max_upload_bytes)
|
|
return service, session_factory, storage
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("filename", "media_type", "payload", "canonical_media_type"),
|
|
[
|
|
("产品手册.pdf", "application/pdf", b"%PDF-demo", "application/pdf"),
|
|
("产品手册.docx", DOCX_MEDIA_TYPE, b"PK-demo", DOCX_MEDIA_TYPE),
|
|
("制度.txt", "text/plain; charset=utf-8", "制度正文".encode(), "text/plain"),
|
|
("README.MD", None, b"# Demo", "text/markdown"),
|
|
],
|
|
)
|
|
def test_upload_supported_document_types(
|
|
tmp_path: Path,
|
|
filename: str,
|
|
media_type: str | None,
|
|
payload: bytes,
|
|
canonical_media_type: str,
|
|
) -> None:
|
|
service, session_factory, storage = _build_upload_service(tmp_path)
|
|
|
|
document = service.upload(filename, media_type, BytesIO(payload))
|
|
|
|
assert document.filename == filename
|
|
assert document.media_type == canonical_media_type
|
|
assert document.size_bytes == len(payload)
|
|
assert document.status is DocumentStatus.UPLOADED
|
|
with storage.open(document.storage_key) as source:
|
|
assert source.read() == payload
|
|
with session_factory() as session:
|
|
stored = session.scalar(select(Document).where(Document.id == document.id))
|
|
assert stored is not None
|
|
assert stored.storage_key == document.storage_key
|
|
|
|
|
|
@pytest.mark.parametrize("filename", ["slides.pptx", "table.xlsx", "image.png", "no-extension"])
|
|
def test_upload_rejects_types_outside_demo_scope(tmp_path: Path, filename: str) -> None:
|
|
service, _, _ = _build_upload_service(tmp_path)
|
|
|
|
with pytest.raises(UnsupportedDocumentTypeError):
|
|
service.upload(filename, "application/octet-stream", BytesIO(b"unsupported"))
|
|
|
|
assert not any((tmp_path / "storage").rglob("*"))
|
|
|
|
|
|
@pytest.mark.parametrize("filename", ["", "../policy.txt", "folder/policy.txt", "bad\\name.md"])
|
|
def test_upload_rejects_unsafe_filenames(tmp_path: Path, filename: str) -> None:
|
|
service, _, _ = _build_upload_service(tmp_path)
|
|
|
|
with pytest.raises(InvalidDocumentFilenameError):
|
|
service.upload(filename, "application/octet-stream", BytesIO(b"unsafe"))
|
|
|
|
|
|
def test_upload_rejects_media_type_mismatch(tmp_path: Path) -> None:
|
|
service, _, _ = _build_upload_service(tmp_path)
|
|
|
|
with pytest.raises(MediaTypeMismatchError):
|
|
service.upload("policy.pdf", "text/plain", BytesIO(b"not a declared PDF"))
|
|
|
|
|
|
def test_upload_rejects_empty_document_and_removes_object(tmp_path: Path) -> None:
|
|
service, _, _ = _build_upload_service(tmp_path)
|
|
|
|
with pytest.raises(EmptyDocumentError):
|
|
service.upload("empty.txt", "text/plain", BytesIO())
|
|
|
|
assert not any(path.is_file() for path in (tmp_path / "storage").rglob("*"))
|
|
|
|
|
|
def test_upload_enforces_streaming_size_limit_and_removes_partial_file(tmp_path: Path) -> None:
|
|
service, _, _ = _build_upload_service(tmp_path, max_upload_bytes=4)
|
|
|
|
with pytest.raises(DocumentTooLargeError):
|
|
service.upload("large.txt", "text/plain", BytesIO(b"12345"))
|
|
|
|
assert not any(path.is_file() for path in (tmp_path / "storage").rglob("*"))
|
|
|
|
|
|
def test_upload_accepts_document_at_exact_size_limit(tmp_path: Path) -> None:
|
|
service, _, storage = _build_upload_service(tmp_path, max_upload_bytes=4)
|
|
|
|
document = service.upload("exact.txt", "text/plain", BytesIO(b"1234"))
|
|
|
|
assert document.size_bytes == 4
|
|
assert storage.exists(document.storage_key)
|
|
|
|
|
|
def test_upload_removes_object_when_metadata_commit_fails(tmp_path: Path) -> None:
|
|
engine = create_engine("sqlite+pysqlite:///:memory:")
|
|
Base.metadata.create_all(engine)
|
|
session_factory = cast(
|
|
sessionmaker[Session],
|
|
sessionmaker(bind=engine, class_=FailingCommitSession, expire_on_commit=False),
|
|
)
|
|
storage = LocalObjectStorage(tmp_path / "storage")
|
|
service = DocumentUploadService(session_factory, storage, max_upload_bytes=1024)
|
|
|
|
with pytest.raises(RuntimeError, match="simulated metadata failure"):
|
|
service.upload("policy.txt", "text/plain", BytesIO(b"policy"))
|
|
|
|
assert not any(path.is_file() for path in (tmp_path / "storage").rglob("*"))
|
|
with session_factory() as session:
|
|
assert session.scalar(select(Document)) is None
|