From d7f65fd0881d96eedc8df0e33f3b0c5d277d50e5 Mon Sep 17 00:00:00 2001 From: mubarak23 Date: Thu, 10 Oct 2024 16:59:11 +0100 Subject: [PATCH] chore: test for emit join and leave community --- src/community/community.cairo | 53 ++++++++++++----------- src/interfaces/ICommunity.cairo | 6 ++- tests/test_community.cairo | 75 ++++++++++++++++++++++++++++++++- 3 files changed, 105 insertions(+), 29 deletions(-) diff --git a/src/community/community.cairo b/src/community/community.cairo index 9c6421a..4f2024d 100644 --- a/src/community/community.cairo +++ b/src/community/community.cairo @@ -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)] @@ -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); @@ -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>(), @@ -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( @@ -462,12 +464,12 @@ pub mod CommunityComponent { /// @return bool true/false stating user's membership status fn is_community_member( self: @ComponentState, 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) } } @@ -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); @@ -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); @@ -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 @@ -802,10 +804,11 @@ pub mod CommunityComponent { fn _burn_community_nft( ref self: ComponentState, 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); } } } diff --git a/src/interfaces/ICommunity.cairo b/src/interfaces/ICommunity.cairo index e9e7d30..4dbaa84 100644 --- a/src/interfaces/ICommunity.cairo +++ b/src/interfaces/ICommunity.cairo @@ -1,6 +1,6 @@ use starknet::{ContractAddress}; use karst::base::constants::types::{ - GateKeepType, CommunityGateKeepDetails, CommunityType, CommunityDetails + GateKeepType, CommunityGateKeepDetails, CommunityType, CommunityDetails, CommunityMember }; // ************************************************************************* @@ -41,7 +41,9 @@ pub trait ICommunity { // ************************************************************************* 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; diff --git a/tests/test_community.cairo b/tests/test_community.cairo index ebc8dbf..5857f55 100644 --- a/tests/test_community.cairo +++ b/tests/test_community.cairo @@ -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'); @@ -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() { @@ -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'); @@ -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() {