Skip to content

Commit

Permalink
Apply minor improvements to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
immrsd committed Nov 19, 2024
1 parent ff38c5e commit 8906e79
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 81 deletions.
20 changes: 9 additions & 11 deletions packages/account/src/tests/extensions/test_src9.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ use openzeppelin_testing::constants::{RECIPIENT, OWNER, OTHER, FELT_VALUE};
use openzeppelin_utils::cryptography::snip12::OffchainMessageHash;
use snforge_std::signature::KeyPairTrait;
use snforge_std::signature::stark_curve::{StarkCurveKeyPairImpl, StarkCurveSignerImpl};
use snforge_std::{
start_cheat_caller_address, cheat_caller_address, start_cheat_block_timestamp_global
};
use snforge_std::{test_address, load, CheatSpan};
use snforge_std::{start_cheat_block_timestamp_global, test_address, load, CheatSpan};
use snforge_std::{start_cheat_caller_address, cheat_caller_address};
use starknet::account::Call;
use starknet::storage::StorageMapWriteAccess;
use starknet::{ContractAddress, contract_address_const};
Expand Down Expand Up @@ -124,7 +122,7 @@ fn test_execute_from_outside_v2_uses_nonce() {
}

#[test]
#[should_panic(expected: ('SRC9: invalid caller',))]
#[should_panic(expected: 'SRC9: invalid caller')]
fn test_execute_from_outside_v2_caller_mismatch() {
let mut state = setup();
let mut outside_execution = setup_outside_execution(RECIPIENT(), false);
Expand All @@ -136,7 +134,7 @@ fn test_execute_from_outside_v2_caller_mismatch() {
}

#[test]
#[should_panic(expected: ('SRC9: now >= execute_before',))]
#[should_panic(expected: 'SRC9: now >= execute_before')]
fn test_execute_from_outside_v2_call_after_execute_before() {
let mut state = setup();
let outside_execution = setup_outside_execution(RECIPIENT(), false);
Expand All @@ -147,7 +145,7 @@ fn test_execute_from_outside_v2_call_after_execute_before() {
}

#[test]
#[should_panic(expected: ('SRC9: now >= execute_before',))]
#[should_panic(expected: 'SRC9: now >= execute_before')]
fn test_execute_from_outside_v2_call_equal_to_execute_before() {
let mut state = setup();
let outside_execution = setup_outside_execution(RECIPIENT(), false);
Expand All @@ -158,7 +156,7 @@ fn test_execute_from_outside_v2_call_equal_to_execute_before() {
}

#[test]
#[should_panic(expected: ('SRC9: now <= execute_after',))]
#[should_panic(expected: 'SRC9: now <= execute_after')]
fn test_execute_from_outside_v2_call_before_execute_after() {
let mut state = setup();
let outside_execution = setup_outside_execution(RECIPIENT(), false);
Expand All @@ -169,7 +167,7 @@ fn test_execute_from_outside_v2_call_before_execute_after() {
}

#[test]
#[should_panic(expected: ('SRC9: now <= execute_after',))]
#[should_panic(expected: 'SRC9: now <= execute_after')]
fn test_execute_from_outside_v2_call_equal_to_execute_after() {
let mut state = setup();
let outside_execution = setup_outside_execution(RECIPIENT(), false);
Expand All @@ -180,7 +178,7 @@ fn test_execute_from_outside_v2_call_equal_to_execute_after() {
}

#[test]
#[should_panic(expected: ('SRC9: duplicated nonce',))]
#[should_panic(expected: 'SRC9: duplicated nonce')]
fn test_execute_from_outside_v2_invalid_nonce() {
let mut state = setup();
let outside_execution = setup_outside_execution(RECIPIENT(), false);
Expand All @@ -191,7 +189,7 @@ fn test_execute_from_outside_v2_invalid_nonce() {
}

#[test]
#[should_panic(expected: ('SRC9: invalid signature',))]
#[should_panic(expected: 'SRC9: invalid signature')]
fn test_execute_from_outside_v2_invalid_signature() {
let key_pair = KeyPairTrait::generate();
let account = setup_account(key_pair.public_key);
Expand Down
66 changes: 28 additions & 38 deletions packages/account/src/tests/test_account.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@ use openzeppelin_test_common::mocks::account::DualCaseAccountMock;
use openzeppelin_test_common::mocks::simple::{ISimpleMockDispatcher, ISimpleMockDispatcherTrait};
use openzeppelin_testing as utils;
use openzeppelin_testing::constants::stark::{KEY_PAIR, KEY_PAIR_2};
use openzeppelin_testing::constants::{
SALT, ZERO, OTHER, CALLER, QUERY_OFFSET, QUERY_VERSION, MIN_TRANSACTION_VERSION
};
use openzeppelin_testing::constants::{SALT, QUERY_OFFSET, QUERY_VERSION, MIN_TRANSACTION_VERSION};
use openzeppelin_testing::constants::{ZERO, OTHER, CALLER};
use openzeppelin_testing::signing::StarkKeyPair;
use snforge_std::{
start_cheat_signature_global, start_cheat_transaction_version_global,
start_cheat_transaction_hash_global
};
use snforge_std::{spy_events, test_address, start_cheat_caller_address};
use snforge_std::{spy_events, start_cheat_signature_global, start_cheat_transaction_hash_global};
use snforge_std::{test_address, start_cheat_caller_address, start_cheat_transaction_version_global};
use starknet::account::Call;

//
Expand Down Expand Up @@ -48,13 +44,13 @@ fn setup_dispatcher(
let contract_class = utils::declare_class("DualCaseAccountMock");

let calldata = array![key_pair.public_key];
let address = utils::deploy(contract_class, calldata);
let dispatcher = AccountABIDispatcher { contract_address: address };
let contract_address = utils::deploy(contract_class, calldata);
let dispatcher = AccountABIDispatcher { contract_address };

start_cheat_signature_global(array![data.r, data.s].span());
start_cheat_transaction_hash_global(data.tx_hash);
start_cheat_transaction_version_global(MIN_TRANSACTION_VERSION);
start_cheat_caller_address(address, ZERO());
start_cheat_caller_address(contract_address, ZERO());

(dispatcher, contract_class.class_hash.into())
}
Expand Down Expand Up @@ -114,7 +110,7 @@ fn test_validate_deploy() {
}

#[test]
#[should_panic(expected: ('Account: invalid signature',))]
#[should_panic(expected: 'Account: invalid signature')]
fn test_validate_deploy_invalid_signature_data() {
let key_pair = KEY_PAIR();
let mut data = SIGNED_TX_DATA(key_pair);
Expand All @@ -125,7 +121,7 @@ fn test_validate_deploy_invalid_signature_data() {
}

#[test]
#[should_panic(expected: ('Account: invalid signature',))]
#[should_panic(expected: 'Account: invalid signature')]
fn test_validate_deploy_invalid_signature_length() {
let key_pair = KEY_PAIR();
let (account, class_hash) = setup_dispatcher(key_pair, SIGNED_TX_DATA(key_pair));
Expand All @@ -136,7 +132,7 @@ fn test_validate_deploy_invalid_signature_length() {
}

#[test]
#[should_panic(expected: ('Account: invalid signature',))]
#[should_panic(expected: 'Account: invalid signature')]
fn test_validate_deploy_empty_signature() {
let key_pair = KEY_PAIR();
let (account, class_hash) = setup_dispatcher(key_pair, SIGNED_TX_DATA(key_pair));
Expand All @@ -159,7 +155,7 @@ fn test_validate_declare() {
}

#[test]
#[should_panic(expected: ('Account: invalid signature',))]
#[should_panic(expected: 'Account: invalid signature')]
fn test_validate_declare_invalid_signature_data() {
let key_pair = KEY_PAIR();
let mut data = SIGNED_TX_DATA(key_pair);
Expand All @@ -170,7 +166,7 @@ fn test_validate_declare_invalid_signature_data() {
}

#[test]
#[should_panic(expected: ('Account: invalid signature',))]
#[should_panic(expected: 'Account: invalid signature')]
fn test_validate_declare_invalid_signature_length() {
let key_pair = KEY_PAIR();
let (account, class_hash) = setup_dispatcher(key_pair, SIGNED_TX_DATA(key_pair));
Expand All @@ -181,7 +177,7 @@ fn test_validate_declare_invalid_signature_length() {
}

#[test]
#[should_panic(expected: ('Account: invalid signature',))]
#[should_panic(expected: 'Account: invalid signature')]
fn test_validate_declare_empty_signature() {
let key_pair = KEY_PAIR();
let (account, class_hash) = setup_dispatcher(key_pair, SIGNED_TX_DATA(key_pair));
Expand All @@ -197,16 +193,14 @@ fn test_execute_with_version(version: Option<felt252>) {

// Deploy target contract
let calldata = array![];
let address = utils::declare_and_deploy("SimpleMock", calldata);
let simple_mock = ISimpleMockDispatcher { contract_address: address };
let contract_address = utils::declare_and_deploy("SimpleMock", calldata);
let simple_mock = ISimpleMockDispatcher { contract_address };

// Craft call and add to calls array
let amount = 200;
let calldata = array![amount];
let call = Call {
to: simple_mock.contract_address,
selector: selector!("increase_balance"),
calldata: calldata.span()
to: contract_address, selector: selector!("increase_balance"), calldata: calldata.span()
};
let calls = array![call];

Expand Down Expand Up @@ -243,7 +237,7 @@ fn test_execute_query_version() {
}

#[test]
#[should_panic(expected: ('Account: invalid tx version',))]
#[should_panic(expected: 'Account: invalid tx version')]
fn test_execute_invalid_query_version() {
test_execute_with_version(Option::Some(QUERY_OFFSET));
}
Expand All @@ -254,7 +248,7 @@ fn test_execute_future_query_version() {
}

#[test]
#[should_panic(expected: ('Account: invalid tx version',))]
#[should_panic(expected: 'Account: invalid tx version')]
fn test_execute_invalid_version() {
test_execute_with_version(Option::Some(MIN_TRANSACTION_VERSION - 1));
}
Expand All @@ -270,7 +264,7 @@ fn test_validate() {
}

#[test]
#[should_panic(expected: ('Account: invalid signature',))]
#[should_panic(expected: 'Account: invalid signature')]
fn test_validate_invalid() {
let key_pair = KEY_PAIR();
let mut data = SIGNED_TX_DATA(key_pair);
Expand All @@ -288,25 +282,21 @@ fn test_multicall() {

// Deploy target contract
let calldata = array![];
let address = utils::declare_and_deploy("SimpleMock", calldata);
let simple_mock = ISimpleMockDispatcher { contract_address: address };
let contract_address = utils::declare_and_deploy("SimpleMock", calldata);
let simple_mock = ISimpleMockDispatcher { contract_address };

// Craft 1st call
let amount1 = 300;
let calldata1 = array![amount1];
let call1 = Call {
to: simple_mock.contract_address,
selector: selector!("increase_balance"),
calldata: calldata1.span()
to: contract_address, selector: selector!("increase_balance"), calldata: calldata1.span()
};

// Craft 2nd call
let amount2 = 500;
let calldata2 = array![amount2];
let call2 = Call {
to: simple_mock.contract_address,
selector: selector!("increase_balance"),
calldata: calldata2.span()
to: contract_address, selector: selector!("increase_balance"), calldata: calldata2.span()
};

// Bundle calls and execute
Expand Down Expand Up @@ -339,7 +329,7 @@ fn test_multicall_zero_calls() {
}

#[test]
#[should_panic(expected: ('Account: invalid caller',))]
#[should_panic(expected: 'Account: invalid caller')]
fn test_account_called_from_contract() {
let state = setup(KEY_PAIR());
let account_address = test_address();
Expand Down Expand Up @@ -379,7 +369,7 @@ fn test_public_key_setter_and_getter() {
}

#[test]
#[should_panic(expected: ('Account: unauthorized',))]
#[should_panic(expected: 'Account: unauthorized')]
fn test_public_key_setter_different_account() {
let mut state = COMPONENT_STATE();
let account_address = test_address();
Expand Down Expand Up @@ -418,7 +408,7 @@ fn test_public_key_setter_and_getter_camel() {
}

#[test]
#[should_panic(expected: ('Account: unauthorized',))]
#[should_panic(expected: 'Account: unauthorized')]
fn test_public_key_setter_different_account_camel() {
let mut state = COMPONENT_STATE();
let account_address = test_address();
Expand Down Expand Up @@ -462,7 +452,7 @@ fn test_assert_only_self_true() {
}

#[test]
#[should_panic(expected: ('Account: unauthorized',))]
#[should_panic(expected: 'Account: unauthorized')]
fn test_assert_only_self_false() {
let mut state = COMPONENT_STATE();
let account_address = test_address();
Expand All @@ -487,7 +477,7 @@ fn test_assert_valid_new_owner() {


#[test]
#[should_panic(expected: ('Account: invalid signature',))]
#[should_panic(expected: 'Account: invalid signature')]
fn test_assert_valid_new_owner_invalid_signature() {
let key_pair = KEY_PAIR();
let state = setup(key_pair);
Expand Down
Loading

0 comments on commit 8906e79

Please sign in to comment.