aboutsummaryrefslogtreecommitdiffstats
path: root/test/unit
diff options
context:
space:
mode:
authorsdtsui <szehungdanieltsui@gmail.com>2017-08-07 10:55:34 +0800
committersdtsui <szehungdanieltsui@gmail.com>2017-08-07 10:55:34 +0800
commit02c2106c3bb6900801cf5ffe4a1d9bd46183b089 (patch)
tree23f6d76a57c7b58a385ee49e8a24e402ed474775 /test/unit
parentfd36d95c506db55afa33a251c6c187c194a55854 (diff)
parent2ba5737728d2539fc3bc9015e440f37341219cdc (diff)
downloadtangerine-wallet-browser-02c2106c3bb6900801cf5ffe4a1d9bd46183b089.tar
tangerine-wallet-browser-02c2106c3bb6900801cf5ffe4a1d9bd46183b089.tar.gz
tangerine-wallet-browser-02c2106c3bb6900801cf5ffe4a1d9bd46183b089.tar.bz2
tangerine-wallet-browser-02c2106c3bb6900801cf5ffe4a1d9bd46183b089.tar.lz
tangerine-wallet-browser-02c2106c3bb6900801cf5ffe4a1d9bd46183b089.tar.xz
tangerine-wallet-browser-02c2106c3bb6900801cf5ffe4a1d9bd46183b089.tar.zst
tangerine-wallet-browser-02c2106c3bb6900801cf5ffe4a1d9bd46183b089.zip
Merge branch 'master' into feat/mm-ui-5
Diffstat (limited to 'test/unit')
-rw-r--r--test/unit/actions/tx_test.js10
-rw-r--r--test/unit/blacklist-controller-test.js41
-rw-r--r--test/unit/blacklister-test.js24
-rw-r--r--test/unit/nodeify-test.js11
-rw-r--r--test/unit/tx-controller-test.js164
5 files changed, 197 insertions, 53 deletions
diff --git a/test/unit/actions/tx_test.js b/test/unit/actions/tx_test.js
index 0ea1bfdc7..67c72e9a5 100644
--- a/test/unit/actions/tx_test.js
+++ b/test/unit/actions/tx_test.js
@@ -45,13 +45,15 @@ describe('tx confirmation screen', function () {
before(function (done) {
actions._setBackgroundConnection({
approveTransaction (txId, cb) { cb('An error!') },
- cancelTransaction (txId) { /* noop */ },
+ cancelTransaction (txId, cb) { cb() },
clearSeedWordCache (cb) { cb() },
})
- const action = actions.cancelTx({value: firstTxId})
- result = reducers(initialState, action)
- done()
+ actions.cancelTx({value: firstTxId})((action) => {
+ result = reducers(initialState, action)
+ done()
+ })
+
})
it('should transition to the account detail view', function () {
diff --git a/test/unit/blacklist-controller-test.js b/test/unit/blacklist-controller-test.js
new file mode 100644
index 000000000..a9260466f
--- /dev/null
+++ b/test/unit/blacklist-controller-test.js
@@ -0,0 +1,41 @@
+const assert = require('assert')
+const BlacklistController = require('../../app/scripts/controllers/blacklist')
+
+describe('blacklist controller', function () {
+ let blacklistController
+
+ before(() => {
+ blacklistController = new BlacklistController()
+ })
+
+ describe('checkForPhishing', function () {
+ it('should not flag whitelisted values', function () {
+ const result = blacklistController.checkForPhishing('www.metamask.io')
+ assert.equal(result, false)
+ })
+ it('should flag explicit values', function () {
+ const result = blacklistController.checkForPhishing('metamask.com')
+ assert.equal(result, true)
+ })
+ it('should flag levenshtein values', function () {
+ const result = blacklistController.checkForPhishing('metmask.io')
+ assert.equal(result, true)
+ })
+ it('should not flag not-even-close values', function () {
+ const result = blacklistController.checkForPhishing('example.com')
+ assert.equal(result, false)
+ })
+ it('should not flag the ropsten faucet domains', function () {
+ const result = blacklistController.checkForPhishing('faucet.metamask.io')
+ assert.equal(result, false)
+ })
+ it('should not flag the mascara domain', function () {
+ const result = blacklistController.checkForPhishing('zero.metamask.io')
+ assert.equal(result, false)
+ })
+ it('should not flag the mascara-faucet domain', function () {
+ const result = blacklistController.checkForPhishing('zero-faucet.metamask.io')
+ assert.equal(result, false)
+ })
+ })
+}) \ No newline at end of file
diff --git a/test/unit/blacklister-test.js b/test/unit/blacklister-test.js
deleted file mode 100644
index 1badc2c8f..000000000
--- a/test/unit/blacklister-test.js
+++ /dev/null
@@ -1,24 +0,0 @@
-const assert = require('assert')
-const isPhish = require('../../app/scripts/lib/is-phish')
-
-describe('blacklister', function () {
- describe('#isPhish', function () {
- it('should not flag whitelisted values', function () {
- var result = isPhish({ hostname: 'www.metamask.io' })
- assert(!result)
- })
- it('should flag explicit values', function () {
- var result = isPhish({ hostname: 'metamask.com' })
- assert(result)
- })
- it('should flag levenshtein values', function () {
- var result = isPhish({ hostname: 'metmask.com' })
- assert(result)
- })
- it('should not flag not-even-close values', function () {
- var result = isPhish({ hostname: 'example.com' })
- assert(!result)
- })
- })
-})
-
diff --git a/test/unit/nodeify-test.js b/test/unit/nodeify-test.js
index 06241334d..537dae605 100644
--- a/test/unit/nodeify-test.js
+++ b/test/unit/nodeify-test.js
@@ -17,4 +17,15 @@ describe('nodeify', function () {
done()
})
})
+
+ it('should throw if the last argument is not a function', function (done) {
+ const nodified = nodeify(obj.promiseFunc, obj)
+ try {
+ nodified('baz')
+ done(new Error('should have thrown if the last argument is not a function'))
+ } catch (err) {
+ assert.equal(err.message, 'callback is not a function')
+ done()
+ }
+ })
})
diff --git a/test/unit/tx-controller-test.js b/test/unit/tx-controller-test.js
index 31908569a..f290088a1 100644
--- a/test/unit/tx-controller-test.js
+++ b/test/unit/tx-controller-test.js
@@ -1,11 +1,11 @@
const assert = require('assert')
const ethUtil = require('ethereumjs-util')
const EthTx = require('ethereumjs-tx')
-const EthQuery = require('eth-query')
const ObservableStore = require('obs-store')
const clone = require('clone')
const sinon = require('sinon')
const TransactionController = require('../../app/scripts/controllers/transactions')
+const TxProvideUtils = require('../../app/scripts/lib/tx-utils')
const noop = () => true
const currentNetworkId = 42
const otherNetworkId = 36
@@ -20,7 +20,6 @@ describe('Transaction Controller', function () {
txHistoryLimit: 10,
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)
@@ -28,24 +27,147 @@ describe('Transaction Controller', function () {
}),
})
txController.nonceTracker.getNonceLock = () => Promise.resolve({ nextNonce: 0, releaseLock: noop })
+ txController.query = new Proxy({}, {
+ get: (queryStubResult, key) => {
+ if (key === 'stubResult') {
+ return function (method, ...args) {
+ queryStubResult[method] = args
+ }
+ } else {
+ const returnValues = queryStubResult[key]
+ return () => Promise.resolve(...returnValues)
+ }
+ },
+ })
+ txController.txProviderUtils = new TxProvideUtils(txController.query)
+ })
+
+ describe('#newUnapprovedTransaction', function () {
+ let stub, txMeta, txParams
+ beforeEach(function () {
+ txParams = {
+ 'from':'0xc684832530fcbddae4b4230a47e991ddcec2831d',
+ 'to':'0xc684832530fcbddae4b4230a47e991ddcec2831d',
+ },
+ txMeta = {
+ status: 'unapproved',
+ id: 1,
+ metamaskNetworkId: currentNetworkId,
+ txParams,
+ }
+ txController._saveTxList([txMeta])
+ stub = sinon.stub(txController, 'addUnapprovedTransaction').returns(Promise.resolve(txMeta))
+ })
+
+ afterEach(function () {
+ stub.restore()
+ })
+
+ it('should emit newUnaprovedTx event and pass txMeta as the first argument', function (done) {
+ txController.once('newUnaprovedTx', (txMetaFromEmit) => {
+ assert(txMetaFromEmit, 'txMeta is falsey')
+ assert.equal(txMetaFromEmit.id, 1, 'the right txMeta was passed')
+ done()
+ })
+ txController.newUnapprovedTransaction(txParams)
+ .catch(done)
+ })
+
+ it('should resolve when finished and status is submitted and resolve with the hash', function (done) {
+ txController.once('newUnaprovedTx', (txMetaFromEmit) => {
+ setTimeout(() => {
+ console.log('HELLLO')
+ txController.setTxHash(txMetaFromEmit.id, '0x0')
+ txController.setTxStatusSubmitted(txMetaFromEmit.id)
+ }, 10)
+ })
+
+ txController.newUnapprovedTransaction(txParams)
+ .then((hash) => {
+ assert(hash, 'newUnapprovedTransaction needs to return the hash')
+ done()
+ })
+ .catch(done)
+ })
+
+ it('should reject when finished and status is rejected', function (done) {
+ txController.once('newUnaprovedTx', (txMetaFromEmit) => {
+ setTimeout(() => {
+ console.log('HELLLO')
+ txController.setTxStatusRejected(txMetaFromEmit.id)
+ }, 10)
+ })
+
+ txController.newUnapprovedTransaction(txParams)
+ .catch((err) => {
+ if (err.message === 'MetaMask Tx Signature: User denied transaction signature.') done()
+ else done(err)
+ })
+ })
+ })
+
+ describe('#addUnapprovedTransaction', function () {
+ it('should add an unapproved transaction and return a valid txMeta', function (done) {
+ const addTxDefaultsStub = sinon.stub(txController, 'addTxDefaults').callsFake(() => Promise.resolve())
+ txController.addUnapprovedTransaction({})
+ .then((txMeta) => {
+ assert(('id' in txMeta), 'should have a id')
+ assert(('time' in txMeta), 'should have a time stamp')
+ assert(('metamaskNetworkId' in txMeta), 'should have a metamaskNetworkId')
+ assert(('txParams' in txMeta), 'should have a txParams')
+ assert(('history' in txMeta), 'should have a history')
+
+ const memTxMeta = txController.getTx(txMeta.id)
+ assert.deepEqual(txMeta, memTxMeta, `txMeta should be stored in txController after adding it\n expected: ${txMeta} \n got: ${memTxMeta}`)
+ addTxDefaultsStub.restore()
+ done()
+ }).catch(done)
+ })
+ })
+
+ describe('#addTxDefaults', function () {
+ it('should add the tx defaults if their are none', function (done) {
+ let txMeta = {
+ 'txParams': {
+ 'from':'0xc684832530fcbddae4b4230a47e991ddcec2831d',
+ 'to':'0xc684832530fcbddae4b4230a47e991ddcec2831d',
+ },
+ }
+
+ txController.query.stubResult('gasPrice', '0x4a817c800')
+ txController.query.stubResult('getBlockByNumber', { gasLimit: '0x47b784' })
+ txController.query.stubResult('estimateGas', '0x5209')
+
+ txController.addTxDefaults(txMeta)
+ .then((txMetaWithDefaults) => {
+ assert(txMetaWithDefaults.txParams.value, '0x0','should have added 0x0 as the value')
+ assert(txMetaWithDefaults.txParams.gasPrice, 'should have added the gas price')
+ assert(txMetaWithDefaults.txParams.gas, 'should have added the gas field')
+ done()
+ })
+ .catch(done)
+ })
})
describe('#validateTxParams', function () {
- it('returns null for positive values', function () {
+ it('does not throw for positive values', function (done) {
var sample = {
value: '0x01',
}
- txController.txProviderUtils.validateTxParams(sample, (err) => {
- assert.equal(err, null, 'no error')
- })
+ txController.txProviderUtils.validateTxParams(sample).then(() => {
+ done()
+ }).catch(done)
})
- it('returns error for negative values', function () {
+ it('returns error for negative values', function (done) {
var sample = {
value: '-0x01',
}
- txController.txProviderUtils.validateTxParams(sample, (err) => {
+ txController.txProviderUtils.validateTxParams(sample)
+ .then(() => done('expected to thrown on negativity values but didn\'t'))
+ .catch((err) => {
assert.ok(err, 'error')
+ done()
})
})
})
@@ -56,9 +178,6 @@ describe('Transaction Controller', function () {
assert.ok(Array.isArray(result))
assert.equal(result.length, 0)
})
- it('should also return transactions from local storage if any', function () {
-
- })
})
describe('#addTx', function () {
@@ -276,16 +395,15 @@ describe('Transaction Controller', function () {
txController.addTx(txMeta)
- const estimateStub = sinon.stub(txController.txProviderUtils.query, 'estimateGas')
- .callsArgWithAsync(1, null, wrongValue)
+ txController.query.stubResult('estimateGas', wrongValue)
+ txController.query.stubResult('gasPrice', wrongValue)
- const priceStub = sinon.stub(txController.txProviderUtils.query, 'gasPrice')
- .callsArgWithAsync(0, null, wrongValue)
+ const signStub = sinon.stub(txController, 'signTransaction').callsFake(() => Promise.resolve())
-
- const signStub = sinon.stub(txController, 'signTransaction', () => Promise.resolve())
-
- const pubStub = sinon.stub(txController.txProviderUtils, 'publishTransaction', () => Promise.resolve(originalValue))
+ const pubStub = sinon.stub(txController, 'publishTransaction').callsFake(() => {
+ txController.setTxHash('1', originalValue)
+ txController.setTxStatusSubmitted('1')
+ })
txController.approveTransaction(txMeta.id).then(() => {
const result = txController.getTx(txMeta.id)
@@ -294,9 +412,6 @@ describe('Transaction Controller', function () {
assert.equal(params.gas, originalValue, 'gas unmodified')
assert.equal(params.gasPrice, originalValue, 'gas price unmodified')
assert.equal(result.hash, originalValue, `hash was set \n got: ${result.hash} \n expected: ${originalValue}`)
-
- estimateStub.restore()
- priceStub.restore()
signStub.restore()
pubStub.restore()
done()
@@ -352,9 +467,8 @@ describe('Transaction Controller', function () {
})
.catch((err) => {
assert.ifError(err, 'should not throw an error')
- done()
+ done(err)
})
})
})
-})
-
+}) \ No newline at end of file