Give a network device somewhere to put its IP

The network device form had no IP field, so the one thing people look up a
switch for could not be entered. The API had accepted an ipaddress since the
legacy import work - create, update and every read already carried it - but
nothing in the UI ever sent one, which also left the device off the "Devices on
this network" list, since that matches on the IP a device does not have.

The field sits next to Hostname and round-trips through the Communication row
the platform keeps IPs in, the same way the PC and printer forms do.

Clearing it now clears the stored address. The upsert helper returned early on a
blank value despite a docstring promising it cleared - harmless while no form
could submit one, wrong the moment this field existed.
This commit is contained in:
cproudlock
2026-08-07 10:30:38 -04:00
parent 76c184fe91
commit ed9c91c47d
3 changed files with 54 additions and 2 deletions

View File

@@ -57,6 +57,39 @@ def test_update_network_device_ipaddress_upserts(client, db, auth_headers, netwo
assert len(comms) == 1 and comms[0].ipaddress == '10.0.0.9'
def test_clearing_the_ip_takes_it_back_out(client, db, auth_headers, network_setup):
"""Blanking the form field must clear the stored IP.
The upsert helper returned early on a blank value, so an address could be
typed and never removed - invisible until the form grew an IP field.
"""
from shopdb.core.models import Communication
created = client.post('/api/network', json={
'assetnumber': 'SW-09', 'name': 'Switch', 'ipaddress': '10.0.0.5',
}, headers=auth_headers).get_json()['data']
devid = created['networkdevice']['networkdeviceid']
cleared = client.put(f'/api/network/{devid}', json={'ipaddress': ''},
headers=auth_headers)
assert cleared.status_code == 200, cleared.get_json()
assert not cleared.get_json()['data']['ipaddress']
comms = Communication.query.filter_by(assetid=created['assetid']).all()
assert len(comms) == 1 and comms[0].ipaddress is None
def test_omitting_ipaddress_leaves_it_alone(client, db, auth_headers, network_setup):
"""A PUT that never mentions the field must not wipe the stored IP."""
created = client.post('/api/network', json={
'assetnumber': 'SW-10', 'name': 'Switch', 'ipaddress': '10.0.0.7',
}, headers=auth_headers).get_json()['data']
devid = created['networkdevice']['networkdeviceid']
updated = client.put(f'/api/network/{devid}', json={'hostname': 'sw-10'},
headers=auth_headers)
assert updated.get_json()['data']['ipaddress'] == '10.0.0.7'
def test_seed_reference_data_creates_ip_commtype(app, db):
"""The reference-data seed now creates the IP communication type the asset
import depends on."""