Skip to content

Commit

Permalink
chore: test set ban status for member
Browse files Browse the repository at this point in the history
  • Loading branch information
mubarak23 committed Oct 6, 2024
1 parent bd57e22 commit 20c1198
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 32 deletions.
56 changes: 34 additions & 22 deletions src/community/community.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -181,32 +181,32 @@ pub mod CommunityComponent {
);
community_id
}
fn join_community(ref self: ComponentState<TContractState>, community_id: u256) {
let member_address = get_caller_address();
fn join_community(
ref self: ComponentState<TContractState>, 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

Expand All @@ -216,29 +216,28 @@ pub mod CommunityComponent {
};
self.communities.write(community_id, updated_community);
}
fn leave_community(ref self: ComponentState<TContractState>, community_id: u256) {
let member_address = get_caller_address();
fn leave_community(
ref self: ComponentState<TContractState>, 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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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()) {
Expand All @@ -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));

Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/ICommunity.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ pub trait ICommunity<TState> {
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);
Expand Down
169 changes: 161 additions & 8 deletions tests/test_community.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}


Expand All @@ -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);
Expand All @@ -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);
}


Expand Down Expand Up @@ -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);

}


0 comments on commit 20c1198

Please sign in to comment.