diff options
Merge branch 'master' into NewUI
Diffstat (limited to 'test/unit')
-rw-r--r-- | test/unit/infura-controller-test.js | 70 | ||||
-rw-r--r-- | test/unit/nodeify-test.js | 2 | ||||
-rw-r--r-- | test/unit/nonce-tracker-test.js | 40 | ||||
-rw-r--r-- | test/unit/tx-controller-test.js | 75 | ||||
-rw-r--r-- | test/unit/tx-helper-test.js | 17 | ||||
-rw-r--r-- | test/unit/tx-utils-test.js | 38 |
6 files changed, 198 insertions, 44 deletions
diff --git a/test/unit/infura-controller-test.js b/test/unit/infura-controller-test.js index 7a2a114f9..605305efa 100644 --- a/test/unit/infura-controller-test.js +++ b/test/unit/infura-controller-test.js @@ -1,33 +1,61 @@ -// polyfill fetch -global.fetch = function () {return Promise.resolve({ - json: () => { return Promise.resolve({"mainnet": "ok", "ropsten": "degraded", "kovan": "down", "rinkeby": "ok"}) }, - }) -} const assert = require('assert') +const sinon = require('sinon') const InfuraController = require('../../app/scripts/controllers/infura') describe('infura-controller', function () { - var infuraController + let infuraController, sandbox, networkStatus + const response = {'mainnet': 'degraded', 'ropsten': 'ok', 'kovan': 'ok', 'rinkeby': 'down'} - beforeEach(function () { + before(async function () { infuraController = new InfuraController() + sandbox = sinon.sandbox.create() + sinon.stub(infuraController, 'checkInfuraNetworkStatus').resolves(response) + networkStatus = await infuraController.checkInfuraNetworkStatus() + }) + + after(function () { + sandbox.restore() }) - describe('network status queries', function () { - describe('#checkInfuraNetworkStatus', function () { - it('should return an object reflecting the network statuses', function (done) { - this.timeout(15000) - infuraController.checkInfuraNetworkStatus() - .then(() => { - const networkStatus = infuraController.store.getState().infuraNetworkStatus - assert.equal(Object.keys(networkStatus).length, 4) - assert.equal(networkStatus.mainnet, 'ok') - assert.equal(networkStatus.ropsten, 'degraded') - assert.equal(networkStatus.kovan, 'down') - }) - .then(() => done()) - .catch(done) + describe('Network status queries', function () { + + describe('Mainnet', function () { + it('should have Mainnet', function () { + assert.equal(Object.keys(networkStatus)[0], 'mainnet') + }) + + it('should have a value for Mainnet status', function () { + assert.equal(networkStatus.mainnet, 'degraded') + }) + }) + + describe('Ropsten', function () { + it('should have Ropsten', function () { + assert.equal(Object.keys(networkStatus)[1], 'ropsten') + }) + + it('should have a value for Ropsten status', function () { + assert.equal(networkStatus.ropsten, 'ok') + }) + }) + + describe('Kovan', function () { + it('should have Kovan', function () { + assert.equal(Object.keys(networkStatus)[2], 'kovan') + }) + + it('should have a value for Kovan status', function () { + assert.equal(networkStatus.kovan, 'ok') + }) + }) + + describe('Rinkeby', function () { + it('should have Rinkeby', function () { + assert.equal(Object.keys(networkStatus)[3], 'rinkeby') + }) + it('should have a value for Rinkeby status', function () { + assert.equal(networkStatus.rinkeby, 'down') }) }) }) diff --git a/test/unit/nodeify-test.js b/test/unit/nodeify-test.js index 5aed758fa..06241334d 100644 --- a/test/unit/nodeify-test.js +++ b/test/unit/nodeify-test.js @@ -11,7 +11,7 @@ describe('nodeify', function () { } it('should retain original context', function (done) { - var nodified = nodeify(obj.promiseFunc).bind(obj) + var nodified = nodeify(obj.promiseFunc, obj) nodified('baz', function (err, res) { assert.equal(res, 'barbaz') done() diff --git a/test/unit/nonce-tracker-test.js b/test/unit/nonce-tracker-test.js new file mode 100644 index 000000000..16cd6d008 --- /dev/null +++ b/test/unit/nonce-tracker-test.js @@ -0,0 +1,40 @@ +const assert = require('assert') +const NonceTracker = require('../../app/scripts/lib/nonce-tracker') + +describe('Nonce Tracker', function () { + let nonceTracker, provider, getPendingTransactions, pendingTxs + + + beforeEach(function () { + pendingTxs = [{ + 'status': 'submitted', + 'txParams': { + 'from': '0x7d3517b0d011698406d6e0aed8453f0be2697926', + 'gas': '0x30d40', + 'value': '0x0', + 'nonce': '0x0', + }, + }] + + + getPendingTransactions = () => pendingTxs + provider = { sendAsync: (_, cb) => { cb(undefined, {result: '0x0'}) } } + nonceTracker = new NonceTracker({ + blockTracker: { + getCurrentBlock: () => '0x11b568', + }, + provider, + getPendingTransactions, + }) + }) + + describe('#getNonceLock', function () { + it('should work', async function (done) { + this.timeout(15000) + const nonceLock = await nonceTracker.getNonceLock('0x7d3517b0d011698406d6e0aed8453f0be2697926') + assert.equal(nonceLock.nextNonce, '1', 'nonce should be 1') + await nonceLock.releaseLock() + done() + }) + }) +}) diff --git a/test/unit/tx-controller-test.js b/test/unit/tx-controller-test.js index 0d35cd62c..7b86cfe14 100644 --- a/test/unit/tx-controller-test.js +++ b/test/unit/tx-controller-test.js @@ -1,5 +1,4 @@ const assert = require('assert') -const EventEmitter = require('events') const ethUtil = require('ethereumjs-util') const EthTx = require('ethereumjs-tx') const EthQuery = require('eth-query') @@ -19,14 +18,16 @@ describe('Transaction Controller', function () { txController = new TransactionController({ networkStore: new ObservableStore(currentNetworkId), txHistoryLimit: 10, - provider: { _blockTracker: new EventEmitter()}, - blockTracker: new EventEmitter(), - ethQuery: new EthQuery(new EventEmitter()), + blockTracker: { getCurrentBlock: noop, on: noop, once: noop }, + provider: { sendAsync: noop }, + ethQuery: new EthQuery({ sendAsync: noop }), + ethStore: { getState: noop }, signTransaction: (ethTx) => new Promise((resolve) => { ethTx.sign(privKey) resolve() }), }) + txController.nonceTracker.getNonceLock = () => Promise.resolve({ nextNonce: 0, releaseLock: noop }) }) describe('#validateTxParams', function () { @@ -270,56 +271,86 @@ describe('Transaction Controller', function () { it('does not overwrite set values', function (done) { + this.timeout(15000) const wrongValue = '0x05' txController.addTx(txMeta) const estimateStub = sinon.stub(txController.txProviderUtils.query, 'estimateGas') - .callsArgWith(1, null, wrongValue) + .callsArgWithAsync(1, null, wrongValue) const priceStub = sinon.stub(txController.txProviderUtils.query, 'gasPrice') - .callsArgWith(0, null, wrongValue) + .callsArgWithAsync(0, null, wrongValue) - const nonceStub = sinon.stub(txController.txProviderUtils.query, 'getTransactionCount') - .callsArgWith(2, null, wrongValue) - const signStub = sinon.stub(txController, 'signTransaction') - .callsArgWith(1, null, noop) + const signStub = sinon.stub(txController, 'signTransaction', () => Promise.resolve()) - const pubStub = sinon.stub(txController.txProviderUtils, 'publishTransaction') - .callsArgWith(1, null, originalValue) - - txController.approveTransaction(txMeta.id, (err) => { - assert.ifError(err, 'should not error') + const pubStub = sinon.stub(txController.txProviderUtils, 'publishTransaction', () => Promise.resolve(originalValue)) + txController.approveTransaction(txMeta.id).then(() => { const result = txController.getTx(txMeta.id) const params = result.txParams assert.equal(params.gas, originalValue, 'gas unmodified') assert.equal(params.gasPrice, originalValue, 'gas price unmodified') - assert.equal(params.nonce, originalValue, 'nonce unmodified') - assert.equal(result.hash, originalValue, 'hash was set') + assert.equal(result.hash, originalValue, `hash was set \n got: ${result.hash} \n expected: ${originalValue}`) estimateStub.restore() priceStub.restore() signStub.restore() - nonceStub.restore() pubStub.restore() - done() - }) + }).catch(done) }) }) describe('#sign replay-protected tx', function () { it('prepares a tx with the chainId set', function (done) { txController.addTx({ id: '1', status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }, noop) - txController.signTransaction('1', (err, rawTx) => { - if (err) return done('it should not fail') + txController.signTransaction('1').then((rawTx) => { const ethTx = new EthTx(ethUtil.toBuffer(rawTx)) assert.equal(ethTx.getChainId(), currentNetworkId) done() + }).catch(done) + }) + }) + + describe('#_resubmitTx with a too-low balance', function () { + it('should fail the transaction', function (done) { + const from = '0xda0da0' + const txMeta = { + id: 1, + status: 'submitted', + metamaskNetworkId: currentNetworkId, + txParams: { + from, + nonce: '0x1', + value: '0xfffff', + }, + } + + const lowBalance = '0x0' + const fakeStoreState = { accounts: {} } + fakeStoreState.accounts[from] = { + balance: lowBalance, + nonce: '0x0', + } + + // Stubbing out current account state: + const getStateStub = sinon.stub(txController.ethStore, 'getState') + .returns(fakeStoreState) + + // Adding the fake tx: + txController.addTx(clone(txMeta)) + + txController._resubmitTx(txMeta, function (err) { + assert.ifError(err, 'should not throw an error') + const updatedMeta = txController.getTx(txMeta.id) + assert.notEqual(updatedMeta.status, txMeta.status, 'status changed.') + assert.equal(updatedMeta.status, 'failed', 'tx set to failed.') + done() }) }) }) }) + diff --git a/test/unit/tx-helper-test.js b/test/unit/tx-helper-test.js new file mode 100644 index 000000000..cc6543c30 --- /dev/null +++ b/test/unit/tx-helper-test.js @@ -0,0 +1,17 @@ +const assert = require('assert') +const txHelper = require('../../ui/lib/tx-helper') + +describe('txHelper', function () { + it('always shows the oldest tx first', function () { + const metamaskNetworkId = 1 + const txs = { + a: { metamaskNetworkId, time: 3 }, + b: { metamaskNetworkId, time: 1 }, + c: { metamaskNetworkId, time: 2 }, + } + + const sorted = txHelper(txs, null, null, metamaskNetworkId) + assert.equal(sorted[0].time, 1, 'oldest tx first') + assert.equal(sorted[2].time, 3, 'newest tx last') + }) +}) diff --git a/test/unit/tx-utils-test.js b/test/unit/tx-utils-test.js index 7ace1f587..a43bcfb35 100644 --- a/test/unit/tx-utils-test.js +++ b/test/unit/tx-utils-test.js @@ -16,6 +16,44 @@ describe('txUtils', function () { })) }) + describe('#sufficientBalance', function () { + it('returns true if max tx cost is equal to balance.', function () { + const tx = { + 'value': '0x1', + 'gas': '0x2', + 'gasPrice': '0x3', + } + const balance = '0x8' + + const result = txUtils.sufficientBalance(tx, balance) + assert.ok(result, 'sufficient balance found.') + }) + + it('returns true if max tx cost is less than balance.', function () { + const tx = { + 'value': '0x1', + 'gas': '0x2', + 'gasPrice': '0x3', + } + const balance = '0x9' + + const result = txUtils.sufficientBalance(tx, balance) + assert.ok(result, 'sufficient balance found.') + }) + + it('returns false if max tx cost is more than balance.', function () { + const tx = { + 'value': '0x1', + 'gas': '0x2', + 'gasPrice': '0x3', + } + const balance = '0x6' + + const result = txUtils.sufficientBalance(tx, balance) + assert.ok(!result, 'insufficient balance found.') + }) + }) + describe('chain Id', function () { it('prepares a transaction with the provided chainId', function () { const txParams = { |