port print-web-form app to nested flask structure
Replace stub print/ app with full PDF web print service from ~/print-web-form. Adapt UPLOAD_FOLDER to use Path(__file__).parent, add pypdf dependency, port all tests (10 passing), remove unused static/.
This commit is contained in:
parent
af5d3f148d
commit
b0c19d5642
9 changed files with 340 additions and 0 deletions
0
print/tests/__init__.py
Normal file
0
print/tests/__init__.py
Normal file
59
print/tests/conftest.py
Normal file
59
print/tests/conftest.py
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
import io
|
||||
import tempfile
|
||||
|
||||
import pypdf
|
||||
import pytest
|
||||
|
||||
from app import app as flask_app, get_pdf_page_count
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def app():
|
||||
flask_app.config["TESTING"] = True
|
||||
flask_app.config["UPLOAD_FOLDER"] = tempfile.mkdtemp()
|
||||
flask_app.config["SECRET_KEY"] = "test-secret"
|
||||
yield flask_app
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(app):
|
||||
return app.test_client()
|
||||
|
||||
|
||||
def _make_pdf(num_pages=3):
|
||||
writer = pypdf.PdfWriter()
|
||||
for _ in range(num_pages):
|
||||
writer.add_blank_page(width=612, height=792)
|
||||
buf = io.BytesIO()
|
||||
writer.write(buf)
|
||||
buf.seek(0)
|
||||
return buf
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sample_pdf():
|
||||
return _make_pdf(3).read()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def large_pdf():
|
||||
return _make_pdf(15).read()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def corrupt_file():
|
||||
return b"This is not a PDF at all"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sample_pdf_path(tmp_path):
|
||||
path = tmp_path / "test.pdf"
|
||||
path.write_bytes(_make_pdf(3).read())
|
||||
return str(path)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def corrupt_file_path(tmp_path):
|
||||
path = tmp_path / "corrupt.pdf"
|
||||
path.write_bytes(b"not a pdf")
|
||||
return str(path)
|
||||
13
print/tests/test_pdf_utils.py
Normal file
13
print/tests/test_pdf_utils.py
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
from app import get_pdf_page_count
|
||||
|
||||
|
||||
def test_page_count_valid_pdf(sample_pdf_path):
|
||||
assert get_pdf_page_count(sample_pdf_path) == 3
|
||||
|
||||
|
||||
def test_page_count_corrupt_file(corrupt_file_path):
|
||||
assert get_pdf_page_count(corrupt_file_path) is None
|
||||
|
||||
|
||||
def test_page_count_nonexistent_file():
|
||||
assert get_pdf_page_count("/nonexistent/file.pdf") is None
|
||||
69
print/tests/test_routes.py
Normal file
69
print/tests/test_routes.py
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
import io
|
||||
import os
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
def test_get_index(client):
|
||||
resp = client.get("/")
|
||||
assert resp.status_code == 200
|
||||
assert b"printer" in resp.data.lower() or b"upload" in resp.data.lower()
|
||||
|
||||
|
||||
def test_post_no_file(client):
|
||||
resp = client.post("/", data={}, follow_redirects=True)
|
||||
assert resp.status_code == 200
|
||||
assert b"No file part" in resp.data
|
||||
|
||||
|
||||
def test_post_empty_filename(client):
|
||||
resp = client.post("/", data={"file": (io.BytesIO(b""), "")}, follow_redirects=True)
|
||||
assert resp.status_code == 200
|
||||
assert b"No file selected" in resp.data
|
||||
|
||||
|
||||
def test_post_invalid_extension(client):
|
||||
data = {"file": (io.BytesIO(b"hello"), "document.txt")}
|
||||
resp = client.post("/", data=data, follow_redirects=True)
|
||||
assert resp.status_code == 200
|
||||
assert b"Invalid file type" in resp.data
|
||||
|
||||
|
||||
@patch("app.subprocess.run")
|
||||
def test_post_valid_pdf(mock_run, client, app, sample_pdf):
|
||||
mock_run.return_value.stdout = "request-id-123"
|
||||
mock_run.return_value.stderr = ""
|
||||
mock_run.return_value.returncode = 0
|
||||
|
||||
data = {"file": (io.BytesIO(sample_pdf), "test.pdf")}
|
||||
resp = client.post("/", data=data, follow_redirects=True)
|
||||
assert resp.status_code == 200
|
||||
assert b"Success" in resp.data
|
||||
|
||||
upload_dir = app.config["UPLOAD_FOLDER"]
|
||||
remaining = os.listdir(upload_dir)
|
||||
assert len(remaining) == 0
|
||||
|
||||
mock_run.assert_called_once()
|
||||
cmd = mock_run.call_args[0][0]
|
||||
assert cmd[0] == "lp"
|
||||
|
||||
|
||||
@patch("app.subprocess.run")
|
||||
def test_post_pdf_exceeds_max_pages(mock_run, client, large_pdf):
|
||||
data = {"file": (io.BytesIO(large_pdf), "big.pdf")}
|
||||
resp = client.post("/", data=data, follow_redirects=True)
|
||||
assert resp.status_code == 200
|
||||
assert b"maximum allowed" in resp.data.lower()
|
||||
|
||||
mock_run.assert_not_called()
|
||||
|
||||
|
||||
def test_post_corrupt_pdf(client, app, corrupt_file):
|
||||
data = {"file": (io.BytesIO(corrupt_file), "bad.pdf")}
|
||||
resp = client.post("/", data=data, follow_redirects=True)
|
||||
assert resp.status_code == 200
|
||||
assert b"corrupt" in resp.data.lower()
|
||||
|
||||
upload_dir = app.config["UPLOAD_FOLDER"]
|
||||
remaining = os.listdir(upload_dir)
|
||||
assert len(remaining) == 0
|
||||
Loading…
Add table
Add a link
Reference in a new issue