This repository was archived by the owner on Apr 8, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_workspace_context.py
More file actions
4600 lines (4268 loc) · 215 KB
/
test_workspace_context.py
File metadata and controls
4600 lines (4268 loc) · 215 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
import importlib.util
import os
import unittest
from unittest import mock
from urllib.parse import urlparse
ROOT = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..'))
WORKSPACE_PY = os.path.join(ROOT, 'workspace.py')
def _load_workspace(name):
spec = importlib.util.spec_from_file_location(name, WORKSPACE_PY)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
class _Headers(dict):
def get(self, key, default=None):
return super().get(key, default)
class WorkspaceContextTests(unittest.TestCase):
def setUp(self):
self.workspace = _load_workspace('kernel_workspace_context_test')
self.orig_workspace_org_id = self.workspace.WORKSPACE_ORG_ID
self.orig_runtime_host_identity_file = self.workspace.RUNTIME_HOST_IDENTITY_FILE
self.orig_runtime_admission_file = self.workspace.RUNTIME_ADMISSION_FILE
self.orig_federation_peers_file = self.workspace.FEDERATION_PEERS_FILE
self.orig_federation_replay_file = self.workspace.FEDERATION_REPLAY_FILE
self.orig_federation_signing_secret = self.workspace.FEDERATION_SIGNING_SECRET
self.orig_load_orgs = self.workspace.load_orgs
self.orig_load_workspace_credentials = self.workspace._load_workspace_credentials
self.orig_load_host_identity = self.workspace.load_host_identity
self.orig_load_admission_registry = self.workspace.load_admission_registry
self.orig_runtime_host_state = self.workspace._runtime_host_state
self.orig_federation_authority = self.workspace._federation_authority
self.orig_refresh_peer_registry_entry = self.workspace.refresh_peer_registry_entry
self.orig_load_peer_registry = self.workspace.load_peer_registry
self.orig_log_event = self.workspace.log_event
self.orig_list_warrants = self.workspace.list_warrants
self.orig_get_warrant = self.workspace.get_warrant
self.orig_issue_warrant = self.workspace.issue_warrant
self.orig_review_warrant = self.workspace.review_warrant
self.orig_commitment_summary = self.workspace.commitment_summary
self.orig_list_commitments = self.workspace.list_commitments
self.orig_validate_commitment_for_delivery = self.workspace.validate_commitment_for_delivery
self.orig_validate_commitment_for_settlement = self.workspace.validate_commitment_for_settlement
self.orig_record_delivery_ref = self.workspace.record_delivery_ref
self.orig_record_settlement_ref = self.workspace.record_settlement_ref
self.orig_settle_commitment = self.workspace.settle_commitment
self.orig_case_summary = self.workspace.case_summary
self.orig_list_cases = self.workspace.list_cases
self.orig_blocking_commitment_ids = self.workspace.blocking_commitment_ids
self.orig_blocked_peer_host_ids = self.workspace.blocked_peer_host_ids
self.orig_blocking_commitment_case = self.workspace.blocking_commitment_case
self.orig_blocking_peer_case = self.workspace.blocking_peer_case
self.orig_maybe_block_commitment_settlement = self.workspace._maybe_block_commitment_settlement
self.orig_set_peer_trust_state = self.workspace.set_peer_trust_state
self.orig_ensure_case_for_delivery_failure = self.workspace.ensure_case_for_delivery_failure
self.orig_summarize_inbox_entries = self.workspace.summarize_inbox_entries
self.orig_get_execution_job = self.workspace.get_execution_job
self.orig_get_execution_job_by_local_warrant = self.workspace.get_execution_job_by_local_warrant
self.orig_list_execution_jobs = self.workspace.list_execution_jobs
self.orig_execution_job_summary = self.workspace.execution_job_summary
self.orig_sync_execution_job_for_local_warrant = self.workspace.sync_execution_job_for_local_warrant
self.orig_upsert_execution_job = self.workspace.upsert_execution_job
self.orig_list_witness_observations = self.workspace.list_witness_observations
self.orig_witness_archive_summary = self.workspace.witness_archive_summary
self.orig_settlement_adapter_readiness_snapshot = self.workspace.settlement_adapter_readiness_snapshot
self.orig_payout_plan_preview_queue_snapshot = self.workspace._payout_plan_preview_queue_snapshot
def tearDown(self):
self.workspace.WORKSPACE_ORG_ID = self.orig_workspace_org_id
self.workspace.RUNTIME_HOST_IDENTITY_FILE = self.orig_runtime_host_identity_file
self.workspace.RUNTIME_ADMISSION_FILE = self.orig_runtime_admission_file
self.workspace.FEDERATION_PEERS_FILE = self.orig_federation_peers_file
self.workspace.FEDERATION_REPLAY_FILE = self.orig_federation_replay_file
self.workspace.FEDERATION_SIGNING_SECRET = self.orig_federation_signing_secret
self.workspace.load_orgs = self.orig_load_orgs
self.workspace._load_workspace_credentials = self.orig_load_workspace_credentials
self.workspace.load_host_identity = self.orig_load_host_identity
self.workspace.load_admission_registry = self.orig_load_admission_registry
self.workspace._runtime_host_state = self.orig_runtime_host_state
self.workspace._federation_authority = self.orig_federation_authority
self.workspace.refresh_peer_registry_entry = self.orig_refresh_peer_registry_entry
self.workspace.load_peer_registry = self.orig_load_peer_registry
self.workspace.log_event = self.orig_log_event
self.workspace.list_warrants = self.orig_list_warrants
self.workspace.get_warrant = self.orig_get_warrant
self.workspace.issue_warrant = self.orig_issue_warrant
self.workspace.review_warrant = self.orig_review_warrant
self.workspace.commitment_summary = self.orig_commitment_summary
self.workspace.list_commitments = self.orig_list_commitments
self.workspace.validate_commitment_for_delivery = self.orig_validate_commitment_for_delivery
self.workspace.validate_commitment_for_settlement = self.orig_validate_commitment_for_settlement
self.workspace.record_delivery_ref = self.orig_record_delivery_ref
self.workspace.record_settlement_ref = self.orig_record_settlement_ref
self.workspace.settle_commitment = self.orig_settle_commitment
self.workspace.case_summary = self.orig_case_summary
self.workspace.list_cases = self.orig_list_cases
self.workspace.blocking_commitment_ids = self.orig_blocking_commitment_ids
self.workspace.blocked_peer_host_ids = self.orig_blocked_peer_host_ids
self.workspace.blocking_commitment_case = self.orig_blocking_commitment_case
self.workspace.blocking_peer_case = self.orig_blocking_peer_case
self.workspace._maybe_block_commitment_settlement = self.orig_maybe_block_commitment_settlement
self.workspace.set_peer_trust_state = self.orig_set_peer_trust_state
self.workspace.ensure_case_for_delivery_failure = self.orig_ensure_case_for_delivery_failure
self.workspace.summarize_inbox_entries = self.orig_summarize_inbox_entries
self.workspace.get_execution_job = self.orig_get_execution_job
self.workspace.get_execution_job_by_local_warrant = self.orig_get_execution_job_by_local_warrant
self.workspace.list_execution_jobs = self.orig_list_execution_jobs
self.workspace.execution_job_summary = self.orig_execution_job_summary
self.workspace.sync_execution_job_for_local_warrant = self.orig_sync_execution_job_for_local_warrant
self.workspace.upsert_execution_job = self.orig_upsert_execution_job
self.workspace.list_witness_observations = self.orig_list_witness_observations
self.workspace.witness_archive_summary = self.orig_witness_archive_summary
self.workspace.settlement_adapter_readiness_snapshot = self.orig_settlement_adapter_readiness_snapshot
self.workspace._payout_plan_preview_queue_snapshot = self.orig_payout_plan_preview_queue_snapshot
def test_configured_org_binds_process_context(self):
self.workspace._load_workspace_credentials = lambda: (None, None, None, None)
self.workspace.WORKSPACE_ORG_ID = 'org_b'
self.workspace.load_orgs = lambda: {
'organizations': {
'org_a': {'id': 'org_a', 'slug': 'a', 'name': 'A'},
'org_b': {'id': 'org_b', 'slug': 'b', 'name': 'B'},
}
}
ctx = self.workspace._resolve_workspace_context()
self.assertEqual(ctx.org_id, 'org_b')
self.assertEqual(ctx.org.get('slug'), 'b')
self.assertEqual(ctx.context_source, 'configured_org')
self.assertEqual(ctx.boundary.name, 'workspace')
def test_credential_scoped_org_binds_process_context(self):
self.workspace._load_workspace_credentials = lambda: ('owner', 'secret', 'org_b', None)
self.workspace.load_orgs = lambda: {
'organizations': {
'org_a': {'id': 'org_a', 'slug': 'a', 'name': 'A'},
'org_b': {'id': 'org_b', 'slug': 'b', 'name': 'B'},
}
}
ctx = self.workspace._resolve_workspace_context()
self.assertEqual(ctx.org_id, 'org_b')
self.assertEqual(ctx.org.get('slug'), 'b')
self.assertEqual(ctx.context_source, 'credentials_org')
self.assertEqual(ctx.boundary.identity_model, 'session')
def test_credential_scope_conflict_is_rejected(self):
self.workspace._load_workspace_credentials = lambda: ('owner', 'secret', 'org_a', None)
self.workspace.WORKSPACE_ORG_ID = 'org_b'
with self.assertRaises(RuntimeError):
self.workspace._resolve_workspace_context()
def test_request_override_must_match_bound_org(self):
with self.assertRaises(ValueError):
self.workspace._enforce_request_context(
urlparse('/api/status?org_id=org_other'),
_Headers(),
'org_a',
)
context = self.workspace._enforce_request_context(
urlparse('/api/status?org_id=org_a'),
_Headers({'X-Meridian-Org-Id': 'org_a'}),
'org_a',
)
self.assertEqual(context['requested_org_id'], 'org_a')
self.assertEqual(context['bound_org_id'], 'org_a')
self.assertEqual(context['effective_org_id'], 'org_a')
self.assertEqual(context['route_state'], 'matched')
self.assertEqual(context['route_reason'], 'request_org_matches_bound_institution')
def test_auth_context_reports_credential_binding(self):
self.workspace._load_workspace_credentials = lambda: ('owner', 'secret', 'org_a', None)
auth = self.workspace._resolve_auth_context('org_a')
self.assertEqual(auth['mode'], 'credential_bound')
self.assertEqual(auth['org_id'], 'org_a')
self.assertEqual(auth['actor_id'], 'workspace_user:owner')
def test_routing_planner_classifies_local_remote_and_blocked_orgs(self):
host = self.workspace.load_host_identity('missing')
host.federation_enabled = True
peer_registry = {
'peers': {
'host_beta': self.workspace.FederationPeer(
host_id='host_beta',
label='Beta Host',
trust_state='trusted',
endpoint_url='http://127.0.0.1:19001',
admitted_org_ids=['org_remote'],
),
'host_gamma': self.workspace.FederationPeer(
host_id='host_gamma',
label='Gamma Host',
trust_state='suspended',
endpoint_url='http://127.0.0.1:19002',
admitted_org_ids=['org_suspended'],
),
},
}
snapshot = self.workspace._routing_planner_snapshot(
'org_local',
requested_org_ids=['org_local', 'org_remote', 'org_suspended', 'org_unknown'],
host_identity=host,
admission_registry={'admitted_org_ids': ['org_local']},
peer_registry=peer_registry,
org_registry={'org_local': {}, 'org_remote': {}, 'org_suspended': {}},
)
decisions = {item['requested_org_id']: item for item in snapshot['decisions']}
self.assertEqual(snapshot['summary'], {'total': 4, 'local': 1, 'remote': 1, 'blocked': 2})
self.assertEqual(decisions['org_local']['route_kind'], 'local')
self.assertEqual(decisions['org_local']['route_reason'], 'request_targets_bound_institution')
self.assertEqual(decisions['org_remote']['route_kind'], 'remote')
self.assertEqual(decisions['org_remote']['target_host_id'], 'host_beta')
self.assertEqual(decisions['org_remote']['target_endpoint_url'], 'http://127.0.0.1:19001')
self.assertEqual(decisions['org_suspended']['route_kind'], 'blocked')
self.assertEqual(decisions['org_suspended']['route_reason'], 'matching_peer_not_trusted')
self.assertEqual(decisions['org_unknown']['route_kind'], 'blocked')
self.assertEqual(decisions['org_unknown']['route_reason'], 'unknown_requested_institution')
def test_routing_handoff_preview_bridges_remote_planner_entries(self):
host = self.workspace.load_host_identity('missing')
host.federation_enabled = True
planner = {
'bound_org_id': 'org_local',
'host_id': host.host_id,
'host_federation_enabled': True,
'requested_org_ids': ['org_local', 'org_remote', 'org_blocked'],
'summary': {'total': 3, 'local': 1, 'remote': 1, 'blocked': 1},
'decisions': [
{
'requested_org_id': 'org_local',
'route_state': 'local',
'route_kind': 'local',
'route_reason': 'request_targets_bound_institution',
'target_host_id': '',
'target_endpoint_url': '',
'peer_host_id': '',
'peer_label': '',
'peer_trust_state': '',
},
{
'requested_org_id': 'org_remote',
'route_state': 'remote',
'route_kind': 'remote',
'route_reason': 'trusted_peer_can_serve_requested_institution',
'target_host_id': 'host_beta',
'target_endpoint_url': 'http://127.0.0.1:19001',
'peer_host_id': 'host_beta',
'peer_label': 'Beta Host',
'peer_trust_state': 'trusted',
},
{
'requested_org_id': 'org_blocked',
'route_state': 'blocked',
'route_kind': 'blocked',
'route_reason': 'unknown_requested_institution',
'target_host_id': '',
'target_endpoint_url': '',
'peer_host_id': '',
'peer_label': '',
'peer_trust_state': '',
},
],
}
self.workspace._routing_planner_snapshot = lambda *args, **kwargs: planner
with mock.patch.object(self.workspace, 'upsert_handoff_preview', return_value={'state': 'previewed'}) as upsert:
preview = self.workspace._routing_handoff_preview_snapshot(
'org_local',
host_identity=host,
admission_registry={'admitted_org_ids': ['org_local']},
peer_registry={'peers': {}},
org_registry={'org_local': {}, 'org_remote': {}, 'org_blocked': {}},
)
self.assertEqual(preview['summary'], {'total': 3, 'local': 1, 'remote': 1, 'blocked': 1, 'remote_previewed': 1})
remote = next(item for item in preview['handoff_candidates'] if item['requested_org_id'] == 'org_remote')
self.assertEqual(remote['handoff_state'], 'previewed')
self.assertTrue(remote['dispatch_ready'])
self.assertTrue(remote['handoff_queue_recorded'])
self.assertEqual(remote['handoff_queue_state'], 'previewed')
self.assertFalse(remote['remote_execution_claimed'])
self.assertEqual(remote['dispatch_paths']['send'], '/api/federation/send')
self.assertEqual(remote['draft_execution_request']['target_host_id'], 'host_beta')
self.assertEqual(remote['draft_execution_request']['target_institution_id'], 'org_remote')
self.assertTrue(remote['handoff_id'].startswith('fhdp_'))
blocked = next(item for item in preview['handoff_candidates'] if item['requested_org_id'] == 'org_blocked')
self.assertEqual(blocked['handoff_state'], 'blocked')
self.assertFalse(blocked['dispatch_ready'])
upsert.assert_called_once()
called_org_id, called_preview = upsert.call_args.args
self.assertEqual(called_org_id, 'org_local')
self.assertEqual(called_preview['handoff_id'], remote['handoff_id'])
self.assertEqual(called_preview['requested_org_id'], 'org_remote')
self.assertEqual(called_preview['draft_execution_request']['target_host_id'], 'host_beta')
def test_federation_snapshot_includes_handoff_dispatch_queue(self):
host = self.workspace.load_host_identity('missing')
host.federation_enabled = True
class _AuthorityStub:
def snapshot(self, **kwargs):
return {'host_id': host.host_id, 'peers': [], 'admitted_org_ids': ['org_local']}
with mock.patch.object(self.workspace, '_federation_authority', return_value=_AuthorityStub()), \
mock.patch.object(self.workspace, '_routing_handoff_preview_snapshot', return_value={'summary': {'total': 0}, 'handoff_candidates': []}), \
mock.patch.object(self.workspace, '_handoff_dispatch_queue_snapshot', return_value={'summary': {'total': 1}, 'handoff_dispatch_records': [{'dispatch_id': 'fhdp_demo'}]}), \
mock.patch.object(self.workspace, '_witness_archive_snapshot', return_value={'summary': {'total': 0}, 'witness_observations': []}):
snapshot = self.workspace._federation_snapshot(
'org_local',
host_identity=host,
admission_registry={'admitted_org_ids': ['org_local']},
peer_registry={'peers': {}},
)
self.assertIn('handoff_dispatch_queue', snapshot)
self.assertEqual(snapshot['handoff_dispatch_queue']['summary']['total'], 1)
self.assertEqual(snapshot['handoff_dispatch_queue']['handoff_dispatch_records'][0]['dispatch_id'], 'fhdp_demo')
def test_auth_context_prefers_explicit_user_id(self):
self.workspace._load_workspace_credentials = lambda: ('owner', 'secret', 'org_a', 'user_meridian_owner')
self.workspace.load_orgs = lambda: {
'organizations': {
'org_a': {
'id': 'org_a',
'slug': 'a',
'name': 'A',
'owner_id': 'user_meridian_owner',
'members': [{'user_id': 'user_meridian_owner', 'role': 'owner'}],
},
}
}
auth = self.workspace._resolve_auth_context('org_a')
self.assertEqual(auth['actor_id'], 'user_meridian_owner')
self.assertEqual(auth['actor_source'], 'credentials')
self.assertEqual(auth['role'], 'owner')
def test_auth_context_resolves_owner_alias_role(self):
self.workspace._load_workspace_credentials = lambda: ('owner', 'secret', 'org_a', None)
self.workspace.load_orgs = lambda: {
'organizations': {
'org_a': {
'id': 'org_a',
'slug': 'a',
'name': 'A',
'owner_id': 'user_owner',
'members': [{'user_id': 'user_owner', 'role': 'owner'}],
},
}
}
auth = self.workspace._resolve_auth_context('org_a')
self.assertEqual(auth['user_id'], 'user_owner')
self.assertEqual(auth['role'], 'owner')
self.assertEqual(auth['actor_source'], 'owner_alias')
def test_mutation_authorization_requires_admin_for_kill_switch(self):
auth = {'enabled': True, 'role': 'member'}
with self.assertRaises(PermissionError):
self.workspace._enforce_mutation_authorization(auth, 'org_a', '/api/authority/kill-switch')
def test_mutation_authorization_allows_member_request(self):
auth = {'enabled': True, 'role': 'member'}
required = self.workspace._enforce_mutation_authorization(auth, 'org_a', '/api/authority/request')
self.assertEqual(required, 'member')
def test_permission_snapshot_tracks_allowed_paths(self):
auth = {'enabled': True, 'role': 'admin'}
permissions = self.workspace._permission_snapshot(auth)['mutation_paths']
self.assertTrue(permissions['/api/authority/kill-switch']['allowed'])
self.assertTrue(permissions['/api/institution/charter']['allowed'])
self.assertFalse(permissions['/api/treasury/contribute']['allowed'])
self.assertTrue(permissions['/api/warrants/issue']['allowed'])
self.assertTrue(permissions['/api/warrants/approve']['allowed'])
self.assertTrue(permissions['/api/payouts/propose']['allowed'])
self.assertTrue(permissions['/api/payouts/submit']['allowed'])
self.assertTrue(permissions['/api/payouts/review']['allowed'])
self.assertFalse(permissions['/api/payouts/approve']['allowed'])
self.assertTrue(permissions['/api/treasury/settlement-adapters/preflight']['allowed'])
self.assertTrue(permissions['/api/subscriptions/add']['allowed'])
self.assertTrue(permissions['/api/subscriptions/record-delivery']['allowed'])
self.assertFalse(permissions['/api/accounting/draw']['allowed'])
self.assertEqual(permissions['/api/accounting/draw']['required_role'], 'owner')
self.assertEqual(
permissions['/api/treasury/settlement-adapters/preflight']['required_role'],
'member',
)
self.assertEqual(permissions['/api/payouts/execute']['required_role'], 'owner')
self.assertTrue(permissions['/api/commitments/propose']['allowed'])
self.assertTrue(permissions['/api/commitments/accept']['allowed'])
self.assertTrue(permissions['/api/cases/open']['allowed'])
self.assertTrue(permissions['/api/cases/resolve']['allowed'])
self.assertTrue(permissions['/api/federation/send']['allowed'])
self.assertTrue(permissions['/api/federation/execution-jobs/execute']['allowed'])
self.assertEqual(permissions['/api/federation/execution-jobs/execute']['required_role'], 'admin')
self.assertTrue(permissions['/api/federation/handoff-preview-queue/acknowledge']['allowed'])
self.assertEqual(permissions['/api/federation/handoff-preview-queue/acknowledge']['required_role'], 'admin')
self.assertTrue(permissions['/api/federation/handoff-dispatch-queue/run']['allowed'])
self.assertEqual(permissions['/api/federation/handoff-dispatch-queue/run']['required_role'], 'admin')
self.assertFalse(permissions['/api/federation/peers/refresh']['allowed'])
self.assertEqual(permissions['/api/federation/peers/refresh']['required_role'], 'owner')
def test_payout_snapshot_surfaces_settlement_adapters(self):
self.workspace.payout_proposal_summary = lambda org_id=None: {'total': 0, 'executed': 0}
self.workspace.list_payout_proposals = lambda org_id=None: []
self.workspace.load_payout_proposals = lambda org_id=None: {'state_machine': {'states': []}}
self.workspace.list_settlement_adapters = lambda org_id=None: [
{'adapter_id': 'internal_ledger', 'payout_execution_enabled': True},
{'adapter_id': 'base_usdc_x402', 'payout_execution_enabled': False},
]
self.workspace.settlement_adapter_summary = lambda org_id=None, host_supported_adapters=None: {
'default_payout_adapter': 'internal_ledger',
'host_supported_adapters': list(host_supported_adapters or []),
}
snapshot = self.workspace._payout_snapshot(
'org_a',
host_supported_adapters=['internal_ledger'],
)
self.assertEqual(snapshot['settlement_adapter_summary']['default_payout_adapter'], 'internal_ledger')
self.assertEqual(snapshot['settlement_adapter_summary']['host_supported_adapters'], ['internal_ledger'])
self.assertEqual(len(snapshot['settlement_adapters']), 2)
def test_settlement_adapter_readiness_snapshot_delegates_host_support(self):
self.workspace.settlement_adapter_readiness_snapshot = lambda org_id=None, host_supported_adapters=None: {
'bound_org_id': org_id,
'host_supported_adapters': list(host_supported_adapters or []),
'summary': {'ready': 1},
'ready_adapter_ids': ['internal_ledger'],
'blocked_adapter_ids': ['base_usdc_x402'],
'adapters': [{'adapter_id': 'internal_ledger'}],
}
snapshot = self.workspace._settlement_adapter_readiness_snapshot(
'org_a',
host_supported_adapters=['internal_ledger'],
)
self.assertEqual(snapshot['bound_org_id'], 'org_a')
self.assertEqual(snapshot['host_supported_adapters'], ['internal_ledger'])
self.assertEqual(snapshot['ready_adapter_ids'], ['internal_ledger'])
self.assertEqual(snapshot['blocked_adapter_ids'], ['base_usdc_x402'])
self.assertEqual(snapshot['summary'], {'ready': 1})
def test_api_status_exposes_runtime_core(self):
from runtime_host import default_host_identity
self.workspace._load_workspace_credentials = lambda: ('owner', 'secret', 'org_a', 'user_owner')
self.workspace.load_orgs = lambda: {
'organizations': {
'org_a': {
'id': 'org_a',
'slug': 'a',
'name': 'A',
'owner_id': 'user_owner',
'members': [{'user_id': 'user_owner', 'role': 'owner'}],
'lifecycle_state': 'active',
'policy_defaults': {},
},
'org_b': {
'id': 'org_b',
'slug': 'b',
'name': 'B',
'owner_id': 'user_owner',
'members': [{'user_id': 'user_owner', 'role': 'owner'}],
'lifecycle_state': 'active',
'policy_defaults': {},
},
'org_c': {
'id': 'org_c',
'slug': 'c',
'name': 'C',
'owner_id': 'user_owner',
'members': [{'user_id': 'user_owner', 'role': 'owner'}],
'lifecycle_state': 'active',
'policy_defaults': {},
},
}
}
self.workspace.load_registry = lambda: {
'agents': {
'agent_atlas_a': {
'id': 'agent_atlas_a',
'org_id': 'org_a',
'name': 'Atlas',
'role': 'analyst',
'purpose': 'Research',
'economy_key': 'atlas',
'runtime_binding': {
'runtime_id': 'local_kernel',
'runtime_label': 'Local Kernel Runtime',
},
'rollout_state': 'active',
'reputation_units': 100,
'authority_units': 100,
'last_active_at': '2026-03-21T00:00:00Z',
'risk_state': 'nominal',
'lifecycle_state': 'active',
'budget': {'max_per_run_usd': 1.0},
'scopes': ['execute'],
},
}
}
self.workspace._load_queue = lambda org_id: {
'kill_switch': False,
'pending_approvals': {},
'delegations': {},
}
self.workspace.treasury_snapshot = lambda org_id: {}
self.workspace._load_records = lambda org_id: {'violations': {}, 'appeals': {}}
self.workspace.list_warrants = lambda org_id=None, **_kwargs: [
{
'warrant_id': 'war_demo',
'court_review_state': 'approved',
'execution_state': 'ready',
}
]
self.workspace.list_commitments = lambda org_id=None, **_kwargs: [
{
'commitment_id': 'cmt_demo',
'status': 'accepted',
}
]
self.workspace.commitment_summary = lambda org_id=None: {
'total': 1,
'proposed': 0,
'accepted': 1,
'rejected': 0,
'breached': 0,
'settled': 0,
'delivery_refs_total': 0,
}
self.workspace.list_cases = lambda org_id=None, **_kwargs: [
{
'case_id': 'case_demo',
'status': 'open',
'claim_type': 'breach_of_commitment',
}
]
self.workspace.case_summary = lambda org_id=None: {
'total': 1,
'open': 1,
'stayed': 0,
'resolved': 0,
}
self.workspace.blocking_commitment_ids = lambda org_id=None: ['cmt_demo']
self.workspace.blocked_peer_host_ids = lambda org_id=None: ['host_beta']
self.workspace.get_sprint_lead = lambda org_id: ('', 0)
self.workspace.get_pending_approvals = lambda org_id=None: []
self.workspace.get_restrictions = lambda *args, **kwargs: {}
self.workspace._ci_vertical_status = lambda reg, lead_id, org_id=None: {}
self.workspace.get_agent_remediation = lambda economy_key, reg: None
self.workspace.load_host_identity = lambda *args, **kwargs: default_host_identity(
host_id='host_alpha',
federation_enabled=True,
supported_boundaries=['workspace', 'cli'],
)
self.workspace.load_admission_registry = lambda *args, **kwargs: {
'source': 'file',
'host_id': 'host_alpha',
'institutions': {
'org_a': {'status': 'admitted'},
'org_b': {'status': 'admitted'},
},
'admitted_org_ids': ['org_a', 'org_b'],
}
self.workspace.load_peer_registry = lambda *args, **kwargs: {
'source': 'file',
'host_id': 'host_alpha',
'peers': {
'host_beta': self.workspace.FederationPeer(
host_id='host_beta',
label='Beta Host',
trust_state='trusted',
endpoint_url='http://127.0.0.1:19014',
admitted_org_ids=['org_c'],
),
},
'trusted_peer_ids': ['host_beta'],
}
self.workspace.load_subscriptions = lambda org_id=None: {
'subscribers': {'111': []},
'delivery_log': [],
'_meta': {'storage_model': 'capsule_canonical'},
}
self.workspace.subscription_summary = lambda org_id=None: {'subscriber_count': 1}
self.workspace.active_delivery_targets = lambda org_id=None, external_only=False: ['111']
self.workspace.accounting_snapshot = lambda org_id=None: {
'bound_org_id': org_id,
'summary': {'entry_count': 0},
'mutation_enabled': True,
}
ctx = self.workspace._resolve_workspace_context()
status = self.workspace.api_status(institution_context=ctx)
self.assertEqual(status['runtime_core']['institution_context']['org_id'], 'org_a')
self.assertEqual(status['context']['request_routing']['effective_org_id'], 'org_a')
self.assertEqual(status['context']['request_routing']['route_state'], 'bound')
self.assertTrue(status['runtime_core']['service_registry']['workspace']['supports_institution_routing'])
self.assertTrue(status['runtime_core']['service_registry']['federation_gateway']['supports_institution_routing'])
self.assertTrue(status['runtime_core']['service_registry']['federation_gateway']['requires_warrant'])
self.assertEqual(
status['runtime_core']['service_registry']['federation_gateway']['required_warrant_actions']['execution_request'],
'federated_execution',
)
self.assertEqual(
status['runtime_core']['service_registry']['federation_gateway']['required_warrant_actions']['commitment_proposal'],
'cross_institution_commitment',
)
self.assertEqual(
status['runtime_core']['service_registry']['federation_gateway']['required_warrant_actions']['commitment_acceptance'],
'cross_institution_commitment',
)
self.assertEqual(
status['runtime_core']['service_registry']['federation_gateway']['required_warrant_actions']['commitment_breach_notice'],
'cross_institution_commitment',
)
self.assertNotIn(
'case_notice',
status['runtime_core']['service_registry']['federation_gateway']['required_warrant_actions'],
)
self.assertTrue(status['runtime_core']['admission']['additional_institutions_allowed'])
self.assertEqual(status['runtime_core']['admission']['management_mode'], 'workspace_api_file_backed')
self.assertTrue(status['runtime_core']['admission']['mutation_enabled'])
self.assertEqual(status['runtime_core']['host_identity']['host_id'], 'host_alpha')
self.assertEqual(status['runtime_core']['admission']['admitted_org_ids'], ['org_a', 'org_b'])
self.assertEqual(status['warrants']['total'], 1)
self.assertEqual(status['warrants']['executable'], 1)
self.assertEqual(status['commitments']['total'], 1)
self.assertEqual(status['commitments']['accepted'], 1)
self.assertEqual(status['commitments']['management_mode'], 'workspace_api_file_backed')
self.assertTrue(status['commitments']['mutation_enabled'])
self.assertEqual(status['cases']['total'], 1)
self.assertEqual(status['cases']['open'], 1)
self.assertEqual(status['cases']['management_mode'], 'workspace_api_file_backed')
self.assertEqual(status['cases']['blocking_commitment_ids'], ['cmt_demo'])
self.assertEqual(status['cases']['blocked_peer_host_ids'], ['host_beta'])
self.assertIn('federation', status['runtime_core'])
self.assertTrue(status['runtime_core']['service_registry']['subscriptions']['supports_institution_routing'])
self.assertTrue(status['runtime_core']['service_registry']['accounting']['supports_institution_routing'])
self.assertEqual(status['routing_planner']['summary'], {'total': 3, 'local': 1, 'remote': 1, 'blocked': 1})
decisions = {item['requested_org_id']: item for item in status['routing_planner']['decisions']}
self.assertEqual(decisions['org_a']['route_kind'], 'local')
self.assertEqual(decisions['org_b']['route_kind'], 'blocked')
self.assertEqual(decisions['org_c']['route_kind'], 'remote')
self.assertEqual(decisions['org_c']['target_host_id'], 'host_beta')
self.assertEqual(status['agents'][0]['runtime_binding']['runtime_id'], 'local_kernel')
self.assertEqual(status['agents'][0]['runtime_binding']['bound_org_id'], 'org_a')
self.assertEqual(status['agents'][0]['runtime_binding']['context_source'], 'agent_registry')
self.assertEqual(status['agents'][0]['runtime_binding']['boundary_name'], 'workspace')
self.assertTrue(status['agents'][0]['runtime_binding']['runtime_registered'])
self.assertEqual(status['runtime_core']['runtime_usage']['runtimes']['local_kernel']['bound_agent_count'], 1)
self.assertEqual(
status['runtime_core']['runtime_usage']['runtimes']['local_kernel']['bound_agent_ids'],
['agent_atlas_a'],
)
self.assertEqual(status['runtime_core']['runtime_usage']['unregistered_bindings'], [])
self.assertEqual(status['service_state']['subscriptions']['summary']['subscriber_count'], 1)
self.assertEqual(status['service_state']['accounting']['summary']['entry_count'], 0)
def test_workspace_get_surfaces_subscriptions_and_accounting(self):
class FakeContext:
def __init__(self, org_id='org_a'):
self.org_id = org_id
self.org = {'id': org_id, 'name': 'Org A'}
self.context_source = 'configured_org'
def to_dict(self):
return {
'org_id': self.org_id,
'org_name': self.org.get('name', ''),
'boundary_name': 'workspace',
'identity_model': 'session',
'routing_scope': 'institution_bound',
'host_id': 'host_gamma',
'host_role': 'witness_host',
'admission_id': '',
'federation_mode': 'federated_runtime_core',
'context_source': self.context_source,
'founded_at': '',
}
def to_dict(self):
return {
'org_id': self.org_id,
'org_name': self.org.get('name', ''),
'boundary_name': 'workspace',
'identity_model': 'session',
'routing_scope': 'institution_bound',
'host_id': 'host_gamma',
'host_role': 'witness_host',
'admission_id': '',
'federation_mode': 'federated_runtime_core',
'context_source': self.context_source,
'founded_at': '',
}
def to_dict(self):
return {
'org_id': self.org_id,
'org_name': self.org.get('name', ''),
'boundary_name': 'workspace',
'identity_model': 'session',
'routing_scope': 'institution_bound',
'host_id': 'host_gamma',
'host_role': 'witness_host',
'admission_id': '',
'federation_mode': 'federated_runtime_core',
'context_source': self.context_source,
'founded_at': '',
}
captured = {}
handler = object.__new__(self.workspace.WorkspaceHandler)
handler.path = '/api/subscriptions'
handler.headers = _Headers()
handler._require_auth = lambda _path: True
handler._session_claims_from_request = lambda expected_org_id=None: None
handler._json = lambda data, status=200: captured.update({'status': status, 'data': data})
handler._html = lambda html: captured.update({'status': 200, 'html': html})
with mock.patch.object(self.workspace, '_resolve_workspace_context', return_value=FakeContext()), \
mock.patch.object(self.workspace, '_enforce_request_context', return_value={'mode': 'process_bound'}), \
mock.patch.object(self.workspace, '_resolve_auth_context', return_value={'enabled': True, 'role': 'owner'}), \
mock.patch.object(self.workspace, 'load_registry', return_value={
'agents': {
'agent_atlas_a': {
'id': 'agent_atlas_a',
'org_id': 'org_a',
'name': 'Atlas',
'role': 'analyst',
'purpose': 'Research',
'economy_key': 'atlas',
'runtime_binding': {'runtime_id': 'mcp_generic'},
'rollout_state': 'active',
'reputation_units': 100,
'authority_units': 100,
'last_active_at': '2026-03-21T00:00:00Z',
'risk_state': 'nominal',
'lifecycle_state': 'active',
'budget': {'max_per_run_usd': 1.0},
'scopes': ['execute'],
},
}
}), \
mock.patch.object(self.workspace, 'load_subscriptions', return_value={'subscribers': {'111': []}, 'delivery_log': [], '_meta': {'storage_model': 'capsule_canonical'}}), \
mock.patch.object(self.workspace, 'subscription_summary', return_value={'subscriber_count': 1}), \
mock.patch.object(self.workspace, 'active_delivery_targets', return_value=['111']), \
mock.patch.object(self.workspace, 'accounting_snapshot', return_value={'bound_org_id': 'org_a', 'summary': {'entry_count': 0}}):
handler.do_GET()
self.assertEqual(captured['status'], 200)
self.assertEqual(captured['data']['bound_org_id'], 'org_a')
self.assertEqual(captured['data']['management_mode'], 'institution_owned_service')
self.assertEqual(captured['data']['summary']['subscriber_count'], 1)
self.assertEqual(captured['data']['state']['subscribers'], {'111': []})
handler.path = '/api/subscriptions/delivery-targets'
captured.clear()
handler.do_GET()
self.assertEqual(captured['status'], 200)
self.assertEqual(captured['data']['targets'], ['111'])
self.assertEqual(captured['data']['external_targets'], ['111'])
handler.path = '/api/accounting'
captured.clear()
handler.do_GET()
self.assertEqual(captured['status'], 200)
self.assertEqual(captured['data']['bound_org_id'], 'org_a')
self.assertEqual(captured['data']['summary']['entry_count'], 0)
handler.path = '/api/agents'
captured.clear()
handler.do_GET()
self.assertEqual(captured['status'], 200)
self.assertEqual(captured['data'][0]['runtime_binding']['runtime_id'], 'mcp_generic')
self.assertEqual(captured['data'][0]['runtime_binding']['bound_org_id'], 'org_a')
self.assertTrue(captured['data'][0]['runtime_binding']['runtime_registered'])
handler.path = '/api/runtimes'
captured.clear()
handler.do_GET()
self.assertEqual(captured['status'], 200)
self.assertEqual(captured['data']['runtimes']['mcp_generic']['runtime_usage']['bound_agent_count'], 1)
self.assertEqual(
captured['data']['runtimes']['mcp_generic']['runtime_usage']['bound_agent_ids'],
['agent_atlas_a'],
)
self.assertEqual(captured['data']['runtime_usage']['unregistered_bindings'], [])
def test_payout_execute_dry_run_skips_warrant_finalization(self):
class FakeContext:
def __init__(self):
self.org_id = 'org_a'
self.org = {'id': 'org_a', 'name': 'Org A'}
self.context_source = 'configured_org'
def to_dict(self):
return {
'org_id': self.org_id,
'org_name': self.org.get('name', ''),
'boundary_name': 'workspace',
'identity_model': 'session',
'routing_scope': 'institution_bound',
'host_id': 'host_alpha',
'host_role': 'institution_host',
'admission_id': '',
'federation_mode': 'federated_runtime_core',
'context_source': self.context_source,
'founded_at': '',
}
captured = {}
handler = object.__new__(self.workspace.WorkspaceHandler)
handler.path = '/api/payouts/execute'
handler.headers = _Headers()
handler._require_auth = lambda _path: True
handler._session_claims_from_request = lambda expected_org_id=None: None
handler._read_body = lambda: {
'proposal_id': 'pay_demo',
'warrant_id': 'war_preview_1',
'dry_run': True,
'settlement_adapter': 'internal_ledger',
'tx_hash': '0xpreview',
}
handler._json = lambda data, status=200: captured.update({'status': status, 'data': data})
handler._html = lambda html: captured.update({'status': 200, 'html': html})
preview = {
'dry_run': True,
'status': 'dispute_window',
'proposal_id': 'pay_demo',
'settlement_adapter': 'internal_ledger',
'execution_plan': {
'amount_usd': 2.0,
'recipient_wallet_id': 'wallet_exec',
'tx_ref': 'ptx_preview_demo',
},
}
warrant = {'warrant_id': 'war_preview_1', 'execution_state': 'ready'}
with mock.patch.object(self.workspace, '_resolve_workspace_context', return_value=FakeContext()), mock.patch.object(self.workspace, '_resolve_auth_context', return_value={'enabled': True, 'role': 'owner', 'actor_id': 'user_owner'}), mock.patch.object(self.workspace, '_enforce_mutation_authorization', return_value='owner'), mock.patch.object(self.workspace, '_runtime_host_state', return_value=(self.workspace.load_host_identity('missing'), {'source': 'file', 'institutions': {}, 'admitted_org_ids': []})), mock.patch.object(self.workspace, 'get_payout_proposal', return_value={'proposal_id': 'pay_demo', 'status': 'dispute_window', 'linked_commitment_id': ''}), mock.patch.object(self.workspace, 'validate_warrant_for_execution', return_value=warrant), mock.patch.object(self.workspace, 'execute_payout_proposal', return_value=preview), mock.patch.object(self.workspace, 'mark_warrant_executed', side_effect=self.fail), mock.patch.object(self.workspace, 'payout_proposal_summary', return_value={'total': 1}), mock.patch.object(self.workspace, 'log_event'):
handler.do_POST()
self.assertEqual(captured['status'], 200)
self.assertEqual(captured['data']['message'], 'Payout execution previewed: pay_demo')
self.assertTrue(captured['data']['proposal']['dry_run'])
self.assertEqual(captured['data']['proposal']['execution_plan']['tx_ref'], 'ptx_preview_demo')
self.assertEqual(captured['data']['warrant'], warrant)
self.assertEqual(captured['data']['summary']['total'], 1)
def test_treasury_payout_plan_preview_queue_endpoint_returns_snapshot(self):
class FakeContext:
def __init__(self):
self.org_id = 'org_a'
self.org = {'id': 'org_a', 'name': 'Org A'}
self.context_source = 'configured_org'
def to_dict(self):
return {
'org_id': self.org_id,
'org_name': self.org.get('name', ''),
'boundary_name': 'workspace',
'identity_model': 'session',
'routing_scope': 'institution_bound',
'host_id': 'host_alpha',
'host_role': 'institution_host',
'admission_id': '',
'federation_mode': 'federated_runtime_core',
'context_source': self.context_source,
'founded_at': '',
}
captured = {}
handler = object.__new__(self.workspace.WorkspaceHandler)
handler.path = '/api/treasury/payout-plan-preview-queue'
handler.headers = _Headers()
handler._require_auth = lambda _path: True
handler._session_claims_from_request = lambda expected_org_id=None: None
handler._json = lambda data, status=200: captured.update({'status': status, 'data': data})
handler._html = lambda html: captured.update({'status': 200, 'html': html})
snapshot = {
'summary': {'total': 1, 'execution_ready': 1},
'payout_plan_previews': [
{'preview_id': 'ptx_preview_demo', 'proposal_id': 'pay_demo'}
],
}
with mock.patch.object(self.workspace, '_resolve_workspace_context', return_value=FakeContext()), mock.patch.object(self.workspace, '_resolve_auth_context', return_value={'enabled': True, 'role': 'owner'}), mock.patch.object(self.workspace, '_payout_plan_preview_queue_snapshot', return_value=snapshot):
handler.do_GET()
self.assertEqual(captured['status'], 200)
self.assertEqual(captured['data']['summary']['total'], 1)
self.assertEqual(captured['data']['payout_plan_previews'][0]['preview_id'], 'ptx_preview_demo')
def test_treasury_payout_plan_preview_queue_inspection_endpoint_returns_inspection(self):
class FakeContext:
def __init__(self):
self.org_id = 'org_a'
self.org = {'id': 'org_a', 'name': 'Org A'}
self.context_source = 'configured_org'
def to_dict(self):
return {
'org_id': self.org_id,
'org_name': self.org.get('name', ''),
'boundary_name': 'workspace',
'identity_model': 'session',
'routing_scope': 'institution_bound',
'host_id': 'host_alpha',
'host_role': 'institution_host',
'admission_id': '',
'federation_mode': 'federated_runtime_core',
'context_source': self.context_source,
'founded_at': '',
}
captured = {}
handler = object.__new__(self.workspace.WorkspaceHandler)
handler.path = '/api/treasury/payout-plan-preview-queue/inspect'
handler.headers = _Headers()
handler._require_auth = lambda _path: True
handler._session_claims_from_request = lambda expected_org_id=None: None
handler._json = lambda data, status=200: captured.update({'status': status, 'data': data})
handler._html = lambda html: captured.update({'status': 200, 'html': html})
inspection = {
'summary': {'total': 1, 'execution_ready': 1},
'inspection_summary': {'total': 1, 'ready_for_ack': 1, 'requires_operator_ack': 1},
'payout_plan_previews': [
{
'preview_id': 'ptx_preview_demo',
'proposal_id': 'pay_demo',
'inspection_state': 'ready_for_ack',
}
],
}
with mock.patch.object(self.workspace, '_resolve_workspace_context', return_value=FakeContext()), \
mock.patch.object(self.workspace, '_resolve_auth_context', return_value={'enabled': True, 'role': 'owner'}), \
mock.patch.object(self.workspace, '_payout_plan_preview_queue_inspection', return_value=inspection):
handler.do_GET()
self.assertEqual(captured['status'], 200)
self.assertEqual(captured['data']['inspection_summary']['total'], 1)
self.assertEqual(captured['data']['payout_plan_previews'][0]['inspection_state'], 'ready_for_ack')
def test_treasury_payout_plan_approval_candidate_queue_endpoint_returns_snapshot(self):
class FakeContext:
def __init__(self):
self.org_id = 'org_a'
self.org = {'id': 'org_a', 'name': 'Org A'}
self.context_source = 'configured_org'
def to_dict(self):
return {
'org_id': self.org_id,
'org_name': self.org.get('name', ''),
'boundary_name': 'workspace',
'identity_model': 'session',
'routing_scope': 'institution_bound',
'host_id': 'host_alpha',
'host_role': 'institution_host',
'admission_id': '',
'federation_mode': 'federated_runtime_core',
'context_source': self.context_source,
'founded_at': '',
}
captured = {}
handler = object.__new__(self.workspace.WorkspaceHandler)
handler.path = '/api/treasury/payout-plan-approval-candidate-queue'
handler.headers = _Headers()
handler._require_auth = lambda _path: True
handler._session_claims_from_request = lambda expected_org_id=None: None
handler._json = lambda data, status=200: captured.update({'status': status, 'data': data})
handler._html = lambda html: captured.update({'status': 200, 'html': html})
snapshot = {
'summary': {'total': 1, 'ready_for_approval': 1},
'payout_plan_approval_candidates': [
{'candidate_id': 'ptx_preview_demo', 'source_preview_id': 'ptx_preview_demo'}
],
}
with mock.patch.object(self.workspace, '_resolve_workspace_context', return_value=FakeContext()), \
mock.patch.object(self.workspace, '_resolve_auth_context', return_value={'enabled': True, 'role': 'owner'}), \
mock.patch.object(self.workspace, 'payout_plan_approval_candidate_queue_snapshot', return_value=snapshot):
handler.do_GET()
self.assertEqual(captured['status'], 200)
self.assertEqual(captured['data']['summary']['total'], 1)
self.assertEqual(captured['data']['payout_plan_approval_candidates'][0]['candidate_id'], 'ptx_preview_demo')
def test_treasury_payout_execution_queue_endpoint_returns_snapshot(self):
class FakeContext:
def __init__(self):
self.org_id = 'org_a'
self.org = {'id': 'org_a', 'name': 'Org A'}
self.context_source = 'configured_org'