Skip to content

Commit

Permalink
Merge pull request #269 from ncoquelet/feat/267
Browse files Browse the repository at this point in the history
Feat: add tests for the new set_reason behaviour implemented in #253
  • Loading branch information
EmmanuelAR authored Nov 25, 2024
2 parents f8d5de4 + eb3bf88 commit 615c3a6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
18 changes: 9 additions & 9 deletions contracts/src/fund.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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::<FundManagerConstants::VALID_ADDRESS_1>();
let valid_address_2 = contract_address_const::<FundManagerConstants::VALID_ADDRESS_2>();
fn set_reason(ref self: ContractState, reason: 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>();

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();
Expand Down
38 changes: 34 additions & 4 deletions contracts/tests/test_fund.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 615c3a6

Please sign in to comment.