From 45e6eb441faeb01d072037c9a272dfb25049c381 Mon Sep 17 00:00:00 2001
From: Hugo Trentesaux <hugo@trentesaux.fr>
Date: Mon, 9 Oct 2023 12:11:13 +0200
Subject: [PATCH] add change owner key test

---
 runtime/gdev/tests/integration_tests.rs | 38 +++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/runtime/gdev/tests/integration_tests.rs b/runtime/gdev/tests/integration_tests.rs
index dc1320930..c12719892 100644
--- a/runtime/gdev/tests/integration_tests.rs
+++ b/runtime/gdev/tests/integration_tests.rs
@@ -21,6 +21,7 @@ use frame_support::traits::{Get, PalletInfo, StorageInfo, StorageInfoTrait};
 use frame_support::{assert_noop, assert_ok};
 use frame_support::{StorageHasher, Twox128};
 use gdev_runtime::*;
+use sp_core::Encode;
 use sp_keyring::AccountKeyring;
 use sp_runtime::MultiAddress;
 
@@ -1088,3 +1089,40 @@ fn test_oneshot_accounts() {
             );
         });
 }
+
+/// test change owner key
+// Dave identity should be able to transfer his identity to Eve pubkey
+#[test]
+fn test_change_owner_key() {
+    ExtBuilder::new(1, 3, 4).build().execute_with(|| {
+        // payload
+        let genesis_hash = System::block_hash(0); // 0x454545454545...
+        let old_owner_key = AccountKeyring::Dave.to_account_id();
+        let new_owner_key = AccountKeyring::Eve.to_account_id();
+        let payload = pallet_identity::NewOwnerKeyPayload {
+            genesis_hash: &genesis_hash,
+            idty_index: 4u64,
+            old_owner_key: &old_owner_key,
+        };
+        // check that Dave identity index is actually 4
+        assert_eq!(
+            Identity::identity_index_of(AccountKeyring::Dave.to_account_id()),
+            Some(4)
+        );
+
+        // Dave can change his owner key
+        assert_ok!(Identity::change_owner_key(
+            // origin is Dave (current identity)
+            frame_system::RawOrigin::Signed(AccountKeyring::Dave.to_account_id()).into(),
+            // new key is Eve's key (no identity)
+            new_owner_key.clone(),
+            // payload is signed by Eve
+            AccountKeyring::Eve
+                .sign(
+                    // payload is prefixed by "icok"
+                    &(pallet_identity::NEW_OWNER_KEY_PAYLOAD_PREFIX, payload).encode()
+                )
+                .into()
+        ));
+    })
+}
-- 
GitLab