Files
rust-discovery/tests/cloud_mvp_guards.py
T
2026-11-13 13:49:11 +01:00

93 lines
5.3 KiB
Python

"""Cloud acceptance regressions: no network, client, approval or database."""
import io
import json
from pathlib import Path
import sys
import unittest
from unittest.mock import patch
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'deploy'))
import cloud_mvp_acceptance as cloud
import cloud_root_acceptance as root_acceptance
class CloudGuards(unittest.TestCase):
def test_native_chrono_nanoseconds_are_read_without_repeating_the_job(self):
value='2026-11-13T10:21:39.987858213Z'
self.assertEqual(cloud.contract.timestamp(value),cloud.contract.timestamp('2026-11-13T10:21:39.987858Z'))
self.assertEqual(value,'2026-11-13T10:21:39.987858213Z')
for fraction in ('1','12','123','1234','12345','123456','123456789'):
self.assertEqual(cloud.contract.timestamp('2026-11-13T10:21:39.'+fraction+'Z').microsecond,
int((fraction+'000000')[:6]))
with self.assertRaises(cloud.contract.GuardError):cloud.contract.timestamp('2026-11-13T10:21:39.987858')
def test_sse_empty_initial_data_is_not_a_json_response(self):
class Response:
status=200
def __init__(self):self.stream=io.BytesIO(b'event: message\ndata: \n\nevent: message\ndata: {"jsonrpc":"2.0","id":1,"result":{"ok":true}}\n\n')
def getheaders(self):return [('Content-Type','text/event-stream')]
def getheader(self,key,default=''):return 'text/event-stream'
def readline(self,size):return self.stream.readline(size)
class Connection:
def __init__(self,*args,**kwargs):pass
def request(self,*args,**kwargs):pass
def getresponse(self):return Response()
def close(self):pass
with patch.object(cloud.http.client,'HTTPSConnection',Connection), \
patch.object(cloud.ssl,'create_default_context'), \
patch.object(Path,'read_text',return_value='synthetic-test-bearer'):
response=cloud.server(dict(kind='http',path='/mcp',method='POST',body={},headers={}))
self.assertTrue(response['data']['result']['ok'])
def test_cloud_adapter_cannot_target_an_approval_or_enrollment_endpoint(self):
for path in ('/agent/v1/enroll','/api/v1/tenants/t/operation-plans/p/authorizations','/auth/dev/login'):
with self.subTest(path=path),self.assertRaisesRegex(RuntimeError,'no administrative mutation endpoint'):
cloud.server(dict(kind='http',path=path))
def test_caller_cannot_inject_interactive_or_bearer_credentials(self):
for key in ('Cookie','authorization','X-CSRF-Token'):
with patch.object(cloud.ssl,'create_default_context'),self.subTest(key=key), \
self.assertRaisesRegex(RuntimeError,'caller credential injection'):
cloud.server(dict(kind='http',path='/mcp',headers={key:'synthetic'},body={},method='POST'))
def test_browser_authorization_uses_only_the_actual_ui(self):
code=(cloud.ROOT/'deploy/browser/cloud_approve.cjs').read_text()
self.assertNotIn('page.request.',code)
self.assertNotIn('page.evaluate(',code)
self.assertNotIn(".post('/",code)
self.assertIn('await approve.click()',code)
self.assertIn('ignoreHTTPSErrors: false',code)
def test_cloud_root_runner_refuses_reboot_before_starting_a_browser(self):
value=dict(profile='dev/test',case='reboot_last',expected_principal='simulated-testprincipal-a')
with patch.object(cloud.contract,'protected_json',return_value=value), \
patch.object(cloud,'prepare_browser') as trust, \
patch.object(root_acceptance.browser.subprocess,'run') as run:
with self.assertRaisesRegex(RuntimeError,'fixed cloud root cases only'):
root_acceptance.browser.run('/synthetic/case.json')
trust.assert_not_called();run.assert_not_called()
def test_root_preparation_requires_explicit_root_identity(self):
value=dict(profile='dev/test',case='service_start',phase='prepare',expected_principal='simulated-testprincipal-a',
request=dict(parameters=dict(execution_identity='agent_service')))
with patch.object(cloud.contract,'protected_json',return_value=value), \
patch.object(cloud,'prepare_browser') as trust:
with self.assertRaisesRegex(RuntimeError,'root preparation only'):
root_acceptance.browser.run('/synthetic/case.json',prepare=True)
trust.assert_not_called()
def test_unresolved_cloud_effect_blocks_every_new_root_request(self):
identity=dict(agent_id='synthetic-agent',asset_id='synthetic-asset')
with patch.object(sys,'argv',['cloud_root_acceptance.py','--case','apt_native_install']), \
patch.object(Path,'exists',return_value=False), \
patch.object(cloud.contract,'protected_json',return_value=dict(identity=identity)), \
patch.object(cloud,'Backend') as backend, \
patch.object(cloud.bootstrap,'rpc',return_value=dict(unresolved=1)), \
patch.object(root_acceptance.browser,'run') as browser:
with self.assertRaisesRegex(cloud.contract.GuardError,'unresolved central job'):
root_acceptance.main()
backend.return_value.call.assert_not_called();browser.assert_not_called()
if __name__=='__main__':unittest.main()