From 122018a96a1b2b6d9b7eefe150ff76a9de69bdd7 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Fri, 24 Jun 2016 16:13:27 -0700 Subject: [PATCH] Fixed tests --- app/scripts/background.js | 8 ++--- app/scripts/lib/config-manager.js | 1 - app/scripts/lib/idStore.js | 7 ++-- app/scripts/metamask-controller.js | 23 ++++++------ test/lib/mock-config-manager.js | 57 ++++++++++++++++++++++++++++++ test/unit/config-manager-test.js | 7 ++-- test/unit/idStore-test.js | 4 +++ 7 files changed, 87 insertions(+), 20 deletions(-) create mode 100644 test/lib/mock-config-manager.js diff --git a/app/scripts/background.js b/app/scripts/background.js index d23951015..09cdffecd 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -1,4 +1,5 @@ const urlUtil = require('url') +const extend = require('xtend') const Dnode = require('dnode') const eos = require('end-of-stream') const PortStream = require('./lib/port-stream.js') @@ -22,7 +23,7 @@ const controller = new MetamaskController({ }) const idStore = controller.idStore -function unlockAccountMessage() { +function unlockAccountMessage () { createUnlockRequestNotification({ title: 'Account Unlock Request', }) @@ -37,7 +38,7 @@ function showUnconfirmedMessage (msgParams, msgId) { }) } -function showUnconfirmedTx(txParams, txData, onTxDoneCb) { +function showUnconfirmedTx (txParams, txData, onTxDoneCb) { createTxNotification({ title: 'New Unsigned Transaction', txParams: txParams, @@ -89,7 +90,7 @@ function setupControllerConnection (stream) { var api = controller.getApi() var dnode = Dnode(api) stream.pipe(dnode).pipe(stream) - dnode.on('remote', function() { + dnode.on('remote', () => { // push updates to popup controller.ethStore.on('update', controller.sendUpdate) idStore.on('update', controller.sendUpdate) @@ -99,7 +100,6 @@ function setupControllerConnection (stream) { controller.ethStore.removeListener('update', controller.sendUpdate) }) }) - } // diff --git a/app/scripts/lib/config-manager.js b/app/scripts/lib/config-manager.js index 9793728bb..0af82c89c 100644 --- a/app/scripts/lib/config-manager.js +++ b/app/scripts/lib/config-manager.js @@ -1,5 +1,4 @@ const Migrator = require('pojo-migrator') -const extend = require('xtend') const MetamaskConfig = require('../config.js') const migrations = require('./migrations') diff --git a/app/scripts/lib/idStore.js b/app/scripts/lib/idStore.js index 568d9f9a5..f705c07a7 100644 --- a/app/scripts/lib/idStore.js +++ b/app/scripts/lib/idStore.js @@ -43,7 +43,10 @@ function IdentityStore (opts = {}) { IdentityStore.prototype.createNewVault = function (password, entropy, cb) { delete this._keyStore - this.configManager.clearWallet() + if (this.configManager) { + this.configManager.clearWallet() + } + this._createIdmgmt(password, null, entropy, (err) => { if (err) return cb(err) @@ -439,7 +442,7 @@ IdentityStore.prototype._createIdmgmt = function (password, seed, entropy, cb) { keyStore: keyStore, derivedKey: derivedKey, hdPathSTring: this.hdPathString, - this.configManager, + configManager: this.configManager, }) cb() diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index aef280310..262c20b96 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -13,11 +13,13 @@ class MetamaskController { constructor (opts) { this.configManager = new ConfigManager(opts) - this.idStore = new IdentityStore({ configManager }) - this.messageManager = messageManager this.provider = this.initializeProvider(opts) this.ethStore = new EthStore(this.provider) - this.idStore.setStore(this.ethStore) + this.idStore = new IdentityStore({ + configManager: this.configManager, + ethStore: this.ethStore, + }) + this.messageManager = messageManager this.publicConfigStore = this.initPublicConfigStore() } @@ -203,7 +205,7 @@ class MetamaskController { } setupPublicConfig (stream) { - var storeStream = publicConfigStore.createStream() + var storeStream = this.publicConfigStore.createStream() stream.pipe(storeStream).pipe(stream) } @@ -223,7 +225,7 @@ class MetamaskController { // config // - function agreeToDisclaimer (cb) { + agreeToDisclaimer (cb) { try { this.configManager.setConfirmed(true) cb() @@ -233,23 +235,22 @@ class MetamaskController { } // called from popup - function setRpcTarget (rpcTarget) { + setRpcTarget (rpcTarget) { this.configManager.setRpcTarget(rpcTarget) chrome.runtime.reload() - idStore.getNetwork() + this.idStore.getNetwork() } - function setProviderType (type) { + setProviderType (type) { this.configManager.setProviderType(type) chrome.runtime.reload() - idStore.getNetwork() + this.idStore.getNetwork() } - function useEtherscanProvider () { + useEtherscanProvider () { this.configManager.useEtherscanProvider() chrome.runtime.reload() } - } function noop () {} diff --git a/test/lib/mock-config-manager.js b/test/lib/mock-config-manager.js new file mode 100644 index 000000000..fe841f455 --- /dev/null +++ b/test/lib/mock-config-manager.js @@ -0,0 +1,57 @@ +var ConfigManager = require('../../app/scripts/lib/config-manager') +const STORAGE_KEY = 'metamask-persistance-key' +const extend = require('xtend') + +module.exports = function() { + return new ConfigManager({ loadData, setData }) +} + +function loadData () { + var oldData = getOldStyleData() + var newData + try { + newData = JSON.parse(window.localStorage[STORAGE_KEY]) + } catch (e) {} + + var data = extend({ + meta: { + version: 0, + }, + data: { + config: { + provider: { + type: 'testnet', + }, + }, + }, + }, oldData || null, newData || null) + return data +} + +function getOldStyleData () { + var config, wallet, seedWords + + var result = { + meta: { version: 0 }, + data: {}, + } + + try { + config = JSON.parse(window.localStorage['config']) + result.data.config = config + } catch (e) {} + try { + wallet = JSON.parse(window.localStorage['lightwallet']) + result.data.wallet = wallet + } catch (e) {} + try { + seedWords = window.localStorage['seedWords'] + result.data.seedWords = seedWords + } catch (e) {} + + return result +} + +function setData (data) { + window.localStorage[STORAGE_KEY] = JSON.stringify(data) +} diff --git a/test/unit/config-manager-test.js b/test/unit/config-manager-test.js index 130bde2ff..7891c5c9e 100644 --- a/test/unit/config-manager-test.js +++ b/test/unit/config-manager-test.js @@ -1,12 +1,14 @@ var assert = require('assert') -var ConfigManager = require('../../app/scripts/lib/config-manager') +const extend = require('xtend') +const STORAGE_KEY = 'metamask-persistance-key' +var configManagerGen = require('../lib/mock-config-manager') var configManager describe('config-manager', function() { beforeEach(function() { window.localStorage = {} // Hacking localStorage support into JSDom - configManager = new ConfigManager() + configManager = configManagerGen() }) describe('confirmation', function() { @@ -209,3 +211,4 @@ describe('config-manager', function() { }) }) }) + diff --git a/test/unit/idStore-test.js b/test/unit/idStore-test.js index e9611d7e8..ee4613236 100644 --- a/test/unit/idStore-test.js +++ b/test/unit/idStore-test.js @@ -1,5 +1,6 @@ var assert = require('assert') var IdentityStore = require('../../app/scripts/lib/idStore') +var configManagerGen = require('../lib/mock-config-manager') describe('IdentityStore', function() { @@ -15,6 +16,7 @@ describe('IdentityStore', function() { window.localStorage = {} // Hacking localStorage support into JSDom idStore = new IdentityStore({ + configManager: configManagerGen(), ethStore: { addAccount(acct) { accounts.push(acct) }, }, @@ -34,6 +36,7 @@ describe('IdentityStore', function() { window.localStorage = {} // Hacking localStorage support into JSDom idStore = new IdentityStore({ + configManager: configManagerGen(), ethStore: { addAccount(acct) { newAccounts.push(acct) }, }, @@ -65,6 +68,7 @@ describe('IdentityStore', function() { window.localStorage = {} // Hacking localStorage support into JSDom idStore = new IdentityStore({ + configManager: configManagerGen(), ethStore: { addAccount(acct) { accounts.push(acct) }, },