tests.rs 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272
  1. #![cfg(test)]
  2. use super::genesis;
  3. use super::mock::{self, *};
  4. //use crate::membership;
  5. use hiring;
  6. use rstd::collections::btree_map::BTreeMap;
  7. use rstd::collections::btree_set::BTreeSet;
  8. use sr_primitives::traits::One;
  9. use srml_support::{assert_err, assert_ok, StorageLinkedMap, StorageValue};
  10. /// DIRTY IMPORT BECAUSE
  11. /// InputValidationLengthConstraint has not been factored out yet!!!
  12. use forum::InputValidationLengthConstraint;
  13. #[test]
  14. fn create_channel_success() {
  15. TestExternalitiesBuilder::<Test>::default()
  16. .build()
  17. .execute_with(|| {
  18. // Add channel creator as member
  19. let channel_creator_member_root_and_controller_account = 12312;
  20. let channel_creator_member_id = add_member(
  21. channel_creator_member_root_and_controller_account,
  22. to_vec(CHANNEL_CREATOR_HANDLE),
  23. );
  24. let fixture = CreateChannelFixture::make_valid_unpulished_video_channel_for(
  25. channel_creator_member_id,
  26. None,
  27. );
  28. fixture.call_and_assert_success();
  29. });
  30. }
  31. #[test]
  32. fn create_channel_is_not_a_member() {
  33. TestExternalitiesBuilder::<Test>::default()
  34. .build()
  35. .execute_with(|| {
  36. let channel_creator_member_id = add_channel_creator_member();
  37. let mut fixture = CreateChannelFixture::make_valid_unpulished_video_channel_for(
  38. channel_creator_member_id,
  39. None,
  40. );
  41. // Change to invalid member id, i.e. != channel_creator_member_id
  42. fixture.channel_creator_member_id = fixture.channel_creator_member_id
  43. + <<Test as members::Trait>::MemberId as One>::one();
  44. fixture.call_and_assert_error(MSG_CREATE_CHANNEL_IS_NOT_MEMBER);
  45. });
  46. }
  47. #[test]
  48. fn create_channel_not_enabled() {
  49. TestExternalitiesBuilder::<Test>::default()
  50. .build()
  51. .execute_with(|| {
  52. add_member_and_set_as_lead();
  53. set_channel_creation_enabled(false);
  54. let channel_creator_member_id = add_channel_creator_member();
  55. let fixture = CreateChannelFixture::make_valid_unpulished_video_channel_for(
  56. channel_creator_member_id,
  57. None,
  58. );
  59. fixture.call_and_assert_error(MSG_CHANNEL_CREATION_DISABLED);
  60. });
  61. }
  62. #[test]
  63. fn create_channel_with_bad_member_role_account() {
  64. TestExternalitiesBuilder::<Test>::default()
  65. .build()
  66. .execute_with(|| {
  67. let channel_creator_member_id = add_channel_creator_member();
  68. let fixture = CreateChannelFixture::make_valid_unpulished_video_channel_for(
  69. channel_creator_member_id,
  70. Some(0),
  71. );
  72. fixture.call_and_assert_error(MSG_CREATE_CHANNEL_NOT_CONTROLLER_ACCOUNT);
  73. });
  74. }
  75. #[test]
  76. fn create_channel_handle_too_long() {
  77. TestExternalitiesBuilder::<Test>::default()
  78. .build()
  79. .execute_with(|| {
  80. let channel_creator_member_id = add_channel_creator_member();
  81. let mut fixture = CreateChannelFixture::make_valid_unpulished_video_channel_for(
  82. channel_creator_member_id,
  83. None,
  84. );
  85. fixture.channel_handle =
  86. generate_too_long_length_buffer(&ChannelHandleConstraint::get());
  87. fixture.call_and_assert_error(MSG_CHANNEL_HANDLE_TOO_LONG);
  88. });
  89. }
  90. #[test]
  91. fn create_channel_handle_too_short() {
  92. TestExternalitiesBuilder::<Test>::default()
  93. .build()
  94. .execute_with(|| {
  95. let channel_creator_member_id = add_channel_creator_member();
  96. let mut fixture = CreateChannelFixture::make_valid_unpulished_video_channel_for(
  97. channel_creator_member_id,
  98. None,
  99. );
  100. fixture.channel_handle =
  101. generate_too_short_length_buffer(&ChannelHandleConstraint::get());
  102. fixture.call_and_assert_error(MSG_CHANNEL_HANDLE_TOO_SHORT);
  103. });
  104. }
  105. #[test]
  106. fn create_channel_description_too_long() {
  107. TestExternalitiesBuilder::<Test>::default()
  108. .build()
  109. .execute_with(|| {
  110. let channel_creator_member_id = add_channel_creator_member();
  111. let mut fixture = CreateChannelFixture::make_valid_unpulished_video_channel_for(
  112. channel_creator_member_id,
  113. None,
  114. );
  115. fixture.description = Some(generate_too_long_length_buffer(
  116. &ChannelDescriptionConstraint::get(),
  117. ));
  118. fixture.call_and_assert_error(MSG_CHANNEL_DESCRIPTION_TOO_LONG);
  119. });
  120. }
  121. #[test]
  122. fn create_channel_description_too_short() {
  123. TestExternalitiesBuilder::<Test>::default()
  124. .build()
  125. .execute_with(|| {
  126. let channel_creator_member_id = add_channel_creator_member();
  127. let mut fixture = CreateChannelFixture::make_valid_unpulished_video_channel_for(
  128. channel_creator_member_id,
  129. None,
  130. );
  131. fixture.description = Some(generate_too_short_length_buffer(
  132. &ChannelDescriptionConstraint::get(),
  133. ));
  134. fixture.call_and_assert_error(MSG_CHANNEL_DESCRIPTION_TOO_SHORT);
  135. });
  136. }
  137. #[test]
  138. fn transfer_channel_ownership_success() {
  139. TestExternalitiesBuilder::<Test>::default()
  140. .build()
  141. .execute_with(|| {
  142. // Add channel creator as member
  143. let channel_creator_member_root_and_controller_account_1 = 1111;
  144. let channel_creator_member_root_and_controller_account_2 = 2222;
  145. let channel_creator_member_id_1 = add_member(
  146. channel_creator_member_root_and_controller_account_1,
  147. to_vec(CHANNEL_CREATOR_HANDLE),
  148. );
  149. let channel_creator_member_id_2 = add_member(
  150. channel_creator_member_root_and_controller_account_2,
  151. to_vec(CHANNEL_CREATOR_HANDLE2),
  152. );
  153. let create_channel_fixture =
  154. CreateChannelFixture::make_valid_unpulished_video_channel_for(
  155. channel_creator_member_id_1,
  156. None,
  157. );
  158. let channel_id = create_channel_fixture.call_and_assert_success();
  159. let original_channel = ChannelById::<Test>::get(channel_id);
  160. let new_role_account = 3333;
  161. let transfer_result = ContentWorkingGroup::transfer_channel_ownership(
  162. Origin::signed(create_channel_fixture.channel_creator_role_account),
  163. channel_id,
  164. channel_creator_member_id_2,
  165. new_role_account,
  166. );
  167. assert_ok!(transfer_result);
  168. let updated_channel = ChannelById::<Test>::get(channel_id);
  169. assert_eq!(
  170. updated_channel,
  171. Channel {
  172. owner: channel_creator_member_id_2,
  173. role_account: new_role_account,
  174. ..original_channel
  175. }
  176. );
  177. });
  178. }
  179. #[test]
  180. fn update_channel_as_owner_success() {}
  181. struct UpdateChannelAsCurationActorFixture {
  182. pub origin: Origin,
  183. pub curation_actor: CurationActor<CuratorId<Test>>,
  184. pub new_verified: Option<bool>,
  185. pub new_description: Option<OptionalText>,
  186. pub new_curation_status: Option<ChannelCurationStatus>,
  187. }
  188. impl UpdateChannelAsCurationActorFixture {
  189. fn update_channel_as_curation_actor(
  190. &self,
  191. channel_id: ChannelId<Test>,
  192. ) -> Result<(), &'static str> {
  193. ContentWorkingGroup::update_channel_as_curation_actor(
  194. self.origin.clone(),
  195. self.curation_actor.clone(),
  196. channel_id,
  197. self.new_verified,
  198. self.new_curation_status,
  199. )
  200. }
  201. pub fn call_and_assert_success(&self, channel_id: ChannelId<Test>) {
  202. let old_channel = ChannelById::<Test>::get(channel_id);
  203. let upd_verified = self.new_verified.unwrap_or(old_channel.verified);
  204. let upd_description = self
  205. .new_description
  206. .clone()
  207. .unwrap_or(old_channel.description);
  208. let upd_curation_status = self
  209. .new_curation_status
  210. .unwrap_or(old_channel.curation_status);
  211. let expected_updated_channel = Channel {
  212. verified: upd_verified,
  213. handle: old_channel.handle,
  214. title: old_channel.title,
  215. description: upd_description,
  216. avatar: old_channel.avatar,
  217. banner: old_channel.banner,
  218. content: old_channel.content,
  219. owner: old_channel.owner,
  220. role_account: old_channel.role_account,
  221. publication_status: old_channel.publication_status,
  222. curation_status: upd_curation_status,
  223. created: old_channel.created,
  224. principal_id: old_channel.principal_id,
  225. };
  226. // Call and check result
  227. let call_result = self.update_channel_as_curation_actor(channel_id);
  228. assert_eq!(call_result, Ok(()));
  229. // Event triggered
  230. let event_channel_id = Self::get_event_deposited();
  231. assert_eq!(event_channel_id, channel_id);
  232. // Channel has been updated correctly
  233. assert!(ChannelById::<Test>::exists(channel_id));
  234. let updated_channel = ChannelById::<Test>::get(channel_id);
  235. assert_eq!(updated_channel, expected_updated_channel);
  236. }
  237. fn get_event_deposited() -> crate::ChannelId<Test> {
  238. if let mock::TestEvent::lib(ref x) = System::events().last().unwrap().event {
  239. if let crate::RawEvent::ChannelUpdatedByCurationActor(ref channel_id) = x {
  240. return channel_id.clone();
  241. } else {
  242. panic!("Event was not ChannelUpdatedByCurationActor.")
  243. }
  244. } else {
  245. panic!("No event deposited.")
  246. }
  247. }
  248. }
  249. #[test]
  250. fn update_channel_as_curation_actor_success() {
  251. TestExternalitiesBuilder::<Test>::default()
  252. .build()
  253. .execute_with(|| {
  254. // Add lead and hire curator
  255. let curator_params = AddMemberAndApplyOnOpeningParams::new(
  256. 2222,
  257. to_vec("yoyoyo0"), // generate_valid_length_buffer(&ChannelHandleConstraint::get()),
  258. 2222 * 2,
  259. generate_valid_length_buffer(&CuratorApplicationHumanReadableText::get()),
  260. );
  261. // Hire curator
  262. let setup_and_fill_opening_result =
  263. setup_and_fill_opening(&vec![FillOpeningApplicantParams::new(
  264. curator_params.clone(),
  265. true,
  266. )]);
  267. let curator_id = match setup_and_fill_opening_result.application_outomes[0] {
  268. FillOpeningApplicantOutcome::Hired { curator_id } => curator_id,
  269. _ => panic!(),
  270. };
  271. // Make channel
  272. let channel_creator_member_id = add_channel_creator_member();
  273. let channel_id = channel_creator_member_id;
  274. CreateChannelFixture::make_valid_unpulished_video_channel_for(
  275. channel_creator_member_id,
  276. None,
  277. )
  278. .call_and_assert_success();
  279. // Update channel as curator
  280. UpdateChannelAsCurationActorFixture {
  281. origin: Origin::signed(curator_params.curator_applicant_role_account),
  282. curation_actor: CurationActor::Curator(curator_id),
  283. new_verified: Some(true),
  284. new_description: None, // don't touch!
  285. new_curation_status: Some(ChannelCurationStatus::Censored),
  286. }
  287. .call_and_assert_success(channel_id);
  288. });
  289. }
  290. #[test]
  291. fn add_curator_opening_success() {
  292. TestExternalitiesBuilder::<Test>::default()
  293. .build()
  294. .execute_with(|| {
  295. /*
  296. * Setup
  297. */
  298. add_member_and_set_as_lead();
  299. let expected_opening_id = hiring::NextOpeningId::<Test>::get();
  300. let expected_curator_opening_id = NextCuratorOpeningId::<Test>::get();
  301. /*
  302. * Test
  303. */
  304. // Add opening
  305. let activate_at = hiring::ActivateOpeningAt::ExactBlock(34);
  306. let human_readable_text =
  307. generate_valid_length_buffer(&OpeningHumanReadableText::get());
  308. assert_eq!(
  309. ContentWorkingGroup::add_curator_opening(
  310. Origin::signed(LEAD_ROLE_ACCOUNT),
  311. activate_at.clone(),
  312. get_baseline_opening_policy(),
  313. human_readable_text.clone()
  314. )
  315. .unwrap(),
  316. ()
  317. );
  318. assert_eq!(
  319. get_last_event_or_panic(),
  320. crate::RawEvent::CuratorOpeningAdded(expected_curator_opening_id)
  321. );
  322. // Assert that given opening id has been added,
  323. // and has the right properties.
  324. assert!(crate::CuratorOpeningById::<Test>::exists(
  325. expected_curator_opening_id
  326. ));
  327. let created_curator_opening =
  328. crate::CuratorOpeningById::<Test>::get(expected_curator_opening_id);
  329. let expected_curator_opening = CuratorOpening {
  330. opening_id: expected_opening_id,
  331. curator_applications: BTreeSet::new(),
  332. policy_commitment: get_baseline_opening_policy(),
  333. };
  334. assert_eq!(created_curator_opening, expected_curator_opening);
  335. // Assert that next id incremented.
  336. assert_eq!(
  337. crate::NextCuratorOpeningId::<Test>::get(),
  338. expected_opening_id + 1
  339. );
  340. /*
  341. * TODO: add assertion abouot side-effect in hiring module,
  342. * this is where state of application has fundamentally changed.
  343. */
  344. });
  345. }
  346. #[test]
  347. fn accept_curator_applications_success() {
  348. TestExternalitiesBuilder::<Test>::default()
  349. .build()
  350. .execute_with(|| {
  351. /*
  352. * Setup
  353. */
  354. add_member_and_set_as_lead();
  355. let curator_opening_id = add_curator_opening();
  356. /*
  357. * Test
  358. */
  359. assert_eq!(
  360. ContentWorkingGroup::accept_curator_applications(
  361. Origin::signed(LEAD_ROLE_ACCOUNT),
  362. curator_opening_id
  363. )
  364. .unwrap(),
  365. ()
  366. );
  367. assert_eq!(
  368. get_last_event_or_panic(),
  369. crate::RawEvent::AcceptedCuratorApplications(curator_opening_id)
  370. )
  371. /*
  372. * TODO: add assertion abouot side-effect in hiring module,
  373. * this is where state of application has fundamentally changed.
  374. */
  375. });
  376. }
  377. #[test]
  378. fn begin_curator_applicant_review_success() {
  379. TestExternalitiesBuilder::<Test>::default()
  380. .build()
  381. .execute_with(|| {
  382. /*
  383. * Setup
  384. */
  385. let normal_opening_constructed = setup_normal_accepting_opening();
  386. let _ = add_member_and_apply_on_opening(
  387. normal_opening_constructed.curator_opening_id,
  388. 333,
  389. to_vec("CuratorWannabe"),
  390. 11111,
  391. generate_valid_length_buffer(&CuratorApplicationHumanReadableText::get()),
  392. );
  393. /*
  394. * Test
  395. */
  396. assert_eq!(
  397. ContentWorkingGroup::begin_curator_applicant_review(
  398. Origin::signed(LEAD_ROLE_ACCOUNT),
  399. normal_opening_constructed.curator_opening_id
  400. )
  401. .unwrap(),
  402. ()
  403. );
  404. assert_eq!(
  405. get_last_event_or_panic(),
  406. crate::RawEvent::BeganCuratorApplicationReview(
  407. normal_opening_constructed.curator_opening_id
  408. )
  409. );
  410. /*
  411. * TODO: add assertion abouot side-effect in hiring module,
  412. * this is where state of application has fundamentally changed.
  413. */
  414. // Assert opening is in opening stage... hiring::ActiveOpeningStage::ReviewPeriod
  415. let opening =
  416. <hiring::OpeningById<Test>>::get(&normal_opening_constructed.curator_opening_id);
  417. match opening.stage {
  418. hiring::OpeningStage::Active { stage, .. } => {
  419. match stage {
  420. hiring::ActiveOpeningStage::ReviewPeriod {
  421. started_review_period_at_block,
  422. ..
  423. } => {
  424. /* OK */
  425. // assert_eq!(started_accepting_applicants_at_block, 0);
  426. assert_eq!(started_review_period_at_block, System::block_number());
  427. }
  428. _ => panic!("ActiveOpeningStage must be in ReviewPeriod"),
  429. }
  430. }
  431. _ => panic!("OpeningStage must be Active"),
  432. };
  433. });
  434. }
  435. #[test]
  436. fn fill_curator_opening_success() {
  437. TestExternalitiesBuilder::<Test>::default()
  438. .build()
  439. .execute_with(|| {
  440. /*
  441. * Setup
  442. */
  443. let applicants = vec![
  444. FillOpeningApplicantParams::new(
  445. AddMemberAndApplyOnOpeningParams::new(
  446. 2222,
  447. to_vec("yoyoyo0"), // generate_valid_length_buffer(&ChannelHandleConstraint::get()),
  448. 2222 * 2,
  449. generate_valid_length_buffer(&CuratorApplicationHumanReadableText::get()),
  450. ),
  451. true,
  452. ),
  453. FillOpeningApplicantParams::new(
  454. AddMemberAndApplyOnOpeningParams::new(
  455. 3333,
  456. to_vec("yoyoyo1"), // generate_valid_length_buffer(&ChannelHandleConstraint::get()),
  457. 3333 * 2,
  458. generate_valid_length_buffer(&CuratorApplicationHumanReadableText::get()),
  459. ),
  460. true,
  461. ),
  462. FillOpeningApplicantParams::new(
  463. AddMemberAndApplyOnOpeningParams::new(
  464. 5555,
  465. to_vec("yoyoyo2"), // generate_valid_length_buffer(&ChannelHandleConstraint::get()),
  466. 5555 * 2,
  467. generate_valid_length_buffer(&CuratorApplicationHumanReadableText::get()),
  468. ),
  469. false,
  470. ),
  471. FillOpeningApplicantParams::new(
  472. AddMemberAndApplyOnOpeningParams::new(
  473. 6666,
  474. to_vec("yoyoyo3"), // generate_valid_length_buffer(&ChannelHandleConstraint::get()),
  475. 6666 * 2,
  476. generate_valid_length_buffer(&CuratorApplicationHumanReadableText::get()),
  477. ),
  478. true,
  479. ),
  480. ];
  481. /*
  482. * Exercise and assert
  483. */
  484. setup_and_fill_opening(&applicants);
  485. });
  486. }
  487. #[test]
  488. fn withdraw_curator_application_success() {
  489. TestExternalitiesBuilder::<Test>::default()
  490. .build()
  491. .execute_with(|| {
  492. /*
  493. * Setup
  494. */
  495. let normal_opening_constructed = setup_normal_accepting_opening();
  496. let curator_applicant_root_and_controller_account = 333;
  497. let curator_applicant_role_account = 11111;
  498. let human_readable_text =
  499. generate_valid_length_buffer(&CuratorApplicationHumanReadableText::get());
  500. let result = add_member_and_apply_on_opening(
  501. normal_opening_constructed.curator_opening_id,
  502. curator_applicant_root_and_controller_account,
  503. to_vec("CuratorWannabe"),
  504. curator_applicant_role_account,
  505. human_readable_text,
  506. );
  507. /*
  508. * Test
  509. */
  510. assert_eq!(
  511. ContentWorkingGroup::withdraw_curator_application(
  512. Origin::signed(curator_applicant_role_account),
  513. result.curator_application_id
  514. )
  515. .unwrap(),
  516. ()
  517. );
  518. // Event was triggered
  519. assert_eq!(
  520. get_last_event_or_panic(),
  521. crate::RawEvent::CuratorApplicationWithdrawn(result.curator_application_id)
  522. );
  523. /*
  524. * TODO: add assertion abouot side-effect in hiring module,
  525. * this is where state of application has fundamentally changed.
  526. */
  527. });
  528. }
  529. #[test]
  530. fn terminate_curator_application_success() {
  531. TestExternalitiesBuilder::<Test>::default()
  532. .build()
  533. .execute_with(|| {
  534. /*
  535. * Setup
  536. */
  537. let normal_opening_constructed = setup_normal_accepting_opening();
  538. let result = add_member_and_apply_on_opening(
  539. normal_opening_constructed.curator_opening_id,
  540. 333,
  541. to_vec("CuratorWannabe"),
  542. 11111,
  543. generate_valid_length_buffer(&CuratorApplicationHumanReadableText::get()),
  544. );
  545. /*
  546. * Test
  547. */
  548. assert_eq!(
  549. ContentWorkingGroup::terminate_curator_application(
  550. Origin::signed(LEAD_ROLE_ACCOUNT),
  551. normal_opening_constructed.curator_opening_id
  552. )
  553. .unwrap(),
  554. ()
  555. );
  556. assert_eq!(
  557. get_last_event_or_panic(),
  558. crate::RawEvent::CuratorApplicationTerminated(result.curator_application_id)
  559. );
  560. /*
  561. * TODO: add assertion abouot side-effect in hiring module,
  562. * this is where state of application has fundamentally changed.
  563. */
  564. });
  565. }
  566. #[test]
  567. fn apply_on_curator_opening_success() {
  568. TestExternalitiesBuilder::<Test>::default()
  569. .build()
  570. .execute_with(|| {
  571. /*
  572. * Setup
  573. */
  574. let normal_opening_constructed = setup_normal_accepting_opening();
  575. // Add curator membership
  576. let curator_applicant_root_and_controller_account = 72618;
  577. let curator_applicant_member_id = add_member(
  578. curator_applicant_root_and_controller_account,
  579. to_vec("IwillTrytoapplyhere"),
  580. );
  581. let curator_applicant_role_account = 8881111;
  582. let role_stake_balance = get_baseline_opening_policy()
  583. .role_staking_policy
  584. .unwrap()
  585. .amount;
  586. let application_stake_balance = get_baseline_opening_policy()
  587. .application_staking_policy
  588. .unwrap()
  589. .amount;
  590. let total_balance = role_stake_balance + application_stake_balance;
  591. let source_account = curator_applicant_root_and_controller_account;
  592. // Credit staking source account
  593. let _ = balances::Module::<Test>::deposit_creating(&source_account, total_balance);
  594. let human_readable_text = generate_valid_length_buffer(&ChannelHandleConstraint::get());
  595. let expected_curator_application_id = NextCuratorApplicationId::<Test>::get();
  596. let old_curator_opening =
  597. CuratorOpeningById::<Test>::get(normal_opening_constructed.curator_opening_id);
  598. let new_curator_application_id = NextCuratorApplicationId::<Test>::get();
  599. /*
  600. * Test
  601. */
  602. assert_eq!(
  603. ContentWorkingGroup::apply_on_curator_opening(
  604. Origin::signed(curator_applicant_root_and_controller_account),
  605. curator_applicant_member_id,
  606. normal_opening_constructed.curator_opening_id,
  607. curator_applicant_role_account,
  608. Some(role_stake_balance),
  609. Some(application_stake_balance),
  610. human_readable_text
  611. )
  612. .unwrap(),
  613. ()
  614. );
  615. assert_eq!(
  616. get_last_event_or_panic(),
  617. crate::RawEvent::AppliedOnCuratorOpening(
  618. normal_opening_constructed.curator_opening_id,
  619. new_curator_application_id
  620. )
  621. );
  622. assert!(CuratorApplicationById::<Test>::exists(
  623. new_curator_application_id
  624. ));
  625. // Assert that appropriate application has been added
  626. let new_curator_application =
  627. CuratorApplicationById::<Test>::get(new_curator_application_id);
  628. let expected_curator_application = CuratorApplication {
  629. role_account: curator_applicant_role_account,
  630. curator_opening_id: normal_opening_constructed.curator_opening_id,
  631. member_id: curator_applicant_member_id,
  632. application_id: expected_curator_application_id,
  633. };
  634. assert_eq!(expected_curator_application, new_curator_application);
  635. // Assert that the opening has had the application added to application list
  636. let mut singleton = BTreeSet::new(); // Unavoidable mutable, BTreeSet can only be populated this way.
  637. singleton.insert(new_curator_application_id);
  638. let new_curator_applications = old_curator_opening
  639. .curator_applications
  640. .union(&singleton)
  641. .cloned()
  642. .collect();
  643. let expected_curator_opening = CuratorOpening {
  644. curator_applications: new_curator_applications,
  645. ..old_curator_opening
  646. };
  647. let new_curator_opening =
  648. CuratorOpeningById::<Test>::get(normal_opening_constructed.curator_opening_id);
  649. assert_eq!(expected_curator_opening, new_curator_opening);
  650. });
  651. }
  652. #[test]
  653. fn multiple_applications_by_same_member_to_opening_fails() {
  654. TestExternalitiesBuilder::<Test>::default()
  655. .build()
  656. .execute_with(|| {
  657. /*
  658. * Setup
  659. */
  660. let normal_opening_constructed = setup_normal_accepting_opening();
  661. // Add curator membership
  662. let curator_applicant_root_and_controller_account = 72618;
  663. let curator_applicant_member_id = add_member(
  664. curator_applicant_root_and_controller_account,
  665. to_vec("IwillTrytoapplyhere"),
  666. );
  667. let curator_applicant_role_account = 8881111;
  668. let role_stake_balance = get_baseline_opening_policy()
  669. .role_staking_policy
  670. .unwrap()
  671. .amount;
  672. let application_stake_balance = get_baseline_opening_policy()
  673. .application_staking_policy
  674. .unwrap()
  675. .amount;
  676. let total_balance = role_stake_balance + application_stake_balance;
  677. let source_account = curator_applicant_root_and_controller_account;
  678. // Credit staking source account with enough funds for two applications,
  679. // because we don't want our second application to fail for lack of funds
  680. let _ = balances::Module::<Test>::deposit_creating(&source_account, total_balance * 2);
  681. let human_readable_text = generate_valid_length_buffer(&ChannelHandleConstraint::get());
  682. /*
  683. * Test
  684. */
  685. // First application should work
  686. assert_ok!(ContentWorkingGroup::apply_on_curator_opening(
  687. Origin::signed(curator_applicant_root_and_controller_account),
  688. curator_applicant_member_id,
  689. normal_opening_constructed.curator_opening_id,
  690. curator_applicant_role_account,
  691. Some(role_stake_balance),
  692. Some(application_stake_balance),
  693. human_readable_text.clone()
  694. ));
  695. // Second application should fail since
  696. // first application is still active
  697. assert_err!(
  698. ContentWorkingGroup::apply_on_curator_opening(
  699. Origin::signed(curator_applicant_root_and_controller_account),
  700. curator_applicant_member_id,
  701. normal_opening_constructed.curator_opening_id,
  702. curator_applicant_role_account,
  703. Some(role_stake_balance),
  704. Some(application_stake_balance),
  705. human_readable_text
  706. ),
  707. MSG_MEMBER_HAS_ACTIVE_APPLICATION_ON_OPENING
  708. );
  709. });
  710. }
  711. struct UpdateCuratorRoleAccountFixture {
  712. pub origin: Origin,
  713. pub member_id: <Test as members::Trait>::MemberId,
  714. pub curator_id: CuratorId<Test>,
  715. pub new_role_account: <Test as system::Trait>::AccountId,
  716. }
  717. impl UpdateCuratorRoleAccountFixture {
  718. fn call(&self) -> Result<(), &'static str> {
  719. ContentWorkingGroup::update_curator_role_account(
  720. self.origin.clone(),
  721. self.member_id,
  722. self.curator_id,
  723. self.new_role_account,
  724. )
  725. }
  726. pub fn call_and_assert_success(&self) {
  727. let original_curator = CuratorById::<Test>::get(self.curator_id);
  728. let call_result = self.call();
  729. assert_eq!(call_result, Ok(()));
  730. let updated_curator = CuratorById::<Test>::get(self.curator_id);
  731. assert_eq!(
  732. crate::Curator {
  733. role_account: self.new_role_account,
  734. ..original_curator
  735. },
  736. updated_curator
  737. );
  738. let (event_curator_id, event_new_role_account) =
  739. if let mock::TestEvent::lib(ref x) = System::events().last().unwrap().event {
  740. if let crate::RawEvent::CuratorRoleAccountUpdated(
  741. ref curator_id,
  742. ref new_role_account,
  743. ) = x
  744. {
  745. (curator_id.clone(), new_role_account.clone())
  746. } else {
  747. panic!("Event was not CuratorRoleAccountUpdated.")
  748. }
  749. } else {
  750. panic!("No event deposited.")
  751. };
  752. assert_eq!(self.curator_id, event_curator_id);
  753. assert_eq!(self.new_role_account, event_new_role_account);
  754. }
  755. }
  756. #[test]
  757. fn update_curator_role_account_success() {
  758. TestExternalitiesBuilder::<Test>::default()
  759. .build()
  760. .execute_with(|| {
  761. let result = setup_lead_and_hire_curator();
  762. let fixture = UpdateCuratorRoleAccountFixture {
  763. origin: Origin::signed(
  764. result
  765. .curator_params()
  766. .curator_applicant_root_and_controller_account,
  767. ),
  768. member_id: result.curator_member_id(),
  769. curator_id: result.curator_id(),
  770. new_role_account: 777777,
  771. };
  772. fixture.call_and_assert_success();
  773. });
  774. }
  775. struct UpdateCuratorRewardAccountFixture {
  776. pub origin: Origin,
  777. pub curator_id: CuratorId<Test>,
  778. pub new_reward_account: <Test as system::Trait>::AccountId,
  779. }
  780. impl UpdateCuratorRewardAccountFixture {
  781. #[allow(dead_code)] // delete if the method is unnecessary
  782. fn call(&self) -> Result<(), &'static str> {
  783. ContentWorkingGroup::update_curator_reward_account(
  784. self.origin.clone(),
  785. self.curator_id,
  786. self.new_reward_account,
  787. )
  788. }
  789. #[allow(dead_code)] // delete if the method is unnecessary
  790. pub fn call_and_assert_success(&self) {
  791. let _original_curator = CuratorById::<Test>::get(self.curator_id);
  792. let call_result = self.call();
  793. assert_eq!(call_result, Ok(()));
  794. /*
  795. Actually checking new reward account requires checking call to token mint module, but we cannot do that properly yet.
  796. */
  797. let (event_curator_id, event_reward_account) =
  798. if let mock::TestEvent::lib(ref x) = System::events().last().unwrap().event {
  799. if let crate::RawEvent::CuratorRewardAccountUpdated(
  800. ref curator_id,
  801. ref reward_account,
  802. ) = x
  803. {
  804. (curator_id.clone(), reward_account.clone())
  805. } else {
  806. panic!("Event was not CuratorRewardAccountUpdated.")
  807. }
  808. } else {
  809. panic!("No event deposited.")
  810. };
  811. assert_eq!(self.curator_id, event_curator_id);
  812. assert_eq!(self.new_reward_account, event_reward_account);
  813. }
  814. }
  815. #[test]
  816. fn update_curator_reward_account_success() {
  817. TestExternalitiesBuilder::<Test>::default()
  818. .build()
  819. .execute_with(|| {
  820. let result = setup_lead_and_hire_curator();
  821. let _fixture = UpdateCuratorRewardAccountFixture {
  822. origin: Origin::signed(result.curator_params().curator_applicant_role_account),
  823. curator_id: result.curator_id(),
  824. new_reward_account: 123321,
  825. };
  826. // TEMPORARILY DISABLED
  827. //fixture.call_and_assert_success();
  828. });
  829. }
  830. struct LeaveCuratorRoleFixture {
  831. pub origin: Origin,
  832. pub curator_id: CuratorId<Test>,
  833. pub rationale_text: Vec<u8>,
  834. }
  835. impl LeaveCuratorRoleFixture {
  836. fn call(&self) -> Result<(), &'static str> {
  837. ContentWorkingGroup::leave_curator_role(
  838. self.origin.clone(),
  839. self.curator_id,
  840. self.rationale_text.clone(),
  841. )
  842. }
  843. pub fn call_and_assert_success(&self) {
  844. let original_curator = CuratorById::<Test>::get(self.curator_id);
  845. let call_result = self.call();
  846. assert_eq!(call_result, Ok(()));
  847. let expected_curator = Curator {
  848. stage: CuratorRoleStage::Unstaking(CuratorExitSummary::new(
  849. &CuratorExitInitiationOrigin::Curator,
  850. &1,
  851. &self.rationale_text,
  852. )),
  853. ..(original_curator.clone())
  854. };
  855. let updated_curator = CuratorById::<Test>::get(self.curator_id);
  856. assert_eq!(updated_curator, expected_curator);
  857. assert_eq!(
  858. get_last_event_or_panic(),
  859. crate::RawEvent::CuratorUnstaking(self.curator_id)
  860. );
  861. // Tracking unstaking
  862. let curator_role_stake_id = original_curator.role_stake_profile.unwrap().stake_id;
  863. assert!(UnstakerByStakeId::<Test>::exists(curator_role_stake_id));
  864. let unstaker = UnstakerByStakeId::<Test>::get(curator_role_stake_id);
  865. assert_eq!(unstaker, WorkingGroupUnstaker::Curator(self.curator_id));
  866. /*
  867. * TODO: Missing checks to calls to
  868. * recurringrewards, stake
  869. */
  870. }
  871. }
  872. #[test]
  873. fn leave_curator_role_success() {
  874. TestExternalitiesBuilder::<Test>::default()
  875. .build()
  876. .execute_with(|| {
  877. let result = setup_lead_and_hire_curator();
  878. let fixture = LeaveCuratorRoleFixture {
  879. origin: Origin::signed(result.curator_params().curator_applicant_role_account),
  880. curator_id: result.curator_id(),
  881. rationale_text: "I am sick of this horrible thing".as_bytes().to_vec(),
  882. };
  883. fixture.call_and_assert_success();
  884. });
  885. }
  886. struct TerminateCuratorRoleFixture {
  887. pub origin: Origin,
  888. pub curator_id: CuratorId<Test>,
  889. pub rationale_text: Vec<u8>,
  890. }
  891. impl TerminateCuratorRoleFixture {
  892. fn call(&self) -> Result<(), &'static str> {
  893. ContentWorkingGroup::terminate_curator_role(
  894. self.origin.clone(),
  895. self.curator_id,
  896. self.rationale_text.clone(),
  897. )
  898. }
  899. pub fn call_and_assert_success(&self) {
  900. let original_curator = CuratorById::<Test>::get(self.curator_id);
  901. let call_result = self.call();
  902. assert_eq!(call_result, Ok(()));
  903. let expected_curator = Curator {
  904. stage: CuratorRoleStage::Unstaking(CuratorExitSummary::new(
  905. &CuratorExitInitiationOrigin::Lead,
  906. &1,
  907. &self.rationale_text,
  908. )),
  909. ..(original_curator.clone())
  910. };
  911. let updated_curator = CuratorById::<Test>::get(self.curator_id);
  912. assert_eq!(updated_curator, expected_curator);
  913. assert_eq!(
  914. get_last_event_or_panic(),
  915. crate::RawEvent::CuratorUnstaking(self.curator_id)
  916. );
  917. // Tracking unstaking
  918. let curator_role_stake_id = original_curator.role_stake_profile.unwrap().stake_id;
  919. assert!(UnstakerByStakeId::<Test>::exists(curator_role_stake_id));
  920. let unstaker = UnstakerByStakeId::<Test>::get(curator_role_stake_id);
  921. assert_eq!(unstaker, WorkingGroupUnstaker::Curator(self.curator_id));
  922. /*
  923. * TODO: Missing checks to calls to
  924. * recurringrewards, stake
  925. */
  926. }
  927. }
  928. #[test]
  929. fn terminate_curator_role_success() {
  930. TestExternalitiesBuilder::<Test>::default()
  931. .build()
  932. .execute_with(|| {
  933. let result = setup_lead_and_hire_curator();
  934. let fixture = TerminateCuratorRoleFixture {
  935. origin: Origin::signed(LEAD_ROLE_ACCOUNT),
  936. curator_id: result.curator_id(),
  937. rationale_text: "This curator is a joke!".as_bytes().to_vec(),
  938. };
  939. fixture.call_and_assert_success();
  940. });
  941. }
  942. struct SetLeadFixture {
  943. pub origin: Origin,
  944. pub member_id: <Test as members::Trait>::MemberId,
  945. pub new_role_account: <Test as system::Trait>::AccountId,
  946. }
  947. impl SetLeadFixture {
  948. fn call(&self) -> Result<(), &'static str> {
  949. ContentWorkingGroup::replace_lead(
  950. self.origin.clone(),
  951. Some((self.member_id, self.new_role_account)),
  952. )
  953. }
  954. pub fn call_and_assert_success(&self) {
  955. let original_next_lead_id = NextLeadId::<Test>::get();
  956. let call_result = self.call();
  957. assert_eq!(call_result, Ok(()));
  958. let updated_next_lead_id = NextLeadId::<Test>::get();
  959. assert_eq!(original_next_lead_id + 1, updated_next_lead_id);
  960. let new_lead_id = if let Some(id) = CurrentLeadId::<Test>::get() {
  961. id
  962. } else {
  963. panic!("Lead not set when it must be.")
  964. };
  965. let new_lead = LeadById::<Test>::get(new_lead_id);
  966. let expected_new_lead = Lead {
  967. role_account: self.new_role_account,
  968. reward_relationship: None,
  969. inducted: 1, // make dynamic later
  970. stage: LeadRoleState::Active,
  971. };
  972. assert_eq!(new_lead, expected_new_lead);
  973. assert_eq!(
  974. get_last_event_or_panic(),
  975. crate::RawEvent::LeadSet(new_lead_id)
  976. );
  977. }
  978. }
  979. #[test]
  980. fn set_lead_success() {
  981. TestExternalitiesBuilder::<Test>::default()
  982. .build()
  983. .execute_with(|| {
  984. let member_id =
  985. add_member(LEAD_ROOT_AND_CONTROLLER_ACCOUNT, to_vec(LEAD_MEMBER_HANDLE));
  986. SetLeadFixture {
  987. origin: Origin::system(system::RawOrigin::Root),
  988. member_id,
  989. new_role_account: 44444,
  990. }
  991. .call_and_assert_success();
  992. });
  993. }
  994. struct UnsetLeadFixture {
  995. pub origin: Origin,
  996. }
  997. impl UnsetLeadFixture {
  998. fn call(&self) -> Result<(), &'static str> {
  999. ContentWorkingGroup::replace_lead(self.origin.clone(), None)
  1000. }
  1001. pub fn call_and_assert_success(&self) {
  1002. let original_lead_id = CurrentLeadId::<Test>::get().unwrap();
  1003. let original_lead = LeadById::<Test>::get(original_lead_id);
  1004. let call_result = self.call();
  1005. assert_eq!(call_result, Ok(()));
  1006. assert!(CurrentLeadId::<Test>::get().is_none());
  1007. let updated_lead = LeadById::<Test>::get(original_lead_id);
  1008. let expected_updated_lead = Lead {
  1009. stage: LeadRoleState::Exited(ExitedLeadRole {
  1010. initiated_at_block_number: 1,
  1011. }),
  1012. ..original_lead
  1013. };
  1014. assert_eq!(updated_lead, expected_updated_lead);
  1015. assert_eq!(
  1016. get_last_event_or_panic(),
  1017. crate::RawEvent::LeadUnset(original_lead_id)
  1018. );
  1019. }
  1020. }
  1021. #[test]
  1022. fn unset_lead_success() {
  1023. TestExternalitiesBuilder::<Test>::default()
  1024. .build()
  1025. .execute_with(|| {
  1026. let _ = add_member_and_set_as_lead();
  1027. UnsetLeadFixture {
  1028. origin: Origin::system(system::RawOrigin::Root),
  1029. }
  1030. .call_and_assert_success();
  1031. });
  1032. }
  1033. struct UnstakedFixture {
  1034. pub stake_id: StakeId<Test>,
  1035. }
  1036. impl UnstakedFixture {
  1037. fn call(&self) {
  1038. ContentWorkingGroup::unstaked(self.stake_id);
  1039. }
  1040. pub fn call_and_assert_success(&self) {
  1041. let unstaker = UnstakerByStakeId::<Test>::get(self.stake_id);
  1042. let curator_id = if let WorkingGroupUnstaker::Curator(curator_id) = unstaker {
  1043. curator_id
  1044. } else {
  1045. panic!("Unstaker not curator")
  1046. };
  1047. let original_curator = CuratorById::<Test>::get(curator_id);
  1048. let original_exit_summary =
  1049. if let CuratorRoleStage::Unstaking(exit_summary) = (original_curator.clone()).stage {
  1050. exit_summary
  1051. } else {
  1052. panic!("Curator not unstaking")
  1053. };
  1054. self.call();
  1055. let expected_curator = Curator {
  1056. stage: CuratorRoleStage::Exited(original_exit_summary),
  1057. ..(original_curator.clone())
  1058. };
  1059. let updated_curator = CuratorById::<Test>::get(curator_id);
  1060. assert_eq!(updated_curator, expected_curator);
  1061. assert_eq!(
  1062. get_last_event_or_panic(),
  1063. crate::RawEvent::TerminatedCurator(curator_id)
  1064. );
  1065. // Unstaker gone
  1066. assert!(!UnstakerByStakeId::<Test>::exists(self.stake_id));
  1067. }
  1068. // pub fn call_and_assert_failed_result(&self, error_message: &'static str) {
  1069. // let call_result = self.call();
  1070. // assert_eq!(call_result, Err(error_message));
  1071. // }
  1072. }
  1073. #[test]
  1074. fn unstaked_curator_success() {
  1075. TestExternalitiesBuilder::<Test>::default()
  1076. .build()
  1077. .execute_with(|| {
  1078. let result = setup_lead_and_hire_curator();
  1079. TerminateCuratorRoleFixture {
  1080. origin: Origin::signed(LEAD_ROLE_ACCOUNT),
  1081. curator_id: result.curator_id(),
  1082. rationale_text: "This curator is a joke!".as_bytes().to_vec(),
  1083. }
  1084. .call_and_assert_success();
  1085. let curator_role_stake_id = CuratorById::<Test>::get(result.curator_id())
  1086. .role_stake_profile
  1087. .unwrap()
  1088. .stake_id;
  1089. UnstakedFixture {
  1090. stake_id: curator_role_stake_id,
  1091. }
  1092. .call_and_assert_success();
  1093. });
  1094. }
  1095. #[test]
  1096. fn account_can_act_as_principal_success() {}
  1097. /*
  1098. * Fixtures
  1099. */
  1100. static LEAD_ROOT_AND_CONTROLLER_ACCOUNT: <Test as system::Trait>::AccountId = 1289;
  1101. static LEAD_ROLE_ACCOUNT: <Test as system::Trait>::AccountId = 1289;
  1102. static LEAD_MEMBER_HANDLE: &str = "IamTheLead";
  1103. static CHANNEL_CREATOR_ROOT_AND_CONTROLLER_ACCOUNT: <Test as system::Trait>::AccountId = 11;
  1104. static CHANNEL_CREATOR_HANDLE: &str = "Coolcreator1";
  1105. static CHANNEL_CREATOR_HANDLE2: &str = "Coolcreator2";
  1106. fn make_generic_add_member_params() -> AddMemberAndApplyOnOpeningParams {
  1107. AddMemberAndApplyOnOpeningParams::new(
  1108. 2222,
  1109. to_vec("yoyoyo0"), // generate_valid_length_buffer(&ChannelHandleConstraint::get()),
  1110. 2222 * 2,
  1111. generate_valid_length_buffer(&CuratorApplicationHumanReadableText::get()),
  1112. )
  1113. }
  1114. /// Made into function to avoid having to clone every time we read fields
  1115. pub fn get_baseline_opening_policy(
  1116. ) -> OpeningPolicyCommitment<<Test as system::Trait>::BlockNumber, BalanceOf<Test>> {
  1117. OpeningPolicyCommitment {
  1118. application_rationing_policy: Some(hiring::ApplicationRationingPolicy {
  1119. max_active_applicants: 5,
  1120. }),
  1121. max_review_period_length: 100,
  1122. application_staking_policy: Some(hiring::StakingPolicy {
  1123. amount: 40000,
  1124. amount_mode: hiring::StakingAmountLimitMode::Exact,
  1125. crowded_out_unstaking_period_length: Some(3),
  1126. review_period_expired_unstaking_period_length: Some(22),
  1127. }),
  1128. role_staking_policy: Some(hiring::StakingPolicy {
  1129. amount: 900000,
  1130. amount_mode: hiring::StakingAmountLimitMode::AtLeast,
  1131. crowded_out_unstaking_period_length: Some(30),
  1132. review_period_expired_unstaking_period_length: Some(2),
  1133. }),
  1134. role_slashing_terms: SlashingTerms::Unslashable,
  1135. fill_opening_successful_applicant_application_stake_unstaking_period: None,
  1136. fill_opening_failed_applicant_application_stake_unstaking_period: None,
  1137. fill_opening_failed_applicant_role_stake_unstaking_period: None,
  1138. terminate_curator_application_stake_unstaking_period: None,
  1139. terminate_curator_role_stake_unstaking_period: None,
  1140. exit_curator_role_application_stake_unstaking_period: None,
  1141. exit_curator_role_stake_unstaking_period: None,
  1142. }
  1143. }
  1144. pub fn to_vec(s: &str) -> Vec<u8> {
  1145. s.as_bytes().to_vec()
  1146. }
  1147. /*
  1148. * Setups
  1149. */
  1150. //type TestSeed = u128;
  1151. /*
  1152. fn account_from_seed(TestSeed: seed) -> <Test as system::Trait>::AccountId {
  1153. }
  1154. fn vector_from_seed(TestSeed: seed) {
  1155. }
  1156. */
  1157. /*
  1158. static INITIAL_SEED_VALUE: u128 = 0;
  1159. static CURRENT_SEED: u128 = INITIAL_SEED_VALUE;
  1160. fn get_current_seed() {
  1161. }
  1162. fn update_seed() {
  1163. }
  1164. fn reset_seed() {
  1165. CURRENT_SEED: u128 = INITIAL_SEED_VALUE;
  1166. }
  1167. */
  1168. // MOVE THIS LATER WHEN fill_opening is factored out
  1169. #[derive(Clone)]
  1170. pub struct FillOpeningApplicantParams {
  1171. pub add_and_apply_params: AddMemberAndApplyOnOpeningParams,
  1172. pub hire: bool,
  1173. }
  1174. impl FillOpeningApplicantParams {
  1175. pub fn new(add_and_apply_params: AddMemberAndApplyOnOpeningParams, hire: bool) -> Self {
  1176. Self {
  1177. add_and_apply_params: add_and_apply_params.clone(),
  1178. hire: hire,
  1179. }
  1180. }
  1181. }
  1182. #[derive(Clone)]
  1183. pub struct AddMemberAndApplyOnOpeningParams {
  1184. pub curator_applicant_root_and_controller_account: <Test as system::Trait>::AccountId,
  1185. pub handle: Vec<u8>,
  1186. pub curator_applicant_role_account: <Test as system::Trait>::AccountId,
  1187. pub human_readable_text: Vec<u8>,
  1188. }
  1189. impl AddMemberAndApplyOnOpeningParams {
  1190. pub fn new(
  1191. curator_applicant_root_and_controller_account: <Test as system::Trait>::AccountId,
  1192. handle: Vec<u8>,
  1193. curator_applicant_role_account: <Test as system::Trait>::AccountId,
  1194. human_readable_text: Vec<u8>,
  1195. ) -> Self {
  1196. Self {
  1197. curator_applicant_root_and_controller_account,
  1198. handle,
  1199. curator_applicant_role_account,
  1200. human_readable_text,
  1201. }
  1202. }
  1203. }
  1204. fn add_members_and_apply_on_opening(
  1205. curator_opening_id: CuratorOpeningId<Test>,
  1206. applicants: &Vec<AddMemberAndApplyOnOpeningParams>,
  1207. ) -> Vec<NewMemberAppliedResult> {
  1208. applicants
  1209. .iter()
  1210. .cloned()
  1211. .map(|params| -> NewMemberAppliedResult {
  1212. add_member_and_apply_on_opening(
  1213. curator_opening_id,
  1214. params.curator_applicant_root_and_controller_account,
  1215. params.handle,
  1216. params.curator_applicant_role_account,
  1217. params.human_readable_text,
  1218. )
  1219. })
  1220. .collect()
  1221. }
  1222. #[derive(Clone)]
  1223. struct NewMemberAppliedResult {
  1224. pub member_id: <Test as members::Trait>::MemberId,
  1225. pub curator_application_id: crate::CuratorApplicationId<Test>,
  1226. }
  1227. fn add_member_and_apply_on_opening(
  1228. curator_opening_id: CuratorOpeningId<Test>,
  1229. curator_applicant_root_and_controller_account: <Test as system::Trait>::AccountId,
  1230. handle: Vec<u8>,
  1231. curator_applicant_role_account: <Test as system::Trait>::AccountId,
  1232. human_readable_text: Vec<u8>,
  1233. ) -> NewMemberAppliedResult {
  1234. // Make membership
  1235. let curator_applicant_member_id =
  1236. add_member(curator_applicant_root_and_controller_account, handle);
  1237. // Guarantee sufficient stake
  1238. let role_stake_balance = if let Some(policy) = get_baseline_opening_policy().role_staking_policy
  1239. {
  1240. policy.amount
  1241. } else {
  1242. 0
  1243. };
  1244. let application_stake_balance =
  1245. if let Some(policy) = get_baseline_opening_policy().application_staking_policy {
  1246. policy.amount
  1247. } else {
  1248. 0
  1249. };
  1250. let total_balance = role_stake_balance + application_stake_balance;
  1251. let source_account = curator_applicant_root_and_controller_account;
  1252. // Credit staking source account if required
  1253. if total_balance > 0 {
  1254. let _ = balances::Module::<Test>::deposit_creating(&source_account, total_balance);
  1255. }
  1256. let expected_hiring_application_id = <hiring::NextApplicationId<Test>>::get();
  1257. let old_curator_opening = CuratorOpeningById::<Test>::get(curator_opening_id);
  1258. let new_curator_application_id = NextCuratorApplicationId::<Test>::get();
  1259. /*
  1260. * Test
  1261. */
  1262. assert_eq!(
  1263. ContentWorkingGroup::apply_on_curator_opening(
  1264. Origin::signed(curator_applicant_root_and_controller_account),
  1265. curator_applicant_member_id,
  1266. curator_opening_id,
  1267. curator_applicant_role_account,
  1268. Some(role_stake_balance),
  1269. Some(application_stake_balance),
  1270. human_readable_text
  1271. )
  1272. .unwrap(),
  1273. ()
  1274. );
  1275. assert_eq!(
  1276. get_last_event_or_panic(),
  1277. crate::RawEvent::AppliedOnCuratorOpening(curator_opening_id, new_curator_application_id)
  1278. );
  1279. assert!(CuratorApplicationById::<Test>::exists(
  1280. new_curator_application_id
  1281. ));
  1282. // Assert that appropriate application has been added
  1283. let new_curator_application = CuratorApplicationById::<Test>::get(new_curator_application_id);
  1284. let expected_curator_application = CuratorApplication {
  1285. role_account: curator_applicant_role_account,
  1286. curator_opening_id: curator_opening_id,
  1287. member_id: curator_applicant_member_id,
  1288. application_id: expected_hiring_application_id,
  1289. };
  1290. assert_eq!(expected_curator_application, new_curator_application);
  1291. // Assert that the opening has had the application added to application list
  1292. let mut singleton = BTreeSet::new(); // Unavoidable mutable, BTreeSet can only be populated this way.
  1293. singleton.insert(new_curator_application_id);
  1294. let new_curator_applications = old_curator_opening
  1295. .curator_applications
  1296. .union(&singleton)
  1297. .cloned()
  1298. .collect();
  1299. let expected_curator_opening = CuratorOpening {
  1300. curator_applications: new_curator_applications,
  1301. ..old_curator_opening
  1302. };
  1303. let new_curator_opening = CuratorOpeningById::<Test>::get(curator_opening_id);
  1304. assert_eq!(expected_curator_opening, new_curator_opening);
  1305. NewMemberAppliedResult {
  1306. member_id: curator_applicant_member_id,
  1307. curator_application_id: new_curator_application_id,
  1308. }
  1309. }
  1310. struct NormalOpeningConstructed {
  1311. pub curator_opening_id: CuratorOpeningId<Test>,
  1312. pub new_member_as_lead: NewMemberAsLead,
  1313. }
  1314. fn setup_normal_opening() -> NormalOpeningConstructed {
  1315. let new_member_as_lead = add_member_and_set_as_lead();
  1316. let expected_curator_opening_id = NextCuratorOpeningId::<Test>::get();
  1317. assert_eq!(
  1318. ContentWorkingGroup::add_curator_opening(
  1319. Origin::signed(LEAD_ROLE_ACCOUNT),
  1320. hiring::ActivateOpeningAt::ExactBlock(34),
  1321. get_baseline_opening_policy(),
  1322. generate_valid_length_buffer(&OpeningHumanReadableText::get())
  1323. )
  1324. .unwrap(),
  1325. ()
  1326. );
  1327. assert_eq!(
  1328. get_last_event_or_panic(),
  1329. crate::RawEvent::CuratorOpeningAdded(expected_curator_opening_id)
  1330. );
  1331. NormalOpeningConstructed {
  1332. curator_opening_id: expected_curator_opening_id,
  1333. new_member_as_lead,
  1334. }
  1335. }
  1336. fn setup_normal_accepting_opening() -> NormalOpeningConstructed {
  1337. let normal_opening_constructed = setup_normal_opening();
  1338. assert_eq!(
  1339. ContentWorkingGroup::accept_curator_applications(
  1340. Origin::signed(LEAD_ROLE_ACCOUNT), // <== get dynamic value from somewhere else later
  1341. normal_opening_constructed.curator_opening_id
  1342. )
  1343. .unwrap(),
  1344. ()
  1345. );
  1346. normal_opening_constructed
  1347. }
  1348. struct SetupOpeningInReview {
  1349. //pub curator_opening_id: lib::CuratorOpeningId<Test>,
  1350. pub normal_opening_constructed: NormalOpeningConstructed,
  1351. pub added_members_application_result: Vec<NewMemberAppliedResult>,
  1352. }
  1353. fn setup_opening_in_review(
  1354. applicants: &Vec<AddMemberAndApplyOnOpeningParams>,
  1355. ) -> SetupOpeningInReview {
  1356. let normal_opening_constructed = setup_normal_accepting_opening();
  1357. let added_members_application_result =
  1358. add_members_and_apply_on_opening(normal_opening_constructed.curator_opening_id, applicants);
  1359. assert_eq!(
  1360. ContentWorkingGroup::begin_curator_applicant_review(
  1361. Origin::signed(LEAD_ROLE_ACCOUNT),
  1362. normal_opening_constructed.curator_opening_id
  1363. )
  1364. .unwrap(),
  1365. ()
  1366. );
  1367. // TODO: assert event stuff !!!!
  1368. SetupOpeningInReview {
  1369. normal_opening_constructed,
  1370. added_members_application_result,
  1371. }
  1372. }
  1373. enum FillOpeningApplicantOutcome {
  1374. NotHired,
  1375. Hired { curator_id: CuratorId<Test> },
  1376. }
  1377. struct SetupAndFillOpeningResult {
  1378. setup_opening_in_review: SetupOpeningInReview,
  1379. application_outomes: Vec<FillOpeningApplicantOutcome>,
  1380. }
  1381. fn setup_and_fill_opening(
  1382. applicants: &Vec<FillOpeningApplicantParams>,
  1383. ) -> SetupAndFillOpeningResult {
  1384. let setup_opening_params = applicants
  1385. .iter()
  1386. .cloned()
  1387. .map(|param| param.add_and_apply_params)
  1388. .collect::<Vec<_>>();
  1389. let setup_opening_in_review = setup_opening_in_review(&setup_opening_params);
  1390. let curator_opening = CuratorOpeningById::<Test>::get(
  1391. setup_opening_in_review
  1392. .normal_opening_constructed
  1393. .curator_opening_id,
  1394. );
  1395. // Set whom to hire
  1396. let applicants_to_hire_iter = applicants.iter().filter(|params| params.hire);
  1397. let num_applicants_hired = applicants_to_hire_iter.cloned().count();
  1398. //let num_applicants_not_to_hire = (applicants.len() - num_applicants_hired) as usize;
  1399. let hired_applicant_and_result = setup_opening_in_review
  1400. .added_members_application_result
  1401. .iter()
  1402. .zip(applicants.iter())
  1403. .filter(|(_, fill_opening_applicant_params)| fill_opening_applicant_params.hire)
  1404. .collect::<Vec<_>>();
  1405. let successful_curator_application_ids = hired_applicant_and_result
  1406. .iter()
  1407. .map(|(new_member_applied_result, _)| new_member_applied_result.curator_application_id)
  1408. .collect::<BTreeSet<_>>();
  1409. // Remember original id counters
  1410. let original_next_curator_id = NextCuratorId::<Test>::get();
  1411. let original_next_principal_id = NextPrincipalId::<Test>::get();
  1412. /*
  1413. * Call
  1414. */
  1415. assert_eq!(
  1416. ContentWorkingGroup::fill_curator_opening(
  1417. Origin::signed(LEAD_ROLE_ACCOUNT),
  1418. setup_opening_in_review
  1419. .normal_opening_constructed
  1420. .curator_opening_id,
  1421. successful_curator_application_ids.clone(),
  1422. None
  1423. ),
  1424. Ok(())
  1425. );
  1426. /*
  1427. * Asserts
  1428. */
  1429. let successful_curator_application_id_to_curator_id = successful_curator_application_ids
  1430. .iter()
  1431. .enumerate()
  1432. .map(
  1433. |(index, item)| -> (CuratorApplicationId<Test>, CuratorId<Test>) {
  1434. let curator_id = original_next_curator_id + (index as CuratorId<Test>);
  1435. (*item, curator_id)
  1436. },
  1437. )
  1438. .collect::<BTreeMap<_, _>>();
  1439. assert_eq!(
  1440. get_last_event_or_panic(),
  1441. crate::RawEvent::CuratorOpeningFilled(
  1442. setup_opening_in_review
  1443. .normal_opening_constructed
  1444. .curator_opening_id,
  1445. successful_curator_application_id_to_curator_id
  1446. )
  1447. );
  1448. // The right number of curators have been created
  1449. let num_curators_created = NextCuratorId::<Test>::get() - original_next_curator_id;
  1450. assert_eq!(num_curators_created, (num_applicants_hired as u64));
  1451. // The right numbe of prinipals were created
  1452. let num_principals_created = NextPrincipalId::<Test>::get() - original_next_principal_id;
  1453. assert_eq!(num_principals_created, (num_applicants_hired as u64));
  1454. // Inspect all expected curators and principal added
  1455. for (hired_index, item) in hired_applicant_and_result.iter().enumerate() {
  1456. let (new_member_applied_result, fill_opening_applicant_params) = item;
  1457. // Curator
  1458. let expected_added_curator_id: u64 = (hired_index as u64) + original_next_curator_id;
  1459. // Principal
  1460. let expected_added_principal_id: u64 = (hired_index as u64) + original_next_principal_id;
  1461. // Curator added
  1462. assert!(CuratorById::<Test>::exists(expected_added_curator_id));
  1463. let added_curator = CuratorById::<Test>::get(expected_added_curator_id);
  1464. // expected_curator
  1465. let reward_relationship = None::<<Test as recurringrewards::Trait>::RewardRelationshipId>;
  1466. let curator_application =
  1467. CuratorApplicationById::<Test>::get(new_member_applied_result.curator_application_id);
  1468. let application_id = curator_application.application_id;
  1469. let application = hiring::ApplicationById::<Test>::get(application_id);
  1470. let role_stake_profile = if let Some(ref stake_id) = application.active_role_staking_id {
  1471. // get_baseline_opening_policy().role_staking_policy {
  1472. Some(CuratorRoleStakeProfile::new(
  1473. stake_id,
  1474. &curator_opening
  1475. .policy_commitment
  1476. .terminate_curator_role_stake_unstaking_period,
  1477. &curator_opening
  1478. .policy_commitment
  1479. .exit_curator_role_stake_unstaking_period,
  1480. ))
  1481. } else {
  1482. None
  1483. };
  1484. let expected_curator = Curator {
  1485. role_account: fill_opening_applicant_params
  1486. .add_and_apply_params
  1487. .curator_applicant_role_account,
  1488. reward_relationship: reward_relationship,
  1489. role_stake_profile: role_stake_profile, // added_curator.role_stake_profile.clone(),
  1490. stage: CuratorRoleStage::Active,
  1491. induction: CuratorInduction::new(
  1492. &setup_opening_in_review
  1493. .normal_opening_constructed
  1494. .new_member_as_lead
  1495. .lead_id,
  1496. &new_member_applied_result.curator_application_id,
  1497. &1,
  1498. ),
  1499. principal_id: expected_added_principal_id,
  1500. };
  1501. assert_eq!(expected_curator, added_curator);
  1502. // Principal added
  1503. assert!(PrincipalById::<Test>::exists(expected_added_principal_id));
  1504. let added_principal = PrincipalById::<Test>::get(expected_added_principal_id);
  1505. let expected_added_principal = Principal::Curator(expected_added_principal_id);
  1506. assert_eq!(added_principal, expected_added_principal);
  1507. }
  1508. /*
  1509. * TODO: add assertion abouot side-effect in !hiring & membership! module,
  1510. * this is where state of application has fundamentally changed.
  1511. */
  1512. let application_outomes = applicants
  1513. .iter()
  1514. .enumerate()
  1515. .map(|(index, params)| {
  1516. if params.hire {
  1517. FillOpeningApplicantOutcome::Hired {
  1518. curator_id: (index as u64) + original_next_curator_id,
  1519. }
  1520. } else {
  1521. FillOpeningApplicantOutcome::NotHired
  1522. }
  1523. })
  1524. .collect::<Vec<_>>();
  1525. SetupAndFillOpeningResult {
  1526. setup_opening_in_review,
  1527. application_outomes,
  1528. }
  1529. }
  1530. struct SetupLeadAndHireCuratorResult {
  1531. pub curator_params: AddMemberAndApplyOnOpeningParams,
  1532. pub setup_and_fill_opening_result: SetupAndFillOpeningResult,
  1533. }
  1534. impl SetupLeadAndHireCuratorResult {
  1535. fn curator_params(&self) -> AddMemberAndApplyOnOpeningParams {
  1536. self.curator_params.clone()
  1537. }
  1538. pub fn curator_id(&self) -> CuratorId<Test> {
  1539. match self.setup_and_fill_opening_result.application_outomes[0] {
  1540. FillOpeningApplicantOutcome::Hired { curator_id } => curator_id,
  1541. _ => panic!(),
  1542. }
  1543. }
  1544. pub fn curator_member_id(&self) -> <Test as members::Trait>::MemberId {
  1545. self.setup_and_fill_opening_result
  1546. .setup_opening_in_review
  1547. .added_members_application_result[0]
  1548. .member_id
  1549. }
  1550. }
  1551. fn setup_lead_and_hire_curator() -> SetupLeadAndHireCuratorResult {
  1552. let curator_params = make_generic_add_member_params();
  1553. // Hire curator
  1554. let setup_and_fill_opening_result =
  1555. setup_and_fill_opening(&vec![FillOpeningApplicantParams::new(
  1556. curator_params.clone(),
  1557. true,
  1558. )]);
  1559. SetupLeadAndHireCuratorResult {
  1560. curator_params,
  1561. setup_and_fill_opening_result,
  1562. }
  1563. }
  1564. struct CreateChannelFixture {
  1565. pub channel_creator_member_id: <Test as members::Trait>::MemberId,
  1566. pub controller_account: <Test as system::Trait>::AccountId,
  1567. pub channel_creator_role_account: <Test as system::Trait>::AccountId,
  1568. pub channel_handle: Vec<u8>,
  1569. pub channel_title: OptionalText,
  1570. pub description: OptionalText,
  1571. pub avatar: OptionalText,
  1572. pub banner: OptionalText,
  1573. pub content: ChannelContentType,
  1574. pub publication_status: ChannelPublicationStatus,
  1575. }
  1576. impl CreateChannelFixture {
  1577. pub fn make_valid_unpulished_video_channel_for(
  1578. channel_creator_member_id: <Test as members::Trait>::MemberId,
  1579. override_controller_account: Option<<Test as system::Trait>::AccountId>,
  1580. ) -> Self {
  1581. let controller_account = if let Some(account) = override_controller_account {
  1582. account
  1583. } else {
  1584. members::Module::<Test>::ensure_profile(channel_creator_member_id)
  1585. .unwrap()
  1586. .controller_account
  1587. };
  1588. Self {
  1589. channel_creator_member_id,
  1590. controller_account,
  1591. channel_creator_role_account: 527489,
  1592. channel_handle: generate_valid_length_buffer(&ChannelHandleConstraint::get()),
  1593. channel_title: Some(generate_valid_length_buffer(&ChannelTitleConstraint::get())),
  1594. avatar: Some(generate_valid_length_buffer(&ChannelAvatarConstraint::get())),
  1595. banner: Some(generate_valid_length_buffer(&ChannelBannerConstraint::get())),
  1596. description: Some(generate_valid_length_buffer(
  1597. &ChannelDescriptionConstraint::get(),
  1598. )),
  1599. content: ChannelContentType::Video,
  1600. publication_status: ChannelPublicationStatus::Unlisted,
  1601. }
  1602. }
  1603. fn create_channel(&self) -> Result<(), &'static str> {
  1604. ContentWorkingGroup::create_channel(
  1605. Origin::signed(self.controller_account),
  1606. self.channel_creator_member_id,
  1607. self.channel_creator_role_account,
  1608. self.content.clone(),
  1609. self.channel_handle.clone(),
  1610. self.channel_title.clone(),
  1611. self.description.clone(),
  1612. self.avatar.clone(),
  1613. self.banner.clone(),
  1614. self.publication_status.clone(),
  1615. )
  1616. }
  1617. pub fn call_and_assert_error(&self, err_message: &'static str) {
  1618. let number_of_events_before_call = System::events().len();
  1619. let call_result = self.create_channel();
  1620. assert_eq!(call_result, Err(err_message));
  1621. // No new events deposited
  1622. assert_eq!(System::events().len(), number_of_events_before_call);
  1623. }
  1624. pub fn call_and_assert_success(&self) -> ChannelId<Test> {
  1625. let old_next_channel_id = NextChannelId::<Test>::get();
  1626. let call_result = self.create_channel();
  1627. // Call result was Ok
  1628. assert_eq!(call_result, Ok(()));
  1629. // Assert that event was triggered,
  1630. // keep channel id.
  1631. assert_eq!(
  1632. get_last_event_or_panic(),
  1633. crate::RawEvent::ChannelCreated(old_next_channel_id)
  1634. );
  1635. let channel_id = old_next_channel_id;
  1636. // Assert that given channel id has been added,
  1637. // and has the right properties.
  1638. assert!(crate::ChannelById::<Test>::exists(channel_id));
  1639. let created_channel = crate::ChannelById::<Test>::get(channel_id);
  1640. let expected_channel = Channel {
  1641. verified: false,
  1642. handle: self.channel_handle.clone(),
  1643. title: self.channel_title.clone(),
  1644. avatar: self.avatar.clone(),
  1645. banner: self.banner.clone(),
  1646. description: self.description.clone(),
  1647. content: self.content.clone(),
  1648. owner: self.channel_creator_member_id,
  1649. role_account: self.channel_creator_role_account,
  1650. publication_status: self.publication_status.clone(),
  1651. curation_status: ChannelCurationStatus::Normal,
  1652. created: 1, // <== replace with now()
  1653. // We have no expectation here, so we just copy what was added
  1654. principal_id: created_channel.principal_id,
  1655. };
  1656. assert_eq!(created_channel, expected_channel);
  1657. // Assert that next id incremented.
  1658. assert_eq!(crate::NextChannelId::<Test>::get(), channel_id + 1);
  1659. // Assert that there is a mapping established for handle
  1660. assert_eq!(
  1661. crate::ChannelIdByHandle::<Test>::get(self.channel_handle.clone()),
  1662. channel_id
  1663. );
  1664. // Check that principal actually has been added
  1665. assert!(crate::PrincipalById::<Test>::exists(
  1666. created_channel.principal_id
  1667. ));
  1668. let created_principal = crate::PrincipalById::<Test>::get(created_channel.principal_id);
  1669. assert!(match created_principal {
  1670. Principal::Lead => false,
  1671. Principal::Curator(_) => false,
  1672. Principal::ChannelOwner(created_principal_channel_id) =>
  1673. created_principal_channel_id == channel_id,
  1674. });
  1675. channel_id
  1676. }
  1677. }
  1678. struct NewMemberAsLead {
  1679. pub member_id: <Test as members::Trait>::MemberId,
  1680. pub lead_id: LeadId<Test>,
  1681. }
  1682. fn add_member_and_set_as_lead() -> NewMemberAsLead {
  1683. let member_id = add_member(LEAD_ROOT_AND_CONTROLLER_ACCOUNT, to_vec(LEAD_MEMBER_HANDLE));
  1684. let lead_id = set_lead(member_id, LEAD_ROLE_ACCOUNT);
  1685. NewMemberAsLead { member_id, lead_id }
  1686. }
  1687. pub fn set_channel_creation_enabled(enabled: bool) {
  1688. crate::Module::<Test>::set_channel_creation_enabled(Origin::signed(LEAD_ROLE_ACCOUNT), enabled)
  1689. .unwrap()
  1690. }
  1691. pub fn add_channel_creator_member() -> <Test as members::Trait>::MemberId {
  1692. let channel_creator_member_id = add_member(
  1693. CHANNEL_CREATOR_ROOT_AND_CONTROLLER_ACCOUNT,
  1694. to_vec(CHANNEL_CREATOR_HANDLE),
  1695. );
  1696. channel_creator_member_id
  1697. }
  1698. pub fn add_member(
  1699. root_and_controller_account: <Test as system::Trait>::AccountId,
  1700. handle: Vec<u8>,
  1701. ) -> <Test as members::Trait>::MemberId {
  1702. let next_member_id = members::MembersCreated::<Test>::get();
  1703. assert_eq!(
  1704. members::Module::<Test>::buy_membership(
  1705. Origin::signed(root_and_controller_account),
  1706. 0,
  1707. members::UserInfo {
  1708. handle: Some(handle),
  1709. avatar_uri: None,
  1710. about: None,
  1711. }
  1712. )
  1713. .unwrap(),
  1714. ()
  1715. );
  1716. next_member_id
  1717. }
  1718. pub fn set_lead(
  1719. member_id: <Test as members::Trait>::MemberId,
  1720. new_role_account: <Test as system::Trait>::AccountId,
  1721. ) -> LeadId<Test> {
  1722. // Get controller account
  1723. //let lead_member_controller_account = members::Module::<Test>::ensure_profile(member_id).unwrap().controller_account;
  1724. let expected_lead_id = NextLeadId::<Test>::get();
  1725. // Set lead
  1726. assert_eq!(
  1727. ContentWorkingGroup::replace_lead(
  1728. mock::Origin::system(system::RawOrigin::Root),
  1729. Some((member_id, new_role_account))
  1730. )
  1731. .unwrap(),
  1732. ()
  1733. );
  1734. assert_eq!(
  1735. get_last_event_or_panic(),
  1736. crate::RawEvent::LeadSet(expected_lead_id)
  1737. );
  1738. expected_lead_id
  1739. }
  1740. // lead_role_account: <Test as system::Trait>::AccountId
  1741. pub fn add_curator_opening() -> CuratorOpeningId<Test> {
  1742. let activate_at = hiring::ActivateOpeningAt::ExactBlock(34);
  1743. let human_readable_text = generate_valid_length_buffer(&OpeningHumanReadableText::get());
  1744. let expected_curator_opening_id = NextCuratorOpeningId::<Test>::get();
  1745. assert_eq!(
  1746. ContentWorkingGroup::add_curator_opening(
  1747. Origin::signed(LEAD_ROLE_ACCOUNT),
  1748. activate_at.clone(),
  1749. get_baseline_opening_policy(),
  1750. human_readable_text.clone()
  1751. )
  1752. .unwrap(),
  1753. ()
  1754. );
  1755. assert_eq!(
  1756. get_last_event_or_panic(),
  1757. crate::RawEvent::CuratorOpeningAdded(expected_curator_opening_id)
  1758. );
  1759. expected_curator_opening_id
  1760. }
  1761. /*
  1762. * Buffer generators
  1763. */
  1764. pub fn generate_text(len: usize) -> Vec<u8> {
  1765. vec![b'x'; len]
  1766. }
  1767. pub fn generate_valid_length_buffer(constraint: &InputValidationLengthConstraint) -> Vec<u8> {
  1768. generate_text(constraint.min as usize)
  1769. }
  1770. pub fn generate_too_short_length_buffer(constraint: &InputValidationLengthConstraint) -> Vec<u8> {
  1771. generate_text((constraint.min - 1) as usize)
  1772. }
  1773. pub fn generate_too_long_length_buffer(constraint: &InputValidationLengthConstraint) -> Vec<u8> {
  1774. generate_text((constraint.max() + 1) as usize)
  1775. }
  1776. #[test]
  1777. fn increasing_mint_capacity() {
  1778. const MINT_CAPACITY: u64 = 50000;
  1779. TestExternalitiesBuilder::<Test>::default()
  1780. .with_content_wg_config(
  1781. genesis::GenesisConfigBuilder::<Test>::default()
  1782. .with_mint_capacity(MINT_CAPACITY)
  1783. .build(),
  1784. )
  1785. .build()
  1786. .execute_with(|| {
  1787. let mint_id = ContentWorkingGroup::mint();
  1788. let mint = Minting::mints(mint_id);
  1789. assert_eq!(mint.capacity(), MINT_CAPACITY);
  1790. let increase = 25000;
  1791. // Increasing mint capacity
  1792. let expected_new_capacity = MINT_CAPACITY + increase;
  1793. assert_ok!(ContentWorkingGroup::increase_mint_capacity(
  1794. Origin::ROOT,
  1795. increase
  1796. ));
  1797. // Excpected event after increasing
  1798. assert_eq!(
  1799. get_last_event_or_panic(),
  1800. crate::RawEvent::MintCapacityIncreased(mint_id, increase, expected_new_capacity)
  1801. );
  1802. // Excpected value of capacity after increasing
  1803. let mint = Minting::mints(mint_id);
  1804. assert_eq!(mint.capacity(), expected_new_capacity);
  1805. });
  1806. }
  1807. #[test]
  1808. fn setting_mint_capacity() {
  1809. const MINT_CAPACITY: u64 = 50000;
  1810. TestExternalitiesBuilder::<Test>::default()
  1811. .with_content_wg_config(
  1812. genesis::GenesisConfigBuilder::<Test>::default()
  1813. .with_mint_capacity(MINT_CAPACITY)
  1814. .build(),
  1815. )
  1816. .build()
  1817. .execute_with(|| {
  1818. let mint_id = ContentWorkingGroup::mint();
  1819. let mint = Minting::mints(mint_id);
  1820. assert_eq!(mint.capacity(), MINT_CAPACITY);
  1821. // Decreasing mint capacity
  1822. let new_lower_capacity = 10000;
  1823. let decrease = MINT_CAPACITY - new_lower_capacity;
  1824. assert_ok!(ContentWorkingGroup::set_mint_capacity(
  1825. Origin::ROOT,
  1826. new_lower_capacity
  1827. ));
  1828. // Correct event after decreasing
  1829. assert_eq!(
  1830. get_last_event_or_panic(),
  1831. crate::RawEvent::MintCapacityDecreased(mint_id, decrease, new_lower_capacity)
  1832. );
  1833. // Correct value of capacity after decreasing
  1834. let mint = Minting::mints(mint_id);
  1835. assert_eq!(mint.capacity(), new_lower_capacity);
  1836. // Increasing mint capacity
  1837. let new_higher_capacity = 25000;
  1838. let increase = new_higher_capacity - mint.capacity();
  1839. assert_ok!(ContentWorkingGroup::set_mint_capacity(
  1840. Origin::ROOT,
  1841. new_higher_capacity
  1842. ));
  1843. // Excpected event after increasing
  1844. assert_eq!(
  1845. get_last_event_or_panic(),
  1846. crate::RawEvent::MintCapacityIncreased(mint_id, increase, new_higher_capacity)
  1847. );
  1848. // Excpected value of capacity after increasing
  1849. let mint = Minting::mints(mint_id);
  1850. assert_eq!(mint.capacity(), new_higher_capacity);
  1851. });
  1852. }