Skip to content

Commit

Permalink
chore: test for emit join and leave community
Browse files Browse the repository at this point in the history
  • Loading branch information
mubarak23 committed Oct 10, 2024
1 parent c9ed7f3 commit d7f65fd
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 29 deletions.
53 changes: 28 additions & 25 deletions src/community/community.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,20 @@ pub mod CommunityComponent {

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

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

#[derive(Drop, starknet::Event)]
Expand Down Expand Up @@ -213,7 +213,7 @@ pub mod CommunityComponent {
let community = self.communities.read(community_id);

// check user is not already a member and wasn't previously banned
let is_community_member = self.is_community_member(profile, community_id);
let (is_community_member, _) = self.is_community_member(profile, community_id);
let is_banned = self.get_ban_status(profile, community_id);
assert(is_community_member != true, ALREADY_MEMBER);
assert(is_banned != true, BANNED_MEMBER);
Expand Down Expand Up @@ -260,9 +260,17 @@ pub mod CommunityComponent {
let community = self.communities.read(community_id);
let community_member_details = self.community_member.read((community_id, profile));

let is_community_member = self.is_community_member(profile, community_id);
let (is_community_member, _) = self.is_community_member(profile, community_id);
assert(is_community_member == true, NOT_MEMBER);

// burn user's community token
self
._burn_community_nft(
community.community_nft_address,
profile,
community_member_details.community_token_id
);

// remove member details and update storage
let updated_member_details = CommunityMember {
profile_address: contract_address_const::<0>(),
Expand All @@ -278,12 +286,6 @@ pub mod CommunityComponent {
};
self.communities.write(community_id, updated_community);

// burn user's community token
self
._burn_community_nft(
community.community_nft_address, community_member_details.community_token_id
);

// emit event
self
.emit(
Expand Down Expand Up @@ -462,12 +464,12 @@ pub mod CommunityComponent {
/// @return bool true/false stating user's membership status
fn is_community_member(
self: @ComponentState<TContractState>, profile: ContractAddress, community_id: u256
) -> bool {
) -> (bool, CommunityMember) {
let community_memeber = self.community_member.read((community_id, profile));
if (community_memeber.community_id == community_id) {
true
(true, community_memeber)
} else {
false
(false, community_memeber)
}
}

Expand Down Expand Up @@ -657,7 +659,7 @@ pub mod CommunityComponent {

while index < length {
let moderator = *moderators.at(index);
let is_community_member = self.is_community_member(moderator, community_id);
let (is_community_member, _) = self.is_community_member(moderator, community_id);
assert(is_community_member == true, NOT_MEMBER);
self.community_mod.write((community_id, moderator), true);

Expand Down Expand Up @@ -693,7 +695,7 @@ pub mod CommunityComponent {

while index < length {
let moderator = *moderators.at(index);
let is_community_member = self.is_community_member(moderator, community_id);
let (is_community_member, _) = self.is_community_member(moderator, community_id);
assert(is_community_member == true, NOT_MEMBER);

self.community_mod.write((community_id, moderator), false);
Expand Down Expand Up @@ -731,7 +733,7 @@ pub mod CommunityComponent {
let profile = *profiles[index];
let ban_status = *ban_statuses[index];
// check profile is a community member
let is_community_member = self.is_community_member(profile, community_id);
let (is_community_member, _) = self.is_community_member(profile, community_id);
assert(is_community_member == true, NOT_MEMBER);

// update storage
Expand Down Expand Up @@ -802,10 +804,11 @@ pub mod CommunityComponent {
fn _burn_community_nft(
ref self: ComponentState<TContractState>,
community_nft_address: ContractAddress,
profile: ContractAddress,
token_id: u256
) {
ICommunityNftDispatcher { contract_address: community_nft_address }
.burn_nft(get_caller_address(), token_id);
.burn_nft(profile, token_id);
}
}
}
6 changes: 4 additions & 2 deletions src/interfaces/ICommunity.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use starknet::{ContractAddress};
use karst::base::constants::types::{
GateKeepType, CommunityGateKeepDetails, CommunityType, CommunityDetails
GateKeepType, CommunityGateKeepDetails, CommunityType, CommunityDetails, CommunityMember
};

// *************************************************************************
Expand Down Expand Up @@ -41,7 +41,9 @@ pub trait ICommunity<TState> {
// *************************************************************************
fn get_community(self: @TState, community_id: u256) -> CommunityDetails;
fn get_community_metadata_uri(self: @TState, community_id: u256) -> ByteArray;
fn is_community_member(self: @TState, profile: ContractAddress, community_id: u256) -> bool;
fn is_community_member(
self: @TState, profile: ContractAddress, community_id: u256
) -> (bool, CommunityMember);
fn get_total_members(self: @TState, community_id: u256) -> u256;
fn is_community_mod(self: @TState, profile: ContractAddress, community_id: u256) -> bool;
fn get_ban_status(self: @TState, profile: ContractAddress, community_id: u256) -> bool;
Expand Down
75 changes: 73 additions & 2 deletions tests/test_community.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ fn test_join_community() {
//create the community
let community_id = communityDispatcher.create_comminuty(CommunityType::Free);

let is_member = communityDispatcher
let (is_member, _) = communityDispatcher
.is_community_member(USER_ONE.try_into().unwrap(), community_id);

assert(is_member == true, 'Not Community Member');
Expand All @@ -134,6 +134,42 @@ fn test_should_panic_if_a_user_joins_one_community_twice() {
}

// TEST TODO: test that joining a community emits event
#[test]
fn test_joining_community_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(CommunityType::Free);

communityDispatcher.join_community(USER_THREE.try_into().unwrap(), community_id);

let (_, member_details) = communityDispatcher
.is_community_member(USER_THREE.try_into().unwrap(), community_id);

spy
.assert_emitted(
@array![
(
community_contract_address,
CommunityComponent::Event::JoinedCommunity(
CommunityComponent::JoinedCommunity {
community_id: community_id,
transaction_executor: USER_ONE.try_into().unwrap(),
token_id: member_details.community_token_id,
profile: USER_THREE.try_into().unwrap(),
block_timestamp: get_block_timestamp(),
}
)
)
]
);
}


#[test]
fn test_leave_community() {
Expand All @@ -156,7 +192,7 @@ fn test_leave_community() {
start_cheat_caller_address(community_contract_address, USER_TWO.try_into().unwrap());
communityDispatcher.leave_community(USER_TWO.try_into().unwrap(), community_id);

let is_member = communityDispatcher
let (is_member, _) = communityDispatcher
.is_community_member(USER_TWO.try_into().unwrap(), community_id);
// println!("is member: {}", is_member);
assert(is_member != true, 'still a community member');
Expand Down Expand Up @@ -187,6 +223,41 @@ fn test_should_panic_if_profile_leaving_is_not_a_member() {
}

// TEST TODO: test that leaving a community emits event
#[test]
fn test_leave_community_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(CommunityType::Free);
communityDispatcher.join_community(USER_THREE.try_into().unwrap(), community_id);
let (_, member_details) = communityDispatcher
.is_community_member(USER_THREE.try_into().unwrap(), community_id);
communityDispatcher.leave_community(USER_THREE.try_into().unwrap(), community_id);

spy
.assert_emitted(
@array![
(
community_contract_address,
CommunityComponent::Event::LeftCommunity(
CommunityComponent::LeftCommunity {
community_id: community_id,
transaction_executor: USER_ONE.try_into().unwrap(),
token_id: member_details.community_token_id,
profile: USER_THREE.try_into().unwrap(),
block_timestamp: get_block_timestamp(),
}
)
)
]
);
}


#[test]
fn test_set_community_metadata_uri() {
Expand Down

0 comments on commit d7f65fd

Please sign in to comment.