Skip to content

Commit

Permalink
test: admin tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eddnewgate committed Aug 24, 2024
1 parent 70e988a commit ce8996d
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 11 deletions.
3 changes: 3 additions & 0 deletions src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pub use interface::IPushComm;

#[starknet::contract]
pub mod PushComm {
use core::traits::TryInto;
use core::serde::Serde;
use core::box::BoxTrait;
use core::clone::Clone;
use core::num::traits::zero::Zero;
Expand Down Expand Up @@ -312,6 +314,7 @@ pub mod PushComm {
ChannelAlias {
chain_name: self.chain_name.read(),
chain_id: self.chain_id.read(),
// chain_id: 1, //self.chain_id.read(),
channel_owner_address: get_caller_address(),
ethereum_channel_address: channel_address
}
Expand Down
45 changes: 38 additions & 7 deletions tests/test_channel_alias.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,29 @@ use snforge_std::{
use push_comm::{PushComm, interface::IPushCommDispatcher, interface::IPushCommDispatcherTrait};
use super::common::{USER_1, deploy_contract, CHAIN_NAME};

// #[test]
// fn test_verify_channel_alias() {
// let contract_address = deploy_contract();
// let push_comm = IPushCommDispatcher { contract_address };
// let channel_address: EthAddress = 'some address'.try_into().unwrap();

// let mut spy = spy_events();

// // user sets the alias
// cheat_caller_address(contract_address, USER_1(), CheatSpan::TargetCalls(1));
// push_comm.verify_channel_alias(channel_address);

// // assert on the vent
// let events = spy.get_events();
// assert(events.events.len() == 1, 'There should be one event');

// let (from_addrs, event) = events.events.at(0);
// assert(from_addrs == @contract_address, 'Emitted from wrong address');
// assert(event.keys.at(0) == @selector!("ChannelAlias"), 'Wrong event was emitted');
// }

#[test]
#[ignore]
fn test_verify_channel_alias() {
let contract_address = deploy_contract();
let push_comm = IPushCommDispatcher { contract_address };
Expand All @@ -19,11 +41,20 @@ fn test_verify_channel_alias() {
cheat_caller_address(contract_address, USER_1(), CheatSpan::TargetCalls(1));
push_comm.verify_channel_alias(channel_address);

// assert on the vent
let events = spy.get_events();
assert(events.events.len() == 1, 'There should be one event');

let (from_addrs, event) = events.events.at(0);
assert(from_addrs == @contract_address, 'Emitted from wrong address');
assert(event.keys.at(0) == @selector!("ChannelAlias"), 'Wrong event was emitted');
spy
.assert_emitted(
@array![ // Ad. 2
(
contract_address,
PushComm::Event::ChannelAlias(
PushComm::ChannelAlias {
chain_name: CHAIN_NAME(),
chain_id: 1,
channel_owner_address: USER_1(),
ethereum_channel_address: channel_address
}
)
)
]
);
}
47 changes: 43 additions & 4 deletions tests/test_push_admin.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use starknet::{ContractAddress, EthAddress};

use snforge_std::{declare, ContractClassTrait, cheat_caller_address, CheatSpan, spy_events};
use push_comm::interface::{IPushCommDispatcher, IPushCommDispatcherTrait};
use super::common::{PUSH_ADMIN, deploy_contract};
use super::common::{USER_1, PUSH_ADMIN, deploy_contract};


#[test]
fn test_core_contract_address() {
fn test_admin_sets_core_contract_address() {
let contract_address = deploy_contract();
let push_comm = IPushCommDispatcher { contract_address };
let CORE_ADDRESS: EthAddress = 'some addrs'.try_into().unwrap();
Expand All @@ -19,9 +19,21 @@ fn test_core_contract_address() {
assert(CORE_ADDRESS == UPDATED_ADDRESS, 'Core Contract Update Failed');
}

#[test]
#[should_panic(expected: 'Caller is not the owner')]
fn test_non_admin_sets_core_contract_fail() {
let contract_address = deploy_contract();
let push_comm = IPushCommDispatcher { contract_address };
let CORE_ADDRESS: EthAddress = 'some addrs'.try_into().unwrap();

// non admin user sets the core channel address
cheat_caller_address(contract_address, USER_1(), CheatSpan::TargetCalls(1));
push_comm.set_push_core_address(CORE_ADDRESS);
}


#[test]
fn test_gov_address() {
fn test_admin_set_gov_address() {
let contract_address = deploy_contract();
let push_comm = IPushCommDispatcher { contract_address };

Expand All @@ -36,7 +48,20 @@ fn test_gov_address() {
}

#[test]
fn test_push_token_address() {
#[should_panic(expected: 'Caller is not the owner')]
fn test_non_admin_set_gov_address() {
let contract_address = deploy_contract();
let push_comm = IPushCommDispatcher { contract_address };

let GOV_ADDRESS: ContractAddress = 'some addrs'.try_into().unwrap();

// admin sets the migration status
cheat_caller_address(contract_address, USER_1(), CheatSpan::TargetCalls(1));
push_comm.set_push_governance_address(GOV_ADDRESS);
}

#[test]
fn test_admin_set_push_token_address() {
let contract_address = deploy_contract();
let push_comm = IPushCommDispatcher { contract_address };

Expand All @@ -50,3 +75,17 @@ fn test_push_token_address() {
assert(TOKEN_ADDRESS == UPDATED_ADDRESS, 'Core Contract Update Failed');
}


#[test]
#[should_panic(expected: 'Caller is not the owner')]
fn test_non_admin_set_push_token_address() {
let contract_address = deploy_contract();
let push_comm = IPushCommDispatcher { contract_address };

let TOKEN_ADDRESS: ContractAddress = 'user_1'.try_into().unwrap();

// admin sets the migration status
cheat_caller_address(contract_address, USER_1(), CheatSpan::TargetCalls(1));
push_comm.set_push_governance_address(TOKEN_ADDRESS);
}

0 comments on commit ce8996d

Please sign in to comment.