From 33182efb1384dc05c8ecdb8994d12dd30abcbc7e Mon Sep 17 00:00:00 2001 From: Thomas Huang Date: Thu, 15 Feb 2018 08:34:31 -0800 Subject: Offline testing --- test/unit/blacklist-controller-test.js | 2 +- test/unit/metamask-controller-test.js | 178 ++++++++++++++------------------- test/unit/network-contoller-test.js | 4 + 3 files changed, 81 insertions(+), 103 deletions(-) (limited to 'test/unit') diff --git a/test/unit/blacklist-controller-test.js b/test/unit/blacklist-controller-test.js index a9260466f..cbf73d3e5 100644 --- a/test/unit/blacklist-controller-test.js +++ b/test/unit/blacklist-controller-test.js @@ -38,4 +38,4 @@ describe('blacklist controller', function () { assert.equal(result, false) }) }) -}) \ No newline at end of file +}) diff --git a/test/unit/metamask-controller-test.js b/test/unit/metamask-controller-test.js index 3fc7f9a98..ac984faf5 100644 --- a/test/unit/metamask-controller-test.js +++ b/test/unit/metamask-controller-test.js @@ -1,129 +1,103 @@ const assert = require('assert') const sinon = require('sinon') const clone = require('clone') +const nock = require('nock') const MetaMaskController = require('../../app/scripts/metamask-controller') -const firstTimeState = require('../../app/scripts/first-time-state') -const BN = require('ethereumjs-util').BN -const GWEI_BN = new BN('1000000000') +const blacklistJSON = require('../stub/blacklist') +const firstTimeState = require('../stub/first-time-state') describe('MetaMaskController', function () { - const noop = () => {} - const metamaskController = new MetaMaskController({ - showUnconfirmedMessage: noop, - unlockAccountMessage: noop, - showUnapprovedTx: noop, - platform: {}, - encryptor: { - encrypt: function(password, object) { - this.object = object - return Promise.resolve() - }, - decrypt: function () { - return Promise.resolve(this.object) - } - }, - // initial state - initState: clone(firstTimeState), - }) + let metamaskController + const sandbox = sinon.sandbox.create() + const noop = () => { } beforeEach(function () { - // sinon allows stubbing methods that are easily verified - this.sinon = sinon.sandbox.create() + + nock('https://api.infura.io') + .persist() + .get('/v2/blacklist') + .reply(200, blacklistJSON) + + nock('https://rinkeby.infura.io') + .persist() + .post('/metamask') + .reply(200) + + metamaskController = new MetaMaskController({ + showUnapprovedTx: noop, + encryptor: { + encrypt: function (password, object) { + this.object = object + return Promise.resolve() + }, + decrypt: function () { + return Promise.resolve(this.object) + }, + }, + initState: clone(firstTimeState), + }) + sandbox.spy(metamaskController.keyringController, 'createNewVaultAndKeychain') + sandbox.spy(metamaskController.keyringController, 'createNewVaultAndRestore') }) afterEach(function () { - // sinon requires cleanup otherwise it will overwrite context - this.sinon.restore() + nock.cleanAll() + sandbox.restore() }) - describe('Metamask Controller', function () { - assert(metamaskController) - - beforeEach(function () { - sinon.spy(metamaskController.keyringController, 'createNewVaultAndKeychain') - sinon.spy(metamaskController.keyringController, 'createNewVaultAndRestore') - }) - - afterEach(function () { - metamaskController.keyringController.createNewVaultAndKeychain.restore() - metamaskController.keyringController.createNewVaultAndRestore.restore() - }) - - describe('#getGasPrice', function () { - it('gives the 50th percentile lowest accepted gas price from recentBlocksController', async function () { - const realRecentBlocksController = metamaskController.recentBlocksController - metamaskController.recentBlocksController = { - store: { - getState: () => { - return { - recentBlocks: [ - { gasPrices: [ '0x3b9aca00', '0x174876e800'] }, - { gasPrices: [ '0x3b9aca00', '0x174876e800'] }, - { gasPrices: [ '0x174876e800', '0x174876e800' ]}, - { gasPrices: [ '0x174876e800', '0x174876e800' ]}, - ] - } - } - } - } - - const gasPrice = metamaskController.getGasPrice() - assert.equal(gasPrice, '0x3b9aca00', 'accurately estimates 50th percentile accepted gas price') - - metamaskController.recentBlocksController = realRecentBlocksController - }) - - it('gives the 1 gwei price if no blocks have been seen.', async function () { - const realRecentBlocksController = metamaskController.recentBlocksController - metamaskController.recentBlocksController = { - store: { - getState: () => { - return { - recentBlocks: [] - } + describe('#getGasPrice', function () { + it('gives the 50th percentile lowest accepted gas price from recentBlocksController', async function () { + const realRecentBlocksController = metamaskController.recentBlocksController + metamaskController.recentBlocksController = { + store: { + getState: () => { + return { + recentBlocks: [ + { gasPrices: [ '0x3b9aca00', '0x174876e800'] }, + { gasPrices: [ '0x3b9aca00', '0x174876e800'] }, + { gasPrices: [ '0x174876e800', '0x174876e800' ]}, + { gasPrices: [ '0x174876e800', '0x174876e800' ]}, + ], } - } - } - - const gasPrice = metamaskController.getGasPrice() - assert.equal(gasPrice, '0x' + GWEI_BN.toString(16), 'defaults to 1 gwei') + }, + }, + } - metamaskController.recentBlocksController = realRecentBlocksController - }) + const gasPrice = metamaskController.getGasPrice() + assert.equal(gasPrice, '0x3b9aca00', 'accurately estimates 50th percentile accepted gas price') + metamaskController.recentBlocksController = realRecentBlocksController }) + }) - describe('#createNewVaultAndKeychain', function () { - it('can only create new vault on keyringController once', async function () { - const selectStub = sinon.stub(metamaskController, 'selectFirstIdentity') - + describe('#createNewVaultAndKeychain', function () { + it('can only create new vault on keyringController once', async function () { + const selectStub = sandbox.stub(metamaskController, 'selectFirstIdentity') - const password = 'a-fake-password' + const password = 'a-fake-password' - const first = await metamaskController.createNewVaultAndKeychain(password) - const second = await metamaskController.createNewVaultAndKeychain(password) + await metamaskController.createNewVaultAndKeychain(password) + await metamaskController.createNewVaultAndKeychain(password) - assert(metamaskController.keyringController.createNewVaultAndKeychain.calledOnce) + assert(metamaskController.keyringController.createNewVaultAndKeychain.calledOnce) - selectStub.reset() - }) + selectStub.reset() }) + }) + + describe('#createNewVaultAndRestore', function () { + it('should be able to call newVaultAndRestore despite a mistake.', async function () { + + const password = 'what-what-what' + const wrongSeed = 'debris dizzy just program just float decrease vacant alarm reduce speak stadiu' + const rightSeed = 'debris dizzy just program just float decrease vacant alarm reduce speak stadium' + await metamaskController.createNewVaultAndRestore(password, wrongSeed) + .catch((e) => { + return + }) + await metamaskController.createNewVaultAndRestore(password, rightSeed) - describe('#createNewVaultAndRestore', function () { - it('should be able to call newVaultAndRestore despite a mistake.', async function () { - // const selectStub = sinon.stub(metamaskController, 'selectFirstIdentity') - - const password = 'what-what-what' - const wrongSeed = 'debris dizzy just program just float decrease vacant alarm reduce speak stadiu' - const rightSeed = 'debris dizzy just program just float decrease vacant alarm reduce speak stadium' - const first = await metamaskController.createNewVaultAndRestore(password, wrongSeed) - .catch((e) => { - return - }) - const second = await metamaskController.createNewVaultAndRestore(password, rightSeed) - - assert(metamaskController.keyringController.createNewVaultAndRestore.calledTwice) - }) + assert(metamaskController.keyringController.createNewVaultAndRestore.calledTwice) }) }) }) diff --git a/test/unit/network-contoller-test.js b/test/unit/network-contoller-test.js index 0b3b5adeb..fe3b9e66a 100644 --- a/test/unit/network-contoller-test.js +++ b/test/unit/network-contoller-test.js @@ -1,4 +1,5 @@ const assert = require('assert') +const nock = require('nock') const NetworkController = require('../../app/scripts/controllers/network') describe('# Network Controller', function () { @@ -8,6 +9,9 @@ describe('# Network Controller', function () { } beforeEach(function () { + nock('https://api.infura.io') + .get('/*/') + .reply(200, {}) networkController = new NetworkController({ provider: { type: 'rinkeby', -- cgit From 8f7094a73d4ca5879e8290e3b1aefdc42397767d Mon Sep 17 00:00:00 2001 From: Thomas Huang Date: Thu, 15 Feb 2018 09:54:22 -0800 Subject: Network controller nock --- test/unit/network-contoller-test.js | 40 +++++++++++++++---------------------- 1 file changed, 16 insertions(+), 24 deletions(-) (limited to 'test/unit') diff --git a/test/unit/network-contoller-test.js b/test/unit/network-contoller-test.js index fe3b9e66a..cd8c345c5 100644 --- a/test/unit/network-contoller-test.js +++ b/test/unit/network-contoller-test.js @@ -2,28 +2,37 @@ const assert = require('assert') const nock = require('nock') const NetworkController = require('../../app/scripts/controllers/network') +const { createTestProviderTools } = require('../stub/provider') +const providerResultStub = {} +const provider = createTestProviderTools({ scaffold: providerResultStub }).provider + describe('# Network Controller', function () { let networkController + const noop = () => {} const networkControllerProviderInit = { getAccounts: () => {}, } beforeEach(function () { + nock('https://api.infura.io') .get('/*/') - .reply(200, {}) + .reply(200) + + nock('https://rinkeby.infura.io') + .post('/metamask') + .reply(200) + networkController = new NetworkController({ - provider: { - type: 'rinkeby', - }, + provider, }) - networkController.initializeProvider(networkControllerProviderInit, dummyProviderConstructor) + networkController.initializeProvider(networkControllerProviderInit, provider) }) describe('network', function () { describe('#provider', function () { it('provider should be updatable without reassignment', function () { - networkController.initializeProvider(networkControllerProviderInit, dummyProviderConstructor) + networkController.initializeProvider(networkControllerProviderInit, provider) const proxy = networkController._proxy proxy.setTarget({ test: true, on: () => {} }) assert.ok(proxy.test) @@ -68,21 +77,4 @@ describe('# Network Controller', function () { }) }) }) -}) - -function dummyProviderConstructor() { - return { - // provider - sendAsync: noop, - // block tracker - _blockTracker: {}, - start: noop, - stop: noop, - on: noop, - addListener: noop, - once: noop, - removeAllListeners: noop, - } -} - -function noop() {} \ No newline at end of file +}) \ No newline at end of file -- cgit From b2f53fa35481006cba37d89ebf4c7a866be4f125 Mon Sep 17 00:00:00 2001 From: Thomas Date: Tue, 27 Feb 2018 15:50:49 -0800 Subject: Revert initializing first-time-state --- test/unit/metamask-controller-test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'test/unit') diff --git a/test/unit/metamask-controller-test.js b/test/unit/metamask-controller-test.js index ac984faf5..adeca9b5f 100644 --- a/test/unit/metamask-controller-test.js +++ b/test/unit/metamask-controller-test.js @@ -4,7 +4,7 @@ const clone = require('clone') const nock = require('nock') const MetaMaskController = require('../../app/scripts/metamask-controller') const blacklistJSON = require('../stub/blacklist') -const firstTimeState = require('../stub/first-time-state') +const firstTimeState = require('../../app/scripts/first-time-state') describe('MetaMaskController', function () { let metamaskController @@ -18,9 +18,9 @@ describe('MetaMaskController', function () { .get('/v2/blacklist') .reply(200, blacklistJSON) - nock('https://rinkeby.infura.io') + nock('https://api.infura.io') .persist() - .post('/metamask') + .get(/.*/) .reply(200) metamaskController = new MetaMaskController({ -- cgit From f9de87af51ccb1190ca93e524de24f8a32ea3d9e Mon Sep 17 00:00:00 2001 From: Thomas Date: Tue, 27 Feb 2018 15:51:14 -0800 Subject: Using noop to not lose it --- test/unit/network-contoller-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/unit') diff --git a/test/unit/network-contoller-test.js b/test/unit/network-contoller-test.js index cd8c345c5..51ad09f87 100644 --- a/test/unit/network-contoller-test.js +++ b/test/unit/network-contoller-test.js @@ -10,7 +10,7 @@ describe('# Network Controller', function () { let networkController const noop = () => {} const networkControllerProviderInit = { - getAccounts: () => {}, + getAccounts: noop, } beforeEach(function () { -- cgit From cad5f5f0009713a1101194d964af7a1620ea158c Mon Sep 17 00:00:00 2001 From: Jeffrey Tong Date: Wed, 7 Mar 2018 10:24:36 -0800 Subject: fix typo in tests and tx-state-manager --- test/unit/message-manager-test.js | 4 ++-- test/unit/tx-state-manager-test.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'test/unit') diff --git a/test/unit/message-manager-test.js b/test/unit/message-manager-test.js index 9b76241ed..5e7039841 100644 --- a/test/unit/message-manager-test.js +++ b/test/unit/message-manager-test.js @@ -1,11 +1,11 @@ const assert = require('assert') -const MessageManger = require('../../app/scripts/lib/message-manager') +const MessageManager = require('../../app/scripts/lib/message-manager') describe('Message Manager', function () { let messageManager beforeEach(function () { - messageManager = new MessageManger() + messageManager = new MessageManager() }) describe('#getMsgList', function () { diff --git a/test/unit/tx-state-manager-test.js b/test/unit/tx-state-manager-test.js index 02dc52967..220bf501f 100644 --- a/test/unit/tx-state-manager-test.js +++ b/test/unit/tx-state-manager-test.js @@ -5,7 +5,7 @@ const TxStateManager = require('../../app/scripts/lib/tx-state-manager') const txStateHistoryHelper = require('../../app/scripts/lib/tx-state-history-helper') const noop = () => true -describe('TransactionStateManger', function () { +describe('TransactionStateManager', function () { let txStateManager const currentNetworkId = 42 const otherNetworkId = 2 @@ -281,4 +281,4 @@ describe('TransactionStateManger', function () { }) }) -}) \ No newline at end of file +}) -- cgit