Skip to content
Snippets Groups Projects
Unverified Commit fb1916de authored by bgallois's avatar bgallois
Browse files

wip update test with new mock

parent e7bfc8ca
No related branches found
No related tags found
No related merge requests found
Pipeline #18988 passed
......@@ -21,12 +21,12 @@ use frame_support::{
traits::{Everything, GenesisBuild, OnFinalize, OnInitialize},
};
use frame_system as system;
use sp_core::H256;
use sp_core::{Pair, H256};
use sp_keystore::{testing::KeyStore, KeystoreExt};
use sp_runtime::{
testing::{Header, UintAuthorityId},
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
MultiSignature,
MultiSignature, MultiSigner,
};
use std::sync::Arc;
......@@ -36,10 +36,9 @@ pub type Signature = MultiSignature;
pub type AccountPublic = <Signature as Verify>::Signer;
pub type AccountId = <AccountPublic as IdentifyAccount>::AccountId;
type AccountIdOf<Test> = <Test as frame_system::Config>::AccountId;
fn account(id: u8) -> AccountIdOf<Test> {
[id; 32].into()
fn account(id: u8) -> AccountId {
let pair = sp_core::sr25519::Pair::from_seed(&[id; 32]);
MultiSigner::Sr25519(pair.public()).into_account()
}
// Configure a mock runtime to test the pallet.
......
......@@ -21,9 +21,31 @@ use crate::{
};
use codec::Encode;
use frame_support::{assert_noop, assert_ok};
use sp_runtime::testing::TestSignature;
use sp_core::sr25519::Pair as KeyPair;
use sp_core::Pair;
use sp_runtime::{traits::IdentifyAccount, MultiSignature, MultiSigner};
type IdtyVal = IdtyValue<u64, u64, ()>;
type IdtyVal = IdtyValue<u64, AccountId, ()>;
// Store the account id and the key pair to sign payload
struct Account {
id: AccountId,
signer: KeyPair,
}
// Create an Account given a u8
fn account(id: u8) -> Account {
let pair = sp_core::sr25519::Pair::from_seed(&[id; 32]);
Account {
id: MultiSigner::Sr25519(pair.public()).into_account(),
signer: pair,
}
}
// Sign a payload using a key pair
fn test_signature(key_pair: KeyPair, payload: Vec<u8>) -> MultiSignature {
MultiSignature::Sr25519(key_pair.sign(&payload))
}
fn alice() -> GenesisIdty<Test> {
GenesisIdty {
......@@ -33,7 +55,7 @@ fn alice() -> GenesisIdty<Test> {
data: (),
next_creatable_identity_on: 0,
old_owner_key: None,
owner_key: 1,
owner_key: account(1).id,
removable_on: 0,
status: crate::IdtyStatus::Validated,
},
......@@ -48,7 +70,7 @@ fn bob() -> GenesisIdty<Test> {
data: (),
next_creatable_identity_on: 0,
old_owner_key: None,
owner_key: 2,
owner_key: account(2).id,
removable_on: 0,
status: crate::IdtyStatus::Validated,
},
......@@ -75,11 +97,14 @@ fn test_create_identity_ok() {
run_to_block(1);
// Alice should be able to create an identity
assert_ok!(Identity::create_identity(RuntimeOrigin::signed(1), 2));
assert_ok!(Identity::create_identity(
RuntimeOrigin::signed(account(1).id),
account(2).id
));
System::assert_has_event(RuntimeEvent::Identity(crate::Event::IdtyCreated {
idty_index: 2,
owner_key: 2,
owner_key: account(2).id,
}));
});
}
......@@ -94,7 +119,10 @@ fn test_create_identity_but_not_confirm_it() {
run_to_block(1);
// Alice should be able to create an identity
assert_ok!(Identity::create_identity(RuntimeOrigin::signed(1), 2));
assert_ok!(Identity::create_identity(
RuntimeOrigin::signed(account(1).id),
account(2).id
));
// The identity shoud expire in blocs #3
run_to_block(3);
......@@ -105,11 +133,14 @@ fn test_create_identity_but_not_confirm_it() {
// We shoud be able to recreate the identity
run_to_block(4);
assert_ok!(Identity::create_identity(RuntimeOrigin::signed(1), 2));
assert_ok!(Identity::create_identity(
RuntimeOrigin::signed(account(1).id),
account(2).id
));
System::assert_has_event(RuntimeEvent::Identity(crate::Event::IdtyCreated {
idty_index: 3,
owner_key: 2,
owner_key: account(2).id,
}));
});
}
......@@ -124,11 +155,14 @@ fn test_idty_creation_period() {
run_to_block(1);
// Alice should be able to create an identity
assert_ok!(Identity::create_identity(RuntimeOrigin::signed(1), 2));
assert_ok!(Identity::create_identity(
RuntimeOrigin::signed(account(1).id),
account(2).id
));
System::assert_has_event(RuntimeEvent::Identity(crate::Event::IdtyCreated {
idty_index: 2,
owner_key: 2,
owner_key: account(2).id,
}));
assert_eq!(Identity::identity(1).unwrap().next_creatable_identity_on, 4);
......@@ -136,21 +170,25 @@ fn test_idty_creation_period() {
// Alice cannot create a new identity before block #4
run_to_block(2);
assert_eq!(
Identity::create_identity(RuntimeOrigin::signed(1), 3),
Identity::create_identity(RuntimeOrigin::signed(account(1).id), account(3).id),
Err(Error::<Test>::NotRespectIdtyCreationPeriod.into())
);
// Alice should be able to create a second identity after block #4
run_to_block(4);
assert_ok!(Identity::create_identity(RuntimeOrigin::signed(1), 3));
assert_ok!(Identity::create_identity(
RuntimeOrigin::signed(account(1).id),
account(3).id
));
System::assert_has_event(RuntimeEvent::Identity(crate::Event::IdtyCreated {
idty_index: 3,
owner_key: 3,
owner_key: account(3).id,
}));
});
}
//
#[test]
fn test_change_owner_key() {
new_test_ext(IdentityConfig {
......@@ -158,7 +196,7 @@ fn test_change_owner_key() {
})
.execute_with(|| {
let genesis_hash = System::block_hash(0);
let old_owner_key = 1u64;
let old_owner_key = account(1).id;
let mut new_key_payload = NewOwnerKeyPayload {
genesis_hash: &genesis_hash,
idty_index: 1u64,
......@@ -169,25 +207,29 @@ fn test_change_owner_key() {
run_to_block(1);
// Verify genesis data
assert_eq!(System::sufficients(&1), 1);
assert_eq!(System::sufficients(&10), 0);
assert_eq!(System::sufficients(&account(1).id), 1);
assert_eq!(System::sufficients(&account(10).id), 0);
// Caller should have an associated identity
assert_noop!(
Identity::change_owner_key(
RuntimeOrigin::signed(42),
10,
TestSignature(10, (NEW_OWNER_KEY_PAYLOAD_PREFIX, new_key_payload).encode())
RuntimeOrigin::signed(account(42).id),
account(10).id,
test_signature(
account(10).signer,
(NEW_OWNER_KEY_PAYLOAD_PREFIX, new_key_payload.clone()).encode()
)
),
Error::<Test>::IdtyIndexNotFound
);
// Payload must be signed by the new key
assert_noop!(
Identity::change_owner_key(
RuntimeOrigin::signed(1),
10,
TestSignature(42, (NEW_OWNER_KEY_PAYLOAD_PREFIX, new_key_payload).encode())
RuntimeOrigin::signed(account(1).id),
account(10).id,
test_signature(
account(42).signer,
(NEW_OWNER_KEY_PAYLOAD_PREFIX, new_key_payload.clone()).encode()
)
),
Error::<Test>::InvalidNewOwnerKeySig
);
......@@ -195,9 +237,9 @@ fn test_change_owner_key() {
// Payload must be prefixed
assert_noop!(
Identity::change_owner_key(
RuntimeOrigin::signed(1),
10,
TestSignature(10, new_key_payload.encode())
RuntimeOrigin::signed(account(1).id),
account(10).id,
test_signature(account(10).signer, new_key_payload.clone().encode())
),
Error::<Test>::InvalidNewOwnerKeySig
);
......@@ -205,46 +247,53 @@ fn test_change_owner_key() {
// New owner key should not be used by another identity
assert_noop!(
Identity::change_owner_key(
RuntimeOrigin::signed(1),
2,
TestSignature(2, (NEW_OWNER_KEY_PAYLOAD_PREFIX, new_key_payload).encode())
RuntimeOrigin::signed(account(1).id),
account(2).id,
test_signature(
account(2).signer,
(NEW_OWNER_KEY_PAYLOAD_PREFIX, new_key_payload.clone()).encode()
)
),
Error::<Test>::OwnerKeyAlreadyUsed
);
// Alice can change her owner key
assert_ok!(Identity::change_owner_key(
RuntimeOrigin::signed(1),
10,
TestSignature(10, (NEW_OWNER_KEY_PAYLOAD_PREFIX, new_key_payload).encode())
RuntimeOrigin::signed(account(1).id),
account(10).id,
test_signature(
account(10).signer,
(NEW_OWNER_KEY_PAYLOAD_PREFIX, new_key_payload.clone()).encode()
)
));
assert_eq!(
Identity::identity(1),
Some(IdtyVal {
data: (),
next_creatable_identity_on: 0,
old_owner_key: Some((1, 1)),
owner_key: 10,
old_owner_key: Some((account(1).id, 1)),
owner_key: account(10).id,
removable_on: 0,
status: crate::IdtyStatus::Validated,
})
);
// Alice still sufficient
assert_eq!(System::sufficients(&1), 1);
assert_eq!(System::sufficients(&account(1).id), 1);
// New owner key should become a sufficient account
assert_eq!(System::sufficients(&10), 1);
assert_eq!(System::sufficients(&account(10).id), 1);
run_to_block(2);
//
// Alice can't re-change her owner key too early
new_key_payload.old_owner_key = &10;
let old = account(10).id;
new_key_payload.old_owner_key = &old;
assert_noop!(
Identity::change_owner_key(
RuntimeOrigin::signed(10),
100,
TestSignature(
100,
(NEW_OWNER_KEY_PAYLOAD_PREFIX, new_key_payload).encode()
RuntimeOrigin::signed(account(10).id),
account(100).id,
test_signature(
account(100).signer,
(NEW_OWNER_KEY_PAYLOAD_PREFIX, new_key_payload.clone()).encode()
)
),
Error::<Test>::OwnerKeyAlreadyRecentlyChanged
......@@ -253,27 +302,27 @@ fn test_change_owner_key() {
// Alice can re-change her owner key after ChangeOwnerKeyPeriod blocs
run_to_block(2 + <Test as crate::Config>::ChangeOwnerKeyPeriod::get());
assert_ok!(Identity::change_owner_key(
RuntimeOrigin::signed(10),
100,
TestSignature(
100,
(NEW_OWNER_KEY_PAYLOAD_PREFIX, new_key_payload).encode()
RuntimeOrigin::signed(account(10).id),
account(100).id,
test_signature(
account(100).signer,
(NEW_OWNER_KEY_PAYLOAD_PREFIX, new_key_payload.clone()).encode()
)
));
// Old old owner key should not be sufficient anymore
assert_eq!(System::sufficients(&1), 0);
assert_eq!(System::sufficients(&account(1).id), 0);
// Old owner key should still sufficient
assert_eq!(System::sufficients(&10), 1);
assert_eq!(System::sufficients(&account(10).id), 1);
// New owner key should become a sufficient account
assert_eq!(System::sufficients(&100), 1);
assert_eq!(System::sufficients(&account(100).id), 1);
// Revoke identity 1
assert_ok!(Identity::revoke_identity(
RuntimeOrigin::signed(42),
RuntimeOrigin::signed(account(42).id),
1,
100,
TestSignature(
100,
account(100).id,
test_signature(
account(100).signer,
(
REVOCATION_PAYLOAD_PREFIX,
RevocationPayload {
......@@ -285,9 +334,9 @@ fn test_change_owner_key() {
)
));
// Old owner key should not be sufficient anymore
assert_eq!(System::sufficients(&10), 0);
assert_eq!(System::sufficients(&account(10).id), 0);
// Last owner key should not be sufficient anymore
assert_eq!(System::sufficients(&100), 0);
assert_eq!(System::sufficients(&account(100).id), 0);
});
}
......@@ -301,7 +350,7 @@ fn test_idty_revocation_with_old_key() {
let new_key_payload = NewOwnerKeyPayload {
genesis_hash: &genesis_hash,
idty_index: 1u64,
old_owner_key: &1u64,
old_owner_key: &account(1).id,
};
let revocation_payload = RevocationPayload {
idty_index: 1u64,
......@@ -313,22 +362,28 @@ fn test_idty_revocation_with_old_key() {
// Change alice owner key
assert_ok!(Identity::change_owner_key(
RuntimeOrigin::signed(1),
10,
TestSignature(10, (NEW_OWNER_KEY_PAYLOAD_PREFIX, new_key_payload).encode())
RuntimeOrigin::signed(account(1).id),
account(10).id,
test_signature(
account(10).signer,
(NEW_OWNER_KEY_PAYLOAD_PREFIX, new_key_payload).encode()
)
));
assert!(Identity::identity(&1).is_some());
let idty_val = Identity::identity(&1).unwrap();
assert_eq!(idty_val.owner_key, 10);
assert_eq!(idty_val.old_owner_key, Some((1, 1)));
assert_eq!(idty_val.owner_key, account(10).id);
assert_eq!(idty_val.old_owner_key, Some((account(1).id, 1)));
// We should be able to revoke Alice identity with old key
run_to_block(2);
assert_ok!(Identity::revoke_identity(
RuntimeOrigin::signed(42),
1,
RuntimeOrigin::signed(account(42).id),
1,
TestSignature(1, (REVOCATION_PAYLOAD_PREFIX, revocation_payload).encode())
account(1).id,
test_signature(
account(1).signer,
(REVOCATION_PAYLOAD_PREFIX, revocation_payload).encode()
)
));
//run_to_block(2 + <Test as crate::Config>::ChangeOwnerKeyPeriod::get());
......@@ -345,7 +400,7 @@ fn test_idty_revocation_with_old_key_after_old_key_expiration() {
let new_key_payload = NewOwnerKeyPayload {
genesis_hash: &genesis_hash,
idty_index: 1u64,
old_owner_key: &1u64,
old_owner_key: &account(1).id,
};
let revocation_payload = RevocationPayload {
idty_index: 1u64,
......@@ -357,23 +412,29 @@ fn test_idty_revocation_with_old_key_after_old_key_expiration() {
// Change alice owner key
assert_ok!(Identity::change_owner_key(
RuntimeOrigin::signed(1),
10,
TestSignature(10, (NEW_OWNER_KEY_PAYLOAD_PREFIX, new_key_payload).encode())
RuntimeOrigin::signed(account(1).id),
account(10).id,
test_signature(
account(10).signer,
(NEW_OWNER_KEY_PAYLOAD_PREFIX, new_key_payload).encode()
)
));
assert!(Identity::identity(&1).is_some());
let idty_val = Identity::identity(&1).unwrap();
assert_eq!(idty_val.owner_key, 10);
assert_eq!(idty_val.old_owner_key, Some((1, 1)));
assert_eq!(idty_val.owner_key, account(10).id);
assert_eq!(idty_val.old_owner_key, Some((account(1).id, 1)));
// We should not be able to revoke Alice identity with old key after ChangeOwnerKeyPeriod
run_to_block(2 + <Test as crate::Config>::ChangeOwnerKeyPeriod::get());
assert_noop!(
Identity::revoke_identity(
RuntimeOrigin::signed(42),
1,
RuntimeOrigin::signed(account(42).id),
1,
TestSignature(1, (REVOCATION_PAYLOAD_PREFIX, revocation_payload).encode())
account(1).id,
test_signature(
account(1).signer,
(REVOCATION_PAYLOAD_PREFIX, revocation_payload).encode()
)
),
Error::<Test>::InvalidRevocationKey
);
......@@ -397,10 +458,13 @@ fn test_idty_revocation() {
// Payload must be signed by the right identity
assert_eq!(
Identity::revoke_identity(
RuntimeOrigin::signed(1),
RuntimeOrigin::signed(account(1).id),
1,
42,
TestSignature(42, (REVOCATION_PAYLOAD_PREFIX, revocation_payload).encode())
account(42).id,
test_signature(
account(42).signer,
(REVOCATION_PAYLOAD_PREFIX, revocation_payload).encode()
)
),
Err(Error::<Test>::InvalidRevocationKey.into())
);
......@@ -408,24 +472,27 @@ fn test_idty_revocation() {
// Payload must be prefixed
assert_eq!(
Identity::revoke_identity(
RuntimeOrigin::signed(1),
1,
RuntimeOrigin::signed(account(1).id),
1,
TestSignature(1, revocation_payload.encode())
account(1).id,
test_signature(account(1).signer, revocation_payload.encode())
),
Err(Error::<Test>::InvalidRevocationSig.into())
);
// Anyone can submit a revocation payload
assert_ok!(Identity::revoke_identity(
RuntimeOrigin::signed(42),
RuntimeOrigin::signed(account(42).id),
1,
1,
TestSignature(1, (REVOCATION_PAYLOAD_PREFIX, revocation_payload).encode())
account(1).id,
test_signature(
account(1).signer,
(REVOCATION_PAYLOAD_PREFIX, revocation_payload).encode()
)
));
System::assert_has_event(RuntimeEvent::System(frame_system::Event::KilledAccount {
account: 1,
account: account(1).id,
}));
System::assert_has_event(RuntimeEvent::Identity(crate::Event::IdtyRemoved {
idty_index: 1,
......@@ -436,10 +503,13 @@ fn test_idty_revocation() {
// The identity no longer exists
assert_eq!(
Identity::revoke_identity(
RuntimeOrigin::signed(1),
RuntimeOrigin::signed(account(1).id),
1,
1,
TestSignature(1, (REVOCATION_PAYLOAD_PREFIX, revocation_payload).encode())
account(1).id,
test_signature(
account(1).signer,
(REVOCATION_PAYLOAD_PREFIX, revocation_payload).encode()
)
),
Err(Error::<Test>::IdtyNotFound.into())
);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment