Skip to content

Commit

Permalink
test: channel delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
eddnewgate committed Aug 24, 2024
1 parent a9fdd2c commit df1f678
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions tests/test_channel_delegate.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use starknet::{ContractAddress, EthAddress};

use snforge_std::{
declare, ContractClassTrait, cheat_caller_address, CheatSpan, spy_events,
EventSpyAssertionsTrait, Event, EventSpyTrait
};
use push_comm::{PushComm, interface::IPushCommDispatcher, interface::IPushCommDispatcherTrait};
use super::common::{USER_1, deploy_contract, CHAIN_NAME};


#[test]
fn test_channel_delegate() {
let contract_address = deploy_contract();
let push_comm = IPushCommDispatcher { contract_address };
let CHANNEL_ADDRESS: ContractAddress = 'some addrs'.try_into().unwrap();
let indentity: ByteArray = "identity";
let mut spy = spy_events();

// Channel owner set the delegate
cheat_caller_address(contract_address, CHANNEL_ADDRESS, CheatSpan::TargetCalls(1));
push_comm.add_delegate(USER_1());

// Assert AddDelegate event was emitted
spy
.assert_emitted(
@array![
(
contract_address,
PushComm::Event::AddDelegate(
PushComm::AddDelegate { channel: CHANNEL_ADDRESS, delegate: USER_1() }
)
)
]
);

// Delegate can send the notification
cheat_caller_address(contract_address, USER_1(), CheatSpan::TargetCalls(1));
let is_success = push_comm.send_notification(CHANNEL_ADDRESS, USER_1(), indentity.clone());
assert(is_success, 'Send notification failed');

// Channel owner remove the delegate
cheat_caller_address(contract_address, CHANNEL_ADDRESS, CheatSpan::TargetCalls(1));
push_comm.remove_delegate(USER_1());

// Assert RemoveDelegate event was emitted
spy
.assert_emitted(
@array![
(
contract_address,
PushComm::Event::RemoveDelegate(
PushComm::RemoveDelegate { channel: CHANNEL_ADDRESS, delegate: USER_1() }
)
)
]
);


// Removed Delegate can send the notification
cheat_caller_address(contract_address, USER_1(), CheatSpan::TargetCalls(1));
let is_success = push_comm.send_notification(CHANNEL_ADDRESS, USER_1(), indentity.clone());
assert(is_success == false, 'Send notification should fail');
}

0 comments on commit df1f678

Please sign in to comment.