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
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 7 additions & 1 deletion contracts/src/fund.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,13 @@ 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!(valid_address_1 == caller || valid_address_2 == caller, "You are not the admin");
self.name.write(name);
}
fn get_name(self: @ContractState) -> ByteArray {
Expand Down
20 changes: 20 additions & 0 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 @@ -105,6 +108,23 @@ fn test_set_name() {
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_1");
assert(dispatcher.get_name() == "NEW_NAME_1", 'Set name method not working');

start_cheat_caller_address_global(VALID_ADDRESS_2());
dispatcher.set_name("NEW_NAME_2");
assert(dispatcher.get_name() == "NEW_NAME_2", 'Set name method not working');
}

#[test]
#[should_panic(expected: ("You are not the admin",))]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add test when the owner call, keep the assert, i left u a comment
Keep test_set_name_not_admin test is ok

fn test_set_name_not_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(OWNER());
dispatcher.set_name("NEW_NAME");
let new_name = dispatcher.get_name();
Expand Down
Loading