From 5909de821aed082b7b8e39b55e454310bf2cf612 Mon Sep 17 00:00:00 2001 From: Aman Raj <113578582+huamanraj@users.noreply.github.com> Date: Sun, 12 Jan 2025 13:14:58 +0530 Subject: [PATCH] network-score : Add Test Cases for SpaceMismatch error Fixes: #304 * network-score: Add Test Cases for SpaceMismatch Error in pallet-network-score Signed-off-by: Aman Raj <113578582+huamanraj@users.noreply.github.com> --- pallets/network-score/src/tests.rs | 117 +++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) diff --git a/pallets/network-score/src/tests.rs b/pallets/network-score/src/tests.rs index f7a9e738b..2c26ba9d8 100644 --- a/pallets/network-score/src/tests.rs +++ b/pallets/network-score/src/tests.rs @@ -915,3 +915,120 @@ fn reference_identifier_not_found_test() { >::remove(message_id_revise.clone(), creator.clone()); }); } + +#[test] +fn revise_rating_with_space_mismatch_should_fail() { + let creator = DID_00.clone(); + let author = ACCOUNT_00.clone(); + + let message_id = BoundedVec::try_from([72u8; 10].to_vec()).unwrap(); + let entity_id = BoundedVec::try_from([73u8; 10].to_vec()).unwrap(); + let provider_id = BoundedVec::try_from([74u8; 10].to_vec()).unwrap(); + + // Valid rating entry + let entry = RatingInputEntryOf:: { + entity_id: entity_id.clone(), + provider_id: provider_id.clone(), + total_encoded_rating: 250u64, + count_of_txn: 7u64, + rating_type: RatingTypeOf::Overall, + provider_did: creator.clone(), + }; + + let entry_digest = + ::Hashing::hash(&[&entry.encode()[..]].concat()[..]); + + // Create "main" space: + 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); + + // Create "mismatch" space: + let mismatch_raw_space = [3u8; 256].to_vec(); + let mismatch_space_digest = + ::Hashing::hash(&mismatch_raw_space.encode()[..]); + let mismatch_space_id_digest = ::Hashing::hash( + &[&mismatch_space_digest.encode()[..], &creator.encode()[..]].concat()[..], + ); + let mismatch_space_id: SpaceIdOf = generate_space_id::(&mismatch_space_id_digest); + + // Auth ID for main space + let auth_digest = ::Hashing::hash( + &[&space_id.encode()[..], &creator.encode()[..], &creator.encode()[..]].concat()[..], + ); + let authorization_id: AuthorizationIdOf = + Ss58Identifier::create_identifier(&auth_digest.encode()[..], IdentifierType::Authorization) + .unwrap(); + + // Auth ID for mismatch space + let mismatch_auth_digest = ::Hashing::hash( + &[&mismatch_space_id.encode()[..], &creator.encode()[..], &creator.encode()[..]].concat()[..], + ); + let mismatch_authorization_id: AuthorizationIdOf = Ss58Identifier::create_identifier( + &mismatch_auth_digest.encode()[..], + IdentifierType::Authorization, + ) + .unwrap(); + + new_test_ext().execute_with(|| { + System::set_block_number(1); + + // Create & approve main space + assert_ok!(Space::create( + DoubleOrigin(author.clone(), creator.clone()).into(), + space_digest + )); + assert_ok!(Space::approve(RawOrigin::Root.into(), space_id, 3u64)); + + // Register a 'credit' rating in main space + assert_ok!(Score::register_rating( + DoubleOrigin(author.clone(), creator.clone()).into(), + entry.clone(), + entry_digest, + message_id.clone(), + authorization_id.clone(), + )); + + // Create & approve mismatch space + assert_ok!(Space::create( + DoubleOrigin(author.clone(), creator.clone()).into(), + mismatch_space_digest + )); + assert_ok!(Space::approve(RawOrigin::Root.into(), mismatch_space_id, 3u64)); + + // Revoke the original rating to produce a 'debit' entry + let revoke_msg_id = BoundedVec::try_from([80u8; 10].to_vec()).unwrap(); + let revoke_digest = ::Hashing::hash(&[50u8; 16].to_vec()[..]); + let orig_rating_id = >::get(&message_id, &creator).unwrap(); + + assert_ok!(Score::revoke_rating( + DoubleOrigin(author.clone(), creator.clone()).into(), + orig_rating_id.clone(), + revoke_msg_id.clone(), + revoke_digest, + authorization_id.clone(), + )); + + // Attempt to revise using the mismatch space + let mismatch_msg_id = BoundedVec::try_from([88u8; 10].to_vec()).unwrap(); + let mismatch_digest = + ::Hashing::hash(&[90u8; 16].to_vec()[..]); + + // This call should fail with SpaceMismatch, because the rating was made in 'space_id', + // but we pass 'mismatch_authorization_id' that points to 'mismatch_space_id'. + assert_err!( + Score::revise_rating( + DoubleOrigin(author.clone(), creator.clone()).into(), + entry.clone(), + mismatch_digest, + mismatch_msg_id, + orig_rating_id.clone(), + mismatch_authorization_id, + ), + Error::::SpaceMismatch + ); + }); +}