Driver rename and icons: `Driver.display_name` plus a `DriverIcon` table, both global/shared like the `Driver` row they hang off, so a rename or an icon is what every Owner sees. The rename/icon dialog keeps its forms as siblings (nested forms are invalid HTML) and the icon routes return an `hx-swap-oob` thumbnail refresh rather than re-rendering the table, which would tear the open `<dialog>` out of the DOM. Web image picker: `GET /web/images` renders a pickable grid for a printer or a driver icon, with the search term prefilled from the entity name and editable. Picking one downloads it server-side and normalizes it. Driver download search: `GET /web/drivers` searches for a vendor-wide driver (the term is rewritten into the vendor's real product name for 15 brands) or for the exact model as typed. Links only — nothing is downloaded, and the fragment says the results are unvetted. Icon uploads no longer reject off-size or non-PNG files: `normalize_icon()` letterboxes any decodable raster into a 256x256 PNG. An already-exact 256x256 PNG is returned byte-identical, because icon storage is content-addressed and re-encoding would move the file on every save. `fetch_image()` makes the request from the server, so `assert_fetchable()` refuses any URL resolving to a private, loopback, or link-local address, and re-runs on every redirect. ImpTune sits on the same LAN as the printers it configures; an unguarded fetcher would be a port scanner for anyone who can reach the UI. DuckDuckGo is scraped, not called through an API — no key needed, but fragile, so both search functions swallow parse failures and return [] instead of 500ing a page. `WEB_SEARCH=false` disables every outbound request and hides the controls, for air-gapped installs. Also: one shared `Jinja2Templates` in `templating.py` instead of five per-router instances, so a template global is declared once; `_add_missing_columns()` in `database.py` adds new nullable columns to a pre-existing table, which `create_tables(safe=True)` skips; `db_env` in test_db.py now closes its connection on teardown, or the next test's ORM writes land in the previous test's DB file. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
228 lines
8.2 KiB
Python
228 lines
8.2 KiB
Python
"""Driver icons — upload, fetch-from-web, and serve. Global, like Driver rows."""
|
|
from __future__ import annotations
|
|
|
|
import io
|
|
import json
|
|
|
|
import pytest
|
|
from PIL import Image
|
|
|
|
|
|
def _png(width: int = 256, height: int = 256, color: str = "red") -> bytes:
|
|
buf = io.BytesIO()
|
|
Image.new("RGBA", (width, height), color=color).save(buf, format="PNG")
|
|
return buf.getvalue()
|
|
|
|
|
|
def _create_driver(sha: str = "b" * 64):
|
|
from imptune.db.models import Driver
|
|
|
|
return Driver.create(
|
|
sha256=sha,
|
|
original_filename="konica_c300i.zip",
|
|
size_bytes=4096,
|
|
driver_desc=json.dumps(["KONICA MINOLTA C300i PCL"]),
|
|
inf_filename="kocpl.inf",
|
|
architecture="amd64",
|
|
)
|
|
|
|
|
|
class TestDriverIconUpload:
|
|
def test_upload_creates_the_record_and_file(self, client, tmp_data_dir):
|
|
from imptune.db.models import DriverIcon
|
|
|
|
driver = _create_driver()
|
|
response = client.post(
|
|
f"/drivers/{driver.id}/icon",
|
|
files={"file": ("logo.png", io.BytesIO(_png()), "image/png")},
|
|
)
|
|
assert response.status_code == 200
|
|
|
|
icon = DriverIcon.get(DriverIcon.driver == driver.id)
|
|
assert icon.original_filename == "logo.png"
|
|
assert (tmp_data_dir / "icons" / icon.sha256).exists()
|
|
|
|
def test_off_size_image_is_normalized(self, client, tmp_data_dir):
|
|
from imptune.db.models import DriverIcon
|
|
|
|
driver = _create_driver()
|
|
client.post(
|
|
f"/drivers/{driver.id}/icon",
|
|
files={"file": ("wide.png", io.BytesIO(_png(600, 120)), "image/png")},
|
|
)
|
|
icon = DriverIcon.get(DriverIcon.driver == driver.id)
|
|
with Image.open(tmp_data_dir / "icons" / icon.sha256) as img:
|
|
assert img.size == (256, 256)
|
|
|
|
def test_second_upload_replaces_the_first(self, client, tmp_data_dir):
|
|
from imptune.db.models import DriverIcon
|
|
|
|
driver = _create_driver()
|
|
client.post(
|
|
f"/drivers/{driver.id}/icon",
|
|
files={"file": ("a.png", io.BytesIO(_png(color="red")), "image/png")},
|
|
)
|
|
client.post(
|
|
f"/drivers/{driver.id}/icon",
|
|
files={"file": ("b.png", io.BytesIO(_png(color="green")), "image/png")},
|
|
)
|
|
icons = list(DriverIcon.select().where(DriverIcon.driver == driver.id))
|
|
assert len(icons) == 1
|
|
assert icons[0].original_filename == "b.png"
|
|
|
|
def test_response_carries_an_oob_row_refresh(self, client, tmp_data_dir):
|
|
"""The dialog stays open, so the table thumbnail is swapped out of band."""
|
|
driver = _create_driver()
|
|
response = client.post(
|
|
f"/drivers/{driver.id}/icon",
|
|
files={"file": ("logo.png", io.BytesIO(_png()), "image/png")},
|
|
)
|
|
assert f'id="driver-thumb-{driver.id}"' in response.text
|
|
assert 'hx-swap-oob="true"' in response.text
|
|
|
|
def test_rejects_an_undecodable_file(self, client, tmp_data_dir):
|
|
driver = _create_driver()
|
|
response = client.post(
|
|
f"/drivers/{driver.id}/icon",
|
|
files={"file": ("x.png", io.BytesIO(b"nope"), "image/png")},
|
|
)
|
|
assert response.status_code == 422
|
|
|
|
def test_rejects_an_oversized_file(self, client, tmp_data_dir):
|
|
driver = _create_driver()
|
|
oversized = _png() + b"\x00" * (750 * 1024)
|
|
response = client.post(
|
|
f"/drivers/{driver.id}/icon",
|
|
files={"file": ("big.png", io.BytesIO(oversized), "image/png")},
|
|
)
|
|
assert response.status_code == 422
|
|
assert "750" in response.text
|
|
|
|
def test_404_for_unknown_driver(self, client, tmp_data_dir):
|
|
response = client.post(
|
|
"/drivers/99999/icon",
|
|
files={"file": ("logo.png", io.BytesIO(_png()), "image/png")},
|
|
)
|
|
assert response.status_code == 404
|
|
|
|
|
|
class TestDriverIconFromWeb:
|
|
def test_downloads_and_stores_the_picked_image(self, client, tmp_data_dir, monkeypatch):
|
|
from imptune.db.models import DriverIcon
|
|
from imptune.services import websearch
|
|
|
|
driver = _create_driver()
|
|
monkeypatch.setattr(websearch, "fetch_image", lambda url: _png(400, 400))
|
|
|
|
response = client.post(
|
|
f"/drivers/{driver.id}/icon/from-web",
|
|
data={"url": "https://example.com/pictures/c300i.png"},
|
|
)
|
|
assert response.status_code == 200
|
|
|
|
icon = DriverIcon.get(DriverIcon.driver == driver.id)
|
|
assert icon.original_filename == "c300i.png"
|
|
with Image.open(tmp_data_dir / "icons" / icon.sha256) as img:
|
|
assert img.size == (256, 256)
|
|
|
|
def test_reports_a_refused_url(self, client, tmp_data_dir, monkeypatch):
|
|
from imptune.services import websearch
|
|
|
|
driver = _create_driver()
|
|
|
|
def refuse(url):
|
|
raise websearch.WebSearchError("10.0.0.5 resolves to a private address — refused.")
|
|
|
|
monkeypatch.setattr(websearch, "fetch_image", refuse)
|
|
response = client.post(
|
|
f"/drivers/{driver.id}/icon/from-web", data={"url": "http://10.0.0.5/x.png"}
|
|
)
|
|
assert response.status_code == 400
|
|
assert "private address" in response.text
|
|
|
|
def test_reports_a_page_that_is_not_an_image(self, client, tmp_data_dir, monkeypatch):
|
|
from imptune.services import websearch
|
|
|
|
driver = _create_driver()
|
|
monkeypatch.setattr(websearch, "fetch_image", lambda url: b"<html>404</html>")
|
|
response = client.post(
|
|
f"/drivers/{driver.id}/icon/from-web", data={"url": "https://example.com/x"}
|
|
)
|
|
assert response.status_code == 422
|
|
assert "readable image" in response.text
|
|
|
|
|
|
class TestDriverIconServe:
|
|
def test_serves_the_png(self, client, tmp_data_dir):
|
|
driver = _create_driver()
|
|
client.post(
|
|
f"/drivers/{driver.id}/icon",
|
|
files={"file": ("logo.png", io.BytesIO(_png()), "image/png")},
|
|
)
|
|
response = client.get(f"/drivers/{driver.id}/icon")
|
|
assert response.status_code == 200
|
|
assert response.headers["content-type"] == "image/png"
|
|
|
|
def test_visible_to_another_owner(self, client, tmp_data_dir):
|
|
"""The driver library is shared, so its icons are not owner-scoped."""
|
|
from fastapi.testclient import TestClient
|
|
|
|
from imptune.main import app
|
|
|
|
driver = _create_driver()
|
|
client.post(
|
|
f"/drivers/{driver.id}/icon",
|
|
files={"file": ("logo.png", io.BytesIO(_png()), "image/png")},
|
|
)
|
|
with TestClient(app) as other:
|
|
assert other.get(f"/drivers/{driver.id}/icon").status_code == 200
|
|
|
|
def test_404_without_an_icon(self, client, tmp_data_dir):
|
|
driver = _create_driver()
|
|
assert client.get(f"/drivers/{driver.id}/icon").status_code == 404
|
|
|
|
def test_404_when_the_file_vanished(self, client, tmp_data_dir):
|
|
from imptune.db.models import DriverIcon
|
|
|
|
driver = _create_driver()
|
|
client.post(
|
|
f"/drivers/{driver.id}/icon",
|
|
files={"file": ("logo.png", io.BytesIO(_png()), "image/png")},
|
|
)
|
|
icon = DriverIcon.get(DriverIcon.driver == driver.id)
|
|
(tmp_data_dir / "icons" / icon.sha256).unlink()
|
|
assert client.get(f"/drivers/{driver.id}/icon").status_code == 404
|
|
|
|
|
|
def test_library_row_shows_the_thumbnail(client, tmp_data_dir):
|
|
driver = _create_driver()
|
|
client.post(
|
|
f"/drivers/{driver.id}/icon",
|
|
files={"file": ("logo.png", io.BytesIO(_png()), "image/png")},
|
|
)
|
|
page = client.get("/drivers")
|
|
assert f'src="/drivers/{driver.id}/icon"' in page.text
|
|
|
|
|
|
def test_printer_and_driver_icons_share_one_stored_file(client, owner, tmp_data_dir):
|
|
"""Content-addressed storage: identical bytes land on the same path."""
|
|
from imptune.db.models import DriverIcon, Icon, Printer
|
|
|
|
driver = _create_driver()
|
|
printer = Printer.create(
|
|
name="P", ip_address="10.0.0.1", port_name="IP_10.0.0.1", owner=owner
|
|
)
|
|
png = _png(300, 300)
|
|
client.post(
|
|
f"/drivers/{driver.id}/icon",
|
|
files={"file": ("a.png", io.BytesIO(png), "image/png")},
|
|
)
|
|
client.post(
|
|
f"/printers/{printer.id}/icon",
|
|
files={"file": ("b.png", io.BytesIO(png), "image/png")},
|
|
)
|
|
assert (
|
|
DriverIcon.get(DriverIcon.driver == driver.id).sha256
|
|
== Icon.get(Icon.printer == printer.id).sha256
|
|
)
|