diff --git a/src/community/community.cairo b/src/community/community.cairo index 95a9b47..d00da19 100644 --- a/src/community/community.cairo +++ b/src/community/community.cairo @@ -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, } // ************************************************************************* @@ -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 @@ -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 }; diff --git a/tests/test_community.cairo b/tests/test_community.cairo index 746a655..0fa0ad6 100644 --- a/tests/test_community.cairo +++ b/tests/test_community.cairo @@ -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 }; @@ -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 @@ -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() + } + ) + ) + ] + ); +} +