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

22 lines
1.1 KiB
Python

import os,tempfile,importlib.util
os.environ['HERMES_HOME']=tempfile.mkdtemp()
from pathlib import Path
import json
(Path(os.environ['HERMES_HOME'])/'snapshot-manifest.json').write_text(json.dumps({'schemaVersion':1,'source':'snapshot','capturedAt':'2020-01-01T00:00:00Z'}))
from fastapi.testclient import TestClient
from starlette.websockets import WebSocketDisconnect
spec=importlib.util.spec_from_file_location('runtime_serve','/contract/serve.py');m=importlib.util.module_from_spec(spec);spec.loader.exec_module(m)
m.secret=lambda name:'test-only-'+'x'*32
app=m.build_app()
# A permitted session must NOT unlock powerful WebSocket routes.
client=TestClient(app)
r=client.post('/auth/password-login',json={'provider':'basic','username':m.secret('hermes_username'),'password':m.secret('hermes_password')})
assert r.status_code==200
for path in ['/api/ws','/api/pty','/api/events']:
try:
with client.websocket_connect(path):
raise AssertionError('Read-only boundary admitted socket '+path)
except WebSocketDisconnect as e:
assert e.code==1008, (path,e.code)
print('PASS authenticated sockets denied1008')