Skip to content

Commit

Permalink
Merge pull request #12 from push-protocol/test/test-cases
Browse files Browse the repository at this point in the history
Increased test coverage
  • Loading branch information
0xNilesh authored Sep 10, 2024
2 parents dd32e59 + 4e59f28 commit 75b04da
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 26 deletions.
Binary file modified .DS_Store
Binary file not shown.
21 changes: 15 additions & 6 deletions src/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ use starknet::{ContractAddress, EthAddress};
pub trait IPushComm<TContractState> {
// Push Admin
fn set_push_core_address(ref self: TContractState, core_address: EthAddress);
fn get_push_core_address(self: @TContractState) -> EthAddress;
fn get_push_governance_address(self: @TContractState) -> ContractAddress;
fn set_push_governance_address(ref self: TContractState, governance_address: ContractAddress);
fn get_push_token_address(self: @TContractState) -> ContractAddress;
fn set_push_token_address(ref self: TContractState, push_token_address: ContractAddress);

// Channel
fn verify_channel_alias(ref self: TContractState, channel_address: EthAddress);
fn add_delegate(ref self: TContractState, delegate: ContractAddress);
Expand All @@ -25,13 +23,24 @@ pub trait IPushComm<TContractState> {
notif_id: u256,
notif_settings: ByteArray
);

// User
fn is_user_subscribed(
self: @TContractState, channel: ContractAddress, user: ContractAddress
) -> bool;
fn subscribe(ref self: TContractState, channel: ContractAddress);
fn unsubscribe(ref self: TContractState, channel: ContractAddress);
fn batch_subscribe(ref self: TContractState, channels: Array<ContractAddress>);
fn batch_unsubscribe(ref self: TContractState, channels: Array<ContractAddress>);

// New Getter Functions
fn is_user_subscribed(
self: @TContractState, channel: ContractAddress, user: ContractAddress
) -> bool;
fn users_count(self: @TContractState) -> u256;
fn chain_id(self: @TContractState) -> felt252;
fn chain_name(self: @TContractState) -> felt252;
fn push_core_address(self: @TContractState) -> EthAddress;
fn push_token_address(self: @TContractState) -> ContractAddress;
fn push_governance_address(self: @TContractState) -> ContractAddress;
fn user_to_channel_notifs(self: @TContractState, user: ContractAddress, channel: ContractAddress) -> ByteArray;
fn map_address_users(self: @TContractState, index: u256) -> ContractAddress;
fn delegated_notification_senders(self: @TContractState, channel: ContractAddress, delegate: ContractAddress) -> bool;
}
53 changes: 36 additions & 17 deletions src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,6 @@ pub mod PushComm {
#[abi(embed_v0)]
impl PushComm of super::IPushComm<ContractState> {
// User
fn is_user_subscribed(
self: @ContractState, channel: ContractAddress, user: ContractAddress
) -> bool {
self._is_user_subscribed(channel, user)
}

fn subscribe(ref self: ContractState, channel: ContractAddress) {
self._subscribe(channel, get_caller_address());
}
Expand Down Expand Up @@ -346,10 +340,6 @@ pub mod PushComm {
self.push_core_address.write(core_address);
}

fn get_push_core_address(self: @ContractState) -> EthAddress {
self.push_core_address.read()
}

// Channel
fn verify_channel_alias(ref self: ContractState, channel_address: EthAddress) {
self
Expand Down Expand Up @@ -384,30 +374,59 @@ pub mod PushComm {
self._send_notification(channel, recipient, identity)
}


// Infos
fn set_push_governance_address(
ref self: ContractState, governance_address: ContractAddress
) {
self.ownable.assert_only_owner();
self.governance.write(governance_address);
}

fn get_push_governance_address(self: @ContractState) -> ContractAddress {
self.governance.read()
}

fn set_push_token_address(ref self: ContractState, push_token_address: ContractAddress) {
self.ownable.assert_only_owner();
self.push_token_address.write(push_token_address);
}

fn get_push_token_address(self: @ContractState) -> ContractAddress {
// Getters Functions
fn push_core_address(self: @ContractState) -> EthAddress {
self.push_core_address.read()
}

fn push_token_address(self: @ContractState) -> ContractAddress {
self.push_token_address.read()
}

fn is_user_subscribed(
self: @ContractState, channel: ContractAddress, user: ContractAddress
) -> bool {
self._is_user_subscribed(channel, user)
}

fn users_count(self: @ContractState) -> u256 {
self.users_count.read()
}

fn chain_id(self: @ContractState) -> felt252 {
self.chain_id.read()
}

fn push_governance_address(self: @ContractState) -> ContractAddress {
self.governance.read()
}

fn chain_name(self: @ContractState) -> felt252 {
self.chain_name.read()
}

fn user_to_channel_notifs(self: @ContractState, user: ContractAddress, channel: ContractAddress) -> ByteArray {
self.user_to_channel_notifs.entry(user).entry(channel).read()
}

fn map_address_users(self: @ContractState, index: u256) -> ContractAddress {
self.map_address_users.entry(index).read()
}

fn delegated_notification_senders(self: @ContractState, channel: ContractAddress, delegate: ContractAddress) -> bool {
self.delegated_notification_senders.entry(channel).entry(delegate).read()
}
}
}
4 changes: 4 additions & 0 deletions tests/test_channel_settings.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ fn test_channel_channel_user_settings() {
let notif_settings: ByteArray = "notif_settings";
push_comm.change_user_channel_settings(CHANNEL_ADDRESS, notif_id, notif_settings.clone());

let modified_notif_settings = format!("@{}+@{}", notif_id, notif_settings);
let setting_saved = push_comm.user_to_channel_notifs(USER_1(), CHANNEL_ADDRESS);
assert(setting_saved == modified_notif_settings, 'Settings saved');

// Assert UserNotifcationSettingsAdded event was emitted
spy
.assert_emitted(
Expand Down
6 changes: 3 additions & 3 deletions tests/test_push_admin.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn test_admin_sets_core_contract_address() {
cheat_caller_address(contract_address, PUSH_ADMIN(), CheatSpan::TargetCalls(1));
push_comm.set_push_core_address(CORE_ADDRESS);

let UPDATED_ADDRESS = push_comm.get_push_core_address();
let UPDATED_ADDRESS = push_comm.push_core_address();
assert(CORE_ADDRESS == UPDATED_ADDRESS, 'Core Contract Update Failed');
}

Expand Down Expand Up @@ -43,7 +43,7 @@ fn test_admin_set_gov_address() {
cheat_caller_address(contract_address, PUSH_ADMIN(), CheatSpan::TargetCalls(1));
push_comm.set_push_governance_address(GOV_ADDRESS);

let UPDATED_ADDRESS = push_comm.get_push_governance_address();
let UPDATED_ADDRESS = push_comm.push_governance_address();
assert(GOV_ADDRESS == UPDATED_ADDRESS, 'Core Contract Update Failed');
}

Expand Down Expand Up @@ -71,7 +71,7 @@ fn test_admin_set_push_token_address() {
cheat_caller_address(contract_address, PUSH_ADMIN(), CheatSpan::TargetCalls(1));
push_comm.set_push_governance_address(TOKEN_ADDRESS);

let UPDATED_ADDRESS = push_comm.get_push_governance_address();
let UPDATED_ADDRESS = push_comm.push_governance_address();
assert(TOKEN_ADDRESS == UPDATED_ADDRESS, 'Core Contract Update Failed');
}

Expand Down
6 changes: 6 additions & 0 deletions tests/test_user_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ fn test_user_subscription_ops() {
let is_user_subscribed = push_comm.is_user_subscribed(CHANNEL_ADDRESS, USER_1());
assert(is_user_subscribed == false, 'Initally user is not subscribed');

let users_count_before_subscription = push_comm.users_count();

// user subscribes to the channel
cheat_caller_address(contract_address, USER_1(), CheatSpan::TargetCalls(1));
push_comm.subscribe(CHANNEL_ADDRESS);

// users_count should increase
let users_count_after_subscription = push_comm.users_count();
assert(users_count_before_subscription + 1 == users_count_after_subscription, 'Users count not increased');

// user should be subscribed to the channel
let is_user_subscribed = push_comm.is_user_subscribed(CHANNEL_ADDRESS, USER_1());
assert(is_user_subscribed, 'User should be subscribed');
Expand Down

0 comments on commit 75b04da

Please sign in to comment.