Skip to content

Commit

Permalink
Fixes: Network-Score: Add tests for 'InvalidRatingValue' #297 (#507)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrswastik-robot authored Sep 20, 2024
1 parent 2be737e commit 9f1fb46
Showing 1 changed file with 148 additions and 0 deletions.
148 changes: 148 additions & 0 deletions pallets/network-score/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,154 @@ fn check_successful_rating_creation() {
});
}

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

let invalid_message_id = BoundedVec::try_from([0u8; 0].to_vec()).unwrap(); // Invalid message ID (empty)
let entity_id = BoundedVec::try_from([73u8; 10].to_vec()).unwrap();
let provider_id = BoundedVec::try_from([74u8; 10].to_vec()).unwrap();
let invalid_entry = RatingInputEntryOf::<Test> {
entity_id,
provider_id,
total_encoded_rating: 0u64, // Invalid rating (0 value)
count_of_txn: 0u64, // Invalid transaction count (0)
rating_type: RatingTypeOf::Overall,
provider_did: creator.clone(),
};
let entry_digest =
<Test as frame_system::Config>::Hashing::hash(&[&invalid_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()[..], &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);

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

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

// Try registering rating with invalid data
assert_err!(
Score::register_rating(
DoubleOrigin(author.clone(), creator.clone()).into(),
invalid_entry.clone(),
entry_digest,
invalid_message_id.clone(),
authorization_id.clone()
),
Error::<Test>::InvalidRatingValue
);
});
}

#[test]
fn revise_rating_with_invalid_values_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();
let entry = RatingInputEntryOf::<Test> {
entity_id,
provider_id,
total_encoded_rating: 250u64, // Initially valid rating
count_of_txn: 7u64, // Initially valid transaction count
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()[..], &creator.encode()[..]].concat()[..],
);
let authorization_id: AuthorizationIdOf =
Ss58Identifier::create_identifier(&auth_digest.encode()[..], IdentifierType::Authorization)
.unwrap();

let id_digest = <Test as frame_system::Config>::Hashing::hash(
&[
&entry_digest.encode()[..],
&entry.entity_id.encode()[..],
&message_id.encode()[..],
&space_id.encode()[..],
&creator.clone().encode()[..],
]
.concat()[..],
);

let rating_identifier =
Ss58Identifier::create_identifier(&(id_digest).encode()[..], IdentifierType::Rating)
.unwrap();

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

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

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

assert_ok!(Score::register_rating(
DoubleOrigin(author.clone(), creator.clone()).into(),
entry.clone(),
entry_digest,
message_id.clone(),
authorization_id.clone(),
));

// Modify the entry for revision with invalid values
let mut revised_entry = entry.clone();
revised_entry.total_encoded_rating = 0u64; // Invalid rating
revised_entry.count_of_txn = 0u64; // Invalid transaction count

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

assert_err!(
Score::revise_rating(
DoubleOrigin(author.clone(), creator.clone()).into(),
revised_entry.clone(),
revised_entry_digest,
message_id.clone(),
rating_identifier.clone(),
authorization_id.clone(),
),
Error::<Test>::InvalidRatingValue
);
});
}

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

0 comments on commit 9f1fb46

Please sign in to comment.