`./receipt.py serve` (receipt_web.py, the new [web] extra) serves a page for printing a note or an uploaded image. The site's nginx gates /receipt/ on the admin session and proxies to it over Tailscale, adding X-Receipt-Token; nothing but /healthz answers without it, and POSTs also need X-Requested-With: receipt. Jobs run one at a time behind a lock around run_action(), and uploads print from memory. run_action(), _dispatch() and setup_network_quiet() move from the TUI to receipt.py so the server doesn't need textual. --no-setup (needs_setup()) skips the sudo connect script everywhere it would run; serve always sets it. cmd_print takes in-memory `data`. deploy/ has the systemd user units. receipt-update.timer runs update.sh every 5 minutes: it fast-forwards the clone from Gitea, runs the tests and restarts receipt-web, rolling back if they fail. Also included from earlier uncommitted work: the CI matrix moved to the right place, connect_receipt.sh no longer re-parses CLI values in bash -c, RECEIPT_PRINTER_IP/PORT are validated, pyusb is declared, the ruff S rules are on, and the img/ cache and junk/ are no longer tracked. Co-Authored-By: Claude Opus 5.5 <[email protected]>
148 lines
4.2 KiB
Python
148 lines
4.2 KiB
Python
import io
|
|
|
|
import pytest
|
|
from conftest import FakePrinter, make_args
|
|
from PIL import Image
|
|
|
|
import receipt
|
|
|
|
pytest.importorskip("fastapi")
|
|
pytest.importorskip("multipart")
|
|
from fastapi.testclient import TestClient # noqa: E402
|
|
|
|
import receipt_web # noqa: E402
|
|
|
|
TOKEN = "t" * 32
|
|
HEADERS = {"X-Receipt-Token": TOKEN, "X-Requested-With": "receipt"}
|
|
|
|
|
|
@pytest.fixture
|
|
def fake_printer(monkeypatch):
|
|
p = FakePrinter()
|
|
monkeypatch.setattr(receipt, "get_printer", lambda args: p)
|
|
return p
|
|
|
|
|
|
@pytest.fixture
|
|
def client():
|
|
base = make_args(usb=False, no_setup=True)
|
|
return TestClient(receipt_web.create_app(base, TOKEN))
|
|
|
|
|
|
def png_bytes(size=(20, 10)):
|
|
buf = io.BytesIO()
|
|
Image.new("RGB", size, "gray").save(buf, "PNG")
|
|
return buf.getvalue()
|
|
|
|
|
|
def test_healthz_is_open(client):
|
|
assert client.get("/healthz").json() == {"ok": True}
|
|
|
|
|
|
@pytest.mark.parametrize("token", [None, "wrong"])
|
|
@pytest.mark.parametrize("method,path", [("get", "/"), ("get", "/api/status"),
|
|
("post", "/api/note")])
|
|
def test_token_required(client, fake_printer, token, method, path):
|
|
headers = {"X-Requested-With": "receipt"}
|
|
if token:
|
|
headers["X-Receipt-Token"] = token
|
|
|
|
res = client.request(method, path, headers=headers, json={"text": "hi"})
|
|
|
|
assert res.status_code == 401
|
|
assert fake_printer.calls == []
|
|
|
|
|
|
def test_page_served_with_token(client):
|
|
res = client.get("/", headers={"X-Receipt-Token": TOKEN})
|
|
|
|
assert res.status_code == 200
|
|
assert "Receipt printer" in res.text
|
|
|
|
|
|
def test_post_needs_page_header(client, fake_printer):
|
|
res = client.post("/api/note", headers={"X-Receipt-Token": TOKEN}, json={"text": "hi"})
|
|
|
|
assert res.status_code == 403
|
|
assert fake_printer.calls == []
|
|
|
|
|
|
def test_note_prints(client, fake_printer):
|
|
res = client.post("/api/note", headers=HEADERS, json={"text": "hello", "timestamp": False})
|
|
|
|
assert res.status_code == 200
|
|
assert res.json()["ok"] is True
|
|
assert ("textln", "hello") in fake_printer.calls
|
|
assert fake_printer.calls[-2:] == [("cut", None), ("close", None)]
|
|
|
|
|
|
@pytest.mark.parametrize("text", ["", " ", "x" * (receipt_web.MAX_NOTE_CHARS + 1)])
|
|
def test_note_rejects_empty_and_long(client, fake_printer, text):
|
|
res = client.post("/api/note", headers=HEADERS, json={"text": text})
|
|
|
|
assert res.status_code in (400, 422)
|
|
assert fake_printer.calls == []
|
|
|
|
|
|
def test_status_reports_reachable(client, fake_printer):
|
|
res = client.get("/api/status", headers=HEADERS)
|
|
|
|
assert res.status_code == 200
|
|
assert "reachable" in res.json()["output"]
|
|
|
|
|
|
def test_image_prints_from_memory(client, fake_printer):
|
|
res = client.post(
|
|
"/api/image",
|
|
headers=HEADERS,
|
|
files={"file": ("x.png", png_bytes(), "image/png")},
|
|
data={"message": "caption"},
|
|
)
|
|
|
|
assert res.status_code == 200, res.text
|
|
kinds = [c[0] for c in fake_printer.calls]
|
|
assert kinds == ["textln", "image", "cut", "close"]
|
|
assert fake_printer.calls[0] == ("textln", "caption")
|
|
assert fake_printer.calls[1][1].size[0] == receipt.PRINT_WIDTH_PX
|
|
|
|
|
|
def test_image_rejects_non_image(client, fake_printer):
|
|
res = client.post(
|
|
"/api/image", headers=HEADERS, files={"file": ("x.png", b"not an image", "image/png")}
|
|
)
|
|
|
|
assert res.status_code == 400
|
|
assert fake_printer.calls == []
|
|
|
|
|
|
def test_image_rejects_oversize(client, fake_printer, monkeypatch):
|
|
monkeypatch.setattr(receipt, "MAX_IMAGE_BYTES", 100)
|
|
|
|
res = client.post(
|
|
"/api/image", headers=HEADERS, files={"file": ("x.png", png_bytes((200, 200)), "image/png")}
|
|
)
|
|
|
|
assert res.status_code == 413
|
|
assert fake_printer.calls == []
|
|
|
|
|
|
def test_printer_error_is_502(client, monkeypatch):
|
|
def boom(args):
|
|
raise OSError("no printer")
|
|
|
|
monkeypatch.setattr(receipt, "get_printer", boom)
|
|
|
|
res = client.post("/api/note", headers=HEADERS, json={"text": "hi"})
|
|
|
|
assert res.status_code == 502
|
|
assert res.json() == {"ok": False, "output": "Error: no printer"}
|
|
|
|
|
|
def test_serve_refuses_without_token(monkeypatch, capsys):
|
|
monkeypatch.delenv(receipt_web.TOKEN_ENV, raising=False)
|
|
|
|
rc = receipt_web.serve(make_args(listen="127.0.0.1:8765"))
|
|
|
|
assert rc == 1
|
|
assert receipt_web.TOKEN_ENV in capsys.readouterr().err
|