From 01a881523fdc00474ddb03133334a9ba19fd82db Mon Sep 17 00:00:00 2001 From: Flavio Fraschetti Date: Tue, 19 Mar 2024 05:55:38 -0300 Subject: [PATCH] JC-Fraschetti/[Wallet] Add fixtures to recovered accounts and refactor wallet_getDerivedAddresses test (#19287) * chore: fix contract test for wallet/create-derived-addresses and add asynchronous operations using Promesa library --- src/test_helpers/integration.cljs | 104 +++++++++++------- .../wallet_recovered_account_test.cljs | 40 +++++++ src/tests/contract_test/wallet_test.cljs | 32 +----- src/tests/integration_test/constants.cljs | 14 ++- 4 files changed, 114 insertions(+), 76 deletions(-) create mode 100644 src/tests/contract_test/wallet_recovered_account_test.cljs diff --git a/src/test_helpers/integration.cljs b/src/test_helpers/integration.cljs index 49ca801352..5d28cb83d2 100644 --- a/src/test_helpers/integration.cljs +++ b/src/test_helpers/integration.cljs @@ -66,10 +66,18 @@ [] @(rf/subscribe [:messenger/started?])) +(defn wallet-loaded? + [] + (not @(rf/subscribe [:wallet/tokens-loading?]))) + (defn assert-messenger-started [] (is (messenger-started))) +(defn assert-wallet-loaded + [] + (is (wallet-loaded?))) + (defn assert-community-created [] (is (= @(rf/subscribe [:communities/create]) constants/community))) @@ -141,18 +149,49 @@ (test-utils/init!) (if (app-initialized) (p/resolved ::app-initialized) - (do - (rf/dispatch [:app-started]) - (wait-for [:profile/get-profiles-overview-success])))) + (p/do! + (rf/dispatch [:app-started]) + (wait-for [:profile/get-profiles-overview-success])))) (defn setup-account [] (if (messenger-started) (p/resolved ::messenger-started) - (do - (create-multiaccount!) - (-> (wait-for [:profile.login/messenger-started]) - (.then #(assert-messenger-started)))))) + (p/do! + (create-multiaccount!) + (p/then (wait-for [:profile.login/messenger-started]) #(assert-messenger-started))))) + +(defn- recover-and-login + [seed-phrase] + (rf/dispatch [:profile.recover/recover-and-login + {:display-name (:name constants/recovery-account) + :seed-phrase seed-phrase + :password constants/password + :color "blue"}])) + +(defn- enable-testnet! + [] + (rf/dispatch [:profile.settings/profile-update :test-networks-enabled? true {}]) + (rf/dispatch [:wallet/initialize])) + +(defn- recover-multiaccount! + [] + (p/let [masked-seed-phrase (security/mask-data (:seed-phrase constants/recovery-account)) + [mnemonic key-uid] (validate-mnemonic masked-seed-phrase)] + (rf/dispatch [:onboarding/seed-phrase-validated (security/mask-data mnemonic) key-uid]) + (rf/dispatch [:pop-to-root :profiles]) + (rf/dispatch [:profile/profile-selected key-uid]) + (recover-and-login mnemonic))) + +(defn setup-recovered-account + [] + (if (messenger-started) + (p/resolved ::messenger-started) + (p/do! + (recover-multiaccount!) + (p/then (wait-for [:profile.login/messenger-started]) #(assert-messenger-started)) + (enable-testnet!) + (p/then (wait-for [:wallet/store-wallet-token]) #(assert-wallet-loaded))))) (defn test-async "Runs `f` inside `cljs.test/async` macro in a restorable re-frame checkpoint. @@ -228,17 +267,24 @@ Usage: (use-fixtures :each (h/fixture-logged))" - [] - {:before (fn [] - (test/async done - (p/do (setup-app) - (setup-account) - (done)))) - :after (fn [] - (test/async done - (p/do (logout) - (wait-for [::logout/logout-method]) - (done))))}) + ([type] + {:before (if (= :recovered-account type) + (fn [] + (test/async done + (p/do (setup-app) + (setup-recovered-account) + (done)))) + (fn [] + (test/async done + (p/do (setup-app) + (setup-account) + (done))))) + :after (fn [] + (test/async done + (p/do (logout) + (wait-for [::logout/logout-method]) + (done))))}) + ([] (fixture-session [:new-account]))) (defn fixture-silence-reframe "Fixture to disable most re-frame messages. @@ -260,25 +306,3 @@ (set! rf.interop/debug-enabled? false)) :after (fn [] (set! rf.interop/debug-enabled? true))}) - -(defn recover-and-login - [seed-phrase] - (rf/dispatch [:profile.recover/recover-and-login - {:display-name (:name constants/recovery-account) - :seed-phrase seed-phrase - :password constants/password - :color "blue"}])) - -(defn enable-testnet! - [] - (rf/dispatch [:profile.settings/profile-update :test-networks-enabled? true {}]) - (rf/dispatch [:wallet/initialize])) - -(defn recover-multiaccount! - [] - (p/let [masked-seed-phrase (security/mask-data (:seed-phrase constants/recovery-account)) - [mnemonic key-uid] (validate-mnemonic masked-seed-phrase)] - (rf/dispatch [:onboarding/seed-phrase-validated (security/mask-data mnemonic) key-uid]) - (rf/dispatch [:pop-to-root :profiles]) - (rf/dispatch [:profile/profile-selected key-uid]) - (recover-and-login mnemonic))) diff --git a/src/tests/contract_test/wallet_recovered_account_test.cljs b/src/tests/contract_test/wallet_recovered_account_test.cljs new file mode 100644 index 0000000000..1150a1aae2 --- /dev/null +++ b/src/tests/contract_test/wallet_recovered_account_test.cljs @@ -0,0 +1,40 @@ +(ns tests.contract-test.wallet-recovered-account-test + (:require + [cljs.test :refer [deftest is use-fixtures]] + legacy.status-im.events + legacy.status-im.subs.root + [native-module.core :as native-module] + [promesa.core :as p] + status-im.events + status-im.navigation.core + status-im.subs.root + [test-helpers.integration :as h] + [tests.contract-test.utils :as contract-utils] + [tests.integration-test.constants :as integration-constants])) + +(use-fixtures :each (h/fixture-session :recovered-account)) + +(defn get-main-account + [accounts] + (:address (first accounts))) + +(defn assert-derived-account + [response] + (let [{:keys [address public-key]} (first response)] + (is (= integration-constants/derived-address address)) + (is (= (:public-key integration-constants/recovery-account) public-key)) + (is (= integration-constants/derivation-path (:path (first response)))))) + +(deftest wallet-get-derived-addressess-contract-test + (h/test-async :wallet/create-derived-addresses + (fn [] + (p/let [sha3-pwd (native-module/sha3 integration-constants/password) + derivation-path [integration-constants/derivation-path] + accounts (contract-utils/call-rpc "accounts_getAccounts") + main-account (get-main-account accounts) + response (contract-utils/call-rpc + "wallet_getDerivedAddresses" + sha3-pwd + main-account + derivation-path)] + (assert-derived-account response))))) diff --git a/src/tests/contract_test/wallet_test.cljs b/src/tests/contract_test/wallet_test.cljs index c4ba2a84d1..adbbb7ec4f 100644 --- a/src/tests/contract_test/wallet_test.cljs +++ b/src/tests/contract_test/wallet_test.cljs @@ -3,7 +3,6 @@ [cljs.test :refer [deftest is use-fixtures]] legacy.status-im.events legacy.status-im.subs.root - [native-module.core :as native-module] [promesa.core :as p] [status-im.common.emoji-picker.utils :as emoji-picker.utils] [status-im.constants :as constants] @@ -12,8 +11,7 @@ status-im.navigation.core status-im.subs.root [test-helpers.integration :as h] - [tests.contract-test.utils :as contract-utils] - [tests.integration-test.constants :as integration-constants])) + [tests.contract-test.utils :as contract-utils])) (use-fixtures :each (h/fixture-session)) @@ -67,31 +65,3 @@ (fn [] (p/let [response (contract-utils/call-rpc "wallet_getEthereumChains")] (assert-ethereum-chains response))))) - -(defn get-main-account - [accounts] - (:address (first accounts))) - -(def test-password integration-constants/password) - -(defn assert-derived-account - [response] - (is (= (:address response) (:address response))) - (is (= (:public-key response) (:public-key response))) - (is (= "m/43'/60'/1581'/0'/0" (:path (first response))))) - -(deftest wallet-get-derived-addressess-contract-test - (h/test-async :wallet/create-derived-addresses - (fn [] - (p/let [_ (h/enable-testnet!) - _ (h/recover-multiaccount!) - sha3-pwd (native-module/sha3 test-password) - derivation-path ["m/43'/60'/1581'/0'/0"] - accounts (contract-utils/call-rpc "accounts_getAccounts") - main-account (get-main-account accounts) - response (contract-utils/call-rpc - "wallet_getDerivedAddresses" - sha3-pwd - main-account - derivation-path)] - (assert-derived-account response))))) diff --git a/src/tests/integration_test/constants.cljs b/src/tests/integration_test/constants.cljs index ddeb4357c9..5ba77d49e2 100644 --- a/src/tests/integration_test/constants.cljs +++ b/src/tests/integration_test/constants.cljs @@ -6,9 +6,13 @@ (def account-name "account-abc") +(def derivation-path "m/43'/60'/1581'/0'/0") + +(def derived-address "0x542bf2d18e83ba176c151c949c53eee896fa5a3e") + (def recovery-account - {:name "wallet-abc" - :seed-phrase "destroy end torch puzzle develop note wise island disease chaos kind bus" - :key-uid "0x6827f3d1e21ae723a24e0dcbac1853b5ed4d651c821a2326492ce8f2367ec905" - :public-key "0x48b8b9e2a8f71e1beae729d83bcd385ffc6af0d5" - :main-account "0x48b8b9e2a8f71e1beae729d83bcd385ffc6af0d5"}) + {:name "wallet-abc" + :seed-phrase "destroy end torch puzzle develop note wise island disease chaos kind bus" + :key-uid "0x6827f3d1e21ae723a24e0dcbac1853b5ed4d651c821a2326492ce8f2367ec905" + :public-key + "0x04937b2d8ed320f89a437e3c854213ec56b34c0c5a5a6105fd6f0850574d10af6988f128b4e2b7b7171f4599deda75584c7c75f7cdf0846f8d485a7f593a5f31f6"})