- note --size 1|2|3 magnifies the text and wraps to 48/24/16 columns, then resets the printer to normal size - Web page gets a Size menu defaulting to Large; the API validates it - README/CLAUDE.md: the printer has no Wi-Fi (UB-E04 needs an adapter), so it is cabled to stppi's eth0 at 192.168.192.168 Co-Authored-By: Claude Opus 5.5 <[email protected]>
163 lines
4.7 KiB
Python
163 lines
4.7 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)]
|
|
|
|
|
|
def test_note_size(client, fake_printer):
|
|
res = client.post("/api/note", headers=HEADERS, json={"text": "hello", "size": 3})
|
|
|
|
assert res.status_code == 200
|
|
assert ("set", {"custom_size": True, "width": 3, "height": 3}) in fake_printer.calls
|
|
|
|
|
|
@pytest.mark.parametrize("size", [0, 4])
|
|
def test_note_rejects_bad_size(client, fake_printer, size):
|
|
res = client.post("/api/note", headers=HEADERS, json={"text": "hi", "size": size})
|
|
|
|
assert res.status_code == 422
|
|
assert fake_printer.calls == []
|
|
|
|
|
|
@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
|