From 6c839edd9118b064f0a69c58ddd6818d42dbfbf4 Mon Sep 17 00:00:00 2001 From: Matias Pando Date: Thu, 15 Jan 2015 12:47:13 -0300 Subject: [PATCH 1/5] Added test on Async, deleted getPeer() --- js/models/Async.js | 4 ---- test/network.Async.js | 47 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/js/models/Async.js b/js/models/Async.js index 00ec189d8..421d83b32 100644 --- a/js/models/Async.js +++ b/js/models/Async.js @@ -358,10 +358,6 @@ Network.prototype.getOnlinePeerIDs = function() { return this.connectedPeers; }; -Network.prototype.getPeer = function() { - return this.peer; -}; - Network.prototype.getCopayerIds = function() { if (this.allowedCopayerIds) { diff --git a/test/network.Async.js b/test/network.Async.js index 3ac23963a..e086a1330 100644 --- a/test/network.Async.js +++ b/test/network.Async.js @@ -323,4 +323,51 @@ describe('Network / Async', function() { }); + describe('#_arrayRemove', function() { + it('should remove an element from an array', function() { + var array = ['1', '2', '3', '4']; + array = Async._arrayRemove('2', array); + array.length.should.be.equal(3); + array.indexOf('2').should.be.equal(-1); + }); + }); + + describe('#getOnlinePeerIDs', function() { + it('should get peer ids that are online', function() { + var n = createN(); + n.getOnlinePeerIDs().length.should.be.equal(0); + n._addCopayer('ab0001'); + n.getOnlinePeerIDs().length.should.be.equal(1); + n._addCopayer('ab0001'); + n.getOnlinePeerIDs().length.should.be.equal(1); + n._addCopayer('ab0002'); + n.getOnlinePeerIDs().length.should.be.equal(2); + }); + }); + + describe('#connectedCopayers', function() { + it('should get peer ids that are online', function() { + var n = createN(); + n.connectedCopayers().length.should.be.equal(0); + n._addCopayer('ab0001'); + n.connectedCopayers().length.should.be.equal(1); + n._addCopayer('ab0001'); + n.connectedCopayers().length.should.be.equal(1); + n._addCopayer('ab0002'); + n.connectedCopayers().length.should.be.equal(2); + }); + }); + + describe('#_deletePeer', function() { + it('should delete a Peer', function() { + var n = createN(); + n._addCopayer('ab0001'); + n.connectedPeers.length.should.be.equal(1); + var peerId = n.connectedPeers[0]; + n._deletePeer(peerId); + n.connectedPeers.length.should.be.equal(0); + }); + }); + + }); From 4cfa0a00e7a11e6a1ef1f2c44bea2df2fb056218 Mon Sep 17 00:00:00 2001 From: Matias Pando Date: Thu, 15 Jan 2015 12:59:30 -0300 Subject: [PATCH 2/5] Rename function --- js/controllers/receive.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/controllers/receive.js b/js/controllers/receive.js index 5e9dc70ce..07aa6b6d8 100644 --- a/js/controllers/receive.js +++ b/js/controllers/receive.js @@ -62,7 +62,7 @@ angular.module('copayApp.controllers').controller('ReceiveController', var w = $rootScope.wallet; var balance = w.balanceInfo.balanceByAddr; - var addresses = w.getAddressesOrderer(); + var addresses = w.getAddressesOrdered(); if (addresses) { $scope.addrLength = addresses.length; From 535539e94b900c7b5cca836bdafb968f4a27a827 Mon Sep 17 00:00:00 2001 From: Matias Pando Date: Thu, 15 Jan 2015 18:51:08 -0300 Subject: [PATCH 3/5] Added test for Async.js --- test/network.Async.js | 48 +++++++++++++++ test/unit/controllers/controllersSpec.js | 74 +++++++++++++++++++++++- 2 files changed, 121 insertions(+), 1 deletion(-) diff --git a/test/network.Async.js b/test/network.Async.js index e086a1330..4b1180410 100644 --- a/test/network.Async.js +++ b/test/network.Async.js @@ -369,5 +369,53 @@ describe('Network / Async', function() { }); }); + describe('#getCopayerIds', function() { + it('should return the copayer ids', function() { + var n = createN(); + n.getCopayerIds().length.should.be.equal(1); + }); + }); + + describe('#isOnline', function() { + it('should return if is online', function() { + var n = createN(); + n.isOnline().should.be.true; + n.cleanUp(); + n.isOnline().should.be.false; + }); + }); + + describe('#greet', function() { + it('should greet ', function() { + var n = createN(); + n.greet('03b51d01d798522cf61211b4dfcdd6db219ee33cf166e1cb7f43d836ab00ccddee', 'mySecret'); + }); + }); + + describe('#setCopayers', function() { + it('should setCopayers ', function() { + var n = createN(); + n.connectedPeers.length.should.be.equal(0); + var cids = ['abc001', 'abc002']; + n.setCopayers(cids); + n.connectedPeers.length.should.be.equal(2); + }); + }); + + + describe('#lockIncommingConnections', function() { + it('should lock Incomming Connections ', function() { + var n = createN(); + var cids = ['abc001', 'abc002', 'abc003']; + n.setCopayers(cids); + + var lockIds = ['abc001', 'abc002']; + n.lockIncommingConnections(lockIds); + console.log(n.allowedCopayerIds); + Object.keys(n.allowedCopayerIds).length.should.be.equal(2); + + + }); + }); }); diff --git a/test/unit/controllers/controllersSpec.js b/test/unit/controllers/controllersSpec.js index edf7f472f..abf95dada 100644 --- a/test/unit/controllers/controllersSpec.js +++ b/test/unit/controllers/controllersSpec.js @@ -52,7 +52,7 @@ describe("Unit: Controllers", function() { // // TODO Use the REAL wallet, and stub only networking and DB components! // - + var w = {}; w.id = 1234; w.isComplete = sinon.stub().returns(true); @@ -148,8 +148,80 @@ describe("Unit: Controllers", function() { c = $controller('ReceiveController', { $scope: scope, }); + + + var createW = function(N, conf) { + + var c = JSON.parse(JSON.stringify(conf || walletConfig)); + if (!N) N = c.totalCopayers; + + var mainPrivateKey = new copay.PrivateKey({ + networkName: walletConfig.networkName + }); + var mainCopayerEPK = mainPrivateKey.deriveBIP45Branch().extendedPublicKeyString(); + c.privateKey = mainPrivateKey; + + c.publicKeyRing = new copay.PublicKeyRing({ + networkName: c.networkName, + requiredCopayers: Math.min(N, c.requiredCopayers), + totalCopayers: N, + }); + c.publicKeyRing.addCopayer(mainCopayerEPK); + + c.publicKeyRing.getAddressesOrdered = sinon.stub().returns(null); + + c.txProposals = new copay.TxProposals({ + networkName: c.networkName, + }); + + c.blockchain = new Blockchain(walletConfig.blockchain); + + c.network = sinon.stub(); + c.network.setHexNonce = sinon.stub(); + c.network.setHexNonces = sinon.stub(); + c.network.getHexNonce = sinon.stub(); + c.network.getHexNonces = sinon.stub(); + c.network.peerFromCopayer = sinon.stub().returns('xxxx'); + c.network.send = sinon.stub(); + + c.addressBook = { + '2NFR2kzH9NUdp8vsXTB4wWQtTtzhpKxsyoJ': { + label: 'John', + copayerId: '026a55261b7c898fff760ebe14fd22a71892295f3b49e0ca66727bc0a0d7f94d03', + createdTs: 1403102115, + hidden: false + }, + '2MtP8WyiwG7ZdVWM96CVsk2M1N8zyfiVQsY': { + label: 'Jennifer', + copayerId: '032991f836543a492bd6d0bb112552bfc7c5f3b7d5388fcbcbf2fbb893b44770d7', + createdTs: 1403103115, + hidden: false + } + }; + + c.networkName = walletConfig.networkName; + c.version = '0.0.1'; + + c.balanceInfo = {}; + + return new Wallet(c); + }; + + $rootScope.wallet = createW(); + $rootScope.wallet.balanceInfo = {}; })); + it('should exist', function() { + should.exist(c); + }); + + it('should call setAddressList', function() { + scope.setAddressList(); + expect(scope.addresses).to.be.empty; + scope.toggleShowAll(); + scope.setAddressList(); + expect(scope.addresses).to.be.empty; + }); }); describe('History Controller', function() { From 368a8c0c2e39dfd454cea61ace8e9dc5cc17df92 Mon Sep 17 00:00:00 2001 From: Matias Pando Date: Fri, 16 Jan 2015 12:04:11 -0300 Subject: [PATCH 4/5] Testing Async #setHexNonce and #getKey --- test/network.Async.js | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/test/network.Async.js b/test/network.Async.js index 4b1180410..d0a879401 100644 --- a/test/network.Async.js +++ b/test/network.Async.js @@ -257,6 +257,26 @@ describe('Network / Async', function() { n.networkNonce.toString('hex').should.equal(hex); }); + it('should return an error', function() { + var hex = '0000'; + var n = createN(); + (function() { + n.setHexNonce(hex); + }).should.throw('incorrect length'); + }); + + it('should iterateNonce', function() { + var n = createN(); + n.iterateNonce = sinon.spy(); + n.setHexNonce(); + n.iterateNonce.callCount.should.be.equal(1); + n.setHexNonce(null); + n.iterateNonce.callCount.should.be.equal(2); + n.setHexNonce(undefined); + n.iterateNonce.callCount.should.be.equal(3); + + }); + }); describe('#setHexNonces', function() { @@ -329,6 +349,8 @@ describe('Network / Async', function() { array = Async._arrayRemove('2', array); array.length.should.be.equal(3); array.indexOf('2').should.be.equal(-1); + array = Async._arrayRemove('5', array); + array.length.should.be.equal(3); }); }); @@ -413,8 +435,18 @@ describe('Network / Async', function() { n.lockIncommingConnections(lockIds); console.log(n.allowedCopayerIds); Object.keys(n.allowedCopayerIds).length.should.be.equal(2); + }); + }); - + describe('#getKey', function() { + it('should return the key or generate a new one ', function() { + var n = createN(); + n.key = null; + var k1 = n.getKey(); + k1.should.not.be.undefined; + var k2 = n.getKey(); + k2.should.not.be.undefined; + k1.should.be.equal(k2); }); }); From c70d5d199ba199f1c936f2aa1eef152dbe4a49b8 Mon Sep 17 00:00:00 2001 From: Matias Pando Date: Mon, 19 Jan 2015 11:03:54 -0300 Subject: [PATCH 5/5] Avoid errors on invalid address --- js/models/Wallet.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/models/Wallet.js b/js/models/Wallet.js index 76abc52d1..02e53b39e 100644 --- a/js/models/Wallet.js +++ b/js/models/Wallet.js @@ -658,7 +658,7 @@ Wallet.prototype._onAddressBook = function(senderId, data) { var self = this, hasChange; _.each(data.addressBook, function(value, key) { - if (key && !self.addressBook[key] && Address.validate(key)) { + if (key && !self.addressBook[key] && _.isString(key) && Address.validate(key)) { self.addressBook[key] = _.pick(value, ['createdTs', 'label']);