From 9a65a49d0b933c34c90bfb2bd64b5c3dbeb3fd57 Mon Sep 17 00:00:00 2001 From: crptm Date: Tue, 4 Jul 2017 16:19:04 +0400 Subject: [PATCH] invalid pkey message --- common/reducers/wallet.js | 9 +++------ common/sagas/wallet.js | 13 +++++++++++-- jest_config/jest.config.json | 19 +++++-------------- spec/sagas/wallet.spec.js | 12 ++++++++++++ 4 files changed, 31 insertions(+), 22 deletions(-) create mode 100644 spec/sagas/wallet.spec.js diff --git a/common/reducers/wallet.js b/common/reducers/wallet.js index 5951791c..7d08914f 100644 --- a/common/reducers/wallet.js +++ b/common/reducers/wallet.js @@ -1,8 +1,8 @@ // @flow import type { WalletAction, - SaveWalletAction, - InitWalletAction + SaveWalletAction + // InitWalletAction } from 'actions/wallet'; import BaseWallet from 'libs/wallet/base'; @@ -28,10 +28,7 @@ function initWallet(state: State): State { return { ...state, balance: 0, tokens: {} }; } -export function wallet( - state: State = initialState, - action: WalletAction -): State { +export function wallet(state: State = initialState, action: WalletAction): State { switch (action.type) { case 'WALLET_SAVE': return saveWallet(state, action); diff --git a/common/sagas/wallet.js b/common/sagas/wallet.js index db7bb7c6..de19c5be 100644 --- a/common/sagas/wallet.js +++ b/common/sagas/wallet.js @@ -4,7 +4,9 @@ import type { Effect } from 'redux-saga/effects'; import { delay } from 'redux-saga'; import { saveWallet, initWallet } from 'actions/wallet'; import type { UnlockPrivateKeyAction } from 'actions/wallet'; +import { showNotification } from 'actions/notifications'; import PrivKeyWallet from 'libs/wallet/privkey'; +import translate from 'translations'; function* init() { yield put(initWallet()); @@ -15,9 +17,16 @@ function* init() { yield delay(100); } -function* unlockPrivateKey(action?: UnlockPrivateKeyAction) { +export function* unlockPrivateKey(action?: UnlockPrivateKeyAction): Generator { if (!action) return; - yield put(saveWallet(new PrivKeyWallet(action.payload))); + let wallet = null; + try { + wallet = new PrivKeyWallet(action.payload); + } catch (e) { + yield put(showNotification('danger', translate('INVALID_PKEY'))); + return; + } + yield put(saveWallet(wallet)); yield call(init); } diff --git a/jest_config/jest.config.json b/jest_config/jest.config.json index 3f324639..e7264521 100644 --- a/jest_config/jest.config.json +++ b/jest_config/jest.config.json @@ -1,16 +1,7 @@ { - "moduleFileExtensions": ["js", "jsx"], - "testPathIgnorePatterns": ["/common/config"], - "setupFiles": ["/jest_config/setupJest.js"], - "automock": false, - "moduleNameMapper": { - "^actions$": "/common/actions", - "^api": "/common/api", - "^reducers$": "/common/reducers", - "^routing": "/common/routing", - "^components$": "/common/components", - "^containers$": "/common/containers", - "^translations(.*)": "/common/translations$1", - "^libs(.*)": "/common/libs$1" - } + "moduleFileExtensions": ["js", "jsx"], + "testPathIgnorePatterns": ["/common/config"], + "setupFiles": ["/jest_config/setupJest.js"], + "automock": false, + "moduleDirectories": ["node_modules", "common"] } diff --git a/spec/sagas/wallet.spec.js b/spec/sagas/wallet.spec.js new file mode 100644 index 00000000..a26c5f59 --- /dev/null +++ b/spec/sagas/wallet.spec.js @@ -0,0 +1,12 @@ +import { unlockPrivateKey } from 'sagas/wallet'; + +describe('Wallet saga', () => { + it('Should show error notification on decryption failure', () => { + const gen = unlockPrivateKey({ + key: '0000000000000000000000000000000000000000000000000000000000000000' + }); + // FIXME fragile + expect(gen.next().value.PUT.action.type).toEqual('SHOW_NOTIFICATION'); + expect(gen.next().done).toBeTruthy(); + }); +});