Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
rm root tests: use util dispatch_as is possible for root to use any o…
Browse files Browse the repository at this point in the history
…rigin.
  • Loading branch information
nuke-web3 committed Sep 13, 2022
1 parent d26a3d3 commit 1dbad94
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 80 deletions.
12 changes: 6 additions & 6 deletions frame/safe-mode/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
use super::*;
use crate as pallet_safe_mode;

use frame_support::{parameter_types, traits::{InsideBoth, SortedMembers, EitherOfDiverse, Everything}};
use frame_system::{EnsureRoot, EnsureSignedBy};
use frame_support::{parameter_types, traits::{InsideBoth, SortedMembers, Everything}};
use frame_system::EnsureSignedBy;
use sp_core::H256;
use sp_runtime::{
testing::Header,
Expand Down Expand Up @@ -132,10 +132,10 @@ impl Config for Test {
type SafeModeFilter = MockSafeModeFilter;
type EnableDuration = EnableDuration;
type ExtendDuration = ExtendDuration;
type EnableOrigin = EitherOfDiverse<EnsureSignedBy<EnableOrigin, Self::AccountId>, EnsureRoot<Self::AccountId>>; // TODO should not need to explicitly define root in addition?
type ExtendOrigin = EitherOfDiverse<EnsureSignedBy<ExtendOrigin, Self::AccountId>, EnsureRoot<Self::AccountId>>; // TODO should not need to explicitly define root in addition?
type DisableOrigin = EitherOfDiverse<EnsureSignedBy<DisableOrigin, Self::AccountId>, EnsureRoot<Self::AccountId>>; // TODO should not need to explicitly define root in addition?
type RepayOrigin = EitherOfDiverse<EnsureSignedBy<RepayOrigin, Self::AccountId>, EnsureRoot<Self::AccountId>>; // TODO should not need to explicitly define root in addition?
type EnableOrigin = EnsureSignedBy<EnableOrigin, Self::AccountId>;
type ExtendOrigin = EnsureSignedBy<ExtendOrigin, Self::AccountId>;
type DisableOrigin = EnsureSignedBy<DisableOrigin, Self::AccountId>;
type RepayOrigin = EnsureSignedBy<RepayOrigin, Self::AccountId>;
type EnableStakeAmount = EnableStakeAmount;
type ExtendStakeAmount = ExtendStakeAmount;
}
Expand Down
76 changes: 2 additions & 74 deletions frame/safe-mode/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn fails_to_extend_if_not_enabled() {
fn can_automatically_disable_after_timeout() {
new_test_ext().execute_with(|| {
let enabled_at_block = System::block_number();
assert_ok!(SafeMode::force_enable(Origin::root()));
assert_ok!(SafeMode::force_enable(Origin::signed(mock::EnableOrigin::get())));
run_to(mock::EnableDuration::get() + enabled_at_block + 1);
SafeMode::on_initialize(System::block_number());
assert_eq!(SafeMode::enabled(), None);
Expand Down Expand Up @@ -135,7 +135,7 @@ fn fails_signed_origin_when_explicit_origin_required() {
#[test]
fn fails_force_disable_if_not_enabled() {
new_test_ext().execute_with(|| {
assert_noop!(SafeMode::force_disable(Origin::root()), Error::<Test>::IsDisabled);
assert_noop!(SafeMode::force_disable(Origin::signed(mock::DisableOrigin::get())), Error::<Test>::IsDisabled);
assert_noop!(
SafeMode::force_disable(Origin::signed(mock::DisableOrigin::get())),
Error::<Test>::IsDisabled
Expand Down Expand Up @@ -250,75 +250,3 @@ fn fails_when_explicit_origin_required() {

});
}

// ROOT ORIGIN CALL TESTS ---------------------

#[test]
fn can_force_enable_with_root() {
new_test_ext().execute_with(|| {
assert_ok!(SafeMode::force_enable(Origin::root()));
assert_eq!(
SafeMode::enabled().unwrap(),
System::block_number() + mock::EnableDuration::get()
);
assert_noop!(SafeMode::force_enable(Origin::root()), Error::<Test>::IsEnabled);
// assert_eq!(Balances::reserved_balance(Origin::root()), 0);
// TODO: without an explicit account required... can "raw" root ever have a balance reserved?
// If so, check here and below.
});
}

#[test]
fn can_force_extend_with_root() {
new_test_ext().execute_with(|| {
assert_ok!(SafeMode::force_enable(Origin::root()));
assert_eq!(
SafeMode::enabled().unwrap(),
System::block_number() + mock::EnableDuration::get()
);
assert_ok!(SafeMode::force_extend(Origin::root()));
assert_eq!(
SafeMode::enabled().unwrap(),
System::block_number() + mock::EnableDuration::get() + mock::ExtendDuration::get()
);
});
}

#[test]
fn can_force_disable_with_root() {
new_test_ext().execute_with(|| {
assert_eq!(SafeMode::enabled(), None);
assert_err!(SafeMode::force_disable(Origin::root()), Error::<Test>::IsDisabled);
assert_ok!(SafeMode::force_enable(Origin::root()));
assert_ok!(SafeMode::force_disable(Origin::root()));
});
}

#[test]
fn can_repay_stake_with_root() {
new_test_ext().execute_with(|| {
let enabled_at_block = System::block_number();
assert_ok!(SafeMode::enable(Origin::signed(0)));
assert_err!(
SafeMode::repay_stake(Origin::root(), 0, enabled_at_block),
Error::<Test>::IsEnabled
);
run_to(mock::EnableDuration::get() + enabled_at_block + 1);
SafeMode::on_initialize(System::block_number());
assert_ok!(SafeMode::repay_stake(Origin::root(), 0, enabled_at_block));
});
}

#[test]
fn can_slash_stake_with_root() {
new_test_ext().execute_with(|| {
let enabled_at_block = System::block_number();
assert_ok!(SafeMode::enable(Origin::signed(0)));
assert_err!(
SafeMode::slash_stake(Origin::root(), 0, enabled_at_block),
Error::<Test>::IsEnabled
);
run_to(mock::EnableDuration::get() + enabled_at_block + 1);
assert_ok!(SafeMode::slash_stake(Origin::root(), 0, enabled_at_block));
});
}

0 comments on commit 1dbad94

Please sign in to comment.