From f4f4114865e39fa8c1442ea47b8f7feca718af33 Mon Sep 17 00:00:00 2001 From: Matias Pando Date: Tue, 27 Jan 2015 18:04:46 -0300 Subject: [PATCH] Add test on TxProposal --- js/models/Wallet.js | 4 -- test/PublicKeyRing.js | 48 +++++++++++++++++ test/TxProposal.js | 4 ++ test/TxProposals.js | 121 +++++++++++++++++++++++++++++++++++++++++- 4 files changed, 172 insertions(+), 5 deletions(-) diff --git a/js/models/Wallet.js b/js/models/Wallet.js index df05c3ae0..02e53b39e 100644 --- a/js/models/Wallet.js +++ b/js/models/Wallet.js @@ -713,10 +713,6 @@ Wallet.prototype._onData = function(senderId, data, ts) { this.updateSyncedTimestamp(ts); - console.log('data.type ', data.type); - console.log('this.id ', this.id); - console.log('data.walletId ', data.walletId); - if (data.type !== 'walletId' && this.id !== data.walletId) { log.debug('Wallet:' + this.id + ' Received corrupt message:', data) this.emitAndKeepAlive('corrupt', senderId); diff --git a/test/PublicKeyRing.js b/test/PublicKeyRing.js index d6ee15443..9d950fcfe 100644 --- a/test/PublicKeyRing.js +++ b/test/PublicKeyRing.js @@ -553,6 +553,54 @@ describe('PublicKeyRing model', function() { w.myCopayerId().should.be.equal(w.getCopayerId(0)); }); + it('#_checkKeys should throw error is not complete', function() { + var config = { + networkName: 'livenet', + }; + var w2 = new PublicKeyRing(config); + + (function() { + return w2._checkKeys(); + }).should.throw('dont have required keys'); + + }); + + it('#pathForAddress', function() { + var k = getCachedW(); + var w = k.w; + var addr = w.generateAddress(true, k.pub); + + var path = w.pathForAddress(addr); + + path.should.not.be.undefined; + + (function() { + return w.pathForAddress('abcd'); + }).should.throw('find path for address'); + + + }); + + + it('#copayersForPubkeys', function() { + var k = getCachedW(); + var w = k.w; + var addr = w.generateAddress(true, k.pub); + var path = w.pathForAddress(addr); + var paths = []; + paths.push(path); + + + + (function() { + return w.copayersForPubkeys(k.pub, paths); + }).should.throw('Pubkeys not identified'); + }); + + + + + }); diff --git a/test/TxProposal.js b/test/TxProposal.js index aa18dca63..97b7b5beb 100644 --- a/test/TxProposal.js +++ b/test/TxProposal.js @@ -728,6 +728,9 @@ describe('TxProposal', function() { }); + + + describe('micelaneous functions', function() { it('should report rejectCount', function() { var txp = dummyProposal(); @@ -756,4 +759,5 @@ describe('TxProposal', function() { }); + }); diff --git a/test/TxProposals.js b/test/TxProposals.js index f01a084ae..adca7c344 100644 --- a/test/TxProposals.js +++ b/test/TxProposals.js @@ -17,7 +17,9 @@ var dummyProposal = new TxProposal({ creator: 1, createdTs: 1, builder: { - toObj: sinon.stub().returns({}), + toObj: sinon.stub().returns({ + getId: sinon.stub().returns('1234') + }), }, inputChainPaths: ['m/1'], }); @@ -42,6 +44,66 @@ describe('TxProposals', function() { should.exist(txps); txps.network.name.should.equal('livenet'); }); + it('should create an instance from an Object using builder', function() { + + function dummyBuilder(opts) { + opts = opts || {}; + + var index = opts.nsig ? opts.nsig - 1 : 1; + var script = SCRIPTSIG[index]; + + var aIn = { + s: script + }; + + var tx = {}; + tx.ins = opts.noins ? [] : [opts.nosigs ? {} : aIn]; + + tx.serialize = sinon.stub().returns(new Buffer('1234', 'hex')); + tx.getSize = sinon.stub().returns(1); + tx.getHashType = sinon.stub().returns(opts.hashtype || 1); + tx.getNormalizedHash = sinon.stub().returns('123456'); + tx.hashForSignature = sinon.stub().returns( + new Buffer('31103626e162f1cbfab6b95b08c9f6e78aae128523261cb37f8dfd4783cb09a7', 'hex')); + tx.getId = sinon.returns(tx.getNormalizedHash().toString('hex')); + + var builder = {}; + + builder.opts = opts.opts || {}; + builder.build = sinon.stub().returns(tx) + builder.toObj = sinon.stub().returns({ + iAmBuilderObj: true, + version: 1, + opts: builder.opts, + }); + builder.isFullySigned = sinon.stub().returns(false); + + builder.vanilla = { + scriptSig: [SCRIPTSIG[1]], + outs: JSON.stringify([{ + address: '2NDJbzwzsmRgD2o5HHXPhuq5g6tkKTjYkd6', + amountSatStr: '123', + }]), + }; + builder.inputsSigned = 0; + + return builder; + }; + + var txps1 = []; + txps1.push(dummyProposal); + + var txps = TxProposals.fromObj({ + networkName: 'testnet', + walletId: '123a12', + txps: txps1, + builder: dummyBuilder + }); + should.exist(txps); + }); + + + it('should skip Objects with errors', function() { var txps = TxProposals.fromObj({ networkName: 'livenet', @@ -131,6 +193,63 @@ describe('TxProposals', function() { }).should.throw('Unknown TXP: c'); }); }); + + describe('#deletePending', function() { + it('should delete pending proposals', function() { + var txps = new TxProposals(); + txps.txps = { + a: { + isPending: sinon.stub().returns(true) + }, + b: { + isPending: sinon.stub().returns(false) + }, + }; + txps.deletePending(2); + txps.getNtxids().should.deep.equal(['b']); + }); + }); + + describe('#getUsedUnspent', function() { + it('should return an empty object', function() { + var txps = new TxProposals(); + + console.log(txps); + txps.txps = { + a: { + isPending: sinon.stub().returns(false) + }, + b: { + isPending: sinon.stub().returns(false) + }, + }; + var r = txps.getUsedUnspent(2); + Object.keys(r).length.should.equal(0); + + }); + + it('should return an non empty object', function() { + var txps = new TxProposals(); + txps.txps = { + a: { + isPending: sinon.stub().returns(true), + builder: { + getSelectedUnspent: sinon.stub().returns([{ + txid: 'a1', + vout: '00' + }]) + } + }, + b: { + isPending: sinon.stub().returns(false) + }, + }; + var r = txps.getUsedUnspent(2); + Object.keys(r).length.should.equal(1); + + }); + }); + describe('#toObj', function() { it('should an object', function() { var txps = TxProposals.fromObj({