diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/integration/lib/idStore-migrator-test.js | 65 | ||||
-rw-r--r-- | test/lib/mock-config-manager.js | 63 | ||||
-rw-r--r-- | test/unit/config-manager-test.js | 10 | ||||
-rw-r--r-- | test/unit/idStore-migration-test.js | 8 | ||||
-rw-r--r-- | test/unit/metamask-controller-test.js | 6 | ||||
-rw-r--r-- | test/unit/migrations-test.js | 48 |
6 files changed, 81 insertions, 119 deletions
diff --git a/test/integration/lib/idStore-migrator-test.js b/test/integration/lib/idStore-migrator-test.js index 4ae30411d..d95cfb401 100644 --- a/test/integration/lib/idStore-migrator-test.js +++ b/test/integration/lib/idStore-migrator-test.js @@ -1,30 +1,23 @@ -var ConfigManager = require('../../../app/scripts/lib/config-manager') -var IdStoreMigrator = require('../../../app/scripts/lib/idStore-migrator') -var SimpleKeyring = require('../../../app/scripts/keyrings/simple') -var normalize = require('../../../app/scripts/lib/sig-util').normalize +const ObservableStore = require('../../../app/scripts/lib/observable/') +const ConfigManager = require('../../../app/scripts/lib/config-manager') +const IdStoreMigrator = require('../../../app/scripts/lib/idStore-migrator') +const SimpleKeyring = require('../../../app/scripts/keyrings/simple') +const normalize = require('../../../app/scripts/lib/sig-util').normalize -var oldStyleVault = require('../mocks/oldVault.json') -var badStyleVault = require('../mocks/badVault.json') +const oldStyleVault = require('../mocks/oldVault.json').data +const badStyleVault = require('../mocks/badVault.json').data -var STORAGE_KEY = 'metamask-config' -var PASSWORD = '12345678' -var FIRST_ADDRESS = '0x4dd5d356c5A016A220bCD69e82e5AF680a430d00'.toLowerCase() -var SEED = 'fringe damage bounce extend tunnel afraid alert sound all soldier all dinner' - -var BAD_STYLE_FIRST_ADDRESS = '0xac39b311dceb2a4b2f5d8461c1cdaf756f4f7ae9' +const PASSWORD = '12345678' +const FIRST_ADDRESS = '0x4dd5d356c5A016A220bCD69e82e5AF680a430d00'.toLowerCase() +const BAD_STYLE_FIRST_ADDRESS = '0xac39b311dceb2a4b2f5d8461c1cdaf756f4f7ae9' +const SEED = 'fringe damage bounce extend tunnel afraid alert sound all soldier all dinner' QUnit.module('Old Style Vaults', { beforeEach: function () { - window.localStorage[STORAGE_KEY] = JSON.stringify(oldStyleVault) - - this.configManager = new ConfigManager({ - loadData: () => { return JSON.parse(window.localStorage[STORAGE_KEY]) }, - setData: (data) => { window.localStorage[STORAGE_KEY] = JSON.stringify(data) }, - }) - - this.migrator = new IdStoreMigrator({ - configManager: this.configManager, - }) + let managers = managersFromInitState(oldStyleVault) + + this.configManager = managers.configManager + this.migrator = managers.migrator } }) @@ -37,6 +30,7 @@ QUnit.test('migrator:migratedVaultForPassword', function (assert) { this.migrator.migratedVaultForPassword(PASSWORD) .then((result) => { + assert.ok(result, 'migratedVaultForPassword returned result') const { serialized, lostAccounts } = result assert.equal(serialized.data.mnemonic, SEED, 'seed phrase recovered') assert.equal(lostAccounts.length, 0, 'no lost accounts') @@ -46,16 +40,10 @@ QUnit.test('migrator:migratedVaultForPassword', function (assert) { QUnit.module('Old Style Vaults with bad HD seed', { beforeEach: function () { - window.localStorage[STORAGE_KEY] = JSON.stringify(badStyleVault) - - this.configManager = new ConfigManager({ - loadData: () => { return JSON.parse(window.localStorage[STORAGE_KEY]) }, - setData: (data) => { window.localStorage[STORAGE_KEY] = JSON.stringify(data) }, - }) - - this.migrator = new IdStoreMigrator({ - configManager: this.configManager, - }) + let managers = managersFromInitState(badStyleVault) + + this.configManager = managers.configManager + this.migrator = managers.migrator } }) @@ -64,6 +52,7 @@ QUnit.test('migrator:migratedVaultForPassword', function (assert) { this.migrator.migratedVaultForPassword(PASSWORD) .then((result) => { + assert.ok(result, 'migratedVaultForPassword returned result') const { serialized, lostAccounts } = result assert.equal(lostAccounts.length, 1, 'one lost account') @@ -89,3 +78,15 @@ QUnit.test('migrator:migratedVaultForPassword', function (assert) { }) }) +function managersFromInitState(initState){ + + let configManager = new ConfigManager({ + store: new ObservableStore(initState), + }) + + let migrator = new IdStoreMigrator({ + configManager: configManager, + }) + + return { configManager, migrator } +}
\ No newline at end of file diff --git a/test/lib/mock-config-manager.js b/test/lib/mock-config-manager.js index b79f63090..c62d91da9 100644 --- a/test/lib/mock-config-manager.js +++ b/test/lib/mock-config-manager.js @@ -1,58 +1,11 @@ -var ConfigManager = require('../../app/scripts/lib/config-manager') +const ConfigManager = require('../../app/scripts/lib/config-manager') +const LocalStorageStore = require('../../app/scripts/lib/observable/local-storage') +const firstTimeState = require('../../app/scripts/first-time-state') const STORAGE_KEY = 'metamask-config' -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) -} + let dataStore = new LocalStorageStore({ storageKey: STORAGE_KEY }) + // initial state for first time users + if (!dataStore.get()) dataStore.put(firstTimeState) + return new ConfigManager({ store: dataStore }) +}
\ No newline at end of file diff --git a/test/unit/config-manager-test.js b/test/unit/config-manager-test.js index 77d431d5f..83b242a8b 100644 --- a/test/unit/config-manager-test.js +++ b/test/unit/config-manager-test.js @@ -1,5 +1,8 @@ // polyfill fetch global.fetch = global.fetch || require('isomorphic-fetch') +// pollyfill localStorage support into JSDom +global.localStorage = global.localStorage || polyfillLocalStorage() + const assert = require('assert') const extend = require('xtend') const rp = require('request-promise') @@ -11,7 +14,7 @@ describe('config-manager', function() { var configManager beforeEach(function() { - window.localStorage = {} // Hacking localStorage support into JSDom + global.localStorage.clear() configManager = configManagerGen() }) @@ -132,7 +135,6 @@ describe('config-manager', function() { }) describe('#setConfig', function() { - window.localStorage = {} // Hacking localStorage support into JSDom it('should set the config key', function () { var testConfig = { @@ -238,3 +240,7 @@ describe('config-manager', function() { }) }) }) + +function polyfillLocalStorage(){ + return Object.create({ clear: function(){ global.localStorage = polyfillLocalStorage() } }) +} diff --git a/test/unit/idStore-migration-test.js b/test/unit/idStore-migration-test.js index 54f38fb2f..4adbef740 100644 --- a/test/unit/idStore-migration-test.js +++ b/test/unit/idStore-migration-test.js @@ -3,6 +3,7 @@ const assert = require('assert') const ethUtil = require('ethereumjs-util') const BN = ethUtil.BN const ConfigManager = require('../../app/scripts/lib/config-manager') +const ObservableStore = require('../../app/scripts/lib/observable/') const delegateCallCode = require('../lib/example-code.json').delegateCallCode // The old way: @@ -42,10 +43,9 @@ describe('IdentityStore to KeyringController migration', function() { beforeEach(function(done) { this.sinon = sinon.sandbox.create() window.localStorage = {} // Hacking localStorage support into JSDom - configManager = new ConfigManager({ - loadData, - setData: (d) => { window.localStorage = d } - }) + let store = new ObservableStore(loadData()) + store.subscribe(setData) + configManager = new ConfigManager({ store }) idStore = new IdentityStore({ diff --git a/test/unit/metamask-controller-test.js b/test/unit/metamask-controller-test.js index a6164c9a0..24d9ddd67 100644 --- a/test/unit/metamask-controller-test.js +++ b/test/unit/metamask-controller-test.js @@ -10,9 +10,11 @@ describe('MetaMaskController', function() { showUnconfirmedMessage: noop, unlockAccountMessage: noop, showUnapprovedTx: noop, - setData, - loadData, + // initial state + initState: loadData(), }) + // setup state persistence + controller.store.subscribe(setData) beforeEach(function() { // sinon allows stubbing methods that are easily verified diff --git a/test/unit/migrations-test.js b/test/unit/migrations-test.js index 9ea8d5c5a..715a5feb0 100644 --- a/test/unit/migrations-test.js +++ b/test/unit/migrations-test.js @@ -1,34 +1,34 @@ -var assert = require('assert') -var path = require('path') +const assert = require('assert') +const path = require('path') -var wallet1 = require(path.join('..', 'lib', 'migrations', '001.json')) +const wallet1 = require(path.join('..', 'lib', 'migrations', '001.json')) -var migration2 = require(path.join('..', '..', 'app', 'scripts', 'migrations', '002')) -var migration3 = require(path.join('..', '..', 'app', 'scripts', 'migrations', '003')) -var migration4 = require(path.join('..', '..', 'app', 'scripts', 'migrations', '004')) +const migration2 = require(path.join('..', '..', 'app', 'scripts', 'migrations', '002')) +const migration3 = require(path.join('..', '..', 'app', 'scripts', 'migrations', '003')) +const migration4 = require(path.join('..', '..', 'app', 'scripts', 'migrations', '004')) -describe('wallet1 is migrated successfully', function() { +const oldTestRpc = 'https://rawtestrpc.metamask.io/' +const newTestRpc = 'https://testrpc.metamask.io/' - it('should convert providers', function(done) { +describe('wallet1 is migrated successfully', function() { + it('should convert providers', function() { wallet1.data.config.provider = { type: 'etherscan', rpcTarget: null } - var firstResult = migration2.migrate(wallet1.data) - assert.equal(firstResult.config.provider.type, 'rpc', 'provider should be rpc') - assert.equal(firstResult.config.provider.rpcTarget, 'https://rpc.metamask.io/', 'main provider should be our rpc') - - var oldTestRpc = 'https://rawtestrpc.metamask.io/' - var newTestRpc = 'https://testrpc.metamask.io/' - firstResult.config.provider.rpcTarget = oldTestRpc - - var secondResult = migration3.migrate(firstResult) - assert.equal(secondResult.config.provider.rpcTarget, newTestRpc) - - var thirdResult = migration4.migrate(secondResult) - assert.equal(secondResult.config.provider.rpcTarget, null) - assert.equal(secondResult.config.provider.type, 'testnet') - - done() + return migration2.migrate(wallet1) + .then((firstResult) => { + assert.equal(firstResult.data.config.provider.type, 'rpc', 'provider should be rpc') + assert.equal(firstResult.data.config.provider.rpcTarget, 'https://rpc.metamask.io/', 'main provider should be our rpc') + firstResult.data.config.provider.rpcTarget = oldTestRpc + return migration3.migrate(firstResult) + }).then((secondResult) => { + assert.equal(secondResult.data.config.provider.rpcTarget, newTestRpc) + return migration4.migrate(secondResult) + }).then((thirdResult) => { + assert.equal(thirdResult.data.config.provider.rpcTarget, null) + assert.equal(thirdResult.data.config.provider.type, 'testnet') + }) + }) }) |