Skip to content

Commit

Permalink
test locking should fail if already locked
Browse files Browse the repository at this point in the history
  • Loading branch information
mubarak23 committed Aug 20, 2024
1 parent 1e4ecf0 commit 94150a3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/components/lockable/lockable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ pub mod LockableComponent {
pub mod Errors {
pub const UNAUTHORIZED: felt252 = 'Account: unauthorized';
pub const NOT_OWNER: felt252 = 'Account: Not Account Owner';
pub const EXCEEDS_MAX_LOCK_TIME: felt252 = 'Lock time exceeded';
pub const LOCKED_ACCOUNT: felt252 = 'Account Locked';
pub const EXCEEDS_MAX_LOCK_TIME: felt252 = 'Account: Lock time exceeded';
pub const LOCKED_ACCOUNT: felt252 = 'Account: Locked';
}


Expand Down
4 changes: 2 additions & 2 deletions src/components/presets/account_preset.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub mod AccountPreset {
fn execute(ref self: ContractState, mut calls: Array<Call>) -> Array<Span<felt252>> {
// cannot make this call when the account is lock
let is_lock = self.lockable.is_lock();
assert(is_lock != true, 'Account locked');
assert(is_lock != true, 'Account: locked');
self.account._execute(calls)
}
}
Expand All @@ -79,7 +79,7 @@ pub mod AccountPreset {
fn upgrade(ref self: ContractState, new_class_hash: ClassHash) {
// cannot make this call when the account is lock
let is_lock = self.lockable.is_lock();
assert(is_lock != true, 'Account locked');
assert(is_lock != true, 'Account: locked');
self.upgradeable._upgrade(new_class_hash);
}
}
Expand Down
33 changes: 25 additions & 8 deletions tests/test_lockable_component.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,16 @@ fn test_lockable() {
start_cheat_caller_address(contract_address, owner);

let lockable_dispatcher = ILockableDispatcher { contract_address };

lockable_dispatcher.lock(40);
let lock_duration = 40_u64;
lockable_dispatcher.lock(lock_duration);
let check_lock = lockable_dispatcher.is_lock();

assert(check_lock == true, 'Account Not Lock');
stop_cheat_caller_address(contract_address);
}

#[test]
#[should_panic(expected: ('Account locked',))]
#[should_panic(expected: ('Account: locked',))]
fn test_execute_should_fail_when_locked() {
let (contract_address, _) = __setup__();
let acct_dispatcher = IAccountDispatcher { contract_address: contract_address };
Expand Down Expand Up @@ -131,7 +131,7 @@ fn test_execute_should_fail_when_locked() {
}

#[test]
#[should_panic(expected: ('Account locked',))]
#[should_panic(expected: ('Account: locked',))]
fn test_upgrade_should_fail_when_locked() {
let (contract_address, _) = __setup__();
let acct_dispatcher = IAccountDispatcher { contract_address: contract_address };
Expand All @@ -151,22 +151,39 @@ fn test_upgrade_should_fail_when_locked() {
start_cheat_caller_address(contract_address, owner);
dispatcher.upgrade(new_class_hash);
}
#[test]
#[should_panic(expected: ('Account: Locked',))]
fn test_locking_should_fail_if_already_locked() {
let (contract_address, _) = __setup__();
let acct_dispatcher = IAccountDispatcher { contract_address: contract_address };

let owner = acct_dispatcher.owner();

start_cheat_caller_address(contract_address, owner);

let lockable_dispatcher = ILockableDispatcher { contract_address };
// First Lock
let lock_duration_one = 40_u64;
lockable_dispatcher.lock(lock_duration_one);
// Second lock
let lock_duration_two = 60_u64;
lockable_dispatcher.lock(lock_duration_two);
}

#[test]
#[should_panic(expected: ('Account: unauthorized',))]
fn test_locking_should_fail_if_not_owner() {
let (contract_address, _) = __setup__();
let acct_dispatcher = IAccountDispatcher { contract_address: contract_address };

start_cheat_caller_address(contract_address, ACCOUNT2.try_into().unwrap());

let lockable_dispatcher = ILockableDispatcher { contract_address };

lockable_dispatcher.lock(40);
let lock_duration = 40_u64;
lockable_dispatcher.lock(lock_duration);
}

#[test]
#[should_panic(expected: ('Lock time exceeded',))]
#[should_panic(expected: ('Account: Lock time exceeded',))]
fn test_should_fail_for_greater_than_a_year_lock_time() {
let (contract_address, _) = __setup__();
let acct_dispatcher = IAccountDispatcher { contract_address: contract_address };
Expand Down

0 comments on commit 94150a3

Please sign in to comment.