Skip to content

Commit

Permalink
[#20035] fix: improve tests and move effects to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsen-ghafouri committed May 29, 2024
1 parent cc9fb02 commit 7b9cc07
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 90 deletions.
45 changes: 45 additions & 0 deletions src/status_im/contexts/settings/wallet/effects.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
(ns status-im.contexts.settings.wallet.effects
(:require [native-module.core :as native-module]
[promesa.core :as promesa]
[status-im.contexts.syncing.events :as syncing-events]
[status-im.contexts.syncing.utils :as sync-utils]
[utils.re-frame :as rf]
[utils.security.core :as security]
[utils.transforms :as transforms]))

(rf/reg-fx :effects.connection-string/export-keypair
(fn [{:keys [key-uid sha3-pwd keypair-key-uid on-success on-fail]}]
(let [config-map (transforms/clj->json {:senderConfig {:loggedInKeyUid key-uid
:keystorePath ""
:keypairsToExport [keypair-key-uid]
:password (security/safe-unmask-data
sha3-pwd)}
:serverConfig {:timeout 0}})]
(-> (native-module/get-connection-string-for-exporting-keypairs-keystores
config-map)
(promesa/then (fn [response]
(if (sync-utils/valid-connection-string? response)
(on-success response)
(on-fail (throw (js/Error.
"generic-error: failed to get connection string"))))))
(promesa/catch on-fail)))))

(rf/reg-fx :effects.connection-string/import-keypair
(fn [{:keys [key-uid sha3-pwd keypairs-key-uids connection-string on-success on-fail]}]
(let [config-map (.stringify js/JSON
(clj->js
{:receiverConfig
{:loggedInKeyUid key-uid
:keystorePath ""
:password (security/safe-unmask-data
sha3-pwd)
:keypairsToImport keypairs-key-uids}}))]
(-> (native-module/input-connection-string-for-importing-keypairs-keystores
connection-string
config-map)
(promesa/then (fn [res]
(if-let [error (when (syncing-events/extract-error res)
(str "generic-error: " res))]
(on-fail error)
(on-success keypairs-key-uids))))
(promesa/catch on-fail)))))
51 changes: 4 additions & 47 deletions src/status_im/contexts/settings/wallet/events.cljs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
(ns status-im.contexts.settings.wallet.events
(:require
[native-module.core :as native-module]
[promesa.core :as promesa]
[status-im.contexts.settings.wallet.data-store :as data-store]
[status-im.contexts.syncing.events :as syncing-events]
[status-im.contexts.syncing.utils :as sync-utils]
[taoensso.timbre :as log]
[utils.i18n :as i18n]
[utils.re-frame :as rf]
[utils.security.core :as security]
[utils.transforms :as transforms]))
[utils.re-frame :as rf]))

(rf/reg-event-fx
:wallet/rename-keypair-success
Expand All @@ -34,22 +29,6 @@

(rf/reg-event-fx :wallet/rename-keypair rename-keypair)

(rf/reg-fx :effects.connection-string/export-keypair
(fn [{:keys [key-uid sha3-pwd keypair-key-uid on-success on-fail]}]
(let [config-map (transforms/clj->json {:senderConfig {:loggedInKeyUid key-uid
:keystorePath ""
:keypairsToExport [keypair-key-uid]
:password (security/safe-unmask-data
sha3-pwd)}
:serverConfig {:timeout 0}})]
(-> (native-module/get-connection-string-for-exporting-keypairs-keystores
config-map)
(promesa/then (fn [response]
(if (sync-utils/valid-connection-string? response)
(on-success response)
(on-fail "generic-error: failed to get connection string"))))
(promesa/catch on-fail)))))

(defn get-keypair-export-connection
[{:keys [db]} [{:keys [sha3-pwd keypair-key-uid callback]}]]
(let [key-uid (get-in db [:profile/profile :key-uid])]
Expand All @@ -63,7 +42,7 @@
:on-fail (fn [error]
(rf/dispatch [:toasts/upsert
{:type :negative
:text error}]))}]]}))
:text (.-message error)}]))}]]}))

(rf/reg-event-fx :wallet/get-keypair-export-connection get-keypair-export-connection)

Expand All @@ -85,7 +64,7 @@
[{:method "accounts_deleteKeypair"
:params [key-uid]
:on-success [:wallet/remove-keypair-success key-uid]
:on-error #(log/info "failed to remove keypair " %)}]]]})
:on-error #(log/error "failed to remove keypair " {:error %})}]]]})

(rf/reg-event-fx :wallet/remove-keypair remove-keypair)

Expand All @@ -107,27 +86,6 @@

(rf/reg-event-fx :wallet/make-keypairs-accounts-fully-operable make-keypairs-accounts-fully-operable)

(rf/reg-fx :effects.connection-string/import-keypair
(fn [{:keys [key-uid sha3-pwd keypairs-key-uids connection-string on-success on-fail]}]
(let [config-map (.stringify js/JSON
(clj->js
{:receiverConfig
{:loggedInKeyUid key-uid
:keystorePath ""
:password (security/safe-unmask-data
sha3-pwd)
:keypairsToImport keypairs-key-uids}}))]
(-> (native-module/input-connection-string-for-importing-keypairs-keystores
connection-string
config-map)
(promesa/then (fn [res]
(let [error (when (syncing-events/extract-error res)
(str "generic-error: " res))]
(if-not (some? error)
(on-success)
(on-fail error)))))
(promesa/catch #(log/error "error import-keypair/connection-string " %))))))

(defn connection-string-for-import-keypair
[{:keys [db]} [{:keys [sha3-pwd keypairs-key-uids connection-string]}]]
(let [key-uid (get-in db [:profile/profile :key-uid])]
Expand All @@ -136,8 +94,7 @@
:sha3-pwd sha3-pwd
:keypairs-key-uids keypairs-key-uids
:connection-string connection-string
:on-success #(rf/dispatch [:wallet/make-keypairs-accounts-fully-operable
keypairs-key-uids])
:on-success #(rf/dispatch [:wallet/make-keypairs-accounts-fully-operable %])
:on-fail #(rf/dispatch [:toasts/upsert
{:type :negative
:text %}])}]]}))
Expand Down
102 changes: 59 additions & 43 deletions src/status_im/contexts/settings/wallet/events_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,54 @@
(def mock-key-uid "key-1")
(defn mock-db
[keypairs accounts]
{:wallet {:keypairs keypairs
:accounts accounts}
:profile {:profile {:key-uid "test-key-uid"}}})
{:wallet {:keypairs keypairs
:accounts accounts}
:profile/profile {:key-uid "test-key-uid"}})

(deftest test-rename-keypair
(deftest rename-keypair-test
(let [new-keypair-name "key pair new"
cofx {:db {}}
expected {:fx [[:json-rpc/call
[{:method "accounts_updateKeypairName"
:params [mock-key-uid new-keypair-name]
:on-success [:wallet/rename-keypair-success mock-key-uid
new-keypair-name]
:on-error fn?}]]]}]
(is (match? expected
(sut/rename-keypair cofx
[{:key-uid mock-key-uid
:keypair-name new-keypair-name}])))))
cofx {:db {}}]
(testing "rename-keypair"
(let [expected {:fx [[:json-rpc/call
[{:method "accounts_updateKeypairName"
:params [mock-key-uid new-keypair-name]
:on-success [:wallet/rename-keypair-success mock-key-uid
new-keypair-name]
:on-error fn?}]]]}]
(is (match? expected
(sut/rename-keypair cofx
[{:key-uid mock-key-uid
:keypair-name new-keypair-name}])))))))

(deftest test-get-keypair-export-connection
(let [db (mock-db [] {})
sha3-pwd "test-password"
keypair-key-uid "test-keypair-uid"
callback (fn [connect-string] (println "callback" connect-string))]
(testing "test-get-keypair-export-connection"
(let [effects (sut/get-keypair-export-connection
{:db db}
[{:sha3-pwd sha3-pwd :keypair-key-uid keypair-key-uid :callback callback}])
fx (:fx effects)]
(is (some? fx))))))
(deftest get-keypair-export-connection-test
(let [cofx {:db (mock-db [] {})}
sha3-pwd "test-password"
user-key-uid "test-key-uid"
callback (fn [connect-string] (println "callback" connect-string))]
(testing "get-keypair-export-connection"
(let [expected {:fx [[:effects.connection-string/export-keypair
{:key-uid user-key-uid
:sha3-pwd sha3-pwd
:keypair-key-uid mock-key-uid
:on-success fn?
:on-fail fn?}]]}]
(is (match? expected
(sut/get-keypair-export-connection
cofx
[{:sha3-pwd sha3-pwd :keypair-key-uid mock-key-uid :callback callback}])))))))

(deftest test-remove-keypair
(let [db (mock-db [{:key-uid mock-key-uid :name "Key 1"}] {})]
(deftest remove-keypair-test
(let [cofx {:db {}}]
(testing "remove-keypair"
(let [effects (sut/remove-keypair {:db db} [{:key-uid mock-key-uid}])
fx (:fx effects)]
(is (some? fx))))))
(let [expected {:fx [[:json-rpc/call
[{:method "accounts_deleteKeypair"
:params [mock-key-uid]
:on-success [:wallet/remove-keypair-success mock-key-uid]
:on-error fn?}]]]}]
(is (match? expected
(sut/remove-keypair cofx [mock-key-uid])))))))

(deftest test-make-keypairs-accounts-fully-operable
(deftest make-keypairs-accounts-fully-operable-test
(let [db (mock-db [{:key-uid mock-key-uid
:accounts [{:key-uid mock-key-uid :operable "no"}]}]
{"0x1" {:key-uid mock-key-uid :operable "no"}})
Expand All @@ -58,21 +68,27 @@
(is (= (keyword (-> updated-keypair :accounts first :operable)) :fully))
(is (= (keyword (:operable updated-account)) :fully))))))

(deftest test-connection-string-for-import-keypair
(let [db (mock-db [] {})
(deftest connection-string-for-import-keypair-test
(let [cofx {:db (mock-db [] {})}
sha3-pwd "test-password"
keypairs-key-uids ["test-keypair-uid"]
user-key-uid "test-key-uid"
connection-string "test-connection-string"]
(testing "connection-string-for-import-keypair"
(let [effects (sut/connection-string-for-import-keypair
{:db db}
[{:sha3-pwd sha3-pwd
:keypairs-key-uids keypairs-key-uids
:connection-string connection-string}])
fx (:fx effects)]
(is (some? fx))))))
(let [expected {:fx [[:effects.connection-string/import-keypair
{:key-uid user-key-uid
:sha3-pwd sha3-pwd
:keypairs-key-uids [mock-key-uid]
:connection-string connection-string
:on-success fn?
:on-fail fn?}]]}]
(is (match? expected
(sut/connection-string-for-import-keypair cofx
[{:sha3-pwd sha3-pwd
:keypairs-key-uids [mock-key-uid]
:connection-string
connection-string}])))))))

(deftest test-success-keypair-qr-scan
(deftest success-keypair-qr-scan-test
(let [connection-string "valid-connection-string"
keypairs-key-uids ["keypair-uid"]]
(testing "success-keypair-qr-scan"
Expand Down
1 change: 1 addition & 0 deletions src/status_im/contexts/wallet/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[clojure.string :as string]
[react-native.platform :as platform]
[status-im.constants :as constants]
[status-im.contexts.settings.wallet.effects]
[status-im.contexts.settings.wallet.events]
[status-im.contexts.wallet.common.utils.networks :as network-utils]
[status-im.contexts.wallet.data-store :as data-store]
Expand Down

0 comments on commit 7b9cc07

Please sign in to comment.