From 5f824938ad200c44c6f1c9ee7b318fd4481e1709 Mon Sep 17 00:00:00 2001 From: Pratik Chandra Pal Date: Thu, 2 May 2024 18:30:29 +0530 Subject: [PATCH] fixes: #368 Details: Created a new function asset_create_unauthorized_operation to generate an asset with invalid authorization. Handled the error --- pallets/asset/src/tests.rs | 61 +++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/pallets/asset/src/tests.rs b/pallets/asset/src/tests.rs index 72541425..31f1053b 100644 --- a/pallets/asset/src/tests.rs +++ b/pallets/asset/src/tests.rs @@ -5,7 +5,7 @@ use cord_utilities::mock::{mock_origin::DoubleOrigin, SubjectId}; use frame_support::{assert_ok, BoundedVec}; use frame_system::RawOrigin; use pallet_chain_space::{SpaceCodeOf, SpaceIdOf}; -use sp_runtime::{traits::Hash, AccountId32}; +use sp_runtime::{traits::Hash, AccountId32, DispatchError, ModuleError}; use sp_std::prelude::*; /// Generates a space ID from a digest. @@ -495,3 +495,62 @@ fn asset_vc_status_change_should_succeed() { )); }); } + +#[test] +fn asset_create_unauthorized_operation() { + let creator = DID_00; + let author = ACCOUNT_00; + + let raw_space = [2u8; 256].to_vec(); + let space_digest = ::Hashing::hash(&raw_space.encode()[..]); + let space_id_digest = ::Hashing::hash( + &[&space_digest.encode()[..], &creator.encode()[..]].concat()[..], + ); + let space_id: SpaceIdOf = generate_space_id::(&space_id_digest); + + // Generate an invalid authorization_id + let invalid_auth_digest = ::Hashing::hash( + &[&space_id.encode()[..], &"invalid".encode()[..]].concat()[..], + ); + let invalid_authorization_id: Ss58Identifier = + generate_authorization_id::(&invalid_auth_digest); + + let asset_desc = BoundedVec::try_from([72u8; 10].to_vec()).unwrap(); + let asset_tag = BoundedVec::try_from([72u8; 10].to_vec()).unwrap(); + let asset_meta = BoundedVec::try_from([72u8; 10].to_vec()).unwrap(); + let asset_qty = 10; + let asset_value = 10; + let asset_type = AssetTypeOf::MF; + + let entry = AssetInputEntryOf:: { + asset_desc, + asset_qty, + asset_type, + asset_value, + asset_tag, + asset_meta, + }; + + let digest = ::Hashing::hash(&entry.encode()[..]); + + new_test_ext().execute_with(|| { + // Attempt to create an asset without proper authorization + let result = Asset::create( + DoubleOrigin(author.clone(), creator.clone()).into(), + entry, + digest, + invalid_authorization_id, + ); + + // Assert that UnauthorizedOperation error is returned + match result { + Ok(_) => panic!("Expected Err(_). Got Ok(_)"), + Err(DispatchError::Module(ModuleError { message, .. })) => { + // Access the error message + let error_message = message.unwrap(); + assert_eq!(error_message, "AuthorizationNotFound"); + }, + _ => panic!("Unexpected error variant"), + } + }); +}