diff --git a/contracts/src/fund.cairo b/contracts/src/fund.cairo index d36a6a3..45a5f7a 100644 --- a/contracts/src/fund.cairo +++ b/contracts/src/fund.cairo @@ -130,7 +130,15 @@ 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!( + 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 c52dc42..2a3b458 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![]; @@ -100,15 +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_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_ADMIN_2"); + assert(dispatcher.get_name() == "NEW_NAME_ADMIN_2", 'Set name method not working'); +} + +#[test] +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]