Skip to content

Commit

Permalink
chore: Add tests for ownership pallet (#256)
Browse files Browse the repository at this point in the history
* Add tests for ownership pallet

* Add utility function to ownership for benchmarking

* Update benchmarks in ownership pallet

---------

Co-authored-by: Oleh Mell <[email protected]>
  • Loading branch information
F3Joule and olehmell authored Mar 14, 2024
1 parent beb18a6 commit 3212edd
Show file tree
Hide file tree
Showing 24 changed files with 854 additions and 326 deletions.
6 changes: 6 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ std = [
'pallet-timestamp/std',
'frame-support/std',
'frame-system/std',
'pallet-domains/std',
'pallet-permissions/std',
'pallet-posts/std',
'pallet-profiles/std',
Expand All @@ -47,6 +48,7 @@ sp-runtime = { git = 'https://github.com/paritytech/substrate', branch = 'polkad
sp-std = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.40', default-features = false }

[dev-dependencies]
pallet-domains = { path = '../pallets/domains', default-features = false }
pallet-permissions = { path = '../pallets/permissions', default-features = false }
pallet-posts = { path = '../pallets/posts', default-features = false }
pallet-profiles = { path = '../pallets/profiles', default-features = false }
Expand Down
30 changes: 30 additions & 0 deletions integration-tests/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ frame_support::construct_runtime!(
SpaceFollows: pallet_space_follows,
SpaceOwnership: pallet_ownership,
Spaces: pallet_spaces,
Domains: pallet_domains,
}
);

Expand Down Expand Up @@ -163,7 +164,12 @@ impl pallet_space_follows::Config for TestRuntime {
impl pallet_ownership::Config for TestRuntime {
type RuntimeEvent = RuntimeEvent;
type ProfileManager = Profiles;
type SpacesInterface = Spaces;
type SpacePermissionsProvider = Spaces;
type CreatorStakingProvider = ();
type DomainsProvider = Domains;
type PostsProvider = Posts;
type Currency = Balances;
type WeightInfo = ();
}

Expand All @@ -177,6 +183,30 @@ impl pallet_spaces::Config for TestRuntime {
type WeightInfo = ();
}

parameter_types! {
pub const RegistrationPeriodLimit: BlockNumber = 100;
pub const BaseDomainDeposit: u64 = 10;
pub const OuterValueByteDeposit: u64 = 1;
pub const InitialPaymentBeneficiary: AccountId = ACCOUNT1;
pub InitialPricesConfig: pallet_domains::types::PricesConfigVec<TestRuntime> = vec![(1, 100)];
}

impl pallet_domains::Config for TestRuntime {
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type MinDomainLength = ConstU32<1>;
type MaxDomainLength = ConstU32<64>;
type MaxDomainsPerAccount = ConstU32<10>;
type DomainsInsertLimit = ConstU32<10>;
type RegistrationPeriodLimit = RegistrationPeriodLimit;
type MaxOuterValueLength = ConstU32<64>;
type BaseDomainDeposit = BaseDomainDeposit;
type OuterValueByteDeposit = OuterValueByteDeposit;
type InitialPaymentBeneficiary = InitialPaymentBeneficiary;
type InitialPricesConfig = InitialPricesConfig;
type WeightInfo = ();
}

pub(crate) type AccountId = u64;
pub(crate) type BlockNumber = u64;

Expand Down
12 changes: 6 additions & 6 deletions integration-tests/src/tests/space_ownership.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use frame_support::{assert_ok, assert_noop};
use sp_runtime::traits::Zero;

use pallet_ownership::Error as SpaceOwnershipError;
use pallet_ownership::{EntityWithOwnership, Error as SpaceOwnershipError};
use pallet_spaces::Error as SpacesError;

use crate::mock::*;
Expand All @@ -20,7 +20,7 @@ fn transfer_space_ownership_should_work() {
assert_ok!(_transfer_default_space_ownership()); // Transfer SpaceId 1 owned by ACCOUNT1 to ACCOUNT2

assert_eq!(
SpaceOwnership::pending_space_owner(SPACE1).unwrap(),
SpaceOwnership::pending_ownership_transfer(EntityWithOwnership::Space(SPACE1)).unwrap(),
ACCOUNT2
);
});
Expand Down Expand Up @@ -70,7 +70,7 @@ fn accept_pending_ownership_should_work() {
assert_eq!(space.owner, ACCOUNT2);

// Check that pending storage is cleared:
assert!(SpaceOwnership::pending_space_owner(SPACE1).is_none());
assert!(SpaceOwnership::pending_ownership_transfer(EntityWithOwnership::Space(SPACE1)).is_none());

assert!(Balances::reserved_balance(ACCOUNT1).is_zero());

Expand Down Expand Up @@ -108,7 +108,7 @@ fn accept_pending_ownership_should_fail_if_origin_is_already_an_owner() {

assert_noop!(
_accept_pending_ownership(Some(RuntimeOrigin::signed(ACCOUNT1)), None),
SpaceOwnershipError::<TestRuntime>::AlreadyOwner
SpaceOwnershipError::<TestRuntime>::NotAllowedToAcceptOwnershipTransfer,
);
});
}
Expand Down Expand Up @@ -137,7 +137,7 @@ fn reject_pending_ownership_should_work() {
assert_eq!(space.owner, ACCOUNT1);

// Check whether storage state is correct
assert!(SpaceOwnership::pending_space_owner(SPACE1).is_none());
assert!(SpaceOwnership::pending_ownership_transfer(EntityWithOwnership::Space(SPACE1)).is_none());
});
}

Expand All @@ -153,7 +153,7 @@ fn reject_pending_ownership_should_work_when_proposal_rejected_by_current_space_
assert_eq!(space.owner, ACCOUNT1);

// Check whether storage state is correct
assert!(SpaceOwnership::pending_space_owner(SPACE1).is_none());
assert!(SpaceOwnership::pending_ownership_transfer(EntityWithOwnership::Space(SPACE1)).is_none());
});
}

Expand Down
7 changes: 4 additions & 3 deletions integration-tests/src/utils/space_ownership_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Full license is available at https://github.com/dappforce/subsocial-parachain/blob/main/LICENSE

use frame_support::pallet_prelude::*;
use pallet_ownership::EntityWithOwnership;

use subsocial_support::SpaceId;

Expand All @@ -22,7 +23,7 @@ pub(crate) fn _transfer_space_ownership(
) -> DispatchResult {
SpaceOwnership::transfer_ownership(
origin.unwrap_or_else(|| RuntimeOrigin::signed(ACCOUNT1)),
space_id.unwrap_or(SPACE1),
space_id.map_or(EntityWithOwnership::Space(SPACE1), |id| EntityWithOwnership::Space(id)),
transfer_to.unwrap_or(ACCOUNT2),
)
}
Expand All @@ -34,7 +35,7 @@ pub(crate) fn _accept_default_pending_ownership() -> DispatchResult {
pub(crate) fn _accept_pending_ownership(origin: Option<RuntimeOrigin>, space_id: Option<SpaceId>) -> DispatchResult {
SpaceOwnership::accept_pending_ownership(
origin.unwrap_or_else(|| RuntimeOrigin::signed(ACCOUNT2)),
space_id.unwrap_or(SPACE1),
space_id.map_or(EntityWithOwnership::Space(SPACE1), |id| EntityWithOwnership::Space(id)),
)
}

Expand All @@ -49,6 +50,6 @@ pub(crate) fn _reject_default_pending_ownership_by_current_owner() -> DispatchRe
pub(crate) fn _reject_pending_ownership(origin: Option<RuntimeOrigin>, space_id: Option<SpaceId>) -> DispatchResult {
SpaceOwnership::reject_pending_ownership(
origin.unwrap_or_else(|| RuntimeOrigin::signed(ACCOUNT2)),
space_id.unwrap_or(SPACE1),
space_id.map_or(EntityWithOwnership::Space(SPACE1), |id| EntityWithOwnership::Space(id)),
)
}
2 changes: 2 additions & 0 deletions pallets/creator-staking/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ mock! {

impl SpacesInterface<AccountId, SpaceId> for Spaces {
fn get_space_owner(_space_id: SpaceId) -> Result<AccountId, DispatchError>;

fn update_space_owner(_space_id: SpaceId, _new_owner: AccountId) -> DispatchResult;

fn create_space(_owner: &AccountId, _content: Content) -> Result<SpaceId, DispatchError>;
}
Expand Down
5 changes: 4 additions & 1 deletion pallets/domains/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-

[features]
default = ["std"]
runtime-benchmarks = ["frame-benchmarking"]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"subsocial-support/runtime-benchmarks",
]
std = [
"codec/std",
"scale-info/std",
Expand Down
16 changes: 16 additions & 0 deletions pallets/domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,5 +775,21 @@ pub mod pallet {

Ok(())
}

#[cfg(feature = "runtime-benchmarks")]
fn register_domain(owner: &T::AccountId, domain: &[u8]) -> Result<Vec<u8>, DispatchError> {
ensure!(domain.len() <= T::MaxDomainLength::get() as usize, Error::<T>::DomainIsTooLong);
let domain_lc = Self::lower_domain_then_bound(domain);

Self::do_register_domain(
owner.clone(),
owner.clone(),
domain_lc.clone(),
Content::None,
T::RegistrationPeriodLimit::get(),
)?;

Ok(domain_lc.into())
}
}
}
13 changes: 13 additions & 0 deletions pallets/posts/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,4 +476,17 @@ impl<T: Config> PostsProvider<T::AccountId> for Pallet<T> {

Ok(())
}

#[cfg(feature = "runtime-benchmarks")]
fn create_post(owner: &T::AccountId, space_id: SpaceId, content: Content) -> Result<PostId, DispatchError> {
let new_post_id = Self::next_post_id();
let new_post: Post<T> =
Post::new(new_post_id, owner.clone(), Some(space_id), PostExtension::RegularPost, content.clone());

PostById::insert(new_post_id, new_post);
PostIdsBySpaceId::<T>::mutate(space_id, |ids| ids.push(new_post_id));
NextPostId::<T>::mutate(|n| n.saturating_inc());

Ok(new_post_id)
}
}
3 changes: 3 additions & 0 deletions pallets/posts/tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ sp-runtime = { git = 'https://github.com/paritytech/substrate', branch = 'polkad
sp-std = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.40', default-features = false }

[dev-dependencies]
frame-benchmarking = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.40', default-features = false }
sp-core = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.40', default-features = false }
sp-io = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.40', default-features = false }
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", default-features = false }
Expand All @@ -41,10 +42,12 @@ pallet-spaces = { default-features = false, path = '../../spaces' }

[features]
default = ['std']
runtime-benchmarks = ['subsocial-support/runtime-benchmarks']
std = [
'codec/std',
'scale-info/std',
'pallet-timestamp/std',
'frame-benchmarking/std',
'frame-support/std',
'frame-system/std',
'sp-runtime/std',
Expand Down
39 changes: 34 additions & 5 deletions pallets/posts/tests/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
// Full license is available at https://github.com/dappforce/subsocial-parachain/blob/main/LICENSE

use frame_support::{pallet_prelude::ConstU32, parameter_types, traits::Everything};
use frame_support::dispatch::DispatchResult;
use sp_core::H256;
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
};
use sp_runtime::{DispatchError, testing::Header, traits::{BlakeTwo256, IdentityLookup}};
use sp_std::convert::{TryFrom, TryInto};
use subsocial_support::traits::DomainsProvider;

use crate::tests_utils::*;

Expand All @@ -32,7 +31,7 @@ frame_support::construct_runtime!(
SpaceFollows: pallet_space_follows,
Posts: pallet_posts,
Spaces: pallet_spaces,
SpaceOwnership: pallet_ownership,
Ownership: pallet_ownership,
}
);

Expand Down Expand Up @@ -150,9 +149,39 @@ impl pallet_space_follows::Config for Test {
type WeightInfo = ();
}

parameter_types! {
pub const MaxDomainLength: u32 = 64;
}
pub struct MockEmptyDomainsProvider;
impl DomainsProvider<AccountId> for MockEmptyDomainsProvider {
type DomainLength = MaxDomainLength;

fn get_domain_owner(_domain: &[u8]) -> Result<AccountId, DispatchError> {
Ok(ACCOUNT1)
}

fn ensure_allowed_to_update_domain(_account: &AccountId, _domain: &[u8]) -> DispatchResult {
Ok(())
}

fn update_domain_owner(_domain: &[u8], _new_owner: &AccountId) -> DispatchResult {
Ok(())
}

#[cfg(feature = "runtime-benchmarks")]
fn register_domain(_owner: &AccountId, _domain: &[u8]) -> Result<Vec<u8>, DispatchError> {
Ok(Vec::new())
}
}

impl pallet_ownership::Config for Test {
type RuntimeEvent = RuntimeEvent;
type ProfileManager = Profiles;
type SpacesInterface = Spaces;
type SpacePermissionsProvider = Spaces;
type CreatorStakingProvider = ();
type DomainsProvider = MockEmptyDomainsProvider;
type PostsProvider = Posts;
type Currency = Balances;
type WeightInfo = ();
}
5 changes: 3 additions & 2 deletions pallets/posts/tests/src/tests_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::{
use frame_support::{assert_ok, pallet_prelude::*};
use sp_core::storage::Storage;
use sp_io::TestExternalities;
use pallet_ownership::EntityWithOwnership;

use pallet_permissions::{SpacePermission as SP, SpacePermission, SpacePermissions};
use pallet_posts::{Comment, PostExtension, PostUpdate};
Expand Down Expand Up @@ -486,9 +487,9 @@ pub(crate) fn _transfer_space_ownership(
space_id: Option<SpaceId>,
transfer_to: Option<AccountId>,
) -> DispatchResult {
SpaceOwnership::transfer_ownership(
Ownership::transfer_ownership(
origin.unwrap_or_else(|| RuntimeOrigin::signed(ACCOUNT1)),
space_id.unwrap_or(SPACE1),
space_id.map_or(EntityWithOwnership::Space(SPACE1), |id| EntityWithOwnership::Space(id)),
transfer_to.unwrap_or(ACCOUNT2),
)
}
2 changes: 2 additions & 0 deletions pallets/profiles/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ mock! {

impl SpacesInterface<AccountId, SpaceId> for Spaces {
fn get_space_owner(_space_id: SpaceId) -> Result<AccountId, DispatchError>;

fn update_space_owner(_space_id: SpaceId, _new_owner: AccountId) -> DispatchResult;

fn create_space(_owner: &AccountId, _content: Content) -> Result<SpaceId, DispatchError>;
}
Expand Down
8 changes: 7 additions & 1 deletion pallets/space-ownership/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ categories = ['cryptography::cryptocurrencies']

[features]
default = ['std']
runtime-benchmarks = ['frame-benchmarking/runtime-benchmarks']
runtime-benchmarks = [
'frame-benchmarking/runtime-benchmarks',
'subsocial-support/runtime-benchmarks',
]
std = [
'codec/std',
'scale-info/std',
Expand Down Expand Up @@ -40,3 +43,6 @@ frame-support = { git = 'https://github.com/paritytech/substrate', branch = 'pol
frame-system = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.40', default-features = false }
sp-runtime = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.40', default-features = false }
sp-std = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.40', default-features = false }

[dev-dependencies]
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", default-features = false }
Loading

0 comments on commit 3212edd

Please sign in to comment.