railiance-platform/tests/test_root_custody_finish.py
codex a252447058
All checks were successful
CI Smoke / host-smoke (push) Successful in 0s
CI Smoke / container-smoke (push) Successful in 1s
Prove isolated custody recovery and prepare governed offsite finish
Assistant: codex
Assistant-Model: gpt-6.1-sol
Assistant-Session: 01a1029d-2c93-7500-a0e0-e3e089668b29
2026-10-03 20:47:48 +02:00

69 lines
3.6 KiB
Python

import configparser
import importlib.util
import json
from pathlib import Path
import sys
import tempfile
import subprocess
import unittest
from unittest.mock import patch
ROOT=Path(__file__).resolve().parents[1]
sys.path.insert(0,str(ROOT/'scripts'))
import root_custody_finish as finish
import root_grant_attended as owner
spec=importlib.util.spec_from_file_location('validator',ROOT.parent/'net-kingdom/tools/custody-record/validate.py')
validator=importlib.util.module_from_spec(spec);spec.loader.exec_module(validator)
class FinishTests(unittest.TestCase):
def test_retained_source_copy_contains_latest_valid_history(self):
path=ROOT/'docs/evidence/root-custody-history/baseline.json'
state,bundle=finish.checked_bundle(path,validator)
self.assertEqual(state['document']['entries'],[])
self.assertIn(state,json.loads(bundle)['history'])
def test_current_historical_share_is_classified_without_values(self):
with tempfile.TemporaryDirectory() as tmp:
path=Path(tmp)/'rclone.conf'
path.write_text('[nextcloud-backup]\nurl=https://example.invalid/public.php/dav/filesdrop/TEST-SHARE-VALUE\nuser=TEST-SHARE-VALUE\n')
result=finish.historical_config_comparison(path,{'NC_WEBDAV_TOKEN':'TEST-SHARE-VALUE'})
self.assertTrue(result['historical_share_matches_current_lane'])
self.assertNotIn('TEST-SHARE-VALUE',json.dumps(result))
result=finish.historical_config_comparison(path,{'NC_WEBDAV_TOKEN':'CURRENT-DIFFERENT'})
self.assertFalse(result['historical_share_matches_current_lane'])
self.assertEqual(result['historical_credential_status'],'historical-owner-and-revocation-unverified')
def test_unverified_upload_destination_refused_before_network(self):
lane={'data':{'NC_WEBDAV_URL':'https://unapproved.invalid/private','NC_WEBDAV_TOKEN':'TEST'}}
with patch.object(finish,'request') as network:
with self.assertRaises(ValueError):
finish.roundtrip(b'{}',lane,{'BACKUP_USERNAME':'Backup','BACKUP_PASSWORD':'TEST'},network=network)
network.assert_not_called()
def test_actual_age_roundtrip_with_disposable_key_and_memory_provider(self):
key=subprocess.run(['age-keygen'],capture_output=True,check=True).stdout.decode()
lane={'data':{'NC_WEBDAV_URL':finish.HOST+'/public.php/dav/filesdrop/DISPOSABLE-SHARE',
'NC_WEBDAV_TOKEN':'DISPOSABLE-SHARE','AGE_PRIVATE_KEY':key},'metadata':{'version':9}}
stored={}
def memory_provider(url,method='GET',body=None,auth=None,headers=None):
if method=='PUT':
self.assertEqual(headers['If-None-Match'],'*')
stored['encrypted']=body
return 201,b''
return 200,stored['encrypted']
receipt=finish.roundtrip(b'{"nonsecret":"receipt-fixture"}',lane,
{'BACKUP_USERNAME':'Backup','BACKUP_PASSWORD':'DISPOSABLE-PASSWORD'},
network=memory_provider,get_quota=lambda auth:{'available_bytes':1048576})
self.assertTrue(receipt['decrypted_history_matches'])
self.assertNotIn(key,json.dumps(receipt))
self.assertNotIn('DISPOSABLE-SHARE',json.dumps(receipt))
def test_missing_latest_history_cannot_make_backup(self):
with tempfile.TemporaryDirectory() as tmp:
path=Path(tmp)/'baseline.json'
path.write_text((ROOT/'docs/evidence/root-custody-history/baseline.json').read_text())
(path.parent/'history').mkdir()
with self.assertRaises(ValueError):
finish.checked_bundle(path,validator)
if __name__=='__main__':unittest.main()