-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathaccess_controllable.rs
1603 lines (1387 loc) · 47.7 KB
/
access_controllable.rs
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
// Using `pub` to avoid invalid `dead_code` warnings, see
// https://users.rust-lang.org/t/invalid-dead-code-warning-for-submodule-in-integration-test/80259
pub mod common;
use common::access_controllable_contract::AccessControllableContract;
use common::utils::{
as_sdk_account_id, assert_insufficient_acl_permissions, assert_private_method_failure,
assert_success_with,
};
use near_plugins::access_controllable::{PermissionedAccounts, PermissionedAccountsPerRole};
use near_sdk::serde_json::{self, json};
use near_workspaces::network::Sandbox;
use near_workspaces::result::ExecutionFinalResult;
use near_workspaces::{Account, AccountId, Contract, Worker};
use std::collections::{HashMap, HashSet};
use std::convert::TryFrom;
use std::path::Path;
const PROJECT_PATH: &str = "./tests/contracts/access_controllable";
/// All roles which are defined in the contract in [`PROJECT_PATH`].
const ALL_ROLES: [&str; 3] = ["ByMax2Increaser", "ByMax3Increaser", "Resetter"];
/// Bundles resources required in tests.
struct Setup {
/// The worker interacting with the current sandbox.
worker: Worker<Sandbox>,
/// Deployed instance of the contract in [`PROJECT_PATH`].
contract: AccessControllableContract,
/// A newly created account (which differs from the contract).
account: Account,
}
impl Setup {
fn contract_account(&self) -> &Account {
self.contract.contract().as_account()
}
/// Deploys the contract and calls the initialization method without passing any accounts to be
/// added as admin or grantees.
async fn new() -> anyhow::Result<Self> {
Self::new_with_admins_and_grantees(Default::default(), Default::default()).await
}
/// Deploys the contract with a specific wasm binary.
async fn new_with_wasm(wasm: Vec<u8>) -> anyhow::Result<Self> {
Self::deploy_contract(
wasm,
json!({
"admins": HashMap::<String, AccountId>::new(),
"grantees": HashMap::<String, AccountId>::new()
}),
)
.await
}
/// Deploys the contract and passes `admins` and `grantees` to the initialization method. Note
/// that accounts corresponding to the ids in `admins` and `grantees` are _not_ created.
async fn new_with_admins_and_grantees(
admins: HashMap<String, AccountId>,
grantees: HashMap<String, AccountId>,
) -> anyhow::Result<Self> {
let wasm =
common::repo::compile_project(Path::new(PROJECT_PATH), "access_controllable").await?;
Self::deploy_contract(wasm, json!({ "admins": admins, "grantees": grantees })).await
}
async fn deploy_contract(wasm: Vec<u8>, args: serde_json::Value) -> anyhow::Result<Self> {
let worker = near_workspaces::sandbox().await?;
let contract = AccessControllableContract::new(worker.dev_deploy(&wasm).await?);
let account = worker.dev_create_account().await?;
contract
.contract()
.call("new")
.args_json(args)
.max_gas()
.transact()
.await?
.into_result()?;
Ok(Self {
worker,
contract,
account,
})
}
/// Returns a new account that is super-admin.
async fn new_super_admin_account(&self) -> anyhow::Result<Account> {
let account = self.worker.dev_create_account().await?;
self.contract
.acl_add_super_admin_unchecked(self.contract_account(), account.id())
.await?
.into_result()?;
Ok(account)
}
/// Returns a new account that is admin for `roles`.
async fn new_account_as_admin(&self, roles: &[&str]) -> anyhow::Result<Account> {
let account = self.worker.dev_create_account().await?;
for &role in roles {
self.contract
.acl_add_admin_unchecked(self.contract_account(), role, account.id())
.await?
.into_result()?;
}
Ok(account)
}
async fn new_account_with_roles(&self, roles: &[&str]) -> anyhow::Result<Account> {
let account = self.worker.dev_create_account().await?;
for &role in roles {
self.contract
.acl_grant_role_unchecked(self.contract_account(), role, account.id())
.await?
.into_result()?;
}
Ok(account)
}
}
async fn call_increase_2(
contract: &Contract,
caller: &Account,
) -> near_workspaces::Result<ExecutionFinalResult> {
caller
.call(contract.id(), "increase_2")
.args_json(())
.max_gas()
.transact()
.await
}
/// Returns new `PermissionedAccounts` for [`ALL_ROLES`].
fn new_permissioned_accounts() -> PermissionedAccounts {
let mut permissioned_accounts = PermissionedAccounts {
super_admins: vec![],
roles: HashMap::new(),
};
for role in ALL_ROLES {
permissioned_accounts.roles.insert(
role.to_string(),
PermissionedAccountsPerRole {
admins: vec![],
grantees: vec![],
},
);
}
permissioned_accounts
}
/// Asserts both `PermissionedAcccounts` contain the same accounts with the same permissions,
/// disregarding order.
///
/// Expects both `a` and `b` to contain every role in [`ALL_ROLES`].
///
/// This function is available only in tests and used for small numbers of accounts, so simplicity
/// is favored over efficiency.
fn assert_permissioned_account_equivalence(a: &PermissionedAccounts, b: &PermissionedAccounts) {
// Verify super admins.
assert_account_ids_equivalence(
a.super_admins.as_slice(),
b.super_admins.as_slice(),
"super_admins",
);
// Verify admins and grantees per role.
assert_eq!(a.roles.len(), b.roles.len(), "Unequal number of roles");
assert_eq!(a.roles.len(), ALL_ROLES.len(), "More roles than expected");
for role in ALL_ROLES {
let per_role_a = a
.roles
.get(role)
.unwrap_or_else(|| panic!("PermissionedAccounts a misses role {}", role));
let per_role_b = b
.roles
.get(role)
.unwrap_or_else(|| panic!("PermissionedAccounts b misses role {}", role));
assert_account_ids_equivalence(
&per_role_a.admins,
&per_role_b.admins,
format!("admins of role {}", role).as_str(),
);
assert_account_ids_equivalence(
&per_role_a.grantees,
&per_role_b.grantees,
format!("grantees of role {}", role).as_str(),
);
}
}
/// Asserts `a` and `b` contain the same `AccountId`s, disregarding order. Parameter `specifier` is
/// passed to the panic message in case of a mismatch.
///
/// This function is available only in tests and used for small numbers of accounts, so simplicity
/// is favored over efficiency.
fn assert_account_ids_equivalence(
a: &[near_sdk::AccountId],
b: &[near_sdk::AccountId],
specifier: &str,
) {
let set_a: HashSet<_> = a.iter().cloned().collect();
let set_b: HashSet<_> = b.iter().cloned().collect();
assert_eq!(set_a, set_b, "Unequal sets of AccountIds for {}", specifier,);
}
/// Smoke test of contract setup and basic functionality.
#[tokio::test]
async fn test_increase_and_get_counter() -> anyhow::Result<()> {
let Setup {
contract, account, ..
} = Setup::new().await?;
let contract = contract.contract();
account
.call(contract.id(), "increase")
.max_gas()
.transact()
.await?
.into_result()?;
let res: u64 = account
.call(contract.id(), "get_counter")
.view()
.await?
.json()?;
assert_eq!(res, 1);
Ok(())
}
#[tokio::test]
async fn test_acl_initialization_in_constructor() -> anyhow::Result<()> {
let admin_id: AccountId = "admin.acl_test.near".parse().unwrap();
let grantee_id: AccountId = "grantee.acl_test.near".parse().unwrap();
let setup = Setup::new_with_admins_and_grantees(
HashMap::from([("ByMax2Increaser".to_string(), admin_id.clone())]),
HashMap::from([("Resetter".to_string(), grantee_id.clone())]),
)
.await?;
setup
.contract
.assert_acl_is_admin(true, "ByMax2Increaser", &admin_id)
.await;
setup
.contract
.assert_acl_has_role(true, "Resetter", &grantee_id)
.await;
Ok(())
}
#[tokio::test]
async fn test_acl_role_variants() -> anyhow::Result<()> {
let setup = Setup::new().await?;
let variants = setup.contract.acl_role_variants(&setup.account).await?;
assert_eq!(variants, ALL_ROLES);
Ok(())
}
#[tokio::test]
async fn test_acl_is_super_admin() -> anyhow::Result<()> {
let Setup {
contract, account, ..
} = Setup::new().await?;
let is_super_admin = contract.acl_is_super_admin(&account, account.id()).await?;
assert!(!is_super_admin);
contract
.acl_add_super_admin_unchecked(contract.contract().as_account(), account.id())
.await?
.into_result()?;
let is_super_admin = contract.acl_is_super_admin(&account, account.id()).await?;
assert!(is_super_admin);
Ok(())
}
#[tokio::test]
async fn test_acl_init_super_admin() -> anyhow::Result<()> {
let Setup {
worker,
contract,
account,
..
} = Setup::new().await?;
let contract_account = contract.contract().as_account();
// Calling `acl_init_super_admin` after initialization adds super-admin.
contract
.assert_acl_is_super_admin(false, contract_account, account.id())
.await;
let res = contract
.acl_init_super_admin(contract_account, account.id())
.await?;
assert_success_with(res, true);
contract
.assert_acl_is_super_admin(true, contract_account, account.id())
.await;
// Once there's a super-admin, `acl_init_super_admin` returns `false`.
let res = contract
.acl_init_super_admin(contract_account, account.id())
.await?;
assert_success_with(res, false);
let other_account = worker.dev_create_account().await?;
let res = contract
.acl_init_super_admin(contract_account, other_account.id())
.await?;
assert_success_with(res, false);
contract
.assert_acl_is_super_admin(false, contract_account, other_account.id())
.await;
// When all super-admins have been removed, it succeeds again.
let res = contract
.acl_revoke_super_admin_unchecked(contract_account, account.id())
.await?;
assert_success_with(res, true);
let res = contract
.acl_init_super_admin(contract_account, other_account.id())
.await?;
assert_success_with(res, true);
contract
.assert_acl_is_super_admin(true, contract_account, other_account.id())
.await;
Ok(())
}
#[tokio::test]
async fn test_acl_add_super_admin() -> anyhow::Result<()> {
let setup = Setup::new().await?;
let to_be_super_admin = setup.worker.dev_create_account().await?;
// Create accounts that add a super-admin.
let caller_unauth = setup.worker.dev_create_account().await?;
let caller_auth = setup.new_super_admin_account().await?;
// Adding is a no-op if the caller is not a super-admin.
let res = setup
.contract
.acl_add_super_admin(&caller_unauth, to_be_super_admin.id())
.await?;
assert_eq!(res, None);
setup
.contract
.assert_acl_is_super_admin(false, setup.contract_account(), to_be_super_admin.id())
.await;
// Adding succeeds if the caller is a super-admin.
let res = setup
.contract
.acl_add_super_admin(&caller_auth, to_be_super_admin.id())
.await?;
assert_eq!(res, Some(true));
setup
.contract
.assert_acl_is_super_admin(true, setup.contract_account(), to_be_super_admin.id())
.await;
// Adding an account which is already super-admin returns `Some(false)`.
let res = setup
.contract
.acl_add_super_admin(&caller_auth, to_be_super_admin.id())
.await?;
assert_eq!(res, Some(false));
Ok(())
}
#[tokio::test]
async fn test_acl_add_super_admin_unchecked() -> anyhow::Result<()> {
let Setup {
contract, account, ..
} = Setup::new().await?;
let contract_account = contract.contract().as_account();
contract
.assert_acl_is_super_admin(false, contract_account, account.id())
.await;
let res = contract
.acl_add_super_admin_unchecked(contract_account, account.id())
.await?;
assert_success_with(res, true);
contract
.assert_acl_is_super_admin(true, contract_account, account.id())
.await;
// Adding as super-admin again behaves as expected.
let res = contract
.acl_add_super_admin_unchecked(contract_account, account.id())
.await?;
assert_success_with(res, false);
Ok(())
}
#[tokio::test]
async fn test_acl_revoke_super_admin() -> anyhow::Result<()> {
let setup = Setup::new().await?;
let super_admin = setup.new_super_admin_account().await?;
setup
.contract
.assert_acl_is_super_admin(true, setup.contract_account(), super_admin.id())
.await;
// Create revoker accounts.
let revoker_unauth = setup.worker.dev_create_account().await?;
let revoker_auth = setup.new_super_admin_account().await?;
// Revoke is a no-op if revoker is not a super-admin.
let res = setup
.contract
.acl_revoke_super_admin(&revoker_unauth, super_admin.id())
.await?;
assert_eq!(res, None);
setup
.contract
.assert_acl_is_super_admin(true, setup.contract_account(), super_admin.id())
.await;
// Revoke succeeds if the revoker is a super-admin.
let res = setup
.contract
.acl_revoke_super_admin(&revoker_auth, super_admin.id())
.await?;
assert_eq!(res, Some(true));
setup
.contract
.assert_acl_is_super_admin(false, setup.contract_account(), super_admin.id())
.await;
// Revoking from an account which isn't super-admin returns `Some(false)`.
let account = setup.worker.dev_create_account().await?;
let res = setup
.contract
.acl_revoke_super_admin(&revoker_auth, account.id())
.await?;
assert_eq!(res, Some(false));
Ok(())
}
#[tokio::test]
async fn test_acl_transfer_super_admin() -> anyhow::Result<()> {
let setup = Setup::new().await?;
let super_admin = setup.new_super_admin_account().await?;
let new_super_admin = setup.worker.dev_create_account().await?;
setup
.contract
.assert_acl_is_super_admin(true, setup.contract_account(), super_admin.id())
.await;
// Create caller account.
let caller_unauth = setup.worker.dev_create_account().await?;
// Transfer is a no-op if caller is not a super-admin.
let res = setup
.contract
.acl_transfer_super_admin(&caller_unauth, super_admin.id())
.await?;
assert_eq!(res, None);
setup
.contract
.assert_acl_is_super_admin(true, setup.contract_account(), super_admin.id())
.await;
setup
.contract
.assert_acl_is_super_admin(false, setup.contract_account(), new_super_admin.id())
.await;
// Transfer succeeds if the caller is a super-admin.
let res = setup
.contract
.acl_transfer_super_admin(&super_admin, new_super_admin.id())
.await?;
assert_eq!(res, Some(true));
setup
.contract
.assert_acl_is_super_admin(false, setup.contract_account(), super_admin.id())
.await;
setup
.contract
.assert_acl_is_super_admin(true, setup.contract_account(), new_super_admin.id())
.await;
// Transfer to an account that is already super-admin returns `Some(false)`.
let admin = setup.new_super_admin_account().await?;
let res = setup
.contract
.acl_transfer_super_admin(&new_super_admin, admin.id())
.await?;
assert_eq!(res, Some(false));
Ok(())
}
#[tokio::test]
async fn test_acl_revoke_super_admin_unchecked() -> anyhow::Result<()> {
let setup = Setup::new().await?;
let account = setup.new_super_admin_account().await?;
setup
.contract
.assert_acl_is_super_admin(true, setup.contract_account(), account.id())
.await;
// Revoke an existing super-admin permission.
let res = setup
.contract
.acl_revoke_super_admin_unchecked(setup.contract_account(), account.id())
.await?;
assert_success_with(res, true);
setup
.contract
.assert_acl_is_super_admin(false, setup.contract_account(), account.id())
.await;
// Revoke from an account which is not super-admin.
let res = setup
.contract
.acl_revoke_super_admin_unchecked(setup.contract_account(), account.id())
.await?;
assert_success_with(res, false);
Ok(())
}
/// Verify that a super-admin is admin for every role.
#[tokio::test]
async fn test_super_admin_is_any_admin() -> anyhow::Result<()> {
let setup = Setup::new().await?;
let super_admin = setup.new_super_admin_account().await?;
for role in ALL_ROLES {
setup
.contract
.assert_acl_is_admin(true, role, super_admin.id())
.await;
}
Ok(())
}
/// Verify that a super-admin may add admins for every role.
#[tokio::test]
async fn test_super_admin_may_add_any_admin() -> anyhow::Result<()> {
let setup = Setup::new().await?;
let super_admin = setup.new_super_admin_account().await?;
for role in ALL_ROLES {
let account = setup.worker.dev_create_account().await?;
setup
.contract
.assert_acl_is_admin(false, role, account.id())
.await;
let res = setup
.contract
.acl_add_admin(&super_admin, role, account.id())
.await?;
assert_eq!(res, Some(true));
setup
.contract
.assert_acl_is_admin(true, role, account.id())
.await;
}
Ok(())
}
/// Verify that a super-admin may revoke admins for every role.
#[tokio::test]
async fn test_super_admin_may_revoke_any_admin() -> anyhow::Result<()> {
let setup = Setup::new().await?;
let super_admin = setup.new_super_admin_account().await?;
for role in ALL_ROLES {
let admin = setup.new_account_as_admin(&[role]).await?;
setup
.contract
.assert_acl_is_admin(true, role, admin.id())
.await;
let res = setup
.contract
.acl_revoke_admin(&super_admin, role, admin.id())
.await?;
assert_eq!(res, Some(true));
setup
.contract
.assert_acl_is_admin(false, role, admin.id())
.await;
}
Ok(())
}
/// Verify that a super-admin may grant every role.
#[tokio::test]
async fn test_super_admin_may_grant_any_role() -> anyhow::Result<()> {
let setup = Setup::new().await?;
let super_admin = setup.new_super_admin_account().await?;
for role in ALL_ROLES {
let account = setup.worker.dev_create_account().await?;
setup
.contract
.assert_acl_has_role(false, role, account.id())
.await;
let res = setup
.contract
.acl_grant_role(&super_admin, role, account.id())
.await?;
assert_eq!(res, Some(true));
setup
.contract
.assert_acl_has_role(true, role, account.id())
.await;
}
Ok(())
}
/// Verify that a super-admin may revoke every role.
#[tokio::test]
async fn test_super_admin_may_revoke_any_role() -> anyhow::Result<()> {
let setup = Setup::new().await?;
let super_admin = setup.new_super_admin_account().await?;
for role in ALL_ROLES {
let grantee = setup.new_account_with_roles(&[role]).await?;
setup
.contract
.assert_acl_has_role(true, role, grantee.id())
.await;
let res = setup
.contract
.acl_revoke_role(&super_admin, role, grantee.id())
.await?;
assert_eq!(res, Some(true));
setup
.contract
.assert_acl_has_role(false, role, grantee.id())
.await;
}
Ok(())
}
#[tokio::test]
async fn test_acl_is_admin() -> anyhow::Result<()> {
let Setup {
contract, account, ..
} = Setup::new().await?;
let contract_account = contract.contract().as_account();
let role = "ByMax2Increaser";
let is_admin = contract.acl_is_admin(&account, role, account.id()).await?;
assert!(!is_admin);
contract
.acl_add_admin_unchecked(contract_account, role, account.id())
.await?
.into_result()?;
let is_admin = contract.acl_is_admin(&account, role, account.id()).await?;
assert!(is_admin);
Ok(())
}
#[tokio::test]
async fn test_acl_add_admin() -> anyhow::Result<()> {
let Setup {
worker,
contract,
account,
..
} = Setup::new().await?;
let contract_account = contract.contract().as_account();
let role = "ByMax2Increaser";
let acc_adding_admin = account;
let acc_to_be_admin = worker.dev_create_account().await?;
contract
.assert_acl_is_admin(false, role, acc_to_be_admin.id())
.await;
// An account which isn't admin can't add admins.
let added = contract
.acl_add_admin(&acc_adding_admin, role, acc_to_be_admin.id())
.await?;
assert_eq!(added, None);
// Admin can add others as admin.
contract
.acl_add_admin_unchecked(contract_account, role, acc_adding_admin.id())
.await?
.into_result()?;
let added = contract
.acl_add_admin(&acc_adding_admin, role, acc_to_be_admin.id())
.await?;
assert_eq!(added, Some(true));
contract
.assert_acl_is_admin(true, role, acc_to_be_admin.id())
.await;
// Adding an account that is already admin.
let added = contract
.acl_add_admin(&acc_adding_admin, role, acc_to_be_admin.id())
.await?;
assert_eq!(added, Some(false));
Ok(())
}
#[tokio::test]
async fn test_acl_add_admin_unchecked() -> anyhow::Result<()> {
let Setup {
contract, account, ..
} = Setup::new().await?;
let contract_account = contract.contract().as_account();
let role = "ByMax2Increaser";
contract
.assert_acl_is_admin(false, role, account.id())
.await;
let res = contract
.acl_add_admin_unchecked(contract_account, role, account.id())
.await?;
assert_success_with(res, true);
contract.assert_acl_is_admin(true, role, account.id()).await;
// Adding as admin again behaves as expected.
let res = contract
.acl_add_admin_unchecked(contract_account, role, account.id())
.await?;
assert_success_with(res, false);
Ok(())
}
#[tokio::test]
async fn test_acl_revoke_admin() -> anyhow::Result<()> {
let setup = Setup::new().await?;
let role = "ByMax3Increaser";
let admin = setup.new_account_as_admin(&[role]).await?;
setup
.contract
.assert_acl_is_admin(true, role, admin.id())
.await;
// Revoke is a no-op if revoker is not an admin for the role.
let revoker = setup.new_account_as_admin(&[]).await?;
let res = setup
.contract
.acl_revoke_admin(&revoker, role, admin.id())
.await?;
assert_eq!(res, None);
let revoker = setup.new_account_as_admin(&["ByMax2Increaser"]).await?;
let res = setup
.contract
.acl_revoke_admin(&revoker, role, admin.id())
.await?;
assert_eq!(res, None);
setup
.contract
.assert_acl_is_admin(true, role, admin.id())
.await;
// Revoke succeeds if the revoker is an admin for the role.
let revoker = setup.new_account_as_admin(&[role]).await?;
let res = setup
.contract
.acl_revoke_admin(&revoker, role, admin.id())
.await?;
assert_eq!(res, Some(true));
setup
.contract
.assert_acl_is_admin(false, role, admin.id())
.await;
// Revoking a role for which the account isn't admin returns `Some(false)`.
let revoker = setup.new_account_as_admin(&[role]).await?;
let account = setup.worker.dev_create_account().await?;
let res = setup
.contract
.acl_revoke_admin(&revoker, role, account.id())
.await?;
assert_eq!(res, Some(false));
Ok(())
}
#[tokio::test]
async fn test_acl_renounce_admin() -> anyhow::Result<()> {
let setup = Setup::new().await?;
let role = "Resetter";
// An account which is isn't admin calls `acl_renounce_admin`.
let res = setup
.contract
.acl_renounce_admin(&setup.account, role)
.await?;
assert!(!res);
// An admin calls `acl_renounce_admin`.
let admin = setup.new_account_as_admin(&[role]).await?;
setup
.contract
.assert_acl_is_admin(true, role, admin.id())
.await;
let res = setup.contract.acl_renounce_admin(&admin, role).await?;
assert!(res);
setup
.contract
.assert_acl_is_admin(false, role, admin.id())
.await;
Ok(())
}
#[tokio::test]
async fn test_acl_revoke_admin_unchecked() -> anyhow::Result<()> {
let setup = Setup::new().await?;
let account = setup
.new_account_as_admin(&["ByMax2Increaser", "Resetter"])
.await?;
setup
.contract
.assert_acl_is_admin(true, "ByMax2Increaser", account.id())
.await;
setup
.contract
.assert_acl_is_admin(true, "Resetter", account.id())
.await;
// Revoke admin permissions for one of the roles.
let res = setup
.contract
.acl_revoke_admin_unchecked(setup.contract_account(), "ByMax2Increaser", account.id())
.await?;
assert_success_with(res, true);
setup
.contract
.assert_acl_is_admin(false, "ByMax2Increaser", account.id())
.await;
setup
.contract
.assert_acl_is_admin(true, "Resetter", account.id())
.await;
// Revoke admin permissions for the other role too.
let res = setup
.contract
.acl_revoke_admin_unchecked(setup.contract_account(), "Resetter", account.id())
.await?;
assert_success_with(res, true);
setup
.contract
.assert_acl_is_admin(false, "ByMax2Increaser", account.id())
.await;
setup
.contract
.assert_acl_is_admin(false, "Resetter", account.id())
.await;
// Revoking behaves as expected if the permission is not present.
let res = setup
.contract
.acl_revoke_admin_unchecked(setup.contract_account(), "Resetter", account.id())
.await?;
assert_success_with(res, false);
Ok(())
}
#[tokio::test]
async fn test_acl_has_role() -> anyhow::Result<()> {
let Setup {
contract, account, ..
} = Setup::new().await?;
let contract_account = contract.contract().as_account();
let role = "ByMax2Increaser";
let has_role = contract.acl_has_role(&account, role, account.id()).await?;
assert!(!has_role);
contract
.acl_grant_role_unchecked(contract_account, role, account.id())
.await?
.into_result()?;
let has_role = contract.acl_has_role(&account, role, account.id()).await?;
assert!(has_role);
Ok(())
}
#[tokio::test]
async fn test_acl_grant_role() -> anyhow::Result<()> {
let Setup {
worker,
contract,
account,
..
} = Setup::new().await?;
let contract_account = contract.contract().as_account();
let role = "ByMax3Increaser";
let granter = account;
let grantee = worker.dev_create_account().await?;
// An account which isn't admin can't grant the role.
contract
.assert_acl_is_admin(false, role, granter.id())
.await;
let granted = contract
.acl_grant_role(&granter, role, grantee.id())
.await?;
assert_eq!(granted, None);
contract
.assert_acl_has_role(false, role, grantee.id())
.await;
// Admin can grant the role.
contract
.acl_add_admin_unchecked(contract_account, role, granter.id())
.await?
.into_result()?;
let granted = contract
.acl_grant_role(&granter, role, grantee.id())
.await?;
assert_eq!(granted, Some(true));
contract.assert_acl_has_role(true, role, grantee.id()).await;
// Granting the role to an account which already is a grantee.
let granted = contract
.acl_grant_role(&granter, role, grantee.id())
.await?;
assert_eq!(granted, Some(false));
Ok(())
}
#[tokio::test]
async fn test_acl_grant_role_unchecked() -> anyhow::Result<()> {
let Setup {
contract, account, ..
} = Setup::new().await?;
let contract_account = contract.contract().as_account();
let role = "ByMax2Increaser";
contract
.assert_acl_has_role(false, role, account.id())
.await;
let res = contract
.acl_grant_role_unchecked(contract_account, role, account.id())
.await?;
assert_success_with(res, true);
contract.assert_acl_has_role(true, role, account.id()).await;
// Granting a role again behaves as expected.
let res = contract
.acl_grant_role_unchecked(contract_account, role, account.id())
.await?;
assert_success_with(res, false);
Ok(())
}
#[tokio::test]
async fn test_acl_revoke_role() -> anyhow::Result<()> {
let setup = Setup::new().await?;
let role = "ByMax3Increaser";
let grantee = setup.new_account_with_roles(&[role]).await?;
setup
.contract
.assert_acl_has_role(true, role, grantee.id())
.await;