Browse Source

Restore the tests

Shamil Gadelshin 4 years ago
parent
commit
4f51b7fb4e

+ 2 - 8
runtime-modules/storage/src/data_directory.rs

@@ -31,8 +31,7 @@ use common::origin_validator::ActorOriginValidator;
 
 use crate::data_object_type_registry;
 use crate::data_object_type_registry::IsActiveDataObjectType;
-use crate::{StorageBureaucracy, StorageProviderId, MemberId};
-
+use crate::{MemberId, StorageBureaucracy, StorageProviderId};
 
 // TODO: create a StorageProviderHelper implementation
 
@@ -57,11 +56,7 @@ pub trait Trait:
     type IsActiveDataObjectType: data_object_type_registry::IsActiveDataObjectType<Self>;
 
     /// Validates member id and origin combination.
-    type MemberOriginValidator: ActorOriginValidator<
-        Self::Origin,
-        MemberId<Self>,
-        Self::AccountId,
-    >;
+    type MemberOriginValidator: ActorOriginValidator<Self::Origin, MemberId<Self>, Self::AccountId>;
 }
 
 static MSG_CID_NOT_FOUND: &str = "Content with this ID not found.";
@@ -304,7 +299,6 @@ impl<T: Trait> Module<T> {
     }
 }
 
-
 /// Provides random storage provider id. We use it when assign the content to the storage provider.
 pub trait StorageProviderHelper<T: Trait> {
     /// Provides random storage provider id.

+ 0 - 1
runtime-modules/storage/src/lib.rs

@@ -13,6 +13,5 @@ pub(crate) type StorageBureaucracy<T> = bureaucracy::Module<T, bureaucracy::Inst
 // Alias for the member id.
 pub(crate) type MemberId<T> = <T as membership::members::Trait>::MemberId;
 
-
 /// Storage provider is a worker from the bureaucracy module.
 pub type StorageProviderId<T> = bureaucracy::WorkerId<T>;

+ 52 - 15
runtime-modules/storage/src/tests/data_directory.rs

@@ -5,10 +5,17 @@ use super::mock::*;
 #[test]
 fn succeed_adding_content() {
     with_default_mock_builder(|| {
-        let sender = 1 as u64;
+        let sender = 1u64;
+        let member_id = 1u64;
         // Register a content with 1234 bytes of type 1, which should be recognized.
-        let res =
-            TestDataDirectory::add_content(Origin::signed(sender), 1, 1234, 0, vec![1, 3, 3, 7]);
+        let res = TestDataDirectory::add_content(
+            Origin::signed(sender),
+            member_id,
+            1,
+            1234,
+            0,
+            vec![1, 3, 3, 7],
+        );
         assert!(res.is_ok());
     });
 }
@@ -16,9 +23,17 @@ fn succeed_adding_content() {
 #[test]
 fn accept_content_as_liaison() {
     with_default_mock_builder(|| {
-        let sender = 1 as u64;
-        let res =
-            TestDataDirectory::add_content(Origin::signed(sender), 1, 1234, 0, vec![1, 2, 3, 4]);
+        let sender = 1u64;
+        let member_id = 1u64;
+
+        let res = TestDataDirectory::add_content(
+            Origin::signed(sender),
+            member_id,
+            1,
+            1234,
+            0,
+            vec![1, 2, 3, 4],
+        );
         assert!(res.is_ok());
 
         // An appropriate event should have been fired.
@@ -32,22 +47,37 @@ fn accept_content_as_liaison() {
         assert_ne!(creator, 0xdeadbeefu64);
         assert_eq!(creator, sender);
 
+        let (storage_provider_account_id, storage_provider_id) = hire_storage_provider();
+
         // Accepting content should not work with some random origin
-        let res = TestDataDirectory::accept_content(Origin::signed(1), content_id);
+        let res =
+            TestDataDirectory::accept_content(Origin::signed(55), storage_provider_id, content_id);
         assert!(res.is_err());
 
         // However, with the liaison as origin it should.
-        let res = TestDataDirectory::accept_content(Origin::signed(TEST_MOCK_LIAISON), content_id);
-        assert!(res.is_ok());
+        let res = TestDataDirectory::accept_content(
+            Origin::signed(storage_provider_account_id),
+            storage_provider_id,
+            content_id,
+        );
+        assert_eq!(res, Ok(()));
     });
 }
 
 #[test]
 fn reject_content_as_liaison() {
     with_default_mock_builder(|| {
-        let sender = 1 as u64;
-        let res =
-            TestDataDirectory::add_content(Origin::signed(sender), 1, 1234, 0, vec![1, 2, 3, 4]);
+        let sender = 1u64;
+        let member_id = 1u64;
+
+        let res = TestDataDirectory::add_content(
+            Origin::signed(sender),
+            member_id,
+            1,
+            1234,
+            0,
+            vec![1, 2, 3, 4],
+        );
         assert!(res.is_ok());
 
         // An appropriate event should have been fired.
@@ -61,12 +91,19 @@ fn reject_content_as_liaison() {
         assert_ne!(creator, 0xdeadbeefu64);
         assert_eq!(creator, sender);
 
+        let (storage_provider_account_id, storage_provider_id) = hire_storage_provider();
+
         // Rejecting content should not work with some random origin
-        let res = TestDataDirectory::reject_content(Origin::signed(1), content_id);
+        let res =
+            TestDataDirectory::reject_content(Origin::signed(55), storage_provider_id, content_id);
         assert!(res.is_err());
 
         // However, with the liaison as origin it should.
-        let res = TestDataDirectory::reject_content(Origin::signed(TEST_MOCK_LIAISON), content_id);
-        assert!(res.is_ok());
+        let res = TestDataDirectory::reject_content(
+            Origin::signed(storage_provider_account_id),
+            storage_provider_id,
+            content_id,
+        );
+        assert_eq!(res, Ok(()));
     });
 }

+ 0 - 20
runtime-modules/storage/src/tests/data_object_storage_registry.rs

@@ -1,26 +1,6 @@
 #![cfg(test)]
 
 use super::mock::*;
-use srml_support::StorageLinkedMap;
-
-fn hire_storage_provider() -> (u64, u32) {
-    let storage_provider_id = 1;
-    let role_account_id = 1;
-
-    let storage_provider = bureaucracy::Worker {
-        member_id: 1,
-        role_account: role_account_id,
-        reward_relationship: None,
-        role_stake_profile: None,
-    };
-
-    <bureaucracy::WorkerById<Test, bureaucracy::Instance2>>::insert(
-        storage_provider_id,
-        storage_provider,
-    );
-
-    (role_account_id, storage_provider_id)
-}
 
 #[test]
 fn initial_state() {

+ 38 - 26
runtime-modules/storage/src/tests/mock.rs

@@ -16,7 +16,7 @@ pub use sr_primitives::{
 
 use crate::data_directory::ContentIdExists;
 use crate::data_object_type_registry::IsActiveDataObjectType;
-use srml_support::{impl_outer_event, impl_outer_origin, parameter_types};
+use srml_support::{impl_outer_event, impl_outer_origin, parameter_types, StorageLinkedMap};
 
 mod bureaucracy_mod {
     pub use bureaucracy::Event;
@@ -44,30 +44,9 @@ 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 const TEST_MOCK_LIAISON: u64 = 0xd00du64;
+pub const TEST_MOCK_LIAISON_STORAGE_PROVIDER_ID: u32 = 1;
 pub const TEST_MOCK_EXISTING_CID: u64 = 42;
 
-pub struct MockRoles {}
-impl roles::traits::Roles<Test> for MockRoles {
-    fn is_role_account(_account_id: &<Test as system::Trait>::AccountId) -> bool {
-        false
-    }
-
-    fn account_has_role(
-        account_id: &<Test as system::Trait>::AccountId,
-        _role: actors::Role,
-    ) -> bool {
-        *account_id == TEST_MOCK_LIAISON
-    }
-
-    fn random_account_for_role(
-        _role: actors::Role,
-    ) -> Result<<Test as system::Trait>::AccountId, &'static str> {
-        // We "randomly" select an account Id.
-        Ok(TEST_MOCK_LIAISON)
-    }
-}
-
 pub struct AnyDataObjectTypeIsActive {}
 impl<T: data_object_type_registry::Trait> IsActiveDataObjectType<T> for AnyDataObjectTypeIsActive {
     fn is_active_data_object_type(_which: &T::DataObjectTypeId) -> bool {
@@ -93,7 +72,7 @@ impl ContentIdExists<Test> for MockContent {
                     time: 1024,
                 },
                 owner: 1,
-                liaison: TEST_MOCK_LIAISON,
+                liaison: TEST_MOCK_LIAISON_STORAGE_PROVIDER_ID,
                 liaison_judgement: data_directory::LiaisonJudgement::Pending,
                 ipfs_content_id: vec![],
             }),
@@ -180,9 +159,23 @@ impl data_object_type_registry::Trait for Test {
 impl data_directory::Trait for Test {
     type Event = MetaEvent;
     type ContentId = u64;
-    type Roles = MockRoles;
+    type StorageProviderHelper = ();
     type IsActiveDataObjectType = AnyDataObjectTypeIsActive;
-    type SchemaId = u64;
+    type MemberOriginValidator = ();
+}
+
+impl crate::data_directory::StorageProviderHelper<Test> for () {
+    fn get_random_storage_provider() -> Result<u32, &'static str> {
+        Ok(1)
+    }
+}
+
+impl common::origin_validator::ActorOriginValidator<Origin, u64, u64> for () {
+    fn ensure_actor_origin(origin: Origin, _account_id: u64) -> Result<u64, &'static str> {
+        let signed_account_id = system::ensure_signed(origin)?;
+
+        Ok(signed_account_id)
+    }
 }
 
 impl data_object_storage_registry::Trait for Test {
@@ -323,3 +316,22 @@ pub fn with_default_mock_builder<R, F: FnOnce() -> R>(f: F) -> R {
             f()
         })
 }
+
+pub(crate) fn hire_storage_provider() -> (u64, u32) {
+    let storage_provider_id = 1;
+    let role_account_id = 1;
+
+    let storage_provider = bureaucracy::Worker {
+        member_id: 1,
+        role_account: role_account_id,
+        reward_relationship: None,
+        role_stake_profile: None,
+    };
+
+    <bureaucracy::WorkerById<Test, bureaucracy::Instance2>>::insert(
+        storage_provider_id,
+        storage_provider,
+    );
+
+    (role_account_id, storage_provider_id)
+}

+ 5 - 5
runtime/src/integration/storage.rs

@@ -1,9 +1,9 @@
-use crate::{Runtime, ActorId};
+use crate::{ActorId, Runtime};
 
 pub struct StorageProviderHelper;
 
 impl storage::data_directory::StorageProviderHelper<Runtime> for StorageProviderHelper {
-	fn get_random_storage_provider() -> Result<ActorId, &'static str> {
-		unimplemented!()
-	}
-}
+    fn get_random_storage_provider() -> Result<ActorId, &'static str> {
+        unimplemented!()
+    }
+}

+ 0 - 1
runtime/src/lib.rs

@@ -113,7 +113,6 @@ pub type PostId = u64;
 /// Represent an actor in membership group, which is the same in the working groups.
 pub type ActorId = u64;
 
-
 /// Opaque types. These are used by the CLI to instantiate machinery that don't need to know
 /// the specifics of the runtime. They can then be made to be agnostic over specific formats
 /// of data like extrinsics, allowing for them to continue syncing the network through upgrades