Skip to content

Commit

Permalink
Merge pull request #260 from jsanchez556/feat/add_asserts_to_set_name
Browse files Browse the repository at this point in the history
[feat] Add asserts to set_name
  • Loading branch information
EmmanuelAR authored Nov 23, 2024
2 parents 9eb43f3 + 3cd73d6 commit 0bac2a6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
10 changes: 9 additions & 1 deletion contracts/src/fund.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
38 changes: 36 additions & 2 deletions contracts/tests/test_fund.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ fn CONTACT_HANDLE_2() -> ByteArray {
fn VALID_ADDRESS_1() -> ContractAddress {
contract_address_const::<FundManagerConstants::VALID_ADDRESS_1>()
}
fn VALID_ADDRESS_2() -> ContractAddress {
contract_address_const::<FundManagerConstants::VALID_ADDRESS_2>()
}
fn _setup_() -> ContractAddress {
let contract = declare("Fund").unwrap();
let mut calldata: Array<felt252> = array![];
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 0bac2a6

Please sign in to comment.