Files
ImpTune/tests/e2e/test_driver_rename.py
T
kawaandClaude Opus 5 2c06806814 feat: driver rename, driver icons, web image + driver search
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>
2026-08-05 10:44:47 +02:00

85 lines
2.8 KiB
Python

"""E2E: the driver library's rename dialog — open, pre-fill, submit, row updates."""
from __future__ import annotations
import io
import zipfile
# Same reasoning as test_printer_edit.py: the button label goes through the i18n
# store, which follows navigator.language, so only a structural hook is portable.
EDIT_BUTTON = "button[onclick*='showModal']"
INF = """[Version]
Signature="$Windows NT$"
Class=Printer
Provider=%Vendor%
[Manufacturer]
%Vendor%=Models,NTamd64
[Models.NTamd64]
"E2E Rename Printer"=Install,USBPRINT\\E2E
[Strings]
Vendor="E2E Vendor"
"""
def _driver_zip() -> bytes:
buf = io.BytesIO()
with zipfile.ZipFile(buf, "w") as zf:
zf.writestr("e2e_rename.inf", INF)
return buf.getvalue()
def _upload_driver(live_server: str, owner_key: str) -> None:
import httpx
from imptune.services.session import COOKIE_NAME
with httpx.Client(
base_url=live_server, follow_redirects=True, cookies={COOKIE_NAME: owner_key}
) as api:
response = api.post(
"/drivers/upload",
files={"file": ("e2e_rename_pkg.zip", _driver_zip(), "application/zip")},
)
assert response.status_code == 200, response.text
def test_driver_rename_dialog_prefills_the_zip_name(
page, live_server: str, _e2e_owner_key: str
) -> None:
"""With no rename yet, the input is empty and the ZIP name is its placeholder."""
_upload_driver(live_server, _e2e_owner_key)
page.goto(f"{live_server}/drivers", wait_until="domcontentloaded")
row = page.locator("tr", has=page.locator("text=e2e_rename_pkg.zip"))
row.first.wait_for()
row.first.locator(EDIT_BUTTON).click()
page.wait_for_selector("dialog[open]")
field = page.locator("dialog[open] input[name='display_name']")
assert field.input_value() == ""
assert field.get_attribute("placeholder") == "e2e_rename_pkg.zip"
def test_driver_rename_updates_the_row(page, live_server: str, _e2e_owner_key: str) -> None:
"""Saving swaps the table in place and shows the new label over the ZIP name."""
_upload_driver(live_server, _e2e_owner_key)
page.goto(f"{live_server}/drivers", wait_until="domcontentloaded")
row = page.locator("tr", has=page.locator("text=e2e_rename_pkg.zip"))
row.first.wait_for()
row.first.locator(EDIT_BUTTON).click()
page.wait_for_selector("dialog[open]")
page.fill("dialog[open] input[name='display_name']", "Ground floor MFP")
# The dialog holds several submit buttons (icon upload, web search); the
# footer's Save is the one bound to the rename form.
page.click("dialog[open] .dialog-foot button[type='submit']")
page.wait_for_selector("text=Ground floor MFP")
assert page.locator("dialog[open]").count() == 0
# Renaming does not hide what is actually stored on disk.
assert page.locator("text=e2e_rename_pkg.zip").count() >= 1