-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
188 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod mock_abstract_module; | ||
pub mod mock_agent_role; | ||
pub mod mock_contract; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#[starknet::contract] | ||
mod MockAgentRole { | ||
use openzeppelin_access::ownable::OwnableComponent; | ||
use roles::agent_role::AgentRoleComponent; | ||
use starknet::ContractAddress; | ||
|
||
component!(path: OwnableComponent, storage: ownable, event: OwnableEvent); | ||
|
||
#[abi(embed_v0)] | ||
impl OwnableImpl = OwnableComponent::OwnableImpl<ContractState>; | ||
impl OwnableInternalImpl = OwnableComponent::InternalImpl<ContractState>; | ||
|
||
component!(path: AgentRoleComponent, storage: agent_role, event: AgentRoleEvent); | ||
|
||
#[abi(embed_v0)] | ||
impl AgentRoleImpl = AgentRoleComponent::AgentRoleImpl<ContractState>; | ||
impl AgentRoleInternalImpl = AgentRoleComponent::InternalImpl<ContractState>; | ||
|
||
#[storage] | ||
struct Storage { | ||
#[substorage(v0)] | ||
ownable: OwnableComponent::Storage, | ||
#[substorage(v0)] | ||
agent_role: AgentRoleComponent::Storage, | ||
} | ||
|
||
#[event] | ||
#[derive(Drop, starknet::Event)] | ||
enum Event { | ||
#[flat] | ||
OwnableEvent: OwnableComponent::Event, | ||
#[flat] | ||
AgentRoleEvent: AgentRoleComponent::Event, | ||
} | ||
|
||
#[constructor] | ||
fn constructor(ref self: ContractState, owner: ContractAddress) { | ||
self.ownable.initializer(owner); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
use roles::agent_role::IAgentRoleDispatcher; | ||
use snforge_std::{ContractClassTrait, DeclareResultTrait, declare}; | ||
|
||
fn setup() -> IAgentRoleDispatcher { | ||
let agent_role_contract = declare("MockAgentRole").unwrap().contract_class(); | ||
let (deployed_address, _) = agent_role_contract | ||
.deploy(@array![starknet::get_contract_address().into()]) | ||
.unwrap(); | ||
IAgentRoleDispatcher { contract_address: deployed_address } | ||
} | ||
|
||
pub mod add_agent { | ||
use core::num::traits::Zero; | ||
use roles::agent_role::{AgentRoleComponent, IAgentRoleDispatcherTrait}; | ||
use snforge_std::{ | ||
EventSpyAssertionsTrait, spy_events, start_cheat_caller_address, stop_cheat_caller_address, | ||
}; | ||
use super::setup; | ||
|
||
#[test] | ||
#[should_panic(expected: 'Caller is not the owner')] | ||
fn test_should_reverts_when_sender_is_not_the_owner() { | ||
let agent_role = setup(); | ||
start_cheat_caller_address( | ||
agent_role.contract_address, starknet::contract_address_const::<'NOT_OWNER'>(), | ||
); | ||
agent_role.add_agent(starknet::contract_address_const::<'AGENT'>()); | ||
stop_cheat_caller_address(agent_role.contract_address); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected: 'Agent address zero!')] | ||
fn test_should_reverts_when_address_to_add_is_zero_address() { | ||
let agent_role = setup(); | ||
|
||
agent_role.add_agent(Zero::zero()); | ||
} | ||
|
||
#[test] | ||
fn test_should_add_the_agent() { | ||
let agent_role = setup(); | ||
let agent = starknet::contract_address_const::<'AGENT'>(); | ||
|
||
let mut spy = spy_events(); | ||
agent_role.add_agent(agent); | ||
|
||
assert(agent_role.is_agent(agent), 'Agent not registered'); | ||
spy | ||
.assert_emitted( | ||
@array![ | ||
( | ||
agent_role.contract_address, | ||
AgentRoleComponent::Event::AgentAdded( | ||
AgentRoleComponent::AgentAdded { agent }, | ||
), | ||
), | ||
], | ||
); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected: 'Agent already registered')] | ||
fn test_should_reverts_when_address_to_add_is_already_an_agent() { | ||
let agent_role = setup(); | ||
let agent = starknet::contract_address_const::<'AGENT'>(); | ||
|
||
agent_role.add_agent(agent); | ||
/// Registering twice should panic | ||
agent_role.add_agent(agent); | ||
} | ||
} | ||
|
||
pub mod remove_agent { | ||
use core::num::traits::Zero; | ||
use roles::agent_role::{AgentRoleComponent, IAgentRoleDispatcherTrait}; | ||
use snforge_std::{ | ||
EventSpyAssertionsTrait, spy_events, start_cheat_caller_address, stop_cheat_caller_address, | ||
}; | ||
use super::setup; | ||
|
||
#[test] | ||
#[should_panic(expected: 'Caller is not the owner')] | ||
fn test_should_reverts_when_sender_is_not_the_owner() { | ||
let agent_role = setup(); | ||
start_cheat_caller_address( | ||
agent_role.contract_address, starknet::contract_address_const::<'NOT_OWNER'>(), | ||
); | ||
agent_role.remove_agent(starknet::contract_address_const::<'AGENT'>()); | ||
stop_cheat_caller_address(agent_role.contract_address); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected: 'Agent address zero!')] | ||
fn test_should_reverts_when_address_to_remove_is_zero_address() { | ||
let agent_role = setup(); | ||
|
||
agent_role.remove_agent(Zero::zero()); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected: 'Agent not registered')] | ||
fn test_should_reverts_when_address_to_remove_is_not_an_agent() { | ||
let agent_role = setup(); | ||
|
||
agent_role.remove_agent(starknet::contract_address_const::<'AGENT'>()); | ||
} | ||
|
||
#[test] | ||
fn test_should_remove_the_agent_when_address_to_remove_is_an_agent_address() { | ||
let agent_role = setup(); | ||
let agent = starknet::contract_address_const::<'AGENT'>(); | ||
agent_role.add_agent(agent); | ||
|
||
let mut spy = spy_events(); | ||
agent_role.remove_agent(agent); | ||
|
||
assert(!agent_role.is_agent(agent), 'Agent not removed'); | ||
spy | ||
.assert_emitted( | ||
@array![ | ||
( | ||
agent_role.contract_address, | ||
AgentRoleComponent::Event::AgentRemoved( | ||
AgentRoleComponent::AgentRemoved { agent }, | ||
), | ||
), | ||
], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod agent_role_test; |