diff --git a/contracts/src/fund.cairo b/contracts/src/fund.cairo index 4ad2cef..5a2d137 100644 --- a/contracts/src/fund.cairo +++ b/contracts/src/fund.cairo @@ -143,17 +143,17 @@ pub mod Fund { fn get_name(self: @ContractState) -> ByteArray { return self.name.read(); } - fn set_reason(ref self: ContractState, reason: ByteArray) { - let caller = get_caller_address(); - let valid_address_1 = contract_address_const::(); - let valid_address_2 = contract_address_const::(); + fn set_reason(ref self: ContractState, reason: ByteArray) { + let caller = get_caller_address(); + 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" - ); + 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.reason.write(reason); + self.reason.write(reason); } fn get_reason(self: @ContractState) -> ByteArray { return self.reason.read(); diff --git a/contracts/tests/test_fund.cairo b/contracts/tests/test_fund.cairo index 1afd5d0..ae05333 100644 --- a/contracts/tests/test_fund.cairo +++ b/contracts/tests/test_fund.cairo @@ -37,10 +37,10 @@ fn NAME() -> ByteArray { "Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum" } fn REASON_1() -> ByteArray { - "Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum" + "Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum 1" } fn REASON_2() -> ByteArray { - "Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum" + "Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum, Lorem impsum 2" } fn GOAL() -> u256 { 1000 @@ -146,15 +146,36 @@ fn test_set_name_not_admin_or_owner() { } #[test] -fn test_set_reason() { +fn test_set_reason_owner() { let contract_address = _setup_(); let dispatcher = IFundDispatcher { contract_address }; let reason = dispatcher.get_reason(); assert(reason == REASON_1(), 'Invalid reason'); + start_cheat_caller_address_global(OWNER()); dispatcher.set_reason(REASON_2()); let new_reason = dispatcher.get_reason(); - assert(new_reason == REASON_2(), 'Set reason method not working') + assert(new_reason == REASON_2(), 'Not allowed to change reason'); +} + +#[test] +fn test_set_reason_admins() { + let contract_address = _setup_(); + let dispatcher = IFundDispatcher { contract_address }; + let reason = dispatcher.get_reason(); + assert(reason == REASON_1(), 'Invalid reason'); + + // test with ADMIN_1 + start_cheat_caller_address_global(VALID_ADDRESS_1()); + dispatcher.set_reason(REASON_1()); + let new_reason = dispatcher.get_reason(); + assert(new_reason == REASON_1(), 'Not allowed to change reason'); + + // test with ADMIN_2 + start_cheat_caller_address_global(VALID_ADDRESS_2()); + dispatcher.set_reason(REASON_2()); + let new_reason = dispatcher.get_reason(); + assert(new_reason == REASON_2(), 'Not allowed to change reason') } #[test] @@ -227,6 +248,15 @@ fn test_new_vote_received_event_emitted_successful() { ); } +#[test] +#[should_panic(expected: ("You must be an owner or admin to perform this action",))] +fn test_set_reason_unauthorized() { + let contract_address = _setup_(); + let dispatcher = IFundDispatcher { contract_address }; + // Change the reason without being authrorized + dispatcher.set_reason("not stored reason"); +} + #[test] #[should_panic(expected: ("Only Admins can set goal",))] fn test_set_goal_unauthorized() {