1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-13 04: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() { function initializeUser() {
// init the auth modules // init the auth modules
auth.init(); auth.init(function(err) {
if (err) {
return dialog.error(err);
}
// get OAuth token from chrome // get OAuth token from chrome
auth.getEmailAddress(function(err, info) { auth.getEmailAddress(function(err, info) {
if (err) { if (err) {
@ -36,6 +40,7 @@ var LoginCtrl = function($scope, $timeout, $location, updateHandler, account, au
redirect(availableKeys); redirect(availableKeys);
}); });
}); });
});
} }
function 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 // Pre-Flight check: initialize and prepare user's local database
function prepareDatabase() { function prepareDatabase() {
try { self._accountStore.init(options.emailAddress, function(err) {
self._accountStore.init(options.emailAddress); if (err) {
} catch (err) { return callback(err);
callback(err);
return;
} }
// Migrate the databases if necessary // Migrate the databases if necessary
@ -75,6 +73,8 @@ Account.prototype.init = function(options, callback) {
prepareKeys(); prepareKeys();
}); });
});
} }
// retrieve keypair fom devicestorage/cloud, refresh public key if signup was incomplete before // 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, cfg = require('../app-config').config,
str = require('../app-config').string; str = require('../app-config').string;
var APP_CONFIG_DB_NAME = 'app-config';
var EMAIL_ADDR_DB_KEY = 'emailaddress'; var EMAIL_ADDR_DB_KEY = 'emailaddress';
var USERNAME_DB_KEY = 'username'; var USERNAME_DB_KEY = 'username';
var REALNAME_DB_KEY = 'realname'; var REALNAME_DB_KEY = 'realname';
@ -34,8 +35,13 @@ function Auth(appConfigStore, oauth, pgp) {
/** /**
* Initialize the service * Initialize the service
*/ */
Auth.prototype.init = function() { Auth.prototype.init = function(callback) {
this._initialized = true; 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 // expose an instance with the static dbName 'app-config' to store configuration data
ngModule.factory('appConfigStore', function(appConfigLawnchair) { ngModule.factory('appConfigStore', function(appConfigLawnchair) {
var deviceStorage = new DeviceStorage(appConfigLawnchair); return new DeviceStorage(appConfigLawnchair);
deviceStorage.init('app-config');
return deviceStorage;
}); });
// expose a singleton instance of DeviceStorage called 'accountStore' to persist user data // expose a singleton instance of DeviceStorage called 'accountStore' to persist user data
@ -31,8 +29,8 @@ function DeviceStorage(lawnchairDAO) {
* Initialize the lawnchair database * Initialize the lawnchair database
* @param {String} dbName The name of the database * @param {String} dbName The name of the database
*/ */
DeviceStorage.prototype.init = function(dbName) { DeviceStorage.prototype.init = function(dbName, callback) {
this._lawnchairDAO.init(dbName); this._lawnchairDAO.init(dbName, callback);
}; };
/** /**

View File

@ -14,13 +14,17 @@ function LawnchairDAO() {}
* Initialize the lawnchair database * Initialize the lawnchair database
* @param {String} dbName The name of the 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) { 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 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() { describe('Email DAO integration tests', function() {
this.timeout(100000); 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; mockKeyPair, inbox, spam;
var testAccount = { var testAccount = {
@ -252,12 +252,20 @@ describe('Email DAO integration tests', function() {
// clear the local database before each test // clear the local database before each test
var cleanup = new DeviceStorageDAO(new LawnchairDAO()); var cleanup = new DeviceStorageDAO(new LawnchairDAO());
cleanup.init(testAccount.user); cleanup.init(testAccount.user, function() {
cleanup.clear(function(err) { cleanup.clear(function(err) {
expect(err).to.not.exist; 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({ accountService.init({
emailAddress: testAccount.user emailAddress: testAccount.user
}, function(err) { }, function(err) {
@ -305,6 +313,7 @@ describe('Email DAO integration tests', function() {
}); });
}); });
} }
}
}); });
afterEach(function(done) { afterEach(function(done) {

View File

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

View File

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

View File

@ -13,6 +13,8 @@ describe('Auth unit tests', function() {
var PASSWD_DB_KEY = 'password'; var PASSWD_DB_KEY = 'password';
var IMAP_DB_KEY = 'imap'; var IMAP_DB_KEY = 'imap';
var SMTP_DB_KEY = 'smtp'; var SMTP_DB_KEY = 'smtp';
var APP_CONFIG_DB_NAME = 'app-config';
// SUT // SUT
var auth; var auth;
@ -46,6 +48,25 @@ describe('Auth unit tests', function() {
auth = new Auth(storageStub, oauthStub, pgpStub); 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() { describe('#getCredentials', function() {
it('should load credentials and retrieve credentials from cfg', function(done) { it('should load credentials and retrieve credentials from cfg', function(done) {
storageStub.listItems.withArgs(EMAIL_ADDR_DB_KEY, 0, null).yieldsAsync(null, [emailAddress]); 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() { describe('init', function() {
it('should work', 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; 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() { describe('store list', function() {
it('should fail', function(done) { it('should fail', function(done) {
var list = [{}]; var list = [{}];

View File

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