From 86016de19d136a6a8fdbebdb472a31df04ee692b Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Tue, 3 Jun 2014 13:07:31 -0300 Subject: [PATCH] add new tests for new reconnect feature --- test/mocks/FakeNetwork.js | 20 +++++- test/test.Wallet.js | 134 ++++++++++++++++++++++---------------- 2 files changed, 98 insertions(+), 56 deletions(-) diff --git a/test/mocks/FakeNetwork.js b/test/mocks/FakeNetwork.js index 0245f5429..cfff1875b 100644 --- a/test/mocks/FakeNetwork.js +++ b/test/mocks/FakeNetwork.js @@ -7,8 +7,14 @@ function Network(opts) { Network.parent=EventEmitter; -Network.prototype.start = function(openCallback, opts) { +Network.prototype.start = function(opts, cb) { // start! :D + this.peer = { + options: { + token: "asd" + } + }; + if (cb) cb(); }; Network.prototype.send = function(peerIds, data, cb) { @@ -24,4 +30,16 @@ Network.prototype.disconnect = function(cb) { // disconect :c }; +Network.prototype.lockIncommingConnections = function() { + +}; + +Network.prototype.getPeer = function() { +}; +Network.prototype.connectToCopayers = function(cps) { +}; +Network.prototype.isOnline = function() { + return true; +}; + module.exports = require('soop')(Network); diff --git a/test/test.Wallet.js b/test/test.Wallet.js index d684de4a1..afec3e43f 100644 --- a/test/test.Wallet.js +++ b/test/test.Wallet.js @@ -2,14 +2,15 @@ var chai = chai || require('chai'); var should = chai.should(); +var sinon = require('sinon'); var copay = copay || require('../copay'); var Wallet = require('../js/models/core/Wallet'); -var Storage= require('./mocks/FakeStorage'); -var Network= require('./mocks/FakeNetwork'); -var Blockchain= copay.Insight; +var Storage = require('./mocks/FakeStorage'); +var Network = require('./mocks/FakeNetwork'); +var Blockchain = copay.Insight; -var addCopayers = function (w) { - for(var i=0; i<4; i++) { +var addCopayers = function(w) { + for (var i = 0; i < 4; i++) { w.publicKeyRing.addCopayer(); } }; @@ -28,15 +29,20 @@ describe('Wallet model', function() { networkName: 'testnet', }; - it('should fail to create an instance', function () { - (function(){new Wallet(config)}).should.throw(); + it('should fail to create an instance', function() { + (function() { + new Wallet(config) + }).should. + throw (); }); - var createW = function (netKey) { + var createW = function(netKey) { var c = JSON.parse(JSON.stringify(config)); - + if (netKey) c.netKey = netKey; - c.privateKey = new copay.PrivateKey({ networkName: c.networkName }); + c.privateKey = new copay.PrivateKey({ + networkName: c.networkName + }); c.publicKeyRing = new copay.PublicKeyRing({ networkName: c.networkName, @@ -49,18 +55,18 @@ describe('Wallet model', function() { c.txProposals = new copay.TxProposals({ networkName: c.networkName, }); - c.storage = new Storage(config.storage); - c.network = new Network(config.network); - c.blockchain = new Blockchain(config.blockchain); + c.storage = new Storage(config.storage); + c.network = new Network(config.network); + c.blockchain = new Blockchain(config.blockchain); c.networkName = config.networkName; - c.verbose = config.verbose; - c.version = '0.0.1'; + c.verbose = config.verbose; + c.version = '0.0.1'; return new Wallet(c); } - it('should create an instance', function () { + it('should create an instance', function() { var w = createW(); should.exist(w); w.publicKeyRing.walletId.should.equal(w.id); @@ -71,11 +77,11 @@ describe('Wallet model', function() { should.exist(w.privateKey); should.exist(w.txProposals); should.exist(w.netKey); - var b = new Buffer(w.netKey,'base64'); + var b = new Buffer(w.netKey, 'base64'); b.toString('hex').length.should.equal(16); }); - it('should provide some basic features', function () { + it('should provide some basic features', function() { var opts = {}; var w = createW(); addCopayers(w); @@ -83,28 +89,26 @@ describe('Wallet model', function() { w.publicKeyRing.isComplete().should.equal(true); }); - var unspentTest = [ - { + var unspentTest = [{ "address": "dummy", "scriptPubKey": "dummy", "txid": "2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc1", "vout": 1, "amount": 10, - "confirmations":7 - } - ]; + "confirmations": 7 + }]; - var createW2 = function (privateKeys) { + var createW2 = function(privateKeys) { var netKey = 'T0FbU2JLby0='; var w = createW(netKey); should.exist(w); - var pkr = w.publicKeyRing; + var pkr = w.publicKeyRing; - for(var i=0; i<4; i++) { + for (var i = 0; i < 4; i++) { if (privateKeys) { - var k=privateKeys[i]; - pkr.addCopayer(k?k.deriveBIP45Branch().extendedPublicKeyString():null); + var k = privateKeys[i]; + pkr.addCopayer(k ? k.deriveBIP45Branch().extendedPublicKeyString() : null); } else { pkr.addCopayer(); } @@ -120,16 +124,16 @@ describe('Wallet model', function() { return w; }; - it('#create, 1 sign', function () { + it('#create, 1 sign', function() { var w = createW2(); - unspentTest[0].address = w.publicKeyRing.getAddress(1, true).toString(); - unspentTest[0].scriptPubKey = w.publicKeyRing.getScriptPubKeyHex(1, true); + unspentTest[0].address = w.publicKeyRing.getAddress(1, true).toString(); + unspentTest[0].scriptPubKey = w.publicKeyRing.getScriptPubKeyHex(1, true); var ntxid = w.createTxSync( - '15q6HKjWHAksHcH91JW23BJEuzZgFwydBt', - '123456789', + '15q6HKjWHAksHcH91JW23BJEuzZgFwydBt', + '123456789', unspentTest ); @@ -142,30 +146,32 @@ describe('Wallet model', function() { Object.keys(txp.signedBy).length.should.equal(1); }); - it('#addressIsOwn', function () { + it('#addressIsOwn', function() { var w = createW2(); var l = w.getAddressesStr(); - for (var i=0; i 0).should.equal(true); - ( t.txps[k].seenBy[w.privateKey.getId()] - ts > 0).should.equal(true); + (t.txps[k].signedBy[w.privateKey.getId()] - ts > 0).should.equal(true); + (t.txps[k].seenBy[w.privateKey.getId()] - ts > 0).should.equal(true); } } }); - it('#fromObj #toObj round trip', function () { + it('#fromObj #toObj round trip', function() { var w = createW2(); @@ -189,9 +195,9 @@ describe('Wallet model', function() { o = JSON.parse(JSON.stringify(o)); var w2 = Wallet.fromObj(o, - new Storage(config.storage), - new Network(config.network), - new Blockchain(config.blockchain)); + new Storage(config.storage), + new Network(config.network), + new Blockchain(config.blockchain)); should.exist(w2); w2.publicKeyRing.requiredCopayers.should.equal(w.publicKeyRing.requiredCopayers); should.exist(w2.publicKeyRing.getCopayerId); @@ -199,21 +205,39 @@ describe('Wallet model', function() { should.exist(w2.privateKey.toObj); }); - it('#getSecret decodeSecret', function () { + it('#getSecret decodeSecret', function() { var w = createW2(); var id = w.getMyCopayerId(); var nk = w.netKey; - var sb= w.getSecret(); + var sb = w.getSecret(); should.exist(sb); var s = Wallet.decodeSecret(sb); s.pubKey.should.equal(id); s.netKey.should.equal(nk); }); - it('decodeSecret check', function () { - (function(){Wallet.decodeSecret('4fp61K187CsYmjoRQC5iAdC5eGmbCRsAAXfwEwetSQgHvZs27eWKaLaNHRoKM');}).should.not.throw(); - (function(){Wallet.decodeSecret('4fp61K187CsYmjoRQC5iAdC5eGmbCRsAAXfwEwetSQgHvZs27eWKaLaNHRoK');}).should.throw(); - (function(){Wallet.decodeSecret('12345');}).should.throw(); + it('decodeSecret check', function() { + (function() { + Wallet.decodeSecret('4fp61K187CsYmjoRQC5iAdC5eGmbCRsAAXfwEwetSQgHvZs27eWKaLaNHRoKM'); + }).should.not. + throw (); + (function() { + Wallet.decodeSecret('4fp61K187CsYmjoRQC5iAdC5eGmbCRsAAXfwEwetSQgHvZs27eWKaLaNHRoK'); + }).should. + throw (); + (function() { + Wallet.decodeSecret('12345'); + }).should. + throw (); + }); + it('call reconnect after interval', function(done) { + var w = createW2(); + var spy = sinon.spy(w, 'scheduleConnect'); + w.netStart(); + setTimeout(function() { + sinon.assert.callCount(spy, 10); + done(); + }, 1000); }); });