1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-11 03:35:01 -05:00

Merge pull request #212 from whiteout-io/dev/WO-767

[WO-767] Initialize lawnchair asynchronously during startup
This commit is contained in:
Felix Hammerl 2014-12-01 15:11:17 +01:00
commit 51715822f8
11 changed files with 167 additions and 98 deletions

View File

@ -9,7 +9,11 @@ var LoginCtrl = function($scope, $timeout, $location, updateHandler, account, au
function initializeUser() {
// init the auth modules
auth.init();
auth.init(function(err) {
if (err) {
return dialog.error(err);
}
// get OAuth token from chrome
auth.getEmailAddress(function(err, info) {
if (err) {
@ -36,6 +40,7 @@ var LoginCtrl = function($scope, $timeout, $location, updateHandler, account, au
redirect(availableKeys);
});
});
});
}
function redirect(availableKeys) {

View File

@ -60,11 +60,9 @@ Account.prototype.init = function(options, callback) {
// Pre-Flight check: initialize and prepare user's local database
function prepareDatabase() {
try {
self._accountStore.init(options.emailAddress);
} catch (err) {
callback(err);
return;
self._accountStore.init(options.emailAddress, function(err) {
if (err) {
return callback(err);
}
// Migrate the databases if necessary
@ -75,6 +73,8 @@ Account.prototype.init = function(options, callback) {
prepareKeys();
});
});
}
// retrieve keypair fom devicestorage/cloud, refresh public key if signup was incomplete before

View File

@ -8,6 +8,7 @@ var axe = require('axe-logger'),
cfg = require('../app-config').config,
str = require('../app-config').string;
var APP_CONFIG_DB_NAME = 'app-config';
var EMAIL_ADDR_DB_KEY = 'emailaddress';
var USERNAME_DB_KEY = 'username';
var REALNAME_DB_KEY = 'realname';
@ -34,8 +35,13 @@ function Auth(appConfigStore, oauth, pgp) {
/**
* Initialize the service
*/
Auth.prototype.init = function() {
this._initialized = true;
Auth.prototype.init = function(callback) {
var self = this;
self._appConfigStore.init(APP_CONFIG_DB_NAME, function(error) {
self._initialized = !error;
callback(error);
});
};
/**

View File

@ -4,9 +4,7 @@ var ngModule = angular.module('woServices');
// expose an instance with the static dbName 'app-config' to store configuration data
ngModule.factory('appConfigStore', function(appConfigLawnchair) {
var deviceStorage = new DeviceStorage(appConfigLawnchair);
deviceStorage.init('app-config');
return deviceStorage;
return new DeviceStorage(appConfigLawnchair);
});
// expose a singleton instance of DeviceStorage called 'accountStore' to persist user data
@ -31,8 +29,8 @@ function DeviceStorage(lawnchairDAO) {
* Initialize the lawnchair database
* @param {String} dbName The name of the database
*/
DeviceStorage.prototype.init = function(dbName) {
this._lawnchairDAO.init(dbName);
DeviceStorage.prototype.init = function(dbName, callback) {
this._lawnchairDAO.init(dbName, callback);
};
/**

View File

@ -14,13 +14,17 @@ function LawnchairDAO() {}
* Initialize the lawnchair database
* @param {String} dbName The name of the database
*/
LawnchairDAO.prototype.init = function(dbName) {
LawnchairDAO.prototype.init = function(dbName, callback) {
var self = this;
if (!dbName) {
throw new Error('Lawnchair DB name must be specified!');
return callback(new Error('Lawnchair DB name must be specified!'));
}
this._db = new Lawnchair({
self._db = new Lawnchair({
name: dbName
}, function(success) {
callback(success ? undefined : new Error('Lawnchair initialization ' + dbName + ' failed!'));
});
};

View File

@ -14,7 +14,7 @@ var ImapClient = require('imap-client'),
describe('Email DAO integration tests', function() {
this.timeout(100000);
var accountService, emailDao, imapClient, imapMessages, imapFolders, imapServer, smtpServer, smtpClient, userStorage,
var accountService, emailDao, imapClient, imapMessages, imapFolders, imapServer, smtpServer, smtpClient, userStorage, auth,
mockKeyPair, inbox, spam;
var testAccount = {
@ -252,12 +252,20 @@ describe('Email DAO integration tests', function() {
// clear the local database before each test
var cleanup = new DeviceStorageDAO(new LawnchairDAO());
cleanup.init(testAccount.user);
cleanup.init(testAccount.user, function() {
cleanup.clear(function(err) {
expect(err).to.not.exist;
userStorage = accountService._accountStore;
onCleaned();
});
});
function onCleaned() {
userStorage = accountService._accountStore;
auth = accountService._auth;
auth.init(function(err) {
expect(err).to.not.exist;
accountService.init({
emailAddress: testAccount.user
}, function(err) {
@ -305,6 +313,7 @@ describe('Email DAO integration tests', function() {
});
});
}
}
});
afterEach(function(done) {

View File

@ -54,6 +54,7 @@ describe('Login Controller unit test', function() {
afterEach(function() {});
it('should fail for auth.getEmailAddress', function() {
authMock.init.yields();
authMock.getEmailAddress.yields(new Error());
createController();
@ -63,7 +64,21 @@ describe('Login Controller unit test', function() {
expect(dialogMock.error.calledOnce).to.be.true;
});
it('should fail for auth.init', function() {
authMock.init.yields(new Error());
authMock.getEmailAddress.yields(null, {
emailAddress: emailAddress
});
createController();
expect(authMock.init.calledOnce).to.be.true;
expect(accountMock.init.called).to.be.false;
expect(dialogMock.error.calledOnce).to.be.true;
});
it('should redirect to /add-account', function() {
authMock.init.yields();
authMock.getEmailAddress.yields(null, {});
createController();
@ -71,19 +86,8 @@ describe('Login Controller unit test', function() {
expect(goToStub.withArgs('/add-account').calledOnce).to.be.true;
});
it('should fail for auth.init', function() {
authMock.getEmailAddress.yields(null, {
emailAddress: emailAddress
});
accountMock.init.yields(new Error());
createController();
expect(accountMock.init.calledOnce).to.be.true;
expect(dialogMock.error.calledOnce).to.be.true;
});
it('should redirect to /login-existing', function() {
authMock.init.yields();
authMock.getEmailAddress.yields(null, {
emailAddress: emailAddress
});
@ -99,6 +103,7 @@ describe('Login Controller unit test', function() {
});
it('should fail for auth.storeCredentials', function() {
authMock.init.yields();
authMock.getEmailAddress.yields(null, {
emailAddress: emailAddress
});
@ -115,6 +120,7 @@ describe('Login Controller unit test', function() {
});
it('should redirect to /desktop', function() {
authMock.init.yields();
authMock.getEmailAddress.yields(null, {
emailAddress: emailAddress
});
@ -131,6 +137,7 @@ describe('Login Controller unit test', function() {
});
it('should fail for keychain.requestPrivateKeyDownload', function() {
authMock.init.yields();
authMock.getEmailAddress.yields(null, {
emailAddress: emailAddress
});
@ -145,6 +152,7 @@ describe('Login Controller unit test', function() {
});
it('should redirect to /login-privatekey-download', function() {
authMock.init.yields();
authMock.getEmailAddress.yields(null, {
emailAddress: emailAddress
});
@ -159,6 +167,7 @@ describe('Login Controller unit test', function() {
});
it('should redirect to /login-new-device', function() {
authMock.init.yields();
authMock.getEmailAddress.yields(null, {
emailAddress: emailAddress
});
@ -173,6 +182,7 @@ describe('Login Controller unit test', function() {
});
it('should redirect to /login-initial', function() {
authMock.init.yields();
authMock.getEmailAddress.yields(null, {
emailAddress: emailAddress
});

View File

@ -67,7 +67,7 @@ describe('Account Service unit test', function() {
});
it('should fail for _accountStore.init', function() {
devicestorageStub.init.throws(new Error('asdf'));
devicestorageStub.init.yields(new Error('asdf'));
account.init({
emailAddress: dummyUser,

View File

@ -13,6 +13,8 @@ describe('Auth unit tests', function() {
var PASSWD_DB_KEY = 'password';
var IMAP_DB_KEY = 'imap';
var SMTP_DB_KEY = 'smtp';
var APP_CONFIG_DB_NAME = 'app-config';
// SUT
var auth;
@ -46,6 +48,25 @@ describe('Auth unit tests', function() {
auth = new Auth(storageStub, oauthStub, pgpStub);
});
describe('#init', function() {
it('should initialize a user db', function(done) {
storageStub.init.withArgs(APP_CONFIG_DB_NAME).yields();
auth.init(function(err) {
expect(err).to.not.exist;
expect(auth._initialized).to.be.true;
done();
});
});
it('should initialize a user db', function(done) {
storageStub.init.withArgs(APP_CONFIG_DB_NAME).yields(new Error());
auth.init(function(err) {
expect(err).to.exist;
expect(auth._initialized).to.be.false;
done();
});
});
});
describe('#getCredentials', function() {
it('should load credentials and retrieve credentials from cfg', function(done) {
storageStub.listItems.withArgs(EMAIL_ADDR_DB_KEY, 0, null).yieldsAsync(null, [emailAddress]);

View File

@ -18,11 +18,24 @@ describe('Device Storage DAO unit tests', function() {
describe('init', function() {
it('should work', function() {
storageDao.init(testUser);
lawnchairDaoStub.init.yields();
storageDao.init(testUser, function(err) {
expect(err).to.not.exist;
expect(lawnchairDaoStub.init.calledOnce).to.be.true;
});
});
it('should fail', function() {
lawnchairDaoStub.init.yields(new Error());
storageDao.init(testUser, function(err) {
expect(err).to.exist;
expect(lawnchairDaoStub.init.calledOnce).to.be.true;
});
});
});
describe('store list', function() {
it('should fail', function(done) {
var list = [{}];

View File

@ -20,10 +20,13 @@ var data2 = {
describe('Lawnchair DAO unit tests', function() {
var lawnchairDao;
beforeEach(function() {
beforeEach(function(done) {
lawnchairDao = new LawnchairDAO();
lawnchairDao.init(dbName);
lawnchairDao.init(dbName, function(err) {
expect(err).to.not.exist;
expect(lawnchairDao._db).to.exist;
done();
});
});
afterEach(function(done) {