From 20c1198e694792d50fd782de03c444c83e71527b Mon Sep 17 00:00:00 2001 From: mubarak23 Date: Sun, 6 Oct 2024 11:13:32 +0100 Subject: [PATCH] chore: test set ban status for member --- src/community/community.cairo | 56 ++++++----- src/interfaces/ICommunity.cairo | 4 +- tests/test_community.cairo | 169 ++++++++++++++++++++++++++++++-- 3 files changed, 197 insertions(+), 32 deletions(-) diff --git a/src/community/community.cairo b/src/community/community.cairo index 41467dd..95a9b47 100644 --- a/src/community/community.cairo +++ b/src/community/community.cairo @@ -181,32 +181,32 @@ pub mod CommunityComponent { ); community_id } - fn join_community(ref self: ComponentState, community_id: u256) { - let member_address = get_caller_address(); + fn join_community( + ref self: ComponentState, profile: ContractAddress, community_id: u256 + ) { + // let member_address = get_caller_address(); let community = self.communities.read(community_id); // if (!community) { // panic('Community does not exist') // } - let community_member = self - .community_membership_status - .read((community_id, member_address)); + let community_member = self.community_membership_status.read((community_id, profile)); assert(community_member != true, ALREADY_MEMBER); // community_token_id // a token is minted from the comunity token contract address let community_member = CommunityMember { - profile_address: member_address, + profile_address: profile, community_id: community_id, total_publications: 0, community_token_id: 1, // community.community_token_id, COMING BACK TO THIS ban_status: false }; - self.community_membership_status.write((community_id, member_address), true); + self.community_membership_status.write((community_id, profile), true); - self.community_member.write((community_id, member_address), community_member); + self.community_member.write((community_id, profile), community_member); // update community member count @@ -216,29 +216,28 @@ pub mod CommunityComponent { }; self.communities.write(community_id, updated_community); } - fn leave_community(ref self: ComponentState, community_id: u256) { - let member_address = get_caller_address(); + fn leave_community( + ref self: ComponentState, profile: ContractAddress, community_id: u256 + ) { let community = self.communities.read(community_id); - let community_member = self - .community_membership_status - .read((community_id, member_address)); - println!("Before inside leave_community is member: {}", community_member); + let community_member = self.community_membership_status.read((community_id, profile)); + // println!("Before inside leave_community is member: {}", community_member); assert(community_member == true, NOT_MEMBER); // remove the member_community_id - self.community_membership_status.write((community_id, member_address), false); + self.community_membership_status.write((community_id, profile), false); // remove member details let leave_community_member = CommunityMember { - profile_address: member_address, + profile_address: profile, community_id: 0, total_publications: 0, community_token_id: 0, ban_status: true }; - self.community_member.write((community_id, member_address), leave_community_member); + self.community_member.write((community_id, profile), leave_community_member); // update community member count let community_total_members = community.community_total_members - 1; @@ -273,6 +272,19 @@ pub mod CommunityComponent { let community_owner = self.community_owner.read(community_id); assert(community_owner == get_caller_address(), NOT_COMMUNITY_OWNER); + // Mod must join the community + let community_member = CommunityMember { + profile_address: moderator, + community_id: community_id, + total_publications: 0, + community_token_id: 1, // community.community_token_id, COMING BACK TO THIS + ban_status: false + }; + + self.community_membership_status.write((community_id, moderator), true); + + self.community_member.write((community_id, moderator), community_member); + let community = self.communities.read(community_id); self.community_mod.write((community_id, moderator), true); @@ -331,9 +343,8 @@ pub mod CommunityComponent { let caller_is_owner = caller == community_owner_address; // If caller is neither a mod nor the owner, throw an error - if (!caller_is_mod && !caller_is_owner) { - panic!("Only community moderator or the owner can ban members"); - } + + assert(caller_is_mod || caller_is_owner, 'Cannot ban member'); // let community = self.communities.read(community_id); // if (!communityis_none()) { @@ -343,8 +354,9 @@ pub mod CommunityComponent { let community = self.communities.read(community_id); assert(community.community_owner == community_owner_address, NOT_COMMUNITY_OWNER); - let member_community_id = self.member_community_id.read(profile); - assert(member_community_id == community_id, NOT_MEMBER); + let community_member = self.community_membership_status.read((community_id, profile)); + println!("Before inside leave_community is member: {}", community_member); + assert(community_member == true, NOT_MEMBER); let community_member = self.community_member.read((community_id, profile)); diff --git a/src/interfaces/ICommunity.cairo b/src/interfaces/ICommunity.cairo index c66a0a1..8061798 100644 --- a/src/interfaces/ICommunity.cairo +++ b/src/interfaces/ICommunity.cairo @@ -14,8 +14,8 @@ pub trait ICommunity { ref self: TState, hub_address: ContractAddress, community_nft_classhash: felt252 ); fn create_comminuty(ref self: TState) -> u256; - fn join_community(ref self: TState, community_id: u256); - fn leave_community(ref self: TState, community_id: u256); + fn join_community(ref self: TState, profile: ContractAddress, community_id: u256); + fn leave_community(ref self: TState, profile: ContractAddress, community_id: u256); fn set_community_metadata_uri(ref self: TState, community_id: u256, metadata_uri: ByteArray); fn add_community_mods(ref self: TState, community_id: u256, moderator: ContractAddress); fn remove_community_mods(ref self: TState, community_id: u256, moderator: ContractAddress); diff --git a/tests/test_community.cairo b/tests/test_community.cairo index dd5ccaf..1f323c4 100644 --- a/tests/test_community.cairo +++ b/tests/test_community.cairo @@ -123,7 +123,7 @@ fn test_join_community() { //create the community let community_id = communityDispatcher.create_comminuty(); // join the community - communityDispatcher.join_community(community_id); + communityDispatcher.join_community(USER_ONE.try_into().unwrap(), community_id); let (is_member, community) = communityDispatcher .is_community_member(USER_ONE.try_into().unwrap(), community_id); println!("is member: {}", is_member); @@ -147,8 +147,8 @@ fn test_should_panic_join_one_community_twice() { let community_id = communityDispatcher.create_comminuty(); - communityDispatcher.join_community(community_id); - communityDispatcher.join_community(community_id); + communityDispatcher.join_community(USER_ONE.try_into().unwrap(), community_id); + communityDispatcher.join_community(USER_ONE.try_into().unwrap(), community_id); } @@ -167,18 +167,18 @@ fn test_leave_community() { //create the community let community_id = communityDispatcher.create_comminuty(); // join the community - communityDispatcher.join_community(community_id); + communityDispatcher.join_community(USER_ONE.try_into().unwrap(), community_id); stop_cheat_caller_address(community_contract_address); start_cheat_caller_address(community_contract_address, USER_TWO.try_into().unwrap()); - communityDispatcher.join_community(community_id); + communityDispatcher.join_community(USER_TWO.try_into().unwrap(), community_id); stop_cheat_caller_address(community_contract_address); // leave community start_cheat_caller_address(community_contract_address, USER_TWO.try_into().unwrap()); - communityDispatcher.leave_community(community_id); + communityDispatcher.leave_community(USER_TWO.try_into().unwrap(), community_id); let (is_member, community) = communityDispatcher .is_community_member(USER_TWO.try_into().unwrap(), community_id); @@ -205,13 +205,13 @@ fn test_should_panic_not_member() { //create the community let community_id = communityDispatcher.create_comminuty(); // join the community - communityDispatcher.join_community(community_id); + communityDispatcher.join_community(USER_ONE.try_into().unwrap(), community_id); stop_cheat_caller_address(community_contract_address); // leave community start_cheat_caller_address(community_contract_address, USER_TWO.try_into().unwrap()); - communityDispatcher.leave_community(community_id); + communityDispatcher.leave_community(USER_TWO.try_into().unwrap(), community_id); } @@ -305,3 +305,156 @@ fn test_should_panic_add_community_mod() { // add a community mod communityDispatcher.add_community_mods(community_id, USER_SIX.try_into().unwrap()); } + +#[test] +fn test_remove_community_mod() { + let ( + community_contract_address, + karst_nft_contract_address, + handle_contract_address, + handle_registry_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(); + // add a community mod + communityDispatcher.add_community_mods(community_id, USER_SIX.try_into().unwrap()); + communityDispatcher.add_community_mods(community_id, USER_FOUR.try_into().unwrap()); + communityDispatcher.add_community_mods(community_id, USER_FIVE.try_into().unwrap()); + + // REMOVE A MOD + communityDispatcher.remove_community_mods(community_id, USER_FIVE.try_into().unwrap()); + + // check a community mod - is_community_mod + let is_community_mod = communityDispatcher + .is_community_mod(USER_FIVE.try_into().unwrap(), community_id); + assert(is_community_mod == false, 'Community Mod Not Remove'); + stop_cheat_caller_address(community_contract_address); +} + +#[test] +#[should_panic(expected: ('Karst: Not Community owner',))] +fn test_should_panic_remove_community_mod() { + let ( + community_contract_address, + karst_nft_contract_address, + handle_contract_address, + handle_registry_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_ONE.try_into().unwrap()); + + // add a community mod + communityDispatcher.add_community_mods(community_id, USER_SIX.try_into().unwrap()); + communityDispatcher.add_community_mods(community_id, USER_FOUR.try_into().unwrap()); + + stop_cheat_caller_address(community_contract_address); + + // when a wrong community owner try to REMOVE a MOD + start_cheat_caller_address(community_contract_address, USER_TWO.try_into().unwrap()); + + // REMOVE A MOD + communityDispatcher.remove_community_mods(community_id, USER_FIVE.try_into().unwrap()); +} + +#[test] +fn test_set_ban_status_owner() { + let ( + community_contract_address, + karst_nft_contract_address, + handle_contract_address, + handle_registry_contract_address, + ) = + __setup__(); + let communityDispatcher = ICommunityDispatcher { contract_address: community_contract_address }; + + start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap()); + //create the community + let community_id = communityDispatcher.create_comminuty(); + // join the community + communityDispatcher.join_community(USER_ONE.try_into().unwrap(), community_id); + communityDispatcher.join_community(USER_TWO.try_into().unwrap(), community_id); + communityDispatcher.join_community(USER_THREE.try_into().unwrap(), community_id); + communityDispatcher.join_community(USER_FOUR.try_into().unwrap(), community_id); + + communityDispatcher.set_ban_status(community_id, USER_TWO.try_into().unwrap(), true); + + let is_ban = communityDispatcher.get_ban_status(USER_TWO.try_into().unwrap(), community_id); + + assert(is_ban == true, 'Community Member is not ban'); + + stop_cheat_caller_address(community_contract_address); +} + +#[test] +fn test_set_ban_status_by_mod() { + let ( + community_contract_address, + karst_nft_contract_address, + handle_contract_address, + handle_registry_contract_address, + ) = + __setup__(); + let communityDispatcher = ICommunityDispatcher { contract_address: community_contract_address }; + + start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap()); + //create the community + let community_id = communityDispatcher.create_comminuty(); + // join the community + communityDispatcher.join_community(USER_ONE.try_into().unwrap(), community_id); + communityDispatcher.join_community(USER_TWO.try_into().unwrap(), community_id); + communityDispatcher.join_community(USER_THREE.try_into().unwrap(), community_id); + communityDispatcher.join_community(USER_FOUR.try_into().unwrap(), community_id); + // add a community mod + communityDispatcher.add_community_mods(community_id, USER_SIX.try_into().unwrap()); + stop_cheat_caller_address(community_contract_address); + + start_cheat_caller_address(community_contract_address, USER_SIX.try_into().unwrap()); + communityDispatcher.set_ban_status(community_id, USER_TWO.try_into().unwrap(), true); + + let is_ban = communityDispatcher.get_ban_status(USER_TWO.try_into().unwrap(), community_id); + + assert(is_ban == true, 'Community Member is not ban'); + + stop_cheat_caller_address(community_contract_address); +} + +#[test] +#[should_panic(expected: ('Cannot ban member',))] +fn test_should_panic_set_ban_status() { + let ( + community_contract_address, + karst_nft_contract_address, + handle_contract_address, + handle_registry_contract_address, + ) = + __setup__(); + let communityDispatcher = ICommunityDispatcher { contract_address: community_contract_address }; + + start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap()); + //create the community + let community_id = communityDispatcher.create_comminuty(); + // join the community + communityDispatcher.join_community(USER_ONE.try_into().unwrap(), community_id); + communityDispatcher.join_community(USER_TWO.try_into().unwrap(), community_id); + communityDispatcher.join_community(USER_THREE.try_into().unwrap(), community_id); + communityDispatcher.join_community(USER_FOUR.try_into().unwrap(), community_id); + // add a community mod + communityDispatcher.add_community_mods(community_id, USER_SIX.try_into().unwrap()); + stop_cheat_caller_address(community_contract_address); + + start_cheat_caller_address(community_contract_address, USER_THREE.try_into().unwrap()); + communityDispatcher.set_ban_status(community_id, USER_TWO.try_into().unwrap(), true); + +} + +