diff --git a/js/models/Identity.js b/js/models/Identity.js index 84868bafd..fb3aafacc 100644 --- a/js/models/Identity.js +++ b/js/models/Identity.js @@ -109,7 +109,7 @@ Identity.create = function(opts, cb) { }); }; -Identity.prototype.resendVerificationEmail = function (cb) { +Identity.prototype.resendVerificationEmail = function(cb) { var self = this; preconditions.checkArgument(_.isFunction(cb)); @@ -152,7 +152,7 @@ Identity.open = function(opts, cb) { }); }; -Identity.prototype.verifyChecksum = function (cb) { +Identity.prototype.verifyChecksum = function(cb) { var self = this; self.storage.getItem(Identity.getKeyForEmail(self.email), function(err, data, headers) { @@ -202,11 +202,9 @@ Identity.prototype.addWallet = function(w) { */ Identity.prototype.deleteWallet = function(walletId, cb) { preconditions.checkArgument(_.isString(walletId)); - var self = this; - - self.verifyChecksum(function (err, match) { + self.verifyChecksum(function(err, match) { if (err) return cb(err); if (!match) return cb('The profile is out of sync. Please re-login to get the latest changes.'); @@ -222,6 +220,7 @@ Identity.prototype.deleteWallet = function(walletId, cb) { self.emitAndKeepAlive('walletDeleted', walletId); self.store(null, cb); }); + }); }; @@ -375,7 +374,7 @@ Identity.prototype.setBackupNeeded = function(backupNeeded) { self.backupNeeded = !!backupNeeded; - self.verifyChecksum(function (err, match) { + self.verifyChecksum(function(err, match) { if (err) return cb(err); if (!match) return cb('The profile is out of sync. Please re-login to get the latest changes.'); @@ -449,8 +448,11 @@ Identity.prototype.remove = function(opts, cb) { }; Identity.prototype._cleanUp = function() { + var self = this; + _.each(this.getWallets(), function(w) { w.close(); + delete self.wallets[w.getId()]; }); }; @@ -458,7 +460,6 @@ Identity.prototype._cleanUp = function() { * @desc Closes the wallet and disconnects all services */ Identity.prototype.close = function() { - this._cleanUp(); this.emitAndKeepAlive('closed'); }; @@ -623,10 +624,10 @@ Identity.prototype.bindWallet = function(w) { */ Identity.prototype.createWallet = function(opts, cb) { preconditions.checkArgument(cb); - + var self = this; - self.verifyChecksum(function (err, match) { + self.verifyChecksum(function(err, match) { if (err) return cb(err); if (!match) return cb('The profile is out of sync. Please re-login to get the latest changes.'); diff --git a/test/Identity.js b/test/Identity.js index 038ca7d34..8a9ee91bf 100644 --- a/test/Identity.js +++ b/test/Identity.js @@ -168,6 +168,17 @@ describe('Identity model', function() { }); }); + + 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'); + }); + }); + describe('#remove', function(done) { it('should remove empty profile', function(done) { var storage = sinon.stub(); @@ -285,6 +296,48 @@ describe('Identity model', function() { done(); }); }); + + 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(); + }); + }); + + + + it('should change wallet version when storing', function(done) { storage.setItem = sinon.stub().yields(null); var w = { @@ -620,7 +673,7 @@ describe('Identity model', function() { }).should.deep.equal(w); }); - + it('should delete wallet', function(done) { iden.addWallet(w); @@ -676,7 +729,7 @@ describe('Identity model', function() { it('should include wallets', function() { iden.addWallet(w); var obj = iden.toObj(); - _.indexOf(obj.walletIds,'32').should.be.above(-1); + _.indexOf(obj.walletIds, '32').should.be.above(-1); }); it('should set version to actual version', function() { @@ -690,8 +743,53 @@ describe('Identity model', function() { iden.addWallet(w); iden.addWallet(w2); var obj = iden.toObj(); - _.indexOf(obj.walletIds,'32').should.be.above(-1); - _.indexOf(obj.walletIds,'33').should.be.above(-1); + _.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: '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(), + }; + 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); }); }); });