generated from john/python-template
116 lines
4.2 KiB
Python
116 lines
4.2 KiB
Python
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from transcription.db import session_scope
|
|
from transcription.db.models import Document
|
|
from transcription.db.models import DocumentType
|
|
from transcription.db.models import Source
|
|
from transcription.ui.pages.print_preview_page import reflow_transcription
|
|
|
|
|
|
def test_reflow_transcription_preserves_paragraph_boundaries():
|
|
assert reflow_transcription("first line\nsecond line\n\nnext paragraph") == [
|
|
"first line second line",
|
|
"next paragraph",
|
|
]
|
|
|
|
|
|
@pytest.mark.integration
|
|
@pytest.mark.asyncio
|
|
async def test_document_print_preview_and_safe_media_route(app_client):
|
|
app, client = app_client
|
|
media_path = app.state.settings.upload_dir / "documents" / "print-page.png"
|
|
pdf_path = app.state.settings.upload_dir / "documents" / "print-page.pdf"
|
|
media_path.parent.mkdir(parents=True, exist_ok=True)
|
|
media_path.write_bytes(b"\x89PNG\r\n\x1a\n")
|
|
pdf_path.write_bytes(b"%PDF-1.4\n%%EOF")
|
|
|
|
async with session_scope() as session:
|
|
document_type = DocumentType(label="Photograph", normalized_label="photograph")
|
|
session.add(document_type)
|
|
await session.flush()
|
|
document = Document(
|
|
name="<Print & Preserve>",
|
|
notes="<script>unsafe()</script>",
|
|
document_type_id=document_type.id,
|
|
)
|
|
session.add(document)
|
|
await session.flush()
|
|
source = Source(
|
|
document_id=document.id,
|
|
page_number=1,
|
|
upload_name="print-page.png",
|
|
filename="print-page.png",
|
|
file_path=str(media_path),
|
|
file_hash="a" * 64,
|
|
file_size_bytes=media_path.stat().st_size,
|
|
raw_transcription="line one\nline two",
|
|
)
|
|
session.add(source)
|
|
session.add(
|
|
Source(
|
|
document_id=document.id,
|
|
page_number=2,
|
|
upload_name="print-page.pdf",
|
|
filename="print-page.pdf",
|
|
file_path=str(pdf_path),
|
|
file_hash="c" * 64,
|
|
file_size_bytes=pdf_path.stat().st_size,
|
|
raw_transcription="PDF source",
|
|
)
|
|
)
|
|
await session.commit()
|
|
document_id = document.id
|
|
source_id = source.id
|
|
|
|
response = client.get(f"/ui/documents/{document_id}/print")
|
|
assert response.status_code == 200
|
|
assert "Print & Preserve" in response.text
|
|
assert "<script>unsafe()</script>" in response.text
|
|
assert "Document Type" in response.text
|
|
assert "Photograph" in response.text
|
|
assert "Facsimile" in response.text
|
|
assert "Text only" in response.text
|
|
assert "print-page-break" in response.text
|
|
assert "print-source-pdf" in response.text
|
|
assert "print-source-image" in response.text
|
|
assert str(media_path) not in response.text
|
|
assert str(pdf_path) not in response.text
|
|
|
|
media_response = client.get(f"/api/v4/documents/{document_id}/sources/{source_id}/media")
|
|
assert media_response.status_code == 200
|
|
assert media_response.headers["content-type"] == "image/png"
|
|
assert "content-disposition" not in media_response.headers
|
|
|
|
|
|
@pytest.mark.integration
|
|
@pytest.mark.asyncio
|
|
async def test_document_source_media_rejects_cross_document_access(app_client):
|
|
app, client = app_client
|
|
media_path = Path(app.state.settings.upload_dir) / "documents" / "other.png"
|
|
media_path.parent.mkdir(parents=True, exist_ok=True)
|
|
media_path.write_bytes(b"\x89PNG\r\n\x1a\n")
|
|
|
|
async with session_scope() as session:
|
|
owner = Document(name="Owner")
|
|
other = Document(name="Other")
|
|
session.add_all([owner, other])
|
|
await session.flush()
|
|
source = Source(
|
|
document_id=owner.id,
|
|
page_number=1,
|
|
upload_name="other.png",
|
|
filename="other.png",
|
|
file_path=str(media_path),
|
|
file_hash="b" * 64,
|
|
file_size_bytes=media_path.stat().st_size,
|
|
)
|
|
session.add(source)
|
|
await session.commit()
|
|
other_id = other.id
|
|
source_id = source.id
|
|
|
|
response = client.get(f"/api/v4/documents/{other_id}/sources/{source_id}/media")
|
|
assert response.status_code == 404
|