@@ -404,3 +404,132 @@ fn call_size_limit() {
404
404
If the limit is too strong, maybe consider increasing the limit" ,
405
405
) ;
406
406
}
407
+
408
+ #[ test]
409
+ fn authorize_call_works ( ) {
410
+ ExtBuilder :: default ( ) . build ( ) . execute_with ( || {
411
+ run_to_block ( 1 ) ;
412
+ let ensure_root_call = Call :: System ( frame_system:: Call :: fill_block ( Perbill :: one ( ) ) ) ;
413
+ let call = Call :: Authority ( authority:: Call :: dispatch_as (
414
+ MockAsOriginId :: Root ,
415
+ Box :: new ( ensure_root_call) ,
416
+ ) ) ;
417
+ let hash = <Runtime as frame_system:: Config >:: Hashing :: hash_of ( & call) ;
418
+
419
+ // works without account
420
+ assert_ok ! ( Authority :: authorize_call( Origin :: root( ) , Box :: new( call. clone( ) ) , None ) ) ;
421
+ assert_eq ! ( Authority :: saved_calls( & hash) , Some ( ( call. clone( ) , None ) ) ) ;
422
+ System :: assert_last_event ( mock:: Event :: Authority ( Event :: AuthorizedCall ( hash, None ) ) ) ;
423
+
424
+ // works with account
425
+ assert_ok ! ( Authority :: authorize_call(
426
+ Origin :: root( ) ,
427
+ Box :: new( call. clone( ) ) ,
428
+ Some ( 1 )
429
+ ) ) ;
430
+ assert_eq ! ( Authority :: saved_calls( & hash) , Some ( ( call. clone( ) , Some ( 1 ) ) ) ) ;
431
+ System :: assert_last_event ( mock:: Event :: Authority ( Event :: AuthorizedCall ( hash, Some ( 1 ) ) ) ) ;
432
+ } ) ;
433
+ }
434
+
435
+ #[ test]
436
+ fn trigger_call_works ( ) {
437
+ ExtBuilder :: default ( ) . build ( ) . execute_with ( || {
438
+ run_to_block ( 1 ) ;
439
+ let ensure_root_call = Call :: System ( frame_system:: Call :: fill_block ( Perbill :: one ( ) ) ) ;
440
+ let call = Call :: Authority ( authority:: Call :: dispatch_as (
441
+ MockAsOriginId :: Root ,
442
+ Box :: new ( ensure_root_call) ,
443
+ ) ) ;
444
+ let hash = <Runtime as frame_system:: Config >:: Hashing :: hash_of ( & call) ;
445
+
446
+ let call_weight_bound = call. get_dispatch_info ( ) . weight ;
447
+
448
+ // call not authorized yet
449
+ assert_noop ! (
450
+ Authority :: trigger_call( Origin :: signed( 1 ) , hash, call_weight_bound) ,
451
+ Error :: <Runtime >:: CallNotAuthorized
452
+ ) ;
453
+
454
+ assert_ok ! ( Authority :: authorize_call( Origin :: root( ) , Box :: new( call. clone( ) ) , None ) ) ;
455
+
456
+ // wrong call weight bound
457
+ assert_noop ! (
458
+ Authority :: trigger_call( Origin :: signed( 1 ) , hash, call_weight_bound - 1 ) ,
459
+ Error :: <Runtime >:: WrongCallWeightBound
460
+ ) ;
461
+
462
+ // works without caller
463
+ assert_ok ! ( Authority :: trigger_call( Origin :: signed( 1 ) , hash, call_weight_bound) ) ;
464
+ assert_eq ! ( Authority :: saved_calls( & hash) , None ) ;
465
+ System :: assert_has_event ( mock:: Event :: Authority ( Event :: TriggeredCallBy ( hash, 1 ) ) ) ;
466
+ System :: assert_last_event ( mock:: Event :: Authority ( Event :: Dispatched ( Ok ( ( ) ) ) ) ) ;
467
+
468
+ // works with caller 1
469
+ assert_ok ! ( Authority :: authorize_call(
470
+ Origin :: root( ) ,
471
+ Box :: new( call. clone( ) ) ,
472
+ Some ( 1 )
473
+ ) ) ;
474
+ // caller 2 is not permitted to trigger the call
475
+ assert_noop ! (
476
+ Authority :: trigger_call( Origin :: signed( 2 ) , hash, call_weight_bound) ,
477
+ Error :: <Runtime >:: TriggerCallNotPermitted
478
+ ) ;
479
+ assert_eq ! ( Authority :: saved_calls( & hash) , Some ( ( call. clone( ) , Some ( 1 ) ) ) ) ;
480
+
481
+ // caller 1 triggering the call
482
+ assert_ok ! ( Authority :: trigger_call( Origin :: signed( 1 ) , hash, call_weight_bound) ) ;
483
+ assert_eq ! ( Authority :: saved_calls( & hash) , None ) ;
484
+ System :: assert_has_event ( mock:: Event :: Authority ( Event :: TriggeredCallBy ( hash, 1 ) ) ) ;
485
+ System :: assert_last_event ( mock:: Event :: Authority ( Event :: Dispatched ( Ok ( ( ) ) ) ) ) ;
486
+ } ) ;
487
+ }
488
+
489
+ #[ test]
490
+ fn remove_authorized_call_works ( ) {
491
+ ExtBuilder :: default ( ) . build ( ) . execute_with ( || {
492
+ run_to_block ( 1 ) ;
493
+ let ensure_root_call = Call :: System ( frame_system:: Call :: fill_block ( Perbill :: one ( ) ) ) ;
494
+ let call = Call :: Authority ( authority:: Call :: dispatch_as (
495
+ MockAsOriginId :: Root ,
496
+ Box :: new ( ensure_root_call) ,
497
+ ) ) ;
498
+ let hash = <Runtime as frame_system:: Config >:: Hashing :: hash_of ( & call) ;
499
+
500
+ assert_noop ! (
501
+ Authority :: remove_authorized_call( Origin :: root( ) , hash) ,
502
+ Error :: <Runtime >:: CallNotAuthorized
503
+ ) ;
504
+
505
+ assert_ok ! ( Authority :: authorize_call( Origin :: root( ) , Box :: new( call. clone( ) ) , None ) ) ;
506
+ assert_noop ! (
507
+ Authority :: remove_authorized_call( Origin :: signed( 1 ) , hash) ,
508
+ Error :: <Runtime >:: CallNotAuthorized
509
+ ) ;
510
+ assert_eq ! ( Authority :: saved_calls( & hash) , Some ( ( call. clone( ) , None ) ) ) ;
511
+ assert_ok ! ( Authority :: remove_authorized_call( Origin :: root( ) , hash) ) ;
512
+ assert_eq ! ( Authority :: saved_calls( & hash) , None ) ;
513
+
514
+ assert_ok ! ( Authority :: authorize_call(
515
+ Origin :: root( ) ,
516
+ Box :: new( call. clone( ) ) ,
517
+ Some ( 1 )
518
+ ) ) ;
519
+ assert_ok ! ( Authority :: remove_authorized_call( Origin :: root( ) , hash) ) ;
520
+ assert_eq ! ( Authority :: saved_calls( & hash) , None ) ;
521
+
522
+ assert_ok ! ( Authority :: authorize_call(
523
+ Origin :: root( ) ,
524
+ Box :: new( call. clone( ) ) ,
525
+ Some ( 1 )
526
+ ) ) ;
527
+ assert_noop ! (
528
+ Authority :: remove_authorized_call( Origin :: signed( 2 ) , hash) ,
529
+ Error :: <Runtime >:: CallNotAuthorized
530
+ ) ;
531
+ assert_eq ! ( Authority :: saved_calls( & hash) , Some ( ( call. clone( ) , Some ( 1 ) ) ) ) ;
532
+ assert_ok ! ( Authority :: remove_authorized_call( Origin :: signed( 1 ) , hash) ) ;
533
+ assert_eq ! ( Authority :: saved_calls( & hash) , None ) ;
534
+ } ) ;
535
+ }
0 commit comments