Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Add asserts to set_name #260

Merged
merged 2 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
EmmanuelAR marked this conversation as resolved.
Show resolved Hide resolved
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
Loading