Display role vocabulary, migration commit fix, stale enforcement reports

This commit is contained in:
2026-08-13 09:30:40 -04:00
parent d327ea1a5c
commit 5dcc7e30af
16 changed files with 490 additions and 46 deletions

View File

@@ -100,7 +100,11 @@ def test_non_admin_cannot_update_or_delete(client, db, auth_headers,
def test_lobby_role_needs_no_businessunit_and_resolves_path(client, db, auth_headers):
"""A lobby-role mapping needs no business unit; display-role returns its path."""
"""A Lobby mapping needs no business unit; display-role returns its path.
Posts the RETIRED lowercase spelling on purpose: an existing caller must not
start failing over a capital, and what comes back is the canonical name.
"""
created = client.post('/api/dashboarddefaults', json={
'ipaddress': '10.20.30.99', 'displayrole': 'lobby',
'description': 'Front lobby TV',
@@ -110,17 +114,19 @@ def test_lobby_role_needs_no_businessunit_and_resolves_path(client, db, auth_hea
resolved = client.get('/api/dashboarddefaults/display-role?ipaddress=10.20.30.99')
data = resolved.get_json()['data']
assert data['role'] == 'lobby'
assert data['role'] == 'Lobby'
assert data['path'] == '/tv'
assert data['businessunitid'] is None
def test_partskiosk_role_resolves_path(client, db, auth_headers):
def test_retired_partskiosk_spelling_maps_to_3dprintroom(client, db, auth_headers):
# 'partskiosk' was core's name for the room the kiosks call '3DPrintRoom'.
# It still posts, and stores as the name the kiosk itself would report.
client.post('/api/dashboarddefaults', json={
'ipaddress': '10.20.30.77', 'displayrole': 'partskiosk',
}, headers=auth_headers)
data = client.get('/api/dashboarddefaults/display-role?ipaddress=10.20.30.77').get_json()['data']
assert data['role'] == 'partskiosk'
assert data['role'] == '3DPrintRoom'
assert data['path'] == '/parts-kiosk'
@@ -143,7 +149,7 @@ def test_fqdn_mapping_create_and_resolve(client, db, auth_headers):
# resolve by FQDN (case-insensitive)
r = client.get('/api/dashboarddefaults/display-role?fqdn=fabc123.device.geaerospace.net')
body = r.get_json()['data']
assert body['role'] == 'lobby'
assert body['role'] == 'Lobby'
assert body['path'] == '/tv'
@@ -164,3 +170,63 @@ def test_invalid_role_rejected(client, db, auth_headers):
def test_unmapped_ip_display_role_null(client, db):
data = client.get('/api/dashboarddefaults/display-role?ipaddress=9.9.9.9').get_json()['data']
assert data['role'] is None and data['path'] is None
# --- role vocabulary -------------------------------------------------------
#
# The role names are not ours to choose: they are the literal values a person
# types into C:\Enrollment\display-type.txt on the kiosk, which the GE-Enforce
# dispatcher reads. These tests exist because core and the kiosks each used to
# hold their own map, and a display could report a role core could not store.
def test_display_roles_are_the_kiosk_vocabulary():
from shopdb.core.models.dashboarddefault import DISPLAY_ROLE_PATHS
assert DISPLAY_ROLE_PATHS == {
'Dashboard': '/shopfloor',
'Lobby': '/tv',
'3DPrintRoom': '/parts-kiosk',
}
def test_geenforce_display_targets_are_core_roles():
"""The display scope must not carry a second copy of the map."""
from shopdb.core.models.dashboarddefault import DISPLAY_ROLE_PATHS
from plugins.geenforce.seed_display_scope import DISPLAY_TYPE_TARGETS
assert DISPLAY_TYPE_TARGETS is DISPLAY_ROLE_PATHS
def test_normalize_display_role_accepts_any_casing():
from shopdb.core.models.dashboarddefault import normalize_display_role
for value in ('Lobby', 'lobby', 'LOBBY', ' Lobby '):
assert normalize_display_role(value) == 'Lobby'
assert normalize_display_role('3dprintroom') == '3DPrintRoom'
assert normalize_display_role('DASHBOARD') == 'Dashboard'
def test_normalize_display_role_maps_retired_spelling():
from shopdb.core.models.dashboarddefault import normalize_display_role
assert normalize_display_role('partskiosk') == '3DPrintRoom'
assert normalize_display_role('PartsKiosk') == '3DPrintRoom'
def test_normalize_display_role_refuses_to_guess():
from shopdb.core.models.dashboarddefault import normalize_display_role
for value in (None, '', ' ', 'bogus', '3d', 'lobbyy'):
assert normalize_display_role(value) is None
def test_mixed_case_role_stores_canonically(client, db, auth_headers):
resp = client.post('/api/dashboarddefaults', json={
'ipaddress': '10.20.30.31', 'displayrole': 'LOBBY',
}, headers=auth_headers)
assert resp.status_code == 201, resp.get_json()
assert resp.get_json()['data']['displayrole'] == 'Lobby'
def test_legacy_stored_value_still_resolves_a_path(db):
"""A row written before the merge keeps working without being rewritten."""
from shopdb.core.models.dashboarddefault import DashboardDefault
row = DashboardDefault(ipaddress='10.20.30.32', displayrole='partskiosk')
db.session.add(row)
db.session.flush()
assert row.displaypath == '/parts-kiosk'

View File

@@ -227,3 +227,39 @@ def test_report_without_a_subtype_stores_none(db):
stored = ManifestEnforcementReport.query.filter_by(
hostname='WJPC77', iscurrent=True).one()
assert stored.subtype is None
def test_reported_subtype_is_normalised_to_the_role_vocabulary(db):
"""display-type.txt is hand-edited, so its casing drifts across the fleet.
Normalising on the way in keeps the fleet table reading one vocabulary
instead of whatever each kiosk happened to be typed as.
"""
from plugins.geenforce.service import record_enforcement_report
for index, reported in enumerate(('lobby', 'LOBBY', ' Lobby ')):
host = f'WJKIOSKCASE{index}'
record_enforcement_report({'hostname': host,
'scopename': 'gea-shopfloor-display',
'subtype': reported, 'counts': {}})
db.session.flush()
stored = ManifestEnforcementReport.query.filter_by(
hostname=host, iscurrent=True).one()
assert stored.subtype == 'Lobby', reported
def test_reported_subtype_keeps_an_unknown_value_verbatim(db):
"""An unrecognised subtype is a typo on a kiosk or a role nobody declared.
Both are worth SEEING in the fleet table, so the value is stored as sent
rather than blanked or guessed at.
"""
from plugins.geenforce.service import record_enforcement_report
record_enforcement_report({'hostname': 'WJKIOSKODD', 'scopename': 'gea-shopfloor-display',
'subtype': 'Lobbby', 'counts': {}})
db.session.flush()
stored = ManifestEnforcementReport.query.filter_by(
hostname='WJKIOSKODD', iscurrent=True).one()
assert stored.subtype == 'Lobbby'

View File

@@ -185,3 +185,103 @@ def test_report_missing_hostname_rejected(client, db, app, auth_headers):
resp = client.post('/api/geenforce/report', json={'scopename': 'x'},
headers={'X-API-Key': secret})
assert resp.status_code == 400
# -- going quiet ------------------------------------------------------------
#
# A report records how one cycle went; nothing ages it. A PC switched off after
# a clean run therefore kept 'ok' indefinitely and read as healthy - which is
# how a machine offline for over a day still showed 'ok'.
def _age_report(db, hostname, minutes):
"""Push a stored report's server-side receivedat back in time."""
from datetime import timedelta
from plugins.geenforce.models import ManifestEnforcementReport
from plugins.geenforce.service import _utcnow
(ManifestEnforcementReport.query
.filter_by(hostname=hostname, iscurrent=True)
.update({'receivedat': _utcnow() - timedelta(minutes=minutes)}))
db.session.commit()
def _post_clean_report(client, secret, hostname):
return client.post('/api/geenforce/report', json={
'hostname': hostname, 'scopename': 'gea-shopfloor-cmm',
'appliedversion': 1, 'enforcerversion': '2.6',
'counts': {'installed': 1, 'skipped': 0, 'failed': 0, 'filtered': 0},
'results': [{'name': 'Alpha', 'action': 'installed'}],
}, headers={'X-API-Key': secret})
def test_a_pc_reporting_now_is_not_stale(client, db, app, auth_headers):
_seed_and_publish(app)
secret = _token(client, auth_headers, ['geenforce.report'])
_post_clean_report(client, secret, 'WJCMM01')
row = client.get('/api/geenforce/reports',
headers=auth_headers).get_json()['data'][0]
assert row['status'] == 'ok'
assert row['isstale'] is False
assert row['staleafterminutes'] == 30
def test_a_pc_that_stopped_reporting_goes_stale(client, db, app, auth_headers):
_seed_and_publish(app)
secret = _token(client, auth_headers, ['geenforce.report'])
_post_clean_report(client, secret, 'WJCMM01')
_age_report(db, 'WJCMM01', minutes=60 * 26) # offline over a day
row = client.get('/api/geenforce/reports',
headers=auth_headers).get_json()['data'][0]
assert row['isstale'] is True
# The REPORTED status is left alone - it is still a true record of the last
# cycle, and the UI shows it in the tooltip behind the stale badge.
assert row['status'] == 'ok'
def test_stale_boundary_is_the_configured_threshold(client, db, app, auth_headers):
_seed_and_publish(app)
secret = _token(client, auth_headers, ['geenforce.report'])
_post_clean_report(client, secret, 'WJCMM01')
_age_report(db, 'WJCMM01', minutes=29)
row = client.get('/api/geenforce/reports',
headers=auth_headers).get_json()['data'][0]
assert row['isstale'] is False, 'inside the window is not stale'
_age_report(db, 'WJCMM01', minutes=31)
row = client.get('/api/geenforce/reports',
headers=auth_headers).get_json()['data'][0]
assert row['isstale'] is True, 'past the window is stale'
def test_stale_threshold_is_a_setting(client, db, app, auth_headers):
from shopdb.api import Setting
_seed_and_publish(app)
secret = _token(client, auth_headers, ['geenforce.report'])
_post_clean_report(client, secret, 'WJCMM01')
_age_report(db, 'WJCMM01', minutes=45)
Setting.set('geenforce_reportstaleminutes', '120', valuetype='integer',
category='geenforce')
db.session.commit()
row = client.get('/api/geenforce/reports',
headers=auth_headers).get_json()['data'][0]
assert row['isstale'] is False
assert row['staleafterminutes'] == 120
def test_zero_disables_the_stale_check(client, db, app, auth_headers):
from shopdb.api import Setting
_seed_and_publish(app)
secret = _token(client, auth_headers, ['geenforce.report'])
_post_clean_report(client, secret, 'WJCMM01')
_age_report(db, 'WJCMM01', minutes=60 * 24 * 7)
Setting.set('geenforce_reportstaleminutes', '0', valuetype='integer',
category='geenforce')
db.session.commit()
row = client.get('/api/geenforce/reports',
headers=auth_headers).get_json()['data'][0]
assert row['isstale'] is False
assert row['staleafterminutes'] == 0