Files
shopdb-flask/tests/test_plugins/test_machines_search.py
cproudlock afce24379c
All checks were successful
CI / naming (push) Successful in 3s
CI / frontend (push) Successful in 13s
CI / migrations-mysql (push) Successful in 39s
CI / backend (push) Successful in 3m42s
Reading anything needs a permission
A sweep of the URL map found 165 GET routes that answered anybody, with no
token at all: the whole asset inventory, every machine, PC, printer and network
device, the USB checkout history and who is holding which drive, the staff
directory with its photographs, every core report. Guest access had been argued
about one feature-flag at a time while the front door stood open.

Each of those now carries the read permission it belongs to. That also makes
the guest role mean something: a site can open exactly the reads it chooses,
and nothing follows by accident.

Thirty-three endpoints stay public, and each is named with its reason rather
than left to be inferred: the app shell and its bootstrap, the settings
allowlist (the handler already filters an anonymous caller down to branding and
a few keys), images fetched by <img src> which carries no Authorization header,
what a display kiosk renders before anybody signs in, the parts and USB kiosks,
the client scripts that run as SYSTEM before a user session exists, and a
secure-send link whose unguessable token IS the authorisation.

tests/test_core/test_public_read_surface.py is the standing gate, the companion
to the mutating-route sweep that has guarded writes for months. A new public
read fails CI until somebody adds it to that list in front of a reviewer. Four
further reads need a login but no permission, because the handler decides from
who you are - your own user record, your own API tokens, the role catalogue the
grid renders - and the same test proves each still refuses an anonymous caller.
Verified not vacuous by ungating one route and watching the sweep name it.

The employees plugin gained employees.view; the directory is names, SSOs,
photographs and reporting lines, and it had no permission of any kind.

Around ninety tests asserted payload shapes against endpoints that answered
anybody. They are about what the answer says, not about who may ask, so they
are signed in now rather than the gate being loosened. The ones that assert an
anonymous caller is refused were left exactly as they were.
2026-08-27 14:26:06 -04:00

96 lines
3.2 KiB
Python

"""Machines list search covers the columns the list actually shows.
The table displays Machine #, Name, Serial Number, Type and Vendor, so all of
them must be searchable. Searching a type name ('Part Washer') used to return
zero rows because the filter only looked at the asset fields.
"""
from shopdb.extensions import db as _db
from shopdb.core.models import Asset, AssetType
from shopdb.core.models.vendor import Vendor
from plugins.machines.models import Machine, MachineType
def _seed_machines():
"""Two machines: one Part Washer by Acme, one Lathe with no type or vendor.
The typeless machine guards the outer joins - it must still be findable by
its own asset fields.
"""
assettype = AssetType(assettype='machine')
_db.session.add(assettype)
_db.session.flush()
washertype = MachineType(machinetype='Part Washer')
lathe_vendor = Vendor(vendor='Acme Industrial')
_db.session.add_all([washertype, lathe_vendor])
_db.session.flush()
washer_asset = Asset(assetnumber='0410', name='Cell 4 washer',
serialnumber='SN-WASH-1', assettypeid=assettype.assettypeid)
plain_asset = Asset(assetnumber='0999', name='Old lathe',
serialnumber='SN-LATHE-9', assettypeid=assettype.assettypeid)
_db.session.add_all([washer_asset, plain_asset])
_db.session.flush()
_db.session.add_all([
Machine(assetid=washer_asset.assetid,
machinetypeid=washertype.machinetypeid,
vendorid=lathe_vendor.vendorid),
Machine(assetid=plain_asset.assetid),
])
_db.session.commit()
def _numbers(resp):
return sorted(item['assetnumber'] for item in resp.get_json()['data'])
def test_search_matches_machine_type_name(client, db, auth_headers):
_seed_machines()
resp = client.get('/api/machines?search=Part Washer', headers=auth_headers)
assert resp.status_code == 200, resp.get_json()
assert _numbers(resp) == ['0410']
def test_search_matches_vendor_name(client, db, auth_headers):
_seed_machines()
resp = client.get('/api/machines?search=Acme', headers=auth_headers)
assert resp.status_code == 200, resp.get_json()
assert _numbers(resp) == ['0410']
def test_search_still_matches_asset_fields(client, db, auth_headers):
_seed_machines()
for term, expected in [('0999', ['0999']),
('Old lathe', ['0999']),
('SN-WASH', ['0410'])]:
resp = client.get(f'/api/machines?search={term}', headers=auth_headers)
assert resp.status_code == 200, resp.get_json()
assert _numbers(resp) == expected, term
def test_search_does_not_drop_machines_without_type_or_vendor(client, db, auth_headers):
"""The outer joins must not turn into inner joins."""
_seed_machines()
resp = client.get('/api/machines?search=lathe', headers=auth_headers)
assert resp.status_code == 200, resp.get_json()
assert _numbers(resp) == ['0999']
def test_search_miss_returns_empty(client, db, auth_headers):
_seed_machines()
resp = client.get('/api/machines?search=zzzznotathing', headers=auth_headers)
assert resp.status_code == 200, resp.get_json()
assert _numbers(resp) == []