Skip to content

Commit

Permalink
chore:community gatekeep test
Browse files Browse the repository at this point in the history
  • Loading branch information
mubarak23 committed Oct 6, 2024
1 parent 0dda59d commit 986dc54
Show file tree
Hide file tree
Showing 2 changed files with 212 additions and 31 deletions.
62 changes: 32 additions & 30 deletions src/community/community.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -77,58 +77,58 @@ pub mod CommunityComponent {

#[derive(Drop, starknet::Event)]
pub struct CommunityCreated {
community_id: u256,
community_owner: ContractAddress,
community_nft_address: ContractAddress,
block_timestamp: u64,
pub community_id: u256,
pub community_owner: ContractAddress,
pub community_nft_address: ContractAddress,
pub block_timestamp: u64,
}

#[derive(Drop, starknet::Event)]
pub struct CommunityModAdded {
community_id: u256,
transaction_executor: ContractAddress,
mod_address: ContractAddress,
block_timestamp: u64,
pub community_id: u256,
pub transaction_executor: ContractAddress,
pub mod_address: ContractAddress,
pub block_timestamp: u64,
}

#[derive(Drop, starknet::Event)]
pub struct CommunityModRemoved {
community_id: u256,
transaction_executor: ContractAddress,
mod_address: ContractAddress,
block_timestamp: u64,
pub community_id: u256,
pub transaction_executor: ContractAddress,
pub mod_address: ContractAddress,
pub block_timestamp: u64,
}

#[derive(Drop, starknet::Event)]
pub struct CommunityBanStatusUpdated {
community_id: u256,
transaction_executor: ContractAddress,
profile: ContractAddress,
block_timestamp: u64,
pub community_id: u256,
pub transaction_executor: ContractAddress,
pub profile: ContractAddress,
pub block_timestamp: u64,
}

#[derive(Drop, starknet::Event)]
pub struct CommunityUpgraded {
community_id: u256,
transaction_executor: ContractAddress,
premiumType: CommunityType,
block_timestamp: u64,
pub community_id: u256,
pub transaction_executor: ContractAddress,
pub premiumType: CommunityType,
pub block_timestamp: u64,
}

#[derive(Drop, starknet::Event)]
pub struct CommunityGatekeeped {
community_id: u256,
transaction_executor: ContractAddress,
gatekeepType: GateKeepType,
block_timestamp: u64,
pub community_id: u256,
pub transaction_executor: ContractAddress,
pub gatekeepType: GateKeepType,
pub block_timestamp: u64,
}

#[derive(Drop, starknet::Event)]
pub struct DeployedCommunityNFT {
community_id: u256,
profile_address: ContractAddress,
community_nft: ContractAddress,
block_timestamp: u64,
pub community_id: u256,
pub profile_address: ContractAddress,
pub community_nft: ContractAddress,
pub block_timestamp: u64,
}

// *************************************************************************
Expand Down Expand Up @@ -383,7 +383,9 @@ pub mod CommunityComponent {
let community = self.communities.read(community_id);
// only owner can upgrade community
assert(community.community_owner == caller, NOT_COMMUNITY_OWNER);
let updated_community = CommunityDetails { community_type: upgrade_type, ..community };
let updated_community = CommunityDetails {
community_type: upgrade_type, community_premium_status: true, ..community
};
self.communities.write(community_id, updated_community);
let community_event = self.communities.read(community_id);
self
Expand Down Expand Up @@ -414,7 +416,7 @@ pub mod CommunityComponent {
let mut community_gate_keep_details = CommunityGateKeepDetails {
community_id: community_id,
gate_keep_type: gate_keep_type.clone(),
community_nft_address: community_details.community_nft_address,
community_nft_address: nft_contract_address,
entry_fee: 0
};

Expand Down
181 changes: 180 additions & 1 deletion tests/test_community.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use karst::mocks::registry::Registry;
use karst::interfaces::IRegistry::{IRegistryDispatcher, IRegistryDispatcherTrait};
use karst::karstnft::karstnft::KarstNFT;
use karst::presets::community::KarstCommunity;

use karst::community::community::CommunityComponent;
use karst::base::constants::types::{
CommunityDetails, GateKeepType, CommunityType, CommunityMember, CommunityGateKeepDetails
};
Expand All @@ -32,6 +32,7 @@ const USER_THREE: felt252 = 'ROB';
const USER_FOUR: felt252 = 'DAN';
const USER_FIVE: felt252 = 'RANDY';
const USER_SIX: felt252 = 'JOE';
const NFT_ONE: felt252 = 'JOE_NFT';

// *************************************************************************
// SETUP
Expand Down Expand Up @@ -456,3 +457,181 @@ fn test_should_panic_set_ban_status() {
communityDispatcher.set_ban_status(community_id, USER_TWO.try_into().unwrap(), true);
}

#[test]
fn test_community_upgrade() {
let (community_contract_address, _, _, _) = __setup__();
let communityDispatcher = ICommunityDispatcher { contract_address: community_contract_address };

start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap());
let community_id = communityDispatcher.create_comminuty();
communityDispatcher.upgrade_community(community_id, CommunityType::Standard);
let community = communityDispatcher.get_community(community_id);
assert(community.community_type == CommunityType::Standard, 'Community Upgrade failed');
stop_cheat_caller_address(community_contract_address);
}

#[test]
#[should_panic(expected: ('Karst: Not Community owner',))]
fn test_should_panic_community_upgrade_by_wrong_owner() {
let (community_contract_address, _, _, _) = __setup__();
let communityDispatcher = ICommunityDispatcher { contract_address: community_contract_address };

start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap());
let community_id = communityDispatcher.create_comminuty();

stop_cheat_caller_address(community_contract_address);

start_cheat_caller_address(community_contract_address, USER_TWO.try_into().unwrap());
communityDispatcher.upgrade_community(community_id, CommunityType::Standard);
let community = communityDispatcher.get_community(community_id);
assert(community.community_type == CommunityType::Standard, 'Community Upgrade failed');
}

#[test]
fn test_community_upgrade_emits_event() {
let (community_contract_address, _, _, _) = __setup__();
let communityDispatcher = ICommunityDispatcher { contract_address: community_contract_address };

// spy on emitted events
let mut spy = spy_events();

start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap());
let community_id = communityDispatcher.create_comminuty();
communityDispatcher.upgrade_community(community_id, CommunityType::Standard);

// check events are emitted
spy
.assert_emitted(
@array![
(
community_contract_address,
CommunityComponent::Event::CommunityUpgraded(
CommunityComponent::CommunityUpgraded {
community_id: community_id,
transaction_executor: USER_ONE.try_into().unwrap(),
premiumType: CommunityType::Standard,
block_timestamp: get_block_timestamp()
}
)
)
]
);
}

#[test]
fn test_community_gatekeep_permission() {
let (community_contract_address, _, _, _) = __setup__();
let communityDispatcher = ICommunityDispatcher { contract_address: community_contract_address };

let mut permission_addresses = ArrayTrait::new();
permission_addresses.append(USER_SIX.try_into().unwrap());
permission_addresses.append(USER_FIVE.try_into().unwrap());
permission_addresses.append(USER_THREE.try_into().unwrap());

start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap());
let community_id = communityDispatcher.create_comminuty();
communityDispatcher
.gatekeep(
community_id,
GateKeepType::PermissionedGating,
NFT_ONE.try_into().unwrap(),
permission_addresses,
0
);

// check is_gatekeeped
let (is_gatekeeped, _) = communityDispatcher.is_gatekeeped(community_id);
assert(is_gatekeeped == true, 'Community gatekeep failed');
stop_cheat_caller_address(community_contract_address);
}

#[test]
fn test_community_gatekeep_paid() {
let (community_contract_address, _, _, _) = __setup__();
let communityDispatcher = ICommunityDispatcher { contract_address: community_contract_address };

let mut permission_addresses = ArrayTrait::new();
permission_addresses.append(USER_SIX.try_into().unwrap());
permission_addresses.append(USER_FIVE.try_into().unwrap());
permission_addresses.append(USER_THREE.try_into().unwrap());

start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap());
let community_id = communityDispatcher.create_comminuty();
communityDispatcher
.gatekeep(
community_id, GateKeepType::Paid, NFT_ONE.try_into().unwrap(), permission_addresses, 450
);

// check is_gatekeeped
let (is_gatekeeped, _) = communityDispatcher.is_gatekeeped(community_id);
assert(is_gatekeeped == true, 'Community gatekeep failed');
stop_cheat_caller_address(community_contract_address);
}

#[test]
#[should_panic(expected: ('Karst: Not Community owner',))]
fn test_should_panic_community_gatekeep() {
let (community_contract_address, _, _, _) = __setup__();
let communityDispatcher = ICommunityDispatcher { contract_address: community_contract_address };

let mut permission_addresses = ArrayTrait::new();
permission_addresses.append(USER_SIX.try_into().unwrap());
permission_addresses.append(USER_FIVE.try_into().unwrap());
permission_addresses.append(USER_THREE.try_into().unwrap());

start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap());
let community_id = communityDispatcher.create_comminuty();

stop_cheat_caller_address(community_contract_address);

// Wrong owner trying to gate keep
start_cheat_caller_address(community_contract_address, USER_TWO.try_into().unwrap());
communityDispatcher
.gatekeep(
community_id, GateKeepType::Paid, NFT_ONE.try_into().unwrap(), permission_addresses, 450
);
}

#[test]
fn test_community_gatekeep_emits_event() {
let (community_contract_address, _, _, _) = __setup__();
let communityDispatcher = ICommunityDispatcher { contract_address: community_contract_address };

let mut permission_addresses = ArrayTrait::new();
permission_addresses.append(USER_SIX.try_into().unwrap());
permission_addresses.append(USER_FIVE.try_into().unwrap());
permission_addresses.append(USER_THREE.try_into().unwrap());

// spy on emitted events
let mut spy = spy_events();

start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap());
let community_id = communityDispatcher.create_comminuty();
communityDispatcher
.gatekeep(
community_id,
GateKeepType::PermissionedGating,
NFT_ONE.try_into().unwrap(),
permission_addresses,
0
);

// check events are emitted
spy
.assert_emitted(
@array![
(
community_contract_address,
CommunityComponent::Event::CommunityGatekeeped(
CommunityComponent::CommunityGatekeeped {
community_id: community_id,
transaction_executor: USER_ONE.try_into().unwrap(),
gatekeepType: GateKeepType::PermissionedGating,
block_timestamp: get_block_timestamp()
}
)
)
]
);
}

0 comments on commit 986dc54

Please sign in to comment.