Skip to content

Commit

Permalink
Asset: added test cases for asset and VC over issuance (#465)
Browse files Browse the repository at this point in the history
  • Loading branch information
atreyasuraj authored Jun 13, 2024
1 parent ad5109e commit 477aa69
Showing 1 changed file with 183 additions and 0 deletions.
183 changes: 183 additions & 0 deletions pallets/asset/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ fn asset_vc_status_change_should_succeed() {
));
});
}

#[test]
fn changing_status_of_asset_instance_with_same_status_should_fail() {
// Set up test parameters
Expand Down Expand Up @@ -874,3 +875,185 @@ fn changing_status_of_vc_asset_with_same_status_should_fail() {
);
});
}

#[test]
fn asset_over_issuance_should_not_succeed() {
let creator = DID_00;
let author = ACCOUNT_00;
let capacity = 5u64;

let raw_space = [2u8; 256].to_vec();
let space_digest = <Test as frame_system::Config>::Hashing::hash(&raw_space.encode()[..]);
let space_id_digest = <Test as frame_system::Config>::Hashing::hash(
&[&space_digest.encode()[..], &creator.encode()[..]].concat()[..],
);
let space_id: SpaceIdOf = generate_space_id::<Test>(&space_id_digest);

let auth_digest = <Test as frame_system::Config>::Hashing::hash(
&[&space_id.encode()[..], &creator.encode()[..]].concat()[..],
);

let authorization_id: Ss58Identifier = generate_authorization_id::<Test>(&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::<Test> {
asset_desc,
asset_qty,
asset_type,
asset_value,
asset_tag,
asset_meta,
};

let digest = <Test as frame_system::Config>::Hashing::hash(&[&entry.encode()[..]].concat()[..]);

let issue_id_digest = <Test as frame_system::Config>::Hashing::hash(
&[&digest.encode()[..], &space_id.encode()[..], &creator.encode()[..]].concat()[..],
);

let asset_id: Ss58Identifier = generate_asset_id::<Test>(&issue_id_digest);

let issue_entry = AssetIssuanceEntryOf::<Test> {
asset_id,
asset_owner: creator.clone(),
asset_issuance_qty: Some(20),
};

let issue_entry_digest =
<Test as frame_system::Config>::Hashing::hash(&[&issue_entry.encode()[..]].concat()[..]);

new_test_ext().execute_with(|| {
assert_ok!(Space::create(
DoubleOrigin(author.clone(), creator.clone()).into(),
space_digest,
));

assert_ok!(Space::approve(RawOrigin::Root.into(), space_id, capacity));

assert_ok!(Asset::create(
DoubleOrigin(author.clone(), creator.clone()).into(),
entry,
digest,
authorization_id.clone()
));

assert_err!(
Asset::issue(
DoubleOrigin(author.clone(), creator.clone()).into(),
issue_entry.clone(),
issue_entry_digest,
authorization_id
),
Error::<Test>::OverIssuanceLimit
);
});
}

#[test]
fn asset_over_issuance_vc_status_change_should_not_succeed() {
let creator = DID_00;
let author = ACCOUNT_00;

let capacity = 5u64;

let raw_space = [2u8; 256].to_vec();
let space_digest = <Test as frame_system::Config>::Hashing::hash(&raw_space.encode()[..]);
let space_id_digest = <Test as frame_system::Config>::Hashing::hash(
&[&space_digest.encode()[..], &creator.encode()[..]].concat()[..],
);
let space_id: SpaceIdOf = generate_space_id::<Test>(&space_id_digest);

let auth_digest = <Test as frame_system::Config>::Hashing::hash(
&[&space_id.encode()[..], &creator.encode()[..]].concat()[..],
);

let authorization_id: Ss58Identifier = generate_authorization_id::<Test>(&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::<Test> {
asset_desc,
asset_qty,
asset_type,
asset_value,
asset_tag,
asset_meta,
};

let digest = <Test as frame_system::Config>::Hashing::hash(&[&entry.encode()[..]].concat()[..]);

let issue_id_digest = <Test as frame_system::Config>::Hashing::hash(
&[&digest.encode()[..], &space_id.encode()[..], &creator.encode()[..]].concat()[..],
);

let asset_id: Ss58Identifier = generate_asset_id::<Test>(&issue_id_digest);

let issue_entry = AssetIssuanceEntryOf::<Test> {
asset_id: asset_id.clone(),
asset_owner: creator.clone(),
asset_issuance_qty: Some(30),
};

let issue_entry_digest =
<Test as frame_system::Config>::Hashing::hash(&[&issue_entry.encode()[..]].concat()[..]);

let instance_id_digest = <Test as frame_system::Config>::Hashing::hash(
&[
&asset_id.encode()[..],
&creator.encode()[..],
&space_id.encode()[..],
&creator.encode()[..],
&issue_entry_digest.encode()[..],
]
.concat()[..],
);

let instance_id = generate_asset_instance_id::<Test>(&instance_id_digest);

new_test_ext().execute_with(|| {
assert_ok!(Space::create(
DoubleOrigin(author.clone(), creator.clone()).into(),
space_digest,
));

assert_ok!(Space::approve(RawOrigin::Root.into(), space_id, capacity));

assert_ok!(Asset::vc_create(
DoubleOrigin(author.clone(), creator.clone()).into(),
asset_qty,
digest,
authorization_id.clone()
));

assert_err!(
Asset::vc_issue(
DoubleOrigin(author.clone(), creator.clone()).into(),
issue_entry.clone(),
issue_entry_digest,
authorization_id
),
Error::<Test>::OverIssuanceLimit
);

assert_err!(
Asset::vc_status_change(
DoubleOrigin(author.clone(), creator.clone()).into(),
asset_id.clone(),
Some(instance_id.clone()),
AssetStatusOf::INACTIVE
),
Error::<Test>::AssetInstanceNotFound
);
});
}

0 comments on commit 477aa69

Please sign in to comment.