From f045a71ebe13f87c6ce714d2d76e80ce36a03a94 Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Fri, 4 Oct 2013 15:47:30 +0200 Subject: [PATCH] cleanup error handling in email dao --- src/js/dao/email-dao.js | 35 ++++++++++++++++++--------------- test/new-unit/email-dao-test.js | 9 ++++++--- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/js/dao/email-dao.js b/src/js/dao/email-dao.js index 1788b07..a65f568 100644 --- a/src/js/dao/email-dao.js +++ b/src/js/dao/email-dao.js @@ -320,14 +320,10 @@ define(function(require) { callback(err); return; } - if (emails.length === 0) { - callback(null, []); - return; - } // find encrypted items emails.forEach(function(i) { - if (i.body.indexOf(str.cryptPrefix) !== -1 && i.body.indexOf(str.cryptSuffix) !== -1) { + if (typeof i.body === 'string' && i.body.indexOf(str.cryptPrefix) !== -1 && i.body.indexOf(str.cryptSuffix) !== -1) { // add item to plaintext list for display later displayList.push(i); // parse ct object from ascii armored message block @@ -335,6 +331,11 @@ define(function(require) { } }); + if (encryptedList.length === 0) { + callback(null, []); + return; + } + // decrypt items decryptList(encryptedList, function(err, decryptedList) { if (err) { @@ -425,17 +426,21 @@ define(function(require) { return; } - fetchList(function(emails) { + fetchList(function(err, emails) { + if (err) { + callback(err); + return; + } + // delete old items from db self._devicestorage.removeList(dbType, function(err) { if (err) { callback(err); return; } + // persist encrypted list in device storage - self._devicestorage.storeList(emails, dbType, function(err) { - callback(err); - }); + self._devicestorage.storeList(emails, dbType, callback); }); }); @@ -449,7 +454,7 @@ define(function(require) { num: options.num }, function(err, emails) { if (err) { - console.log(err); + callback(err); return; } @@ -461,9 +466,7 @@ define(function(require) { }); // fetch message bodies - fetchBodies(encryptedList, function(messages) { - callback(messages); - }); + fetchBodies(encryptedList, callback); }); } @@ -471,12 +474,12 @@ define(function(require) { var emails = []; if (messageList.length < 1) { - callback(emails); + callback(null, emails); return; } var after = _.after(messageList.length, function() { - callback(emails); + callback(null, emails); }); _.each(messageList, function(messageItem) { @@ -485,7 +488,7 @@ define(function(require) { uid: messageItem.uid }, function(err, message) { if (err) { - console.log(err); + callback(err); return; } diff --git a/test/new-unit/email-dao-test.js b/test/new-unit/email-dao-test.js index 979429d..eb228b8 100644 --- a/test/new-unit/email-dao-test.js +++ b/test/new-unit/email-dao-test.js @@ -426,14 +426,17 @@ define(function(require) { it('should work', function(done) { devicestorageStub.listItems.yields(null, [{ - body: '' + body: app.string.cryptPrefix + btoa(JSON.stringify({})) + app.string.cryptSuffix }]); keychainStub.getPublicKeys.yields(null, [{ _id: "fcf8b4aa-5d09-4089-8b4f-e3bc5091daf3", userId: "safewithme.testuser@gmail.com", publicKey: publicKey }]); - cryptoStub.decryptListForUser.yields(null, []); + cryptoStub.decryptListForUser.yields(null, [{ + body: 'test body', + subject: 'test subject' + }]); emailDao.listMessages({ folder: 'INBOX', @@ -444,7 +447,7 @@ define(function(require) { expect(keychainStub.getPublicKeys.calledOnce).to.be.true; expect(cryptoStub.decryptListForUser.calledOnce).to.be.true; expect(err).to.not.exist; - expect(emails.length).to.equal(0); + expect(emails.length).to.equal(1); done(); }); });