1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-28 20:02:16 -05:00

refactor imap login out of email dao.init

This commit is contained in:
Tankred Hase 2013-09-26 17:37:56 +02:00
parent c4b4999814
commit ee7d179298
4 changed files with 60 additions and 39 deletions

View File

@ -10,7 +10,7 @@ define(function(require) {
// start the main app controller // start the main app controller
appController.fetchOAuthToken('passphrase', function(err) { appController.fetchOAuthToken('passphrase', function(err) {
if (err) { if (err) {
console.log(err); console.error(err);
return; return;
} }

View File

@ -39,6 +39,8 @@ define(function(require) {
offset: offset, offset: offset,
num: num num: num
}, function() { }, function() {
// login to imap
loginImap(function() {
// sync from imap to local db // sync from imap to local db
syncImapFolder({ syncImapFolder({
folder: $scope.folder, folder: $scope.folder,
@ -51,17 +53,31 @@ define(function(require) {
offset: offset, offset: offset,
num: num num: num
}, function() { }, function() {
console.log('syncing ' + $scope.folder + ' complete'); 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) { function syncImapFolder(options, callback) {
console.log('Syncing IMAP...');
// sync if emails are empty // sync if emails are empty
emailDao.imapSync(options, function(err) { emailDao.imapSync(options, function(err) {
if (err) { if (err) {
console.log(err); console.error(err);
return; return;
} }
@ -72,7 +88,7 @@ define(function(require) {
function listLocalMessages(options, callback) { function listLocalMessages(options, callback) {
emailDao.listMessages(options, function(err, emails) { emailDao.listMessages(options, function(err, emails) {
if (err) { if (err) {
console.log(err); console.error(err);
return; return;
} }
// add display dates // add display dates

View File

@ -36,18 +36,8 @@ define(function(require) {
return; return;
} }
// login IMAP client if existent // init keychain and then crypto module
if (self._imapClient) {
self._imapClient.login(function(err) {
if (err) {
callback(err);
return;
}
initKeychain(); initKeychain();
});
} else {
initKeychain();
}
function initKeychain() { function initKeychain() {
// init user's local database // init user's local database
@ -91,6 +81,16 @@ define(function(require) {
// IMAP/SMTP Apis // 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. * Cleanup by logging the user off.
*/ */

View File

@ -58,37 +58,24 @@ define(function(require) {
afterEach(function() {}); afterEach(function() {});
describe('init', 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) { it('should fail due to error in getUserKeyPair', function(done) {
imapClientStub.login.yields();
devicestorageStub.init.yields(); devicestorageStub.init.yields();
keychainStub.getUserKeyPair.yields(42); keychainStub.getUserKeyPair.yields(42);
emailDao.init(account, emaildaoTest.passphrase, function(err) { emailDao.init(account, emaildaoTest.passphrase, function(err) {
expect(devicestorageStub.init.calledOnce).to.be.true; expect(devicestorageStub.init.calledOnce).to.be.true;
expect(imapClientStub.login.calledOnce).to.be.true;
expect(err).to.equal(42); expect(err).to.equal(42);
done(); done();
}); });
}); });
it('should init with new keygen', function(done) { it('should init with new keygen', function(done) {
imapClientStub.login.yields();
devicestorageStub.init.yields(); devicestorageStub.init.yields();
keychainStub.getUserKeyPair.yields(); keychainStub.getUserKeyPair.yields();
cryptoStub.init.yields(null, {}); cryptoStub.init.yields(null, {});
keychainStub.putUserKeyPair.yields(); keychainStub.putUserKeyPair.yields();
emailDao.init(account, emaildaoTest.passphrase, function(err) { emailDao.init(account, emaildaoTest.passphrase, function(err) {
expect(imapClientStub.login.calledOnce).to.be.true;
expect(devicestorageStub.init.calledOnce).to.be.true; expect(devicestorageStub.init.calledOnce).to.be.true;
expect(keychainStub.getUserKeyPair.calledOnce).to.be.true; expect(keychainStub.getUserKeyPair.calledOnce).to.be.true;
expect(cryptoStub.init.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() { describe('IMAP/SMTP tests', function() {
beforeEach(function(done) { beforeEach(function(done) {
imapClientStub.login.yields();
devicestorageStub.init.yields(); devicestorageStub.init.yields();
keychainStub.getUserKeyPair.yields(); keychainStub.getUserKeyPair.yields();
cryptoStub.init.yields(null, {}); cryptoStub.init.yields(null, {});
keychainStub.putUserKeyPair.yields(); keychainStub.putUserKeyPair.yields();
emailDao.init(account, emaildaoTest.passphrase, function(err) { emailDao.init(account, emaildaoTest.passphrase, function(err) {
expect(imapClientStub.login.calledOnce).to.be.true;
expect(devicestorageStub.init.calledOnce).to.be.true; expect(devicestorageStub.init.calledOnce).to.be.true;
expect(keychainStub.getUserKeyPair.calledOnce).to.be.true; expect(keychainStub.getUserKeyPair.calledOnce).to.be.true;
expect(cryptoStub.init.calledOnce).to.be.true; expect(cryptoStub.init.calledOnce).to.be.true;