From 6f028f0f7a6a0a9a727e4ba959a7d9e38bc72559 Mon Sep 17 00:00:00 2001 From: ChrisFernandezVivas Date: Mon, 25 Nov 2024 14:51:32 -0600 Subject: [PATCH 1/3] test for set_contact_handle --- contracts/tests/test_fund.cairo | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/contracts/tests/test_fund.cairo b/contracts/tests/test_fund.cairo index 48b7e2a..e389190 100644 --- a/contracts/tests/test_fund.cairo +++ b/contracts/tests/test_fund.cairo @@ -502,3 +502,33 @@ fn test_emit_event_donation_withdraw() { ) ]); } + +#[test] +fn test_set_contact_handle(){ + let contract_address = _setup_(); + let dispatcher = IFundDispatcher { contract_address }; + let contact_handle = dispatcher.get_contact_handle(); + let authorized_callers:Vec = vec![OWNER(), VALID_ADDRESS_1(), VALID_ADDRESS_2()]; + assert(contact_handle == CONTACT_HANDLE_1(), 'Invalid contact handle'); + + for caller in authorized_callers { + start_cheat_caller_address_global(caller); + dispatcher.set_contact_handle(CONTACT_HANDLE_2());//set + let new_contact_handle = dispatcher.get_contact_handle();//get + assert(new_contact_handle == CONTACT_HANDLE_2(),format!("Set contact method not working for caller {:?}", caller)); + dispatcher.set_contact_handle(CONTACT_HANDLE_1()); + let reverted_contact_handle = dispatcher.get_contact_handle(); + assert(reverted_contact_handle == CONTACT_HANDLE_1(),format!("Revert contact handle not working for caller {:?}", caller) + ) + }; + + // set a no valid address + let no_valid_address = 0x12345; + start_cheat_caller_address_global(no_valid_address); + dispatcher.set_contact_handle(CONTACT_HANDLE_2());//set + let new_contact_handle = dispatcher.get_contact_handle();//get + assert(new_contact_handle == CONTACT_HANDLE_1(),"this caller is not a valid address and changed the storage, please check the function ..."); + + + +} From 452bc596d15a5d2b3b66fa92d17f578e9ca660bb Mon Sep 17 00:00:00 2001 From: ChrisFernandezVivas Date: Mon, 25 Nov 2024 15:41:38 -0600 Subject: [PATCH 2/3] test set contact handle --- contracts/tests/test_fund.cairo | 44 ++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/contracts/tests/test_fund.cairo b/contracts/tests/test_fund.cairo index e389190..890cd5e 100644 --- a/contracts/tests/test_fund.cairo +++ b/contracts/tests/test_fund.cairo @@ -504,30 +504,46 @@ fn test_emit_event_donation_withdraw() { } #[test] +#[should_panic(expected: ("You must be an owner or admin to perform this action",))] fn test_set_contact_handle(){ let contract_address = _setup_(); let dispatcher = IFundDispatcher { contract_address }; let contact_handle = dispatcher.get_contact_handle(); - let authorized_callers:Vec = vec![OWNER(), VALID_ADDRESS_1(), VALID_ADDRESS_2()]; assert(contact_handle == CONTACT_HANDLE_1(), 'Invalid contact handle'); - for caller in authorized_callers { - start_cheat_caller_address_global(caller); - dispatcher.set_contact_handle(CONTACT_HANDLE_2());//set - let new_contact_handle = dispatcher.get_contact_handle();//get - assert(new_contact_handle == CONTACT_HANDLE_2(),format!("Set contact method not working for caller {:?}", caller)); - dispatcher.set_contact_handle(CONTACT_HANDLE_1()); - let reverted_contact_handle = dispatcher.get_contact_handle(); - assert(reverted_contact_handle == CONTACT_HANDLE_1(),format!("Revert contact handle not working for caller {:?}", caller) - ) - }; + //owner + start_cheat_caller_address_global(OWNER()); + dispatcher.set_contact_handle(CONTACT_HANDLE_2());//set + let new_contact_handle = dispatcher.get_contact_handle();//get + assert(new_contact_handle == CONTACT_HANDLE_2(), 'Set contact method not working'); + dispatcher.set_contact_handle(CONTACT_HANDLE_1()); + let reverted_contact_handle = dispatcher.get_contact_handle(); + assert(reverted_contact_handle == CONTACT_HANDLE_1(), 'revert'); + + //valid_address1 + start_cheat_caller_address_global(VALID_ADDRESS_1()); + dispatcher.set_contact_handle(CONTACT_HANDLE_2());//set + let new_contact_handle = dispatcher.get_contact_handle();//get + assert(new_contact_handle == CONTACT_HANDLE_2(), 'Set contact method not working'); + dispatcher.set_contact_handle(CONTACT_HANDLE_1()); + let reverted_contact_handle = dispatcher.get_contact_handle(); + assert(reverted_contact_handle == CONTACT_HANDLE_1(), 'revert'); + + //valid_address2 + start_cheat_caller_address_global(VALID_ADDRESS_2()); + dispatcher.set_contact_handle(CONTACT_HANDLE_2());//set + let new_contact_handle = dispatcher.get_contact_handle();//get + assert(new_contact_handle == CONTACT_HANDLE_2(), 'Set contact method not working'); + dispatcher.set_contact_handle(CONTACT_HANDLE_1()); + let reverted_contact_handle = dispatcher.get_contact_handle(); + assert(reverted_contact_handle == CONTACT_HANDLE_1(),' revert '); // set a no valid address - let no_valid_address = 0x12345; - start_cheat_caller_address_global(no_valid_address); + //always should fail + start_cheat_caller_address_global(OTHER_USER()); dispatcher.set_contact_handle(CONTACT_HANDLE_2());//set let new_contact_handle = dispatcher.get_contact_handle();//get - assert(new_contact_handle == CONTACT_HANDLE_1(),"this caller is not a valid address and changed the storage, please check the function ..."); + assert(new_contact_handle == CONTACT_HANDLE_1(),'check the function'); From 6373a024a64134a701805aef2301ae95d930730c Mon Sep 17 00:00:00 2001 From: ChrisFernandezVivas Date: Mon, 25 Nov 2024 16:41:55 -0600 Subject: [PATCH 3/3] remover blank spaces,divided test in success and error cases --- contracts/src/fund.cairo | 15 ++++--- contracts/tests/test_fund.cairo | 70 ++++++++++++++++++--------------- 2 files changed, 45 insertions(+), 40 deletions(-) diff --git a/contracts/src/fund.cairo b/contracts/src/fund.cairo index ccae2a9..dab8ea2 100644 --- a/contracts/src/fund.cairo +++ b/contracts/src/fund.cairo @@ -130,15 +130,14 @@ pub mod Fund { } fn set_name(ref self: ContractState, name: ByteArray) { let caller = get_caller_address(); - let valid_address_1 = contract_address_const::< - FundManagerConstants::VALID_ADDRESS_1 - >(); - let valid_address_2 = contract_address_const::< - FundManagerConstants::VALID_ADDRESS_2 - >(); + let valid_address_1 = contract_address_const::(); + let valid_address_2 = contract_address_const::(); assert!( - self.owner.read() == caller || valid_address_1 == caller || valid_address_2 == caller, - "You must be an owner or admin to perform this action"); + self.owner.read() == caller + || valid_address_1 == caller + || valid_address_2 == caller, + "You must be an owner or admin to perform this action" + ); self.name.write(name); } fn get_name(self: @ContractState) -> ByteArray { diff --git a/contracts/tests/test_fund.cairo b/contracts/tests/test_fund.cairo index 890cd5e..8278a94 100644 --- a/contracts/tests/test_fund.cairo +++ b/contracts/tests/test_fund.cairo @@ -491,60 +491,66 @@ fn test_emit_event_donation_withdraw() { dispatcher.withdraw(); - spy.assert_emitted(@array![ - ( - contract_address, - Fund::Event::DonationWithdraw(Fund::DonationWithdraw { - owner_address: OWNER(), - fund_contract_address: contract_address, - withdrawn_amount - }) - ) - ]); + spy + .assert_emitted( + @array![ + ( + contract_address, + Fund::Event::DonationWithdraw( + Fund::DonationWithdraw { + owner_address: OWNER(), + fund_contract_address: contract_address, + withdrawn_amount + } + ) + ) + ] + ); } + #[test] #[should_panic(expected: ("You must be an owner or admin to perform this action",))] -fn test_set_contact_handle(){ +fn test_set_contact_handle_error() { + let contract_address = _setup_(); + let dispatcher = IFundDispatcher { contract_address }; + let contact_handle = dispatcher.get_contact_handle(); + assert(contact_handle == CONTACT_HANDLE_1(), 'Invalid contact handle'); + + start_cheat_caller_address_global(OTHER_USER()); + dispatcher.set_contact_handle(CONTACT_HANDLE_2()) +} + +#[test] +fn test_set_contact_handle_success() { let contract_address = _setup_(); let dispatcher = IFundDispatcher { contract_address }; let contact_handle = dispatcher.get_contact_handle(); assert(contact_handle == CONTACT_HANDLE_1(), 'Invalid contact handle'); - //owner start_cheat_caller_address_global(OWNER()); - dispatcher.set_contact_handle(CONTACT_HANDLE_2());//set - let new_contact_handle = dispatcher.get_contact_handle();//get + dispatcher.set_contact_handle(CONTACT_HANDLE_2()); + let new_contact_handle = dispatcher.get_contact_handle(); assert(new_contact_handle == CONTACT_HANDLE_2(), 'Set contact method not working'); dispatcher.set_contact_handle(CONTACT_HANDLE_1()); let reverted_contact_handle = dispatcher.get_contact_handle(); assert(reverted_contact_handle == CONTACT_HANDLE_1(), 'revert'); - - //valid_address1 + start_cheat_caller_address_global(VALID_ADDRESS_1()); - dispatcher.set_contact_handle(CONTACT_HANDLE_2());//set - let new_contact_handle = dispatcher.get_contact_handle();//get + dispatcher.set_contact_handle(CONTACT_HANDLE_2()); + let new_contact_handle = dispatcher.get_contact_handle(); assert(new_contact_handle == CONTACT_HANDLE_2(), 'Set contact method not working'); dispatcher.set_contact_handle(CONTACT_HANDLE_1()); let reverted_contact_handle = dispatcher.get_contact_handle(); assert(reverted_contact_handle == CONTACT_HANDLE_1(), 'revert'); - - //valid_address2 + start_cheat_caller_address_global(VALID_ADDRESS_2()); - dispatcher.set_contact_handle(CONTACT_HANDLE_2());//set - let new_contact_handle = dispatcher.get_contact_handle();//get + dispatcher.set_contact_handle(CONTACT_HANDLE_2()); + let new_contact_handle = dispatcher.get_contact_handle(); assert(new_contact_handle == CONTACT_HANDLE_2(), 'Set contact method not working'); dispatcher.set_contact_handle(CONTACT_HANDLE_1()); let reverted_contact_handle = dispatcher.get_contact_handle(); - assert(reverted_contact_handle == CONTACT_HANDLE_1(),' revert '); - - // set a no valid address - //always should fail - start_cheat_caller_address_global(OTHER_USER()); - dispatcher.set_contact_handle(CONTACT_HANDLE_2());//set - let new_contact_handle = dispatcher.get_contact_handle();//get - assert(new_contact_handle == CONTACT_HANDLE_1(),'check the function'); - + assert(reverted_contact_handle == CONTACT_HANDLE_1(), ' revert ') +} -}