From 1c2120573dc1bf407b93eea4fc46664cbbe2ec1e Mon Sep 17 00:00:00 2001 From: Brian Clifton Date: Sat, 30 Sep 2017 12:40:30 -0700 Subject: [PATCH] Adding unit tests for ledgerReducer Auditors: @NejcZdovc --- .../app/browser/reducers/ledgerReducerTest.js | 524 ++++++++++++++++++ test/unit/app/sessionStoreTest.js | 27 +- test/unit/lib/fakeFileSystem.js | 28 + 3 files changed, 553 insertions(+), 26 deletions(-) create mode 100644 test/unit/app/browser/reducers/ledgerReducerTest.js create mode 100644 test/unit/lib/fakeFileSystem.js diff --git a/test/unit/app/browser/reducers/ledgerReducerTest.js b/test/unit/app/browser/reducers/ledgerReducerTest.js new file mode 100644 index 00000000000..57658534b63 --- /dev/null +++ b/test/unit/app/browser/reducers/ledgerReducerTest.js @@ -0,0 +1,524 @@ +/* global describe, it, before, after */ +const Immutable = require('immutable') +const assert = require('assert') +const mockery = require('mockery') +const sinon = require('sinon') +const appConstants = require('../../../../../js/constants/appConstants') +const settings = require('../../../../../js/constants/settings') +require('../../../braveUnit') + +describe('ledgerReducer unit tests', function () { + let ledgerReducer + let fakeLedgerApi + let fakeLedgerState + let appState + let paymentsEnabled + let returnedState + + before(function () { + mockery.enable({ + warnOnReplace: false, + warnOnUnregistered: false, + useCleanCache: true + }) + const fakeLevel = () => {} + const fakeElectron = require('../../../lib/fakeElectron') + const fakeAdBlock = require('../../../lib/fakeAdBlock') + mockery.registerMock('electron', fakeElectron) + mockery.registerMock('level', fakeLevel) + mockery.registerMock('ad-block', fakeAdBlock) + + const dummyModifyState = (state) => { + return state.set('unittest', true) + } + fakeLedgerApi = { + init: dummyModifyState, + migration: dummyModifyState, + backupKeys: dummyModifyState, + recoverKeys: dummyModifyState, + quit: dummyModifyState, + pageDataChanged: dummyModifyState, + addVisit: dummyModifyState, + + boot: () => {}, + onBootStateFile: dummyModifyState, + balanceReceived: dummyModifyState, + onWalletProperties: dummyModifyState, + paymentPresent: dummyModifyState, + addFoundClosed: dummyModifyState, + onWalletRecovery: dummyModifyState, + onBraveryProperties: dummyModifyState, + onLedgerFirstSync: dummyModifyState, + onCallback: dummyModifyState, + onTimeUntilReconcile: dummyModifyState, + run: () => {}, + onNetworkConnected: dummyModifyState + } + fakeLedgerState = { + resetSynopsis: dummyModifyState, + setRecoveryStatus: dummyModifyState + } + mockery.registerMock('../../browser/api/ledger', fakeLedgerApi) + mockery.registerMock('../../common/state/ledgerState', fakeLedgerState) + mockery.registerMock('../../../js/settings', { + getSetting: (settingKey, settingsCollection, value) => { + if (settingKey === settings.PAYMENTS_ENABLED) { + return paymentsEnabled + } + return false + } + }) + ledgerReducer = require('../../../../../app/browser/reducers/ledgerReducer') + + appState = Immutable.fromJS({ + ledger: {} + }) + }) + + after(function () { + mockery.disable() + }) + + describe('APP_SET_STATE', function () { + let migrationSpy + let initSpy + before(function () { + migrationSpy = sinon.spy(fakeLedgerApi, 'migration') + initSpy = sinon.spy(fakeLedgerApi, 'init') + returnedState = ledgerReducer(appState, Immutable.fromJS({ + actionType: appConstants.APP_SET_STATE + })) + }) + after(function () { + migrationSpy.restore() + initSpy.restore() + }) + it('calls ledgerApi.migration', function () { + assert(migrationSpy.withArgs(appState).calledOnce) + }) + it('calls ledgerApi.init', function () { + assert(initSpy.calledOnce) + }) + it('returns a modified state', function () { + assert.notDeepEqual(returnedState, appState) + }) + }) + + describe('APP_BACKUP_KEYS', function () { + let backupKeysSpy + before(function () { + backupKeysSpy = sinon.spy(fakeLedgerApi, 'backupKeys') + returnedState = ledgerReducer(appState, Immutable.fromJS({ + actionType: appConstants.APP_BACKUP_KEYS, + backupAction: 'ActionGoesHere' + })) + }) + after(function () { + backupKeysSpy.restore() + }) + it('calls ledgerApi.backupKeys', function () { + assert(backupKeysSpy.withArgs(appState, 'ActionGoesHere').calledOnce) + }) + it('returns an ununmodified state', function () { + assert.deepEqual(returnedState, appState) + }) + }) + + describe('APP_RECOVER_WALLET', function () { + let recoverKeysSpy + before(function () { + recoverKeysSpy = sinon.spy(fakeLedgerApi, 'recoverKeys') + returnedState = ledgerReducer(appState, Immutable.fromJS({ + actionType: appConstants.APP_RECOVER_WALLET, + useRecoveryKeyFile: 'useKeyFile', + firstRecoveryKey: 'firstKey', + secondRecoveryKey: 'secondKey' + })) + }) + after(function () { + recoverKeysSpy.restore() + }) + it('calls ledgerApi.recoverKeys', function () { + assert(recoverKeysSpy.withArgs(appState, 'useKeyFile', 'firstKey', 'secondKey').calledOnce) + }) + it('returns a modified state', function () { + assert.notDeepEqual(returnedState, appState) + }) + }) + + describe('APP_SHUTTING_DOWN', function () { + let quitSpy + before(function () { + quitSpy = sinon.spy(fakeLedgerApi, 'quit') + returnedState = ledgerReducer(appState, Immutable.fromJS({ + actionType: appConstants.APP_SHUTTING_DOWN + })) + }) + after(function () { + quitSpy.restore() + }) + it('calls ledgerApi.quit', function () { + assert(quitSpy.withArgs(appState).calledOnce) + }) + it('returns a modified state', function () { + assert.notDeepEqual(returnedState, appState) + }) + }) + + describe('APP_ON_CLEAR_BROWSING_DATA', function () { + let resetSynopsisSpy + let clearAppState + before(function () { + resetSynopsisSpy = sinon.spy(fakeLedgerState, 'resetSynopsis') + }) + after(function () { + resetSynopsisSpy.restore() + }) + describe('when clearData.browserHistory is true and payments is disabled', function () { + before(function () { + resetSynopsisSpy.reset() + paymentsEnabled = false + clearAppState = appState.setIn(['settings', settings.PAYMENTS_ENABLED], paymentsEnabled) + clearAppState = clearAppState.set('clearBrowsingDataDefaults', Immutable.fromJS({ + browserHistory: true + })) + returnedState = ledgerReducer(clearAppState, Immutable.fromJS({ + actionType: appConstants.APP_ON_CLEAR_BROWSING_DATA + })) + }) + it('calls ledgerState.resetSynopsis', function () { + assert(resetSynopsisSpy.withArgs(clearAppState).calledOnce) + }) + it('returns a modified state', function () { + assert.notDeepEqual(returnedState, clearAppState) + }) + }) + describe('else', function () { + before(function () { + resetSynopsisSpy.reset() + paymentsEnabled = true + clearAppState = appState.setIn(['settings', settings.PAYMENTS_ENABLED], paymentsEnabled) + clearAppState = clearAppState.set('clearBrowsingDataDefaults', Immutable.fromJS({ + browserHistory: true + })) + returnedState = ledgerReducer(clearAppState, Immutable.fromJS({ + actionType: appConstants.APP_ON_CLEAR_BROWSING_DATA + })) + }) + it('does not call ledgerState.resetSynopsis', function () { + assert(resetSynopsisSpy.notCalled) + }) + it('returns an ununmodified state', function () { + assert.deepEqual(returnedState, clearAppState) + }) + }) + }) + + describe('APP_IDLE_STATE_CHANGED', function () { + let pageDataChangedSpy + let addVisitSpy + before(function () { + pageDataChangedSpy = sinon.spy(fakeLedgerApi, 'pageDataChanged') + addVisitSpy = sinon.spy(fakeLedgerApi, 'addVisit') + returnedState = ledgerReducer(appState, Immutable.fromJS({ + actionType: appConstants.APP_IDLE_STATE_CHANGED + })) + }) + after(function () { + pageDataChangedSpy.restore() + addVisitSpy.restore() + }) + it('calls ledgerApi.pageDataChanged', function () { + assert(pageDataChangedSpy.withArgs(appState).calledOnce) + }) + it('calls ledgerApi.addVisit', function () { + assert(addVisitSpy.calledOnce) + }) + it('returns a modified state', function () { + assert.notDeepEqual(returnedState, appState) + }) + }) + + describe('', function () { + + }) + + describe('APP_ON_LEDGER_WALLET_CREATE', function () { + let bootSpy + before(function () { + bootSpy = sinon.spy(fakeLedgerApi, 'boot') + returnedState = ledgerReducer(appState, Immutable.fromJS({ + actionType: appConstants.APP_ON_LEDGER_WALLET_CREATE + })) + }) + after(function () { + bootSpy.restore() + }) + it('calls ledgerApi.boot', function () { + assert(bootSpy.calledOnce) + }) + it('returns an ununmodified state', function () { + assert.deepEqual(returnedState, appState) + }) + }) + + describe('APP_ON_BOOT_STATE_FILE', function () { + let onBootStateFileSpy + before(function () { + onBootStateFileSpy = sinon.spy(fakeLedgerApi, 'onBootStateFile') + returnedState = ledgerReducer(appState, Immutable.fromJS({ + actionType: appConstants.APP_ON_BOOT_STATE_FILE + })) + }) + after(function () { + onBootStateFileSpy.restore() + }) + it('calls ledgerApi.onBootStateFile', function () { + assert(onBootStateFileSpy.withArgs(appState).calledOnce) + }) + it('returns a modified state', function () { + assert.notDeepEqual(returnedState, appState) + }) + }) + + describe('APP_ON_LEDGER_BALANCE_RECEIVED', function () { + let balanceReceivedSpy + before(function () { + balanceReceivedSpy = sinon.spy(fakeLedgerApi, 'balanceReceived') + returnedState = ledgerReducer(appState, Immutable.fromJS({ + actionType: appConstants.APP_ON_LEDGER_BALANCE_RECEIVED, + unconfirmed: true + })) + }) + after(function () { + balanceReceivedSpy.restore() + }) + it('calls ledgerApi.balanceReceived', function () { + assert(balanceReceivedSpy.withArgs(appState, true).calledOnce) + }) + it('returns a modified state', function () { + assert.notDeepEqual(returnedState, appState) + }) + }) + + describe('APP_ON_WALLET_PROPERTIES', function () { + let onWalletPropertiesSpy + before(function () { + onWalletPropertiesSpy = sinon.spy(fakeLedgerApi, 'onWalletProperties') + returnedState = ledgerReducer(appState, Immutable.fromJS({ + actionType: appConstants.APP_ON_WALLET_PROPERTIES, + body: 'text-goes-here' + })) + }) + after(function () { + onWalletPropertiesSpy.restore() + }) + it('calls ledgerApi.onWalletProperties', function () { + assert(onWalletPropertiesSpy.withArgs(appState, 'text-goes-here').calledOnce) + }) + it('returns a modified state', function () { + assert.notDeepEqual(returnedState, appState) + }) + }) + + describe('APP_LEDGER_PAYMENTS_PRESENT', function () { + let paymentPresentSpy + before(function () { + paymentPresentSpy = sinon.spy(fakeLedgerApi, 'paymentPresent') + returnedState = ledgerReducer(appState, Immutable.fromJS({ + actionType: appConstants.APP_LEDGER_PAYMENTS_PRESENT, + tabId: 123, + present: true + })) + }) + after(function () { + paymentPresentSpy.restore() + }) + it('calls ledgerApi.paymentPresent', function () { + assert(paymentPresentSpy.withArgs(appState, 123, true).calledOnce) + }) + it('returns an ununmodified state', function () { + assert.deepEqual(returnedState, appState) + }) + }) + + describe('APP_ON_ADD_FUNDS_CLOSED', function () { + let addFoundClosedSpy + before(function () { + addFoundClosedSpy = sinon.spy(fakeLedgerApi, 'addFoundClosed') + returnedState = ledgerReducer(appState, Immutable.fromJS({ + actionType: appConstants.APP_ON_ADD_FUNDS_CLOSED + })) + }) + after(function () { + addFoundClosedSpy.restore() + }) + it('calls ledgerApi.addFoundClosed', function () { + assert(addFoundClosedSpy.withArgs(appState).calledOnce) + }) + it('returns an ununmodified state', function () { + assert.deepEqual(returnedState, appState) + }) + }) + + describe('APP_ON_WALLET_RECOVERY', function () { + let onWalletRecoverySpy + before(function () { + onWalletRecoverySpy = sinon.spy(fakeLedgerApi, 'onWalletRecovery') + returnedState = ledgerReducer(appState, Immutable.fromJS({ + actionType: appConstants.APP_ON_WALLET_RECOVERY, + error: 'error-goes-here', + result: 'result-goes-here' + })) + }) + after(function () { + onWalletRecoverySpy.restore() + }) + it('calls ledgerApi.onWalletRecovery', function () { + assert(onWalletRecoverySpy.withArgs(appState, 'error-goes-here', 'result-goes-here').calledOnce) + }) + it('returns a modified state', function () { + assert.notDeepEqual(returnedState, appState) + }) + }) + + describe('APP_ON_BRAVERY_PROPERTIES', function () { + let onBraveryPropertiesSpy + before(function () { + onBraveryPropertiesSpy = sinon.spy(fakeLedgerApi, 'onBraveryProperties') + returnedState = ledgerReducer(appState, Immutable.fromJS({ + actionType: appConstants.APP_ON_BRAVERY_PROPERTIES, + error: 'error-goes-here', + result: 'result-goes-here' + })) + }) + after(function () { + onBraveryPropertiesSpy.restore() + }) + it('calls ledgerApi.onBraveryProperties', function () { + assert(onBraveryPropertiesSpy.withArgs(appState, 'error-goes-here', 'result-goes-here').calledOnce) + }) + it('returns a modified state', function () { + assert.notDeepEqual(returnedState, appState) + }) + }) + + describe('APP_ON_FIRST_LEDGER_SYNC', function () { + let onLedgerFirstSyncSpy + before(function () { + onLedgerFirstSyncSpy = sinon.spy(fakeLedgerApi, 'onLedgerFirstSync') + returnedState = ledgerReducer(appState, Immutable.fromJS({ + actionType: appConstants.APP_ON_FIRST_LEDGER_SYNC, + parsedData: 'parsed-data-goes-here' + })) + }) + after(function () { + onLedgerFirstSyncSpy.restore() + }) + it('calls ledgerApi.onLedgerFirstSync', function () { + assert(onLedgerFirstSyncSpy.withArgs(appState, 'parsed-data-goes-here').calledOnce) + }) + it('returns a modified state', function () { + assert.notDeepEqual(returnedState, appState) + }) + }) + + describe('APP_ON_LEDGER_CALLBACK', function () { + let onCallbackSpy + before(function () { + onCallbackSpy = sinon.spy(fakeLedgerApi, 'onCallback') + returnedState = ledgerReducer(appState, Immutable.fromJS({ + actionType: appConstants.APP_ON_LEDGER_CALLBACK, + result: 'result-goes-here', + delayTime: 39 + })) + }) + after(function () { + onCallbackSpy.restore() + }) + it('calls ledgerApi.onCallback', function () { + assert(onCallbackSpy.withArgs(appState, 'result-goes-here', 39).calledOnce) + }) + it('returns a modified state', function () { + assert.notDeepEqual(returnedState, appState) + }) + }) + + describe('APP_ON_TIME_UNTIL_RECONCILE', function () { + let onTimeUntilReconcileSpy + before(function () { + onTimeUntilReconcileSpy = sinon.spy(fakeLedgerApi, 'onTimeUntilReconcile') + returnedState = ledgerReducer(appState, Immutable.fromJS({ + actionType: appConstants.APP_ON_TIME_UNTIL_RECONCILE, + stateResult: 'state-result-goes-here' + })) + }) + after(function () { + onTimeUntilReconcileSpy.restore() + }) + it('calls ledgerApi.onTimeUntilReconcile', function () { + assert(onTimeUntilReconcileSpy.withArgs(appState, 'state-result-goes-here').calledOnce) + }) + it('returns a modified state', function () { + assert.notDeepEqual(returnedState, appState) + }) + }) + + describe('APP_ON_LEDGER_RUN', function () { + let runSpy + before(function () { + runSpy = sinon.spy(fakeLedgerApi, 'run') + returnedState = ledgerReducer(appState, Immutable.fromJS({ + actionType: appConstants.APP_ON_LEDGER_RUN, + delay: 7 + })) + }) + after(function () { + runSpy.restore() + }) + it('calls ledgerApi.run', function () { + assert(runSpy.withArgs(appState, 7).calledOnce) + }) + it('returns an ununmodified state', function () { + assert.deepEqual(returnedState, appState) + }) + }) + + describe('APP_ON_NETWORK_CONNECTED', function () { + let onNetworkConnectedSpy + before(function () { + onNetworkConnectedSpy = sinon.spy(fakeLedgerApi, 'onNetworkConnected') + returnedState = ledgerReducer(appState, Immutable.fromJS({ + actionType: appConstants.APP_ON_NETWORK_CONNECTED + })) + }) + after(function () { + onNetworkConnectedSpy.restore() + }) + it('calls ledgerApi.onNetworkConnected', function () { + assert(onNetworkConnectedSpy.withArgs(appState).calledOnce) + }) + it('returns a modified state', function () { + assert.notDeepEqual(returnedState, appState) + }) + }) + + describe('APP_ON_RESET_RECOVERY_STATUS', function () { + let setRecoveryStatusSpy + before(function () { + setRecoveryStatusSpy = sinon.spy(fakeLedgerState, 'setRecoveryStatus') + returnedState = ledgerReducer(appState, Immutable.fromJS({ + actionType: appConstants.APP_ON_RESET_RECOVERY_STATUS + })) + }) + after(function () { + setRecoveryStatusSpy.restore() + }) + it('calls ledgerApi.setRecoveryStatus', function () { + assert(setRecoveryStatusSpy.withArgs(appState, null).calledOnce) + }) + it('returns a modified state', function () { + assert.notDeepEqual(returnedState, appState) + }) + }) +}) diff --git a/test/unit/app/sessionStoreTest.js b/test/unit/app/sessionStoreTest.js index 2c7e3a8df91..47523cb4dec 100644 --- a/test/unit/app/sessionStoreTest.js +++ b/test/unit/app/sessionStoreTest.js @@ -43,32 +43,7 @@ describe('sessionStore unit tests', function () { const fakeWindowState = { getPersistentState: (data) => { return makeImmutable(data) } } - const fakeFileSystem = { - readFileSync: (path) => { - return JSON.stringify({ - cleanedOnShutdown: false - }) - }, - writeFile: (path, options, callback) => { - console.log('calling mocked fs.writeFile') - callback() - }, - rename: (oldPath, newPath, callback) => { - console.log('calling mocked fs.rename') - callback() - }, - copySync: (oldPath, newPath) => { - console.log('calling mocked fs.copySync') - }, - existsSync: (path) => { - console.log('calling mocked fs.existsSync') - return true - }, - remove: (path, callback) => { - console.log('calling mocked fs.remove') - if (callback) callback() - } - } + const fakeFileSystem = require('../lib/fakeFileSystem') const fakeLocale = { init: (language) => { return new Promise((resolve, reject) => { diff --git a/test/unit/lib/fakeFileSystem.js b/test/unit/lib/fakeFileSystem.js new file mode 100644 index 00000000000..cf2a25d2454 --- /dev/null +++ b/test/unit/lib/fakeFileSystem.js @@ -0,0 +1,28 @@ +const fakeFileSystem = { + readFileSync: (path) => { + return JSON.stringify({ + cleanedOnShutdown: false + }) + }, + writeFile: (path, options, callback) => { + console.log('calling mocked fs.writeFile') + callback() + }, + rename: (oldPath, newPath, callback) => { + console.log('calling mocked fs.rename') + callback() + }, + copySync: (oldPath, newPath) => { + console.log('calling mocked fs.copySync') + }, + existsSync: (path) => { + console.log('calling mocked fs.existsSync') + return true + }, + remove: (path, callback) => { + console.log('calling mocked fs.remove') + if (callback) callback() + } +} + +module.exports = fakeFileSystem