Skip to content

Commit

Permalink
test: pallet/network-score: Message Id Already Exist (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeel991 authored Feb 21, 2024
1 parent 3d6388d commit fbf6d19
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,3 @@ release.json
rls*.log
runtime/wasm/target/
target/

70 changes: 69 additions & 1 deletion pallets/network-score/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use super::*;
use crate::mock::*;
use codec::Encode;
use cord_utilities::mock::{mock_origin::DoubleOrigin, SubjectId};
use frame_support::{assert_ok, BoundedVec};
use frame_support::{assert_err, assert_ok, BoundedVec};
use frame_system::RawOrigin;
use pallet_chain_space::SpaceCodeOf;
use sp_runtime::{traits::Hash, AccountId32};
Expand Down Expand Up @@ -91,3 +91,71 @@ fn check_successful_rating_creation() {
));
});
}

#[test]
fn check_duplicate_message_id() {
let creator = DID_00.clone();
let author = ACCOUNT_00.clone();

let message_id = BoundedVec::try_from([72u8; 10].to_vec()).unwrap();
let entity_uid = BoundedVec::try_from([73u8; 10].to_vec()).unwrap();
let provider_uid = BoundedVec::try_from([74u8; 10].to_vec()).unwrap();
let entry = RatingInputEntryOf::<Test> {
entity_uid,
provider_uid,
total_encoded_rating: 250u64,
count_of_txn: 7u64,
entity_type: EntityTypeOf::Logistic,
rating_type: RatingTypeOf::Overall,
provider_did: creator.clone(),
};
let entry_digest =
<Test as frame_system::Config>::Hashing::hash(&[&entry.encode()[..]].concat()[..]);

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: AuthorizationIdOf =
Ss58Identifier::create_identifier(&auth_digest.encode()[..], IdentifierType::Authorization)
.unwrap();

new_test_ext().execute_with(|| {
System::set_block_number(1);
// Author Transaction

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

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

// Register the rating entry once
assert_ok!(Score::register_rating(
DoubleOrigin(author.clone(), creator.clone()).into(),
entry.clone(),
entry_digest,
message_id.clone(),
authorization_id.clone(),
));

//error as if found the same message id
assert_err!(
Score::register_rating(
DoubleOrigin(author.clone(), creator.clone()).into(),
entry,
entry_digest,
message_id,
authorization_id,
),
Error::<Test>::MessageIdAlreadyExists
);
});
}

0 comments on commit fbf6d19

Please sign in to comment.