mod.rs 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998
  1. mod fixtures;
  2. mod hiring_workflow;
  3. mod mock;
  4. use crate::types::{OpeningPolicyCommitment, OpeningType, RewardPolicy};
  5. use crate::{Error, RawEvent, Worker};
  6. use common::constraints::InputValidationLengthConstraint;
  7. use mock::{
  8. build_test_externalities, Test, TestWorkingGroup, TestWorkingGroupInstance,
  9. WORKING_GROUP_CONSTRAINT_DIFF, WORKING_GROUP_CONSTRAINT_MIN, WORKING_GROUP_MINT_CAPACITY,
  10. };
  11. use srml_support::{StorageLinkedMap, StorageValue};
  12. use std::collections::BTreeMap;
  13. use system::RawOrigin;
  14. use crate::tests::hiring_workflow::HiringWorkflow;
  15. use fixtures::*;
  16. #[test]
  17. fn hire_lead_succeeds() {
  18. build_test_externalities().execute_with(|| {
  19. // Ensure that lead is default
  20. assert_eq!(TestWorkingGroup::current_lead(), None);
  21. HireLeadFixture::default().hire_lead();
  22. assert!(TestWorkingGroup::current_lead().is_some());
  23. });
  24. }
  25. #[test]
  26. fn hire_lead_fails_with_existing_lead() {
  27. build_test_externalities().execute_with(|| {
  28. HireLeadFixture::default().hire_lead();
  29. let hiring_workflow = HiringWorkflow::default()
  30. .disable_setup_environment()
  31. .with_opening_type(OpeningType::Leader)
  32. .add_application(b"leader_handle".to_vec())
  33. .expect(Err(Error::CannotHireLeaderWhenLeaderExists));
  34. hiring_workflow.execute();
  35. });
  36. }
  37. #[test]
  38. fn hire_lead_fails_multiple_applications() {
  39. build_test_externalities().execute_with(|| {
  40. let hiring_workflow = HiringWorkflow::default()
  41. .with_opening_type(OpeningType::Leader)
  42. .add_application_with_origin(b"leader_handle".to_vec(), RawOrigin::Signed(1), 1)
  43. .add_application_with_origin(b"leader_handle2".to_vec(), RawOrigin::Signed(2), 2)
  44. .expect(Err(Error::CannotHireMultipleLeaders));
  45. hiring_workflow.execute();
  46. });
  47. }
  48. #[test]
  49. fn add_opening_fails_with_incorrect_unstaking_periods() {
  50. build_test_externalities().execute_with(|| {
  51. HireLeadFixture::default().hire_lead();
  52. let add_opening_fixture =
  53. AddWorkerOpeningFixture::default().with_policy_commitment(OpeningPolicyCommitment {
  54. fill_opening_failed_applicant_role_stake_unstaking_period: Some(0),
  55. ..OpeningPolicyCommitment::default()
  56. });
  57. add_opening_fixture.call_and_assert(Err(
  58. Error::FillOpeningFailedApplicantRoleStakeUnstakingPeriodIsZero,
  59. ));
  60. let add_opening_fixture =
  61. AddWorkerOpeningFixture::default().with_policy_commitment(OpeningPolicyCommitment {
  62. fill_opening_failed_applicant_application_stake_unstaking_period: Some(0),
  63. ..OpeningPolicyCommitment::default()
  64. });
  65. add_opening_fixture.call_and_assert(Err(
  66. Error::FillOpeningFailedApplicantApplicationStakeUnstakingPeriodIsZero,
  67. ));
  68. let add_opening_fixture =
  69. AddWorkerOpeningFixture::default().with_policy_commitment(OpeningPolicyCommitment {
  70. fill_opening_successful_applicant_application_stake_unstaking_period: Some(0),
  71. ..OpeningPolicyCommitment::default()
  72. });
  73. add_opening_fixture.call_and_assert(Err(
  74. Error::FillOpeningSuccessfulApplicantApplicationStakeUnstakingPeriodIsZero,
  75. ));
  76. });
  77. }
  78. #[test]
  79. fn add_opening_succeeds() {
  80. build_test_externalities().execute_with(|| {
  81. HireLeadFixture::default().hire_lead();
  82. let add_opening_fixture = AddWorkerOpeningFixture::default();
  83. let opening_id = add_opening_fixture.call_and_assert(Ok(()));
  84. EventFixture::assert_last_crate_event(RawEvent::OpeningAdded(opening_id));
  85. });
  86. }
  87. #[test]
  88. fn add_leader_opening_succeeds_fails_with_incorrect_origin_for_opening_type() {
  89. build_test_externalities().execute_with(|| {
  90. HireLeadFixture::default().hire_lead();
  91. let add_opening_fixture =
  92. AddWorkerOpeningFixture::default().with_opening_type(OpeningType::Leader);
  93. add_opening_fixture.call_and_assert(Err(Error::RequireRootOrigin));
  94. });
  95. }
  96. #[test]
  97. fn add_leader_opening_succeeds() {
  98. build_test_externalities().execute_with(|| {
  99. HireLeadFixture::default().hire_lead();
  100. let add_opening_fixture = AddWorkerOpeningFixture::default()
  101. .with_opening_type(OpeningType::Leader)
  102. .with_origin(RawOrigin::Root);
  103. add_opening_fixture.call_and_assert(Ok(()));
  104. });
  105. }
  106. #[test]
  107. fn add_opening_fails_with_lead_is_not_set() {
  108. build_test_externalities().execute_with(|| {
  109. let add_opening_fixture = AddWorkerOpeningFixture::default();
  110. add_opening_fixture.call_and_assert(Err(Error::CurrentLeadNotSet));
  111. });
  112. }
  113. #[test]
  114. fn add_opening_fails_with_invalid_human_readable_text() {
  115. build_test_externalities().execute_with(|| {
  116. HireLeadFixture::default().hire_lead();
  117. <crate::OpeningHumanReadableText<TestWorkingGroupInstance>>::put(
  118. InputValidationLengthConstraint {
  119. min: 1,
  120. max_min_diff: 5,
  121. },
  122. );
  123. let add_opening_fixture = AddWorkerOpeningFixture::default().with_text(Vec::new());
  124. add_opening_fixture.call_and_assert(Err(Error::Other("OpeningTextTooShort")));
  125. let add_opening_fixture =
  126. AddWorkerOpeningFixture::default().with_text(b"Long text".to_vec());
  127. add_opening_fixture.call_and_assert(Err(Error::Other("OpeningTextTooLong")));
  128. });
  129. }
  130. #[test]
  131. fn add_opening_fails_with_hiring_error() {
  132. build_test_externalities().execute_with(|| {
  133. HireLeadFixture::default().hire_lead();
  134. let add_opening_fixture = AddWorkerOpeningFixture::default()
  135. .with_activate_at(hiring::ActivateOpeningAt::ExactBlock(0));
  136. add_opening_fixture.call_and_assert(Err(Error::AddWorkerOpeningActivatesInThePast));
  137. });
  138. }
  139. #[test]
  140. fn accept_applications_succeeds() {
  141. build_test_externalities().execute_with(|| {
  142. HireLeadFixture::default().hire_lead();
  143. let add_opening_fixture = AddWorkerOpeningFixture::default()
  144. .with_activate_at(hiring::ActivateOpeningAt::ExactBlock(5));
  145. let opening_id = add_opening_fixture.call_and_assert(Ok(()));
  146. let accept_applications_fixture =
  147. AcceptWorkerApplicationsFixture::default_for_opening_id(opening_id);
  148. accept_applications_fixture.call_and_assert(Ok(()));
  149. EventFixture::assert_last_crate_event(RawEvent::AcceptedApplications(opening_id));
  150. });
  151. }
  152. #[test]
  153. fn accept_applications_fails_for_invalid_opening_type() {
  154. build_test_externalities().execute_with(|| {
  155. HireLeadFixture::default().hire_lead();
  156. let add_opening_fixture = AddWorkerOpeningFixture::default()
  157. .with_origin(RawOrigin::Root)
  158. .with_opening_type(OpeningType::Leader)
  159. .with_activate_at(hiring::ActivateOpeningAt::ExactBlock(5));
  160. let opening_id = add_opening_fixture.call_and_assert(Ok(()));
  161. let accept_applications_fixture =
  162. AcceptWorkerApplicationsFixture::default_for_opening_id(opening_id);
  163. accept_applications_fixture.call_and_assert(Err(Error::RequireRootOrigin));
  164. });
  165. }
  166. #[test]
  167. fn accept_applications_fails_with_hiring_error() {
  168. build_test_externalities().execute_with(|| {
  169. HireLeadFixture::default().hire_lead();
  170. let add_opening_fixture = AddWorkerOpeningFixture::default();
  171. let opening_id = add_opening_fixture.call_and_assert(Ok(()));
  172. let accept_applications_fixture =
  173. AcceptWorkerApplicationsFixture::default_for_opening_id(opening_id);
  174. accept_applications_fixture.call_and_assert(Err(
  175. Error::AcceptWorkerApplicationsOpeningIsNotWaitingToBegin,
  176. ));
  177. });
  178. }
  179. #[test]
  180. fn accept_applications_fails_with_not_lead() {
  181. build_test_externalities().execute_with(|| {
  182. HireLeadFixture::default().hire_lead();
  183. let add_opening_fixture = AddWorkerOpeningFixture::default();
  184. let opening_id = add_opening_fixture.call_and_assert(Ok(()));
  185. SetLeadFixture::set_lead_with_ids(2, 2, 2);
  186. let accept_applications_fixture =
  187. AcceptWorkerApplicationsFixture::default_for_opening_id(opening_id);
  188. accept_applications_fixture.call_and_assert(Err(Error::IsNotLeadAccount));
  189. });
  190. }
  191. #[test]
  192. fn accept_applications_fails_with_no_opening() {
  193. build_test_externalities().execute_with(|| {
  194. HireLeadFixture::default().hire_lead();
  195. let opening_id = 55; // random opening id
  196. let accept_applications_fixture =
  197. AcceptWorkerApplicationsFixture::default_for_opening_id(opening_id);
  198. accept_applications_fixture.call_and_assert(Err(Error::OpeningDoesNotExist));
  199. });
  200. }
  201. #[test]
  202. fn apply_on_opening_succeeds() {
  203. build_test_externalities().execute_with(|| {
  204. HireLeadFixture::default().hire_lead();
  205. let add_opening_fixture = AddWorkerOpeningFixture::default();
  206. let opening_id = add_opening_fixture.call_and_assert(Ok(()));
  207. let apply_on_opening_fixture =
  208. ApplyOnWorkerOpeningFixture::default_for_opening_id(opening_id);
  209. let application_id = apply_on_opening_fixture.call_and_assert(Ok(()));
  210. EventFixture::assert_last_crate_event(RawEvent::AppliedOnOpening(
  211. opening_id,
  212. application_id,
  213. ));
  214. });
  215. }
  216. #[test]
  217. fn apply_on_opening_fails_with_no_opening() {
  218. build_test_externalities().execute_with(|| {
  219. HireLeadFixture::default().hire_lead();
  220. let opening_id = 123; // random opening id
  221. let apply_on_opening_fixture =
  222. ApplyOnWorkerOpeningFixture::default_for_opening_id(opening_id);
  223. apply_on_opening_fixture.call_and_assert(Err(Error::OpeningDoesNotExist));
  224. });
  225. }
  226. #[test]
  227. fn apply_on_opening_fails_with_not_set_members() {
  228. build_test_externalities().execute_with(|| {
  229. HireLeadFixture::default().hire_lead();
  230. let add_opening_fixture = AddWorkerOpeningFixture::default();
  231. let opening_id = add_opening_fixture.call_and_assert(Ok(()));
  232. let apply_on_opening_fixture =
  233. ApplyOnWorkerOpeningFixture::default_for_opening_id(opening_id)
  234. .with_origin(RawOrigin::Signed(55), 55);
  235. apply_on_opening_fixture.call_and_assert(Err(Error::OriginIsNeitherMemberControllerOrRoot));
  236. });
  237. }
  238. #[test]
  239. fn apply_on_opening_fails_with_hiring_error() {
  240. build_test_externalities().execute_with(|| {
  241. increase_total_balance_issuance_using_account_id(1, 500000);
  242. HireLeadFixture::default().hire_lead();
  243. let add_opening_fixture = AddWorkerOpeningFixture::default();
  244. let opening_id = add_opening_fixture.call_and_assert(Ok(()));
  245. let apply_on_opening_fixture =
  246. ApplyOnWorkerOpeningFixture::default_for_opening_id(opening_id)
  247. .with_application_stake(100);
  248. apply_on_opening_fixture
  249. .call_and_assert(Err(Error::AddWorkerOpeningStakeProvidedWhenRedundant));
  250. });
  251. }
  252. #[test]
  253. fn apply_on_opening_fails_with_invalid_application_stake() {
  254. build_test_externalities().execute_with(|| {
  255. HireLeadFixture::default().hire_lead();
  256. let stake = 100;
  257. let add_opening_fixture =
  258. AddWorkerOpeningFixture::default().with_policy_commitment(OpeningPolicyCommitment {
  259. application_staking_policy: Some(hiring::StakingPolicy {
  260. amount: stake,
  261. ..hiring::StakingPolicy::default()
  262. }),
  263. ..OpeningPolicyCommitment::default()
  264. });
  265. let opening_id = add_opening_fixture.call_and_assert(Ok(()));
  266. let apply_on_opening_fixture =
  267. ApplyOnWorkerOpeningFixture::default_for_opening_id(opening_id)
  268. .with_origin(RawOrigin::Signed(2), 2)
  269. .with_application_stake(stake);
  270. apply_on_opening_fixture.call_and_assert(Err(Error::InsufficientBalanceToApply));
  271. });
  272. }
  273. #[test]
  274. fn add_opening_fails_with_invalid_zero_application_stake() {
  275. build_test_externalities().execute_with(|| {
  276. HireLeadFixture::default().hire_lead();
  277. let zero_stake = 0;
  278. let add_opening_fixture =
  279. AddWorkerOpeningFixture::default().with_policy_commitment(OpeningPolicyCommitment {
  280. application_staking_policy: Some(hiring::StakingPolicy {
  281. amount: zero_stake,
  282. amount_mode: hiring::StakingAmountLimitMode::AtLeast,
  283. ..hiring::StakingPolicy::default()
  284. }),
  285. ..OpeningPolicyCommitment::default()
  286. });
  287. add_opening_fixture
  288. .call_and_assert(Err(Error::AddWorkerOpeningApplicationStakeCannotBeZero));
  289. });
  290. }
  291. #[test]
  292. fn apply_on_opening_fails_with_invalid_role_stake() {
  293. build_test_externalities().execute_with(|| {
  294. HireLeadFixture::default().hire_lead();
  295. let stake = 100;
  296. let add_opening_fixture =
  297. AddWorkerOpeningFixture::default().with_policy_commitment(OpeningPolicyCommitment {
  298. role_staking_policy: Some(hiring::StakingPolicy {
  299. amount: stake,
  300. ..hiring::StakingPolicy::default()
  301. }),
  302. ..OpeningPolicyCommitment::default()
  303. });
  304. let opening_id = add_opening_fixture.call_and_assert(Ok(()));
  305. let apply_on_opening_fixture =
  306. ApplyOnWorkerOpeningFixture::default_for_opening_id(opening_id)
  307. .with_role_stake(Some(stake))
  308. .with_origin(RawOrigin::Signed(2), 2);
  309. apply_on_opening_fixture.call_and_assert(Err(Error::InsufficientBalanceToApply));
  310. });
  311. }
  312. #[test]
  313. fn apply_on_opening_fails_with_invalid_text() {
  314. build_test_externalities().execute_with(|| {
  315. HireLeadFixture::default().hire_lead();
  316. let add_opening_fixture = AddWorkerOpeningFixture::default();
  317. let opening_id = add_opening_fixture.call_and_assert(Ok(()));
  318. <crate::WorkerApplicationHumanReadableText<TestWorkingGroupInstance>>::put(
  319. InputValidationLengthConstraint {
  320. min: 1,
  321. max_min_diff: 5,
  322. },
  323. );
  324. let apply_on_opening_fixture =
  325. ApplyOnWorkerOpeningFixture::default_for_opening_id(opening_id).with_text(Vec::new());
  326. apply_on_opening_fixture
  327. .call_and_assert(Err(Error::Other("WorkerApplicationTextTooShort")));
  328. let apply_on_opening_fixture =
  329. ApplyOnWorkerOpeningFixture::default_for_opening_id(opening_id)
  330. .with_text(b"Long text".to_vec());
  331. apply_on_opening_fixture.call_and_assert(Err(Error::Other("WorkerApplicationTextTooLong")));
  332. });
  333. }
  334. #[test]
  335. fn apply_on_opening_fails_with_already_active_application() {
  336. build_test_externalities().execute_with(|| {
  337. HireLeadFixture::default().hire_lead();
  338. let add_opening_fixture = AddWorkerOpeningFixture::default();
  339. let opening_id = add_opening_fixture.call_and_assert(Ok(()));
  340. let apply_on_opening_fixture =
  341. ApplyOnWorkerOpeningFixture::default_for_opening_id(opening_id);
  342. apply_on_opening_fixture.call_and_assert(Ok(()));
  343. apply_on_opening_fixture.call_and_assert(Err(Error::MemberHasActiveApplicationOnOpening));
  344. });
  345. }
  346. #[test]
  347. fn withdraw_worker_application_succeeds() {
  348. build_test_externalities().execute_with(|| {
  349. HireLeadFixture::default().hire_lead();
  350. let add_opening_fixture = AddWorkerOpeningFixture::default();
  351. let opening_id = add_opening_fixture.call_and_assert(Ok(()));
  352. let apply_on_opening_fixture =
  353. ApplyOnWorkerOpeningFixture::default_for_opening_id(opening_id);
  354. let application_id = apply_on_opening_fixture.call_and_assert(Ok(()));
  355. let withdraw_application_fixture =
  356. WithdrawApplicationFixture::default_for_application_id(application_id);
  357. withdraw_application_fixture.call_and_assert(Ok(()));
  358. EventFixture::assert_last_crate_event(RawEvent::ApplicationWithdrawn(application_id));
  359. });
  360. }
  361. #[test]
  362. fn withdraw_worker_application_fails_invalid_application_id() {
  363. build_test_externalities().execute_with(|| {
  364. let invalid_application_id = 6;
  365. let withdraw_application_fixture =
  366. WithdrawApplicationFixture::default_for_application_id(invalid_application_id);
  367. withdraw_application_fixture.call_and_assert(Err(Error::WorkerApplicationDoesNotExist));
  368. });
  369. }
  370. #[test]
  371. fn withdraw_worker_application_fails_invalid_origin() {
  372. build_test_externalities().execute_with(|| {
  373. HireLeadFixture::default().hire_lead();
  374. let add_opening_fixture = AddWorkerOpeningFixture::default();
  375. let opening_id = add_opening_fixture.call_and_assert(Ok(()));
  376. let apply_on_opening_fixture =
  377. ApplyOnWorkerOpeningFixture::default_for_opening_id(opening_id);
  378. let application_id = apply_on_opening_fixture.call_and_assert(Ok(()));
  379. let withdraw_application_fixture =
  380. WithdrawApplicationFixture::default_for_application_id(application_id)
  381. .with_origin(RawOrigin::None);
  382. withdraw_application_fixture.call_and_assert(Err(Error::RequireSignedOrigin));
  383. });
  384. }
  385. #[test]
  386. fn withdraw_worker_application_fails_with_invalid_application_author() {
  387. build_test_externalities().execute_with(|| {
  388. HireLeadFixture::default().hire_lead();
  389. let add_opening_fixture = AddWorkerOpeningFixture::default();
  390. let opening_id = add_opening_fixture.call_and_assert(Ok(()));
  391. let apply_on_opening_fixture =
  392. ApplyOnWorkerOpeningFixture::default_for_opening_id(opening_id);
  393. let application_id = apply_on_opening_fixture.call_and_assert(Ok(()));
  394. let invalid_author_account_id = 55;
  395. let withdraw_application_fixture =
  396. WithdrawApplicationFixture::default_for_application_id(application_id)
  397. .with_signer(invalid_author_account_id);
  398. withdraw_application_fixture.call_and_assert(Err(Error::OriginIsNotApplicant));
  399. });
  400. }
  401. #[test]
  402. fn withdraw_worker_application_fails_with_hiring_error() {
  403. build_test_externalities().execute_with(|| {
  404. HireLeadFixture::default().hire_lead();
  405. let add_opening_fixture = AddWorkerOpeningFixture::default();
  406. let opening_id = add_opening_fixture.call_and_assert(Ok(()));
  407. let apply_on_opening_fixture =
  408. ApplyOnWorkerOpeningFixture::default_for_opening_id(opening_id);
  409. let application_id = apply_on_opening_fixture.call_and_assert(Ok(()));
  410. let withdraw_application_fixture =
  411. WithdrawApplicationFixture::default_for_application_id(application_id);
  412. withdraw_application_fixture.call_and_assert(Ok(()));
  413. withdraw_application_fixture
  414. .call_and_assert(Err(Error::WithdrawWorkerApplicationApplicationNotActive));
  415. });
  416. }
  417. #[test]
  418. fn terminate_worker_application_succeeds() {
  419. build_test_externalities().execute_with(|| {
  420. HireLeadFixture::default().hire_lead();
  421. let add_opening_fixture = AddWorkerOpeningFixture::default();
  422. let opening_id = add_opening_fixture.call_and_assert(Ok(()));
  423. let apply_on_opening_fixture =
  424. ApplyOnWorkerOpeningFixture::default_for_opening_id(opening_id);
  425. let application_id = apply_on_opening_fixture.call_and_assert(Ok(()));
  426. let terminate_application_fixture =
  427. TerminateApplicationFixture::default_for_application_id(application_id);
  428. terminate_application_fixture.call_and_assert(Ok(()));
  429. EventFixture::assert_last_crate_event(RawEvent::ApplicationTerminated(application_id));
  430. });
  431. }
  432. #[test]
  433. fn terminate_worker_application_fails_with_invalid_application_author() {
  434. build_test_externalities().execute_with(|| {
  435. HireLeadFixture::default().hire_lead();
  436. let add_opening_fixture = AddWorkerOpeningFixture::default();
  437. let opening_id = add_opening_fixture.call_and_assert(Ok(()));
  438. let apply_on_opening_fixture =
  439. ApplyOnWorkerOpeningFixture::default_for_opening_id(opening_id);
  440. let application_id = apply_on_opening_fixture.call_and_assert(Ok(()));
  441. let invalid_author_account_id = 55;
  442. let terminate_application_fixture =
  443. TerminateApplicationFixture::default_for_application_id(application_id)
  444. .with_signer(invalid_author_account_id);
  445. terminate_application_fixture.call_and_assert(Err(Error::IsNotLeadAccount));
  446. });
  447. }
  448. #[test]
  449. fn terminate_worker_application_fails_invalid_origin() {
  450. build_test_externalities().execute_with(|| {
  451. HireLeadFixture::default().hire_lead();
  452. let add_opening_fixture = AddWorkerOpeningFixture::default();
  453. let opening_id = add_opening_fixture.call_and_assert(Ok(()));
  454. let apply_on_opening_fixture =
  455. ApplyOnWorkerOpeningFixture::default_for_opening_id(opening_id);
  456. let application_id = apply_on_opening_fixture.call_and_assert(Ok(()));
  457. let terminate_application_fixture =
  458. TerminateApplicationFixture::default_for_application_id(application_id)
  459. .with_origin(RawOrigin::None);
  460. terminate_application_fixture.call_and_assert(Err(Error::RequireSignedOrigin));
  461. });
  462. }
  463. #[test]
  464. fn terminate_worker_application_fails_invalid_application_id() {
  465. build_test_externalities().execute_with(|| {
  466. HireLeadFixture::default().hire_lead();
  467. let invalid_application_id = 6;
  468. let terminate_application_fixture =
  469. TerminateApplicationFixture::default_for_application_id(invalid_application_id);
  470. terminate_application_fixture.call_and_assert(Err(Error::WorkerApplicationDoesNotExist));
  471. });
  472. }
  473. #[test]
  474. fn terminate_worker_application_fails_with_hiring_error() {
  475. build_test_externalities().execute_with(|| {
  476. HireLeadFixture::default().hire_lead();
  477. let add_opening_fixture = AddWorkerOpeningFixture::default();
  478. let opening_id = add_opening_fixture.call_and_assert(Ok(()));
  479. let apply_on_opening_fixture =
  480. ApplyOnWorkerOpeningFixture::default_for_opening_id(opening_id);
  481. let application_id = apply_on_opening_fixture.call_and_assert(Ok(()));
  482. let terminate_application_fixture =
  483. TerminateApplicationFixture::default_for_application_id(application_id);
  484. terminate_application_fixture.call_and_assert(Ok(()));
  485. terminate_application_fixture
  486. .call_and_assert(Err(Error::WithdrawWorkerApplicationApplicationNotActive));
  487. });
  488. }
  489. #[test]
  490. fn begin_review_worker_applications_succeeds() {
  491. build_test_externalities().execute_with(|| {
  492. HireLeadFixture::default().hire_lead();
  493. let add_opening_fixture = AddWorkerOpeningFixture::default();
  494. let opening_id = add_opening_fixture.call_and_assert(Ok(()));
  495. let begin_review_worker_applications_fixture =
  496. BeginReviewWorkerApplicationsFixture::default_for_opening_id(opening_id);
  497. begin_review_worker_applications_fixture.call_and_assert(Ok(()));
  498. EventFixture::assert_last_crate_event(RawEvent::BeganApplicationReview(opening_id));
  499. });
  500. }
  501. #[test]
  502. fn begin_review_worker_applications_fails_with_invalid_origin_for_opening_type() {
  503. build_test_externalities().execute_with(|| {
  504. HireLeadFixture::default().hire_lead();
  505. let add_opening_fixture = AddWorkerOpeningFixture::default()
  506. .with_origin(RawOrigin::Root)
  507. .with_opening_type(OpeningType::Leader);
  508. let opening_id = add_opening_fixture.call_and_assert(Ok(()));
  509. let begin_review_worker_applications_fixture =
  510. BeginReviewWorkerApplicationsFixture::default_for_opening_id(opening_id);
  511. begin_review_worker_applications_fixture.call_and_assert(Err(Error::RequireRootOrigin));
  512. });
  513. }
  514. #[test]
  515. fn begin_review_worker_applications_fails_with_not_a_lead() {
  516. build_test_externalities().execute_with(|| {
  517. HireLeadFixture::default().hire_lead();
  518. let add_opening_fixture = AddWorkerOpeningFixture::default();
  519. let opening_id = add_opening_fixture.call_and_assert(Ok(()));
  520. SetLeadFixture::set_lead_with_ids(2, 2, 2);
  521. let begin_review_worker_applications_fixture =
  522. BeginReviewWorkerApplicationsFixture::default_for_opening_id(opening_id);
  523. begin_review_worker_applications_fixture.call_and_assert(Err(Error::IsNotLeadAccount));
  524. });
  525. }
  526. #[test]
  527. fn begin_review_worker_applications_fails_with_invalid_opening() {
  528. build_test_externalities().execute_with(|| {
  529. HireLeadFixture::default().hire_lead();
  530. let invalid_opening_id = 6;
  531. let begin_review_worker_applications_fixture =
  532. BeginReviewWorkerApplicationsFixture::default_for_opening_id(invalid_opening_id);
  533. begin_review_worker_applications_fixture.call_and_assert(Err(Error::OpeningDoesNotExist));
  534. });
  535. }
  536. #[test]
  537. fn begin_review_worker_applications_with_hiring_error() {
  538. build_test_externalities().execute_with(|| {
  539. HireLeadFixture::default().hire_lead();
  540. let add_opening_fixture = AddWorkerOpeningFixture::default();
  541. let opening_id = add_opening_fixture.call_and_assert(Ok(()));
  542. let begin_review_worker_applications_fixture =
  543. BeginReviewWorkerApplicationsFixture::default_for_opening_id(opening_id);
  544. begin_review_worker_applications_fixture.call_and_assert(Ok(()));
  545. begin_review_worker_applications_fixture.call_and_assert(Err(
  546. Error::BeginWorkerApplicantReviewOpeningOpeningIsNotWaitingToBegin,
  547. ));
  548. });
  549. }
  550. #[test]
  551. fn begin_review_worker_applications_fails_with_invalid_origin() {
  552. build_test_externalities().execute_with(|| {
  553. HireLeadFixture::default().hire_lead();
  554. let add_opening_fixture = AddWorkerOpeningFixture::default();
  555. let opening_id = add_opening_fixture.call_and_assert(Ok(()));
  556. let begin_review_worker_applications_fixture =
  557. BeginReviewWorkerApplicationsFixture::default_for_opening_id(opening_id)
  558. .with_origin(RawOrigin::None);
  559. begin_review_worker_applications_fixture.call_and_assert(Err(Error::RequireSignedOrigin));
  560. });
  561. }
  562. #[test]
  563. fn fill_opening_succeeds() {
  564. build_test_externalities().execute_with(|| {
  565. HireLeadFixture::default().hire_lead();
  566. increase_total_balance_issuance_using_account_id(1, 10000);
  567. let add_opening_fixture =
  568. AddWorkerOpeningFixture::default().with_policy_commitment(OpeningPolicyCommitment {
  569. role_staking_policy: Some(hiring::StakingPolicy {
  570. amount: 10,
  571. amount_mode: hiring::StakingAmountLimitMode::AtLeast,
  572. crowded_out_unstaking_period_length: None,
  573. review_period_expired_unstaking_period_length: None,
  574. }),
  575. ..OpeningPolicyCommitment::default()
  576. });
  577. let opening_id = add_opening_fixture.call_and_assert(Ok(()));
  578. let apply_on_opening_fixture =
  579. ApplyOnWorkerOpeningFixture::default_for_opening_id(opening_id)
  580. .with_role_stake(Some(10));
  581. let application_id = apply_on_opening_fixture.call_and_assert(Ok(()));
  582. let begin_review_worker_applications_fixture =
  583. BeginReviewWorkerApplicationsFixture::default_for_opening_id(opening_id);
  584. begin_review_worker_applications_fixture.call_and_assert(Ok(()));
  585. let mint_id = create_mint();
  586. set_mint_id(mint_id);
  587. let fill_opening_fixture =
  588. FillWorkerOpeningFixture::default_for_ids(opening_id, vec![application_id])
  589. .with_reward_policy(RewardPolicy {
  590. amount_per_payout: 1000,
  591. next_payment_at_block: 20,
  592. payout_interval: None,
  593. });
  594. let worker_id = fill_opening_fixture.call_and_assert(Ok(()));
  595. let mut worker_application_dictionary = BTreeMap::new();
  596. worker_application_dictionary.insert(application_id, worker_id);
  597. EventFixture::assert_last_crate_event(RawEvent::OpeningFilled(
  598. opening_id,
  599. worker_application_dictionary,
  600. ));
  601. });
  602. }
  603. #[test]
  604. fn fill_opening_fails_with_invalid_origin_for_opening_type() {
  605. build_test_externalities().execute_with(|| {
  606. HireLeadFixture::default().hire_lead();
  607. increase_total_balance_issuance_using_account_id(1, 10000);
  608. let add_opening_fixture = AddWorkerOpeningFixture::default()
  609. .with_policy_commitment(OpeningPolicyCommitment {
  610. role_staking_policy: Some(hiring::StakingPolicy {
  611. amount: 10,
  612. amount_mode: hiring::StakingAmountLimitMode::AtLeast,
  613. crowded_out_unstaking_period_length: None,
  614. review_period_expired_unstaking_period_length: None,
  615. }),
  616. ..OpeningPolicyCommitment::default()
  617. })
  618. .with_opening_type(OpeningType::Leader)
  619. .with_origin(RawOrigin::Root);
  620. let opening_id = add_opening_fixture.call_and_assert(Ok(()));
  621. let apply_on_opening_fixture =
  622. ApplyOnWorkerOpeningFixture::default_for_opening_id(opening_id)
  623. .with_role_stake(Some(10));
  624. let application_id = apply_on_opening_fixture.call_and_assert(Ok(()));
  625. let begin_review_worker_applications_fixture =
  626. BeginReviewWorkerApplicationsFixture::default_for_opening_id(opening_id)
  627. .with_origin(RawOrigin::Root);
  628. begin_review_worker_applications_fixture.call_and_assert(Ok(()));
  629. set_mint_id(create_mint());
  630. let fill_opening_fixture =
  631. FillWorkerOpeningFixture::default_for_ids(opening_id, vec![application_id])
  632. .with_reward_policy(RewardPolicy {
  633. amount_per_payout: 1000,
  634. next_payment_at_block: 20,
  635. payout_interval: None,
  636. });
  637. fill_opening_fixture.call_and_assert(Err(Error::RequireRootOrigin));
  638. });
  639. }
  640. #[test]
  641. fn fill_opening_fails_with_invalid_origin() {
  642. build_test_externalities().execute_with(|| {
  643. HireLeadFixture::default().hire_lead();
  644. let add_opening_fixture = AddWorkerOpeningFixture::default();
  645. let opening_id = add_opening_fixture.call_and_assert(Ok(()));
  646. let fill_opening_fixture =
  647. FillWorkerOpeningFixture::default_for_ids(opening_id, Vec::new())
  648. .with_origin(RawOrigin::None);
  649. fill_opening_fixture.call_and_assert(Err(Error::RequireSignedOrigin));
  650. });
  651. }
  652. #[test]
  653. fn fill_opening_fails_with_not_a_lead() {
  654. build_test_externalities().execute_with(|| {
  655. HireLeadFixture::default().hire_lead();
  656. let add_opening_fixture = AddWorkerOpeningFixture::default();
  657. let opening_id = add_opening_fixture.call_and_assert(Ok(()));
  658. SetLeadFixture::set_lead_with_ids(2, 2, 2);
  659. let fill_opening_fixture =
  660. FillWorkerOpeningFixture::default_for_ids(opening_id, Vec::new());
  661. fill_opening_fixture.call_and_assert(Err(Error::IsNotLeadAccount));
  662. });
  663. }
  664. #[test]
  665. fn fill_opening_fails_with_invalid_opening() {
  666. build_test_externalities().execute_with(|| {
  667. HireLeadFixture::default().hire_lead();
  668. let invalid_opening_id = 6;
  669. let fill_opening_fixture =
  670. FillWorkerOpeningFixture::default_for_ids(invalid_opening_id, Vec::new());
  671. fill_opening_fixture.call_and_assert(Err(Error::OpeningDoesNotExist));
  672. });
  673. }
  674. #[test]
  675. fn fill_opening_fails_with_invalid_application_list() {
  676. build_test_externalities().execute_with(|| {
  677. HireLeadFixture::default().hire_lead();
  678. let add_opening_fixture = AddWorkerOpeningFixture::default();
  679. let opening_id = add_opening_fixture.call_and_assert(Ok(()));
  680. let apply_on_opening_fixture =
  681. ApplyOnWorkerOpeningFixture::default_for_opening_id(opening_id);
  682. let application_id = apply_on_opening_fixture.call_and_assert(Ok(()));
  683. let begin_review_worker_applications_fixture =
  684. BeginReviewWorkerApplicationsFixture::default_for_opening_id(opening_id);
  685. begin_review_worker_applications_fixture.call_and_assert(Ok(()));
  686. let invalid_application_id = 66;
  687. let fill_opening_fixture = FillWorkerOpeningFixture::default_for_ids(
  688. opening_id,
  689. vec![application_id, invalid_application_id],
  690. );
  691. fill_opening_fixture.call_and_assert(Err(Error::SuccessfulWorkerApplicationDoesNotExist));
  692. });
  693. }
  694. #[test]
  695. fn fill_opening_fails_with_invalid_application_with_hiring_error() {
  696. build_test_externalities().execute_with(|| {
  697. HireLeadFixture::default().hire_lead();
  698. let add_opening_fixture = AddWorkerOpeningFixture::default();
  699. let opening_id = add_opening_fixture.call_and_assert(Ok(()));
  700. let fill_opening_fixture =
  701. FillWorkerOpeningFixture::default_for_ids(opening_id, Vec::new());
  702. fill_opening_fixture
  703. .call_and_assert(Err(Error::FullWorkerOpeningOpeningNotInReviewPeriodStage));
  704. });
  705. }
  706. #[test]
  707. fn fill_opening_fails_with_invalid_reward_policy() {
  708. build_test_externalities().execute_with(|| {
  709. HireLeadFixture::default().hire_lead();
  710. let add_opening_fixture = AddWorkerOpeningFixture::default();
  711. let opening_id = add_opening_fixture.call_and_assert(Ok(()));
  712. let apply_on_opening_fixture =
  713. ApplyOnWorkerOpeningFixture::default_for_opening_id(opening_id);
  714. let application_id = apply_on_opening_fixture.call_and_assert(Ok(()));
  715. let begin_review_worker_applications_fixture =
  716. BeginReviewWorkerApplicationsFixture::default_for_opening_id(opening_id);
  717. begin_review_worker_applications_fixture.call_and_assert(Ok(()));
  718. let fill_opening_fixture =
  719. FillWorkerOpeningFixture::default_for_ids(opening_id, vec![application_id])
  720. .with_reward_policy(RewardPolicy {
  721. amount_per_payout: 10000,
  722. // Invalid next payment at block zero
  723. next_payment_at_block: 0,
  724. payout_interval: None,
  725. });
  726. fill_opening_fixture
  727. });
  728. }
  729. #[test]
  730. fn update_worker_role_account_succeeds() {
  731. build_test_externalities().execute_with(|| {
  732. let new_account_id = 10;
  733. let worker_id = fill_default_worker_position();
  734. let update_worker_account_fixture =
  735. UpdateWorkerRoleAccountFixture::default_with_ids(worker_id, new_account_id);
  736. update_worker_account_fixture.call_and_assert(Ok(()));
  737. EventFixture::assert_last_crate_event(RawEvent::WorkerRoleAccountUpdated(
  738. worker_id,
  739. new_account_id,
  740. ));
  741. });
  742. }
  743. #[test]
  744. fn update_worker_role_account_by_leader_succeeds() {
  745. build_test_externalities().execute_with(|| {
  746. let new_account_id = 10;
  747. let worker_id = HireLeadFixture::default().hire_lead();
  748. let old_lead = TestWorkingGroup::worker_by_id(worker_id);
  749. let update_worker_account_fixture =
  750. UpdateWorkerRoleAccountFixture::default_with_ids(worker_id, new_account_id);
  751. update_worker_account_fixture.call_and_assert(Ok(()));
  752. let new_lead = TestWorkingGroup::worker_by_id(worker_id);
  753. assert_eq!(
  754. new_lead,
  755. Worker {
  756. role_account_id: new_account_id,
  757. ..old_lead
  758. }
  759. );
  760. });
  761. }
  762. #[test]
  763. fn update_worker_role_account_fails_with_invalid_origin() {
  764. build_test_externalities().execute_with(|| {
  765. let worker_id = fill_default_worker_position();
  766. let update_worker_account_fixture =
  767. UpdateWorkerRoleAccountFixture::default_with_ids(worker_id, 1)
  768. .with_origin(RawOrigin::None);
  769. update_worker_account_fixture.call_and_assert(Err(Error::MembershipUnsignedOrigin));
  770. });
  771. }
  772. #[test]
  773. fn update_worker_reward_account_succeeds() {
  774. build_test_externalities().execute_with(|| {
  775. let worker_id = fill_default_worker_position();
  776. let new_role_account = 22;
  777. let update_worker_account_fixture =
  778. UpdateWorkerRewardAccountFixture::default_with_ids(worker_id, new_role_account);
  779. update_worker_account_fixture.call_and_assert(Ok(()));
  780. EventFixture::assert_last_crate_event(RawEvent::WorkerRewardAccountUpdated(
  781. worker_id,
  782. new_role_account,
  783. ));
  784. });
  785. }
  786. #[test]
  787. fn update_worker_reward_account_succeeds_for_leader() {
  788. build_test_externalities().execute_with(|| {
  789. let worker_id = HireLeadFixture::default()
  790. .with_reward_policy(RewardPolicy {
  791. amount_per_payout: 1000,
  792. next_payment_at_block: 20,
  793. payout_interval: None,
  794. })
  795. .hire_lead();
  796. let new_reward_account = 22;
  797. let update_worker_account_fixture =
  798. UpdateWorkerRewardAccountFixture::default_with_ids(worker_id, new_reward_account);
  799. update_worker_account_fixture.call_and_assert(Ok(()));
  800. });
  801. }
  802. #[test]
  803. fn update_worker_reward_account_fails_with_invalid_origin() {
  804. build_test_externalities().execute_with(|| {
  805. let update_worker_account_fixture =
  806. UpdateWorkerRewardAccountFixture::default_with_ids(1, 1).with_origin(RawOrigin::None);
  807. update_worker_account_fixture.call_and_assert(Err(Error::RequireSignedOrigin));
  808. });
  809. }
  810. #[test]
  811. fn update_worker_reward_account_fails_with_invalid_origin_signed_account() {
  812. build_test_externalities().execute_with(|| {
  813. let worker_id = fill_default_worker_position();
  814. let worker = get_worker_by_id(worker_id);
  815. let invalid_role_account = 23333;
  816. let update_worker_account_fixture =
  817. UpdateWorkerRewardAccountFixture::default_with_ids(worker_id, worker.role_account_id)
  818. .with_origin(RawOrigin::Signed(invalid_role_account));
  819. update_worker_account_fixture.call_and_assert(Err(Error::SignerIsNotWorkerRoleAccount));
  820. });
  821. }
  822. #[test]
  823. fn update_worker_reward_account_fails_with_invalid_worker_id() {
  824. build_test_externalities().execute_with(|| {
  825. let invalid_worker_id = 1;
  826. fill_default_worker_position();
  827. let new_reward_account = 2;
  828. let update_worker_account_fixture = UpdateWorkerRewardAccountFixture::default_with_ids(
  829. invalid_worker_id,
  830. new_reward_account,
  831. );
  832. update_worker_account_fixture.call_and_assert(Err(Error::WorkerDoesNotExist));
  833. });
  834. }
  835. #[test]
  836. fn update_worker_reward_account_fails_with_no_recurring_reward() {
  837. build_test_externalities().execute_with(|| {
  838. let worker_id = fill_worker_position_with_no_reward();
  839. let new_reward_account = 343;
  840. let update_worker_account_fixture =
  841. UpdateWorkerRewardAccountFixture::default_with_ids(worker_id, new_reward_account);
  842. update_worker_account_fixture.call_and_assert(Err(Error::WorkerHasNoReward));
  843. });
  844. }
  845. #[test]
  846. fn update_worker_reward_amount_succeeds() {
  847. build_test_externalities().execute_with(|| {
  848. let worker_id = fill_default_worker_position();
  849. let update_worker_amount_fixture =
  850. UpdateWorkerRewardAmountFixture::default_for_worker_id(worker_id);
  851. update_worker_amount_fixture.call_and_assert(Ok(()));
  852. EventFixture::assert_last_crate_event(RawEvent::WorkerRewardAmountUpdated(worker_id));
  853. });
  854. }
  855. #[test]
  856. fn update_worker_reward_amount_succeeds_for_leader() {
  857. build_test_externalities().execute_with(|| {
  858. let worker_id = HireLeadFixture::default()
  859. .with_reward_policy(RewardPolicy {
  860. amount_per_payout: 1000,
  861. next_payment_at_block: 20,
  862. payout_interval: None,
  863. })
  864. .hire_lead();
  865. let update_worker_amount_fixture =
  866. UpdateWorkerRewardAmountFixture::default_for_worker_id(worker_id)
  867. .with_origin(RawOrigin::Root);
  868. update_worker_amount_fixture.call_and_assert(Ok(()));
  869. });
  870. }
  871. #[test]
  872. fn update_worker_reward_amount_fails_with_invalid_origin() {
  873. build_test_externalities().execute_with(|| {
  874. HireLeadFixture::default().hire_lead();
  875. let worker_id = 22; // random worker id
  876. let update_worker_amount_fixture =
  877. UpdateWorkerRewardAmountFixture::default_for_worker_id(worker_id)
  878. .with_origin(RawOrigin::None);
  879. update_worker_amount_fixture.call_and_assert(Err(Error::RequireSignedOrigin));
  880. });
  881. }
  882. #[test]
  883. fn update_worker_reward_amount_fails_with_invalid_origin_for_leader() {
  884. build_test_externalities().execute_with(|| {
  885. let worker_id = HireLeadFixture::default().hire_lead();
  886. let update_worker_amount_fixture =
  887. UpdateWorkerRewardAmountFixture::default_for_worker_id(worker_id)
  888. .with_origin(RawOrigin::None);
  889. update_worker_amount_fixture.call_and_assert(Err(Error::RequireRootOrigin));
  890. });
  891. }
  892. #[test]
  893. fn update_worker_reward_amount_fails_with_invalid_origin_signed_account() {
  894. build_test_externalities().execute_with(|| {
  895. let worker_id = fill_default_worker_position();
  896. let update_worker_amount_fixture =
  897. UpdateWorkerRewardAmountFixture::default_for_worker_id(worker_id)
  898. .with_origin(RawOrigin::Signed(2));
  899. update_worker_amount_fixture.call_and_assert(Err(Error::IsNotLeadAccount));
  900. });
  901. }
  902. #[test]
  903. fn update_worker_reward_amount_fails_with_invalid_worker_id() {
  904. build_test_externalities().execute_with(|| {
  905. let invalid_worker_id = 12;
  906. fill_default_worker_position();
  907. let update_worker_amount_fixture =
  908. UpdateWorkerRewardAmountFixture::default_for_worker_id(invalid_worker_id);
  909. update_worker_amount_fixture.call_and_assert(Err(Error::WorkerDoesNotExist));
  910. });
  911. }
  912. #[test]
  913. fn update_worker_reward_amount_fails_with_no_recurring_reward() {
  914. build_test_externalities().execute_with(|| {
  915. let worker_id = fill_worker_position_with_no_reward();
  916. let update_worker_amount_fixture =
  917. UpdateWorkerRewardAmountFixture::default_for_worker_id(worker_id);
  918. update_worker_amount_fixture.call_and_assert(Err(Error::WorkerHasNoReward));
  919. });
  920. }
  921. fn fill_default_worker_position() -> u64 {
  922. fill_worker_position(
  923. Some(RewardPolicy {
  924. amount_per_payout: 1000,
  925. next_payment_at_block: 20,
  926. payout_interval: None,
  927. }),
  928. None,
  929. true,
  930. OpeningType::Worker,
  931. None,
  932. )
  933. }
  934. fn fill_worker_position_with_no_reward() -> u64 {
  935. fill_worker_position(None, None, true, OpeningType::Worker, None)
  936. }
  937. fn fill_worker_position_with_stake(stake: u64) -> u64 {
  938. fill_worker_position(
  939. Some(RewardPolicy {
  940. amount_per_payout: 1000,
  941. next_payment_at_block: 20,
  942. payout_interval: None,
  943. }),
  944. Some(stake),
  945. true,
  946. OpeningType::Worker,
  947. None,
  948. )
  949. }
  950. fn fill_worker_position(
  951. reward_policy: Option<RewardPolicy<u64, u64>>,
  952. role_stake: Option<u64>,
  953. setup_environment: bool,
  954. opening_type: OpeningType,
  955. worker_handle: Option<Vec<u8>>,
  956. ) -> u64 {
  957. let mut hiring_workflow = HiringWorkflow::default()
  958. .with_role_stake(role_stake)
  959. .with_setup_environment(setup_environment)
  960. .with_opening_type(opening_type)
  961. .with_reward_policy(reward_policy);
  962. hiring_workflow = if let Some(worker_handle) = worker_handle {
  963. hiring_workflow.add_application(worker_handle)
  964. } else {
  965. hiring_workflow.add_default_application()
  966. };
  967. hiring_workflow.execute().unwrap()
  968. }
  969. #[test]
  970. fn leave_worker_role_succeeds() {
  971. build_test_externalities().execute_with(|| {
  972. let worker_id = fill_default_worker_position();
  973. let leave_worker_role_fixture = LeaveWorkerRoleFixture::default_for_worker_id(worker_id);
  974. leave_worker_role_fixture.call_and_assert(Ok(()));
  975. EventFixture::assert_last_crate_event(RawEvent::WorkerExited(
  976. worker_id,
  977. b"rationale_text".to_vec(),
  978. ));
  979. });
  980. }
  981. #[test]
  982. fn leave_worker_role_by_leader_succeeds() {
  983. build_test_externalities().execute_with(|| {
  984. // Ensure that lead is default
  985. assert_eq!(TestWorkingGroup::current_lead(), None);
  986. let worker_id = HireLeadFixture::default().hire_lead();
  987. assert!(TestWorkingGroup::current_lead().is_some());
  988. let leave_worker_role_fixture = LeaveWorkerRoleFixture::default_for_worker_id(worker_id);
  989. leave_worker_role_fixture.call_and_assert(Ok(()));
  990. assert_eq!(TestWorkingGroup::current_lead(), None);
  991. });
  992. }
  993. #[test]
  994. fn leave_worker_role_fails_with_invalid_origin() {
  995. build_test_externalities().execute_with(|| {
  996. let leave_worker_role_fixture =
  997. LeaveWorkerRoleFixture::default_for_worker_id(1).with_origin(RawOrigin::None);
  998. leave_worker_role_fixture.call_and_assert(Err(Error::RequireSignedOrigin));
  999. });
  1000. }
  1001. #[test]
  1002. fn leave_worker_role_fails_with_invalid_origin_signed_account() {
  1003. build_test_externalities().execute_with(|| {
  1004. let worker_id = fill_default_worker_position();
  1005. let leave_worker_role_fixture = LeaveWorkerRoleFixture::default_for_worker_id(worker_id)
  1006. .with_origin(RawOrigin::Signed(2));
  1007. leave_worker_role_fixture.call_and_assert(Err(Error::SignerIsNotWorkerRoleAccount));
  1008. });
  1009. }
  1010. #[test]
  1011. fn leave_worker_role_fails_with_invalid_worker_id() {
  1012. build_test_externalities().execute_with(|| {
  1013. let invalid_worker_id = 1;
  1014. fill_default_worker_position();
  1015. let leave_worker_role_fixture =
  1016. LeaveWorkerRoleFixture::default_for_worker_id(invalid_worker_id);
  1017. leave_worker_role_fixture.call_and_assert(Err(Error::WorkerDoesNotExist));
  1018. });
  1019. }
  1020. #[test]
  1021. fn leave_worker_role_fails_with_invalid_recurring_reward_relationships() {
  1022. build_test_externalities().execute_with(|| {
  1023. let worker_id = fill_default_worker_position();
  1024. let mut worker = TestWorkingGroup::worker_by_id(worker_id);
  1025. worker.reward_relationship = Some(2);
  1026. <crate::WorkerById<Test, TestWorkingGroupInstance>>::insert(worker_id, worker);
  1027. let leave_worker_role_fixture = LeaveWorkerRoleFixture::default_for_worker_id(worker_id);
  1028. leave_worker_role_fixture.call_and_assert(Err(Error::RelationshipMustExist));
  1029. });
  1030. }
  1031. #[test]
  1032. fn leave_worker_role_succeeds_with_stakes() {
  1033. build_test_externalities().execute_with(|| {
  1034. let worker_id = fill_worker_position_with_stake(100);
  1035. let leave_worker_role_fixture = LeaveWorkerRoleFixture::default_for_worker_id(worker_id);
  1036. leave_worker_role_fixture.call_and_assert(Ok(()));
  1037. EventFixture::assert_last_crate_event(RawEvent::WorkerExited(
  1038. worker_id,
  1039. b"rationale_text".to_vec(),
  1040. ));
  1041. });
  1042. }
  1043. #[test]
  1044. fn terminate_worker_role_succeeds_with_stakes() {
  1045. build_test_externalities().execute_with(|| {
  1046. let total_balance = 10000;
  1047. let stake_balance = 100;
  1048. let worker_account_id = 2;
  1049. let worker_member_id = 2;
  1050. increase_total_balance_issuance_using_account_id(worker_account_id, total_balance);
  1051. HireLeadFixture::default().hire_lead();
  1052. let worker_id = HiringWorkflow::default()
  1053. .disable_setup_environment()
  1054. .with_role_stake(Some(stake_balance))
  1055. .add_application_with_origin(
  1056. b"worker_handle".to_vec(),
  1057. RawOrigin::Signed(worker_account_id),
  1058. worker_member_id,
  1059. )
  1060. .execute()
  1061. .unwrap();
  1062. // Balance was staked.
  1063. assert_eq!(
  1064. get_balance(worker_account_id),
  1065. total_balance - stake_balance
  1066. );
  1067. let stake_id = 0;
  1068. let old_stake = <stake::Module<Test>>::stakes(stake_id);
  1069. assert_eq!(get_stake_balance(old_stake), stake_balance);
  1070. let terminate_worker_role_fixture =
  1071. TerminateWorkerRoleFixture::default_for_worker_id(worker_id);
  1072. terminate_worker_role_fixture.call_and_assert(Ok(()));
  1073. EventFixture::assert_last_crate_event(RawEvent::TerminatedWorker(
  1074. worker_id,
  1075. b"rationale_text".to_vec(),
  1076. ));
  1077. // Balance was restored.
  1078. assert_eq!(get_balance(worker_account_id), total_balance);
  1079. let new_stake = <stake::Module<Test>>::stakes(stake_id);
  1080. assert!(matches!(
  1081. new_stake.staking_status,
  1082. stake::StakingStatus::NotStaked
  1083. ));
  1084. });
  1085. }
  1086. #[test]
  1087. fn terminate_worker_role_succeeds_with_slashing() {
  1088. build_test_externalities().execute_with(|| {
  1089. let total_balance = 10000;
  1090. let stake_balance = 100;
  1091. let worker_account_id = 2;
  1092. let worker_member_id = 2;
  1093. increase_total_balance_issuance_using_account_id(worker_account_id, total_balance);
  1094. assert_eq!(get_balance(worker_account_id), total_balance);
  1095. HireLeadFixture::default().hire_lead();
  1096. let worker_id = HiringWorkflow::default()
  1097. .disable_setup_environment()
  1098. .with_role_stake(Some(stake_balance))
  1099. .add_application_with_origin(
  1100. b"worker_handle".to_vec(),
  1101. RawOrigin::Signed(worker_account_id),
  1102. worker_member_id,
  1103. )
  1104. .execute()
  1105. .unwrap();
  1106. // Balance was staked.
  1107. assert_eq!(
  1108. get_balance(worker_account_id),
  1109. total_balance - stake_balance
  1110. );
  1111. let stake_id = 0;
  1112. let old_stake = <stake::Module<Test>>::stakes(stake_id);
  1113. assert_eq!(get_stake_balance(old_stake), stake_balance);
  1114. // Terminate with slashing.
  1115. let terminate_worker_role_fixture =
  1116. TerminateWorkerRoleFixture::default_for_worker_id(worker_id).with_slashing();
  1117. terminate_worker_role_fixture.call_and_assert(Ok(()));
  1118. // Balance was slashed.
  1119. assert_eq!(
  1120. get_balance(worker_account_id),
  1121. total_balance - stake_balance
  1122. );
  1123. let new_stake = <stake::Module<Test>>::stakes(stake_id);
  1124. assert!(matches!(
  1125. new_stake.staking_status,
  1126. stake::StakingStatus::NotStaked
  1127. ));
  1128. });
  1129. }
  1130. #[test]
  1131. fn terminate_worker_role_succeeds() {
  1132. build_test_externalities().execute_with(|| {
  1133. HireLeadFixture::default().hire_lead();
  1134. let worker_id = HiringWorkflow::default()
  1135. .disable_setup_environment()
  1136. .add_application_with_origin(b"worker_handle".to_vec(), RawOrigin::Signed(2), 2)
  1137. .execute()
  1138. .unwrap();
  1139. let terminate_worker_role_fixture =
  1140. TerminateWorkerRoleFixture::default_for_worker_id(worker_id);
  1141. terminate_worker_role_fixture.call_and_assert(Ok(()));
  1142. EventFixture::assert_last_crate_event(RawEvent::TerminatedWorker(
  1143. worker_id,
  1144. b"rationale_text".to_vec(),
  1145. ));
  1146. });
  1147. }
  1148. #[test]
  1149. fn fire_leader_succeeds() {
  1150. build_test_externalities().execute_with(|| {
  1151. let worker_id = HireLeadFixture::default().hire_lead();
  1152. let terminate_worker_role_fixture =
  1153. TerminateWorkerRoleFixture::default_for_worker_id(worker_id)
  1154. .with_origin(RawOrigin::Root);
  1155. terminate_worker_role_fixture.call_and_assert(Ok(()));
  1156. EventFixture::assert_last_crate_event(RawEvent::TerminatedLeader(
  1157. worker_id,
  1158. b"rationale_text".to_vec(),
  1159. ));
  1160. assert_eq!(TestWorkingGroup::current_lead(), None);
  1161. });
  1162. }
  1163. #[test]
  1164. fn terminate_worker_role_fails_with_invalid_text() {
  1165. build_test_externalities().execute_with(|| {
  1166. HireLeadFixture::default().hire_lead();
  1167. let worker_id = HiringWorkflow::default()
  1168. .disable_setup_environment()
  1169. .add_application_with_origin(b"worker_handle".to_vec(), RawOrigin::Signed(2), 2)
  1170. .execute()
  1171. .unwrap();
  1172. let terminate_worker_role_fixture =
  1173. TerminateWorkerRoleFixture::default_for_worker_id(worker_id).with_text(Vec::new());
  1174. terminate_worker_role_fixture
  1175. .call_and_assert(Err(Error::Other("WorkerExitRationaleTextTooShort")));
  1176. let terminate_worker_role_fixture =
  1177. TerminateWorkerRoleFixture::default_for_worker_id(worker_id)
  1178. .with_text(b"MSG_WORKER_EXIT_RATIONALE_TEXT_TOO_LONG".to_vec());
  1179. terminate_worker_role_fixture
  1180. .call_and_assert(Err(Error::Other("WorkerExitRationaleTextTooLong")));
  1181. });
  1182. }
  1183. #[test]
  1184. fn terminate_worker_role_fails_with_unset_lead() {
  1185. build_test_externalities().execute_with(|| {
  1186. let worker_id = fill_default_worker_position();
  1187. SetLeadFixture::unset_lead();
  1188. let terminate_worker_role_fixture =
  1189. TerminateWorkerRoleFixture::default_for_worker_id(worker_id);
  1190. terminate_worker_role_fixture.call_and_assert(Err(Error::CurrentLeadNotSet));
  1191. });
  1192. }
  1193. #[test]
  1194. fn terminate_worker_role_fails_with_invalid_origin() {
  1195. build_test_externalities().execute_with(|| {
  1196. HireLeadFixture::default().hire_lead();
  1197. let worker_id = HiringWorkflow::default()
  1198. .disable_setup_environment()
  1199. .add_application_with_origin(b"worker_handle".to_vec(), RawOrigin::Signed(2), 2)
  1200. .execute()
  1201. .unwrap();
  1202. let terminate_worker_role_fixture =
  1203. TerminateWorkerRoleFixture::default_for_worker_id(worker_id)
  1204. .with_origin(RawOrigin::None);
  1205. terminate_worker_role_fixture.call_and_assert(Err(Error::RequireSignedOrigin));
  1206. });
  1207. }
  1208. #[test]
  1209. fn fire_leader_fails_with_invalid_origin() {
  1210. build_test_externalities().execute_with(|| {
  1211. let worker_id = HireLeadFixture::default().hire_lead();
  1212. let terminate_worker_role_fixture =
  1213. TerminateWorkerRoleFixture::default_for_worker_id(worker_id)
  1214. .with_origin(RawOrigin::None);
  1215. terminate_worker_role_fixture.call_and_assert(Err(Error::RequireRootOrigin));
  1216. });
  1217. }
  1218. #[test]
  1219. fn increase_worker_stake_succeeds() {
  1220. build_test_externalities().execute_with(|| {
  1221. let worker_id = fill_worker_position_with_stake(100);
  1222. let increase_stake_fixture = IncreaseWorkerStakeFixture::default_for_worker_id(worker_id);
  1223. increase_stake_fixture.call_and_assert(Ok(()));
  1224. EventFixture::assert_last_crate_event(RawEvent::StakeIncreased(worker_id));
  1225. });
  1226. }
  1227. #[test]
  1228. fn increase_worker_stake_succeeds_for_leader() {
  1229. build_test_externalities().execute_with(|| {
  1230. let worker_id = HireLeadFixture::default().with_stake(100).hire_lead();
  1231. let increase_stake_fixture = IncreaseWorkerStakeFixture::default_for_worker_id(worker_id);
  1232. increase_stake_fixture.call_and_assert(Ok(()));
  1233. });
  1234. }
  1235. #[test]
  1236. fn increase_worker_stake_fails_with_invalid_origin() {
  1237. build_test_externalities().execute_with(|| {
  1238. let worker_id = 0;
  1239. let increase_stake_fixture = IncreaseWorkerStakeFixture::default_for_worker_id(worker_id)
  1240. .with_origin(RawOrigin::None);
  1241. increase_stake_fixture.call_and_assert(Err(Error::RequireSignedOrigin));
  1242. });
  1243. }
  1244. #[test]
  1245. fn increase_worker_stake_fails_with_zero_balance() {
  1246. build_test_externalities().execute_with(|| {
  1247. let worker_id = fill_worker_position_with_stake(100);
  1248. let increase_stake_fixture =
  1249. IncreaseWorkerStakeFixture::default_for_worker_id(worker_id).with_balance(0);
  1250. increase_stake_fixture.call_and_assert(Err(Error::StakeBalanceCannotBeZero));
  1251. });
  1252. }
  1253. #[test]
  1254. fn increase_worker_stake_fails_with_invalid_worker_id() {
  1255. build_test_externalities().execute_with(|| {
  1256. let invalid_worker_id = 11;
  1257. let increase_stake_fixture =
  1258. IncreaseWorkerStakeFixture::default_for_worker_id(invalid_worker_id);
  1259. increase_stake_fixture.call_and_assert(Err(Error::WorkerDoesNotExist));
  1260. });
  1261. }
  1262. #[test]
  1263. fn increase_worker_stake_fails_with_invalid_balance() {
  1264. build_test_externalities().execute_with(|| {
  1265. let worker_id = fill_worker_position_with_stake(100);
  1266. let invalid_balance = 100000000;
  1267. let increase_stake_fixture = IncreaseWorkerStakeFixture::default_for_worker_id(worker_id)
  1268. .with_balance(invalid_balance);
  1269. increase_stake_fixture
  1270. .call_and_assert(Err(Error::StakingErrorInsufficientBalanceInSourceAccount));
  1271. });
  1272. }
  1273. #[test]
  1274. fn increase_worker_stake_fails_with_no_stake_profile() {
  1275. build_test_externalities().execute_with(|| {
  1276. let worker_id = fill_default_worker_position();
  1277. let increase_stake_fixture = IncreaseWorkerStakeFixture::default_for_worker_id(worker_id);
  1278. increase_stake_fixture.call_and_assert(Err(Error::NoWorkerStakeProfile));
  1279. });
  1280. }
  1281. #[test]
  1282. fn decrease_worker_stake_succeeds() {
  1283. build_test_externalities().execute_with(|| {
  1284. let worker_id = fill_worker_position_with_stake(100);
  1285. let decrease_stake_fixture = DecreaseWorkerStakeFixture::default_for_worker_id(worker_id);
  1286. decrease_stake_fixture.call_and_assert(Ok(()));
  1287. EventFixture::assert_last_crate_event(RawEvent::StakeDecreased(worker_id));
  1288. });
  1289. }
  1290. #[test]
  1291. fn decrease_worker_stake_succeeds_for_leader() {
  1292. build_test_externalities().execute_with(|| {
  1293. let worker_id = HireLeadFixture::default().with_stake(100).hire_lead();
  1294. let decrease_stake_fixture = DecreaseWorkerStakeFixture::default_for_worker_id(worker_id)
  1295. .with_origin(RawOrigin::Root);
  1296. decrease_stake_fixture.call_and_assert(Ok(()));
  1297. });
  1298. }
  1299. #[test]
  1300. fn decrease_worker_stake_fails_with_invalid_origin() {
  1301. build_test_externalities().execute_with(|| {
  1302. HireLeadFixture::default().hire_lead();
  1303. let worker_id = 22; // random worker id
  1304. let decrease_stake_fixture = DecreaseWorkerStakeFixture::default_for_worker_id(worker_id)
  1305. .with_origin(RawOrigin::None);
  1306. decrease_stake_fixture.call_and_assert(Err(Error::RequireSignedOrigin));
  1307. });
  1308. }
  1309. #[test]
  1310. fn decrease_worker_stake_fails_with_invalid_origin_for_leader() {
  1311. build_test_externalities().execute_with(|| {
  1312. let worker_id = HireLeadFixture::default().hire_lead();
  1313. let decrease_stake_fixture = DecreaseWorkerStakeFixture::default_for_worker_id(worker_id)
  1314. .with_origin(RawOrigin::None);
  1315. decrease_stake_fixture.call_and_assert(Err(Error::RequireRootOrigin));
  1316. });
  1317. }
  1318. #[test]
  1319. fn decrease_worker_stake_fails_with_zero_balance() {
  1320. build_test_externalities().execute_with(|| {
  1321. let worker_id = fill_worker_position_with_stake(100);
  1322. let decrease_stake_fixture =
  1323. DecreaseWorkerStakeFixture::default_for_worker_id(worker_id).with_balance(0);
  1324. decrease_stake_fixture.call_and_assert(Err(Error::StakeBalanceCannotBeZero));
  1325. });
  1326. }
  1327. #[test]
  1328. fn decrease_worker_stake_fails_with_invalid_worker_id() {
  1329. build_test_externalities().execute_with(|| {
  1330. HireLeadFixture::default().hire_lead();
  1331. let invalid_worker_id = 11;
  1332. let decrease_stake_fixture =
  1333. DecreaseWorkerStakeFixture::default_for_worker_id(invalid_worker_id);
  1334. decrease_stake_fixture.call_and_assert(Err(Error::WorkerDoesNotExist));
  1335. });
  1336. }
  1337. #[test]
  1338. fn decrease_worker_stake_fails_with_invalid_balance() {
  1339. build_test_externalities().execute_with(|| {
  1340. let worker_id = fill_worker_position_with_stake(100);
  1341. let invalid_balance = 100000000;
  1342. let decrease_stake_fixture = DecreaseWorkerStakeFixture::default_for_worker_id(worker_id)
  1343. .with_balance(invalid_balance);
  1344. decrease_stake_fixture.call_and_assert(Err(Error::StakingErrorInsufficientStake));
  1345. });
  1346. }
  1347. #[test]
  1348. fn decrease_worker_stake_fails_with_no_stake_profile() {
  1349. build_test_externalities().execute_with(|| {
  1350. let worker_id = fill_default_worker_position();
  1351. let decrease_stake_fixture = DecreaseWorkerStakeFixture::default_for_worker_id(worker_id);
  1352. decrease_stake_fixture.call_and_assert(Err(Error::NoWorkerStakeProfile));
  1353. });
  1354. }
  1355. #[test]
  1356. fn decrease_worker_stake_fails_with_not_set_lead() {
  1357. build_test_externalities().execute_with(|| {
  1358. let invalid_worker_id = 11;
  1359. let decrease_stake_fixture =
  1360. DecreaseWorkerStakeFixture::default_for_worker_id(invalid_worker_id);
  1361. decrease_stake_fixture.call_and_assert(Err(Error::CurrentLeadNotSet));
  1362. });
  1363. }
  1364. #[test]
  1365. fn slash_worker_stake_succeeds() {
  1366. build_test_externalities().execute_with(|| {
  1367. let worker_id = fill_worker_position_with_stake(100);
  1368. let slash_stake_fixture = SlashWorkerStakeFixture::default_for_worker_id(worker_id);
  1369. slash_stake_fixture.call_and_assert(Ok(()));
  1370. EventFixture::assert_last_crate_event(RawEvent::StakeSlashed(worker_id));
  1371. });
  1372. }
  1373. #[test]
  1374. fn slash_leader_stake_succeeds() {
  1375. build_test_externalities().execute_with(|| {
  1376. let leader_worker_id = HiringWorkflow::default()
  1377. .with_role_stake(Some(100))
  1378. .with_opening_type(OpeningType::Leader)
  1379. .add_default_application()
  1380. .execute()
  1381. .unwrap();
  1382. let slash_stake_fixture = SlashWorkerStakeFixture::default_for_worker_id(leader_worker_id)
  1383. .with_origin(RawOrigin::Root);
  1384. slash_stake_fixture.call_and_assert(Ok(()));
  1385. EventFixture::assert_last_crate_event(RawEvent::StakeSlashed(leader_worker_id));
  1386. });
  1387. }
  1388. #[test]
  1389. fn slash_worker_stake_fails_with_invalid_origin() {
  1390. build_test_externalities().execute_with(|| {
  1391. HireLeadFixture::default().hire_lead();
  1392. let invalid_worker_id = 22;
  1393. let slash_stake_fixture = SlashWorkerStakeFixture::default_for_worker_id(invalid_worker_id)
  1394. .with_origin(RawOrigin::None);
  1395. slash_stake_fixture.call_and_assert(Err(Error::RequireSignedOrigin));
  1396. });
  1397. }
  1398. #[test]
  1399. fn slash_leader_stake_fails_with_invalid_origin() {
  1400. build_test_externalities().execute_with(|| {
  1401. let worker_id = HireLeadFixture::default().hire_lead();
  1402. let slash_stake_fixture =
  1403. SlashWorkerStakeFixture::default_for_worker_id(worker_id).with_origin(RawOrigin::None);
  1404. slash_stake_fixture.call_and_assert(Err(Error::RequireRootOrigin));
  1405. });
  1406. }
  1407. #[test]
  1408. fn slash_worker_stake_fails_with_zero_balance() {
  1409. build_test_externalities().execute_with(|| {
  1410. let worker_id = fill_worker_position_with_stake(100);
  1411. let slash_stake_fixture =
  1412. SlashWorkerStakeFixture::default_for_worker_id(worker_id).with_balance(0);
  1413. slash_stake_fixture.call_and_assert(Err(Error::StakeBalanceCannotBeZero));
  1414. });
  1415. }
  1416. #[test]
  1417. fn slash_worker_stake_fails_with_invalid_worker_id() {
  1418. build_test_externalities().execute_with(|| {
  1419. HireLeadFixture::default().hire_lead();
  1420. let invalid_worker_id = 11;
  1421. let slash_stake_fixture = SlashWorkerStakeFixture::default_for_worker_id(invalid_worker_id);
  1422. slash_stake_fixture.call_and_assert(Err(Error::WorkerDoesNotExist));
  1423. });
  1424. }
  1425. #[test]
  1426. fn slash_worker_stake_fails_with_no_stake_profile() {
  1427. build_test_externalities().execute_with(|| {
  1428. let worker_id = fill_default_worker_position();
  1429. let slash_stake_fixture = SlashWorkerStakeFixture::default_for_worker_id(worker_id);
  1430. slash_stake_fixture.call_and_assert(Err(Error::NoWorkerStakeProfile));
  1431. });
  1432. }
  1433. #[test]
  1434. fn slash_worker_stake_fails_with_not_set_lead() {
  1435. build_test_externalities().execute_with(|| {
  1436. let invalid_worker_id = 11;
  1437. let slash_stake_fixture = SlashWorkerStakeFixture::default_for_worker_id(invalid_worker_id);
  1438. slash_stake_fixture.call_and_assert(Err(Error::CurrentLeadNotSet));
  1439. });
  1440. }
  1441. #[test]
  1442. fn get_all_worker_ids_succeeds() {
  1443. build_test_externalities().execute_with(|| {
  1444. let worker_ids = TestWorkingGroup::get_regular_worker_ids();
  1445. assert_eq!(worker_ids, Vec::new());
  1446. let leader_worker_id = HireLeadFixture::default().hire_lead();
  1447. let worker_id1 = fill_worker_position(None, None, false, OpeningType::Worker, None);
  1448. let worker_id2 = fill_worker_position(None, None, false, OpeningType::Worker, None);
  1449. let mut expected_ids = vec![worker_id1, worker_id2];
  1450. expected_ids.sort();
  1451. let mut worker_ids = TestWorkingGroup::get_regular_worker_ids();
  1452. worker_ids.sort();
  1453. assert_eq!(worker_ids, expected_ids);
  1454. assert!(!expected_ids.contains(&leader_worker_id));
  1455. <crate::WorkerById<Test, TestWorkingGroupInstance>>::remove(worker_id1);
  1456. let worker_ids = TestWorkingGroup::get_regular_worker_ids();
  1457. assert_eq!(worker_ids, vec![worker_id2]);
  1458. });
  1459. }
  1460. #[test]
  1461. fn set_working_group_mint_capacity_succeeds() {
  1462. build_test_externalities().execute_with(|| {
  1463. let mint_id = <minting::Module<Test>>::add_mint(0, None).unwrap();
  1464. <crate::Mint<Test, TestWorkingGroupInstance>>::put(mint_id);
  1465. let capacity = 15000;
  1466. let result = TestWorkingGroup::set_mint_capacity(RawOrigin::Root.into(), capacity);
  1467. assert_eq!(result, Ok(()));
  1468. let mint = <minting::Module<Test>>::mints(mint_id);
  1469. assert_eq!(mint.capacity(), capacity);
  1470. });
  1471. }
  1472. #[test]
  1473. fn set_working_group_mint_capacity_fails_with_mint_not_found() {
  1474. build_test_externalities().execute_with(|| {
  1475. let capacity = 15000;
  1476. <crate::Mint<Test, TestWorkingGroupInstance>>::put(5); // random mint id
  1477. let result = TestWorkingGroup::set_mint_capacity(RawOrigin::Root.into(), capacity);
  1478. assert_eq!(result, Err(Error::CannotFindMint));
  1479. });
  1480. }
  1481. #[test]
  1482. fn set_working_group_mint_capacity_fails_with_invalid_origin() {
  1483. build_test_externalities().execute_with(|| {
  1484. let capacity = 15000;
  1485. let result = TestWorkingGroup::set_mint_capacity(RawOrigin::None.into(), capacity);
  1486. assert_eq!(result, Err(Error::RequireRootOrigin));
  1487. });
  1488. }
  1489. #[test]
  1490. fn ensure_setting_genesis_working_group_mint_succeeds() {
  1491. build_test_externalities().execute_with(|| {
  1492. let mint_id = TestWorkingGroup::mint();
  1493. assert!(minting::Mints::<Test>::exists(mint_id));
  1494. let mint = <minting::Module<Test>>::mints(mint_id);
  1495. assert_eq!(mint.capacity(), WORKING_GROUP_MINT_CAPACITY);
  1496. });
  1497. }
  1498. #[test]
  1499. fn ensure_setting_genesis_constraints_succeeds() {
  1500. build_test_externalities().execute_with(|| {
  1501. let default_constraint = common::constraints::InputValidationLengthConstraint::new(
  1502. WORKING_GROUP_CONSTRAINT_MIN,
  1503. WORKING_GROUP_CONSTRAINT_DIFF,
  1504. );
  1505. let opening_text_constraint = TestWorkingGroup::opening_human_readable_text();
  1506. let worker_text_constraint = TestWorkingGroup::application_human_readable_text();
  1507. let worker_exit_text_constraint = TestWorkingGroup::worker_exit_rationale_text();
  1508. assert_eq!(opening_text_constraint, default_constraint);
  1509. assert_eq!(worker_text_constraint, default_constraint);
  1510. assert_eq!(worker_exit_text_constraint, default_constraint);
  1511. });
  1512. }
  1513. #[test]
  1514. fn active_worker_counter_works_successfully() {
  1515. build_test_externalities().execute_with(|| {
  1516. assert_eq!(TestWorkingGroup::active_worker_count(), 0);
  1517. let leader_id = HireLeadFixture::default().hire_lead();
  1518. assert_eq!(TestWorkingGroup::active_worker_count(), 1);
  1519. let worker_id1 = fill_worker_position(
  1520. None,
  1521. None,
  1522. false,
  1523. OpeningType::Worker,
  1524. Some(b"worker1".to_vec()),
  1525. );
  1526. assert_eq!(TestWorkingGroup::active_worker_count(), 2);
  1527. let worker_id2 = fill_worker_position(
  1528. None,
  1529. None,
  1530. false,
  1531. OpeningType::Worker,
  1532. Some(b"worker1".to_vec()),
  1533. );
  1534. assert_eq!(TestWorkingGroup::active_worker_count(), 3);
  1535. TerminateWorkerRoleFixture::default_for_worker_id(worker_id1).call_and_assert(Ok(()));
  1536. assert_eq!(TestWorkingGroup::active_worker_count(), 2);
  1537. TerminateWorkerRoleFixture::default_for_worker_id(worker_id2).call_and_assert(Ok(()));
  1538. assert_eq!(TestWorkingGroup::active_worker_count(), 1);
  1539. TerminateWorkerRoleFixture::default_for_worker_id(leader_id)
  1540. .with_origin(RawOrigin::Root)
  1541. .call_and_assert(Ok(()));
  1542. assert_eq!(TestWorkingGroup::active_worker_count(), 0);
  1543. });
  1544. }
  1545. #[test]
  1546. fn adding_too_much_workers_fails_with_single_application_out_of_limit() {
  1547. build_test_externalities().execute_with(|| {
  1548. HireLeadFixture::default().hire_lead();
  1549. fill_worker_position(None, None, false, OpeningType::Worker, None);
  1550. fill_worker_position(None, None, false, OpeningType::Worker, None);
  1551. let hiring_workflow = HiringWorkflow::default()
  1552. .disable_setup_environment()
  1553. .add_default_application()
  1554. .expect(Err(Error::MaxActiveWorkerNumberExceeded));
  1555. hiring_workflow.execute()
  1556. });
  1557. }
  1558. #[test]
  1559. fn fill_opening_cannot_hire_more_workers_using_several_applicationst_han_allows_worker_limit() {
  1560. build_test_externalities().execute_with(|| {
  1561. HireLeadFixture::default().hire_lead();
  1562. fill_worker_position(None, None, false, OpeningType::Worker, None);
  1563. let hiring_workflow = HiringWorkflow::default()
  1564. .disable_setup_environment()
  1565. .add_application_with_origin(b"Some1".to_vec(), RawOrigin::Signed(2), 2)
  1566. .add_application_with_origin(b"Some2".to_vec(), RawOrigin::Signed(3), 3)
  1567. .expect(Err(Error::MaxActiveWorkerNumberExceeded));
  1568. hiring_workflow.execute()
  1569. });
  1570. }