Merge pull request #2298 from matiaspando/increaseCoverage85

Increase coverage to 83%
This commit is contained in:
Matias Alejo Garcia 2015-01-16 10:07:14 -03:00
commit 93b0a484c8
9 changed files with 422 additions and 80 deletions

View file

@ -19,7 +19,7 @@ angular.module('copayApp.controllers').controller('ReceiveController',
$scope.showAll = false; $scope.showAll = false;
var w = $rootScope.wallet; var w = $rootScope.wallet;
var lastAddr = _.first(w.getAddressesOrderer()); var lastAddr = _.first(w.getAddressesOrdered());
var balance = w.balanceInfo.balanceByAddr; var balance = w.balanceInfo.balanceByAddr;
$scope.setAddressList(); $scope.setAddressList();

View file

@ -507,7 +507,7 @@ Identity.prototype.importWalletFromObj = function(obj, opts, cb) {
log.debug('Updating Indexes for wallet:' + w.getName()); log.debug('Updating Indexes for wallet:' + w.getName());
w.updateIndexes(function(err) { w.updateIndexes(function(err) {
log.debug('Adding wallet to profile:' + w.getName()); log.debug('Adding wallet to profile:' + w.getName());
self.storeWallet(w, function (err) { self.storeWallet(w, function(err) {
if (err) return cb(err); if (err) return cb(err);
self.addWallet(w); self.addWallet(w);

View file

@ -14,8 +14,8 @@ var log = require('../util/log.js');
/* /*
This class lets interfaces with the blockchain, making general queries and This class lets interface with the blockchain, making general queries and
subscribing to transactions on adressess and blocks. subscribing to transactions on addresses and blocks.
Opts: Opts:
- url - url

View file

@ -540,13 +540,13 @@ PublicKeyRing.prototype.getAddresses = function() {
}; };
/** /**
* getAddressesOrderer * getAddressesOrdered
* {@link Wallet#getAddressesOrderer} * {@link Wallet#getAddressesOrdered}
* *
* @param pubkey * @param pubkey
* @return {string[]} * @return {string[]}
*/ */
PublicKeyRing.prototype.getAddressesOrderer = function(pubkey) { PublicKeyRing.prototype.getAddressesOrdered = function(pubkey) {
this._checkCache(); this._checkCache();
var info = _.map(this.cache.addressToPath, function(path, addr) { var info = _.map(this.cache.addressToPath, function(path, addr) {
@ -559,9 +559,7 @@ PublicKeyRing.prototype.getAddressesOrderer = function(pubkey) {
var l = info.length; var l = info.length;
var sortedInfo = _.sortBy(info, function(i) { var sortedInfo = _.sortBy(info, function(i) {
var goodness = ( (i.copayerIndex !== copayerIndex) ? 2 * l : 0 ) var goodness = ((i.copayerIndex !== copayerIndex) ? 2 * l : 0) + (i.isChange ? l : 0) + l - i.addressIndex;
+ ( i.isChange ? l : 0 )
+ l - i.addressIndex;
return goodness; return goodness;
}); });

View file

@ -2062,13 +2062,13 @@ Wallet.prototype.getAddresses = function() {
/** /**
* @desc gets the list of addresses, orderder for the caller: * @desc gets the list of addresses, ordered for the caller:
* 1) himselfs first * 1) himselfs first
* 2) receive address first * 2) receive address first
* 3) last created first * 3) last created first
*/ */
Wallet.prototype.getAddressesOrderer = function() { Wallet.prototype.getAddressesOrdered = function() {
return this.publicKeyRing.getAddressesOrderer(this.publicKey); return this.publicKeyRing.getAddressesOrdered(this.publicKey);
}; };
/** /**

View file

@ -146,28 +146,63 @@ describe('Identity model', function() {
}); });
describe('#open', function(done) { describe('#open', function(done) {
it.skip('should return last focused wallet', function(done) { it('should open a profile', function(done) {
var wallets = [{ var storage = sinon.stub();
id: 'wallet1', storage.setCredentials = sinon.stub();
store: sinon.stub().yields(null),
netStart: sinon.stub(), var opts = {
}, { email: 'test@test.com',
id: 'wallet2', password: '123',
store: sinon.stub().yields(null), network: {
netStart: sinon.stub(), testnet: {
}, { url: 'https://test-insight.bitpay.com:443'
id: 'wallet3', },
store: sinon.stub().yields(null), livenet: {
netStart: sinon.stub(), url: 'https://insight.bitpay.com:443'
}]; },
var args = createIdentity(); },
Identity.create(args.params, function(err, identity) { storage: storage,
// TODO: Add checks for what is this testing };
var iden = new Identity(opts);
storage.getItem = sinon.stub().yields(null, JSON.stringify(iden));
Identity.open(opts, function(err, res) {
should.not.exist(err);
should.exist(res);
res.should.be.deep.equal(iden);
done(); done();
}); });
}); });
}); });
describe('#resendVerificationEmail', function(done) {
it('should resend verification email', function() {
var storage = sinon.stub();
storage.setCredentials = sinon.stub();
storage.resendVerificationEmail = sinon.stub().yields();
var opts = {
email: 'test@test.com',
password: '123',
network: {
testnet: {
url: 'https://test-insight.bitpay.com:443'
},
livenet: {
url: 'https://insight.bitpay.com:443'
},
},
storage: storage,
};
var iden = new Identity(opts);
iden.resendVerificationEmail(function() {});
storage.resendVerificationEmail.calledOnce.should.be.true;
});
});
describe('#openWallets', function(done) { describe('#openWallets', function(done) {
it('should emit noWallets', function() { it('should emit noWallets', function() {
@ -179,6 +214,57 @@ describe('Identity model', function() {
}); });
}); });
describe('#setBackupNeeded', function(done) {
it('should set the flag backupNeeded to true', function() {
var iden = new Identity(getDefaultParams());
iden.verifyChecksum = sinon.stub().yields(null, true);
iden.setBackupNeeded(true);
iden.backupNeeded.should.be.true;
iden.store.calledOnce.should.be.true;
iden.store.getCall(0).args[0].noWallets.should.equal(true);
});
it('should set the flag backupNeeded to false', function() {
var iden = new Identity(getDefaultParams());
iden.verifyChecksum = sinon.stub().yields(null, true);
iden.setBackupNeeded(false);
iden.backupNeeded.should.be.false;
iden.store.calledOnce.should.be.true;
iden.store.getCall(0).args[0].noWallets.should.equal(true);
});
});
describe('#close', function(done) {
it('should store profile', function(done) {
var iden = new Identity(getDefaultParams());
sinon.spy(iden, 'emitAndKeepAlive');
sinon.spy(iden, '_cleanUp');
iden.verifyChecksum = sinon.stub().yields(null, true);
iden.close(function() {
iden._cleanUp.calledOnce.should.be.true;
iden.emitAndKeepAlive.calledOnce.should.be.true;
iden.emitAndKeepAlive.getCall(0).args[0].should.equal('closed');
iden.store.calledOnce.should.be.true;
iden.store.getCall(0).args[0].noWallets.should.equal(true);
done();
});
});
it('should not store profile when out of sync', function(done) {
var iden = new Identity(getDefaultParams());
sinon.spy(iden, 'emitAndKeepAlive');
sinon.spy(iden, '_cleanUp');
iden.verifyChecksum = sinon.stub().yields(null, false);
iden.close(function() {
iden._cleanUp.calledOnce.should.be.true;
iden.emitAndKeepAlive.calledOnce.should.be.true;
iden.emitAndKeepAlive.getCall(0).args[0].should.equal('closed');
iden.store.calledOnce.should.be.false;
done();
});
});
});
describe('#remove', function(done) { describe('#remove', function(done) {
it('should remove empty profile', function(done) { it('should remove empty profile', function(done) {
var storage = sinon.stub(); var storage = sinon.stub();
@ -421,8 +507,6 @@ describe('Identity model', function() {
}); });
describe('#retrieveWalletFromStorage', function() { describe('#retrieveWalletFromStorage', function() {
it('should return wallet', function(done) { it('should return wallet', function(done) {
var args = createIdentity(); var args = createIdentity();
args.storage.getItem.onFirstCall().callsArgWith(1, null, '{"wallet": "fakeData"}'); args.storage.getItem.onFirstCall().callsArgWith(1, null, '{"wallet": "fakeData"}');
@ -446,6 +530,56 @@ describe('Identity model', function() {
}); });
}); });
describe('#importWalletFromObj', function() {
it('should import a wallet, call the right encryption functions', function(done) {
var args = createIdentity();
args.storage.getItem.onFirstCall().callsArgWith(1, null, '{"wallet": "fakeData"}');
var backup = Wallet.fromUntrustedObj;
args.params.noWallets = true;
sinon.stub().returns(args.wallet);
var fakeCrypto = {
kdf: sinon.stub().returns('passphrase'),
decrypt: sinon.stub().returns('{"walletId":123}'),
};
function getNewWallet2(args) {
var w = sinon.stub();
w.getId = sinon.stub().returns('wid1');
w.getStorageKey = sinon.stub().returns('wkey');
w.toObj = sinon.stub().returns({
obj: 1
});
w.getName = sinon.stub().returns('name');
w.setVersion = sinon.stub();
w.on = sinon.stub();
w.netStart = sinon.stub();
w.updateIndexes = sinon.stub().yields(null),
w.args = args;
return w;
}
var opts = {
importWallet: sinon.stub().returns(getNewWallet2()),
cryptoUtil: fakeCrypto,
};
Identity.create(args.params, function(err, iden) {
iden.importWalletFromObj(getNewWallet2(), opts, function(err) {
should.not.exist(err);
iden.store.restore();
done();
});
});
});
});
// This is implemented in Compatibility // This is implemented in Compatibility
describe.skip('#importWallet', function() { describe.skip('#importWallet', function() {
it('should import a wallet, call the right encryption functions', function(done) { it('should import a wallet, call the right encryption functions', function(done) {
@ -480,6 +614,60 @@ describe('Identity model', function() {
}); });
describe('#deleteWallet', function() {
var iden, w;
beforeEach(function() {
var storage = sinon.stub();
storage.setCredentials = sinon.stub();
storage.removeItem = sinon.stub().yields(null);
storage.clear = sinon.stub().yields();
var opts = {
email: 'test@test.com',
password: '123',
network: {
testnet: {
url: 'https://test-insight.bitpay.com:443'
},
livenet: {
url: 'https://insight.bitpay.com:443'
},
},
storage: storage,
};
iden = new Identity(opts);
w = {
getId: sinon.stub().returns('32'),
getName: sinon.stub().returns('treintaydos'),
close: sinon.stub(),
};
});
it('should not save other wallets', function(done) {
iden.addWallet(w);
iden.storage.getItem = sinon.stub().yields(null, JSON.stringify(iden));
iden.deleteWallet('32', function(err) {
iden.walletIds.should.deep.equal([]);
should.not.exist(_.find(iden.getWallets(), function(w) {
return w.getName() == 'treintaydos';
}));
iden.store.calledOnce.should.be.true;
iden.store.getCall(0).args[0].noWallets.should.equal(true);
done();
});
});
});
describe('#export', function() { describe('#export', function() {
@ -492,12 +680,7 @@ describe('Identity model', function() {
/** /**
* TODO (eordano): Move this to a different test file * TODO (eordano): Move this to a different test file
* *
describe('#pluginManager', function() {
it('should create a new PluginManager object', function() {
var pm = new PluginManager({plugins: { FakeLocalStorage: true }, pluginsPath: '../../test/mocks/'});
should.exist(pm);
});
});
describe('#Insight', function() { describe('#Insight', function() {
it('should parse a uri', function() { it('should parse a uri', function() {
@ -623,7 +806,7 @@ describe('Identity model', function() {
describe('add / delete / list Wallets', function() { describe('add / delete / list Wallets', function() {
var iden, w; var iden, w, w2;
beforeEach(function() { beforeEach(function() {
var storage = sinon.stub(); var storage = sinon.stub();
storage.setCredentials = sinon.stub(); storage.setCredentials = sinon.stub();
@ -650,6 +833,12 @@ describe('Identity model', function() {
getName: sinon.stub().returns('treintaydos'), getName: sinon.stub().returns('treintaydos'),
close: sinon.stub(), close: sinon.stub(),
}; };
w2 = {
getId: sinon.stub().returns('33'),
getName: sinon.stub().returns('treintaytres'),
close: sinon.stub(),
};
}); });
it('should add wallet', function() { it('should add wallet', function() {
@ -661,6 +850,48 @@ describe('Identity model', function() {
return w.getName() == 'treintaydos'; return w.getName() == 'treintaydos';
}).should.deep.equal(w); }).should.deep.equal(w);
iden.addWallet(w2);
iden.getWalletById('33').getName().should.equal('treintaytres');
iden.walletIds.should.deep.equal(['32', '33']);
});
it('should read and bind wallet', function(done) {
iden.addWallet(w);
iden.storage.getItem = sinon.stub().yields(w, JSON.stringify(iden));
iden.readAndBindWallet('32', function(err) {
iden.getWalletById('32').getName().should.equal('treintaydos');
done();
});
});
it('should open wallet', function() {
iden.addWallet(w);
iden.storage.getItem = sinon.stub().yields(w, JSON.stringify(iden));
iden.readAndBindWallet = sinon.spy();
iden.openWallets();
iden.readAndBindWallet.should.calledOnce;
});
it('should open wallets', function(done) {
var c = 0;
var old = iden.readAndBindWallet;
iden.addWallet(w);
iden.addWallet(w2);
iden.readAndBindWallet = function(wid, myFunction) {
c++;
if (c == 2) {
iden.readAndBindWallet = old;
done();
}
myFunction();
};
iden.openWallets();
}); });
it('should not add same wallet twice', function() { it('should not add same wallet twice', function() {
@ -831,7 +1062,7 @@ describe('Identity model', function() {
}); });
it('should return indefined', function() { it('should return undefined', function() {
expect(iden.getLastFocusedWalletId()).to.be.undefined; expect(iden.getLastFocusedWalletId()).to.be.undefined;
}); });
@ -891,9 +1122,57 @@ describe('Identity model', function() {
expect(err).to.be.null; expect(err).to.be.null;
iden.should.not.be.null; iden.should.not.be.null;
}); });
}); });
});
describe('importFromEncryptedFullJson', function() {
var opts;
beforeEach(function() {
var storage = sinon.stub();
storage.setCredentials = sinon.stub();
storage.removeItem = sinon.stub().yields(null);
storage.clear = sinon.stub().yields();
var fakeCrypto = {
kdf: sinon.stub().returns('passphrase'),
decrypt: sinon.stub().returns('{"walletId":123}'),
};
opts = {
email: 'test@test.com',
password: '123',
network: {
testnet: {
url: 'https://test-insight.bitpay.com:443'
},
livenet: {
url: 'https://insight.bitpay.com:443'
},
},
storage: storage,
cryptoUtil: fakeCrypto,
};
});
it('should throw error because json is wrong', function() {
Identity.importFromEncryptedFullJson('asdfg', '1', {}, function(c) {
c.should.be.equal('BADSTR');
});
});
it('should throw error because json does not have required fields', function() {
Identity.importFromEncryptedFullJson('{"age":23}', '1', {}, function(c) {
c.should.be.equal('BADSTR');
});
});
it('should create a profile', function() {
var json = '{"networkOpts":{"livenet":{"url":"https://insight.bitpay.com:443","transports":["polling"]},"testnet":{"url":"https://test-insight.bitpay.com:443","transports":["polling"]}},"blockchainOpts":{"livenet":{"url":"https://insight.bitpay.com:443","transports":["polling"]},"testnet":{"url":"https://test-insight.bitpay.com:443","transports":["polling"]}},"fullName":"l@l","email":"l@l","password":"1","storage":{"type":"DB","storeUrl":"https://insight.bitpay.com:443/api/email","iterations":1000,"salt":"jBbYTj8zTrOt6V","email":"l@l","password":"1","_cachedKey":"y4a352k6sM15gGag+PgQwXRdFjzi0yX6aLEGttWaeP+kbU7JeSPDUfbhhzonnQRUicJu/1IMWgDZbDJjWmrKgA=="},"walletDefaults":{"requiredCopayers":2,"totalCopayers":3,"spendUnconfirmed":true,"reconnectDelay":5000,"idleDurationMin":4,"settings":{"unitName":"bits","unitToSatoshi":100,"unitDecimals":2,"alternativeName":"US Dollar","alternativeIsoCode":"USD"}},"version":"0.8.2","walletIds":["15a3ecd34dfb7000","59220d2110461861","bfd6adad419078d9","893dc0c0a776648b","e8ee7218c6ea7f93"],"wallets":{},"focusedTimestamps":{"15a3ecd34dfb7000":1418916813711,"bfd6adad419078d9":1418835855887,"e8ee7218c6ea7f93":1418775999995,"59220d2110461861":1418835858871,"893dc0c0a776648b":1418835763680},"backupNeeded":true,"_events":{}}';
opts.cryptoUtil.decrypt = sinon.stub().returns(json);
Identity.importFromEncryptedFullJson(json, '1', opts, function(err, iden) {
expect(err).to.be.null;
iden.should.not.be.null;
});
});
}); });
describe('#closeWallet', function() { describe('#closeWallet', function() {

View file

@ -137,7 +137,7 @@ describe('PublicKeyRing model', function() {
[true, false].forEach(function(isChange) { [true, false].forEach(function(isChange) {
for (var i = 0; i < 2; i++) { for (var i = 0; i < 2; i++) {
var aStr = w.generateAddress(isChange, k.pub); var aStr = w.generateAddress(isChange, k.pub);
var a= new bitcore.Address(aStr); var a = new bitcore.Address(aStr);
a.isValid().should.equal(true); a.isValid().should.equal(true);
a.isScript().should.equal(true); a.isScript().should.equal(true);
a.network().name.should.equal('livenet'); a.network().name.should.equal('livenet');
@ -161,8 +161,8 @@ describe('PublicKeyRing model', function() {
var setup = getCachedW(); var setup = getCachedW();
var pubkeyring = setup.w; var pubkeyring = setup.w;
var address = pubkeyring.generateAddress(false, setup.pub); var address = pubkeyring.generateAddress(false, setup.pub);
_.indexOf(pubkeyring.getReceiveAddresses(),address).should.be.above(-1); _.indexOf(pubkeyring.getReceiveAddresses(), address).should.be.above(-1);
_.indexOf(pubkeyring.getAddresses(),address).should.be.above(-1); _.indexOf(pubkeyring.getAddresses(), address).should.be.above(-1);
}); });
@ -170,8 +170,8 @@ describe('PublicKeyRing model', function() {
var setup = getCachedW(); var setup = getCachedW();
var pubkeyring = setup.w; var pubkeyring = setup.w;
var address = pubkeyring.generateAddress(true, setup.pub); var address = pubkeyring.generateAddress(true, setup.pub);
_.indexOf(pubkeyring.getReceiveAddresses(),address).should.be.equal(-1); _.indexOf(pubkeyring.getReceiveAddresses(), address).should.be.equal(-1);
_.indexOf(pubkeyring.getAddresses(),address).should.be.above(-1); _.indexOf(pubkeyring.getAddresses(), address).should.be.above(-1);
}); });
it('should generate one address by default', function() { it('should generate one address by default', function() {
@ -179,6 +179,8 @@ describe('PublicKeyRing model', function() {
var w = k.w; var w = k.w;
var a = w.getAddresses(); var a = w.getAddresses();
a.length.should.equal(1); a.length.should.equal(1);
var b = w.getAddressesOrdered();
b.length.should.equal(1);
}); });
it('should generate one address by default', function() { it('should generate one address by default', function() {
@ -190,7 +192,7 @@ describe('PublicKeyRing model', function() {
a = w.getAddresses(); a = w.getAddresses();
a.length.should.equal(1); a.length.should.equal(1);
}); });
it('should generate 4+1 addresses', function() { it('should generate 4+1 addresses', function() {
var k = createW(); var k = createW();
@ -546,6 +548,10 @@ describe('PublicKeyRing model', function() {
ret.pubKeys[1].length.should.equal(5); ret.pubKeys[1].length.should.equal(5);
}); });
it('#myCopayerId should return first copayerId ', function() {
var w = getCachedW().w;
w.myCopayerId().should.be.equal(w.getCopayerId(0));
});
}); });

View file

@ -77,6 +77,7 @@ describe('Wallet model', function() {
it('should getNetworkName', function() { it('should getNetworkName', function() {
var w = cachedCreateW(); var w = cachedCreateW();
w.getNetworkName().should.equal('testnet'); w.getNetworkName().should.equal('testnet');
w.isTestnet().should.be.true;
}); });
@ -155,6 +156,7 @@ describe('Wallet model', function() {
var w = cachedCreateW(); var w = cachedCreateW();
should.exist(w); should.exist(w);
w.publicKeyRing.walletId.should.equal(w.id); w.publicKeyRing.walletId.should.equal(w.id);
w.getId().should.equal(w.id);
w.txProposals.walletId.should.equal(w.id); w.txProposals.walletId.should.equal(w.id);
w.requiredCopayers.should.equal(3); w.requiredCopayers.should.equal(3);
should.exist(w.id); should.exist(w.id);
@ -318,6 +320,7 @@ describe('Wallet model', function() {
var w = cachedCreateW2(); var w = cachedCreateW2();
unSpentTestFromWallet(unspentTest[0], w.publicKeyRing.generateAddress(true)); unSpentTestFromWallet(unspentTest[0], w.publicKeyRing.generateAddress(true));
var txp = w._createTxProposal( var txp = w._createTxProposal(
'mgGJEugdPnvhmRuFdbdQcFfoFLc1XXeB79', 'mgGJEugdPnvhmRuFdbdQcFfoFLc1XXeB79',
'123456789', '123456789',
@ -325,11 +328,13 @@ describe('Wallet model', function() {
unspentTest unspentTest
); );
w.addSeenToTxProposals().should.be.false;
Object.keys(txp.getSignersPubKeys()).length.should.equal(1); Object.keys(txp.getSignersPubKeys()).length.should.equal(1);
var tx = txp.builder.build(); var tx = txp.builder.build();
should.exist(tx); should.exist(tx);
chai.expect(txp.comment).to.be.null; chai.expect(txp.comment).to.be.null;
tx.isComplete().should.equal(false); tx.isComplete().should.equal(false);
w.addSeenToTxProposals().should.be.false;
Object.keys(txp.seenBy).length.should.equal(1); Object.keys(txp.seenBy).length.should.equal(1);
Object.keys(txp.signedBy).length.should.equal(1); Object.keys(txp.signedBy).length.should.equal(1);
}); });
@ -379,6 +384,14 @@ describe('Wallet model', function() {
wallet.addressIsOwn('mgtUfP9sTJ6vPLoBxZLPEccGpcjNVryaCX').should.equal(false); wallet.addressIsOwn('mgtUfP9sTJ6vPLoBxZLPEccGpcjNVryaCX').should.equal(false);
}); });
it('#getAddressesOrdered', function() {
var wallet = cachedCreateW2();
var allAddresses = wallet.getAddressesOrdered();
for (var i = 0; i < allAddresses.length; i++) {
wallet.addressIsOwn(allAddresses[i]).should.equal(true);
}
});
it('#create. Signing with derivate keys', function() { it('#create. Signing with derivate keys', function() {
var w = cachedCreateW2(); var w = cachedCreateW2();
@ -760,7 +773,7 @@ describe('Wallet model', function() {
}); });
it('should exportEncrypted', function() { it('should exportEncrypted ', function() {
var w = createW2(); var w = createW2();
var enc = w.exportEncrypted('', {}); var enc = w.exportEncrypted('', {});
enc.length.should.equal(2405); enc.length.should.equal(2405);
@ -874,7 +887,9 @@ describe('Wallet model', function() {
}, function(err, ntxid) { }, function(err, ntxid) {
var s = sinon.stub(w, 'getMyCopayerId').returns('213'); var s = sinon.stub(w, 'getMyCopayerId').returns('213');
Object.keys(w.txProposals.get(ntxid).rejectedBy).length.should.equal(0); Object.keys(w.txProposals.get(ntxid).rejectedBy).length.should.equal(0);
w.addSeenToTxProposals().should.be.true;
w.reject(ntxid); w.reject(ntxid);
Object.keys(w.txProposals.get(ntxid).rejectedBy).length.should.equal(1); Object.keys(w.txProposals.get(ntxid).rejectedBy).length.should.equal(1);
w.txProposals.get(ntxid).rejectedBy['213'].should.gt(1); w.txProposals.get(ntxid).rejectedBy['213'].should.gt(1);
s.restore(); s.restore();
@ -2053,6 +2068,15 @@ describe('Wallet model', function() {
payload.walletId.should.equal(w.id); payload.walletId.should.equal(w.id);
payload.txProposal.should.deep.equal(txp.toObjTrim()); payload.txProposal.should.deep.equal(txp.toObjTrim());
}); });
it('should be able to sendAllTxProposals since ', function() {
w.txProposals.add(txp);
w.sendAllTxProposals(null, txp.createdTs);
w.network.send.calledOnce.should.equal(true);
var payload = w.network.send.getCall(0).args[1];
payload.type.should.equal('txProposal');
payload.walletId.should.equal(w.id);
payload.txProposal.should.deep.equal(txp.toObjTrim());
});
it('should be able to sendSignature', function() { it('should be able to sendSignature', function() {
w.txProposals.add(txp); w.txProposals.add(txp);
w.sendSignature(txp.getId()); w.sendSignature(txp.getId());
@ -2541,7 +2565,6 @@ describe('Wallet model', function() {
}); });
}); });
// TODO // TODO
describe.skip('#onPayProPaymentAck', function() { describe.skip('#onPayProPaymentAck', function() {
it('should emit', function() { it('should emit', function() {

View file

@ -128,8 +128,12 @@ describe('Insight model', function() {
sinon.stub(blockchain, "requestPost", function(url, data, cb) { sinon.stub(blockchain, "requestPost", function(url, data, cb) {
url.should.be.equal('/api/tx/send'); url.should.be.equal('/api/tx/send');
var res = {statusCode: 200}; var res = {
var body = {txid: 1234}; statusCode: 200
};
var body = {
txid: 1234
};
setTimeout(function() { setTimeout(function() {
cb(null, res, body); cb(null, res, body);
}, 0); }, 0);
@ -145,11 +149,16 @@ describe('Insight model', function() {
it('should get a transaction by id', function(done) { it('should get a transaction by id', function(done) {
var blockchain = new Insight(FAKE_OPTS); var blockchain = new Insight(FAKE_OPTS);
var txid = '123321'; var txid = '123321';
var tx = {txid: txid, more: 'something'}; var tx = {
txid: txid,
more: 'something'
};
sinon.stub(blockchain, "request", function(url, cb) { sinon.stub(blockchain, "request", function(url, cb) {
url.should.be.equal('/api/tx/' + txid); url.should.be.equal('/api/tx/' + txid);
var res = {statusCode: 200}; var res = {
statusCode: 200
};
var body = JSON.stringify(tx); var body = JSON.stringify(tx);
setTimeout(function() { setTimeout(function() {
cb(null, res, body); cb(null, res, body);
@ -171,7 +180,9 @@ describe('Insight model', function() {
sinon.stub(blockchain, "request", function(url, cb) { sinon.stub(blockchain, "request", function(url, cb) {
url.should.be.equal('/api/tx/' + txid); url.should.be.equal('/api/tx/' + txid);
var res = {statusCode: 404}; var res = {
statusCode: 404
};
var body = ''; var body = '';
setTimeout(function() { setTimeout(function() {
cb(null, res, body); cb(null, res, body);
@ -191,7 +202,9 @@ describe('Insight model', function() {
sinon.stub(blockchain, "request", function(url, cb) { sinon.stub(blockchain, "request", function(url, cb) {
url.should.be.equal('/api/tx/' + txid); url.should.be.equal('/api/tx/' + txid);
var res = {statusCode: 200}; var res = {
statusCode: 200
};
var body = null; var body = null;
setTimeout(function() { setTimeout(function() {
cb(null, res, body); cb(null, res, body);
@ -211,7 +224,9 @@ describe('Insight model', function() {
sinon.stub(blockchain, "request", function(url, cb) { sinon.stub(blockchain, "request", function(url, cb) {
url.should.be.equal('/api/tx/' + txid); url.should.be.equal('/api/tx/' + txid);
var res = {statusCode: 200}; var res = {
statusCode: 200
};
var body = null; var body = null;
setTimeout(function() { setTimeout(function() {
cb(null, res, body); cb(null, res, body);
@ -233,8 +248,13 @@ describe('Insight model', function() {
url.should.be.equal('/api/addrs/txs?from=0'); url.should.be.equal('/api/addrs/txs?from=0');
data.addrs.should.be.equal('2NATQJnaQe2CUKLyhL1zdNkttJM1dUH9HaM,2NE9hTCffeugo5gQtfB4owq98gyTeWC56yb'); data.addrs.should.be.equal('2NATQJnaQe2CUKLyhL1zdNkttJM1dUH9HaM,2NE9hTCffeugo5gQtfB4owq98gyTeWC56yb');
setTimeout(function() { setTimeout(function() {
var res = {statusCode: 200}; var res = {
var body = { totalItems: 3, items: [1, 2, 3] }; statusCode: 200
};
var body = {
totalItems: 3,
items: [1, 2, 3]
};
cb(null, res, body); cb(null, res, body);
}, 0); }, 0);
}); });
@ -254,7 +274,9 @@ describe('Insight model', function() {
url.should.be.equal('/api/addrs/utxo'); url.should.be.equal('/api/addrs/utxo');
data.addrs.should.be.equal('2NATQJnaQe2CUKLyhL1zdNkttJM1dUH9HaM,2NE9hTCffeugo5gQtfB4owq98gyTeWC56yb,2N9D5bcCQ2bPWUDByQ6Qb5bMgMtgsk1rw3x'); data.addrs.should.be.equal('2NATQJnaQe2CUKLyhL1zdNkttJM1dUH9HaM,2NE9hTCffeugo5gQtfB4owq98gyTeWC56yb,2N9D5bcCQ2bPWUDByQ6Qb5bMgMtgsk1rw3x');
setTimeout(function() { setTimeout(function() {
var res = {statusCode: 200}; var res = {
statusCode: 200
};
var body = UNSPENT; var body = UNSPENT;
cb(null, res, body); cb(null, res, body);
}, 0); }, 0);
@ -292,27 +314,27 @@ describe('Insight model', function() {
sinon.stub(blockchain, "getTransactions", function(addresses, from, to, cb) { sinon.stub(blockchain, "getTransactions", function(addresses, from, to, cb) {
cb(null, [{ cb(null, [{
vin: [{ vin: [{
addr: '2NATQJnaQe2CUKLyhL1zdNkttJM1dUH9HaM' addr: '2NATQJnaQe2CUKLyhL1zdNkttJM1dUH9HaM'
}], }],
vout: [] vout: []
}, { }, {
vin: [{ vin: [{
addr: '2NATQJnaQe2CUKLyhL1zdNkttJM1dUH9HaM' addr: '2NATQJnaQe2CUKLyhL1zdNkttJM1dUH9HaM'
}], }],
vout: [] vout: []
}, { }, {
vin: [{ vin: [{
addr: '2N9D5bcCQ2bPWUDByQ6Qb5bMgMtgsk1rw3x' addr: '2N9D5bcCQ2bPWUDByQ6Qb5bMgMtgsk1rw3x'
}], }],
vout: [] vout: []
}, { }, {
vin: [], vin: [],
vout: [{ vout: [{
scriptPubKey: { scriptPubKey: {
addresses: ['2NFjCBFZSsxiwWAD7CKQ3hzWFtf9DcqTucY'] addresses: ['2NFjCBFZSsxiwWAD7CKQ3hzWFtf9DcqTucY']
} }
}] }]
}]); }]);
}); });
@ -364,7 +386,9 @@ describe('Insight model', function() {
blockchain.on('connect', function() { blockchain.on('connect', function() {
var socket = blockchain.getSocket(); var socket = blockchain.getSocket();
socket.emit('2NFjCBFZSsxiwWAD7CKQ3hzWFtf9DcqTucY', '1123'); socket.emit('2NFjCBFZSsxiwWAD7CKQ3hzWFtf9DcqTucY', '1123');
setTimeout(function() { done(); }, 20); setTimeout(function() {
done();
}, 20);
}); });
blockchain.on('tx', function(ev) { blockchain.on('tx', function(ev) {
@ -392,5 +416,17 @@ describe('Insight model', function() {
}); });
}); });
}); });
describe('setCompleteUrl', function() {
it('should check setCompleteUrl', function() {
var url;
expect(Insight.setCompleteUrl(url)).to.be.undefined;
url = '';
Insight.setCompleteUrl(url).should.be.equal(url);
url = 'http://mydomain.com';
Insight.setCompleteUrl(url).should.be.equal('http://mydomain.com:80');
url = 'https://mydomain.com';
Insight.setCompleteUrl(url).should.be.equal('https://mydomain.com:443');
});
});
}); });