From 884f2035648a8ab7ddbde9f911ed608670912752 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Mon, 20 Nov 2017 14:15:00 -0800 Subject: Add failing test for #2577 Seed Phrase Bug --- test/unit/metamask-controller-test.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'test/unit') diff --git a/test/unit/metamask-controller-test.js b/test/unit/metamask-controller-test.js index ef6cae758..548b5f87f 100644 --- a/test/unit/metamask-controller-test.js +++ b/test/unit/metamask-controller-test.js @@ -27,6 +27,24 @@ describe('MetaMaskController', function () { describe('Metamask Controller', function () { assert(metamaskController) + + describe('#createNewVaultAndKeychain', function () { + it('can only create new vault on keyringController once', async function () { + + const selectStub = sinon.stub(metamaskController, 'selectFirstIdentity') + + const expectation = sinon.mock(metamaskController.keyringController) + .expects('createNewVaultAndKeychain').once() + + const password = 'a-fake-password' + + const first = await metamaskController.createNewVaultAndKeychain(password) + const second = await metamaskController.createNewVaultAndKeychain(password) + + expectation.verify() + selectStub.reset() + }) + }) }) }) -- cgit From 764a5bac56ddc855f669495daee720288cbee200 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Mon, 20 Nov 2017 14:44:12 -0800 Subject: Get test passing --- test/unit/metamask-controller-test.js | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) (limited to 'test/unit') diff --git a/test/unit/metamask-controller-test.js b/test/unit/metamask-controller-test.js index 548b5f87f..e80198dda 100644 --- a/test/unit/metamask-controller-test.js +++ b/test/unit/metamask-controller-test.js @@ -11,6 +11,17 @@ describe('MetaMaskController', function () { unlockAccountMessage: noop, showUnapprovedTx: noop, platform: {}, + encryptor: { + encrypt: function(password, object) { + console.log('encrypting ', object) + this.object = object + return Promise.resolve() + }, + decrypt: function () { + console.log('decrypting') + return Promise.resolve(this.object) + } + }, // initial state initState: clone(firstTimeState), }) @@ -28,20 +39,30 @@ describe('MetaMaskController', function () { describe('Metamask Controller', function () { assert(metamaskController) + beforeEach(function () { + sinon.spy(metamaskController.keyringController, 'createNewVaultAndKeychain') + }) + + afterEach(function () { + metamaskController.keyringController.createNewVaultAndKeychain.restore() + }) + describe('#createNewVaultAndKeychain', function () { it('can only create new vault on keyringController once', async function () { const selectStub = sinon.stub(metamaskController, 'selectFirstIdentity') - const expectation = sinon.mock(metamaskController.keyringController) - .expects('createNewVaultAndKeychain').once() - const password = 'a-fake-password' const first = await metamaskController.createNewVaultAndKeychain(password) + console.log('FIRST ONE RETURNED:') + console.dir(first) const second = await metamaskController.createNewVaultAndKeychain(password) + console.log('SECOND ONE RETURNED:') + console.dir(second) + + assert(metamaskController.keyringController.createNewVaultAndKeychain.calledOnce) - expectation.verify() selectStub.reset() }) }) -- cgit From 8dd0093184cfade8fa75fcab588f4a0e660f39fb Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Mon, 20 Nov 2017 14:46:36 -0800 Subject: Remove logs --- test/unit/metamask-controller-test.js | 6 ------ 1 file changed, 6 deletions(-) (limited to 'test/unit') diff --git a/test/unit/metamask-controller-test.js b/test/unit/metamask-controller-test.js index e80198dda..fd420a70f 100644 --- a/test/unit/metamask-controller-test.js +++ b/test/unit/metamask-controller-test.js @@ -13,12 +13,10 @@ describe('MetaMaskController', function () { platform: {}, encryptor: { encrypt: function(password, object) { - console.log('encrypting ', object) this.object = object return Promise.resolve() }, decrypt: function () { - console.log('decrypting') return Promise.resolve(this.object) } }, @@ -55,11 +53,7 @@ describe('MetaMaskController', function () { const password = 'a-fake-password' const first = await metamaskController.createNewVaultAndKeychain(password) - console.log('FIRST ONE RETURNED:') - console.dir(first) const second = await metamaskController.createNewVaultAndKeychain(password) - console.log('SECOND ONE RETURNED:') - console.dir(second) assert(metamaskController.keyringController.createNewVaultAndKeychain.calledOnce) -- cgit From af8fcb67778fadd7056c0930eeb9d22eb28b0e69 Mon Sep 17 00:00:00 2001 From: Dan Date: Wed, 6 Dec 2017 14:10:11 -0330 Subject: Update resubmitPendingTxs tests. --- test/unit/pending-tx-test.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'test/unit') diff --git a/test/unit/pending-tx-test.js b/test/unit/pending-tx-test.js index 4b5170dfe..7069554f7 100644 --- a/test/unit/pending-tx-test.js +++ b/test/unit/pending-tx-test.js @@ -206,6 +206,7 @@ describe('PendingTransactionTracker', function () { }) describe('#resubmitPendingTxs', function () { + const blockStub = { number: '0x0' }; beforeEach(function () { const txMeta2 = txMeta3 = txMeta txList = [txMeta, txMeta2, txMeta3].map((tx) => { @@ -223,7 +224,7 @@ describe('PendingTransactionTracker', function () { Promise.all(txList.map((tx) => tx.processed)) .then((txCompletedList) => done()) .catch(done) - pendingTxTracker.resubmitPendingTxs() + pendingTxTracker.resubmitPendingTxs(blockStub) }) it('should not emit \'tx:failed\' if the txMeta throws a known txError', function (done) { knownErrors =[ @@ -250,7 +251,7 @@ describe('PendingTransactionTracker', function () { .then((txCompletedList) => done()) .catch(done) - pendingTxTracker.resubmitPendingTxs() + pendingTxTracker.resubmitPendingTxs(blockStub) }) it('should emit \'tx:warning\' if it encountered a real error', function (done) { pendingTxTracker.once('tx:warning', (txMeta, err) => { @@ -268,7 +269,7 @@ describe('PendingTransactionTracker', function () { .then((txCompletedList) => done()) .catch(done) - pendingTxTracker.resubmitPendingTxs() + pendingTxTracker.resubmitPendingTxs(blockStub) }) }) describe('#_resubmitTx', function () { -- cgit From 3356c15d04d947c92a2c19690384396651c352e5 Mon Sep 17 00:00:00 2001 From: Dan Date: Wed, 6 Dec 2017 14:11:14 -0330 Subject: Add tests for exponential backoff code in _resubmitTx --- test/unit/pending-tx-test.js | 80 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 63 insertions(+), 17 deletions(-) (limited to 'test/unit') diff --git a/test/unit/pending-tx-test.js b/test/unit/pending-tx-test.js index 7069554f7..393601a57 100644 --- a/test/unit/pending-tx-test.js +++ b/test/unit/pending-tx-test.js @@ -273,24 +273,70 @@ describe('PendingTransactionTracker', function () { }) }) describe('#_resubmitTx', function () { - it('should publishing the transaction', function (done) { - const enoughBalance = '0x100000' - pendingTxTracker.getBalance = (address) => { - assert.equal(address, txMeta.txParams.from, 'Should pass the address') - return enoughBalance - } - pendingTxTracker.publishTransaction = async (rawTx) => { - assert.equal(rawTx, txMeta.rawTx, 'Should pass the rawTx') - } + const mockFirstRetryBlockNumber = '0x1' + let txMetaToTestExponentialBackoff + + beforeEach(() => { + pendingTxTracker.getBalance = (address) => { + assert.equal(address, txMeta.txParams.from, 'Should pass the address') + return enoughBalance + } + pendingTxTracker.publishTransaction = async (rawTx) => { + assert.equal(rawTx, txMeta.rawTx, 'Should pass the rawTx') + } + sinon.spy(pendingTxTracker, 'publishTransaction') + + txMetaToTestExponentialBackoff = Object.assign({}, txMeta, { + retryCount: 4, + firstRetryBlockNumber: mockFirstRetryBlockNumber, + }) + }) - // Stubbing out current account state: - // Adding the fake tx: - pendingTxTracker._resubmitTx(txMeta) - .then(() => done()) - .catch((err) => { - assert.ifError(err, 'should not throw an error') - done(err) + afterEach(() => { + pendingTxTracker.publishTransaction.reset() + }) + + it('should publish the transaction', function (done) { + const enoughBalance = '0x100000' + + // Stubbing out current account state: + // Adding the fake tx: + pendingTxTracker._resubmitTx(txMeta) + .then(() => done()) + .catch((err) => { + assert.ifError(err, 'should not throw an error') + done(err) + }) + + assert.equal(pendingTxTracker.publishTransaction.callCount, 1, 'Should call publish transaction') + }) + + it('should not publish the transaction if the limit of retries has been exceeded', function (done) { + const enoughBalance = '0x100000' + const mockLatestBlockNumber = '0x5' + + pendingTxTracker._resubmitTx(txMetaToTestExponentialBackoff, mockLatestBlockNumber) + .then(() => done()) + .catch((err) => { + assert.ifError(err, 'should not throw an error') + done(err) + }) + + assert.equal(pendingTxTracker.publishTransaction.callCount, 0, 'Should NOT call publish transaction') + }) + + it('should publish the transaction if the number of blocks since last retry exceeds the last set limit', function (done) { + const enoughBalance = '0x100000' + const mockLatestBlockNumber = '0x11' + + pendingTxTracker._resubmitTx(txMetaToTestExponentialBackoff, mockLatestBlockNumber) + .then(() => done()) + .catch((err) => { + assert.ifError(err, 'should not throw an error') + done(err) + }) + + assert.equal(pendingTxTracker.publishTransaction.callCount, 1, 'Should call publish transaction') }) - }) }) }) -- cgit