Skip to content

Commit

Permalink
add test to assign multiple roles
Browse files Browse the repository at this point in the history
  • Loading branch information
salman01zp committed Nov 17, 2023
1 parent 36dc8b9 commit 69f70ee
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
6 changes: 1 addition & 5 deletions pallets/roles/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,9 @@ impl pallet_staking::Config for Runtime {
type WeightInfo = ();
}

parameter_types! {
pub const RolesPalletId: PalletId = PalletId(*b"py/roles");
}

impl Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type PalletId = RolesPalletId;
type MaxRolesPerAccount = ConstU32<2>;
type WeightInfo = ();
}

Expand Down
38 changes: 32 additions & 6 deletions pallets/roles/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn test_assign_role() {
})]);

// Lets verify role assigned to account.
assert_eq!(Roles::account_role(1), Some(RoleType::Tss));
assert_eq!(Roles::has_role(1, RoleType::Tss), true);
// Verify ledger mapping
assert_eq!(Roles::ledger(1), Some(RoleStakingLedger { stash: 1, total: 5000 }));
});
Expand All @@ -57,12 +57,38 @@ fn test_assign_role_with_full_staking_option() {
})]);

// Lets verify role assigned to account.
assert_eq!(Roles::account_role(1), Some(RoleType::Tss));
assert_eq!(Roles::has_role(1, RoleType::Tss), true);
// Verify ledger mapping
assert_eq!(Roles::ledger(1), Some(RoleStakingLedger { stash: 1, total: 10000 }));
});
}

// test assign multiple roles to an account.
#[test]
fn test_assign_multiple_roles() {
new_test_ext_raw_authorities(vec![1, 2, 3, 4]).execute_with(|| {
// Initially account if funded with 10000 tokens and we are trying to bond 5000 tokens
assert_ok!(Roles::assign_role(
RuntimeOrigin::signed(1),
RoleType::Tss,
ReStakingOption::Full
));

// Lets verify role assigned to account.
assert_eq!(Roles::has_role(1, RoleType::Tss), true);

// Now lets assign another role to the same account.
assert_ok!(Roles::assign_role(
RuntimeOrigin::signed(1),
RoleType::ZkSaas,
ReStakingOption::Full
));

// Lets verify role assigned to account.
assert_eq!(Roles::has_role(1, RoleType::ZkSaas), true);
});
}

#[test]
fn test_clear_role() {
new_test_ext_raw_authorities(vec![1, 2, 3, 4]).execute_with(|| {
Expand All @@ -82,7 +108,7 @@ fn test_clear_role() {
})]);

// Role should be removed from account role mappings.
assert_eq!(Roles::account_role(1), None);
assert_eq!(Roles::has_role(1, RoleType::Tss), false);

// Ledger should be removed from ledger mappings.
assert_eq!(Roles::ledger(1), None);
Expand Down Expand Up @@ -116,13 +142,13 @@ fn test_unbound_funds_should_work() {
));

// Lets verify role is assigned to account.
assert_eq!(Roles::account_role(1), Some(RoleType::Tss));
assert_eq!(Roles::has_role(1, RoleType::Tss), true);

// Lets clear the role.
assert_ok!(Roles::clear_role(RuntimeOrigin::signed(1), RoleType::Tss));

// Role should be removed from account role mappings.
assert_eq!(Roles::account_role(1), None);
assert_eq!(Roles::has_role(1, RoleType::Tss), false);

// unbound funds.
assert_ok!(Roles::unbound_funds(RuntimeOrigin::signed(1), 5000));
Expand Down Expand Up @@ -152,7 +178,7 @@ fn test_unbound_funds_should_fail_if_role_assigned() {
));

// Lets verify role is assigned to account.
assert_eq!(Roles::account_role(1), Some(RoleType::Tss));
assert_eq!(Roles::has_role(1, RoleType::Tss), true);

// Lets try to unbound funds.
assert_err!(
Expand Down

0 comments on commit 69f70ee

Please sign in to comment.