mirror of
https://github.com/moparisthebest/mail
synced 2024-12-25 16:58:58 -05:00
refactor imap login out of email dao.init
This commit is contained in:
parent
c4b4999814
commit
ee7d179298
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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.
|
||||
*/
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user