Browse Source

content-directory: more tests for channels

Mokhtar Naamani 4 years ago
parent
commit
012ba6666f
2 changed files with 208 additions and 52 deletions
  1. 33 52
      runtime-modules/content/src/lib.rs
  2. 175 0
      runtime-modules/content/src/tests/channels.rs

+ 33 - 52
runtime-modules/content/src/lib.rs

@@ -796,7 +796,6 @@ decl_module! {
         #[weight = 10_000_000] // TODO: adjust weight
         pub fn delete_channel(
             origin,
-            owner: ChannelOwner<T::MemberId, T::CuratorGroupId, T::DAOId>,
             actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
             channel_id: T::ChannelId,
         ) -> DispatchResult {
@@ -835,6 +834,11 @@ decl_module! {
                 Self::deposit_event(RawEvent::SeriesDeleted(*id));
             });
 
+            // If the channel was owned by a curator group, decrement counter
+            if let ChannelOwner::CuratorGroup(curator_group_id) = channel.owner {
+                Self::decrement_number_of_channels_owned_by_curator_group(curator_group_id);
+            }
+
             // TODO: Remove any channel transfer requests and refund requester
             // Self::terminate_channel_transfer_requests(channel_id)
             // Self::deposit_event(RawEvent::ChannelOwnershipTransferRequestCancelled());
@@ -846,9 +850,8 @@ decl_module! {
         #[weight = 10_000_000] // TODO: adjust weight
         pub fn request_channel_transfer(
             origin,
-            new_owner: ChannelOwner<T::MemberId, T::CuratorGroupId, T::DAOId>,
-            channel_id: T::ChannelId,
-            payment: BalanceOf<T>,
+            actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
+            request: ChannelOwnershipTransferRequest<T>,
         ) -> DispatchResult {
             // requester must be new_owner
             Ok(())
@@ -866,7 +869,7 @@ decl_module! {
         #[weight = 10_000_000] // TODO: adjust weight
         pub fn accept_channel_transfer(
             origin,
-            owner: ChannelOwner<T::MemberId, T::CuratorGroupId, T::DAOId>,
+            actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
             request_id: T::ChannelOwnershipTransferRequestId,
         ) -> DispatchResult {
             // only current owner of channel can approve
@@ -876,7 +879,7 @@ decl_module! {
         #[weight = 10_000_000] // TODO: adjust weight
         pub fn create_video(
             origin,
-            owner: ChannelOwner<T::MemberId, T::CuratorGroupId, T::DAOId>,
+            actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
             channel_id: T::ChannelId,
             params: VideoCreationParameters<ContentParameters<T>>,
         ) -> DispatchResult {
@@ -886,7 +889,7 @@ decl_module! {
         #[weight = 10_000_000] // TODO: adjust weight
         pub fn update_video(
             origin,
-            owner: ChannelOwner<T::MemberId, T::CuratorGroupId, T::DAOId>,
+            actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
             video: T::VideoId,
             params: VideoUpdateParameters<ContentParameters<T>>,
         ) -> DispatchResult {
@@ -896,7 +899,7 @@ decl_module! {
         #[weight = 10_000_000] // TODO: adjust weight
         pub fn delete_video(
             origin,
-            owner: ChannelOwner<T::MemberId, T::CuratorGroupId, T::DAOId>,
+            actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
             video: T::VideoId,
         ) -> DispatchResult {
             Ok(())
@@ -905,7 +908,7 @@ decl_module! {
         #[weight = 10_000_000] // TODO: adjust weight
         pub fn create_playlist(
             origin,
-            owner: ChannelOwner<T::MemberId, T::CuratorGroupId, T::DAOId>,
+            actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
             channel_id: T::ChannelId,
             params: PlaylistCreationParameters,
         ) -> DispatchResult {
@@ -915,7 +918,7 @@ decl_module! {
         #[weight = 10_000_000] // TODO: adjust weight
         pub fn update_playlist(
             origin,
-            owner: ChannelOwner<T::MemberId, T::CuratorGroupId, T::DAOId>,
+            actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
             playlist: T::PlaylistId,
             params: PlaylistUpdateParameters,
         ) -> DispatchResult {
@@ -925,7 +928,7 @@ decl_module! {
         #[weight = 10_000_000] // TODO: adjust weight
         pub fn delete_playlist(
             origin,
-            owner: ChannelOwner<T::MemberId, T::CuratorGroupId, T::DAOId>,
+            actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
             channel_id: T::ChannelId,
             playlist: T::PlaylistId,
         ) -> DispatchResult {
@@ -935,6 +938,7 @@ decl_module! {
         #[weight = 10_000_000] // TODO: adjust weight
         pub fn set_featured_videos(
             origin,
+            actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
             list: Vec<T::VideoId>
         ) -> DispatchResult {
             // can only be set by lead
@@ -944,7 +948,7 @@ decl_module! {
         #[weight = 10_000_000] // TODO: adjust weight
         pub fn create_video_category(
             origin,
-            curator: T::CuratorId,
+            actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
             params: VideoCategoryCreationParameters,
         ) -> DispatchResult {
             Ok(())
@@ -953,7 +957,7 @@ decl_module! {
         #[weight = 10_000_000] // TODO: adjust weight
         pub fn update_video_category(
             origin,
-            curator: T::CuratorId,
+            actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
             category: T::VideoCategoryId,
             params: VideoCategoryUpdateParameters,
         ) -> DispatchResult {
@@ -963,7 +967,7 @@ decl_module! {
         #[weight = 10_000_000] // TODO: adjust weight
         pub fn delete_video_category(
             origin,
-            curator: T::CuratorId,
+            actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
             category: T::VideoCategoryId,
         ) -> DispatchResult {
             Ok(())
@@ -972,7 +976,7 @@ decl_module! {
         #[weight = 10_000_000] // TODO: adjust weight
         pub fn create_channel_category(
             origin,
-            curator: T::CuratorId,
+            actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
             params: ChannelCategoryCreationParameters,
         ) -> DispatchResult {
             // check origin is curator in active curator group
@@ -984,7 +988,7 @@ decl_module! {
         #[weight = 10_000_000] // TODO: adjust weight
         pub fn update_channel_category(
             origin,
-            curator: T::CuratorId,
+            actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
             category: T::ChannelCategoryId,
             params: ChannelCategoryUpdateParameters,
         ) -> DispatchResult {
@@ -994,7 +998,7 @@ decl_module! {
         #[weight = 10_000_000] // TODO: adjust weight
         pub fn delete_channel_category(
             origin,
-            curator: T::CuratorId,
+            actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
             category: T::ChannelCategoryId,
         ) -> DispatchResult {
             Ok(())
@@ -1031,7 +1035,7 @@ decl_module! {
         #[weight = 10_000_000] // TODO: adjust weight
         pub fn add_person_to_video(
             origin,
-            owner: ChannelOwner<T::MemberId, T::CuratorGroupId, T::DAOId>,
+            actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
             video_id: T::VideoId,
             person: T::PersonId
         ) -> DispatchResult {
@@ -1041,7 +1045,7 @@ decl_module! {
         #[weight = 10_000_000] // TODO: adjust weight
         pub fn remove_person_from_video(
             origin,
-            owner: ChannelOwner<T::MemberId, T::CuratorGroupId, T::DAOId>,
+            actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
             video_id: T::VideoId
         ) -> DispatchResult {
             Ok(())
@@ -1050,7 +1054,7 @@ decl_module! {
         #[weight = 10_000_000] // TODO: adjust weight
         pub fn censor_video(
             origin,
-            curator_id: T::CuratorId,
+            actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
             video_id: T::VideoId,
             rationale: Vec<u8>,
         ) -> DispatchResult {
@@ -1060,7 +1064,7 @@ decl_module! {
         #[weight = 10_000_000] // TODO: adjust weight
         pub fn censor_channel(
             origin,
-            curator_id: T::CuratorId,
+            actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
             channel_id: T::ChannelId,
             rationale: Vec<u8>,
         ) -> DispatchResult {
@@ -1070,7 +1074,7 @@ decl_module! {
         #[weight = 10_000_000] // TODO: adjust weight
         pub fn uncensor_video(
             origin,
-            curator_id: T::CuratorId,
+            actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
             video_id: T::VideoId,
             rationale: Vec<u8>
         ) -> DispatchResult {
@@ -1080,7 +1084,7 @@ decl_module! {
         #[weight = 10_000_000] // TODO: adjust weight
         pub fn uncensor_channel(
             origin,
-            curator_id: T::CuratorId,
+            actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
             channel_id: T::ChannelId,
             rationale: Vec<u8>,
         ) -> DispatchResult {
@@ -1090,7 +1094,7 @@ decl_module! {
         #[weight = 10_000_000] // TODO: adjust weight
         pub fn create_series(
             origin,
-            owner: ChannelOwner<T::MemberId, T::CuratorGroupId, T::DAOId>,
+            actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
             channel_id: T::ChannelId,
             params: SeriesParameters<T::VideoId, ContentParameters<T>>,
         ) -> DispatchResult {
@@ -1100,7 +1104,7 @@ decl_module! {
         #[weight = 10_000_000] // TODO: adjust weight
         pub fn update_series(
             origin,
-            owner: ChannelOwner<T::MemberId, T::CuratorGroupId, T::DAOId>,
+            actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
             channel_id: T::ChannelId,
             params: SeriesParameters<T::VideoId, ContentParameters<T>>,
         ) -> DispatchResult {
@@ -1110,7 +1114,7 @@ decl_module! {
         #[weight = 10_000_000] // TODO: adjust weight
         pub fn delete_series(
             origin,
-            owner: ChannelOwner<T::MemberId, T::CuratorGroupId, T::DAOId>,
+            actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
             series: T::SeriesId,
         ) -> DispatchResult {
             Ok(())
@@ -1119,38 +1123,15 @@ decl_module! {
 }
 
 impl<T: Trait> Module<T> {
-    // TODO: make this private again after used in module
-    /// Increment number of channels, maintained by each curator group
-    pub fn increment_number_of_channels_owned_by_curator_groups(
-        curator_group_ids: BTreeSet<T::CuratorGroupId>,
-    ) {
-        curator_group_ids.into_iter().for_each(|curator_group_id| {
-            Self::increment_number_of_channels_owned_by_curator_group(curator_group_id);
-        });
-    }
-
-    // TODO: make this private again after used in module
-    /// Decrement number of channels, maintained by each curator group
-    pub fn decrement_number_of_channels_owned_by_curator_groups(
-        curator_group_ids: BTreeSet<T::CuratorGroupId>,
-    ) {
-        curator_group_ids.into_iter().for_each(|curator_group_id| {
-            Self::decrement_number_of_channels_owned_by_curator_group(curator_group_id);
-        });
-    }
-
-    /// Increment number of channels, maintained by curator group
+    /// Increment number of channels, owned by curator group
     fn increment_number_of_channels_owned_by_curator_group(curator_group_id: T::CuratorGroupId) {
         <CuratorGroupById<T>>::mutate(curator_group_id, |curator_group| {
             curator_group.increment_number_of_channels_owned_count();
         });
     }
 
-    // TODO: make this private again after used in module
-    /// Decrement number of channels, maintained by curator group
-    pub fn decrement_number_of_channels_owned_by_curator_group(
-        curator_group_id: T::CuratorGroupId,
-    ) {
+    /// Decrement number of channels, owned by curator group
+    fn decrement_number_of_channels_owned_by_curator_group(curator_group_id: T::CuratorGroupId) {
         <CuratorGroupById<T>>::mutate(curator_group_id, |curator_group| {
             curator_group.decrement_number_of_channels_owned_count();
         });

+ 175 - 0
runtime-modules/content/src/tests/channels.rs

@@ -4,6 +4,24 @@ use super::mock::*;
 use crate::*;
 use frame_support::{assert_err, assert_ok};
 
+fn add_curator_to_new_group(curator_id: CuratorId) -> CuratorGroupId {
+    let curator_group_id = Content::next_curator_group_id();
+    // create new group and add curator id to it
+    assert_ok!(Content::create_curator_group(Origin::signed(LEAD_ORIGIN)));
+    assert_ok!(Content::add_curator_to_group(
+        Origin::signed(LEAD_ORIGIN),
+        curator_group_id,
+        curator_id
+    ));
+    // make group active
+    assert_ok!(Content::set_curator_group_status(
+        Origin::signed(LEAD_ORIGIN),
+        curator_group_id,
+        true
+    ));
+    curator_group_id
+}
+
 #[test]
 fn lead_cannot_create_channel() {
     with_default_mock_builder(|| {
@@ -21,3 +39,160 @@ fn lead_cannot_create_channel() {
         );
     })
 }
+
+#[test]
+fn curators_can_create_channel() {
+    with_default_mock_builder(|| {
+        // Curator group doesn't exist yet
+        assert_err!(
+            Content::create_channel(
+                Origin::signed(FIRST_CURATOR_ORIGIN),
+                ContentActor::Curator(FIRST_CURATOR_GROUP_ID, FIRST_CURATOR_ID),
+                ChannelCreationParameters {
+                    assets: vec![],
+                    meta: vec![],
+                    reward_account: None,
+                }
+            ),
+            Error::<Test>::CuratorGroupIsNotActive
+        );
+
+        let group_id = add_curator_to_new_group(FIRST_CURATOR_ID);
+        assert_eq!(FIRST_CURATOR_GROUP_ID, group_id);
+
+        // Curator from wrong group
+        assert_err!(
+            Content::create_channel(
+                Origin::signed(SECOND_CURATOR_ORIGIN),
+                ContentActor::Curator(FIRST_CURATOR_GROUP_ID, SECOND_CURATOR_ID),
+                ChannelCreationParameters {
+                    assets: vec![],
+                    meta: vec![],
+                    reward_account: None,
+                }
+            ),
+            Error::<Test>::CuratorIsNotAMemberOfGivenCuratorGroup
+        );
+
+        // Curator in correct active group, but wrong origin
+        assert_err!(
+            Content::create_channel(
+                Origin::signed(SECOND_CURATOR_ORIGIN),
+                ContentActor::Curator(FIRST_CURATOR_GROUP_ID, FIRST_CURATOR_ID),
+                ChannelCreationParameters {
+                    assets: vec![],
+                    meta: vec![],
+                    reward_account: None,
+                }
+            ),
+            Error::<Test>::CuratorAuthFailed
+        );
+
+        // Curator in correct active group, with correct origin
+        assert_ok!(Content::create_channel(
+            Origin::signed(FIRST_CURATOR_ORIGIN),
+            ContentActor::Curator(FIRST_CURATOR_GROUP_ID, FIRST_CURATOR_ID),
+            ChannelCreationParameters {
+                assets: vec![],
+                meta: vec![],
+                reward_account: None,
+            }
+        ));
+    })
+}
+#[test]
+fn members_can_manage_channels() {
+    with_default_mock_builder(|| {
+        // Not a member
+        assert_err!(
+            Content::create_channel(
+                Origin::signed(UNKNOWN_ORIGIN),
+                ContentActor::Member(MEMBERS_COUNT + 1),
+                ChannelCreationParameters {
+                    assets: vec![],
+                    meta: vec![],
+                    reward_account: None,
+                }
+            ),
+            Error::<Test>::MemberAuthFailed
+        );
+
+        let channel_id_1 = Content::next_channel_id();
+
+        // Member can create the channel
+        assert_ok!(Content::create_channel(
+            Origin::signed(FIRST_MEMBER_ORIGIN),
+            ContentActor::Member(FIRST_MEMBER_ID),
+            ChannelCreationParameters {
+                assets: vec![],
+                meta: vec![],
+                reward_account: None,
+            }
+        ));
+
+        // TODO: assert emitted events...
+
+        let channel_id_2 = Content::next_channel_id();
+
+        // Member can create the channel
+        assert_ok!(Content::create_channel(
+            Origin::signed(SECOND_MEMBER_ORIGIN),
+            ContentActor::Member(SECOND_MEMBER_ID),
+            ChannelCreationParameters {
+                assets: vec![],
+                meta: vec![],
+                reward_account: None,
+            }
+        ));
+
+        // TODO: assert emitted events...
+
+        // Update channel
+        assert_ok!(Content::update_channel(
+            Origin::signed(FIRST_MEMBER_ORIGIN),
+            ContentActor::Member(FIRST_MEMBER_ID),
+            channel_id_1,
+            ChannelUpdateParameters {
+                assets: None,
+                new_meta: None,
+                reward_account: None,
+            }
+        ));
+
+        // TODO: assert emitted events...
+
+        // Member cannot update a channel they do not own
+        assert_err!(
+            Content::update_channel(
+                Origin::signed(FIRST_MEMBER_ORIGIN),
+                ContentActor::Member(FIRST_MEMBER_ID),
+                channel_id_2,
+                ChannelUpdateParameters {
+                    assets: None,
+                    new_meta: None,
+                    reward_account: None,
+                }
+            ),
+            Error::<Test>::ActorNotAuthorized
+        );
+
+        // Member cannot delete a channel they do not own
+        assert_err!(
+            Content::delete_channel(
+                Origin::signed(FIRST_MEMBER_ORIGIN),
+                ContentActor::Member(FIRST_MEMBER_ID),
+                channel_id_2
+            ),
+            Error::<Test>::ActorNotAuthorized
+        );
+
+        // Delete channel
+        assert_ok!(Content::delete_channel(
+            Origin::signed(FIRST_MEMBER_ORIGIN),
+            ContentActor::Member(FIRST_MEMBER_ID),
+            channel_id_1
+        ));
+
+        // TODO: assert emitted events...
+    })
+}