From bf18b86af8e0ab02b98262e8ace3c7fc844df499 Mon Sep 17 00:00:00 2001 From: muditDhiway Date: Mon, 20 Jan 2025 12:17:15 +0530 Subject: [PATCH] Registries: Add tests for AuthorizationNotFound (#566) * registries: add tests for AuthorizationNotFound --------- Co-authored-by: Mudit Sarda --- pallets/asset/src/tests.rs | 113 +++-- pallets/registries/src/tests.rs | 840 ++++++++++++++++++++++++++++++++ test-utils/service/src/lib.rs | 2 +- 3 files changed, 902 insertions(+), 53 deletions(-) diff --git a/pallets/asset/src/tests.rs b/pallets/asset/src/tests.rs index bcd67534c..739d6ad00 100644 --- a/pallets/asset/src/tests.rs +++ b/pallets/asset/src/tests.rs @@ -278,12 +278,15 @@ fn asset_unauthorized_operation_should_fail() { authorization_id.clone() )); - assert_err!(Asset::issue( - DoubleOrigin(unauthorized_author.clone(), unauthorized_creator.clone()).into(), // Unauthorized author attempting the action - issue_entry.clone(), - issue_entry_digest, - unauthorized_authorization_id.clone() - ), Error::::UnauthorizedOperation); + assert_err!( + Asset::issue( + DoubleOrigin(unauthorized_author.clone(), unauthorized_creator.clone()).into(), // Unauthorized author attempting the action + issue_entry.clone(), + issue_entry_digest, + unauthorized_authorization_id.clone() + ), + Error::::UnauthorizedOperation + ); assert_ok!(Asset::issue( DoubleOrigin(author.clone(), creator.clone()).into(), @@ -292,29 +295,32 @@ fn asset_unauthorized_operation_should_fail() { authorization_id.clone() )); - assert_err!(Asset::transfer( - DoubleOrigin(unauthorized_author.clone(), unauthorized_creator.clone()).into(), - transfer_entry.clone(), - transfer_entry_digest, - ), - Error::::UnauthorizedOperation + assert_err!( + Asset::transfer( + DoubleOrigin(unauthorized_author.clone(), unauthorized_creator.clone()).into(), + transfer_entry.clone(), + transfer_entry_digest, + ), + Error::::UnauthorizedOperation ); - assert_err!(Asset::transfer( - DoubleOrigin(author.clone(), creator.clone()).into(), - unauthorized_transfer_entry.clone(), - unauthorized_entry_digest, - ), - Error::::UnauthorizedOperation + assert_err!( + Asset::transfer( + DoubleOrigin(author.clone(), creator.clone()).into(), + unauthorized_transfer_entry.clone(), + unauthorized_entry_digest, + ), + Error::::UnauthorizedOperation ); - assert_err!(Asset::status_change( - DoubleOrigin(unauthorized_author.clone(), unauthorized_creator.clone()).into(), - asset_id.clone(), - Some(instance_id.clone()), - AssetStatusOf::INACTIVE - ), - Error::::UnauthorizedOperation + assert_err!( + Asset::status_change( + DoubleOrigin(unauthorized_author.clone(), unauthorized_creator.clone()).into(), + asset_id.clone(), + Some(instance_id.clone()), + AssetStatusOf::INACTIVE + ), + Error::::UnauthorizedOperation ); assert_ok!(Asset::vc_create( @@ -324,13 +330,14 @@ fn asset_unauthorized_operation_should_fail() { authorization_id.clone() )); - assert_err!(Asset::vc_issue( - DoubleOrigin(unauthorized_author.clone(), unauthorized_creator.clone()).into(), - issue_entry.clone(), - issue_entry_digest, - unauthorized_authorization_id - ), - Error::::UnauthorizedOperation + assert_err!( + Asset::vc_issue( + DoubleOrigin(unauthorized_author.clone(), unauthorized_creator.clone()).into(), + issue_entry.clone(), + issue_entry_digest, + unauthorized_authorization_id + ), + Error::::UnauthorizedOperation ); assert_ok!(Asset::vc_issue( @@ -340,31 +347,33 @@ fn asset_unauthorized_operation_should_fail() { authorization_id )); - assert_err!(Asset::vc_transfer( - DoubleOrigin(unauthorized_author.clone(), unauthorized_creator.clone()).into(), - transfer_entry.clone(), - transfer_entry_digest, - ), - Error::::UnauthorizedOperation + assert_err!( + Asset::vc_transfer( + DoubleOrigin(unauthorized_author.clone(), unauthorized_creator.clone()).into(), + transfer_entry.clone(), + transfer_entry_digest, + ), + Error::::UnauthorizedOperation ); - assert_err!(Asset::vc_transfer( - DoubleOrigin(author.clone(), creator.clone()).into(), - unauthorized_transfer_entry.clone(), - unauthorized_entry_digest, - ), - Error::::UnauthorizedOperation + assert_err!( + Asset::vc_transfer( + DoubleOrigin(author.clone(), creator.clone()).into(), + unauthorized_transfer_entry.clone(), + unauthorized_entry_digest, + ), + Error::::UnauthorizedOperation ); - assert_err!(Asset::vc_status_change( - DoubleOrigin(unauthorized_author.clone(), unauthorized_creator.clone()).into(), - asset_id.clone(), - Some(instance_id.clone()), - AssetStatusOf::INACTIVE - ), - Error::::UnauthorizedOperation + assert_err!( + Asset::vc_status_change( + DoubleOrigin(unauthorized_author.clone(), unauthorized_creator.clone()).into(), + asset_id.clone(), + Some(instance_id.clone()), + AssetStatusOf::INACTIVE + ), + Error::::UnauthorizedOperation ); - }); } diff --git a/pallets/registries/src/tests.rs b/pallets/registries/src/tests.rs index 451e05f67..394990e6b 100644 --- a/pallets/registries/src/tests.rs +++ b/pallets/registries/src/tests.rs @@ -2151,3 +2151,843 @@ fn registry_id_should_be_updated_on_namespace_chainstorage_on_create() { ); }); } + +#[test] +fn remove_delegate_should_fail_if_admin_authorization_is_not_found() { + let creator = ACCOUNT_00; + let delegate = ACCOUNT_01; + let registry = [2u8; 256].to_vec(); + + let namespace = [2u8; 256].to_vec(); + let namespace_digest = ::Hashing::hash(&namespace.encode()[..]); + + let id_digest = ::Hashing::hash( + &[&namespace_digest.encode()[..], &creator.encode()[..]].concat()[..], + ); + let namespace_id: NameSpaceIdOf = generate_namespace_id::(&id_digest); + + let namespace_auth_id_digest = ::Hashing::hash( + &[&namespace_id.encode()[..], &creator.encode()[..], &creator.encode()[..]].concat()[..], + ); + let namespace_authorization_id: NamespaceAuthorizationIdOf = + generate_namespace_authorization_id::(&namespace_auth_id_digest); + + let raw_blob = [2u8; 256].to_vec(); + let blob: RegistryBlobOf = BoundedVec::try_from(raw_blob) + .expect("Test blob should fit into the expected input length for the test runtime."); + + let registry_digest = ::Hashing::hash(®istry.encode()[..]); + + let id_digest = ::Hashing::hash( + &[®istry_digest.encode()[..], &creator.encode()[..]].concat()[..], + ); + + let registry_id: RegistryIdOf = generate_registry_id::(&id_digest); + + let auth_id_digest = ::Hashing::hash( + &[®istry_id.encode()[..], &creator.encode()[..], &creator.encode()[..]].concat()[..], + ); + + let non_existent_auth_id_digest = + ::Hashing::hash(®istry_id.encode()[..]); + + let authorization_id: RegistryAuthorizationIdOf = + generate_authorization_id::(&auth_id_digest); + + let non_existent_authorization_id: RegistryAuthorizationIdOf = + generate_authorization_id::(&non_existent_auth_id_digest); + + let delegate_auth_id_digest = ::Hashing::hash( + &[®istry_id.encode()[..], &delegate.encode()[..], &creator.encode()[..]].concat()[..], + ); + + let delegate_authorization_id: RegistryAuthorizationIdOf = + generate_authorization_id::(&delegate_auth_id_digest); + + let raw_schema = [2u8; 256].to_vec(); + let schema: InputSchemaOf = BoundedVec::try_from(raw_schema) + .expect("Test Schema should fit into the expected input length for the test runtime."); + let _digest: SchemaHashOf = ::Hashing::hash(&schema[..]); + let schema_id_digest = ::Hashing::hash(&schema.encode()[..]); + let schema_id: SchemaIdOf = generate_schema_id::(&schema_id_digest); + + new_test_ext().execute_with(|| { + assert_ok!(NameSpace::create( + frame_system::RawOrigin::Signed(creator.clone()).into(), + namespace_digest, + None, + )); + + assert_ok!(Registries::create( + frame_system::RawOrigin::Signed(creator.clone()).into(), + registry_digest, + namespace_authorization_id.clone(), + Some(schema_id), + Some(blob.clone()), + )); + + assert_ok!(Registries::add_delegate( + frame_system::RawOrigin::Signed(creator.clone()).into(), + registry_id.clone(), + delegate.clone(), + namespace_authorization_id.clone(), + authorization_id.clone(), + )); + + assert_err!( + Registries::remove_delegate( + frame_system::RawOrigin::Signed(creator.clone()).into(), + registry_id.clone(), + delegate_authorization_id, + namespace_authorization_id.clone(), + non_existent_authorization_id.clone(), + ), + Error::::AuthorizationNotFound + ); + }); +} + +#[test] +fn remove_delegate_should_fail_if_remove_authorization_is_not_found_as_delegate_not_added() { + let creator = ACCOUNT_00; + let delegate = ACCOUNT_01; + let registry = [2u8; 256].to_vec(); + + let namespace = [2u8; 256].to_vec(); + let namespace_digest = ::Hashing::hash(&namespace.encode()[..]); + + let id_digest = ::Hashing::hash( + &[&namespace_digest.encode()[..], &creator.encode()[..]].concat()[..], + ); + let namespace_id: NameSpaceIdOf = generate_namespace_id::(&id_digest); + + let namespace_auth_id_digest = ::Hashing::hash( + &[&namespace_id.encode()[..], &creator.encode()[..], &creator.encode()[..]].concat()[..], + ); + let namespace_authorization_id: NamespaceAuthorizationIdOf = + generate_namespace_authorization_id::(&namespace_auth_id_digest); + + let raw_blob = [2u8; 256].to_vec(); + let blob: RegistryBlobOf = BoundedVec::try_from(raw_blob) + .expect("Test blob should fit into the expected input length for the test runtime."); + + let registry_digest = ::Hashing::hash(®istry.encode()[..]); + + let id_digest = ::Hashing::hash( + &[®istry_digest.encode()[..], &creator.encode()[..]].concat()[..], + ); + + let registry_id: RegistryIdOf = generate_registry_id::(&id_digest); + + let auth_id_digest = ::Hashing::hash( + &[®istry_id.encode()[..], &creator.encode()[..], &creator.encode()[..]].concat()[..], + ); + + let authorization_id: RegistryAuthorizationIdOf = + generate_authorization_id::(&auth_id_digest); + + let delegate_auth_id_digest = ::Hashing::hash( + &[®istry_id.encode()[..], &delegate.encode()[..], &creator.encode()[..]].concat()[..], + ); + + let delegate_authorization_id: RegistryAuthorizationIdOf = + generate_authorization_id::(&delegate_auth_id_digest); + + let raw_schema = [2u8; 256].to_vec(); + let schema: InputSchemaOf = BoundedVec::try_from(raw_schema) + .expect("Test Schema should fit into the expected input length for the test runtime."); + let _digest: SchemaHashOf = ::Hashing::hash(&schema[..]); + let schema_id_digest = ::Hashing::hash(&schema.encode()[..]); + let schema_id: SchemaIdOf = generate_schema_id::(&schema_id_digest); + + new_test_ext().execute_with(|| { + assert_ok!(NameSpace::create( + frame_system::RawOrigin::Signed(creator.clone()).into(), + namespace_digest, + None, + )); + + assert_ok!(Registries::create( + frame_system::RawOrigin::Signed(creator.clone()).into(), + registry_digest, + namespace_authorization_id.clone(), + Some(schema_id), + Some(blob.clone()), + )); + + assert_err!( + Registries::remove_delegate( + frame_system::RawOrigin::Signed(creator.clone()).into(), + registry_id.clone(), + delegate_authorization_id, + namespace_authorization_id.clone(), + authorization_id.clone(), + ), + Error::::AuthorizationNotFound + ); + }); +} + +#[test] +fn remove_delegate_should_fail_if_remove_authorization_is_not_found_as_non_existant_auth_id_entered( +) { + let creator = ACCOUNT_00; + let delegate = ACCOUNT_01; + let registry = [2u8; 256].to_vec(); + + let namespace = [2u8; 256].to_vec(); + let namespace_digest = ::Hashing::hash(&namespace.encode()[..]); + + let id_digest = ::Hashing::hash( + &[&namespace_digest.encode()[..], &creator.encode()[..]].concat()[..], + ); + let namespace_id: NameSpaceIdOf = generate_namespace_id::(&id_digest); + + let namespace_auth_id_digest = ::Hashing::hash( + &[&namespace_id.encode()[..], &creator.encode()[..], &creator.encode()[..]].concat()[..], + ); + let namespace_authorization_id: NamespaceAuthorizationIdOf = + generate_namespace_authorization_id::(&namespace_auth_id_digest); + + let raw_blob = [2u8; 256].to_vec(); + let blob: RegistryBlobOf = BoundedVec::try_from(raw_blob) + .expect("Test blob should fit into the expected input length for the test runtime."); + + let registry_digest = ::Hashing::hash(®istry.encode()[..]); + + let id_digest = ::Hashing::hash( + &[®istry_digest.encode()[..], &creator.encode()[..]].concat()[..], + ); + + let registry_id: RegistryIdOf = generate_registry_id::(&id_digest); + + let auth_id_digest = ::Hashing::hash( + &[®istry_id.encode()[..], &creator.encode()[..], &creator.encode()[..]].concat()[..], + ); + + let authorization_id: RegistryAuthorizationIdOf = + generate_authorization_id::(&auth_id_digest); + + let non_existent_auth_id_digest = + ::Hashing::hash(®istry_id.encode()[..]); + + let non_existent_authorization_id: RegistryAuthorizationIdOf = + generate_authorization_id::(&non_existent_auth_id_digest); + + let raw_schema = [2u8; 256].to_vec(); + let schema: InputSchemaOf = BoundedVec::try_from(raw_schema) + .expect("Test Schema should fit into the expected input length for the test runtime."); + let _digest: SchemaHashOf = ::Hashing::hash(&schema[..]); + let schema_id_digest = ::Hashing::hash(&schema.encode()[..]); + let schema_id: SchemaIdOf = generate_schema_id::(&schema_id_digest); + + new_test_ext().execute_with(|| { + assert_ok!(NameSpace::create( + frame_system::RawOrigin::Signed(creator.clone()).into(), + namespace_digest, + None, + )); + + assert_ok!(Registries::create( + frame_system::RawOrigin::Signed(creator.clone()).into(), + registry_digest, + namespace_authorization_id.clone(), + Some(schema_id), + Some(blob.clone()), + )); + + assert_ok!(Registries::add_delegate( + frame_system::RawOrigin::Signed(creator.clone()).into(), + registry_id.clone(), + delegate.clone(), + namespace_authorization_id.clone(), + authorization_id.clone(), + )); + + assert_err!( + Registries::remove_delegate( + frame_system::RawOrigin::Signed(creator.clone()).into(), + registry_id.clone(), + non_existent_authorization_id, + namespace_authorization_id.clone(), + authorization_id.clone(), + ), + Error::::AuthorizationNotFound + ); + }); +} + +#[test] +fn revoke_should_fail_if_admin_authorization_is_not_found() { + let creator = ACCOUNT_00; + let registry = [2u8; 256].to_vec(); + + let namespace = [2u8; 256].to_vec(); + let namespace_digest = ::Hashing::hash(&namespace.encode()[..]); + + let id_digest = ::Hashing::hash( + &[&namespace_digest.encode()[..], &creator.encode()[..]].concat()[..], + ); + let namespace_id: NameSpaceIdOf = generate_namespace_id::(&id_digest); + + let namespace_auth_id_digest = ::Hashing::hash( + &[&namespace_id.encode()[..], &creator.encode()[..], &creator.encode()[..]].concat()[..], + ); + let namespace_authorization_id: NamespaceAuthorizationIdOf = + generate_namespace_authorization_id::(&namespace_auth_id_digest); + + let raw_blob = [2u8; 256].to_vec(); + let blob: RegistryBlobOf = BoundedVec::try_from(raw_blob) + .expect("Test blob should fit into the expected input length for the test runtime."); + + let registry_digest = ::Hashing::hash(®istry.encode()[..]); + + let id_digest = ::Hashing::hash( + &[®istry_digest.encode()[..], &creator.encode()[..]].concat()[..], + ); + + let registry_id: RegistryIdOf = generate_registry_id::(&id_digest); + + let non_existent_auth_id_digest = + ::Hashing::hash(®istry_id.encode()[..]); + + let non_existent_authorization_id: RegistryAuthorizationIdOf = + generate_authorization_id::(&non_existent_auth_id_digest); + + let raw_schema = [2u8; 256].to_vec(); + let schema: InputSchemaOf = BoundedVec::try_from(raw_schema) + .expect("Test Schema should fit into the expected input length for the test runtime."); + let _digest: SchemaHashOf = ::Hashing::hash(&schema[..]); + let schema_id_digest = ::Hashing::hash(&schema.encode()[..]); + let schema_id: SchemaIdOf = generate_schema_id::(&schema_id_digest); + + new_test_ext().execute_with(|| { + assert_ok!(NameSpace::create( + frame_system::RawOrigin::Signed(creator.clone()).into(), + namespace_digest, + None, + )); + + assert_ok!(Registries::create( + frame_system::RawOrigin::Signed(creator.clone()).into(), + registry_digest, + namespace_authorization_id.clone(), + Some(schema_id), + Some(blob.clone()), + )); + + assert_err!( + Registries::revoke( + frame_system::RawOrigin::Signed(creator.clone()).into(), + registry_id.clone(), + namespace_authorization_id.clone(), + non_existent_authorization_id.clone(), + ), + Error::::AuthorizationNotFound + ); + }); +} + +#[test] +fn reinstate_should_fail_if_admin_authorization_is_not_found() { + let creator = ACCOUNT_00; + let registry = [2u8; 256].to_vec(); + + let namespace = [2u8; 256].to_vec(); + let namespace_digest = ::Hashing::hash(&namespace.encode()[..]); + + let id_digest = ::Hashing::hash( + &[&namespace_digest.encode()[..], &creator.encode()[..]].concat()[..], + ); + let namespace_id: NameSpaceIdOf = generate_namespace_id::(&id_digest); + + let namespace_auth_id_digest = ::Hashing::hash( + &[&namespace_id.encode()[..], &creator.encode()[..], &creator.encode()[..]].concat()[..], + ); + let namespace_authorization_id: NamespaceAuthorizationIdOf = + generate_namespace_authorization_id::(&namespace_auth_id_digest); + + let raw_blob = [2u8; 256].to_vec(); + let blob: RegistryBlobOf = BoundedVec::try_from(raw_blob) + .expect("Test blob should fit into the expected input length for the test runtime."); + + let registry_digest = ::Hashing::hash(®istry.encode()[..]); + + let id_digest = ::Hashing::hash( + &[®istry_digest.encode()[..], &creator.encode()[..]].concat()[..], + ); + + let registry_id: RegistryIdOf = generate_registry_id::(&id_digest); + + let auth_id_digest = ::Hashing::hash( + &[®istry_id.encode()[..], &creator.encode()[..], &creator.encode()[..]].concat()[..], + ); + + let non_existent_auth_id_digest = + ::Hashing::hash(®istry_id.encode()[..]); + + let authorization_id: RegistryAuthorizationIdOf = + generate_authorization_id::(&auth_id_digest); + + let non_existent_authorization_id: RegistryAuthorizationIdOf = + generate_authorization_id::(&non_existent_auth_id_digest); + + let raw_schema = [2u8; 256].to_vec(); + let schema: InputSchemaOf = BoundedVec::try_from(raw_schema) + .expect("Test Schema should fit into the expected input length for the test runtime."); + let _digest: SchemaHashOf = ::Hashing::hash(&schema[..]); + let schema_id_digest = ::Hashing::hash(&schema.encode()[..]); + let schema_id: SchemaIdOf = generate_schema_id::(&schema_id_digest); + + new_test_ext().execute_with(|| { + assert_ok!(NameSpace::create( + frame_system::RawOrigin::Signed(creator.clone()).into(), + namespace_digest, + None, + )); + + assert_ok!(Registries::create( + frame_system::RawOrigin::Signed(creator.clone()).into(), + registry_digest, + namespace_authorization_id.clone(), + Some(schema_id), + Some(blob.clone()), + )); + + assert_ok!(Registries::revoke( + frame_system::RawOrigin::Signed(creator.clone()).into(), + registry_id.clone(), + namespace_authorization_id.clone(), + authorization_id.clone(), + )); + + assert_err!( + Registries::reinstate( + frame_system::RawOrigin::Signed(creator.clone()).into(), + registry_id.clone(), + namespace_authorization_id.clone(), + non_existent_authorization_id.clone(), + ), + Error::::AuthorizationNotFound + ); + }); +} + +#[test] +fn update_should_fail_if_admin_authorization_is_not_found() { + let creator = ACCOUNT_00; + let registry = [2u8; 256].to_vec(); + + let namespace = [2u8; 256].to_vec(); + let namespace_digest = ::Hashing::hash(&namespace.encode()[..]); + + let id_digest = ::Hashing::hash( + &[&namespace_digest.encode()[..], &creator.encode()[..]].concat()[..], + ); + let namespace_id: NameSpaceIdOf = generate_namespace_id::(&id_digest); + + let namespace_auth_id_digest = ::Hashing::hash( + &[&namespace_id.encode()[..], &creator.encode()[..], &creator.encode()[..]].concat()[..], + ); + let namespace_authorization_id: NamespaceAuthorizationIdOf = + generate_namespace_authorization_id::(&namespace_auth_id_digest); + + let new_digest = + ::Hashing::hash(&[3u8; 256].to_vec().encode()[..]); + + let raw_blob = [2u8; 256].to_vec(); + let initial_blob: RegistryBlobOf = BoundedVec::try_from(raw_blob.clone()) + .expect("Test Blob should fit into the expected input length for the test runtime."); + + let new_raw_blob = [4u8; 256].to_vec(); + let new_blob: RegistryBlobOf = BoundedVec::try_from(new_raw_blob.clone()) + .expect("New Test Blob should fit into the expected input length for the test runtime."); + + let registry_digest = ::Hashing::hash(®istry.encode()[..]); + + let id_digest = ::Hashing::hash( + &[®istry_digest.encode()[..], &creator.encode()[..]].concat()[..], + ); + + let registry_id: RegistryIdOf = generate_registry_id::(&id_digest); + + let non_existent_auth_id_digest = + ::Hashing::hash(®istry_id.encode()[..]); + + let non_existent_authorization_id: RegistryAuthorizationIdOf = + generate_authorization_id::(&non_existent_auth_id_digest); + + let raw_schema = [2u8; 256].to_vec(); + let schema: InputSchemaOf = BoundedVec::try_from(raw_schema) + .expect("Test Schema should fit into the expected input length for the test runtime."); + let _digest: SchemaHashOf = ::Hashing::hash(&schema[..]); + let schema_id_digest = ::Hashing::hash(&schema.encode()[..]); + let schema_id: SchemaIdOf = generate_schema_id::(&schema_id_digest); + + new_test_ext().execute_with(|| { + assert_ok!(NameSpace::create( + frame_system::RawOrigin::Signed(creator.clone()).into(), + namespace_digest, + None, + )); + + assert_ok!(Registries::create( + frame_system::RawOrigin::Signed(creator.clone()).into(), + registry_digest, + namespace_authorization_id.clone(), + Some(schema_id), + Some(initial_blob), + )); + + assert_err!( + Registries::update( + frame_system::RawOrigin::Signed(creator.clone()).into(), + registry_id.clone(), + new_digest, + Some(new_blob.clone()), + namespace_authorization_id.clone(), + non_existent_authorization_id.clone(), + ), + Error::::AuthorizationNotFound + ); + }); +} + +#[test] +fn archive_should_fail_if_admin_authorization_is_not_found() { + let creator = ACCOUNT_00; + let registry = [2u8; 256].to_vec(); + + let namespace = [2u8; 256].to_vec(); + let namespace_digest = ::Hashing::hash(&namespace.encode()[..]); + + let id_digest = ::Hashing::hash( + &[&namespace_digest.encode()[..], &creator.encode()[..]].concat()[..], + ); + let namespace_id: NameSpaceIdOf = generate_namespace_id::(&id_digest); + + let namespace_auth_id_digest = ::Hashing::hash( + &[&namespace_id.encode()[..], &creator.encode()[..], &creator.encode()[..]].concat()[..], + ); + let namespace_authorization_id: NamespaceAuthorizationIdOf = + generate_namespace_authorization_id::(&namespace_auth_id_digest); + + let raw_blob = [2u8; 256].to_vec(); + let blob: RegistryBlobOf = BoundedVec::try_from(raw_blob) + .expect("Test blob should fit into the expected input length for the test runtime."); + + let registry_digest = ::Hashing::hash(®istry.encode()[..]); + + let id_digest = ::Hashing::hash( + &[®istry_digest.encode()[..], &creator.encode()[..]].concat()[..], + ); + + let registry_id: RegistryIdOf = generate_registry_id::(&id_digest); + + let non_existent_auth_id_digest = + ::Hashing::hash(®istry_id.encode()[..]); + + let non_existent_authorization_id: RegistryAuthorizationIdOf = + generate_authorization_id::(&non_existent_auth_id_digest); + + let raw_schema = [2u8; 256].to_vec(); + let schema: InputSchemaOf = BoundedVec::try_from(raw_schema) + .expect("Test Schema should fit into the expected input length for the test runtime."); + let _digest: SchemaHashOf = ::Hashing::hash(&schema[..]); + let schema_id_digest = ::Hashing::hash(&schema.encode()[..]); + let schema_id: SchemaIdOf = generate_schema_id::(&schema_id_digest); + + new_test_ext().execute_with(|| { + assert_ok!(NameSpace::create( + frame_system::RawOrigin::Signed(creator.clone()).into(), + namespace_digest, + None, + )); + + assert_ok!(Registries::create( + frame_system::RawOrigin::Signed(creator.clone()).into(), + registry_digest, + namespace_authorization_id.clone(), + Some(schema_id), + Some(blob.clone()), + )); + + assert_err!( + Registries::archive( + frame_system::RawOrigin::Signed(creator.clone()).into(), + registry_id.clone(), + namespace_authorization_id.clone(), + non_existent_authorization_id.clone(), + ), + Error::::AuthorizationNotFound + ); + }); +} + +#[test] +fn restore_should_fail_if_admin_authorization_is_not_found() { + let creator = ACCOUNT_00; + let registry = [2u8; 256].to_vec(); + + let namespace = [2u8; 256].to_vec(); + let namespace_digest = ::Hashing::hash(&namespace.encode()[..]); + + let id_digest = ::Hashing::hash( + &[&namespace_digest.encode()[..], &creator.encode()[..]].concat()[..], + ); + let namespace_id: NameSpaceIdOf = generate_namespace_id::(&id_digest); + + let namespace_auth_id_digest = ::Hashing::hash( + &[&namespace_id.encode()[..], &creator.encode()[..], &creator.encode()[..]].concat()[..], + ); + let namespace_authorization_id: NamespaceAuthorizationIdOf = + generate_namespace_authorization_id::(&namespace_auth_id_digest); + + let raw_blob = [2u8; 256].to_vec(); + let blob: RegistryBlobOf = BoundedVec::try_from(raw_blob) + .expect("Test blob should fit into the expected input length for the test runtime."); + + let registry_digest = ::Hashing::hash(®istry.encode()[..]); + + let id_digest = ::Hashing::hash( + &[®istry_digest.encode()[..], &creator.encode()[..]].concat()[..], + ); + + let registry_id: RegistryIdOf = generate_registry_id::(&id_digest); + + let auth_id_digest = ::Hashing::hash( + &[®istry_id.encode()[..], &creator.encode()[..], &creator.encode()[..]].concat()[..], + ); + + let non_existent_auth_id_digest = + ::Hashing::hash(®istry_id.encode()[..]); + + let authorization_id: RegistryAuthorizationIdOf = + generate_authorization_id::(&auth_id_digest); + + let non_existent_authorization_id: RegistryAuthorizationIdOf = + generate_authorization_id::(&non_existent_auth_id_digest); + + let raw_schema = [2u8; 256].to_vec(); + let schema: InputSchemaOf = BoundedVec::try_from(raw_schema) + .expect("Test Schema should fit into the expected input length for the test runtime."); + let _digest: SchemaHashOf = ::Hashing::hash(&schema[..]); + let schema_id_digest = ::Hashing::hash(&schema.encode()[..]); + let schema_id: SchemaIdOf = generate_schema_id::(&schema_id_digest); + + new_test_ext().execute_with(|| { + assert_ok!(NameSpace::create( + frame_system::RawOrigin::Signed(creator.clone()).into(), + namespace_digest, + None, + )); + + assert_ok!(Registries::create( + frame_system::RawOrigin::Signed(creator.clone()).into(), + registry_digest, + namespace_authorization_id.clone(), + Some(schema_id), + Some(blob.clone()), + )); + + assert_ok!(Registries::archive( + frame_system::RawOrigin::Signed(creator.clone()).into(), + registry_id.clone(), + namespace_authorization_id.clone(), + authorization_id.clone(), + )); + + assert_err!( + Registries::restore( + frame_system::RawOrigin::Signed(creator.clone()).into(), + registry_id.clone(), + namespace_authorization_id.clone(), + non_existent_authorization_id.clone(), + ), + Error::::AuthorizationNotFound + ); + }); +} + +#[test] +fn ensure_authorization_origin_should_fail_if_authorization_is_not_found() { + let creator = ACCOUNT_00; + let delegate = ACCOUNT_01; + let registry = [2u8; 256].to_vec(); + + let registry_digest = ::Hashing::hash(®istry.encode()[..]); + + let id_digest = ::Hashing::hash( + &[®istry_digest.encode()[..], &creator.encode()[..]].concat()[..], + ); + + let registry_id: RegistryIdOf = generate_registry_id::(&id_digest); + + let non_existent_auth_id_digest = + ::Hashing::hash(®istry_id.encode()[..]); + + let non_existent_authorization_id: RegistryAuthorizationIdOf = + generate_authorization_id::(&non_existent_auth_id_digest); + + new_test_ext().execute_with(|| { + assert_err!( + Registries::ensure_authorization_origin(&non_existent_authorization_id, &delegate), + Error::::AuthorizationNotFound + ); + }); +} + +#[test] +fn ensure_authorization_reinstate_origin_should_fail_if_authorization_is_not_found() { + let creator = ACCOUNT_00; + let delegate = ACCOUNT_01; + let registry = [2u8; 256].to_vec(); + + let registry_digest = ::Hashing::hash(®istry.encode()[..]); + + let id_digest = ::Hashing::hash( + &[®istry_digest.encode()[..], &creator.encode()[..]].concat()[..], + ); + + let registry_id: RegistryIdOf = generate_registry_id::(&id_digest); + + let non_existent_auth_id_digest = + ::Hashing::hash(®istry_id.encode()[..]); + + let non_existent_authorization_id: RegistryAuthorizationIdOf = + generate_authorization_id::(&non_existent_auth_id_digest); + + new_test_ext().execute_with(|| { + assert_err!( + Registries::ensure_authorization_reinstate_origin( + &non_existent_authorization_id, + &delegate + ), + Error::::AuthorizationNotFound + ); + }); +} + +#[test] +fn ensure_authorization_restore_origin_should_fail_if_authorization_is_not_found() { + let creator = ACCOUNT_00; + let delegate = ACCOUNT_01; + let registry = [2u8; 256].to_vec(); + + let registry_digest = ::Hashing::hash(®istry.encode()[..]); + + let id_digest = ::Hashing::hash( + &[®istry_digest.encode()[..], &creator.encode()[..]].concat()[..], + ); + + let registry_id: RegistryIdOf = generate_registry_id::(&id_digest); + + let non_existent_auth_id_digest = + ::Hashing::hash(®istry_id.encode()[..]); + + let non_existent_authorization_id: RegistryAuthorizationIdOf = + generate_authorization_id::(&non_existent_auth_id_digest); + + new_test_ext().execute_with(|| { + assert_err!( + Registries::ensure_authorization_restore_origin( + &non_existent_authorization_id, + &delegate + ), + Error::::AuthorizationNotFound + ); + }); +} + +#[test] +fn ensure_authorization_admin_origin_should_fail_if_authorization_is_not_found() { + let creator = ACCOUNT_00; + let delegate = ACCOUNT_01; + let registry = [2u8; 256].to_vec(); + + let registry_digest = ::Hashing::hash(®istry.encode()[..]); + + let id_digest = ::Hashing::hash( + &[®istry_digest.encode()[..], &creator.encode()[..]].concat()[..], + ); + + let registry_id: RegistryIdOf = generate_registry_id::(&id_digest); + + let non_existent_auth_id_digest = + ::Hashing::hash(®istry_id.encode()[..]); + + let non_existent_authorization_id: RegistryAuthorizationIdOf = + generate_authorization_id::(&non_existent_auth_id_digest); + + new_test_ext().execute_with(|| { + assert_err!( + Registries::ensure_authorization_admin_origin( + &non_existent_authorization_id, + &delegate + ), + Error::::AuthorizationNotFound + ); + }); +} + +#[test] +fn ensure_authorization_delegator_origin_should_fail_if_authorization_is_not_found() { + let creator = ACCOUNT_00; + let delegate = ACCOUNT_01; + let registry = [2u8; 256].to_vec(); + + let registry_digest = ::Hashing::hash(®istry.encode()[..]); + + let id_digest = ::Hashing::hash( + &[®istry_digest.encode()[..], &creator.encode()[..]].concat()[..], + ); + + let registry_id: RegistryIdOf = generate_registry_id::(&id_digest); + + let non_existent_auth_id_digest = + ::Hashing::hash(®istry_id.encode()[..]); + + let non_existent_authorization_id: RegistryAuthorizationIdOf = + generate_authorization_id::(&non_existent_auth_id_digest); + + new_test_ext().execute_with(|| { + assert_err!( + Registries::ensure_authorization_delegator_origin( + &non_existent_authorization_id, + &delegate + ), + Error::::AuthorizationNotFound + ); + }); +} + +#[test] +fn ensure_authorization_admin_remove_origin_should_fail_if_authorization_is_not_found() { + let creator = ACCOUNT_00; + let delegate = ACCOUNT_01; + let registry = [2u8; 256].to_vec(); + + let registry_digest = ::Hashing::hash(®istry.encode()[..]); + + let id_digest = ::Hashing::hash( + &[®istry_digest.encode()[..], &creator.encode()[..]].concat()[..], + ); + + let registry_id: RegistryIdOf = generate_registry_id::(&id_digest); + + let non_existent_auth_id_digest = + ::Hashing::hash(®istry_id.encode()[..]); + + let non_existent_authorization_id: RegistryAuthorizationIdOf = + generate_authorization_id::(&non_existent_auth_id_digest); + + new_test_ext().execute_with(|| { + assert_err!( + Registries::ensure_authorization_admin_remove_origin( + &non_existent_authorization_id, + &delegate + ), + Error::::AuthorizationNotFound + ); + }); +} diff --git a/test-utils/service/src/lib.rs b/test-utils/service/src/lib.rs index 485a00754..470f8364f 100644 --- a/test-utils/service/src/lib.rs +++ b/test-utils/service/src/lib.rs @@ -178,7 +178,7 @@ where interval.tick().await; if full_nodes.iter().all(|(id, service, _, _)| full_predicate(*id, service)) { - break + break; } } };