diff --git a/js/models/RateService.js b/js/models/RateService.js index aec56366d..de0072375 100644 --- a/js/models/RateService.js +++ b/js/models/RateService.js @@ -106,7 +106,10 @@ RateService.prototype.getHistoricRates = function(code, dates, cb) { }, function(err, res, body) { if (err || res.statusCode != 200 || !body) return cb(err || res); if (!_.isArray(body)) { - body = [{ ts: dates[0], rate: body.rate }]; + body = [{ + ts: dates[0], + rate: body.rate + }]; } return cb(null, body); }); diff --git a/test/RateService.js b/test/RateService.js index db2445976..fa68e2961 100644 --- a/test/RateService.js +++ b/test/RateService.js @@ -1,5 +1,6 @@ 'use strict'; +var moment = moment || require('moment'); var RateService = copay.RateService; describe('RateService model', function() { @@ -10,10 +11,10 @@ describe('RateService model', function() { describe('Fetching currencies', function() { var clock; - before(function () { + before(function() { clock = sinon.useFakeTimers(); }); - after(function () { + after(function() { clock.restore(); }); it('should retry fetching currencies on error', function() { @@ -81,7 +82,7 @@ describe('RateService model', function() { rs.isAvailable = sinon.stub().returns(false); (function() { rs.toFiat(10000, 'USD'); - }).should.throw; + }).should.throw('not available'); }); it('should return current valuation', function() { var rs = new RateService(); @@ -174,13 +175,122 @@ describe('RateService model', function() { }); }); + describe('#getHistoricRate', function() { + it('should return historic rate', function() { + var yesterday = moment().subtract(1, 'day'); + var reqStub = sinon.stub(); + reqStub.get = sinon.stub().yields(null, { + statusCode: 200 + }, { + ts: yesterday, + rate: 100 + }); + + var rs = new RateService({ + request: reqStub + }); + rs.isAvailable = sinon.stub().returns(true); + + var params = [{ + code: 'USD', + date: yesterday, + expected: '100.00' + }]; + + _.each(params, function(p) { + rs.getHistoricRate('USD', yesterday, function(err, rate) { + rate.toFixed(2).should.equal(p.expected); + }); + }); + }); + it('should return error', function() { + var yesterday = moment().subtract(1, 'day'); + var reqStub = sinon.stub(); + reqStub.get = sinon.stub().yields(null, { + statusCode: 500 + }); + + var rs = new RateService({ + request: reqStub + }); + rs.isAvailable = sinon.stub().returns(true); + + rs.getHistoricRate('USD', yesterday, function(err, rate) { + err.statusCode.should.equal(500); + }); + + }); + }); + + describe('#getHistoricRates', function() { + it('should return historic rates for many dates', function() { + var yesterday = moment().subtract(1, 'day'); + var lastWeekday = moment().subtract(7, 'day'); + var reqStub = sinon.stub(); + reqStub.get = sinon.stub().yields(null, { + statusCode: 200 + }, [{ + ts: lastWeekday, + rate: 90, + }, { + ts: yesterday, + rate: 100, + }, ]); + + var rs = new RateService({ + request: reqStub + }); + rs.isAvailable = sinon.stub().returns(true); + + var params = [{ + code: 'USD', + date: lastWeekday, + expected: '90.00' + }, { + code: 'USD', + date: yesterday, + expected: '100.00' + }]; + + var dates = [lastWeekday, yesterday]; + rs.getHistoricRates('USD', dates, function(err, r) { + r.length.should.equal(2); + }); + + //same input dates should return only two values + dates.push(lastWeekday); + dates.push(yesterday); + rs.getHistoricRates('USD', dates, function(err, r) { + r.length.should.equal(2); + }); + }); + + it('should return error', function() { + var yesterday = moment().subtract(1, 'day'); + var reqStub = sinon.stub(); + reqStub.get = sinon.stub().yields(null, { + statusCode: 500 + }); + + var rs = new RateService({ + request: reqStub + }); + rs.isAvailable = sinon.stub().returns(true); + + var dates = [yesterday, yesterday]; + rs.getHistoricRates('USD', dates, function(err, rate) { + err.statusCode.should.equal(500); + }); + }); + }); + describe('#fromFiat', function() { it('should throw error when unavailable', function() { var rs = new RateService(); rs.isAvailable = sinon.stub().returns(false); (function() { rs.fromFiat(300, 'USD'); - }).should.throw; + }).should.throw('not available'); }); it('should return current valuation', function() { var rs = new RateService(); @@ -217,9 +327,8 @@ describe('RateService model', function() { rs.isAvailable = sinon.stub().returns(false); (function() { rs.listAlternatives(); - }).should.throw; + }).should.throw('not available'); }); - it('should return list of available currencies', function() { var rs = new RateService(); rs.isAvailable = sinon.stub().returns(true); @@ -238,5 +347,23 @@ describe('RateService model', function() { list.length.should.equal(2); }); }); + describe('#getAlternatives', function() { + it('should return nothing when trying to get the list of available currencies', function() { + var rs = new RateService(); + rs.isAvailable = sinon.stub().returns(false); + rs.whenAvailable(function() {}); + rs.getAlternatives().length.should.equal(0); + }); + + }); + describe('#singleton', function() { + it('should create only one instance', function() { + var rs = RateService.singleton(); + rs.should.be.not.null; + var rs2 = RateService.singleton(); + rs2.should.be.not.null; + rs.should.deep.equal(rs2); + }); + }); }); }); diff --git a/test/Wallet.js b/test/Wallet.js index 2393973fc..972b547ff 100644 --- a/test/Wallet.js +++ b/test/Wallet.js @@ -173,6 +173,58 @@ describe('Wallet model', function() { (new bitcore.Address(w.generateAddress(true))).isValid().should.equal(true); }); + it('should check sizes', function() { + var opts = {}; + var w = cachedCreateW(); + var s = w.sizes(); + s.total.should.be.equal(1634); + }); + + it('should check pending proposals', function() { + var opts = {}; + var w = cachedCreateW(); + + var p = w.getPendingTxProposalsCount(); + p.pending.should.be.equal(0); + p.pendingForUs.should.be.equal(0); + }); + + it('should set version', function() { + var opts = {}; + var w = cachedCreateW(); + w.setVersion('0.123') + w.version.should.be.equal('0.123'); + }); + + it('should check pending proposals case 2', function(done) { + var w = createW2(null, 1); + var utxo = createUTXO(w); + w.blockchain.fixUnspent(utxo); + + w.spend({ + toAddress: toAddress, + amountSat: amountSatStr, + }, function(err, id, status) { + should.not.exist(err); + should.exist(id); + status.should.equal(Wallet.TX_PROPOSAL_SENT); + + w.network.send.calledOnce.should.equal(true); + console.log(w.network.send.getCall(0).args[1]); + w.network.send.getCall(0).args[1].type.should.equal("txProposal"); + should.exist(w.network.send.getCall(0).args[1].indexes); + should.exist(w.network.send.getCall(0).args[1].txProposal); + + var p = w.getPendingTxProposalsCount(); + p.pending.should.be.equal(1); + p.pendingForUs.should.be.equal(0); + + var p2 = w.getPendingTxProposals(); + p2.length.should.be.equal(1); + done(); + }); + }); + var unspentTest = [{ "address": "dummy", "scriptPubKey": "dummy", @@ -708,6 +760,28 @@ describe('Wallet model', function() { }); + it('should exportEncrypted', function() { + var w = createW2(); + var enc = w.exportEncrypted('', {}); + enc.length.should.equal(2405); + }); + + it('should close wallet', function(done) { + var w = createW2(); + w.network.removeAllListeners = sinon.stub(); + w.network.cleanUp = sinon.stub(); + w.blockchain.removeAllListeners = sinon.stub(); + w.blockchain.destroy = sinon.stub(); + + w.close(function() { + w.network.removeAllListeners.calledOnce.should.equal(true); + w.network.cleanUp.calledOnce.should.equal(true); + w.blockchain.removeAllListeners.calledOnce.should.equal(true); + w.network.cleanUp.calledOnce.should.equal(true); + done(); + }); + }); + // tx handling var createUTXO = function(w) {