Browse Source

Content directory test & some changes from review

Jens Finkhaeuser 6 years ago
parent
commit
40be6a99f7
2 changed files with 79 additions and 13 deletions
  1. 50 12
      src/storage/content_directory.rs
  2. 29 1
      src/storage/mock.rs

+ 50 - 12
src/storage/content_directory.rs

@@ -41,7 +41,7 @@ pub trait Trait: timestamp::Trait + system::Trait + DOTRTrait + MaybeDebug {
 }
 
 static MSG_CREATOR_MUST_BE_MEMBER: &str = "Only active members may create content!";
-static MSG_INVALID_SCHEMA_ID: &str = "The metadata schema is not known or invalid!";
+// TODO for future: static MSG_INVALID_SCHEMA_ID: &str = "The metadata schema is not known or invalid!";
 static MSG_METADATA_NOT_FOUND: &str = "Metadata with the given ID cannot be found!";
 static MSG_ONLY_OWNER_MAY_PUBLISH: &str = "Only metadata owners may publish their metadata!";
 
@@ -101,10 +101,8 @@ decl_module! {
         pub fn add_metadata(origin, schema: T::SchemaId, metadata: Vec<u8>)
         {
             // Origin has to be a member
-            let who = ensure_signed(origin).clone().unwrap();
-            if !T::Members::is_active_member(&who) {
-                return Err(MSG_CREATOR_MUST_BE_MEMBER);
-            }
+            let who = ensure_signed(origin)?;
+            ensure!(T::Members::is_active_member(&who), MSG_CREATOR_MUST_BE_MEMBER);
 
             // TODO in future, we want the schema IDs to correspond to a schema
             // registry, and validate the data. For now, we just allow the
@@ -136,17 +134,13 @@ decl_module! {
         pub fn publish_metadata(origin, id: T::MetadataId)
         {
             // Ensure signed account
-            let who = ensure_signed(origin).clone().unwrap();
+            let who = ensure_signed(origin)?;
 
             // Try t find metadata
-            let found = Self::metadata(id);
-            ensure!(!found.is_some(), MSG_METADATA_NOT_FOUND);
+            let mut data = Self::metadata(id).ok_or(MSG_METADATA_NOT_FOUND)?;
 
             // Ensure it's the metadata creator who publishes
-            let mut data = found.unwrap();
-            if data.origin != who {
-                return Err(MSG_ONLY_OWNER_MAY_PUBLISH);
-            }
+            ensure!(data.origin == who, MSG_ONLY_OWNER_MAY_PUBLISH);
 
             // Modify
             data.state = MetadataState::Published;
@@ -159,3 +153,47 @@ decl_module! {
         }
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use crate::storage::mock::*;
+
+    #[test]
+    fn add_metadata() {
+        with_default_mock_builder(|| {
+            let res =
+                TestContentDirectory::add_metadata(Origin::signed(1), 1, "foo".as_bytes().to_vec());
+            assert!(res.is_ok());
+        });
+    }
+
+    #[test]
+    fn publish_metadata() {
+        with_default_mock_builder(|| {
+            let res =
+                TestContentDirectory::add_metadata(Origin::signed(1), 1, "foo".as_bytes().to_vec());
+            assert!(res.is_ok());
+
+            // Grab ID from event
+            let metadata_id = match System::events().last().unwrap().event {
+                MetaEvent::content_directory(
+                    content_directory::RawEvent::MetadataDraftCreated(metadata_id),
+                ) => metadata_id,
+                _ => 0xdeadbeefu64, // invalid value, unlikely to match
+            };
+            assert_ne!(metadata_id, 0xdeadbeefu64);
+
+            // Publishing a bad ID should fail
+            let res = TestContentDirectory::publish_metadata(Origin::signed(1), metadata_id + 1);
+            assert!(res.is_err());
+
+            // Publishing should not work for non-owners
+            let res = TestContentDirectory::publish_metadata(Origin::signed(2), metadata_id);
+            assert!(res.is_err());
+
+            // For the owner, it should work however
+            let res = TestContentDirectory::publish_metadata(Origin::signed(1), metadata_id);
+            assert!(res.is_ok());
+        });
+    }
+}

+ 29 - 1
src/storage/mock.rs

@@ -1,6 +1,8 @@
 #![cfg(test)]
 
-pub use super::{data_directory, data_object_storage_registry, data_object_type_registry};
+pub use super::{
+    content_directory, data_directory, data_object_storage_registry, data_object_type_registry,
+};
 use crate::traits;
 use runtime_io::with_externalities;
 pub use system;
@@ -23,6 +25,7 @@ impl_outer_event! {
         data_object_type_registry<T>,
         data_directory<T>,
         data_object_storage_registry<T>,
+        content_directory<T>,
     }
 }
 
@@ -115,6 +118,13 @@ impl data_object_storage_registry::Trait for Test {
     type ContentIdExists = MockContent;
 }
 
+impl content_directory::Trait for Test {
+    type Event = MetaEvent;
+    type MetadataId = u64;
+    type SchemaId = u64;
+    type Members = MockMembers;
+}
+
 impl timestamp::Trait for Test {
     type Moment = u64;
     type OnTimestampSet = ();
@@ -130,6 +140,7 @@ pub struct ExtBuilder {
     first_data_object_type_id: u64,
     first_content_id: u64,
     first_relationship_id: u64,
+    first_metadata_id: u64,
 }
 
 impl Default for ExtBuilder {
@@ -138,6 +149,7 @@ impl Default for ExtBuilder {
             first_data_object_type_id: 1,
             first_content_id: 2,
             first_relationship_id: 3,
+            first_metadata_id: 4,
         }
     }
 }
@@ -155,6 +167,10 @@ impl ExtBuilder {
         self.first_relationship_id = first_relationship_id;
         self
     }
+    pub fn first_metadata_id(mut self, first_metadata_id: u64) -> Self {
+        self.first_metadata_id = first_metadata_id;
+        self
+    }
     pub fn build(self) -> runtime_io::TestExternalities<Blake2Hasher> {
         let mut t = system::GenesisConfig::<Test>::default()
             .build_storage()
@@ -188,6 +204,15 @@ impl ExtBuilder {
             .0,
         );
 
+        t.extend(
+            content_directory::GenesisConfig::<Test> {
+                first_metadata_id: self.first_metadata_id,
+            }
+            .build_storage()
+            .unwrap()
+            .0,
+        );
+
         t.into()
     }
 }
@@ -198,16 +223,19 @@ pub type TestDataObjectType = data_object_type_registry::DataObjectType;
 pub type TestDataDirectory = data_directory::Module<Test>;
 // pub type TestDataObject = data_directory::DataObject<Test>;
 pub type TestDataObjectStorageRegistry = data_object_storage_registry::Module<Test>;
+pub type TestContentDirectory = content_directory::Module<Test>;
 
 pub const TEST_FIRST_DATA_OBJECT_TYPE_ID: u64 = 1000;
 pub const TEST_FIRST_CONTENT_ID: u64 = 2000;
 pub const TEST_FIRST_RELATIONSHIP_ID: u64 = 3000;
+pub const TEST_FIRST_METADATA_ID: u64 = 4000;
 pub fn with_default_mock_builder<R, F: FnOnce() -> R>(f: F) -> R {
     with_externalities(
         &mut ExtBuilder::default()
             .first_data_object_type_id(TEST_FIRST_DATA_OBJECT_TYPE_ID)
             .first_content_id(TEST_FIRST_CONTENT_ID)
             .first_relationship_id(TEST_FIRST_RELATIONSHIP_ID)
+            .first_metadata_id(TEST_FIRST_METADATA_ID)
             .build(),
         || f(),
     )