am-freescan/scripts/verify_robot_connections.py

61 lines
3.9 KiB
Python
Raw Normal View History

"""Verify endpoint UI API boundaries without changing addresses or controlling devices."""
import json
from pathlib import Path
from verify_cmvr_native import Client, TargetReadOnlyDb, require
def main():
client = Client().login()
db = TargetReadOnlyDb(client)
checks = []
def checked(name, condition):
require(condition, name)
checks.append(name)
def config_rows():
return db.select('SELECT service_type,robot_id,host,port,updated_by FROM lab_robot_service_endpoint ORDER BY service_type')
def commands():
return db.select('SELECT COUNT(*) AS n FROM lab_scanner_command')[0]['n']
before, command_count = config_rows(), commands()
try:
scanner = client.native('GET', '/scanner/configuration')['data']
rotary = client.native('GET', '/rotary-base/configuration')['data']
for config, kind in [(scanner, 'SCANNER'), (rotary, 'ROTARY_BASE')]:
robot = config['robotId']
path = '/resource/robots/' + robot + '/connections'
before_robot = client.native('GET', '/resource/robots/' + robot)['data']
data = client.native('GET', path)['data']
item = next(x for x in data['services'] if x['type'] == kind)
checked(kind + ':configured endpoint matches runtime', item['host'] == config['host'] and item['port'] == config['port'])
checked(kind + ':no credential fields', not any(key in json.dumps(data).lower() for key in ('apikey', 'password', 'bearer')))
payload = dict(type=kind, host=item['host'], port=item['port'], expectedHost=item['host'], expectedPort=item['port'])
response = client.native('PUT', path, payload)
checked(kind + ':same endpoint is an idempotent no-op', response['data']['robotId'] == robot)
for label, changes in [('fractional port', {'port': 1.5}), ('string port', {'port': '8080'}),
('zero port', {'port': 0}), ('out-of-range port', {'port': 65536}),
('URL instead of host', {'host': 'http://192.168.0.1'}),
('stale editor', {'expectedHost': 'stale.invalid'}), ('QUIC mutation', {'type': 'QUIC'})]:
bad = client.native('PUT', path, {**payload, **changes}, expected=None)
checked(kind + ':reject ' + label, bad.get('code') not in (0, 200))
after_robot = client.native('GET', '/resource/robots/' + robot)['data']
fields = ('robotId', 'ipAddress', 'port', 'quicNodeId', 'quicSessionId', 'quicBootId')
checked(kind + ':original robot and QUIC identities preserved', all(before_robot.get(k) == after_robot.get(k) for k in fields))
robot = 'am-freescan-sim-robot'
path = '/resource/robots/' + robot + '/connections'
checked('QUIC robot keeps original automatic connection', client.native('GET', path)['data']['services'] == [])
unauthorized = client.request('GET', '/admin-api/cmvr' + path, expected=None, auth=False)
checked('unauthenticated connection query rejected', unauthorized.get('code') == 401)
unauthorized = client.request('PUT', '/admin-api/cmvr' + path, {}, expected=None, auth=False)
checked('unauthenticated connection save rejected', unauthorized.get('code') == 401)
checked('no address changes persisted during verification', config_rows() == before)
checked('no scanner control command created', commands() == command_count)
result = dict(passed=True, checks=checks, count=len(checks), changedAddresses=False, deviceControlRequests=0)
target = Path(__file__).resolve().parents[1] / '.local/robot-connections-verification.json'
target.write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding='utf-8')
print(json.dumps(result, ensure_ascii=True))
finally:
db.close()
if __name__ == '__main__':
main()