Wallet/test/Identity.js

364 lines
9.7 KiB
JavaScript
Raw Normal View History

'use strict';
2014-10-25 19:57:12 -03:00
var _ = require('lodash');
var chai = chai || require('chai');
2014-10-23 17:34:01 -03:00
var sinon = sinon || require('sinon');
var should = chai.should();
2014-10-20 12:55:18 -03:00
var PluginManager = require('../js/models/PluginManager');
var Insight = require('../js/models/Insight');
2014-10-01 10:33:06 -03:00
var Identity = copay.Identity;
2014-10-25 17:14:04 -03:00
var Wallet = copay.Wallet;
2014-10-01 10:33:06 -03:00
var Passphrase = copay.Passphrase;
2014-10-25 17:14:04 -03:00
var FakeBlockchain = require('./mocks/FakeBlockchain');
var PERSISTED_PROPERTIES = (copay.Wallet || require('../js/models/Wallet')).PERSISTED_PROPERTIES;
function assertObjectEqual(a, b) {
PERSISTED_PROPERTIES.forEach(function(k) {
if (a[k] && b[k]) {
_.omit(a[k], 'name').should.be.deep.equal(b[k], k + ' differs');
}
})
}
2014-10-25 20:10:54 -03:00
describe.only('Identity model', function() {
2014-10-25 19:38:07 -03:00
var wallet;
2014-09-28 18:38:06 -03:00
var email = 'hola@hola.com';
var password = 'password';
2014-10-25 17:14:04 -03:00
var blockchain;
var config = {
walletDefaults: {
requiredCopayers: 3,
totalCopayers: 5,
spendUnconfirmed: 1,
reconnectDelay: 100,
},
blockchain: {
host: 'test.insight.is',
port: 80,
schema: 'https'
},
networkName: 'testnet',
passphrase: {
iterations: 100,
storageSalt: 'mjuBtGybi/4=',
},
// network layer config
network: {
testnet: {
url: 'https://test-insight.bitpay.com:443'
},
livenet: {
url: 'https://insight.bitpay.com:443'
},
},
2014-10-25 17:14:04 -03:00
version: '0.0.1'
};
2014-10-25 19:38:07 -03:00
2014-10-25 17:14:04 -03:00
function getDefaultParams() {
2014-10-25 19:57:12 -03:00
var params = _.cloneDeep(config);
2014-10-25 17:14:04 -03:00
_.extend(params, {
email: email,
password: password
});
params.storage = sinon.stub();
params.storage.setCredentials = sinon.stub();
params.storage.getItem = sinon.stub();
params.storage.setItem = sinon.stub();
params.storage.setItem.onFirstCall().callsArgWith(2, null);
params.storage.setItem.onSecondCall().callsArgWith(2, null);
return params;
}
2014-10-25 19:38:07 -03:00
function getNewWallet() {
var wallet = sinon.stub();
wallet.on = sinon.stub().yields(null);
wallet.netStart = sinon.stub();
wallet.toObj = sinon.stub();
wallet.getName = sinon.stub().returns('walletname');
wallet.getId = sinon.stub().returns('wid:123');
return wallet;
}
2014-10-25 17:14:04 -03:00
function createIdentity(done) {
// TODO (eordano): Change this to proper dependency injection
2014-10-25 19:57:12 -03:00
var blockchain = new FakeBlockchain(config.blockchain);
var params = getDefaultParams();
2014-10-25 17:14:04 -03:00
blockchain.on = sinon.stub();
Wallet._newInsight = sinon.stub().returns(blockchain);
2014-10-25 19:57:12 -03:00
var wallet = getNewWallet();
2014-10-25 17:14:04 -03:00
Identity._newWallet = sinon.stub().returns(wallet);
2014-10-25 19:38:07 -03:00
return {
blockchain: blockchain,
storage: params.storage,
wallet: wallet,
params: params
};
2014-10-25 17:14:04 -03:00
};
describe('new Identity()', function() {
it('returns an identity', function() {
var iden = new Identity(getDefaultParams());
should.exist(iden);
iden.walletDefaults.should.deep.equal(config.walletDefaults);
2014-09-28 18:38:06 -03:00
});
2014-10-25 17:14:04 -03:00
});
2014-09-28 18:38:06 -03:00
2014-10-25 17:14:04 -03:00
describe('Identity.create()', function() {
it('should call .store', function(done) {
2014-10-25 19:57:12 -03:00
var args = createIdentity();
args.blockchain.on = sinon.stub();
Identity.create(args.params, function(err, iden) {
2014-10-25 17:14:04 -03:00
should.not.exist(err);
2014-10-25 19:38:07 -03:00
should.exist(iden.wallets);
2014-10-25 17:14:04 -03:00
done();
2014-09-28 18:38:06 -03:00
});
});
2014-10-25 17:14:04 -03:00
});
2014-09-28 18:38:06 -03:00
2014-10-25 20:10:54 -03:00
describe('#open', function(done) {
2014-10-25 17:14:04 -03:00
it('should return last focused wallet', function(done) {
var wallets = [{
id: 'wallet1',
store: sinon.stub().yields(null),
netStart: sinon.stub(),
}, {
id: 'wallet2',
store: sinon.stub().yields(null),
netStart: sinon.stub(),
}, {
id: 'wallet3',
store: sinon.stub().yields(null),
netStart: sinon.stub(),
}];
2014-10-25 19:57:12 -03:00
var args = createIdentity();
Identity.create(args.params, function(err, identity) {
// TODO: Add checks for what is this testing
2014-10-25 17:14:04 -03:00
done();
});
});
});
2014-10-25 17:14:04 -03:00
2014-09-28 20:50:37 -03:00
describe('#store', function() {
2014-10-25 20:10:54 -03:00
it('should call .store for identity and wallets', function(done) {
var args = createIdentity();
Identity.create(args.params, function(err, identity) {
2014-09-28 20:50:37 -03:00
2014-10-25 20:10:54 -03:00
args.storage.setItem = sinon.stub();
args.storage.setItem.onFirstCall().callsArg(2);
var wallet1 = {}, wallet2 = {};
identity.storeWallet = sinon.stub();
identity.storeWallet.onFirstCall().callsArg(1);
identity.storeWallet.onSecondCall().callsArg(1);
identity.wallets = {'a': wallet1, 'b': wallet2};
identity.store({}, function(err) {
should.not.exist(err);
done();
});
2014-09-28 20:50:37 -03:00
});
});
});
2014-09-28 21:22:53 -03:00
describe('#createWallet', function() {
2014-10-25 20:10:54 -03:00
var iden = null;
var args = null;
beforeEach(function(done) {
args = createIdentity();
Identity.create(args.params, function(err, identity) {
iden = identity;
2014-09-29 06:31:04 -03:00
done();
});
});
2014-09-28 21:22:53 -03:00
it('should be able to create wallets with given pk', function(done) {
var priv = 'tprv8ZgxMBicQKsPdEqHcA7RjJTayxA3gSSqeRTttS1JjVbgmNDZdSk9EHZK5pc52GY5xFmwcakmUeKWUDzGoMLGAhrfr5b3MovMUZUTPqisL2m';
2014-10-25 20:10:54 -03:00
args.storage.setItem = sinon.stub();
args.storage.setItem.onFirstCall().callsArg(2);
args.storage.setItem.onSecondCall().callsArg(2);
2014-09-28 21:22:53 -03:00
iden.createWallet({
privateKeyHex: priv,
}, function(err, w) {
should.not.exist(err);
done();
});
});
it('should be able to create wallets with random pk', function(done) {
2014-10-25 20:10:54 -03:00
args.storage.setItem = sinon.stub();
args.storage.setItem.onCall(0).callsArg(2);
args.storage.setItem.onCall(1).callsArg(2);
args.storage.setItem.onCall(2).callsArg(2);
args.storage.setItem.onCall(3).callsArg(2);
2014-09-28 21:22:53 -03:00
iden.createWallet(null, function(err, w1) {
2014-10-25 20:10:54 -03:00
should.exist(w1);
2014-09-28 21:22:53 -03:00
iden.createWallet(null, function(err, w2) {
2014-10-25 20:10:54 -03:00
should.exist(w2);
2014-09-28 21:22:53 -03:00
done();
});
});
});
});
2014-10-25 20:10:54 -03:00
describe('#retrieveWalletFromStorage', function() {
it('should return wallet', function(done) {
var args = createIdentity();
args.storage.getItem.onFirstCall().callsArgWith(1, null, '{"wallet": "fakeData"}');
var backup = Wallet.fromUntrustedObj;
Wallet.fromUntrustedObj = sinon.stub().returns(args.wallet);
Identity.create(args.params, function(err, iden) {
iden.retrieveWalletFromStorage('dummy', function(err, wallet) {
2014-09-29 16:55:45 -03:00
should.not.exist(err);
2014-10-25 20:10:54 -03:00
should.exist(wallet);
Wallet.fromUntrustedObj = backup;
2014-09-29 19:58:00 -03:00
done();
2014-09-29 16:55:45 -03:00
});
});
});
});
2014-10-21 09:57:54 -03:00
describe('#export', function() {
2014-10-15 16:24:21 -03:00
});
2014-10-21 12:03:09 -03:00
describe('#import', function() {
2014-10-21 09:57:54 -03:00
});
2014-10-25 17:14:04 -03:00
/**
* TODO (eordano): Move this to a different test file
*
2014-10-20 12:55:18 -03:00
describe('#pluginManager', function() {
it('should create a new PluginManager object', function() {
2014-10-21 16:14:34 -03:00
var pm = new PluginManager({plugins: { FakeLocalStorage: true }, pluginsPath: '../../test/mocks/'});
2014-10-20 12:55:18 -03:00
should.exist(pm);
});
});
describe('#Insight', function() {
it('should parse a uri', function() {
var uri = Insight.setCompleteUrl('http://someurl.bitpay.com:443');
should.exist(uri);
});
});
2014-10-25 17:14:04 -03:00
*/
2014-09-27 18:56:25 -03:00
describe('#joinWallet', function() {
var opts = {
secret: '8WtTuiFTkhP5ao7AF2QErSwV39Cbur6pdMebKzQXFqL59RscXM',
nickname: 'test',
2014-10-01 08:35:17 -03:00
password: 'pass'
};
2014-10-25 19:57:12 -03:00
var iden = null;
var args = null;
var net = null;
beforeEach(function(done) {
args = createIdentity();
args.params.Async = net = sinon.stub();
net.cleanUp = sinon.spy();
net.on = sinon.stub();
net.start = sinon.spy();
Identity.create(args.params, function(err, identity) {
iden = identity;
done();
});
});
it('should yield bad network error', function(done) {
2014-10-07 18:33:55 -03:00
var net = sinon.stub();
net.greet = sinon.stub();
2014-09-27 18:53:34 -03:00
net.cleanUp = sinon.stub();
net.start = sinon.stub().yields(null);
net.on = sinon.stub();
net.on.withArgs('data').yields('senderId', {
type: 'walletId',
networkName: 'aWeirdNetworkName',
opts: {},
});
2014-10-07 18:33:55 -03:00
opts.privHex = undefined;
2014-10-25 20:10:54 -03:00
opts.Async = net;
2014-09-28 18:38:06 -03:00
iden.joinWallet(opts, function(err, w) {
err.should.equal('badNetwork');
done();
});
});
it('should yield to join error', function(done) {
opts.privHex = undefined;
2014-10-07 18:33:55 -03:00
var net = sinon.stub();
net.greet = sinon.stub();
2014-09-27 18:53:34 -03:00
net.cleanUp = sinon.stub();
net.start = sinon.stub().yields(null);
2014-09-28 18:38:06 -03:00
net.on = sinon.stub();
net.on.withArgs('serverError').yields(null);
net.on.withArgs('data').yields('senderId', {
type: 'walletId',
2014-09-28 18:38:06 -03:00
networkName: iden.networkName,
});
Identity._newAsync = function() {
return net;
};
2014-10-07 18:33:55 -03:00
2014-09-28 18:38:06 -03:00
iden.joinWallet(opts, function(err, w) {
done();
});
});
it('should call network.start / create', function(done) {
opts.privHex = undefined;
net.on.withArgs('connected').yields(null);
net.on.withArgs('data').yields('senderId', {
type: 'walletId',
networkName: 'testnet',
opts: {},
});
var w = sinon.stub();
w.sendWalletReady = sinon.spy();
2014-09-28 18:38:06 -03:00
iden.createWallet = sinon.stub().yields(null, w);
iden.joinWallet(opts, function(err, w) {
done();
});
});
it('should return walletFull', function(done) {
2014-09-28 18:38:06 -03:00
iden.joinWallet(opts, function(err, w) {
done();
});
});
it('should accept a priv key a input', function() {
opts.privHex = 'tprv8ZgxMBicQKsPf7MCvCjnhnr4uiR2Z2gyNC27vgd9KUu98F9mM1tbaRrWMyddVju36GxLbeyntuSadBAttriwGGMWUkRgVmUUCg5nFioGZsd';
2014-09-28 18:38:06 -03:00
iden.joinWallet(opts, function(err, w) {
});
});
2014-10-25 19:57:12 -03:00
it('should call network.start with private key', function(done) {
opts.privHex = undefined;
2014-09-28 18:38:06 -03:00
iden.joinWallet(opts, function(err, w) {
2014-10-25 20:10:54 -03:00
console.error(err);
2014-10-25 19:57:12 -03:00
done();
});
});
});
2014-09-30 08:16:58 -03:00
});