From ee7d179298c97bd6dfa2e6ef4cfa66f03ad2ca3c Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Thu, 26 Sep 2013 17:37:56 +0200 Subject: [PATCH] refactor imap login out of email dao.init --- src/js/controller/login.js | 2 +- src/js/controller/mail-list.js | 38 +++++++++++++++++++++++---------- src/js/dao/email-dao.js | 24 ++++++++++----------- test/new-unit/email-dao-test.js | 35 +++++++++++++++++------------- 4 files changed, 60 insertions(+), 39 deletions(-) diff --git a/src/js/controller/login.js b/src/js/controller/login.js index 1f8b3a1..bfa3bea 100644 --- a/src/js/controller/login.js +++ b/src/js/controller/login.js @@ -10,7 +10,7 @@ define(function(require) { // start the main app controller appController.fetchOAuthToken('passphrase', function(err) { if (err) { - console.log(err); + console.error(err); return; } diff --git a/src/js/controller/mail-list.js b/src/js/controller/mail-list.js index 681400b..08d450c 100644 --- a/src/js/controller/mail-list.js +++ b/src/js/controller/mail-list.js @@ -39,29 +39,45 @@ define(function(require) { offset: offset, num: num }, function() { - // sync from imap to local db - syncImapFolder({ - folder: $scope.folder, - offset: offset, - num: num - }, function() { - // list again from local db after syncing - listLocalMessages({ + // login to imap + loginImap(function() { + // sync from imap to local db + syncImapFolder({ folder: $scope.folder, offset: offset, num: num }, function() { - console.log('syncing ' + $scope.folder + ' complete'); + // list again from local db after syncing + listLocalMessages({ + folder: $scope.folder, + offset: offset, + num: num + }, function() { + console.log('Syncing ' + $scope.folder + ' complete.'); + }); }); }); }); } + function loginImap(callback) { + emailDao.imapLogin(function(err) { + if (err) { + console.error(err); + return; + } + + console.log('Logged in to IMAP.'); + callback(); + }); + } + function syncImapFolder(options, callback) { + console.log('Syncing IMAP...'); // sync if emails are empty emailDao.imapSync(options, function(err) { if (err) { - console.log(err); + console.error(err); return; } @@ -72,7 +88,7 @@ define(function(require) { function listLocalMessages(options, callback) { emailDao.listMessages(options, function(err, emails) { if (err) { - console.log(err); + console.error(err); return; } // add display dates diff --git a/src/js/dao/email-dao.js b/src/js/dao/email-dao.js index 6748407..5669629 100644 --- a/src/js/dao/email-dao.js +++ b/src/js/dao/email-dao.js @@ -36,18 +36,8 @@ define(function(require) { return; } - // login IMAP client if existent - if (self._imapClient) { - self._imapClient.login(function(err) { - if (err) { - callback(err); - return; - } - initKeychain(); - }); - } else { - initKeychain(); - } + // init keychain and then crypto module + initKeychain(); function initKeychain() { // init user's local database @@ -91,6 +81,16 @@ define(function(require) { // IMAP/SMTP Apis // + /** + * Login the imap client + */ + EmailDAO.prototype.imapLogin = function(callback) { + var self = this; + + // login IMAP client if existent + self._imapClient.login(callback); + }; + /** * Cleanup by logging the user off. */ diff --git a/test/new-unit/email-dao-test.js b/test/new-unit/email-dao-test.js index e368952..be7f00e 100644 --- a/test/new-unit/email-dao-test.js +++ b/test/new-unit/email-dao-test.js @@ -58,37 +58,24 @@ define(function(require) { afterEach(function() {}); describe('init', function() { - it('should fail due to error in imap login', function(done) { - imapClientStub.login.yields(42); - - emailDao.init(account, emaildaoTest.passphrase, function(err) { - expect(err).to.equal(42); - done(); - }); - }); - it('should fail due to error in getUserKeyPair', function(done) { - imapClientStub.login.yields(); devicestorageStub.init.yields(); keychainStub.getUserKeyPair.yields(42); emailDao.init(account, emaildaoTest.passphrase, function(err) { expect(devicestorageStub.init.calledOnce).to.be.true; - expect(imapClientStub.login.calledOnce).to.be.true; expect(err).to.equal(42); done(); }); }); it('should init with new keygen', function(done) { - imapClientStub.login.yields(); devicestorageStub.init.yields(); keychainStub.getUserKeyPair.yields(); cryptoStub.init.yields(null, {}); keychainStub.putUserKeyPair.yields(); emailDao.init(account, emaildaoTest.passphrase, function(err) { - expect(imapClientStub.login.calledOnce).to.be.true; expect(devicestorageStub.init.calledOnce).to.be.true; expect(keychainStub.getUserKeyPair.calledOnce).to.be.true; expect(cryptoStub.init.calledOnce).to.be.true; @@ -99,16 +86,34 @@ define(function(require) { }); }); + describe('login', function() { + it('should fail due to error in imap login', function(done) { + imapClientStub.login.yields(42); + + emailDao.imapLogin(function(err) { + expect(err).to.equal(42); + done(); + }); + }); + + it('should work', function(done) { + imapClientStub.login.yields(); + + emailDao.imapLogin(function(err) { + expect(err).to.not.exist; + done(); + }); + }); + }); + describe('IMAP/SMTP tests', function() { beforeEach(function(done) { - imapClientStub.login.yields(); devicestorageStub.init.yields(); keychainStub.getUserKeyPair.yields(); cryptoStub.init.yields(null, {}); keychainStub.putUserKeyPair.yields(); emailDao.init(account, emaildaoTest.passphrase, function(err) { - expect(imapClientStub.login.calledOnce).to.be.true; expect(devicestorageStub.init.calledOnce).to.be.true; expect(keychainStub.getUserKeyPair.calledOnce).to.be.true; expect(cryptoStub.init.calledOnce).to.be.true;