mod.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. mod mock;
  2. use crate::constraints::InputValidationLengthConstraint;
  3. use crate::types::{Lead, OpeningPolicyCommitment};
  4. use crate::{Instance1, RawEvent};
  5. use mock::{build_test_externalities, Bureaucracy1, Membership, System, TestEvent};
  6. use srml_support::StorageValue;
  7. use system::{EventRecord, Phase, RawOrigin};
  8. fn setup_members(count: u8) {
  9. let authority_account_id = 1;
  10. Membership::set_screening_authority(RawOrigin::Root.into(), authority_account_id).unwrap();
  11. for i in 0..count {
  12. let account_id: u64 = i as u64;
  13. let handle: [u8; 20] = [i; 20];
  14. Membership::add_screened_member(
  15. RawOrigin::Signed(authority_account_id).into(),
  16. account_id,
  17. membership::members::UserInfo {
  18. handle: Some(handle.to_vec()),
  19. avatar_uri: None,
  20. about: None,
  21. },
  22. )
  23. .unwrap();
  24. }
  25. }
  26. struct ApplyOnCuratorOpeningFixture {
  27. origin: RawOrigin<u64>,
  28. member_id: u64,
  29. curator_opening_id: u64,
  30. role_account: u64,
  31. opt_role_stake_balance: Option<u64>,
  32. opt_application_stake_balance: Option<u64>,
  33. human_readable_text: Vec<u8>,
  34. }
  35. impl ApplyOnCuratorOpeningFixture {
  36. pub fn default_for_opening_id(opening_id: u64) -> Self {
  37. ApplyOnCuratorOpeningFixture {
  38. origin: RawOrigin::Signed(1),
  39. member_id: 1,
  40. curator_opening_id: opening_id,
  41. role_account: 1,
  42. opt_role_stake_balance: None,
  43. opt_application_stake_balance: None,
  44. human_readable_text: Vec::new(),
  45. }
  46. }
  47. pub fn call_and_assert(&self, expected_result: Result<(), &str>) {
  48. let actual_result = Bureaucracy1::apply_on_curator_opening(
  49. self.origin.clone().into(),
  50. self.member_id,
  51. self.curator_opening_id,
  52. self.role_account,
  53. self.opt_role_stake_balance,
  54. self.opt_application_stake_balance,
  55. self.human_readable_text.clone(),
  56. );
  57. assert_eq!(actual_result, expected_result);
  58. }
  59. }
  60. struct AcceptCuratorApplicationsFixture {
  61. origin: RawOrigin<u64>,
  62. opening_id: u64,
  63. }
  64. impl AcceptCuratorApplicationsFixture {
  65. pub fn default_for_opening_id(opening_id: u64) -> Self {
  66. AcceptCuratorApplicationsFixture {
  67. origin: RawOrigin::Signed(1),
  68. opening_id,
  69. }
  70. }
  71. pub fn call_and_assert(&self, expected_result: Result<(), &str>) {
  72. let actual_result =
  73. Bureaucracy1::accept_curator_applications(self.origin.clone().into(), self.opening_id);
  74. assert_eq!(actual_result, expected_result);
  75. }
  76. }
  77. struct SetLeadFixture;
  78. impl SetLeadFixture {
  79. fn set_lead(lead_account_id: u64) {
  80. assert_eq!(
  81. Bureaucracy1::set_lead(RawOrigin::Root.into(), 1, lead_account_id),
  82. Ok(())
  83. );
  84. }
  85. }
  86. struct AddCuratorOpeningFixture {
  87. origin: RawOrigin<u64>,
  88. activate_at: hiring::ActivateOpeningAt<u64>,
  89. commitment: OpeningPolicyCommitment<u64, u64>,
  90. human_readable_text: Vec<u8>,
  91. }
  92. impl Default for AddCuratorOpeningFixture {
  93. fn default() -> Self {
  94. AddCuratorOpeningFixture {
  95. origin: RawOrigin::Signed(1),
  96. activate_at: hiring::ActivateOpeningAt::CurrentBlock,
  97. commitment: <OpeningPolicyCommitment<u64, u64>>::default(),
  98. human_readable_text: Vec::new(),
  99. }
  100. }
  101. }
  102. impl AddCuratorOpeningFixture {
  103. pub fn call_and_assert(&self, expected_result: Result<(), &str>) {
  104. let actual_result = Bureaucracy1::add_curator_opening(
  105. self.origin.clone().into(),
  106. self.activate_at.clone(),
  107. self.commitment.clone(),
  108. self.human_readable_text.clone(),
  109. );
  110. assert_eq!(actual_result, expected_result);
  111. }
  112. fn with_text(self, text: Vec<u8>) -> Self {
  113. AddCuratorOpeningFixture {
  114. human_readable_text: text,
  115. ..self
  116. }
  117. }
  118. fn with_activate_at(self, activate_at: hiring::ActivateOpeningAt<u64>) -> Self {
  119. AddCuratorOpeningFixture {
  120. activate_at,
  121. ..self
  122. }
  123. }
  124. }
  125. struct EventFixture;
  126. impl EventFixture {
  127. fn assert_crate_events(
  128. expected_raw_events: Vec<RawEvent<u64, u64, u64, u64, crate::Instance1>>,
  129. ) {
  130. let converted_events = expected_raw_events
  131. .iter()
  132. .map(|ev| TestEvent::bureaucracy_Instance1(ev.clone()))
  133. .collect::<Vec<TestEvent>>();
  134. Self::assert_global_events(converted_events)
  135. }
  136. fn assert_global_events(expected_raw_events: Vec<TestEvent>) {
  137. let expected_events = expected_raw_events
  138. .iter()
  139. .map(|ev| EventRecord {
  140. phase: Phase::ApplyExtrinsic(0),
  141. event: ev.clone(),
  142. topics: vec![],
  143. })
  144. .collect::<Vec<EventRecord<_, _>>>();
  145. assert_eq!(System::events(), expected_events);
  146. }
  147. }
  148. #[test]
  149. fn set_forum_sudo_set() {
  150. build_test_externalities().execute_with(|| {
  151. // Ensure that lead is default
  152. assert_eq!(Bureaucracy1::current_lead(), None);
  153. let lead_account_id = 1;
  154. let lead_member_id = 1;
  155. // Set lead
  156. assert_eq!(
  157. Bureaucracy1::set_lead(RawOrigin::Root.into(), lead_member_id, lead_account_id),
  158. Ok(())
  159. );
  160. let lead = Lead {
  161. member_id: lead_member_id,
  162. role_account_id: lead_account_id,
  163. };
  164. assert_eq!(Bureaucracy1::current_lead(), Some(lead));
  165. EventFixture::assert_crate_events(vec![RawEvent::LeaderSet(
  166. lead_member_id,
  167. lead_account_id,
  168. )]);
  169. });
  170. }
  171. #[test]
  172. fn add_curator_opening_succeeds() {
  173. build_test_externalities().execute_with(|| {
  174. let lead_account_id = 1;
  175. SetLeadFixture::set_lead(lead_account_id);
  176. let add_curator_opening_fixture = AddCuratorOpeningFixture::default();
  177. add_curator_opening_fixture.call_and_assert(Ok(()));
  178. EventFixture::assert_crate_events(vec![
  179. RawEvent::LeaderSet(1, lead_account_id),
  180. RawEvent::CuratorOpeningAdded(0),
  181. ]);
  182. });
  183. }
  184. #[test]
  185. fn add_curator_opening_fails_with_lead_is_not_set() {
  186. build_test_externalities().execute_with(|| {
  187. let add_curator_opening_fixture = AddCuratorOpeningFixture::default();
  188. add_curator_opening_fixture.call_and_assert(Err(crate::MSG_CURRENT_LEAD_NOT_SET));
  189. });
  190. }
  191. #[test]
  192. fn add_curator_opening_fails_with_invalid_human_readable_text() {
  193. build_test_externalities().execute_with(|| {
  194. SetLeadFixture::set_lead(1);
  195. <crate::OpeningHumanReadableText<Instance1>>::put(InputValidationLengthConstraint {
  196. min: 1,
  197. max_min_diff: 5,
  198. });
  199. let add_curator_opening_fixture = AddCuratorOpeningFixture::default().with_text(Vec::new());
  200. add_curator_opening_fixture.call_and_assert(Err(crate::MSG_OPENING_TEXT_TOO_SHORT));
  201. let add_curator_opening_fixture =
  202. AddCuratorOpeningFixture::default().with_text(b"Long text".to_vec());
  203. add_curator_opening_fixture.call_and_assert(Err(crate::MSG_OPENING_TEXT_TOO_LONG));
  204. });
  205. }
  206. #[test]
  207. fn add_curator_opening_fails_with_hiring_error() {
  208. build_test_externalities().execute_with(|| {
  209. SetLeadFixture::set_lead(1);
  210. let add_curator_opening_fixture = AddCuratorOpeningFixture::default()
  211. .with_activate_at(hiring::ActivateOpeningAt::ExactBlock(0));
  212. add_curator_opening_fixture.call_and_assert(Err("Opening does not activate in the future"));
  213. });
  214. }
  215. #[test]
  216. fn accept_curator_applications_succeeds() {
  217. build_test_externalities().execute_with(|| {
  218. let lead_account_id = 1;
  219. SetLeadFixture::set_lead(lead_account_id);
  220. let add_curator_opening_fixture = AddCuratorOpeningFixture::default()
  221. .with_activate_at(hiring::ActivateOpeningAt::ExactBlock(5));
  222. add_curator_opening_fixture.call_and_assert(Ok(()));
  223. let opening_id = 0; // newly created opening
  224. let accept_curator_applications_fixture =
  225. AcceptCuratorApplicationsFixture::default_for_opening_id(opening_id);
  226. accept_curator_applications_fixture.call_and_assert(Ok(()));
  227. EventFixture::assert_crate_events(vec![
  228. RawEvent::LeaderSet(1, lead_account_id),
  229. RawEvent::CuratorOpeningAdded(opening_id),
  230. RawEvent::AcceptedCuratorApplications(opening_id),
  231. ]);
  232. });
  233. }
  234. #[test]
  235. fn accept_curator_applications_fails_with_hiring_error() {
  236. build_test_externalities().execute_with(|| {
  237. SetLeadFixture::set_lead(1);
  238. let add_curator_opening_fixture = AddCuratorOpeningFixture::default();
  239. add_curator_opening_fixture.call_and_assert(Ok(()));
  240. let opening_id = 0; // newly created opening
  241. let accept_curator_applications_fixture =
  242. AcceptCuratorApplicationsFixture::default_for_opening_id(opening_id);
  243. accept_curator_applications_fixture
  244. .call_and_assert(Err("Opening Is Not in Waiting to begin"));
  245. });
  246. }
  247. #[test]
  248. fn accept_curator_applications_fails_with_not_lead() {
  249. build_test_externalities().execute_with(|| {
  250. SetLeadFixture::set_lead(1);
  251. let add_curator_opening_fixture = AddCuratorOpeningFixture::default();
  252. add_curator_opening_fixture.call_and_assert(Ok(()));
  253. SetLeadFixture::set_lead(2);
  254. let opening_id = 0; // newly created opening
  255. let accept_curator_applications_fixture =
  256. AcceptCuratorApplicationsFixture::default_for_opening_id(opening_id);
  257. accept_curator_applications_fixture.call_and_assert(Err(crate::MSG_IS_NOT_LEAD_ACCOUNT));
  258. });
  259. }
  260. #[test]
  261. fn appy_on_curator_opening_succeeds() {
  262. build_test_externalities().execute_with(|| {
  263. let lead_account_id = 1;
  264. SetLeadFixture::set_lead(lead_account_id);
  265. setup_members(2);
  266. let add_curator_opening_fixture = AddCuratorOpeningFixture::default();
  267. add_curator_opening_fixture.call_and_assert(Ok(()));
  268. let opening_id = 0; // newly created opening
  269. let appy_on_curator_opening_fixture =
  270. ApplyOnCuratorOpeningFixture::default_for_opening_id(opening_id);
  271. appy_on_curator_opening_fixture.call_and_assert(Ok(()));
  272. EventFixture::assert_global_events(vec![
  273. TestEvent::bureaucracy_Instance1(RawEvent::LeaderSet(1, lead_account_id)),
  274. TestEvent::membership_mod(membership::members::RawEvent::MemberRegistered(0, 0)),
  275. TestEvent::membership_mod(membership::members::RawEvent::MemberRegistered(1, 1)),
  276. TestEvent::bureaucracy_Instance1(RawEvent::CuratorOpeningAdded(opening_id)),
  277. TestEvent::bureaucracy_Instance1(RawEvent::AppliedOnCuratorOpening(opening_id, 0)),
  278. ]);
  279. });
  280. }