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

implement register and is_registered tests #98

Merged
merged 5 commits into from
Oct 2, 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
128 changes: 109 additions & 19 deletions contracts/src/components/registry/registry_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -29,50 +29,140 @@ fn test_register() {
let mut state = setup();
let mut spy = spy_events();
let contract_address = test_address();
let caller = constants::CALLER();
let offchain_id = constants::REVOLUT_ID();

// setup caller
start_cheat_caller_address(contract_address, constants::CALLER());

// register
state.register(offchain_id: constants::REVOLUT_ID());
state.register(:offchain_id);

// assert state after
assert!(state.is_registered(constants::CALLER(), constants::REVOLUT_ID()));
assert!(state.is_registered(contract_address: caller, :offchain_id));

// check on emitted events
spy
.assert_emitted(
@array![
(
contract_address,
Event::RegistrationEvent(
RegistrationEvent { caller: constants::CALLER(), offchain_id: constants::REVOLUT_ID() }
)
)
]
)
@array![(contract_address, Event::RegistrationEvent(RegistrationEvent { caller, offchain_id }))]
);
}

// #[test]
#[test]
fn test_register_twice_same_offchain_id() {
panic!("Not implemented yet");
let mut state = setup();
let mut spy = spy_events();
let contract_address = test_address();
let caller = constants::CALLER();
let offchain_id = constants::REVOLUT_ID();

// setup caller
start_cheat_caller_address(contract_address, caller);

// double registeration
state.register(:offchain_id);
state.register(:offchain_id);

// assert state after
assert!(state.is_registered(contract_address: caller, :offchain_id));

// check on emitted events
spy
.assert_emitted(
@array![
(contract_address, Event::RegistrationEvent(RegistrationEvent { caller, offchain_id })),
(contract_address, Event::RegistrationEvent(RegistrationEvent { caller, offchain_id }))
]
);
}

// #[test]
#[test]
fn test_register_two_different_offchain_id() {
panic!("Not implemented yet");
let mut state = setup();
let mut spy = spy_events();
let contract_address = test_address();
let caller = constants::CALLER();
let offchain_id1 = constants::REVOLUT_ID();
let offchain_id2 = constants::REVOLUT_ID2();

// setup caller
start_cheat_caller_address(contract_address, caller);

// registerations
state.register(offchain_id: offchain_id1);
state.register(offchain_id: offchain_id2);

// assert state after
assert!(state.is_registered(contract_address: caller, offchain_id: offchain_id1));
assert!(state.is_registered(contract_address: caller, offchain_id: offchain_id2));

// check on emitted events
spy
.assert_emitted(
@array![
(contract_address, Event::RegistrationEvent(RegistrationEvent { caller, offchain_id: offchain_id1 })),
(contract_address, Event::RegistrationEvent(RegistrationEvent { caller, offchain_id: offchain_id2 }))
]
);
}

// #[test]
#[test]
fn test_register_same_offchain_id_from_two_different_callers() {
panic!("Not implemented yet");
let mut state = setup();
let mut spy = spy_events();
let contract_address = test_address();
let caller1 = constants::CALLER();
let caller2 = constants::OTHER();
let offchain_id = constants::REVOLUT_ID();

// setup caller one and register
start_cheat_caller_address(contract_address, caller1);
state.register(:offchain_id);

// setup caller two and register
start_cheat_caller_address(contract_address, caller2);
state.register(:offchain_id);

// assert state after
assert!(state.is_registered(contract_address: caller1, :offchain_id));
assert!(state.is_registered(contract_address: caller2, :offchain_id));

// check on emitted events
spy
.assert_emitted(
@array![
(contract_address, Event::RegistrationEvent(RegistrationEvent { caller: caller1, offchain_id })),
(contract_address, Event::RegistrationEvent(RegistrationEvent { caller: caller2, offchain_id }))
]
);
}

//
// is_registered
//

// #[test]
#[test]
fn test_is_registered() {
panic!("Not implemented yet");
let mut state = setup();
let contract_address = test_address();
let caller = constants::CALLER();
let offchain_id1 = constants::REVOLUT_ID();
let offchain_id2 = constants::REVOLUT_ID2();

assert!(!state.is_registered(contract_address: caller, offchain_id: offchain_id1));
assert!(!state.is_registered(contract_address: caller, offchain_id: offchain_id2));

// register
start_cheat_caller_address(contract_address, constants::CALLER());
state.register(offchain_id: offchain_id1);

assert!(state.is_registered(contract_address: caller, offchain_id: offchain_id1));
assert!(!state.is_registered(contract_address: caller, offchain_id: offchain_id2));

// register another offchain ID
start_cheat_caller_address(contract_address, constants::CALLER());
state.register(offchain_id: offchain_id2);

assert!(state.is_registered(contract_address: caller, offchain_id: offchain_id1));
assert!(state.is_registered(contract_address: caller, offchain_id: offchain_id2));
}
1 change: 1 addition & 0 deletions contracts/src/tests/constants.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use core::starknet::{ContractAddress, contract_address_const};
use zkramp::components::registry::interface::OffchainId;

const REVTAG: felt252 = 'just a random revtag hash';
const REVTAG_TWO: felt252 = 'just another random revtag hash';

const REVTAG2: felt252 = 'just a 2nd random revtag hash';

Expand Down
Loading