mock.rs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #![cfg(test)]
  2. use crate::*;
  3. // use frame_support::storage::StorageMap;
  4. // use frame_support::traits::{OnFinalize, OnInitialize};
  5. use frame_support::{impl_outer_event, impl_outer_origin, parameter_types};
  6. use sp_core::H256;
  7. use sp_runtime::{
  8. testing::Header,
  9. traits::{BlakeTwo256, IdentityLookup},
  10. Perbill,
  11. };
  12. use crate::ContentActorAuthenticator;
  13. use crate::Trait;
  14. use common::currency::GovernanceCurrency;
  15. use common::storage::StorageSystem;
  16. pub type CuratorId = <Test as ContentActorAuthenticator>::CuratorId;
  17. pub type CuratorGroupId = <Test as ContentActorAuthenticator>::CuratorGroupId;
  18. pub type MemberId = <Test as MembershipTypes>::MemberId;
  19. pub type ChannelId = <Test as StorageOwnership>::ChannelId;
  20. pub type DAOId = <Test as StorageOwnership>::DAOId;
  21. /// Origins
  22. pub const LEAD_ORIGIN: u64 = 1;
  23. pub const FIRST_CURATOR_ORIGIN: u64 = 2;
  24. pub const SECOND_CURATOR_ORIGIN: u64 = 3;
  25. pub const FIRST_MEMBER_ORIGIN: u64 = 4;
  26. pub const SECOND_MEMBER_ORIGIN: u64 = 5;
  27. pub const UNKNOWN_ORIGIN: u64 = 7777;
  28. // Members range from MemberId 1 to 10
  29. pub const MEMBERS_COUNT: MemberId = 10;
  30. /// Runtime Id's
  31. pub const FIRST_CURATOR_ID: CuratorId = 1;
  32. pub const SECOND_CURATOR_ID: CuratorId = 2;
  33. pub const FIRST_CURATOR_GROUP_ID: CuratorGroupId = 1;
  34. pub const SECOND_CURATOR_GROUP_ID: CuratorGroupId = 2;
  35. pub const FIRST_MEMBER_ID: MemberId = 1;
  36. pub const SECOND_MEMBER_ID: MemberId = 2;
  37. impl_outer_origin! {
  38. pub enum Origin for Test {}
  39. }
  40. mod content {
  41. pub use crate::Event;
  42. }
  43. impl_outer_event! {
  44. pub enum MetaEvent for Test {
  45. content<T>,
  46. system<T>,
  47. balances<T>,
  48. }
  49. }
  50. #[derive(Clone, PartialEq, Eq, Debug)]
  51. pub struct Test;
  52. parameter_types! {
  53. pub const BlockHashCount: u64 = 250;
  54. pub const MaximumBlockWeight: u32 = 1024;
  55. pub const MaximumBlockLength: u32 = 2 * 1024;
  56. pub const AvailableBlockRatio: Perbill = Perbill::one();
  57. pub const MinimumPeriod: u64 = 5;
  58. }
  59. impl system::Trait for Test {
  60. type BaseCallFilter = ();
  61. type Origin = Origin;
  62. type Call = ();
  63. type Index = u64;
  64. type BlockNumber = u64;
  65. type Hash = H256;
  66. type Hashing = BlakeTwo256;
  67. type AccountId = u64;
  68. type Lookup = IdentityLookup<Self::AccountId>;
  69. type Header = Header;
  70. type Event = MetaEvent;
  71. type BlockHashCount = BlockHashCount;
  72. type MaximumBlockWeight = MaximumBlockWeight;
  73. type DbWeight = ();
  74. type BlockExecutionWeight = ();
  75. type ExtrinsicBaseWeight = ();
  76. type MaximumExtrinsicWeight = ();
  77. type MaximumBlockLength = MaximumBlockLength;
  78. type AvailableBlockRatio = AvailableBlockRatio;
  79. type Version = ();
  80. type ModuleToIndex = ();
  81. type AccountData = balances::AccountData<u64>;
  82. type OnNewAccount = ();
  83. type OnKilledAccount = ();
  84. }
  85. impl pallet_timestamp::Trait for Test {
  86. type Moment = u64;
  87. type OnTimestampSet = ();
  88. type MinimumPeriod = MinimumPeriod;
  89. }
  90. impl common::MembershipTypes for Test {
  91. type MemberId = u64;
  92. type ActorId = u64;
  93. }
  94. impl common::StorageOwnership for Test {
  95. type ChannelId = u64;
  96. type DAOId = u64;
  97. type ContentId = u64;
  98. type DataObjectTypeId = u64;
  99. }
  100. parameter_types! {
  101. pub const ExistentialDeposit: u32 = 0;
  102. }
  103. impl balances::Trait for Test {
  104. type Balance = u64;
  105. type DustRemoval = ();
  106. type Event = MetaEvent;
  107. type ExistentialDeposit = ExistentialDeposit;
  108. type AccountStore = System;
  109. }
  110. impl GovernanceCurrency for Test {
  111. type Currency = balances::Module<Self>;
  112. }
  113. impl ContentActorAuthenticator for Test {
  114. type CuratorId = u64;
  115. type CuratorGroupId = u64;
  116. fn is_lead(account_id: &Self::AccountId) -> bool {
  117. let lead_account_id = ensure_signed(Origin::signed(LEAD_ORIGIN)).unwrap();
  118. *account_id == lead_account_id
  119. }
  120. fn is_curator(curator_id: &Self::CuratorId, account_id: &Self::AccountId) -> bool {
  121. let first_curator_account_id = ensure_signed(Origin::signed(FIRST_CURATOR_ORIGIN)).unwrap();
  122. let second_curator_account_id =
  123. ensure_signed(Origin::signed(SECOND_CURATOR_ORIGIN)).unwrap();
  124. (first_curator_account_id == *account_id && FIRST_CURATOR_ID == *curator_id)
  125. || (second_curator_account_id == *account_id && SECOND_CURATOR_ID == *curator_id)
  126. }
  127. fn is_member(member_id: &Self::MemberId, account_id: &Self::AccountId) -> bool {
  128. let unknown_member_account_id = ensure_signed(Origin::signed(UNKNOWN_ORIGIN)).unwrap();
  129. *member_id < MEMBERS_COUNT && unknown_member_account_id != *account_id
  130. }
  131. }
  132. pub struct MockStorageSystem {}
  133. // Anyone can upload without restriction
  134. impl StorageSystem<Test> for MockStorageSystem {
  135. fn atomically_add_content(
  136. _owner: StorageObjectOwner<Test>,
  137. _content_parameters: Vec<ContentParameters<Test>>,
  138. ) -> DispatchResult {
  139. Ok(())
  140. }
  141. fn can_add_content(
  142. _owner: StorageObjectOwner<Test>,
  143. _content_parameters: Vec<ContentParameters<Test>>,
  144. ) -> DispatchResult {
  145. Ok(())
  146. }
  147. }
  148. parameter_types! {
  149. pub const MaxNumberOfCuratorsPerGroup: u32 = 10;
  150. pub const ChannelOwnershipPaymentEscrowId: [u8; 8] = *b"12345678";
  151. }
  152. impl Trait for Test {
  153. /// The overarching event type.
  154. type Event = MetaEvent;
  155. /// Channel Transfer Payments Escrow Account seed for ModuleId to compute deterministic AccountId
  156. type ChannelOwnershipPaymentEscrowId = ChannelOwnershipPaymentEscrowId;
  157. /// Type of identifier for Videos
  158. type VideoId = u64;
  159. /// Type of identifier for Video Categories
  160. type VideoCategoryId = u64;
  161. /// Type of identifier for Channel Categories
  162. type ChannelCategoryId = u64;
  163. /// Type of identifier for Playlists
  164. type PlaylistId = u64;
  165. /// Type of identifier for Persons
  166. type PersonId = u64;
  167. /// Type of identifier for Channels
  168. type SeriesId = u64;
  169. /// Type of identifier for Channel transfer requests
  170. type ChannelOwnershipTransferRequestId = u64;
  171. /// The maximum number of curators per group constraint
  172. type MaxNumberOfCuratorsPerGroup = MaxNumberOfCuratorsPerGroup;
  173. // Type that handles asset uploads to storage system
  174. type StorageSystem = MockStorageSystem;
  175. }
  176. pub type System = system::Module<Test>;
  177. pub type Content = Module<Test>;
  178. // #[derive (Default)]
  179. pub struct ExtBuilder {
  180. next_channel_category_id: u64,
  181. next_channel_id: u64,
  182. next_video_category_id: u64,
  183. next_video_id: u64,
  184. next_playlist_id: u64,
  185. next_person_id: u64,
  186. next_series_id: u64,
  187. next_channel_transfer_request_id: u64,
  188. next_curator_group_id: u64,
  189. }
  190. impl Default for ExtBuilder {
  191. fn default() -> Self {
  192. Self {
  193. next_channel_category_id: 1,
  194. next_channel_id: 1,
  195. next_video_category_id: 1,
  196. next_video_id: 1,
  197. next_playlist_id: 1,
  198. next_person_id: 1,
  199. next_series_id: 1,
  200. next_channel_transfer_request_id: 1,
  201. next_curator_group_id: 1,
  202. }
  203. }
  204. }
  205. impl ExtBuilder {
  206. pub fn build(self) -> sp_io::TestExternalities {
  207. let mut t = system::GenesisConfig::default()
  208. .build_storage::<Test>()
  209. .unwrap();
  210. GenesisConfig::<Test> {
  211. next_channel_category_id: self.next_channel_category_id,
  212. next_channel_id: self.next_channel_id,
  213. next_video_category_id: self.next_video_category_id,
  214. next_video_id: self.next_video_id,
  215. next_playlist_id: self.next_playlist_id,
  216. next_person_id: self.next_person_id,
  217. next_series_id: self.next_series_id,
  218. next_channel_transfer_request_id: self.next_channel_transfer_request_id,
  219. next_curator_group_id: self.next_curator_group_id,
  220. }
  221. .assimilate_storage(&mut t)
  222. .unwrap();
  223. t.into()
  224. }
  225. }
  226. pub fn with_default_mock_builder<R, F: FnOnce() -> R>(f: F) -> R {
  227. ExtBuilder::default().build().execute_with(|| f())
  228. }