Skip to content

Commit

Permalink
network-score : Add Test Cases for SpaceMismatch error
Browse files Browse the repository at this point in the history
Fixes: #304 
* network-score: Add Test Cases for SpaceMismatch Error in pallet-network-score

Signed-off-by: Aman Raj <[email protected]>
  • Loading branch information
huamanraj authored Jan 12, 2025
1 parent 1170905 commit 5909de8
Showing 1 changed file with 117 additions and 0 deletions.
117 changes: 117 additions & 0 deletions pallets/network-score/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,3 +915,120 @@ fn reference_identifier_not_found_test() {
<MessageIdentifiers<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::<Test> {
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 =
<Test as frame_system::Config>::Hashing::hash(&[&entry.encode()[..]].concat()[..]);

// Create "main" space:
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);

// Create "mismatch" space:
let mismatch_raw_space = [3u8; 256].to_vec();
let mismatch_space_digest =
<Test as frame_system::Config>::Hashing::hash(&mismatch_raw_space.encode()[..]);
let mismatch_space_id_digest = <Test as frame_system::Config>::Hashing::hash(
&[&mismatch_space_digest.encode()[..], &creator.encode()[..]].concat()[..],
);
let mismatch_space_id: SpaceIdOf = generate_space_id::<Test>(&mismatch_space_id_digest);

// Auth ID for main space
let auth_digest = <Test as frame_system::Config>::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 = <Test as frame_system::Config>::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 = <Test as frame_system::Config>::Hashing::hash(&[50u8; 16].to_vec()[..]);
let orig_rating_id = <MessageIdentifiers<Test>>::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 =
<Test as frame_system::Config>::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::<Test>::SpaceMismatch
);
});
}

0 comments on commit 5909de8

Please sign in to comment.