Bläddra i källkod

content: additional tests and fix authorization for creating videos

Mokhtar Naamani 4 år sedan
förälder
incheckning
2df61df6dd

+ 10 - 7
runtime-modules/content/src/lib.rs

@@ -638,7 +638,7 @@ decl_module! {
             actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
             params: ChannelCreationParameters<ContentParameters<T>, T::AccountId>,
         ) {
-            ensure_actor_authorized_to_create_channels_and_videos_assets::<T>(
+            ensure_actor_authorized_to_create_channel::<T>(
                 origin,
                 &actor,
             )?;
@@ -691,7 +691,7 @@ decl_module! {
             // check that channel exists
             let channel = Self::ensure_channel_exists(&channel_id)?;
 
-            ensure_actor_authorized_update_channel_and_videos::<T>(
+            ensure_actor_authorized_to_update_channel::<T>(
                 origin,
                 &actor,
                 &channel.owner,
@@ -752,7 +752,7 @@ decl_module! {
             // check that channel exists
             let channel = Self::ensure_channel_exists(&channel_id)?;
 
-            ensure_actor_authorized_update_channel_and_videos::<T>(
+            ensure_actor_authorized_to_update_channel::<T>(
                 origin,
                 &actor,
                 &channel.owner,
@@ -926,9 +926,13 @@ decl_module! {
             channel_id: T::ChannelId,
             params: VideoCreationParameters<ContentParameters<T>>,
         ) {
-            ensure_actor_authorized_to_create_channels_and_videos_assets::<T>(
+            // check that channel exists
+            let channel = Self::ensure_channel_exists(&channel_id)?;
+
+            ensure_actor_authorized_to_update_channel::<T>(
                 origin,
                 &actor,
+                &channel.owner,
             )?;
 
             // Pick out the assets to be uploaded to storage system
@@ -982,10 +986,9 @@ decl_module! {
             // check that video exists, retrieve corresponding channel id.
             let channel_id = Self::ensure_video_exists(&video_id)?.in_channel;
 
-            ensure_actor_authorized_update_channel_and_videos::<T>(
+            ensure_actor_authorized_to_update_channel::<T>(
                 origin,
                 &actor,
-                // The channel owner will be..
                 &Self::channel_by_id(channel_id).owner,
             )?;
 
@@ -1035,7 +1038,7 @@ decl_module! {
 
             let channel_id = video.in_channel;
 
-            ensure_actor_authorized_update_channel_and_videos::<T>(
+            ensure_actor_authorized_to_update_channel::<T>(
                 origin,
                 &actor,
                 // The channel owner will be..

+ 17 - 17
runtime-modules/content/src/permissions/mod.rs

@@ -12,7 +12,7 @@ use frame_support::{ensure, Parameter};
 pub use serde::{Deserialize, Serialize};
 use sp_arithmetic::traits::BaseArithmetic;
 use sp_runtime::traits::{MaybeSerializeDeserialize, Member};
-use system::ensure_root;
+// use system::ensure_root;
 
 /// Model of authentication manager.
 pub trait ContentActorAuthenticator: system::Trait + MembershipTypes {
@@ -99,7 +99,7 @@ pub fn ensure_is_lead<T: Trait>(origin: T::Origin) -> DispatchResult {
     ensure_lead_auth_success::<T>(&account_id)
 }
 
-pub fn ensure_actor_authorized_to_create_channels_and_videos_assets<T: Trait>(
+pub fn ensure_actor_authorized_to_create_channel<T: Trait>(
     origin: T::Origin,
     actor: &ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
 ) -> DispatchResult {
@@ -128,8 +128,8 @@ pub fn ensure_actor_authorized_to_create_channels_and_videos_assets<T: Trait>(
     }
 }
 
-// Enure actor can update or delete channels and videos
-pub fn ensure_actor_authorized_update_channel_and_videos<T: Trait>(
+// Enure actor can update channels and videos in the channel
+pub fn ensure_actor_authorized_to_update_channel<T: Trait>(
     origin: T::Origin,
     actor: &ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
     owner: &ChannelOwner<T::MemberId, T::CuratorGroupId, T::DAOId>,
@@ -263,19 +263,19 @@ pub fn ensure_actor_authorized_to_manage_categories<T: Trait>(
     }
 }
 
-pub fn ensure_actor_authorized_to_delete_stale_assets<T: Trait>(
-    origin: T::Origin,
-    actor: &ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
-) -> DispatchResult {
-    // Only Lead and (sudo) can delete assets no longer associated with a channel or person.
-    if let ContentActor::Lead = actor {
-        let sender = ensure_signed(origin)?;
-        ensure_lead_auth_success::<T>(&sender)
-    } else {
-        ensure_root(origin)?;
-        Ok(())
-    }
-}
+// pub fn ensure_actor_authorized_to_delete_stale_assets<T: Trait>(
+//     origin: T::Origin,
+//     actor: &ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
+// ) -> DispatchResult {
+//     // Only Lead and (sudo) can delete assets no longer associated with a channel or person.
+//     if let ContentActor::Lead = actor {
+//         let sender = ensure_signed(origin)?;
+//         ensure_lead_auth_success::<T>(&sender)
+//     } else {
+//         ensure_root(origin)?;
+//         Ok(())
+//     }
+// }
 
 /// Enum, representing all possible `Actor`s
 #[cfg_attr(feature = "std", derive(Serialize, Deserialize))]

+ 1 - 1
runtime-modules/content/src/tests/mock.rs

@@ -19,7 +19,7 @@ use common::storage::StorageSystem;
 pub type CuratorId = <Test as ContentActorAuthenticator>::CuratorId;
 pub type CuratorGroupId = <Test as ContentActorAuthenticator>::CuratorGroupId;
 pub type MemberId = <Test as MembershipTypes>::MemberId;
-// pub type ChannelId = <Test as StorageOwnership>::ChannelId;
+pub type ChannelId = <Test as StorageOwnership>::ChannelId;
 // pub type DAOId = <Test as StorageOwnership>::DAOId;
 
 /// Origins

+ 1 - 0
runtime-modules/content/src/tests/mod.rs

@@ -3,3 +3,4 @@
 mod channels;
 mod curators;
 mod mock;
+mod videos;

+ 127 - 0
runtime-modules/content/src/tests/videos.rs

@@ -0,0 +1,127 @@
+#![cfg(test)]
+
+use super::curators;
+use super::mock::*;
+use crate::*;
+use frame_support::{assert_err, assert_ok};
+
+fn create_channel() -> ChannelId {
+    let channel_id = 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,
+        }
+    ));
+
+    channel_id
+}
+
+#[test]
+fn member_can_create_videos() {
+    with_default_mock_builder(|| {
+        // Run to block one to see emitted events
+        run_to_block(1);
+        let channel_id = create_channel();
+
+        let video_id = Content::next_video_id();
+        assert_ok!(Content::create_video(
+            Origin::signed(FIRST_MEMBER_ORIGIN),
+            ContentActor::Member(FIRST_MEMBER_ID),
+            channel_id,
+            VideoCreationParameters {
+                assets: vec![],
+                meta: vec![],
+            }
+        ));
+
+        assert_eq!(
+            System::events().last().unwrap().event,
+            MetaEvent::content(RawEvent::VideoCreated(
+                ContentActor::Member(FIRST_MEMBER_ID),
+                channel_id,
+                video_id,
+                VideoCreationParameters {
+                    assets: vec![],
+                    meta: vec![],
+                }
+            ))
+        );
+
+        let video = Content::video_by_id(video_id);
+        assert_eq!(channel_id, video.in_channel);
+
+        assert_ok!(Content::update_video(
+            Origin::signed(FIRST_MEMBER_ORIGIN),
+            ContentActor::Member(FIRST_MEMBER_ID),
+            video_id,
+            VideoUpdateParameters {
+                assets: None,
+                new_meta: None,
+            }
+        ));
+
+        assert_eq!(
+            System::events().last().unwrap().event,
+            MetaEvent::content(RawEvent::VideoUpdated(
+                ContentActor::Member(FIRST_MEMBER_ID),
+                video_id,
+                VideoUpdateParameters {
+                    assets: None,
+                    new_meta: None,
+                }
+            ))
+        );
+
+        assert_ok!(Content::delete_video(
+            Origin::signed(FIRST_MEMBER_ORIGIN),
+            ContentActor::Member(FIRST_MEMBER_ID),
+            video_id
+        ));
+
+        assert_eq!(
+            System::events().last().unwrap().event,
+            MetaEvent::content(RawEvent::VideoDeleted(
+                ContentActor::Member(FIRST_MEMBER_ID),
+                video_id
+            ))
+        );
+
+        // Member cannot create video in a channel they do not own
+        assert!(Content::create_video(
+            Origin::signed(SECOND_MEMBER_ORIGIN),
+            ContentActor::Member(SECOND_MEMBER_ID),
+            channel_id,
+            VideoCreationParameters {
+                assets: vec![],
+                meta: vec![],
+            }
+        )
+        .is_err());
+
+        // Member cannot update video in a channel they do not own
+        assert!(Content::update_video(
+            Origin::signed(SECOND_MEMBER_ORIGIN),
+            ContentActor::Member(SECOND_MEMBER_ID),
+            video_id,
+            VideoUpdateParameters {
+                assets: None,
+                new_meta: None,
+            }
+        )
+        .is_err());
+
+        // Member cannot delete video in a channel they do not own
+        assert!(Content::delete_video(
+            Origin::signed(SECOND_MEMBER_ORIGIN),
+            ContentActor::Member(SECOND_MEMBER_ID),
+            video_id
+        )
+        .is_err());
+    })
+}