Skip to content

Commit

Permalink
Fix admin of existing communities being replaced
Browse files Browse the repository at this point in the history
  • Loading branch information
olanod committed May 9, 2024
1 parent 8283aa1 commit 96aed55
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions pallets/communities/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ impl<T: Config> Pallet<T> {
!Self::community_exists(community_id),
Error::<T>::CommunityAlreadyExists
);
ensure!(!CommunityIdFor::<T>::contains_key(admin), Error::<T>::AlreadyAdmin);

if let Some((deposit, depositor, depositee)) = maybe_deposit {
T::Balances::transfer(
Expand Down
2 changes: 2 additions & 0 deletions pallets/communities/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ pub mod pallet {
NoVoteCasted,
/// The poll
NoLocksInPlace,
/// The origin trying to register a community already controls another community
AlreadyAdmin,
}

// Dispatchable functions allows users to interact with the pallet and invoke
Expand Down
15 changes: 15 additions & 0 deletions pallets/communities/src/tests/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,19 @@ mod create {
assert_eq!(Balances::free_balance(ALICE), 5);
});
}

#[test]
fn fails_if_admin_already_controls_another_community() {
new_test_ext(&[], &[]).execute_with(|| {
const ANOTHER_COMMUNITY: CommunityId = 99;
// Simulate a pre-existing community
Info::<Test>::insert(COMMUNITY, CommunityInfo::default());

// Should fail adding another community with existing admin
assert_noop!(
Communities::create(Root.into(), COMMUNITY_ORIGIN, ANOTHER_COMMUNITY),
Error::AlreadyAdmin
);
});
}
}

0 comments on commit 96aed55

Please sign in to comment.