Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes: Network-Score: Add tests for 'InvalidRatingValue' #297 #507

Merged
merged 3 commits into from
Sep 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

InvalidRatingValue is also used in revise_rating. Please add that tests for that also.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @vatsa287 , is it revise_rating_with_entry_entity or revise_rating_with_existing_rating ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No InvalidRatingValue is being returned from two extrinsics. One being register which you already done. Now you have to add it for revise as well. Look at lib.rs file, where all this is being returned from.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @vatsa287 for helping me out, I read lib.rs file and have added a test function revise_rating_with_invalid_values_should_fail in the latest commit. Please review and let me know if further changes are required

);
});
}

#[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