Skip to content

Commit

Permalink
[#13804] Extend integration tests to back up seed phrase
Browse files Browse the repository at this point in the history
 Please enter the commit message for your changes. Lines starting
  • Loading branch information
erikseppanen committed Aug 24, 2022
1 parent 2b3d59e commit 462a279
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 18 deletions.
62 changes: 52 additions & 10 deletions src/status_im/integration_test.cljs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns status-im.integration-test
(:require [cljs.test :refer [deftest is run-tests]]
[day8.re-frame.test :as rf-test]
[clojure.string :as string]
[re-frame.core :as rf]
status-im.events
[status-im.multiaccounts.logout.core :as logout]
Expand All @@ -18,7 +19,7 @@
(utils.test/init!)

(defn initialize-app! []
(rf/dispatch-sync [:init/app-started]))
(rf/dispatch [:init/app-started]))

(defn generate-and-derive-addresses! []
(rf/dispatch [:generate-and-derive-addresses]))
Expand Down Expand Up @@ -46,11 +47,6 @@
(defn assert-messenger-started []
(is (messenger-started)))

(deftest initialize-app-test
(rf-test/run-test-sync
(rf/dispatch [:init/app-started])
(assert-app-initialized)))

(defn assert-multiaccount-loaded []
(is (false? @(rf/subscribe [:multiaccounts/loading]))))

Expand All @@ -69,11 +65,22 @@
(defn logout! []
(rf/dispatch [:logout]))

(deftest initialize-app-test
(println "========= initialize-app-test")
(rf-test/run-test-async
(rf/dispatch [:init/app-started])
(rf-test/wait-for
;; use initialize-view because it has the longest avg. time and
;; is dispatched by initialize-multiaccounts (last non-view event)
[:status-im.init.core/initialize-view]
(assert-app-initialized))))

(deftest create-account-test
(println "====== create-account-test")
(rf-test/run-test-async
(initialize-app!) ; initialize app
(rf-test/wait-for
[:status-im.init.core/initialize-multiaccounts] ; wait so we load accounts.db
[:status-im.init.core/initialize-view]
(generate-and-derive-addresses!) ; generate 5 new keys
(rf-test/wait-for
[:multiaccount-generate-and-derive-addresses-success] ; wait for the keys
Expand All @@ -87,10 +94,11 @@
(assert-logout)))))))

(deftest create-community-test
(println "====== create-community-test")
(rf-test/run-test-async
(initialize-app!) ; initialize app
(rf-test/wait-for
[:status-im.init.core/initialize-multiaccounts]
[:status-im.init.core/initialize-view]
(generate-and-derive-addresses!) ; generate 5 new keys
(rf-test/wait-for
[:multiaccount-generate-and-derive-addresses-success]
Expand All @@ -111,10 +119,11 @@
(assert-logout))))))))

(deftest create-wallet-account-test
(println "====== create-wallet-account-test")
(rf-test/run-test-async
(initialize-app!) ; initialize app
(initialize-app!)
(rf-test/wait-for
[:status-im.init.core/initialize-multiaccounts]
[:status-im.init.core/initialize-view]
(generate-and-derive-addresses!) ; generate 5 new keys
(rf-test/wait-for
[:multiaccount-generate-and-derive-addresses-success]
Expand All @@ -131,5 +140,38 @@
(rf-test/wait-for [::logout/logout-method] ; we need to logout to make sure the node is not in an inconsistent state between tests
(assert-logout))))))))

(deftest back-up-seed-phrase-test
(println "========= back-up-seed-phrase-test")
(rf-test/run-test-async
(initialize-app!)
(rf-test/wait-for
[:status-im.init.core/initialize-view]
(generate-and-derive-addresses!)
(rf-test/wait-for
[:multiaccount-generate-and-derive-addresses-success]
(assert-multiaccount-loaded)
(create-multiaccount!)
(rf-test/wait-for
[::transport/messenger-started]
(assert-messenger-started)
(rf/dispatch-sync [:set-in [:my-profile/seed :step] :12-words]) ; display seed phrase to user
(rf/dispatch-sync [:my-profile/enter-two-random-words]) ; begin prompting user for seed words
(let [ma @(rf/subscribe [:multiaccount])
seed @(rf/subscribe [:my-profile/seed])
word1 (second (:first-word seed))
word2 (second (:second-word seed))]
(is (= 12 (count (string/split (:mnemonic ma) #" ")))) ; assert 12-word seed phrase
(rf/dispatch-sync [:set-in [:my-profile/seed :word] word1])
(rf/dispatch-sync [:my-profile/set-step :second-word])
(rf/dispatch-sync [:set-in [:my-profile/seed :word] word2])
;; TODO: refactor (defn next-handler) & (defn enter-word) to improve test coverage?
(rf/dispatch [:my-profile/finish])
(rf-test/wait-for
[:my-profile/finish-success]
(is (nil? @(rf/subscribe [:mnemonic]))) ; assert seed phrase has been removed
(logout!)
(rf-test/wait-for [::logout/logout-method] ; we need to logout to make sure the node is not in an inconsistent state between tests
(assert-logout)))))))))

(comment
(run-tests))
6 changes: 2 additions & 4 deletions src/status_im/multiaccounts/update/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@

(fx/defn clean-seed-phrase
"A helper function that removes seed phrase from storage."
[cofx]
(multiaccount-update cofx
:mnemonic nil
{}))
[cofx on-success]
(multiaccount-update cofx :mnemonic nil on-success))

(defn augment-synchronized-recent-stickers
"Add 'url' parameter to stickers that are synchronized from other devices.
Expand Down
2 changes: 1 addition & 1 deletion src/status_im/multiaccounts/update/core_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
(is (= (get-in efx [:db :multiaccount]) {:not-empty "would throw an error if was empty"}))))

(deftest test-clean-seed-phrase
(let [efx (multiaccounts.update/clean-seed-phrase {:db {:multiaccount {:mnemonic "lalalala"}}})
(let [efx (multiaccounts.update/clean-seed-phrase {:db {:multiaccount {:mnemonic "lalalala"}}} nil)
json-rpc (into #{} (map :method (::json-rpc/call efx)))]
(is (json-rpc "settings_saveSetting"))
(is (nil? (get-in efx [:db :multiaccount :mnemonic])))))
11 changes: 8 additions & 3 deletions src/status_im/profile/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,17 @@
(let [link (universal-links/generate-link :user :external contact-code)]
(list-selection/open-share {:message link}))))

(fx/defn finish-success
{:events [:my-profile/finish-success]}
[{:keys [db] :as cofx}]
{:db (update db :my-profile/seed assoc :step :finish :error nil :word nil)})

(fx/defn finish
{:events [:my-profile/finish]}
[{:keys [db] :as cofx}]
[cofx]
(fx/merge cofx
{:db (update db :my-profile/seed assoc :step :finish :error nil :word nil)}
(multiaccounts.update/clean-seed-phrase)))
(multiaccounts.update/clean-seed-phrase
{:on-success #(re-frame/dispatch [:my-profile/finish-success])})))

(fx/defn enter-two-random-words
{:events [:my-profile/enter-two-random-words]}
Expand Down

0 comments on commit 462a279

Please sign in to comment.