From fc62dcdde75fdcb1eb7492152e1013daad32e893 Mon Sep 17 00:00:00 2001 From: jsanchez556 Date: Fri, 22 Nov 2024 10:22:33 -0600 Subject: [PATCH 1/2] [feat] Add asserts to set_name --- contracts/src/fund.cairo | 8 +++++++- contracts/tests/test_fund.cairo | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/contracts/src/fund.cairo b/contracts/src/fund.cairo index d36a6a3..9ea5480 100644 --- a/contracts/src/fund.cairo +++ b/contracts/src/fund.cairo @@ -130,7 +130,13 @@ pub mod Fund { } fn set_name(ref self: ContractState, name: ByteArray) { let caller = get_caller_address(); - assert!(self.owner.read() == caller, "You are not the owner"); + let valid_address_1 = contract_address_const::< + FundManagerConstants::VALID_ADDRESS_1 + >(); + let valid_address_2 = contract_address_const::< + FundManagerConstants::VALID_ADDRESS_2 + >(); + assert!(valid_address_1 == caller || valid_address_2 == caller, "You are not the admin"); 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 c52dc42..8b38bc7 100644 --- a/contracts/tests/test_fund.cairo +++ b/contracts/tests/test_fund.cairo @@ -60,6 +60,9 @@ fn CONTACT_HANDLE_2() -> ByteArray { fn VALID_ADDRESS_1() -> ContractAddress { contract_address_const::() } +fn VALID_ADDRESS_2() -> ContractAddress { + contract_address_const::() +} fn _setup_() -> ContractAddress { let contract = declare("Fund").unwrap(); let mut calldata: Array = array![]; @@ -105,6 +108,23 @@ fn test_set_name() { let dispatcher = IFundDispatcher { contract_address }; let name = dispatcher.get_name(); assert(name == NAME(), 'Invalid name'); + + start_cheat_caller_address_global(VALID_ADDRESS_1()); + dispatcher.set_name("NEW_NAME_1"); + assert(dispatcher.get_name() == "NEW_NAME_1", 'Set name method not working'); + + start_cheat_caller_address_global(VALID_ADDRESS_2()); + dispatcher.set_name("NEW_NAME_2"); + assert(dispatcher.get_name() == "NEW_NAME_2", 'Set name method not working'); +} + +#[test] +#[should_panic(expected: ("You are not the admin",))] +fn test_set_name_not_admin() { + let contract_address = _setup_(); + let dispatcher = IFundDispatcher { contract_address }; + let name = dispatcher.get_name(); + assert(name == NAME(), 'Invalid name'); start_cheat_caller_address_global(OWNER()); dispatcher.set_name("NEW_NAME"); let new_name = dispatcher.get_name(); From 3cd73d697776fffcc8d90facef840c9b4dbfca1a Mon Sep 17 00:00:00 2001 From: jsanchez556 Date: Fri, 22 Nov 2024 23:03:31 -0600 Subject: [PATCH 2/2] [feat] Add asserts to set_name --- contracts/src/fund.cairo | 4 +++- contracts/tests/test_fund.cairo | 30 ++++++++++++++++++++++-------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/contracts/src/fund.cairo b/contracts/src/fund.cairo index 9ea5480..45a5f7a 100644 --- a/contracts/src/fund.cairo +++ b/contracts/src/fund.cairo @@ -136,7 +136,9 @@ pub mod Fund { let valid_address_2 = contract_address_const::< FundManagerConstants::VALID_ADDRESS_2 >(); - assert!(valid_address_1 == caller || valid_address_2 == caller, "You are not the admin"); + 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.name.write(name); } fn get_name(self: @ContractState) -> ByteArray { diff --git a/contracts/tests/test_fund.cairo b/contracts/tests/test_fund.cairo index 8b38bc7..2a3b458 100644 --- a/contracts/tests/test_fund.cairo +++ b/contracts/tests/test_fund.cairo @@ -103,32 +103,46 @@ fn test_constructor() { } #[test] -fn test_set_name() { +fn test_set_name_admin() { let contract_address = _setup_(); let dispatcher = IFundDispatcher { contract_address }; let name = dispatcher.get_name(); assert(name == NAME(), 'Invalid name'); start_cheat_caller_address_global(VALID_ADDRESS_1()); - dispatcher.set_name("NEW_NAME_1"); - assert(dispatcher.get_name() == "NEW_NAME_1", 'Set name method not working'); + dispatcher.set_name("NEW_NAME_ADMIN_1"); + assert(dispatcher.get_name() == "NEW_NAME_ADMIN_1", 'Set name method not working'); start_cheat_caller_address_global(VALID_ADDRESS_2()); - dispatcher.set_name("NEW_NAME_2"); - assert(dispatcher.get_name() == "NEW_NAME_2", 'Set name method not working'); + dispatcher.set_name("NEW_NAME_ADMIN_2"); + assert(dispatcher.get_name() == "NEW_NAME_ADMIN_2", 'Set name method not working'); } #[test] -#[should_panic(expected: ("You are not the admin",))] -fn test_set_name_not_admin() { +fn test_set_name_owner() { let contract_address = _setup_(); let dispatcher = IFundDispatcher { contract_address }; let name = dispatcher.get_name(); assert(name == NAME(), 'Invalid name'); + start_cheat_caller_address_global(OWNER()); dispatcher.set_name("NEW_NAME"); let new_name = dispatcher.get_name(); - assert(new_name == "NEW_NAME", 'Set name method not working') + assert(new_name == "NEW_NAME", 'Set name method not working'); +} + +#[test] +#[should_panic(expected: ("You must be an owner or admin to perform this action",))] +fn test_set_name_not_admin_or_owner() { + let contract_address = _setup_(); + let dispatcher = IFundDispatcher { contract_address }; + let name = dispatcher.get_name(); + assert(name == NAME(), 'Invalid name'); + + start_cheat_caller_address_global(FUND_MANAGER()); + dispatcher.set_name("NEW_NAME"); + let new_name = dispatcher.get_name(); + assert(new_name == "NEW_NAME", 'Set name method not working'); } #[test]