add imap folder listing functionality

This commit is contained in:
Felix Hammerl 2013-11-28 15:05:29 +01:00
parent 12860f6146
commit 5ddddb5568
2 changed files with 239 additions and 68 deletions

View File

@ -2,9 +2,9 @@ define(function(require) {
'use strict'; 'use strict';
var util = require('cryptoLib/util'); var util = require('cryptoLib/util');
// _ = require('underscore'), // _ = require('underscore'),
// str = require('js/app-config').string, // str = require('js/app-config').string,
// config = require('js/app-config').config; // config = require('js/app-config').config;
var EmailDAO = function(keychain, imapClient, smtpClient, crypto, devicestorage) { var EmailDAO = function(keychain, imapClient, smtpClient, crypto, devicestorage) {
var self = this; var self = this;
@ -28,7 +28,8 @@ define(function(require) {
// //
EmailDAO.prototype.init = function(options, callback) { EmailDAO.prototype.init = function(options, callback) {
var self = this; var self = this,
keypair;
self._account = options.account; self._account = options.account;
@ -53,7 +54,28 @@ define(function(require) {
callback(err); callback(err);
return; return;
} }
callback(null, storedKeypair);
keypair = storedKeypair;
initFolders();
});
});
}
function initFolders() {
self._imapLogin(function(err) {
if (err) {
callback(err);
return;
}
self._imapListFolders(function(err, folders) {
if (err) {
callback(err);
return;
}
self._account.folders = folders;
callback(null, keypair);
}); });
}); });
} }
@ -113,11 +135,14 @@ define(function(require) {
}); });
}; };
// // //
// // IMAP Apis // IMAP Apis
// // //
EmailDAO.prototype.login = function(callback) { /**
* Login the imap client
*/
EmailDAO.prototype._imapLogin = function(callback) {
var self = this; var self = this;
// login IMAP client if existent // login IMAP client if existent
@ -127,72 +152,69 @@ define(function(require) {
/** /**
* Cleanup by logging the user off. * Cleanup by logging the user off.
*/ */
EmailDAO.prototype.destroy = function(callback) { EmailDAO.prototype._imapLogout = function(callback) {
var self = this; var self = this;
self._imapClient.logout(callback); self._imapClient.logout(callback);
}; };
// /**
// * Login the imap client
// */
// /**
// * List the folders in the user's IMAP mailbox.
// */
// EmailDAO.prototype.imapListFolders = function(callback) {
// var self = this,
// dbType = 'folders';
// // check local cache /**
// self._devicestorage.listItems(dbType, 0, null, function(err, stored) { * List the folders in the user's IMAP mailbox.
// if (err) { */
// callback(err); EmailDAO.prototype._imapListFolders = function(callback) {
// return; var self = this,
// } dbType = 'folders';
// if (!stored || stored.length < 1) { // check local cache
// // no folders cached... fetch from server self._devicestorage.listItems(dbType, 0, null, function(err, stored) {
// fetchFromServer(); if (err) {
// return; callback(err);
// } return;
}
// callback(null, stored[0]); if (!stored || stored.length < 1) {
// }); // no folders cached... fetch from server
fetchFromServer();
return;
}
// function fetchFromServer() { callback(null, stored[0]);
// var folders; });
// // fetch list from imap server function fetchFromServer() {
// self._imapClient.listWellKnownFolders(function(err, wellKnownFolders) { var folders;
// if (err) {
// callback(err);
// return;
// }
// folders = [ // fetch list from imap server
// wellKnownFolders.inbox, self._imapClient.listWellKnownFolders(function(err, wellKnownFolders) {
// wellKnownFolders.sent, { if (err) {
// type: 'Outbox', callback(err);
// path: 'OUTBOX' return;
// }, }
// wellKnownFolders.drafts,
// wellKnownFolders.trash
// ];
// // cache locally folders = [
// // persist encrypted list in device storage wellKnownFolders.inbox,
// self._devicestorage.storeList([folders], dbType, function(err) { wellKnownFolders.sent, {
// if (err) { type: 'Outbox',
// callback(err); path: 'OUTBOX'
// return; },
// } wellKnownFolders.drafts,
wellKnownFolders.trash
];
// callback(null, folders); // cache locally
// }); // persist encrypted list in device storage
// }); self._devicestorage.storeList([folders], dbType, function(err) {
// } if (err) {
// }; callback(err);
return;
}
callback(null, folders);
});
});
}
};
// /** // /**
// * Get the number of unread message for a folder // * Get the number of unread message for a folder

View File

@ -58,9 +58,18 @@ define(function(require) {
}); });
it('should init', function(done) { it('should init', function(done) {
var loginStub, listFolderStub;
// initKeychain
devicestorageStub.init.withArgs(emailAddress).yields(); devicestorageStub.init.withArgs(emailAddress).yields();
keychainStub.getUserKeyPair.yields(null, mockKeyPair); keychainStub.getUserKeyPair.yields(null, mockKeyPair);
// initFolders
loginStub = sinon.stub(dao, '_imapLogin');
listFolderStub = sinon.stub(dao, '_imapListFolders');
loginStub.yields();
listFolderStub.yields();
dao.init({ dao.init({
account: account account: account
}, function(err, keyPair) { }, function(err, keyPair) {
@ -71,6 +80,65 @@ define(function(require) {
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(loginStub.calledOnce).to.be.true;
expect(listFolderStub.calledOnce).to.be.true;
done();
});
});
it('should fail due to error while listing folders', function(done) {
var loginStub, listFolderStub;
// initKeychain
devicestorageStub.init.withArgs(emailAddress).yields();
keychainStub.getUserKeyPair.yields(null, mockKeyPair);
// initFolders
loginStub = sinon.stub(dao, '_imapLogin');
listFolderStub = sinon.stub(dao, '_imapListFolders');
loginStub.yields();
listFolderStub.yields({});
dao.init({
account: account
}, function(err, keyPair) {
expect(err).to.exist;
expect(keyPair).to.not.exist;
expect(dao._account).to.equal(account);
expect(devicestorageStub.init.calledOnce).to.be.true;
expect(keychainStub.getUserKeyPair.calledOnce).to.be.true;
expect(loginStub.calledOnce).to.be.true;
expect(listFolderStub.calledOnce).to.be.true;
done();
});
});
it('should fail due to error during imap login', function(done) {
var loginStub = sinon.stub(dao, '_imapLogin');
// initKeychain
devicestorageStub.init.withArgs(emailAddress).yields();
keychainStub.getUserKeyPair.yields(null, mockKeyPair);
// initFolders
loginStub.yields({});
dao.init({
account: account
}, function(err, keyPair) {
expect(err).to.exist;
expect(keyPair).to.not.exist;
expect(dao._account).to.equal(account);
expect(devicestorageStub.init.calledOnce).to.be.true;
expect(keychainStub.getUserKeyPair.calledOnce).to.be.true;
expect(loginStub.calledOnce).to.be.true;
done(); done();
}); });
}); });
@ -218,11 +286,11 @@ define(function(require) {
}); });
}); });
describe('login', function() { describe('_imapLogin', function() {
it('should work', function(done) { it('should work', function(done) {
imapClientStub.login.yields(); imapClientStub.login.yields();
dao.login(function(err) { dao._imapLogin(function(err) {
expect(err).to.not.exist; expect(err).to.not.exist;
done(); done();
}); });
@ -231,18 +299,18 @@ define(function(require) {
it('should fail due to error in imap login', function(done) { it('should fail due to error in imap login', function(done) {
imapClientStub.login.yields({}); imapClientStub.login.yields({});
dao.login(function(err) { dao._imapLogin(function(err) {
expect(err).to.exist; expect(err).to.exist;
done(); done();
}); });
}); });
}); });
describe('destroy', function() { describe('_imapLogout', function() {
it('should work', function(done) { it('should work', function(done) {
imapClientStub.logout.yields(); imapClientStub.logout.yields();
dao.destroy(function(err) { dao._imapLogout(function(err) {
expect(err).to.not.exist; expect(err).to.not.exist;
done(); done();
}); });
@ -251,12 +319,93 @@ define(function(require) {
it('should fail due to error in imap login', function(done) { it('should fail due to error in imap login', function(done) {
imapClientStub.logout.yields({}); imapClientStub.logout.yields({});
dao.destroy(function(err) { dao._imapLogout(function(err) {
expect(err).to.exist; expect(err).to.exist;
done(); done();
}); });
}); });
}); });
describe('_imapListFolders', function() {
var dummyFolders = [{
type: 'Inbox',
path: 'INBOX'
}, {
type: 'Outbox',
path: 'OUTBOX'
}];
it('should list from storage', function(done) {
devicestorageStub.listItems.withArgs('folders').yields(null, [dummyFolders]);
dao._imapListFolders(function(err, folders) {
expect(err).to.not.exist;
expect(devicestorageStub.listItems.calledOnce).to.be.true;
expect(folders[0].type).to.equal('Inbox');
done();
});
});
it('should not list from storage due to error', function(done) {
devicestorageStub.listItems.yields({});
dao._imapListFolders(function(err, folders) {
expect(err).to.exist;
expect(folders).to.not.exist;
expect(devicestorageStub.listItems.calledOnce).to.be.true;
expect(imapClientStub.listWellKnownFolders.called).to.be.false;
done();
});
});
it('should list from imap', function(done) {
devicestorageStub.listItems.yields(null, []);
imapClientStub.listWellKnownFolders.yields(null, {
inbox: dummyFolders[0]
});
devicestorageStub.storeList.yields();
dao._imapListFolders(function(err, folders) {
expect(err).to.not.exist;
expect(devicestorageStub.listItems.calledOnce).to.be.true;
expect(imapClientStub.listWellKnownFolders.calledOnce).to.be.true;
expect(devicestorageStub.storeList.calledOnce).to.be.true;
expect(folders[0].type).to.equal('Inbox');
done();
});
});
it('should not list from imap due to store error', function(done) {
devicestorageStub.listItems.yields(null, []);
imapClientStub.listWellKnownFolders.yields(null, {
inbox: dummyFolders[0]
});
devicestorageStub.storeList.yields({});
dao._imapListFolders(function(err, folders) {
expect(err).to.exist;
expect(folders).to.not.exist;
expect(devicestorageStub.listItems.calledOnce).to.be.true;
expect(imapClientStub.listWellKnownFolders.calledOnce).to.be.true;
expect(devicestorageStub.storeList.calledOnce).to.be.true;
done();
});
});
it('should not list from imap due to imap error', function(done) {
devicestorageStub.listItems.yields(null, []);
imapClientStub.listWellKnownFolders.yields({});
dao._imapListFolders(function(err, folders) {
expect(err).to.exist;
expect(folders).to.not.exist;
expect(devicestorageStub.listItems.calledOnce).to.be.true;
expect(imapClientStub.listWellKnownFolders.calledOnce).to.be.true;
expect(devicestorageStub.storeList.called).to.be.false;
done();
});
});
});
}); });
}); });