1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-26 02:42:17 -05:00

wire up the update handler

This commit is contained in:
Felix Hammerl 2014-03-11 17:49:47 +01:00
parent 72794971da
commit 1ad8a2da46
4 changed files with 88 additions and 50 deletions

View File

@ -17,6 +17,7 @@ define(function(require) {
OutboxBO = require('js/bo/outbox'), OutboxBO = require('js/bo/outbox'),
PGP = require('js/crypto/pgp'), PGP = require('js/crypto/pgp'),
PgpBuilder = require('pgpbuilder'), PgpBuilder = require('pgpbuilder'),
UpdateHandler = require('js/util/update/update-handler'),
config = require('js/app-config').config; config = require('js/app-config').config;
var self = {}; var self = {};
@ -334,23 +335,24 @@ define(function(require) {
}; };
self.buildModules = function() { self.buildModules = function() {
var lawnchairDao, restDao, pubkeyDao, invitationDao, var lawnchairDao, restDao, pubkeyDao, emailDao, keychain, pgp, devicestorage, pgpbuilder;
emailDao, keychain, pgp, userStorage, pgpbuilder;
// start the mailreader's worker thread
mailreader.startWorker(config.workerPath + '/../lib/mailreader-parser-worker.js'); mailreader.startWorker(config.workerPath + '/../lib/mailreader-parser-worker.js');
// init objects and inject dependencies // init objects and inject dependencies
restDao = new RestDAO(); restDao = new RestDAO();
pubkeyDao = new PublicKeyDAO(restDao); pubkeyDao = new PublicKeyDAO(restDao);
lawnchairDao = new LawnchairDAO(); lawnchairDao = new LawnchairDAO();
userStorage = new DeviceStorageDAO(lawnchairDao);
self._invitationDao = invitationDao = new InvitationDAO(restDao); self._devicestorage = devicestorage = new DeviceStorageDAO(lawnchairDao);
self._invitationDao = new InvitationDAO(restDao);
self._keychain = keychain = new KeychainDAO(lawnchairDao, pubkeyDao); self._keychain = keychain = new KeychainDAO(lawnchairDao, pubkeyDao);
self._crypto = pgp = new PGP(); self._crypto = pgp = new PGP();
self._pgpbuilder = pgpbuilder = new PgpBuilder(); self._pgpbuilder = pgpbuilder = new PgpBuilder();
self._emailDao = emailDao = new EmailDAO(keychain, pgp, userStorage, pgpbuilder, mailreader); self._emailDao = emailDao = new EmailDAO(keychain, pgp, devicestorage, pgpbuilder, mailreader);
self._outboxBo = new OutboxBO(emailDao, keychain, userStorage); self._outboxBo = new OutboxBO(emailDao, keychain, devicestorage);
self._updateHandler = new UpdateHandler(self._appConfigStore, devicestorage);
}; };
/** /**
@ -359,30 +361,48 @@ define(function(require) {
self.init = function(options, callback) { self.init = function(options, callback) {
self.buildModules(); self.buildModules();
// init email dao // init user's local database
var account = { self._devicestorage.init(options.emailAddress, function() {
emailAddress: options.emailAddress,
asymKeySize: config.asymKeySize
};
self._emailDao.init({ // Migrate the databases if necessary
account: account self._updateHandler.update(onUpdate);
}, function(err, keypair) { });
function onUpdate(err) {
if (err) { if (err) {
callback(err); callback({
errMsg: 'Update failed, please reinstall the app.',
err: err
});
return; return;
} }
// connect tcp clients on first startup // account information for the email dao
self.onConnect(function(err) { var account = {
emailAddress: options.emailAddress,
asymKeySize: config.asymKeySize
};
// init email dao
self._emailDao.init({
account: account
}, function(err, keypair) {
if (err) { if (err) {
callback(err); callback(err);
return; return;
} }
callback(null, keypair); // connect tcp clients on first startup
self.onConnect(function(err) {
if (err) {
callback(err);
return;
}
callback(null, keypair);
});
}); });
}); }
}; };
return self; return self;

View File

@ -39,18 +39,15 @@ define(function(require) {
initKeychain(); initKeychain();
function initKeychain() { function initKeychain() {
// init user's local database // call getUserKeyPair to read/sync keypair with devicestorage/cloud
self._devicestorage.init(emailAddress, function() { self._keychain.getUserKeyPair(emailAddress, function(err, storedKeypair) {
// call getUserKeyPair to read/sync keypair with devicestorage/cloud if (err) {
self._keychain.getUserKeyPair(emailAddress, function(err, storedKeypair) { callback(err);
if (err) { return;
callback(err); }
return;
}
keypair = storedKeypair; keypair = storedKeypair;
initFolders(); initFolders();
});
}); });
} }

View File

@ -5,19 +5,19 @@ define(function(require) {
EmailDAO = require('js/dao/email-dao'), EmailDAO = require('js/dao/email-dao'),
OutboxBO = require('js/bo/outbox'), OutboxBO = require('js/bo/outbox'),
DeviceStorageDAO = require('js/dao/devicestorage-dao'), DeviceStorageDAO = require('js/dao/devicestorage-dao'),
UpdateHandler = require('js/util/update/update-handler'),
expect = chai.expect; expect = chai.expect;
describe('App Controller unit tests', function() { describe('App Controller unit tests', function() {
var emailDaoStub, outboxStub, appConfigStoreStub, isOnlineStub, var emailDaoStub, outboxStub, updateHandlerStub, appConfigStoreStub, devicestorageStub, isOnlineStub,
identityStub; identityStub;
beforeEach(function() { beforeEach(function() {
emailDaoStub = sinon.createStubInstance(EmailDAO); controller._emailDao = emailDaoStub = sinon.createStubInstance(EmailDAO);
controller._emailDao = emailDaoStub; controller._outboxBo = outboxStub = sinon.createStubInstance(OutboxBO);
outboxStub = sinon.createStubInstance(OutboxBO); controller._appConfigStore = appConfigStoreStub = sinon.createStubInstance(DeviceStorageDAO);
controller._outboxBo = outboxStub; controller._devicestorage = devicestorageStub = sinon.createStubInstance(DeviceStorageDAO);
appConfigStoreStub = sinon.createStubInstance(DeviceStorageDAO); controller._updateHandler = updateHandlerStub = sinon.createStubInstance(UpdateHandler);
controller._appConfigStore = appConfigStoreStub;
isOnlineStub = sinon.stub(controller, 'isOnline'); isOnlineStub = sinon.stub(controller, 'isOnline');
@ -226,9 +226,11 @@ define(function(require) {
}); });
describe('init', function() { describe('init', function() {
var buildModulesStub, onConnectStub; var buildModulesStub, onConnectStub, emailAddress;
beforeEach(function() { beforeEach(function() {
emailAddress = 'alice@bob.com';
// buildModules // buildModules
buildModulesStub = sinon.stub(controller, 'buildModules'); buildModulesStub = sinon.stub(controller, 'buildModules');
buildModulesStub.returns(); buildModulesStub.returns();
@ -241,42 +243,70 @@ define(function(require) {
onConnectStub.restore(); onConnectStub.restore();
}); });
it('should fail due to error in update handler', function(done) {
devicestorageStub.init.yields();
updateHandlerStub.update.yields({});
controller.init({}, function(err, keypair) {
expect(err).to.exist;
expect(keypair).to.not.exist;
expect(updateHandlerStub.update.calledOnce).to.be.true;
expect(devicestorageStub.init.calledOnce).to.be.true;
done();
});
});
it('should fail due to error in emailDao.init', function(done) { it('should fail due to error in emailDao.init', function(done) {
devicestorageStub.init.yields();
updateHandlerStub.update.yields();
emailDaoStub.init.yields({}); emailDaoStub.init.yields({});
controller.init({}, function(err, keypair) { controller.init({}, function(err, keypair) {
expect(err).to.exist; expect(err).to.exist;
expect(keypair).to.not.exist; expect(keypair).to.not.exist;
expect(updateHandlerStub.update.calledOnce).to.be.true;
expect(emailDaoStub.init.calledOnce).to.be.true;
expect(devicestorageStub.init.calledOnce).to.be.true;
done(); done();
}); });
}); });
it('should fail due to error in onConnect', function(done) { it('should fail due to error in onConnect', function(done) {
devicestorageStub.init.yields();
updateHandlerStub.update.yields();
emailDaoStub.init.yields(); emailDaoStub.init.yields();
onConnectStub.yields({}); onConnectStub.yields({});
controller.init({}, function(err) { controller.init({}, function(err) {
expect(err).to.exist; expect(err).to.exist;
expect(updateHandlerStub.update.calledOnce).to.be.true;
expect(emailDaoStub.init.calledOnce).to.be.true;
expect(devicestorageStub.init.calledOnce).to.be.true;
expect(onConnectStub.calledOnce).to.be.true; expect(onConnectStub.calledOnce).to.be.true;
done(); done();
}); });
}); });
it('should work and return a keypair', function(done) { it('should work and return a keypair', function(done) {
devicestorageStub.init.withArgs(emailAddress).yields();
emailDaoStub.init.yields(null, {}); emailDaoStub.init.yields(null, {});
updateHandlerStub.update.yields();
onConnectStub.yields(); onConnectStub.yields();
controller.init({}, function(err, keypair) { controller.init({
emailAddress: emailAddress
}, function(err, keypair) {
expect(err).to.not.exist; expect(err).to.not.exist;
expect(keypair).to.exist; expect(keypair).to.exist;
expect(updateHandlerStub.update.calledOnce).to.be.true;
expect(emailDaoStub.init.calledOnce).to.be.true;
expect(devicestorageStub.init.calledOnce).to.be.true;
expect(onConnectStub.calledOnce).to.be.true; expect(onConnectStub.calledOnce).to.be.true;
done(); done();
}); });
}); });
}); });
}); });
}); });

View File

@ -177,7 +177,6 @@ define(function(require) {
folders = [{}, {}]; folders = [{}, {}];
// initKeychain // initKeychain
devicestorageStub.init.withArgs(emailAddress).yields();
keychainStub.getUserKeyPair.yields(null, mockKeyPair); keychainStub.getUserKeyPair.yields(null, mockKeyPair);
// initFolders // initFolders
@ -194,7 +193,6 @@ define(function(require) {
expect(dao._account).to.equal(account); expect(dao._account).to.equal(account);
expect(dao._account.folders).to.equal(folders); expect(dao._account.folders).to.equal(folders);
expect(devicestorageStub.init.calledOnce).to.be.true;
expect(keychainStub.getUserKeyPair.calledOnce).to.be.true; expect(keychainStub.getUserKeyPair.calledOnce).to.be.true;
expect(listFolderStub.calledOnce).to.be.true; expect(listFolderStub.calledOnce).to.be.true;
@ -207,7 +205,6 @@ define(function(require) {
var listFolderStub; var listFolderStub;
// initKeychain // initKeychain
devicestorageStub.init.withArgs(emailAddress).yields();
keychainStub.getUserKeyPair.yields(null, mockKeyPair); keychainStub.getUserKeyPair.yields(null, mockKeyPair);
// initFolders // initFolders
@ -226,7 +223,6 @@ define(function(require) {
expect(dao._account).to.equal(account); expect(dao._account).to.equal(account);
expect(dao._account.folders).to.equal(undefined); expect(dao._account.folders).to.equal(undefined);
expect(devicestorageStub.init.calledOnce).to.be.true;
expect(keychainStub.getUserKeyPair.calledOnce).to.be.true; expect(keychainStub.getUserKeyPair.calledOnce).to.be.true;
expect(listFolderStub.calledOnce).to.be.true; expect(listFolderStub.calledOnce).to.be.true;
@ -238,7 +234,6 @@ define(function(require) {
var listFolderStub; var listFolderStub;
// initKeychain // initKeychain
devicestorageStub.init.withArgs(emailAddress).yields();
keychainStub.getUserKeyPair.yields(null, mockKeyPair); keychainStub.getUserKeyPair.yields(null, mockKeyPair);
// initFolders // initFolders
@ -252,7 +247,6 @@ define(function(require) {
expect(keyPair).to.not.exist; expect(keyPair).to.not.exist;
expect(dao._account).to.equal(account); expect(dao._account).to.equal(account);
expect(devicestorageStub.init.calledOnce).to.be.true;
expect(keychainStub.getUserKeyPair.calledOnce).to.be.true; expect(keychainStub.getUserKeyPair.calledOnce).to.be.true;
expect(listFolderStub.calledOnce).to.be.true; expect(listFolderStub.calledOnce).to.be.true;
@ -261,7 +255,6 @@ define(function(require) {
}); });
it('should fail due to error in getUserKeyPair', function(done) { it('should fail due to error in getUserKeyPair', function(done) {
devicestorageStub.init.yields();
keychainStub.getUserKeyPair.yields({}); keychainStub.getUserKeyPair.yields({});
dao.init({ dao.init({
@ -270,8 +263,6 @@ define(function(require) {
expect(err).to.exist; expect(err).to.exist;
expect(keyPair).to.not.exist; expect(keyPair).to.not.exist;
expect(devicestorageStub.init.calledOnce).to.be.true;
done(); done();
}); });
}); });