tests.rs 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186
  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::set_lead(self.origin.clone(), self.member_id, self.new_role_account)
  950. }
  951. pub fn call_and_assert_success(&self) {
  952. let original_next_lead_id = NextLeadId::<Test>::get();
  953. let call_result = self.call();
  954. assert_eq!(call_result, Ok(()));
  955. let updated_next_lead_id = NextLeadId::<Test>::get();
  956. assert_eq!(original_next_lead_id + 1, updated_next_lead_id);
  957. let new_lead_id = if let Some(id) = CurrentLeadId::<Test>::get() {
  958. id
  959. } else {
  960. panic!("Lead not set when it must be.")
  961. };
  962. let new_lead = LeadById::<Test>::get(new_lead_id);
  963. let expected_new_lead = Lead {
  964. role_account: self.new_role_account,
  965. reward_relationship: None,
  966. inducted: 1, // make dynamic later
  967. stage: LeadRoleState::Active,
  968. };
  969. assert_eq!(new_lead, expected_new_lead);
  970. assert_eq!(
  971. get_last_event_or_panic(),
  972. crate::RawEvent::LeadSet(new_lead_id)
  973. );
  974. }
  975. }
  976. #[test]
  977. fn set_lead_success() {
  978. TestExternalitiesBuilder::<Test>::default()
  979. .build()
  980. .execute_with(|| {
  981. let member_id =
  982. add_member(LEAD_ROOT_AND_CONTROLLER_ACCOUNT, to_vec(LEAD_MEMBER_HANDLE));
  983. SetLeadFixture {
  984. origin: Origin::system(system::RawOrigin::Root),
  985. member_id,
  986. new_role_account: 44444,
  987. }
  988. .call_and_assert_success();
  989. });
  990. }
  991. struct UnsetLeadFixture {
  992. pub origin: Origin,
  993. }
  994. impl UnsetLeadFixture {
  995. fn call(&self) -> Result<(), &'static str> {
  996. ContentWorkingGroup::unset_lead(self.origin.clone())
  997. }
  998. pub fn call_and_assert_success(&self) {
  999. let original_lead_id = CurrentLeadId::<Test>::get().unwrap();
  1000. let original_lead = LeadById::<Test>::get(original_lead_id);
  1001. let call_result = self.call();
  1002. assert_eq!(call_result, Ok(()));
  1003. assert!(CurrentLeadId::<Test>::get().is_none());
  1004. let updated_lead = LeadById::<Test>::get(original_lead_id);
  1005. let expected_updated_lead = Lead {
  1006. stage: LeadRoleState::Exited(ExitedLeadRole {
  1007. initiated_at_block_number: 1,
  1008. }),
  1009. ..original_lead
  1010. };
  1011. assert_eq!(updated_lead, expected_updated_lead);
  1012. assert_eq!(
  1013. get_last_event_or_panic(),
  1014. crate::RawEvent::LeadUnset(original_lead_id)
  1015. );
  1016. }
  1017. }
  1018. #[test]
  1019. fn unset_lead_success() {
  1020. TestExternalitiesBuilder::<Test>::default()
  1021. .build()
  1022. .execute_with(|| {
  1023. let _ = add_member_and_set_as_lead();
  1024. UnsetLeadFixture {
  1025. origin: Origin::system(system::RawOrigin::Root),
  1026. }
  1027. .call_and_assert_success();
  1028. });
  1029. }
  1030. struct UnstakedFixture {
  1031. pub stake_id: StakeId<Test>,
  1032. }
  1033. impl UnstakedFixture {
  1034. fn call(&self) {
  1035. ContentWorkingGroup::unstaked(self.stake_id);
  1036. }
  1037. pub fn call_and_assert_success(&self) {
  1038. let unstaker = UnstakerByStakeId::<Test>::get(self.stake_id);
  1039. let curator_id = if let WorkingGroupUnstaker::Curator(curator_id) = unstaker {
  1040. curator_id
  1041. } else {
  1042. panic!("Unstaker not curator")
  1043. };
  1044. let original_curator = CuratorById::<Test>::get(curator_id);
  1045. let original_exit_summary =
  1046. if let CuratorRoleStage::Unstaking(exit_summary) = (original_curator.clone()).stage {
  1047. exit_summary
  1048. } else {
  1049. panic!("Curator not unstaking")
  1050. };
  1051. self.call();
  1052. let expected_curator = Curator {
  1053. stage: CuratorRoleStage::Exited(original_exit_summary),
  1054. ..(original_curator.clone())
  1055. };
  1056. let updated_curator = CuratorById::<Test>::get(curator_id);
  1057. assert_eq!(updated_curator, expected_curator);
  1058. assert_eq!(
  1059. get_last_event_or_panic(),
  1060. crate::RawEvent::TerminatedCurator(curator_id)
  1061. );
  1062. // Unstaker gone
  1063. assert!(!UnstakerByStakeId::<Test>::exists(self.stake_id));
  1064. }
  1065. // pub fn call_and_assert_failed_result(&self, error_message: &'static str) {
  1066. // let call_result = self.call();
  1067. // assert_eq!(call_result, Err(error_message));
  1068. // }
  1069. }
  1070. #[test]
  1071. fn unstaked_curator_success() {
  1072. TestExternalitiesBuilder::<Test>::default()
  1073. .build()
  1074. .execute_with(|| {
  1075. let result = setup_lead_and_hire_curator();
  1076. TerminateCuratorRoleFixture {
  1077. origin: Origin::signed(LEAD_ROLE_ACCOUNT),
  1078. curator_id: result.curator_id(),
  1079. rationale_text: "This curator is a joke!".as_bytes().to_vec(),
  1080. }
  1081. .call_and_assert_success();
  1082. let curator_role_stake_id = CuratorById::<Test>::get(result.curator_id())
  1083. .role_stake_profile
  1084. .unwrap()
  1085. .stake_id;
  1086. UnstakedFixture {
  1087. stake_id: curator_role_stake_id,
  1088. }
  1089. .call_and_assert_success();
  1090. });
  1091. }
  1092. #[test]
  1093. fn account_can_act_as_principal_success() {}
  1094. /*
  1095. * Fixtures
  1096. */
  1097. static LEAD_ROOT_AND_CONTROLLER_ACCOUNT: <Test as system::Trait>::AccountId = 1289;
  1098. static LEAD_ROLE_ACCOUNT: <Test as system::Trait>::AccountId = 1289;
  1099. static LEAD_MEMBER_HANDLE: &str = "IamTheLead";
  1100. static CHANNEL_CREATOR_ROOT_AND_CONTROLLER_ACCOUNT: <Test as system::Trait>::AccountId = 11;
  1101. static CHANNEL_CREATOR_HANDLE: &str = "Coolcreator1";
  1102. static CHANNEL_CREATOR_HANDLE2: &str = "Coolcreator2";
  1103. fn make_generic_add_member_params() -> AddMemberAndApplyOnOpeningParams {
  1104. AddMemberAndApplyOnOpeningParams::new(
  1105. 2222,
  1106. to_vec("yoyoyo0"), // generate_valid_length_buffer(&ChannelHandleConstraint::get()),
  1107. 2222 * 2,
  1108. generate_valid_length_buffer(&CuratorApplicationHumanReadableText::get()),
  1109. )
  1110. }
  1111. /// Made into function to avoid having to clone every time we read fields
  1112. pub fn get_baseline_opening_policy(
  1113. ) -> OpeningPolicyCommitment<<Test as system::Trait>::BlockNumber, BalanceOf<Test>> {
  1114. OpeningPolicyCommitment {
  1115. application_rationing_policy: Some(hiring::ApplicationRationingPolicy {
  1116. max_active_applicants: 5,
  1117. }),
  1118. max_review_period_length: 100,
  1119. application_staking_policy: Some(hiring::StakingPolicy {
  1120. amount: 40000,
  1121. amount_mode: hiring::StakingAmountLimitMode::Exact,
  1122. crowded_out_unstaking_period_length: Some(3),
  1123. review_period_expired_unstaking_period_length: Some(22),
  1124. }),
  1125. role_staking_policy: Some(hiring::StakingPolicy {
  1126. amount: 900000,
  1127. amount_mode: hiring::StakingAmountLimitMode::AtLeast,
  1128. crowded_out_unstaking_period_length: Some(30),
  1129. review_period_expired_unstaking_period_length: Some(2),
  1130. }),
  1131. role_slashing_terms: SlashingTerms::Unslashable,
  1132. fill_opening_successful_applicant_application_stake_unstaking_period: None,
  1133. fill_opening_failed_applicant_application_stake_unstaking_period: None,
  1134. fill_opening_failed_applicant_role_stake_unstaking_period: None,
  1135. terminate_curator_application_stake_unstaking_period: None,
  1136. terminate_curator_role_stake_unstaking_period: None,
  1137. exit_curator_role_application_stake_unstaking_period: None,
  1138. exit_curator_role_stake_unstaking_period: None,
  1139. }
  1140. }
  1141. pub fn to_vec(s: &str) -> Vec<u8> {
  1142. s.as_bytes().to_vec()
  1143. }
  1144. /*
  1145. * Setups
  1146. */
  1147. //type TestSeed = u128;
  1148. /*
  1149. fn account_from_seed(TestSeed: seed) -> <Test as system::Trait>::AccountId {
  1150. }
  1151. fn vector_from_seed(TestSeed: seed) {
  1152. }
  1153. */
  1154. /*
  1155. static INITIAL_SEED_VALUE: u128 = 0;
  1156. static CURRENT_SEED: u128 = INITIAL_SEED_VALUE;
  1157. fn get_current_seed() {
  1158. }
  1159. fn update_seed() {
  1160. }
  1161. fn reset_seed() {
  1162. CURRENT_SEED: u128 = INITIAL_SEED_VALUE;
  1163. }
  1164. */
  1165. // MOVE THIS LATER WHEN fill_opening is factored out
  1166. #[derive(Clone)]
  1167. pub struct FillOpeningApplicantParams {
  1168. pub add_and_apply_params: AddMemberAndApplyOnOpeningParams,
  1169. pub hire: bool,
  1170. }
  1171. impl FillOpeningApplicantParams {
  1172. pub fn new(add_and_apply_params: AddMemberAndApplyOnOpeningParams, hire: bool) -> Self {
  1173. Self {
  1174. add_and_apply_params: add_and_apply_params.clone(),
  1175. hire: hire,
  1176. }
  1177. }
  1178. }
  1179. #[derive(Clone)]
  1180. pub struct AddMemberAndApplyOnOpeningParams {
  1181. pub curator_applicant_root_and_controller_account: <Test as system::Trait>::AccountId,
  1182. pub handle: Vec<u8>,
  1183. pub curator_applicant_role_account: <Test as system::Trait>::AccountId,
  1184. pub human_readable_text: Vec<u8>,
  1185. }
  1186. impl AddMemberAndApplyOnOpeningParams {
  1187. pub fn new(
  1188. curator_applicant_root_and_controller_account: <Test as system::Trait>::AccountId,
  1189. handle: Vec<u8>,
  1190. curator_applicant_role_account: <Test as system::Trait>::AccountId,
  1191. human_readable_text: Vec<u8>,
  1192. ) -> Self {
  1193. Self {
  1194. curator_applicant_root_and_controller_account,
  1195. handle,
  1196. curator_applicant_role_account,
  1197. human_readable_text,
  1198. }
  1199. }
  1200. }
  1201. fn add_members_and_apply_on_opening(
  1202. curator_opening_id: CuratorOpeningId<Test>,
  1203. applicants: &Vec<AddMemberAndApplyOnOpeningParams>,
  1204. ) -> Vec<NewMemberAppliedResult> {
  1205. applicants
  1206. .iter()
  1207. .cloned()
  1208. .map(|params| -> NewMemberAppliedResult {
  1209. add_member_and_apply_on_opening(
  1210. curator_opening_id,
  1211. params.curator_applicant_root_and_controller_account,
  1212. params.handle,
  1213. params.curator_applicant_role_account,
  1214. params.human_readable_text,
  1215. )
  1216. })
  1217. .collect()
  1218. }
  1219. #[derive(Clone)]
  1220. struct NewMemberAppliedResult {
  1221. pub member_id: <Test as members::Trait>::MemberId,
  1222. pub curator_application_id: crate::CuratorApplicationId<Test>,
  1223. }
  1224. fn add_member_and_apply_on_opening(
  1225. curator_opening_id: CuratorOpeningId<Test>,
  1226. curator_applicant_root_and_controller_account: <Test as system::Trait>::AccountId,
  1227. handle: Vec<u8>,
  1228. curator_applicant_role_account: <Test as system::Trait>::AccountId,
  1229. human_readable_text: Vec<u8>,
  1230. ) -> NewMemberAppliedResult {
  1231. // Make membership
  1232. let curator_applicant_member_id =
  1233. add_member(curator_applicant_root_and_controller_account, handle);
  1234. // Guarantee sufficient stake
  1235. let role_stake_balance = if let Some(policy) = get_baseline_opening_policy().role_staking_policy
  1236. {
  1237. policy.amount
  1238. } else {
  1239. 0
  1240. };
  1241. let application_stake_balance =
  1242. if let Some(policy) = get_baseline_opening_policy().application_staking_policy {
  1243. policy.amount
  1244. } else {
  1245. 0
  1246. };
  1247. let total_balance = role_stake_balance + application_stake_balance;
  1248. let source_account = curator_applicant_root_and_controller_account;
  1249. // Credit staking source account if required
  1250. if total_balance > 0 {
  1251. let _ = balances::Module::<Test>::deposit_creating(&source_account, total_balance);
  1252. }
  1253. let expected_hiring_application_id = <hiring::NextApplicationId<Test>>::get();
  1254. let old_curator_opening = CuratorOpeningById::<Test>::get(curator_opening_id);
  1255. let new_curator_application_id = NextCuratorApplicationId::<Test>::get();
  1256. /*
  1257. * Test
  1258. */
  1259. assert_eq!(
  1260. ContentWorkingGroup::apply_on_curator_opening(
  1261. Origin::signed(curator_applicant_root_and_controller_account),
  1262. curator_applicant_member_id,
  1263. curator_opening_id,
  1264. curator_applicant_role_account,
  1265. Some(role_stake_balance),
  1266. Some(application_stake_balance),
  1267. human_readable_text
  1268. )
  1269. .unwrap(),
  1270. ()
  1271. );
  1272. assert_eq!(
  1273. get_last_event_or_panic(),
  1274. crate::RawEvent::AppliedOnCuratorOpening(curator_opening_id, new_curator_application_id)
  1275. );
  1276. assert!(CuratorApplicationById::<Test>::exists(
  1277. new_curator_application_id
  1278. ));
  1279. // Assert that appropriate application has been added
  1280. let new_curator_application = CuratorApplicationById::<Test>::get(new_curator_application_id);
  1281. let expected_curator_application = CuratorApplication {
  1282. role_account: curator_applicant_role_account,
  1283. curator_opening_id: curator_opening_id,
  1284. member_id: curator_applicant_member_id,
  1285. application_id: expected_hiring_application_id,
  1286. };
  1287. assert_eq!(expected_curator_application, new_curator_application);
  1288. // Assert that the opening has had the application added to application list
  1289. let mut singleton = BTreeSet::new(); // Unavoidable mutable, BTreeSet can only be populated this way.
  1290. singleton.insert(new_curator_application_id);
  1291. let new_curator_applications = old_curator_opening
  1292. .curator_applications
  1293. .union(&singleton)
  1294. .cloned()
  1295. .collect();
  1296. let expected_curator_opening = CuratorOpening {
  1297. curator_applications: new_curator_applications,
  1298. ..old_curator_opening
  1299. };
  1300. let new_curator_opening = CuratorOpeningById::<Test>::get(curator_opening_id);
  1301. assert_eq!(expected_curator_opening, new_curator_opening);
  1302. NewMemberAppliedResult {
  1303. member_id: curator_applicant_member_id,
  1304. curator_application_id: new_curator_application_id,
  1305. }
  1306. }
  1307. struct NormalOpeningConstructed {
  1308. pub curator_opening_id: CuratorOpeningId<Test>,
  1309. pub new_member_as_lead: NewMemberAsLead,
  1310. }
  1311. fn setup_normal_opening() -> NormalOpeningConstructed {
  1312. let new_member_as_lead = add_member_and_set_as_lead();
  1313. let expected_curator_opening_id = NextCuratorOpeningId::<Test>::get();
  1314. assert_eq!(
  1315. ContentWorkingGroup::add_curator_opening(
  1316. Origin::signed(LEAD_ROLE_ACCOUNT),
  1317. hiring::ActivateOpeningAt::ExactBlock(34),
  1318. get_baseline_opening_policy(),
  1319. generate_valid_length_buffer(&OpeningHumanReadableText::get())
  1320. )
  1321. .unwrap(),
  1322. ()
  1323. );
  1324. assert_eq!(
  1325. get_last_event_or_panic(),
  1326. crate::RawEvent::CuratorOpeningAdded(expected_curator_opening_id)
  1327. );
  1328. NormalOpeningConstructed {
  1329. curator_opening_id: expected_curator_opening_id,
  1330. new_member_as_lead,
  1331. }
  1332. }
  1333. fn setup_normal_accepting_opening() -> NormalOpeningConstructed {
  1334. let normal_opening_constructed = setup_normal_opening();
  1335. assert_eq!(
  1336. ContentWorkingGroup::accept_curator_applications(
  1337. Origin::signed(LEAD_ROLE_ACCOUNT), // <== get dynamic value from somewhere else later
  1338. normal_opening_constructed.curator_opening_id
  1339. )
  1340. .unwrap(),
  1341. ()
  1342. );
  1343. normal_opening_constructed
  1344. }
  1345. struct SetupOpeningInReview {
  1346. //pub curator_opening_id: lib::CuratorOpeningId<Test>,
  1347. pub normal_opening_constructed: NormalOpeningConstructed,
  1348. pub added_members_application_result: Vec<NewMemberAppliedResult>,
  1349. }
  1350. fn setup_opening_in_review(
  1351. applicants: &Vec<AddMemberAndApplyOnOpeningParams>,
  1352. ) -> SetupOpeningInReview {
  1353. let normal_opening_constructed = setup_normal_accepting_opening();
  1354. let added_members_application_result =
  1355. add_members_and_apply_on_opening(normal_opening_constructed.curator_opening_id, applicants);
  1356. assert_eq!(
  1357. ContentWorkingGroup::begin_curator_applicant_review(
  1358. Origin::signed(LEAD_ROLE_ACCOUNT),
  1359. normal_opening_constructed.curator_opening_id
  1360. )
  1361. .unwrap(),
  1362. ()
  1363. );
  1364. // TODO: assert event stuff !!!!
  1365. SetupOpeningInReview {
  1366. normal_opening_constructed,
  1367. added_members_application_result,
  1368. }
  1369. }
  1370. enum FillOpeningApplicantOutcome {
  1371. NotHired,
  1372. Hired { curator_id: CuratorId<Test> },
  1373. }
  1374. struct SetupAndFillOpeningResult {
  1375. setup_opening_in_review: SetupOpeningInReview,
  1376. application_outomes: Vec<FillOpeningApplicantOutcome>,
  1377. }
  1378. fn setup_and_fill_opening(
  1379. applicants: &Vec<FillOpeningApplicantParams>,
  1380. ) -> SetupAndFillOpeningResult {
  1381. let setup_opening_params = applicants
  1382. .iter()
  1383. .cloned()
  1384. .map(|param| param.add_and_apply_params)
  1385. .collect::<Vec<_>>();
  1386. let setup_opening_in_review = setup_opening_in_review(&setup_opening_params);
  1387. let curator_opening = CuratorOpeningById::<Test>::get(
  1388. setup_opening_in_review
  1389. .normal_opening_constructed
  1390. .curator_opening_id,
  1391. );
  1392. // Set whom to hire
  1393. let applicants_to_hire_iter = applicants.iter().filter(|params| params.hire);
  1394. let num_applicants_hired = applicants_to_hire_iter.cloned().count();
  1395. //let num_applicants_not_to_hire = (applicants.len() - num_applicants_hired) as usize;
  1396. let hired_applicant_and_result = setup_opening_in_review
  1397. .added_members_application_result
  1398. .iter()
  1399. .zip(applicants.iter())
  1400. .filter(|(_, fill_opening_applicant_params)| fill_opening_applicant_params.hire)
  1401. .collect::<Vec<_>>();
  1402. let successful_curator_application_ids = hired_applicant_and_result
  1403. .iter()
  1404. .map(|(new_member_applied_result, _)| new_member_applied_result.curator_application_id)
  1405. .collect::<BTreeSet<_>>();
  1406. // Remember original id counters
  1407. let original_next_curator_id = NextCuratorId::<Test>::get();
  1408. let original_next_principal_id = NextPrincipalId::<Test>::get();
  1409. /*
  1410. * Call
  1411. */
  1412. assert_eq!(
  1413. ContentWorkingGroup::fill_curator_opening(
  1414. Origin::signed(LEAD_ROLE_ACCOUNT),
  1415. setup_opening_in_review
  1416. .normal_opening_constructed
  1417. .curator_opening_id,
  1418. successful_curator_application_ids.clone(),
  1419. None
  1420. ),
  1421. Ok(())
  1422. );
  1423. /*
  1424. * Asserts
  1425. */
  1426. let successful_curator_application_id_to_curator_id = successful_curator_application_ids
  1427. .iter()
  1428. .enumerate()
  1429. .map(
  1430. |(index, item)| -> (CuratorApplicationId<Test>, CuratorId<Test>) {
  1431. let curator_id = original_next_curator_id + (index as CuratorId<Test>);
  1432. (*item, curator_id)
  1433. },
  1434. )
  1435. .collect::<BTreeMap<_, _>>();
  1436. assert_eq!(
  1437. get_last_event_or_panic(),
  1438. crate::RawEvent::CuratorOpeningFilled(
  1439. setup_opening_in_review
  1440. .normal_opening_constructed
  1441. .curator_opening_id,
  1442. successful_curator_application_id_to_curator_id
  1443. )
  1444. );
  1445. // The right number of curators have been created
  1446. let num_curators_created = NextCuratorId::<Test>::get() - original_next_curator_id;
  1447. assert_eq!(num_curators_created, (num_applicants_hired as u64));
  1448. // The right numbe of prinipals were created
  1449. let num_principals_created = NextPrincipalId::<Test>::get() - original_next_principal_id;
  1450. assert_eq!(num_principals_created, (num_applicants_hired as u64));
  1451. // Inspect all expected curators and principal added
  1452. for (hired_index, item) in hired_applicant_and_result.iter().enumerate() {
  1453. let (new_member_applied_result, fill_opening_applicant_params) = item;
  1454. // Curator
  1455. let expected_added_curator_id: u64 = (hired_index as u64) + original_next_curator_id;
  1456. // Principal
  1457. let expected_added_principal_id: u64 = (hired_index as u64) + original_next_principal_id;
  1458. // Curator added
  1459. assert!(CuratorById::<Test>::exists(expected_added_curator_id));
  1460. let added_curator = CuratorById::<Test>::get(expected_added_curator_id);
  1461. // expected_curator
  1462. let reward_relationship = None::<<Test as recurringrewards::Trait>::RewardRelationshipId>;
  1463. let curator_application =
  1464. CuratorApplicationById::<Test>::get(new_member_applied_result.curator_application_id);
  1465. let application_id = curator_application.application_id;
  1466. let application = hiring::ApplicationById::<Test>::get(application_id);
  1467. let role_stake_profile = if let Some(ref stake_id) = application.active_role_staking_id {
  1468. // get_baseline_opening_policy().role_staking_policy {
  1469. Some(CuratorRoleStakeProfile::new(
  1470. stake_id,
  1471. &curator_opening
  1472. .policy_commitment
  1473. .terminate_curator_role_stake_unstaking_period,
  1474. &curator_opening
  1475. .policy_commitment
  1476. .exit_curator_role_stake_unstaking_period,
  1477. ))
  1478. } else {
  1479. None
  1480. };
  1481. let expected_curator = Curator {
  1482. role_account: fill_opening_applicant_params
  1483. .add_and_apply_params
  1484. .curator_applicant_role_account,
  1485. reward_relationship: reward_relationship,
  1486. role_stake_profile: role_stake_profile, // added_curator.role_stake_profile.clone(),
  1487. stage: CuratorRoleStage::Active,
  1488. induction: CuratorInduction::new(
  1489. &setup_opening_in_review
  1490. .normal_opening_constructed
  1491. .new_member_as_lead
  1492. .lead_id,
  1493. &new_member_applied_result.curator_application_id,
  1494. &1,
  1495. ),
  1496. principal_id: expected_added_principal_id,
  1497. };
  1498. assert_eq!(expected_curator, added_curator);
  1499. // Principal added
  1500. assert!(PrincipalById::<Test>::exists(expected_added_principal_id));
  1501. let added_principal = PrincipalById::<Test>::get(expected_added_principal_id);
  1502. let expected_added_principal = Principal::Curator(expected_added_principal_id);
  1503. assert_eq!(added_principal, expected_added_principal);
  1504. }
  1505. /*
  1506. * TODO: add assertion abouot side-effect in !hiring & membership! module,
  1507. * this is where state of application has fundamentally changed.
  1508. */
  1509. let application_outomes = applicants
  1510. .iter()
  1511. .enumerate()
  1512. .map(|(index, params)| {
  1513. if params.hire {
  1514. FillOpeningApplicantOutcome::Hired {
  1515. curator_id: (index as u64) + original_next_curator_id,
  1516. }
  1517. } else {
  1518. FillOpeningApplicantOutcome::NotHired
  1519. }
  1520. })
  1521. .collect::<Vec<_>>();
  1522. SetupAndFillOpeningResult {
  1523. setup_opening_in_review,
  1524. application_outomes,
  1525. }
  1526. }
  1527. struct SetupLeadAndHireCuratorResult {
  1528. pub curator_params: AddMemberAndApplyOnOpeningParams,
  1529. pub setup_and_fill_opening_result: SetupAndFillOpeningResult,
  1530. }
  1531. impl SetupLeadAndHireCuratorResult {
  1532. fn curator_params(&self) -> AddMemberAndApplyOnOpeningParams {
  1533. self.curator_params.clone()
  1534. }
  1535. pub fn curator_id(&self) -> CuratorId<Test> {
  1536. match self.setup_and_fill_opening_result.application_outomes[0] {
  1537. FillOpeningApplicantOutcome::Hired { curator_id } => curator_id,
  1538. _ => panic!(),
  1539. }
  1540. }
  1541. pub fn curator_member_id(&self) -> <Test as members::Trait>::MemberId {
  1542. self.setup_and_fill_opening_result
  1543. .setup_opening_in_review
  1544. .added_members_application_result[0]
  1545. .member_id
  1546. }
  1547. }
  1548. fn setup_lead_and_hire_curator() -> SetupLeadAndHireCuratorResult {
  1549. let curator_params = make_generic_add_member_params();
  1550. // Hire curator
  1551. let setup_and_fill_opening_result =
  1552. setup_and_fill_opening(&vec![FillOpeningApplicantParams::new(
  1553. curator_params.clone(),
  1554. true,
  1555. )]);
  1556. SetupLeadAndHireCuratorResult {
  1557. curator_params,
  1558. setup_and_fill_opening_result,
  1559. }
  1560. }
  1561. struct CreateChannelFixture {
  1562. pub channel_creator_member_id: <Test as members::Trait>::MemberId,
  1563. pub controller_account: <Test as system::Trait>::AccountId,
  1564. pub channel_creator_role_account: <Test as system::Trait>::AccountId,
  1565. pub channel_handle: Vec<u8>,
  1566. pub channel_title: OptionalText,
  1567. pub description: OptionalText,
  1568. pub avatar: OptionalText,
  1569. pub banner: OptionalText,
  1570. pub content: ChannelContentType,
  1571. pub publication_status: ChannelPublicationStatus,
  1572. }
  1573. impl CreateChannelFixture {
  1574. pub fn make_valid_unpulished_video_channel_for(
  1575. channel_creator_member_id: <Test as members::Trait>::MemberId,
  1576. override_controller_account: Option<<Test as system::Trait>::AccountId>,
  1577. ) -> Self {
  1578. let controller_account = if let Some(account) = override_controller_account {
  1579. account
  1580. } else {
  1581. members::Module::<Test>::ensure_profile(channel_creator_member_id)
  1582. .unwrap()
  1583. .controller_account
  1584. };
  1585. Self {
  1586. channel_creator_member_id,
  1587. controller_account,
  1588. channel_creator_role_account: 527489,
  1589. channel_handle: generate_valid_length_buffer(&ChannelHandleConstraint::get()),
  1590. channel_title: Some(generate_valid_length_buffer(&ChannelTitleConstraint::get())),
  1591. avatar: Some(generate_valid_length_buffer(&ChannelAvatarConstraint::get())),
  1592. banner: Some(generate_valid_length_buffer(&ChannelBannerConstraint::get())),
  1593. description: Some(generate_valid_length_buffer(
  1594. &ChannelDescriptionConstraint::get(),
  1595. )),
  1596. content: ChannelContentType::Video,
  1597. publication_status: ChannelPublicationStatus::Unlisted,
  1598. }
  1599. }
  1600. fn create_channel(&self) -> Result<(), &'static str> {
  1601. ContentWorkingGroup::create_channel(
  1602. Origin::signed(self.controller_account),
  1603. self.channel_creator_member_id,
  1604. self.channel_creator_role_account,
  1605. self.content.clone(),
  1606. self.channel_handle.clone(),
  1607. self.channel_title.clone(),
  1608. self.description.clone(),
  1609. self.avatar.clone(),
  1610. self.banner.clone(),
  1611. self.publication_status.clone(),
  1612. )
  1613. }
  1614. pub fn call_and_assert_error(&self, err_message: &'static str) {
  1615. let number_of_events_before_call = System::events().len();
  1616. let call_result = self.create_channel();
  1617. assert_eq!(call_result, Err(err_message));
  1618. // No new events deposited
  1619. assert_eq!(System::events().len(), number_of_events_before_call);
  1620. }
  1621. pub fn call_and_assert_success(&self) -> ChannelId<Test> {
  1622. let old_next_channel_id = NextChannelId::<Test>::get();
  1623. let call_result = self.create_channel();
  1624. // Call result was Ok
  1625. assert_eq!(call_result, Ok(()));
  1626. // Assert that event was triggered,
  1627. // keep channel id.
  1628. assert_eq!(
  1629. get_last_event_or_panic(),
  1630. crate::RawEvent::ChannelCreated(old_next_channel_id)
  1631. );
  1632. let channel_id = old_next_channel_id;
  1633. // Assert that given channel id has been added,
  1634. // and has the right properties.
  1635. assert!(crate::ChannelById::<Test>::exists(channel_id));
  1636. let created_channel = crate::ChannelById::<Test>::get(channel_id);
  1637. let expected_channel = Channel {
  1638. verified: false,
  1639. handle: self.channel_handle.clone(),
  1640. title: self.channel_title.clone(),
  1641. avatar: self.avatar.clone(),
  1642. banner: self.banner.clone(),
  1643. description: self.description.clone(),
  1644. content: self.content.clone(),
  1645. owner: self.channel_creator_member_id,
  1646. role_account: self.channel_creator_role_account,
  1647. publication_status: self.publication_status.clone(),
  1648. curation_status: ChannelCurationStatus::Normal,
  1649. created: 1, // <== replace with now()
  1650. // We have no expectation here, so we just copy what was added
  1651. principal_id: created_channel.principal_id,
  1652. };
  1653. assert_eq!(created_channel, expected_channel);
  1654. // Assert that next id incremented.
  1655. assert_eq!(crate::NextChannelId::<Test>::get(), channel_id + 1);
  1656. // Assert that there is a mapping established for handle
  1657. assert_eq!(
  1658. crate::ChannelIdByHandle::<Test>::get(self.channel_handle.clone()),
  1659. channel_id
  1660. );
  1661. // Check that principal actually has been added
  1662. assert!(crate::PrincipalById::<Test>::exists(
  1663. created_channel.principal_id
  1664. ));
  1665. let created_principal = crate::PrincipalById::<Test>::get(created_channel.principal_id);
  1666. assert!(match created_principal {
  1667. Principal::Lead => false,
  1668. Principal::Curator(_) => false,
  1669. Principal::ChannelOwner(created_principal_channel_id) =>
  1670. created_principal_channel_id == channel_id,
  1671. });
  1672. channel_id
  1673. }
  1674. }
  1675. struct NewMemberAsLead {
  1676. pub member_id: <Test as members::Trait>::MemberId,
  1677. pub lead_id: LeadId<Test>,
  1678. }
  1679. fn add_member_and_set_as_lead() -> NewMemberAsLead {
  1680. let member_id = add_member(LEAD_ROOT_AND_CONTROLLER_ACCOUNT, to_vec(LEAD_MEMBER_HANDLE));
  1681. let lead_id = set_lead(member_id, LEAD_ROLE_ACCOUNT);
  1682. NewMemberAsLead { member_id, lead_id }
  1683. }
  1684. pub fn set_channel_creation_enabled(enabled: bool) {
  1685. crate::Module::<Test>::set_channel_creation_enabled(Origin::signed(LEAD_ROLE_ACCOUNT), enabled)
  1686. .unwrap()
  1687. }
  1688. pub fn add_channel_creator_member() -> <Test as members::Trait>::MemberId {
  1689. let channel_creator_member_id = add_member(
  1690. CHANNEL_CREATOR_ROOT_AND_CONTROLLER_ACCOUNT,
  1691. to_vec(CHANNEL_CREATOR_HANDLE),
  1692. );
  1693. channel_creator_member_id
  1694. }
  1695. pub fn add_member(
  1696. root_and_controller_account: <Test as system::Trait>::AccountId,
  1697. handle: Vec<u8>,
  1698. ) -> <Test as members::Trait>::MemberId {
  1699. let next_member_id = members::MembersCreated::<Test>::get();
  1700. assert_eq!(
  1701. members::Module::<Test>::buy_membership(
  1702. Origin::signed(root_and_controller_account),
  1703. 0,
  1704. members::UserInfo {
  1705. handle: Some(handle),
  1706. avatar_uri: None,
  1707. about: None,
  1708. }
  1709. )
  1710. .unwrap(),
  1711. ()
  1712. );
  1713. next_member_id
  1714. }
  1715. pub fn set_lead(
  1716. member_id: <Test as members::Trait>::MemberId,
  1717. new_role_account: <Test as system::Trait>::AccountId,
  1718. ) -> LeadId<Test> {
  1719. // Get controller account
  1720. //let lead_member_controller_account = members::Module::<Test>::ensure_profile(member_id).unwrap().controller_account;
  1721. let expected_lead_id = NextLeadId::<Test>::get();
  1722. // Set lead
  1723. assert_eq!(
  1724. ContentWorkingGroup::set_lead(
  1725. mock::Origin::system(system::RawOrigin::Root),
  1726. member_id,
  1727. new_role_account
  1728. )
  1729. .unwrap(),
  1730. ()
  1731. );
  1732. assert_eq!(
  1733. get_last_event_or_panic(),
  1734. crate::RawEvent::LeadSet(expected_lead_id)
  1735. );
  1736. expected_lead_id
  1737. }
  1738. // lead_role_account: <Test as system::Trait>::AccountId
  1739. pub fn add_curator_opening() -> CuratorOpeningId<Test> {
  1740. let activate_at = hiring::ActivateOpeningAt::ExactBlock(34);
  1741. let human_readable_text = generate_valid_length_buffer(&OpeningHumanReadableText::get());
  1742. let expected_curator_opening_id = NextCuratorOpeningId::<Test>::get();
  1743. assert_eq!(
  1744. ContentWorkingGroup::add_curator_opening(
  1745. Origin::signed(LEAD_ROLE_ACCOUNT),
  1746. activate_at.clone(),
  1747. get_baseline_opening_policy(),
  1748. human_readable_text.clone()
  1749. )
  1750. .unwrap(),
  1751. ()
  1752. );
  1753. assert_eq!(
  1754. get_last_event_or_panic(),
  1755. crate::RawEvent::CuratorOpeningAdded(expected_curator_opening_id)
  1756. );
  1757. expected_curator_opening_id
  1758. }
  1759. /*
  1760. * Buffer generators
  1761. */
  1762. pub fn generate_text(len: usize) -> Vec<u8> {
  1763. vec![b'x'; len]
  1764. }
  1765. pub fn generate_valid_length_buffer(constraint: &InputValidationLengthConstraint) -> Vec<u8> {
  1766. generate_text(constraint.min as usize)
  1767. }
  1768. pub fn generate_too_short_length_buffer(constraint: &InputValidationLengthConstraint) -> Vec<u8> {
  1769. generate_text((constraint.min - 1) as usize)
  1770. }
  1771. pub fn generate_too_long_length_buffer(constraint: &InputValidationLengthConstraint) -> Vec<u8> {
  1772. generate_text((constraint.max() + 1) as usize)
  1773. }