Add device-bound robot notifications and automatic OAuth cards
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
import hashlib
|
||||
import json
|
||||
import sys
|
||||
import time
|
||||
import types
|
||||
from pathlib import Path
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
sys.path.insert(0,str(Path(__file__).resolve().parents[1]))
|
||||
from relay import work_client
|
||||
from relay.crypto import pem_private, verified, canonical
|
||||
from relay.notification_client import push, register_notification_tool
|
||||
from relay.work_client import WorkClient, WorkError
|
||||
from test_notifications import channel
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_client_auto_authorization_card_once(channel, tmp_path, monkeypatch):
|
||||
# DPAPI is separately implemented; keep the test independent of OS account keys.
|
||||
monkeypatch.setattr(work_client,'protect',lambda b:b)
|
||||
monkeypatch.setattr(work_client,'unprotect',lambda b:b)
|
||||
(tmp_path/'device.json').write_text(json.dumps({'host_id':'a'*32,'owner':'ou_a'}))
|
||||
(tmp_path/'device.key').write_bytes(pem_private(channel['keys']['a'*32]))
|
||||
(tmp_path/'server.pem').write_bytes(channel['settings'].ca_cert.read_bytes())
|
||||
calls=[]
|
||||
def handler(request):
|
||||
payload=json.loads(request.content)
|
||||
calls.append((request.url.path,payload))
|
||||
if request.url.path=='/oauth/start':
|
||||
return httpx.Response(201,json={'state':'fake-state','claim_secret':'fake-claim-secret',
|
||||
'authorization_url':'https://work.example.test/authorize?state=fake-state','expires_in':600})
|
||||
plain={k:v for k,v in payload.items() if k not in {'host_id','proof'}}
|
||||
signed=verified(payload['proof'],channel['keys']['a'*32].public_key())
|
||||
assert signed['payload_hash']==hashlib.sha256(canonical(plain)).hexdigest()
|
||||
assert 'fake-claim-secret' not in json.dumps(payload)
|
||||
return httpx.Response(200,json={'status':'sent','message_id':'om_test'})
|
||||
client=WorkClient(tmp_path,'https://relay.example.test',transport=httpx.MockTransport(handler))
|
||||
try:
|
||||
url=client.authorize()
|
||||
assert client.authorize()==url
|
||||
assert [c[0] for c in calls]==['/oauth/start','/device/notifications']
|
||||
assert client.read(client.pending)['notification_status']=='sent'
|
||||
finally:client.close()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_old_ecs_failure_keeps_local_authorization(channel,tmp_path,monkeypatch):
|
||||
monkeypatch.setattr(work_client,'protect',lambda b:b)
|
||||
monkeypatch.setattr(work_client,'unprotect',lambda b:b)
|
||||
(tmp_path/'device.json').write_text(json.dumps({'host_id':'a'*32,'owner':'ou_a'}))
|
||||
(tmp_path/'device.key').write_bytes(pem_private(channel['keys']['a'*32]))
|
||||
(tmp_path/'server.pem').write_bytes(channel['settings'].ca_cert.read_bytes())
|
||||
attempts=[]
|
||||
def handler(request):
|
||||
if request.url.path=='/oauth/start':
|
||||
return httpx.Response(201,json={'state':'fake-state','claim_secret':'private-claim',
|
||||
'authorization_url':'https://work.example.test/authorize','expires_in':600})
|
||||
attempts.append(1)
|
||||
return httpx.Response(404)
|
||||
client=WorkClient(tmp_path,'https://relay.example.test',transport=httpx.MockTransport(handler))
|
||||
try:
|
||||
assert client.authorize()=='https://work.example.test/authorize'
|
||||
assert client.authorize()=='https://work.example.test/authorize'
|
||||
assert len(attempts)==1
|
||||
assert client.read(client.pending)['notification_status']=='unconfirmed'
|
||||
finally:client.close()
|
||||
|
||||
|
||||
def test_native_notification_tool_has_no_recipient(monkeypatch):
|
||||
found={}
|
||||
registry=types.SimpleNamespace(register=lambda **kw:found.update(kw))
|
||||
monkeypatch.setitem(sys.modules,'tools',types.ModuleType('tools'))
|
||||
monkeypatch.setitem(sys.modules,'tools.registry',types.SimpleNamespace(registry=registry))
|
||||
toolsets=types.SimpleNamespace(TOOLSETS={'hainuo_work':{'tools':[]}},_HERMES_CORE_TOOLS=[])
|
||||
monkeypatch.setitem(sys.modules,'toolsets',toolsets)
|
||||
register_notification_tool('not-used')
|
||||
assert found['name']=='work_notification'
|
||||
assert set(found['schema']['parameters']['properties'])=={'request_id','kind','text','title'}
|
||||
result=json.loads(found['handler']({'request_id':'request_0001','text':'hi','receive_id':'ou_b'}))
|
||||
assert result['status']=='unconfirmed'
|
||||
assert 'work_notification' in toolsets._HERMES_CORE_TOOLS
|
||||
Reference in New Issue
Block a user