From 3f65288dca6963d7a2d53c194a756e2c5d5a349c Mon Sep 17 00:00:00 2001 From: Matias Pando Date: Thu, 20 Nov 2014 18:13:11 -0300 Subject: [PATCH] Added the flag backupNeeded --- js/models/Identity.js | 25 +++++++++++++-------- test/Identity.js | 47 ++++++++++++++++++++++++++++++++++++++-- views/includes/head.html | 3 +++ 3 files changed, 64 insertions(+), 11 deletions(-) diff --git a/js/models/Identity.js b/js/models/Identity.js index 8062b3ba9..bb8a4113a 100644 --- a/js/models/Identity.js +++ b/js/models/Identity.js @@ -62,6 +62,8 @@ function Identity(opts) { this.walletIds = opts.walletIds || {}; this.wallets = opts.wallets || {}; this.focusedTimestamps = opts.focusedTimestamps || {}; + this.backupNeeded = opts.backupNeeded || false; + }; @@ -91,7 +93,9 @@ Identity.prototype.getName = function() { * @return {undefined} */ Identity.create = function(opts, cb) { - opts = _.extend({}, opts); + opts = _.extend({ + backupNeeded: true + }, opts); var iden = new Identity(opts); iden.store(_.extend(opts, { @@ -265,11 +269,12 @@ Identity.prototype.toObj = function() { return _.extend({ walletIds: _.isEmpty(this.wallets) ? this.walletsIds : _.keys(this.wallets), }, - _.pick(this, 'version', 'fullName', 'password', 'email', 'focusedTimestamps')); + _.pick(this, 'version', 'fullName', 'password', 'email', 'backupNeeded', 'focusedTimestamps')); }; Identity.prototype.exportEncryptedWithWalletInfo = function(opts) { var crypto = opts.cryptoUtil || cryptoUtil; + this.backupNeeded = false; return crypto.encrypt(this.password, this.exportWithWalletInfo(opts)); }; @@ -279,7 +284,7 @@ Identity.prototype.exportWithWalletInfo = function(opts) { return wallet.toObj(); }) }, - _.pick(this, 'version', 'fullName', 'password', 'email') + _.pick(this, 'version', 'fullName', 'password', 'email', 'backupNeeded') ); }; @@ -288,10 +293,9 @@ Identity.prototype.exportWithWalletInfo = function(opts) { * @param {Function} cb */ Identity.prototype.store = function(opts, cb) { - log.debug('Storing profile'); - var self = this; opts = opts || {}; + opts.backupNeeded = false; var storeFunction = opts.failIfExists ? self.storage.createItem : self.storage.setItem; @@ -323,9 +327,9 @@ Identity.prototype.remove = function(opts, cb) { if (err) return cb(err); cb(); }); - }, function (err) { + }, function(err) { if (err) return cb(err); - + self.storage.removeItem(self.getId(), function(err) { if (err) return cb(err); self.emitAndKeepAlive('closed'); @@ -552,13 +556,16 @@ Identity.prototype.createWallet = function(opts, cb) { var self = this; + var w = new walletClass(opts); self.bindWallet(w); self.updateFocusedTimestamp(w.getId()); self.storeWallet(w, function(err) { - if (err) return cb(err); + if (err) return cb(err); << << << < HEAD === === = + + self.backupNeeded = true; >>> >>> > Added the flag backupNeeded self.store({ - noWallets: true + noWallets: true, }, function(err) { return cb(err, w); }); diff --git a/test/Identity.js b/test/Identity.js index f46c72f0e..bf1da2b1f 100644 --- a/test/Identity.js +++ b/test/Identity.js @@ -111,7 +111,14 @@ describe('Identity model', function() { params: params }; }; - + var orig; + beforeEach(function() { + orig = Identity.prototype.store; + sinon.stub(Identity.prototype, 'store').yields(null); + }); + afterEach(function() { + Identity.prototype.store = orig; + }); describe('new Identity()', function() { it('returns an identity', function() { var iden = new Identity(getDefaultParams()); @@ -124,7 +131,6 @@ describe('Identity model', function() { it('should create and store identity', function() { var args = createIdentity(); args.blockchain.on = sinon.stub(); - sinon.stub(Identity.prototype, 'store').yields(null); Identity.create(args.params, function(err, iden) { should.not.exist(err); should.exist(iden); @@ -492,4 +498,41 @@ describe('Identity model', function() { }); }); }); + + describe('Identity backupNeeded', function() { + + it('should create Profile with backupNeeded set to true', function(done) { + var args = createIdentity(); + Identity.create(args.params, function(err, iden) { + should.not.exist(err); + iden.backupNeeded.should.be.true; + done(); + }); + }); + + it('making a backup should set backupNeeded set to false', function(done) { + var args = createIdentity(); + Identity.create(args.params, function(err, iden) { + should.not.exist(err); + iden.exportEncryptedWithWalletInfo(iden.password) + iden.backupNeeded.should.be.false; + done(); + }); + }); + + it('adding a wallet should set backupNeeded to true', function(done) { + var args = createIdentity(); + Identity.create(args.params, function(err, iden) { + should.not.exist(err); + iden.exportEncryptedWithWalletInfo(iden.password); + iden.createWallet({ + walletClass: walletClass, + }, function(err, w2) { + iden.backupNeeded.should.be.true; + done(); + }); + }); + }); + + }); }); diff --git a/views/includes/head.html b/views/includes/head.html index 87bf9307f..5a2197576 100644 --- a/views/includes/head.html +++ b/views/includes/head.html @@ -5,6 +5,9 @@ + + BACKUP! +