Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
TAdev0 committed Feb 26, 2024
1 parent 5b93ea5 commit 1a38c1e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/config/component.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
/// Errors.
mod errors {
const INVALID_CALLER: felt252 = 'Config: not owner or operator';
const ALREADY_REGISTERED: felt252 = 'Config: already operator';
const NOT_OPERATOR: felt252 = 'Config: not an operator';
}

/// Configuration component.
Expand Down Expand Up @@ -39,9 +41,16 @@ mod config_cpt {
> of IConfig<ComponentState<TContractState>> {
fn register_operator(ref self: ComponentState<TContractState>, address: ContractAddress) {
get_dep_component!(@self, Ownable).assert_only_owner();
assert(!self.operators.read(address), errors::ALREADY_REGISTERED);
self.operators.write(address, true);
}

fn unregister_operator(ref self: ComponentState<TContractState>, address: ContractAddress) {
get_dep_component!(@self, Ownable).assert_only_owner();
assert(self.operators.read(address), errors::NOT_OPERATOR);
self.operators.write(address, false);
}

fn is_operator(self: @ComponentState<TContractState>, address: ContractAddress) -> bool {
self.operators.read(address)
}
Expand Down Expand Up @@ -92,7 +101,7 @@ mod config_cpt {
ref self: ComponentState<TContractState>, address: ContractAddress
) -> bool {
let owner = get_dep_component!(@self, Ownable).owner();
address == owner || self.operators.read(address)
address == owner || self.is_operator(address)
}
}
}
2 changes: 2 additions & 0 deletions src/config/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ trait IConfig<T> {
/// * `address` - The operator account address.
fn register_operator(ref self: T, address: ContractAddress);

fn unregister_operator(ref self: T, address: ContractAddress);

/// Verifies if the given address is an operator.
/// # Arguments
///
Expand Down
35 changes: 33 additions & 2 deletions src/config/tests/test_config.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn deploy_mock() -> IConfigDispatcher {
}

#[test]
fn config_set_operator_ok() {
fn config_register_operator_ok() {
let mock = deploy_mock();
assert(!mock.is_operator(c::OPERATOR()), 'expect not operator');

Expand All @@ -26,7 +26,7 @@ fn config_set_operator_ok() {
}

#[test]
fn config_set_multiple_operators_ok() {
fn config_register_multiple_operators_ok() {
let mock = deploy_mock();
assert(!mock.is_operator(c::OPERATOR()), 'expect not operator');
assert(!mock.is_operator(c::OTHER()), 'expect not operator');
Expand All @@ -40,6 +40,37 @@ fn config_set_multiple_operators_ok() {
assert(mock.is_operator(c::OTHER()), 'expect operator');
}

#[test]
fn config_unregister_operator_ok() {
let mock = deploy_mock();

snf::start_prank(CheatTarget::One(mock.contract_address), c::OWNER());

mock.register_operator(c::OPERATOR());
assert(mock.is_operator(c::OPERATOR()), 'expect operator');

mock.unregister_operator(c::OPERATOR());
assert(!mock.is_operator(c::OPERATOR()), 'expect not operator');
}

#[test]
fn config_unregister_multiple_operators_ok() {
let mock = deploy_mock();

snf::start_prank(CheatTarget::One(mock.contract_address), c::OWNER());

mock.register_operator(c::OPERATOR());
mock.register_operator(c::OTHER());

assert(mock.is_operator(c::OPERATOR()), 'expect operator');
assert(mock.is_operator(c::OTHER()), 'expect operator');

mock.unregister_operator(c::OPERATOR());
mock.unregister_operator(c::OTHER());
assert(!mock.is_operator(c::OPERATOR()), 'expect not operator');
assert(!mock.is_operator(c::OTHER()), 'expect not operator');
}

#[test]
#[should_panic(expected: ('Caller is not the owner',))]
fn config_set_operator_unauthorized() {
Expand Down

0 comments on commit 1a38c1e

Please sign in to comment.