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/.
69 lines
2.1 KiB
Python
69 lines
2.1 KiB
Python
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
|