Files
mtusi2026lab5/student-tests/operations_panel.py
T

39 lines
2.5 KiB
Python

import base64
from support import *
# The operator session is supplied locally, never stored in evidence.
import os
password=os.environ.get('OPS_TEST_PASSWORD')
if not password:raise SystemExit('Set OPS_TEST_PASSWORD for the configured local operator')
c=Client('http://localhost:18085')
credential=base64.b64encode((os.environ.get('OPS_TEST_USERNAME','n1k0der')+':'+password).encode()).decode()
session=parsed(c,'POST','/api/v1/auth/sessions',201,headers={'Authorization':'Basic '+credential})
owner={'id':session['user']['id'],'token':session['token']}
a,b,e,chat=c.bootstrap()
check(parsed(c,'GET','/api/ui/operator',actor=owner)['enabled'],'operator not enabled')
check(not parsed(c,'GET','/api/ui/operator',actor=a)['enabled'],'regular user promoted')
c.call('GET','/api/ui/operations',401)
c.call('GET','/api/ui/operations',404,actor=a)
snapshot=parsed(c,'GET','/api/ui/operations',actor=owner)
check(all(name in snapshot['counts'] for name in ('queued','processing','ready','failed')),'counts missing')
check(snapshot['services']['postgres'] and snapshot['services']['s3'] and snapshot['services']['broker'],'probes failed')
user=parsed(c,'GET','/api/ui/users/n1k0der',actor=a)
check(user['id']==owner['id'] and 'password_hash' not in user,'lookup unsafe')
c.call('GET','/api/ui/users/n1k0der',401)
attachment=upload(c,a,chat)['json'];ready(c,a,attachment)
path='/api/ui/operations/attachments/'+attachment['id']+'/replay'
c.call('POST',path,404,actor=a);c.call('POST',path,401);c.call('POST',path,409,actor=owner)
# A controlled failed-state fixture retains the original staging bytes for replay.
compose('stop','worker')
try:
attachment=upload(c,a,chat)['json'];id=job_id(attachment)
sql("UPDATE attachment_jobs SET state='failed',error_code='test_fixture' WHERE id='"+id+"'; UPDATE attachments SET status='failed',error_code='test_fixture' WHERE id='"+attachment['id']+"'")
path='/api/ui/operations/attachments/'+attachment['id']+'/replay'
c.call('POST',path,202,actor=owner);c.call('POST',path,409,actor=owner)
check(sql("SELECT replay_count FROM attachment_jobs WHERE id='"+id+"'")=='1','replay duplicated')
finally:compose('up','-d','--no-deps','worker')
def completed():
result=c.call('GET','/api/v1/attachments/'+attachment['id'],[200,503],actor=a,schema='Attachment')
return result['json']['status']=='ready' if result['status']==200 else False
wait(completed)
print('PASS: operator-only metrics and replay; anonymous 401; regular user 404; exact login lookup; one replay completes with real worker')