generated from substrate-developer-hub/substrate-pallet-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.rs
65 lines (55 loc) · 2.28 KB
/
tests.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use crate::{Error, mock::*};
use frame_support::{assert_ok, assert_noop};
#[test]
fn should_not_throw_errors() {
new_test_ext().execute_with(|| {
// Dispatch a signed extrinsic
// create identity "prasad" for accountId 1
let identity = "prasad".as_bytes().to_vec();
assert_ok!(IdentityModule::create_identity(Origin::signed(1), "prasad".as_bytes().to_vec() ));
// Read pallet storage and assert an expected result.
assert_eq!(IdentityModule::get_identity(&identity), Some(1));
let attribute_key = "name".as_bytes().to_vec();
let attribute_value = "prasad kumkar".as_bytes().to_vec();
// add attribute name => prasad kumkar
assert_ok!(IdentityModule::add_attribute(Origin::signed(1), "prasad".as_bytes().to_vec(), "name".as_bytes().to_vec(), "prasad kumkar".as_bytes().to_vec()));
// check attribute value
assert_eq!(IdentityModule::get_attribute((&identity, &attribute_key)), attribute_value);
// Remmove attribute
assert_ok!(IdentityModule::remove_attribute(Origin::signed(1), "prasad".as_bytes().to_vec(), "name".as_bytes().to_vec()));
// after removing, attribute value should be blank
assert_eq!(IdentityModule::get_attribute((identity, attribute_key)), "".as_bytes().to_vec());
});
}
#[test]
fn check_for_errors() {
new_test_ext().execute_with(|| {
// Dispatch a signed extrinsic.
let identity = "prasad".as_bytes().to_vec();
assert_ok!(IdentityModule::create_identity(Origin::signed(1), "prasad".as_bytes().to_vec() ));
// Read pallet storage and assert an expected result.
assert_eq!(IdentityModule::get_identity(&identity), Some(1));
// Should throw error as identity "prasad" is already claimed
let identity = "prasad".as_bytes().to_vec();
assert_noop!(
IdentityModule::create_identity(
Origin::signed(2),
"prasad".as_bytes().to_vec()
),
Error::<Test>::IdentityAlreadyClaimed
);
// add_attribute signed by different identity (2)
// should throw NotAuthrized error
assert_noop!(
IdentityModule::add_attribute(
Origin::signed(2),
"prasad".as_bytes().to_vec(),
"name".as_bytes().to_vec(),
"prasad kumkar".as_bytes().to_vec()
),
Error::<Test>::NotAuthorized
);
// Attribute value should be blank
assert_eq!(IdentityModule::get_attribute((identity, "name".as_bytes().to_vec())), "".as_bytes().to_vec());
});
}