|
@@ -85,6 +85,10 @@ impl DiscussionFixture {
|
|
|
DiscussionFixture { title, ..self }
|
|
|
}
|
|
|
|
|
|
+ fn with_mode(self, mode: ThreadMode<u64>) -> Self {
|
|
|
+ Self { mode, ..self }
|
|
|
+ }
|
|
|
+
|
|
|
fn create_discussion_and_assert(&self, result: Result<u64, DispatchError>) -> Option<u64> {
|
|
|
let create_discussion_result =
|
|
|
Discussions::create_thread(self.author_id, self.title.clone(), self.mode.clone());
|
|
@@ -195,6 +199,47 @@ fn create_post_call_succeeds() {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+struct ChangeThreadModeFixture {
|
|
|
+ pub origin: RawOrigin<u64>,
|
|
|
+ pub thread_id: u64,
|
|
|
+ pub member_id: u64,
|
|
|
+ pub mode: ThreadMode<u64>,
|
|
|
+}
|
|
|
+
|
|
|
+impl ChangeThreadModeFixture {
|
|
|
+ fn default_for_thread_id(thread_id: u64) -> Self {
|
|
|
+ Self {
|
|
|
+ origin: RawOrigin::Signed(1),
|
|
|
+ thread_id,
|
|
|
+ member_id: 1,
|
|
|
+ mode: ThreadMode::Open,
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ fn with_mode(self, mode: ThreadMode<u64>) -> Self {
|
|
|
+ Self { mode, ..self }
|
|
|
+ }
|
|
|
+
|
|
|
+ fn with_member_id(self, member_id: u64) -> Self {
|
|
|
+ Self { member_id, ..self }
|
|
|
+ }
|
|
|
+
|
|
|
+ fn with_origin(self, origin: RawOrigin<u64>) -> Self {
|
|
|
+ Self { origin, ..self }
|
|
|
+ }
|
|
|
+
|
|
|
+ fn call_and_assert(&self, expected_result: DispatchResult) {
|
|
|
+ let actual_result = Discussions::change_thread_mode(
|
|
|
+ self.origin.clone().into(),
|
|
|
+ self.member_id,
|
|
|
+ self.thread_id,
|
|
|
+ self.mode.clone(),
|
|
|
+ );
|
|
|
+
|
|
|
+ assert_eq!(actual_result, expected_result);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
#[test]
|
|
|
fn update_post_call_succeeds() {
|
|
|
initial_test_ext().execute_with(|| {
|
|
@@ -258,7 +303,7 @@ fn update_post_call_fails_because_of_the_wrong_author() {
|
|
|
|
|
|
post_fixture.add_post_and_assert(Ok(()));
|
|
|
|
|
|
- post_fixture = post_fixture.with_author(2);
|
|
|
+ post_fixture = post_fixture.with_author(5);
|
|
|
|
|
|
post_fixture.update_post_and_assert(Err(DispatchError::Other("Invalid author")));
|
|
|
|
|
@@ -428,3 +473,162 @@ fn discussion_thread_and_post_counters_are_valid() {
|
|
|
assert_eq!(Discussions::post_count(), 1);
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
+#[test]
|
|
|
+fn change_thread_mode_succeeds() {
|
|
|
+ initial_test_ext().execute_with(|| {
|
|
|
+ /*
|
|
|
+ Events are not emitted on block 0.
|
|
|
+ So any dispatchable calls made during genesis block formation will have no events emitted.
|
|
|
+ https://substrate.dev/recipes/2-appetizers/4-events.html
|
|
|
+ */
|
|
|
+ run_to_block(1);
|
|
|
+
|
|
|
+ let discussion_fixture = DiscussionFixture::default();
|
|
|
+
|
|
|
+ let thread_id = discussion_fixture
|
|
|
+ .create_discussion_and_assert(Ok(1))
|
|
|
+ .unwrap();
|
|
|
+
|
|
|
+ let thread_mode = ThreadMode::Closed(vec![2, 3]);
|
|
|
+ let change_thread_mode_fixture = ChangeThreadModeFixture::default_for_thread_id(thread_id)
|
|
|
+ .with_mode(thread_mode.clone());
|
|
|
+ change_thread_mode_fixture.call_and_assert(Ok(()));
|
|
|
+
|
|
|
+ EventFixture::assert_events(vec![
|
|
|
+ RawEvent::ThreadCreated(1, 1),
|
|
|
+ RawEvent::ThreadModeChanged(1, thread_mode),
|
|
|
+ ]);
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+#[test]
|
|
|
+fn change_mode_failed_with_invalid_origin() {
|
|
|
+ initial_test_ext().execute_with(|| {
|
|
|
+ let discussion_fixture = DiscussionFixture::default();
|
|
|
+
|
|
|
+ let thread_id = discussion_fixture
|
|
|
+ .create_discussion_and_assert(Ok(1))
|
|
|
+ .unwrap();
|
|
|
+
|
|
|
+ let change_thread_mode_fixture = ChangeThreadModeFixture::default_for_thread_id(thread_id)
|
|
|
+ .with_origin(RawOrigin::Root)
|
|
|
+ .with_member_id(12);
|
|
|
+ change_thread_mode_fixture.call_and_assert(Err(DispatchError::Other("Invalid author")));
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+#[test]
|
|
|
+fn change_mode_failed_with_invalid_thread_id() {
|
|
|
+ initial_test_ext().execute_with(|| {
|
|
|
+ let invalid_thread_id = 12;
|
|
|
+
|
|
|
+ let change_thread_mode_fixture =
|
|
|
+ ChangeThreadModeFixture::default_for_thread_id(invalid_thread_id);
|
|
|
+ change_thread_mode_fixture.call_and_assert(Err(Error::<Test>::ThreadDoesntExist.into()));
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+#[test]
|
|
|
+fn change_mode_failed_with_not_thread_author_or_councilor() {
|
|
|
+ initial_test_ext().execute_with(|| {
|
|
|
+ let discussion_fixture = DiscussionFixture::default();
|
|
|
+
|
|
|
+ let thread_id = discussion_fixture
|
|
|
+ .create_discussion_and_assert(Ok(1))
|
|
|
+ .unwrap();
|
|
|
+
|
|
|
+ let change_thread_mode_fixture = ChangeThreadModeFixture::default_for_thread_id(thread_id)
|
|
|
+ .with_origin(RawOrigin::Signed(12))
|
|
|
+ .with_member_id(12);
|
|
|
+ change_thread_mode_fixture.call_and_assert(Err(Error::<Test>::NotAuthorOrCouncilor.into()));
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+#[test]
|
|
|
+fn change_thread_mode_succeeds_with_councilor() {
|
|
|
+ initial_test_ext().execute_with(|| {
|
|
|
+ let discussion_fixture = DiscussionFixture::default();
|
|
|
+
|
|
|
+ let thread_id = discussion_fixture
|
|
|
+ .create_discussion_and_assert(Ok(1))
|
|
|
+ .unwrap();
|
|
|
+
|
|
|
+ let change_thread_mode_fixture = ChangeThreadModeFixture::default_for_thread_id(thread_id)
|
|
|
+ .with_member_id(2)
|
|
|
+ .with_origin(RawOrigin::Signed(2));
|
|
|
+ change_thread_mode_fixture.call_and_assert(Ok(()));
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+#[test]
|
|
|
+fn create_post_call_succeeds_with_closed_mode_by_author() {
|
|
|
+ initial_test_ext().execute_with(|| {
|
|
|
+ let mode = ThreadMode::Closed(vec![2, 11]);
|
|
|
+ let discussion_fixture = DiscussionFixture::default().with_mode(mode);
|
|
|
+
|
|
|
+ let thread_id = discussion_fixture
|
|
|
+ .create_discussion_and_assert(Ok(1))
|
|
|
+ .unwrap();
|
|
|
+
|
|
|
+ let mut post_fixture = PostFixture::default_for_thread(thread_id)
|
|
|
+ .with_origin(RawOrigin::Signed(1))
|
|
|
+ .with_author(1);
|
|
|
+
|
|
|
+ post_fixture.add_post_and_assert(Ok(()));
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+#[test]
|
|
|
+fn create_post_call_succeeds_with_closed_mode_by_councilor() {
|
|
|
+ initial_test_ext().execute_with(|| {
|
|
|
+ let mode = ThreadMode::Closed(vec![2, 11]);
|
|
|
+ let discussion_fixture = DiscussionFixture::default().with_mode(mode);
|
|
|
+
|
|
|
+ let thread_id = discussion_fixture
|
|
|
+ .create_discussion_and_assert(Ok(1))
|
|
|
+ .unwrap();
|
|
|
+
|
|
|
+ let mut post_fixture = PostFixture::default_for_thread(thread_id)
|
|
|
+ .with_origin(RawOrigin::Signed(2))
|
|
|
+ .with_author(2);
|
|
|
+
|
|
|
+ post_fixture.add_post_and_assert(Ok(()));
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+#[test]
|
|
|
+fn create_post_call_succeeds_with_closed_mode_by_white_listed_member() {
|
|
|
+ initial_test_ext().execute_with(|| {
|
|
|
+ let mode = ThreadMode::Closed(vec![2, 11]);
|
|
|
+ let discussion_fixture = DiscussionFixture::default().with_mode(mode);
|
|
|
+
|
|
|
+ let thread_id = discussion_fixture
|
|
|
+ .create_discussion_and_assert(Ok(1))
|
|
|
+ .unwrap();
|
|
|
+
|
|
|
+ let mut post_fixture = PostFixture::default_for_thread(thread_id)
|
|
|
+ .with_origin(RawOrigin::Signed(11))
|
|
|
+ .with_author(11);
|
|
|
+
|
|
|
+ post_fixture.add_post_and_assert(Ok(()));
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+#[test]
|
|
|
+fn create_post_call_fails_with_closed_mode_by_not_allowed_member() {
|
|
|
+ initial_test_ext().execute_with(|| {
|
|
|
+ let mode = ThreadMode::Closed(vec![2, 10]);
|
|
|
+ let discussion_fixture = DiscussionFixture::default().with_mode(mode);
|
|
|
+
|
|
|
+ let thread_id = discussion_fixture
|
|
|
+ .create_discussion_and_assert(Ok(1))
|
|
|
+ .unwrap();
|
|
|
+
|
|
|
+ let mut post_fixture = PostFixture::default_for_thread(thread_id)
|
|
|
+ .with_origin(RawOrigin::Signed(11))
|
|
|
+ .with_author(11);
|
|
|
+
|
|
|
+ post_fixture.add_post_and_assert(Err(Error::<Test>::CannotPostOnClosedThread.into()));
|
|
|
+ });
|
|
|
+}
|