From 4e3e76aecf7934e9b238b9132ff5e27b248e5dea Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Tue, 29 Dec 2015 16:36:51 -0300 Subject: [PATCH] make blockchain monitor component testable Conflicts: lib/blockchainmonitor.js --- lib/blockchainmonitor.js | 4 +- test/integration/bcmonitor.js | 90 +++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 test/integration/bcmonitor.js diff --git a/lib/blockchainmonitor.js b/lib/blockchainmonitor.js index 27dcb31..8545522 100644 --- a/lib/blockchainmonitor.js +++ b/lib/blockchainmonitor.js @@ -84,7 +84,7 @@ BlockchainMonitor.prototype._initExplorer = function(explorer) { socket.on('connect_error', function() { log.error('Error connecting to ' + explorer.getConnectionInfo()); }); - socket.on('tx', _.bind(self._handleIncommingTx, self)); + socket.on('tx', _.bind(self._handleIncomingTx, self)); socket.on('block', _.bind(self._handleNewBlock, self, explorer.network)); }; @@ -191,7 +191,7 @@ BlockchainMonitor.prototype._updateActiveAddresses = function(address, cb) { }); }; -BlockchainMonitor.prototype._handleIncommingTx = function(data) { +BlockchainMonitor.prototype._handleIncomingTx = function(data) { this._handleTxId(data); this._handleTxOuts(data); }; diff --git a/test/integration/bcmonitor.js b/test/integration/bcmonitor.js new file mode 100644 index 0000000..8d4781e --- /dev/null +++ b/test/integration/bcmonitor.js @@ -0,0 +1,90 @@ +'use strict'; + +var _ = require('lodash'); +var async = require('async'); + +var chai = require('chai'); +var sinon = require('sinon'); +var should = chai.should(); +var log = require('npmlog'); +log.debug = log.verbose; +log.level = 'info'; + +var WalletService = require('../../lib/server'); +var BlockchainMonitor = require('../../lib/blockchainmonitor'); + +var TestData = require('../testdata'); +var helpers = require('./helpers'); +var storage, blockchainExplorer; + +var socket = { + handlers: {}, +}; +socket.on = function(eventName, handler) { + this.handlers[eventName] = handler; +}; + +describe('Blockchain monitor', function() { + var server, wallet; + + before(function(done) { + helpers.before(done); + }); + after(function(done) { + helpers.after(done); + }); + beforeEach(function(done) { + helpers.beforeEach(function(res) { + storage = res.storage; + blockchainExplorer = res.blockchainExplorer; + blockchainExplorer.initSocket = sinon.stub().returns(socket); + + helpers.createAndJoinWallet(2, 3, function(s, w) { + server = s; + wallet = w; + + var bcmonitor = new BlockchainMonitor(); + bcmonitor.start({ + lockOpts: {}, + messageBroker: server.messageBroker, + storage: storage, + blockchainExplorers: { + 'testnet': blockchainExplorer, + 'livenet': blockchainExplorer + }, + }, function(err) { + should.not.exist(err); + done(); + }); + }); + }); + }); + + it('should notify copayers of incoming txs', function(done) { + server.createAddress({}, function(err, address) { + should.not.exist(err); + + var incoming = { + txid: '123', + vout: [{}], + }; + incoming.vout[0][address.address] = 1500; + socket.handlers['tx'](incoming); + + setTimeout(function() { + server.getNotifications({}, function(err, notifications) { + should.not.exist(err); + var notification = _.find(notifications, { + type: 'NewIncomingTx' + }); + should.exist(notification); + notification.walletId.should.equal(wallet.id); + notification.data.txid.should.equal('123'); + notification.data.address.should.equal(address.address); + notification.data.amount.should.equal(1500); + done(); + }); + }, 100); + }); + }); +});