copay/test/Identity.js

796 lines
22 KiB
JavaScript
Raw Normal View History

'use strict';
2014-10-25 15:57:12 -07:00
var _ = require('lodash');
var chai = chai || require('chai');
2014-10-23 13:34:01 -07:00
var sinon = sinon || require('sinon');
var should = chai.should();
2014-10-20 08:55:18 -07:00
var PluginManager = require('../js/models/PluginManager');
var Insight = require('../js/models/Insight');
2014-10-01 06:33:06 -07:00
var Identity = copay.Identity;
2014-10-25 13:14:04 -07:00
var Wallet = copay.Wallet;
2014-10-01 06:33:06 -07:00
var Passphrase = copay.Passphrase;
2014-12-17 19:38:00 -08:00
var version = copay.version;
2014-10-25 13:14:04 -07: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-26 17:06:43 -07:00
describe('Identity model', function() {
2014-10-25 15:38:07 -07:00
var wallet;
2014-09-28 14:38:06 -07:00
var email = '[email protected]';
var password = 'password';
2014-10-25 13:14:04 -07: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 13:14:04 -07:00
version: '0.0.1'
};
2014-10-25 15:38:07 -07:00
2014-10-25 13:14:04 -07:00
function getDefaultParams() {
2014-10-25 15:57:12 -07:00
var params = _.cloneDeep(config);
2014-10-25 13:14:04 -07: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-12-04 07:32:06 -08:00
var wid = 0;
2014-12-17 19:38:00 -08:00
2014-10-27 10:53:40 -07:00
function getNewWallet(args) {
var w = sinon.stub();
2014-12-04 07:32:06 -08:00
w.getId = sinon.stub().returns('wid' + (++wid));
2014-10-27 10:53:40 -07:00
w.getStorageKey = sinon.stub().returns('wkey');
w.toObj = sinon.stub().returns({
obj: 1
});
w.getName = sinon.stub().returns('name');
2014-12-02 12:24:37 -08:00
w.setVersion = sinon.stub();
2014-10-27 10:53:40 -07:00
w.on = sinon.stub();
w.netStart = sinon.stub();
w.args = args;
return w;
2014-10-25 15:38:07 -07:00
}
2014-10-27 10:53:40 -07:00
var walletClass = function(args) {
return getNewWallet(args);
};
2014-10-25 13:14:04 -07:00
function createIdentity(done) {
// TODO (eordano): Change this to proper dependency injection
2014-10-25 15:57:12 -07:00
var blockchain = new FakeBlockchain(config.blockchain);
var params = getDefaultParams();
2014-10-25 13:14:04 -07:00
blockchain.on = sinon.stub();
Wallet._newInsight = sinon.stub().returns(blockchain);
2014-10-25 15:38:07 -07:00
return {
blockchain: blockchain,
storage: params.storage,
wallet: wallet,
params: params
};
2014-10-25 13:14:04 -07:00
};
2014-11-20 13:13:11 -08:00
var orig;
beforeEach(function() {
orig = Identity.prototype.store;
sinon.stub(Identity.prototype, 'store').yields(null);
});
afterEach(function() {
Identity.prototype.store = orig;
});
2014-10-25 13:14:04 -07: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 14:38:06 -07:00
});
2014-10-25 13:14:04 -07:00
});
2014-09-28 14:38:06 -07:00
2014-10-25 13:14:04 -07:00
describe('Identity.create()', function() {
2014-10-31 15:11:16 -07:00
it('should create and store identity', function() {
2014-10-25 15:57:12 -07:00
var args = createIdentity();
args.blockchain.on = sinon.stub();
2014-10-30 14:08:50 -07:00
Identity.create(args.params, function(err, iden) {
should.not.exist(err);
should.exist(iden);
should.exist(iden.wallets);
2014-11-26 10:15:12 -08:00
iden.store.calledOnce.should.be.true;
iden.store.restore();
2014-10-30 14:08:50 -07:00
});
2014-09-28 14:38:06 -07:00
});
2014-10-25 13:14:04 -07:00
});
2014-09-28 14:38:06 -07:00
2014-10-25 16:10:54 -07:00
describe('#open', function(done) {
2014-10-27 10:53:40 -07:00
it.skip('should return last focused wallet', function(done) {
2014-10-25 13:14:04 -07:00
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 15:57:12 -07:00
var args = createIdentity();
Identity.create(args.params, function(err, identity) {
// TODO: Add checks for what is this testing
2014-10-25 13:14:04 -07:00
done();
});
});
});
2014-10-25 13:14:04 -07:00
2014-12-18 13:23:17 -08:00
describe('#openWallets', function(done) {
it('should emit noWallets', function() {
var iden = new Identity(getDefaultParams());
sinon.spy(iden, 'emitAndKeepAlive');
iden.openWallets();
iden.emitAndKeepAlive.calledOnce.should.be.true;
iden.emitAndKeepAlive.getCall(0).args[0].should.equal('noWallets');
});
});
2014-12-01 10:37:28 -08:00
describe('#remove', function(done) {
2014-12-17 19:38:00 -08:00
it('should remove empty profile', function(done) {
2014-12-01 10:37:28 -08:00
var storage = sinon.stub();
storage.setCredentials = sinon.stub();
storage.removeItem = sinon.stub().yields(null);
2014-12-10 05:49:25 -08:00
storage.clear = sinon.stub().yields();
2014-12-01 10:37:28 -08:00
var opts = {
email: '[email protected]',
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);
2014-12-17 19:38:00 -08:00
iden.remove(null, function(err, res) {
2014-12-01 10:37:28 -08:00
should.not.exist(err);
storage.removeItem.calledOnce.should.be.true;
storage.removeItem.getCall(0).args[0].should.equal(iden.getId());
2014-12-10 05:49:25 -08:00
storage.clear.calledOnce.should.be.true;
2014-12-01 10:37:28 -08:00
done();
});
});
it('should remove profile and wallets', function(done) {
var storage = sinon.stub();
storage.setCredentials = sinon.stub();
storage.removeItem = sinon.stub().yields(null);
2014-12-10 05:49:25 -08:00
storage.clear = sinon.stub().yields();
2014-12-01 10:37:28 -08:00
var opts = {
email: '[email protected]',
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);
_.each(_.range(3), function(i) {
var w = {
on: sinon.stub().yields(null),
getId: sinon.stub().returns('wallet' + i),
getName: sinon.stub().returns('wallet' + i),
close: sinon.stub(),
};
iden.wallets[w.getId()] = w;
2014-12-01 10:37:28 -08:00
});
iden.remove(null, function(err, res) {
should.not.exist(err);
storage.removeItem.callCount.should.equal(4);
storage.removeItem.getCall(0).args[0].should.equal(Wallet.getStorageKey('wallet0'));
storage.removeItem.getCall(3).args[0].should.equal(iden.getId());
2014-12-10 05:49:25 -08:00
storage.clear.calledOnce.should.be.true;
2014-12-01 10:37:28 -08:00
done();
});
});
});
2014-12-02 12:24:37 -08:00
describe('#storeWallet', function() {
var iden = null;
var storage = null;
beforeEach(function() {
storage = sinon.stub();
storage.setCredentials = sinon.stub();
var opts = {
email: '[email protected]',
password: '123',
network: {
testnet: {
url: 'https://test-insight.bitpay.com:443'
},
livenet: {
url: 'https://insight.bitpay.com:443'
},
},
storage: storage,
};
iden = new Identity(opts);
});
2014-12-17 19:38:00 -08:00
it('should store a simple wallet', function(done) {
2014-12-02 12:24:37 -08:00
storage.setItem = sinon.stub().yields(null);
var w = {
2014-12-17 19:38:00 -08:00
toObj: sinon.stub().returns({
key1: 'val1'
}),
2014-12-02 12:24:37 -08:00
getStorageKey: sinon.stub().returns('storage_key'),
getName: sinon.stub().returns('name'),
setVersion: sinon.spy(),
};
2014-12-17 19:38:00 -08:00
iden.storeWallet(w, function(err) {
2014-12-02 12:24:37 -08:00
should.not.exist(err);
storage.setItem.calledOnce.should.be.true;
2014-12-17 19:38:00 -08:00
storage.setItem.calledWith('storage_key', {
key1: 'val1'
});
2014-12-02 12:24:37 -08:00
done();
});
});
2014-12-18 13:23:17 -08:00
it('should return error because the limit has been reached', function(done) {
storage.setItem = sinon.stub().yields('OVERQUOTA');
var w = {
toObj: sinon.stub().returns({
key1: 'val1'
}),
getStorageKey: sinon.stub().returns('storage_key'),
getName: sinon.stub().returns('name'),
setVersion: sinon.spy(),
sizes: sinon.stub().returns(99),
getId: sinon.spy(),
};
iden.storeWallet(w, function(err) {
should.exist(err);
err.should.be.equal('OVERQUOTA');
done();
});
});
it('should return error', function(done) {
storage.setItem = sinon.stub().yields('UNKNOWN');
var w = {
toObj: sinon.stub().returns({
key1: 'val1'
}),
getStorageKey: sinon.stub().returns('storage_key'),
getName: sinon.stub().returns('name'),
setVersion: sinon.spy(),
sizes: sinon.stub().returns(99),
getId: sinon.spy(),
};
iden.storeWallet(w, function(err) {
should.exist(err);
err.should.be.equal('UNKNOWN');
done();
});
});
2014-12-17 19:38:00 -08:00
it('should change wallet version when storing', function(done) {
2014-12-02 12:24:37 -08:00
storage.setItem = sinon.stub().yields(null);
var w = {
2014-12-17 19:38:00 -08:00
toObj: sinon.stub().returns({
key1: 'val1'
}),
2014-12-02 12:24:37 -08:00
getStorageKey: sinon.stub().returns('storage_key'),
getName: sinon.stub().returns('name'),
setVersion: sinon.spy(),
version: '1.0',
2014-12-17 19:38:00 -08:00
opts: {
version: '1.0'
},
2014-12-02 12:24:37 -08:00
};
iden.version = '2.0';
2014-12-17 19:38:00 -08:00
iden.storeWallet(w, function(err) {
2014-12-02 12:24:37 -08:00
should.not.exist(err);
w.setVersion.calledWith('2.0').should.be.true;
done();
});
});
2014-09-28 16:50:37 -07:00
});
2014-09-28 17:22:53 -07:00
2014-10-27 09:13:08 -07:00
describe('#createWallet', function() {
2014-10-25 16:10:54 -07:00
var iden = null;
var args = null;
beforeEach(function() {
2014-10-25 16:10:54 -07:00
args = createIdentity();
2014-10-27 09:13:08 -07:00
args.params.noWallets = true;
2014-10-27 10:53:40 -07:00
var old = Identity.prototype.createWallet;
2014-10-30 14:08:50 -07:00
Identity.create(args.params, function(err, res) {
iden = res;
});
2014-09-29 02:31:04 -07:00
});
2014-11-26 10:15:12 -08:00
afterEach(function() {
iden.store.restore();
});
2014-09-28 17:22:53 -07:00
it('should be able to create wallets with given pk', function(done) {
var priv = 'tprv8ZgxMBicQKsPdEqHcA7RjJTayxA3gSSqeRTttS1JjVbgmNDZdSk9EHZK5pc52GY5xFmwcakmUeKWUDzGoMLGAhrfr5b3MovMUZUTPqisL2m';
2014-12-30 11:30:05 -08:00
args.storage.getItem = sinon.stub().yields(null, JSON.stringify(iden));
2014-10-25 16:10:54 -07:00
args.storage.setItem = sinon.stub();
args.storage.setItem.onFirstCall().callsArg(2);
args.storage.setItem.onSecondCall().callsArg(2);
2014-10-27 09:13:08 -07:00
should.exist(walletClass, 'check walletClass stub');
2014-09-28 17:22:53 -07:00
iden.createWallet({
privateKeyHex: priv,
2014-10-27 09:13:08 -07:00
walletClass: walletClass,
2014-09-28 17:22:53 -07:00
}, function(err, w) {
should.not.exist(err);
2014-10-27 09:13:08 -07:00
w.args.privateKey.toObj().extendedPrivateKeyString.should.equal(priv);
2014-09-28 17:22:53 -07:00
done();
});
});
it('should be able to create wallets with random pk', function(done) {
2014-12-30 11:30:05 -08:00
args.storage.getItem = sinon.stub().yields(null, JSON.stringify(iden));
2014-10-25 16:10:54 -07: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-10-27 09:13:08 -07:00
iden.createWallet({
walletClass: walletClass,
}, function(err, w1) {
2014-10-25 16:10:54 -07:00
should.exist(w1);
2014-12-30 11:30:05 -08:00
args.storage.getItem = sinon.stub().yields(null, JSON.stringify(iden));
2014-10-27 09:13:08 -07:00
iden.createWallet({
walletClass: walletClass,
}, function(err, w2) {
2014-10-25 16:10:54 -07:00
should.exist(w2);
2014-10-27 09:13:08 -07:00
w2.args.privateKey.toObj().extendedPrivateKeyString.should.not.equal(
w1.args.privateKey.toObj().extendedPrivateKeyString
2014-10-27 10:53:40 -07:00
); + done();
2014-09-28 17:22:53 -07:00
});
});
});
});
2014-10-25 16:10:54 -07:00
describe('#retrieveWalletFromStorage', function() {
2014-10-27 10:53:40 -07:00
2014-10-25 16:10:54 -07:00
it('should return wallet', function(done) {
var args = createIdentity();
args.storage.getItem.onFirstCall().callsArgWith(1, null, '{"wallet": "fakeData"}');
var backup = Wallet.fromUntrustedObj;
2014-10-27 10:53:40 -07:00
args.params.noWallets = true;
sinon.stub().returns(args.wallet);
var opts = {
importWallet: sinon.stub().returns(getNewWallet()),
};
2014-10-30 14:08:50 -07:00
Identity.create(args.params, function(err, iden) {
iden.retrieveWalletFromStorage('dummy', opts, function(err, wallet) {
should.not.exist(err);
opts.importWallet.calledOnce.should.equal(true);
should.exist(wallet);
2014-11-26 10:15:12 -08:00
iden.store.restore();
2014-10-30 14:08:50 -07:00
done();
});
2014-09-29 12:55:45 -07:00
});
});
});
// This is implemented in Compatibility
describe.skip('#importWallet', function() {
2014-10-28 11:56:03 -07:00
it('should import a wallet, call the right encryption functions', function(done) {
2014-10-27 13:23:01 -07:00
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);
2014-11-26 10:15:12 -08:00
sinon.stub(Identity.prototype, 'store').yields(null);
2014-10-27 13:23:01 -07:00
var fakeCrypto = {
kdf: sinon.stub().returns('passphrase'),
decrypt: sinon.stub().returns('{"walletId":123}'),
2014-10-27 13:23:01 -07:00
};
var opts = {
importWallet: sinon.stub().returns(getNewWallet()),
cryptoUtil: fakeCrypto,
};
2014-10-30 14:08:50 -07:00
Identity.create(args.params, function(err, iden) {
sinon.stub(iden, 'importWalletFromObj').yields(null);
iden.importEncryptedWallet(123, 'password', opts, function(err) {
should.not.exist(err);
2014-11-07 12:35:32 -08:00
fakeCrypto.decrypt.getCall(0).args[0].should.equal('password');
2014-10-30 14:08:50 -07:00
fakeCrypto.decrypt.getCall(0).args[1].should.equal(123);
2014-11-26 10:15:12 -08:00
iden.store.restore();
2014-10-30 14:08:50 -07:00
done();
});
2014-10-27 13:23:01 -07:00
});
});
});
2014-10-21 05:57:54 -07:00
describe('#export', function() {
2014-10-15 12:24:21 -07:00
});
2014-10-21 08:03:09 -07:00
describe('#import', function() {
2014-10-21 05:57:54 -07:00
});
2014-10-25 13:14:04 -07:00
/**
* TODO (eordano): Move this to a different test file
*
2014-10-20 08:55:18 -07:00
describe('#pluginManager', function() {
it('should create a new PluginManager object', function() {
2014-10-21 12:14:34 -07:00
var pm = new PluginManager({plugins: { FakeLocalStorage: true }, pluginsPath: '../../test/mocks/'});
2014-10-20 08:55:18 -07: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 13:14:04 -07:00
*/
2014-09-27 14:56:25 -07:00
describe('#joinWallet', function() {
var opts = {
secret: '8WtTuiFTkhP5ao7AF2QErSwV39Cbur6pdMebKzQXFqL59RscXM',
nickname: 'test',
2014-10-01 04:35:17 -07:00
password: 'pass'
};
2014-10-25 15:57:12 -07:00
var iden = null;
var args = null;
var net = null;
beforeEach(function() {
2014-10-25 15:57:12 -07:00
args = createIdentity();
args.params.Async = net = sinon.stub();
net.cleanUp = sinon.spy();
net.on = sinon.stub();
net.start = sinon.spy();
2014-10-27 10:53:40 -07:00
var old = Identity.prototype.createWallet;
2014-10-30 14:08:50 -07:00
Identity.create(args.params, function(err, res) {
iden = res;
2014-11-26 10:15:12 -08:00
iden.store.restore();
2014-10-30 14:08:50 -07:00
});
2014-10-25 15:57:12 -07:00
});
it('should yield bad network error', function(done) {
2014-10-07 14:33:55 -07:00
var net = sinon.stub();
net.greet = sinon.stub();
2014-09-27 14:53:34 -07: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 14:33:55 -07:00
opts.privHex = undefined;
2014-10-25 16:10:54 -07:00
opts.Async = net;
2014-09-28 14:38:06 -07:00
iden.joinWallet(opts, function(err, w) {
err.should.equal('badNetwork');
done();
});
});
2014-10-26 17:06:43 -07:00
it('should callback with a join error in case of a problem', function(done) {
opts.privHex = undefined;
2014-10-07 14:33:55 -07:00
var net = sinon.stub();
net.greet = sinon.stub();
2014-09-27 14:53:34 -07:00
net.cleanUp = sinon.stub();
net.start = sinon.stub().yields(null);
2014-09-28 14:38:06 -07:00
net.on = sinon.stub();
net.on.withArgs('serverError').yields(null);
net.on.withArgs('data').yields('senderId', {
type: 'walletId',
2014-09-28 14:38:06 -07:00
networkName: iden.networkName,
});
2014-10-26 17:06:43 -07:00
opts.Async = net;
2014-10-07 14:33:55 -07:00
2014-09-28 14:38:06 -07:00
iden.joinWallet(opts, function(err, w) {
2014-10-26 17:06:43 -07:00
err.should.equal('joinError');
done();
});
});
2014-10-26 17:06:43 -07:00
it('should return walletFull', function(done) {
net = sinon.stub();
net.on = sinon.stub();
net.start = sinon.stub();
net.start.onFirstCall().callsArg(1);
net.greet = sinon.stub();
iden.createWallet = sinon.stub();
2014-10-30 14:08:50 -07:00
iden.createWallet.onFirstCall().yields(null, null);
net.on.withArgs('data').yields('senderId', {
type: 'walletId',
networkName: 'testnet',
opts: {},
});
2014-10-26 17:06:43 -07:00
opts.privHex = undefined;
opts.Async = net;
2014-09-28 14:38:06 -07:00
iden.joinWallet(opts, function(err, w) {
2014-10-26 17:06:43 -07:00
err.should.equal('walletFull');
done();
});
});
2014-10-26 17:06:43 -07:00
it('should accept a priv key as an input', function(done) {
net = sinon.stub();
net.on = sinon.stub();
net.start = sinon.stub();
net.start.onFirstCall().callsArg(1);
net.greet = sinon.stub();
iden.createWallet = sinon.stub();
2014-10-27 09:13:08 -07:00
var fakeWallet = {
sendWalletReady: _.noop
};
2014-10-26 17:06:43 -07:00
iden.createWallet.onFirstCall().yields(null, fakeWallet);
net.on.withArgs('data').yields('senderId', {
type: 'walletId',
networkName: 'testnet',
opts: {},
});
opts.privHex = 'tprv8ZgxMBicQKsPf7MCvCjnhnr4uiR2Z2gyNC27vgd9KUu98F9mM1tbaRrWMyddVju36GxLbeyntuSadBAttriwGGMWUkRgVmUUCg5nFioGZsd';
2014-10-26 17:06:43 -07:00
opts.Async = net;
2014-09-28 14:38:06 -07:00
iden.joinWallet(opts, function(err, w) {
2014-10-26 17:06:43 -07:00
w.should.equal(fakeWallet);
2014-10-25 15:57:12 -07:00
done();
});
});
});
2014-12-17 19:38:00 -08:00
describe('add / delete / list Wallets', 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: '[email protected]',
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 add wallet', function() {
iden.addWallet(w);
iden.getWalletById('32').getName().should.equal('treintaydos');
iden.walletIds.should.deep.equal(['32']);
_.find(iden.getWallets(), function(w) {
return w.getName() == 'treintaydos';
}).should.deep.equal(w);
});
it('should not add same wallet twice', function() {
iden.addWallet(w);
iden.addWallet(w);
iden.getWalletById('32').getName().should.equal('treintaydos');
iden.walletIds.should.deep.equal(['32']);
_.find(iden.getWallets(), function(w) {
return w.getName() == 'treintaydos';
}).should.deep.equal(w);
});
2014-12-18 13:23:17 -08:00
2014-12-17 19:38:00 -08:00
it('should delete wallet', function(done) {
iden.addWallet(w);
iden.getWalletById('32').getName().should.equal('treintaydos');
2014-12-30 11:30:05 -08:00
iden.storage.getItem = sinon.stub().yields(null, JSON.stringify(iden));
2014-12-17 19:38:00 -08:00
iden.deleteWallet('32', function(err) {
should.not.exist(iden.getWalletById('32'));
iden.walletIds.should.deep.equal([]);
should.not.exist(_.find(iden.getWallets(), function(w) {
return w.getName() == 'treintaydos';
}));
done();
});
});
});
describe('toObj', function() {
var iden, w, w2;
beforeEach(function() {
var storage = sinon.stub();
storage.setCredentials = sinon.stub();
storage.removeItem = sinon.stub().yields(null);
storage.clear = sinon.stub().yields();
var opts = {
email: '[email protected]',
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(),
};
w2 = {
getId: sinon.stub().returns('33'),
getName: sinon.stub().returns('treintaytres'),
close: sinon.stub(),
};
});
it('should include wallets', function() {
iden.addWallet(w);
var obj = iden.toObj();
2014-12-18 13:23:17 -08:00
_.indexOf(obj.walletIds, '32').should.be.above(-1);
2014-12-17 19:38:00 -08:00
});
it('should set version to actual version', function() {
var obj = iden.toObj();
obj.version.should.equal(version);
});
it('should include 2 wallets', function() {
iden.addWallet(w);
iden.addWallet(w2);
var obj = iden.toObj();
2014-12-18 13:23:17 -08:00
_.indexOf(obj.walletIds, '32').should.be.above(-1);
_.indexOf(obj.walletIds, '33').should.be.above(-1);
});
});
describe('#_cleanUp', function() {
var iden, w, w2;
beforeEach(function() {
var storage = sinon.stub();
storage.setCredentials = sinon.stub();
storage.removeItem = sinon.stub().yields(null);
storage.clear = sinon.stub().yields();
var opts = {
email: '[email protected]',
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(),
};
w2 = {
getId: sinon.stub().returns('33'),
getName: sinon.stub().returns('treintaytres'),
close: sinon.stub(),
};
iden.addWallet(w);
iden.addWallet(w2);
});
it('should close all wallets', function() {
_.size(iden.wallets).should.be.equal(2);
iden._cleanUp();
_.size(iden.wallets).should.be.equal(0);
2014-12-17 19:38:00 -08:00
});
});
2014-09-30 04:16:58 -07:00
});