Files
canina/infra/hermes-runtime/test_serve.py
T

32 lines
1.8 KiB
Python

import importlib.util, os, secrets, tempfile, threading, socket, time
from pathlib import Path
from fastapi.testclient import TestClient
os.environ['HERMES_HOME']=tempfile.mkdtemp(prefix='inventory-snapshot-')
os.environ['HERMES_SERVE_HEADLESS']='1'
spec=importlib.util.spec_from_file_location('runtime_serve','/contract/serve.py')
module=importlib.util.module_from_spec(spec);spec.loader.exec_module(module)
values={'hermes_username':'contract','hermes_password':secrets.token_urlsafe(32),'hermes_signing_key':secrets.token_urlsafe(32)}
module.secret=lambda name:values[name]
app=module.build_app()
import uvicorn
server=uvicorn.Server(uvicorn.Config(app,host='0.0.0.0',port=9119,lifespan='off',access_log=False,log_level='warning'))
thread=threading.Thread(target=server.run,daemon=True);thread.start()
for _ in range(200):
if server.started:break
time.sleep(.05)
assert server.started
import httpx
with httpx.Client(base_url='http://127.0.0.1:9119') as c:
assert c.get('/api/profiles').status_code==401
assert c.post('/auth/password-login',json={'provider':'basic','username':values['hermes_username'],'password':'wrong'}).status_code==401
r=c.post('/auth/password-login',json={'provider':'basic','username':values['hermes_username'],'password':values['hermes_password']})
assert r.status_code==200 and r.json()['ok'] is True
assert c.get('/api/profiles').status_code==200
assert c.get('/api/health').json()['auth_required'] is True
assert c.post('/api/cron/jobs/example/resume').status_code==404
assert c.get('/api/config').status_code==404
assert c.get('/api/ws').status_code==404
print('PASS real TCP pinned serve: unauth401 wrong-password401 login200 inventory200 health.auth_required=true mutations/config/ws404')
server.should_exit=True;thread.join(10)
assert not thread.is_alive()