Refactor update scripts

This commit is contained in:
Tankred Hase 2014-12-11 13:57:40 +01:00
parent dab09f1068
commit f4fe1a36a6
7 changed files with 160 additions and 232 deletions

View File

@ -15,7 +15,8 @@ var axe = require('axe-logger'),
/** /**
* Handles database migration * Handles database migration
*/ */
function UpdateHandler(appConfigStore, accountStore, auth, dialog) { function UpdateHandler($q, appConfigStore, accountStore, auth, dialog) {
this._q = $q;
this._appConfigStorage = appConfigStore; this._appConfigStorage = appConfigStore;
this._userStorage = accountStore; this._userStorage = accountStore;
this._updateScripts = [updateV1, updateV2, updateV3, updateV4, updateV5]; this._updateScripts = [updateV1, updateV2, updateV3, updateV4, updateV5];
@ -25,76 +26,67 @@ function UpdateHandler(appConfigStore, accountStore, auth, dialog) {
/** /**
* Executes all the necessary updates * Executes all the necessary updates
* @param {Function} callback(error) Invoked when all the database updates were executed, or if an error occurred
*/ */
UpdateHandler.prototype.update = function(callback) { UpdateHandler.prototype.update = function() {
var self = this, var self = this,
currentVersion = 0, currentVersion = 0,
targetVersion = cfg.dbVersion, targetVersion = cfg.dbVersion,
versionDbType = 'dbVersion'; versionDbType = 'dbVersion';
self._appConfigStorage.listItems(versionDbType, 0, null, function(err, items) { return self._appConfigStorage.listItems(versionDbType, 0, null).then(function(items) {
if (err) {
callback(err);
return;
}
// parse the database version number // parse the database version number
if (items && items.length > 0) { if (items && items.length > 0) {
currentVersion = parseInt(items[0], 10); currentVersion = parseInt(items[0], 10);
} }
self._applyUpdate({ return self._applyUpdate({
currentVersion: currentVersion, currentVersion: currentVersion,
targetVersion: targetVersion targetVersion: targetVersion
}, callback); });
}); });
}; };
/** /**
* Schedules necessary updates and executes thom in order * Schedules necessary updates and executes thom in order
*/ */
UpdateHandler.prototype._applyUpdate = function(options, callback) { UpdateHandler.prototype._applyUpdate = function(options) {
var self = this, var self = this;
scriptOptions, return self._q(function(resolve, reject) {
queue = []; var scriptOptions,
queue = [];
if (options.currentVersion >= options.targetVersion) { if (options.currentVersion >= options.targetVersion) {
// the current database version is up to date // the current database version is up to date
callback(); resolve();
return;
}
scriptOptions = {
appConfigStorage: self._appConfigStorage,
userStorage: self._userStorage,
auth: self._auth
};
// add all the necessary database updates to the queue
for (var i = options.currentVersion; i < options.targetVersion; i++) {
queue.push(self._updateScripts[i]);
}
// takes the next update from the queue and executes it
function executeNextUpdate(err) {
if (err) {
callback(err);
return; return;
} }
if (queue.length < 1) { scriptOptions = {
// we're done appConfigStorage: self._appConfigStorage,
callback(); userStorage: self._userStorage,
return; auth: self._auth
};
// add all the necessary database updates to the queue
for (var i = options.currentVersion; i < options.targetVersion; i++) {
queue.push(self._updateScripts[i]);
} }
// process next update // takes the next update from the queue and executes it
var script = queue.shift(); function executeNextUpdate() {
script(scriptOptions, executeNextUpdate); if (queue.length < 1) {
} // we're done
resolve();
return;
}
executeNextUpdate(); // process next update
var script = queue.shift();
script(scriptOptions).then(executeNextUpdate).catch(reject);
}
executeNextUpdate();
});
}; };
/** /**

View File

@ -7,20 +7,15 @@
* every non-prefixed mail in the IMAP folders would be nuked due to the implementation * every non-prefixed mail in the IMAP folders would be nuked due to the implementation
* of the delta sync. * of the delta sync.
*/ */
function updateV1(options, callback) { function updateV1(options) {
var emailDbType = 'email_', var emailDbType = 'email_',
versionDbType = 'dbVersion', versionDbType = 'dbVersion',
postUpdateDbVersion = 1; postUpdateDbVersion = 1;
// remove the emails // remove the emails
options.userStorage.removeList(emailDbType, function(err) { return options.userStorage.removeList(emailDbType).then(function() {
if (err) {
callback(err);
return;
}
// update the database version to postUpdateDbVersion // update the database version to postUpdateDbVersion
options.appConfigStorage.storeList([postUpdateDbVersion], versionDbType, callback); return options.appConfigStorage.storeList([postUpdateDbVersion], versionDbType);
}); });
} }

View File

@ -6,20 +6,15 @@
* In database version 2, the stored email objects have to be purged, because the * In database version 2, the stored email objects have to be purged, because the
* new data model stores information about the email structure in the property 'bodyParts'. * new data model stores information about the email structure in the property 'bodyParts'.
*/ */
function updateV2(options, callback) { function updateV2(options) {
var emailDbType = 'email_', var emailDbType = 'email_',
versionDbType = 'dbVersion', versionDbType = 'dbVersion',
postUpdateDbVersion = 2; postUpdateDbVersion = 2;
// remove the emails // remove the emails
options.userStorage.removeList(emailDbType, function(err) { return options.userStorage.removeList(emailDbType).then(function() {
if (err) {
callback(err);
return;
}
// update the database version to postUpdateDbVersion // update the database version to postUpdateDbVersion
options.appConfigStorage.storeList([postUpdateDbVersion], versionDbType, callback); return options.appConfigStorage.storeList([postUpdateDbVersion], versionDbType);
}); });
} }

View File

@ -6,20 +6,15 @@
* In database version 3, we introduced new flags to the messages, also * In database version 3, we introduced new flags to the messages, also
* the outbox uses artificial uids * the outbox uses artificial uids
*/ */
function update(options, callback) { function update(options) {
var emailDbType = 'email_', var emailDbType = 'email_',
versionDbType = 'dbVersion', versionDbType = 'dbVersion',
postUpdateDbVersion = 3; postUpdateDbVersion = 3;
// remove the emails // remove the emails
options.userStorage.removeList(emailDbType, function(err) { return options.userStorage.removeList(emailDbType).then(function() {
if (err) {
callback(err);
return;
}
// update the database version to postUpdateDbVersion // update the database version to postUpdateDbVersion
options.appConfigStorage.storeList([postUpdateDbVersion], versionDbType, callback); return options.appConfigStorage.storeList([postUpdateDbVersion], versionDbType);
}); });
} }

View File

@ -7,7 +7,7 @@
* indexeddb. only gmail was allowed as a mail service provider before, * indexeddb. only gmail was allowed as a mail service provider before,
* so let's add this... * so let's add this...
*/ */
function update(options, callback) { function update(options) {
var VERSION_DB_TYPE = 'dbVersion', var VERSION_DB_TYPE = 'dbVersion',
EMAIL_ADDR_DB_KEY = 'emailaddress', EMAIL_ADDR_DB_KEY = 'emailaddress',
USERNAME_DB_KEY = 'username', USERNAME_DB_KEY = 'username',
@ -29,77 +29,45 @@ function update(options, callback) {
}; };
// load the email address (if existing) // load the email address (if existing)
loadFromDB(EMAIL_ADDR_DB_KEY, function(err, emailAddress) { var emailAddress;
if (err) { return loadFromDB(EMAIL_ADDR_DB_KEY).then(function(address) {
return callback(err); emailAddress = address;
// load the provider (if existing)
return loadFromDB(PROVIDER_DB_KEY);
}).then(function(provider) {
// if there is an email address without a provider, we need to add the missing provider entry
// for any other situation, we're good.
if (!(emailAddress && !provider)) {
// update the database version to POST_UPDATE_DB_VERSION
return options.appConfigStorage.storeList([POST_UPDATE_DB_VERSION], VERSION_DB_TYPE);
} }
// load the provider (if existing) // add the missing provider key
loadFromDB(PROVIDER_DB_KEY, function(err, provider) { var storeProvider = options.appConfigStorage.storeList(['gmail'], PROVIDER_DB_KEY);
if (err) { // add the missing user name key
return callback(err); var storeAdress = options.appConfigStorage.storeList([emailAddress], USERNAME_DB_KEY);
} // add the missing imap host info key
var storeImap = options.appConfigStorage.storeList([imap], IMAP_DB_KEY);
// add the missing empty real name
var storeEmptyName = options.appConfigStorage.storeList([''], REALNAME_DB_KEY);
// add the missing smtp host info key
var storeSmtp = options.appConfigStorage.storeList([smtp], SMTP_DB_KEY);
// if there is an email address without a provider, we need to add the missing provider entry return Promise.all([storeProvider, storeAdress, storeImap, storeEmptyName, storeSmtp]).then(function() {
// for any other situation, we're good. // reload the credentials
options.auth.initialized = false;
return options.auth._loadCredentials();
if (!(emailAddress && !provider)) { }).then(function() {
// update the database version to POST_UPDATE_DB_VERSION // update the database version to POST_UPDATE_DB_VERSION
return options.appConfigStorage.storeList([POST_UPDATE_DB_VERSION], VERSION_DB_TYPE, callback); return options.appConfigStorage.storeList([POST_UPDATE_DB_VERSION], VERSION_DB_TYPE);
}
// add the missing provider key
options.appConfigStorage.storeList(['gmail'], PROVIDER_DB_KEY, function(err) {
if (err) {
return callback(err);
}
// add the missing user name key
options.appConfigStorage.storeList([emailAddress], USERNAME_DB_KEY, function(err) {
if (err) {
return callback(err);
}
// add the missing imap host info key
options.appConfigStorage.storeList([imap], IMAP_DB_KEY, function(err) {
if (err) {
return callback(err);
}
// add the missing empty real name
options.appConfigStorage.storeList([''], REALNAME_DB_KEY, function(err) {
if (err) {
return callback(err);
}
// add the missing smtp host info key
options.appConfigStorage.storeList([smtp], SMTP_DB_KEY, function(err) {
if (err) {
return callback(err);
}
// reload the credentials
options.auth.initialized = false;
options.auth._loadCredentials(function(err) {
if (err) {
return callback(err);
}
// update the database version to POST_UPDATE_DB_VERSION
options.appConfigStorage.storeList([POST_UPDATE_DB_VERSION], VERSION_DB_TYPE, callback);
});
});
});
});
});
});
}); });
}); });
function loadFromDB(key, callback) { function loadFromDB(key) {
options.appConfigStorage.listItems(key, 0, null, function(err, cachedItems) { return options.appConfigStorage.listItems(key, 0, null).then(function(cachedItems) {
callback(err, (!err && cachedItems && cachedItems[0])); return cachedItems && cachedItems[0];
}); });
} }
} }

View File

@ -16,14 +16,9 @@ var POST_UPDATE_DB_VERSION = 5;
* Due to an overlooked issue, there may be multiple folders, e.g. for sent mails. * Due to an overlooked issue, there may be multiple folders, e.g. for sent mails.
* This removes the "duplicate" folders. * This removes the "duplicate" folders.
*/ */
function update(options, callback) { function update(options) {
// remove the emails // remove the emails
options.userStorage.listItems(FOLDER_DB_TYPE, 0, null, function(err, stored) { return options.userStorage.listItems(FOLDER_DB_TYPE, 0, null).then(function(stored) {
if (err) {
return callback(err);
}
var folders = stored[0] || []; var folders = stored[0] || [];
[FOLDER_TYPE_INBOX, FOLDER_TYPE_SENT, FOLDER_TYPE_DRAFTS, FOLDER_TYPE_TRASH].forEach(function(mbxType) { [FOLDER_TYPE_INBOX, FOLDER_TYPE_SENT, FOLDER_TYPE_DRAFTS, FOLDER_TYPE_TRASH].forEach(function(mbxType) {
var foldersForType = folders.filter(function(mbx) { var foldersForType = folders.filter(function(mbx) {
@ -39,15 +34,11 @@ function update(options, callback) {
folders.splice(folders.indexOf(foldersForType[i]), 1); folders.splice(folders.indexOf(foldersForType[i]), 1);
} }
}); });
return options.userStorage.storeList([folders], FOLDER_DB_TYPE);
options.userStorage.storeList([folders], FOLDER_DB_TYPE, function(err) { }).then(function() {
if (err) { // update the database version to POST_UPDATE_DB_VERSION
return callback(err); return options.appConfigStorage.storeList([POST_UPDATE_DB_VERSION], VERSION_DB_TYPE);
}
// update the database version to POST_UPDATE_DB_VERSION
options.appConfigStorage.storeList([POST_UPDATE_DB_VERSION], VERSION_DB_TYPE, callback);
});
}); });
} }

View File

@ -17,7 +17,7 @@ describe('UpdateHandler', function() {
userStorageStub = sinon.createStubInstance(DeviceStorageDAO); userStorageStub = sinon.createStubInstance(DeviceStorageDAO);
authStub = sinon.createStubInstance(Auth); authStub = sinon.createStubInstance(Auth);
dialogStub = sinon.createStubInstance(Dialog); dialogStub = sinon.createStubInstance(Dialog);
updateHandler = new UpdateHandler(appConfigStorageStub, userStorageStub, authStub, dialogStub); updateHandler = new UpdateHandler(qMock, appConfigStorageStub, userStorageStub, authStub, dialogStub);
}); });
afterEach(function() { afterEach(function() {
@ -40,10 +40,9 @@ describe('UpdateHandler', function() {
it('should not update when up to date', function(done) { it('should not update when up to date', function(done) {
cfg.dbVersion = 10; // app requires database version 10 cfg.dbVersion = 10; // app requires database version 10
appConfigStorageStub.listItems.withArgs(versionDbType).yieldsAsync(null, ['10']); // database version is 10 appConfigStorageStub.listItems.withArgs(versionDbType).returns(resolves(['10'])); // database version is 10
updateHandler.update(function(error) { updateHandler.update().then(function() {
expect(error).to.not.exist;
expect(appConfigStorageStub.listItems.calledOnce).to.be.true; expect(appConfigStorageStub.listItems.calledOnce).to.be.true;
done(); done();
@ -55,7 +54,7 @@ describe('UpdateHandler', function() {
beforeEach(function() { beforeEach(function() {
updateCounter = 0; updateCounter = 0;
appConfigStorageStub.listItems.withArgs(versionDbType).yieldsAsync(null, ['2']); // database version is 0 appConfigStorageStub.listItems.withArgs(versionDbType).returns(resolves(['2'])); // database version is 0
}); });
afterEach(function() { afterEach(function() {
@ -67,17 +66,16 @@ describe('UpdateHandler', function() {
cfg.dbVersion = 4; // app requires database version 4 cfg.dbVersion = 4; // app requires database version 4
// a simple dummy update to executed that only increments the update counter // a simple dummy update to executed that only increments the update counter
function dummyUpdate(options, callback) { function dummyUpdate() {
updateCounter++; updateCounter++;
callback(); return resolves();
} }
// inject the dummy updates instead of live ones // inject the dummy updates instead of live ones
updateHandler._updateScripts = [dummyUpdate, dummyUpdate, dummyUpdate, dummyUpdate]; updateHandler._updateScripts = [dummyUpdate, dummyUpdate, dummyUpdate, dummyUpdate];
// execute test // execute test
updateHandler.update(function(error) { updateHandler.update().then(function() {
expect(error).to.not.exist;
expect(updateCounter).to.equal(2); expect(updateCounter).to.equal(2);
done(); done();
@ -87,20 +85,20 @@ describe('UpdateHandler', function() {
it('should fail while updating to v3', function(done) { it('should fail while updating to v3', function(done) {
cfg.dbVersion = 4; // app requires database version 4 cfg.dbVersion = 4; // app requires database version 4
function dummyUpdate(options, callback) { function dummyUpdate() {
updateCounter++; updateCounter++;
callback(); return resolves();
} }
function failingUpdate(options, callback) { function failingUpdate() {
callback({}); return rejects({});
} }
// inject the dummy updates instead of live ones // inject the dummy updates instead of live ones
updateHandler._updateScripts = [dummyUpdate, dummyUpdate, failingUpdate, dummyUpdate]; updateHandler._updateScripts = [dummyUpdate, dummyUpdate, failingUpdate, dummyUpdate];
// execute test // execute test
updateHandler.update(function(error) { updateHandler.update().catch(function(error) {
expect(error).to.exist; expect(error).to.exist;
expect(updateCounter).to.equal(0); expect(updateCounter).to.equal(0);
@ -115,7 +113,7 @@ describe('UpdateHandler', function() {
beforeEach(function() { beforeEach(function() {
cfg.dbVersion = 1; // app requires database version 1 cfg.dbVersion = 1; // app requires database version 1
appConfigStorageStub.listItems.withArgs(versionDbType).yieldsAsync(); // database version is 0 appConfigStorageStub.listItems.withArgs(versionDbType).returns(resolves()); // database version is 0
}); });
afterEach(function() { afterEach(function() {
@ -125,11 +123,10 @@ describe('UpdateHandler', function() {
}); });
it('should work', function(done) { it('should work', function(done) {
userStorageStub.removeList.withArgs(emailDbType).yieldsAsync(); userStorageStub.removeList.withArgs(emailDbType).returns(resolves());
appConfigStorageStub.storeList.withArgs([1], versionDbType).yieldsAsync(); appConfigStorageStub.storeList.withArgs([1], versionDbType).returns(resolves());
updateHandler.update(function(error) { updateHandler.update().then(function() {
expect(error).to.not.exist;
expect(userStorageStub.removeList.calledOnce).to.be.true; expect(userStorageStub.removeList.calledOnce).to.be.true;
expect(appConfigStorageStub.storeList.calledOnce).to.be.true; expect(appConfigStorageStub.storeList.calledOnce).to.be.true;
@ -138,10 +135,10 @@ describe('UpdateHandler', function() {
}); });
it('should fail when persisting database version fails', function(done) { it('should fail when persisting database version fails', function(done) {
userStorageStub.removeList.yieldsAsync(); userStorageStub.removeList.returns(resolves());
appConfigStorageStub.storeList.yieldsAsync(new Error()); appConfigStorageStub.storeList.returns(rejects(new Error()));
updateHandler.update(function(error) { updateHandler.update().catch(function(error) {
expect(error).to.exist; expect(error).to.exist;
expect(userStorageStub.removeList.calledOnce).to.be.true; expect(userStorageStub.removeList.calledOnce).to.be.true;
expect(appConfigStorageStub.storeList.calledOnce).to.be.true; expect(appConfigStorageStub.storeList.calledOnce).to.be.true;
@ -151,9 +148,9 @@ describe('UpdateHandler', function() {
}); });
it('should fail when wiping emails from database fails', function(done) { it('should fail when wiping emails from database fails', function(done) {
userStorageStub.removeList.yieldsAsync(new Error()); userStorageStub.removeList.returns(rejects(new Error()));
updateHandler.update(function(error) { updateHandler.update().catch(function(error) {
expect(error).to.exist; expect(error).to.exist;
expect(userStorageStub.removeList.calledOnce).to.be.true; expect(userStorageStub.removeList.calledOnce).to.be.true;
expect(appConfigStorageStub.storeList.called).to.be.false; expect(appConfigStorageStub.storeList.called).to.be.false;
@ -168,7 +165,7 @@ describe('UpdateHandler', function() {
beforeEach(function() { beforeEach(function() {
cfg.dbVersion = 2; // app requires database version 2 cfg.dbVersion = 2; // app requires database version 2
appConfigStorageStub.listItems.withArgs(versionDbType).yieldsAsync(null, [1]); // database version is 0 appConfigStorageStub.listItems.withArgs(versionDbType).returns(resolves([1])); // database version is 0
}); });
afterEach(function() { afterEach(function() {
@ -178,11 +175,10 @@ describe('UpdateHandler', function() {
}); });
it('should work', function(done) { it('should work', function(done) {
userStorageStub.removeList.withArgs(emailDbType).yieldsAsync(); userStorageStub.removeList.withArgs(emailDbType).returns(resolves());
appConfigStorageStub.storeList.withArgs([2], versionDbType).yieldsAsync(); appConfigStorageStub.storeList.withArgs([2], versionDbType).returns(resolves());
updateHandler.update(function(error) { updateHandler.update().then(function() {
expect(error).to.not.exist;
expect(userStorageStub.removeList.calledOnce).to.be.true; expect(userStorageStub.removeList.calledOnce).to.be.true;
expect(appConfigStorageStub.storeList.calledOnce).to.be.true; expect(appConfigStorageStub.storeList.calledOnce).to.be.true;
@ -191,10 +187,10 @@ describe('UpdateHandler', function() {
}); });
it('should fail when persisting database version fails', function(done) { it('should fail when persisting database version fails', function(done) {
userStorageStub.removeList.yieldsAsync(); userStorageStub.removeList.returns(resolves());
appConfigStorageStub.storeList.yieldsAsync(new Error()); appConfigStorageStub.storeList.returns(rejects(new Error()));
updateHandler.update(function(error) { updateHandler.update().catch(function(error) {
expect(error).to.exist; expect(error).to.exist;
expect(userStorageStub.removeList.calledOnce).to.be.true; expect(userStorageStub.removeList.calledOnce).to.be.true;
expect(appConfigStorageStub.storeList.calledOnce).to.be.true; expect(appConfigStorageStub.storeList.calledOnce).to.be.true;
@ -204,9 +200,9 @@ describe('UpdateHandler', function() {
}); });
it('should fail when wiping emails from database fails', function(done) { it('should fail when wiping emails from database fails', function(done) {
userStorageStub.removeList.yieldsAsync(new Error()); userStorageStub.removeList.returns(rejects(new Error()));
updateHandler.update(function(error) { updateHandler.update().catch(function(error) {
expect(error).to.exist; expect(error).to.exist;
expect(userStorageStub.removeList.calledOnce).to.be.true; expect(userStorageStub.removeList.calledOnce).to.be.true;
expect(appConfigStorageStub.storeList.called).to.be.false; expect(appConfigStorageStub.storeList.called).to.be.false;
@ -221,7 +217,7 @@ describe('UpdateHandler', function() {
beforeEach(function() { beforeEach(function() {
cfg.dbVersion = 3; // app requires database version 2 cfg.dbVersion = 3; // app requires database version 2
appConfigStorageStub.listItems.withArgs(versionDbType).yieldsAsync(null, [2]); // database version is 0 appConfigStorageStub.listItems.withArgs(versionDbType).returns(resolves([2])); // database version is 0
}); });
afterEach(function() { afterEach(function() {
@ -231,11 +227,10 @@ describe('UpdateHandler', function() {
}); });
it('should work', function(done) { it('should work', function(done) {
userStorageStub.removeList.withArgs(emailDbType).yieldsAsync(); userStorageStub.removeList.withArgs(emailDbType).returns(resolves());
appConfigStorageStub.storeList.withArgs([3], versionDbType).yieldsAsync(); appConfigStorageStub.storeList.withArgs([3], versionDbType).returns(resolves());
updateHandler.update(function(error) { updateHandler.update().then(function() {
expect(error).to.not.exist;
expect(userStorageStub.removeList.calledOnce).to.be.true; expect(userStorageStub.removeList.calledOnce).to.be.true;
expect(appConfigStorageStub.storeList.calledOnce).to.be.true; expect(appConfigStorageStub.storeList.calledOnce).to.be.true;
@ -244,10 +239,10 @@ describe('UpdateHandler', function() {
}); });
it('should fail when persisting database version fails', function(done) { it('should fail when persisting database version fails', function(done) {
userStorageStub.removeList.yieldsAsync(); userStorageStub.removeList.returns(resolves());
appConfigStorageStub.storeList.yieldsAsync(new Error()); appConfigStorageStub.storeList.returns(rejects(new Error()));
updateHandler.update(function(error) { updateHandler.update().catch(function(error) {
expect(error).to.exist; expect(error).to.exist;
expect(userStorageStub.removeList.calledOnce).to.be.true; expect(userStorageStub.removeList.calledOnce).to.be.true;
expect(appConfigStorageStub.storeList.calledOnce).to.be.true; expect(appConfigStorageStub.storeList.calledOnce).to.be.true;
@ -257,9 +252,9 @@ describe('UpdateHandler', function() {
}); });
it('should fail when wiping emails from database fails', function(done) { it('should fail when wiping emails from database fails', function(done) {
userStorageStub.removeList.yieldsAsync(new Error()); userStorageStub.removeList.returns(rejects(new Error()));
updateHandler.update(function(error) { updateHandler.update().catch(function(error) {
expect(error).to.exist; expect(error).to.exist;
expect(userStorageStub.removeList.calledOnce).to.be.true; expect(userStorageStub.removeList.calledOnce).to.be.true;
expect(appConfigStorageStub.storeList.called).to.be.false; expect(appConfigStorageStub.storeList.called).to.be.false;
@ -290,22 +285,21 @@ describe('UpdateHandler', function() {
beforeEach(function() { beforeEach(function() {
cfg.dbVersion = 4; // app requires database version 4 cfg.dbVersion = 4; // app requires database version 4
appConfigStorageStub.listItems.withArgs(versionDbType).yieldsAsync(null, [3]); // database version is 3 appConfigStorageStub.listItems.withArgs(versionDbType).returns(resolves([3])); // database version is 3
}); });
it('should add gmail as mail service provider with email address and no provider present in db', function(done) { it('should add gmail as mail service provider with email address and no provider present in db', function(done) {
appConfigStorageStub.listItems.withArgs(EMAIL_ADDR_DB_KEY).yieldsAsync(null, [emailaddress]); appConfigStorageStub.listItems.withArgs(EMAIL_ADDR_DB_KEY).returns(resolves([emailaddress]));
appConfigStorageStub.listItems.withArgs(PROVIDER_DB_KEY).yieldsAsync(null, []); appConfigStorageStub.listItems.withArgs(PROVIDER_DB_KEY).returns(resolves([]));
appConfigStorageStub.storeList.withArgs([4], versionDbType).yieldsAsync(); appConfigStorageStub.storeList.withArgs([4], versionDbType).returns(resolves());
appConfigStorageStub.storeList.withArgs(['gmail'], PROVIDER_DB_KEY).yieldsAsync(); appConfigStorageStub.storeList.withArgs(['gmail'], PROVIDER_DB_KEY).returns(resolves());
appConfigStorageStub.storeList.withArgs([emailaddress], USERNAME_DB_KEY).yieldsAsync(); appConfigStorageStub.storeList.withArgs([emailaddress], USERNAME_DB_KEY).returns(resolves());
appConfigStorageStub.storeList.withArgs([imap], IMAP_DB_KEY).yieldsAsync(); appConfigStorageStub.storeList.withArgs([imap], IMAP_DB_KEY).returns(resolves());
appConfigStorageStub.storeList.withArgs([smtp], SMTP_DB_KEY).yieldsAsync(); appConfigStorageStub.storeList.withArgs([smtp], SMTP_DB_KEY).returns(resolves());
appConfigStorageStub.storeList.withArgs([''], REALNAME_DB_KEY).yieldsAsync(); appConfigStorageStub.storeList.withArgs([''], REALNAME_DB_KEY).returns(resolves());
authStub._loadCredentials.yieldsAsync(); authStub._loadCredentials.returns(resolves());
updateHandler.update(function(error) { updateHandler.update().then(function() {
expect(error).to.not.exist;
expect(appConfigStorageStub.storeList.callCount).to.equal(6); expect(appConfigStorageStub.storeList.callCount).to.equal(6);
expect(appConfigStorageStub.listItems.calledThrice).to.be.true; expect(appConfigStorageStub.listItems.calledThrice).to.be.true;
expect(authStub._loadCredentials.calledOnce).to.be.true; expect(authStub._loadCredentials.calledOnce).to.be.true;
@ -315,12 +309,11 @@ describe('UpdateHandler', function() {
}); });
it('should not add a provider when no email adress is in db', function(done) { it('should not add a provider when no email adress is in db', function(done) {
appConfigStorageStub.listItems.withArgs(EMAIL_ADDR_DB_KEY).yieldsAsync(null, []); appConfigStorageStub.listItems.withArgs(EMAIL_ADDR_DB_KEY).returns(resolves([]));
appConfigStorageStub.listItems.withArgs(PROVIDER_DB_KEY).yieldsAsync(null, []); appConfigStorageStub.listItems.withArgs(PROVIDER_DB_KEY).returns(resolves([]));
appConfigStorageStub.storeList.withArgs([4], versionDbType).yieldsAsync(); appConfigStorageStub.storeList.withArgs([4], versionDbType).returns(resolves());
updateHandler.update(function(error) { updateHandler.update().then(function() {
expect(error).to.not.exist;
expect(appConfigStorageStub.storeList.calledOnce).to.be.true; expect(appConfigStorageStub.storeList.calledOnce).to.be.true;
expect(appConfigStorageStub.listItems.calledThrice).to.be.true; expect(appConfigStorageStub.listItems.calledThrice).to.be.true;
@ -329,10 +322,10 @@ describe('UpdateHandler', function() {
}); });
it('should fail when appConfigStore write fails', function(done) { it('should fail when appConfigStore write fails', function(done) {
appConfigStorageStub.listItems.yieldsAsync(null, []); appConfigStorageStub.listItems.returns(resolves([]));
appConfigStorageStub.storeList.yieldsAsync(new Error()); appConfigStorageStub.storeList.returns(rejects(new Error()));
updateHandler.update(function(error) { updateHandler.update().catch(function(error) {
expect(error).to.exist; expect(error).to.exist;
expect(appConfigStorageStub.listItems.calledThrice).to.be.true; expect(appConfigStorageStub.listItems.calledThrice).to.be.true;
expect(appConfigStorageStub.storeList.calledOnce).to.be.true; expect(appConfigStorageStub.storeList.calledOnce).to.be.true;
@ -342,10 +335,10 @@ describe('UpdateHandler', function() {
}); });
it('should fail when appConfigStore read fails', function(done) { it('should fail when appConfigStore read fails', function(done) {
appConfigStorageStub.listItems.withArgs(EMAIL_ADDR_DB_KEY).yieldsAsync(new Error()); appConfigStorageStub.listItems.withArgs(EMAIL_ADDR_DB_KEY).returns(rejects(new Error()));
appConfigStorageStub.storeList.yieldsAsync(new Error()); appConfigStorageStub.storeList.returns(rejects(new Error()));
updateHandler.update(function(error) { updateHandler.update().catch(function(error) {
expect(error).to.exist; expect(error).to.exist;
expect(appConfigStorageStub.listItems.calledTwice).to.be.true; expect(appConfigStorageStub.listItems.calledTwice).to.be.true;
expect(appConfigStorageStub.storeList.called).to.be.false; expect(appConfigStorageStub.storeList.called).to.be.false;
@ -368,7 +361,7 @@ describe('UpdateHandler', function() {
beforeEach(function() { beforeEach(function() {
cfg.dbVersion = 5; // app requires database version 4 cfg.dbVersion = 5; // app requires database version 4
appConfigStorageStub.listItems.withArgs(VERSION_DB_TYPE).yieldsAsync(null, [4]); // database version is 4 appConfigStorageStub.listItems.withArgs(VERSION_DB_TYPE).returns(resolves([4])); // database version is 4
}); });
afterEach(function() { afterEach(function() {
@ -378,7 +371,7 @@ describe('UpdateHandler', function() {
}); });
it('should work', function(done) { it('should work', function(done) {
userStorageStub.listItems.withArgs(FOLDER_DB_TYPE).yieldsAsync(null, [ userStorageStub.listItems.withArgs(FOLDER_DB_TYPE).returns(resolves([
[{ [{
name: 'inbox1', name: 'inbox1',
type: FOLDER_TYPE_INBOX type: FOLDER_TYPE_INBOX
@ -404,7 +397,7 @@ describe('UpdateHandler', function() {
name: 'trash2', name: 'trash2',
type: FOLDER_TYPE_TRASH type: FOLDER_TYPE_TRASH
}] }]
]); ]));
userStorageStub.storeList.withArgs([ userStorageStub.storeList.withArgs([
[{ [{
@ -420,12 +413,11 @@ describe('UpdateHandler', function() {
name: 'trash1', name: 'trash1',
type: FOLDER_TYPE_TRASH type: FOLDER_TYPE_TRASH
}] }]
], FOLDER_DB_TYPE).yieldsAsync(); ], FOLDER_DB_TYPE).returns(resolves());
appConfigStorageStub.storeList.withArgs([POST_UPDATE_DB_VERSION], VERSION_DB_TYPE).yieldsAsync(); appConfigStorageStub.storeList.withArgs([POST_UPDATE_DB_VERSION], VERSION_DB_TYPE).returns(resolves());
updateHandler.update(function(error) { updateHandler.update().then(function() {
expect(error).to.not.exist;
expect(userStorageStub.listItems.calledOnce).to.be.true; expect(userStorageStub.listItems.calledOnce).to.be.true;
expect(userStorageStub.storeList.calledOnce).to.be.true; expect(userStorageStub.storeList.calledOnce).to.be.true;
expect(appConfigStorageStub.storeList.calledOnce).to.be.true; expect(appConfigStorageStub.storeList.calledOnce).to.be.true;
@ -435,11 +427,11 @@ describe('UpdateHandler', function() {
}); });
it('should fail when persisting database version fails', function(done) { it('should fail when persisting database version fails', function(done) {
userStorageStub.listItems.yieldsAsync(null, []); userStorageStub.listItems.returns(resolves([]));
userStorageStub.storeList.yieldsAsync(); userStorageStub.storeList.returns(resolves());
appConfigStorageStub.storeList.yieldsAsync(new Error()); appConfigStorageStub.storeList.returns(rejects(new Error()));
updateHandler.update(function(error) { updateHandler.update().catch(function(error) {
expect(error).to.exist; expect(error).to.exist;
expect(userStorageStub.listItems.calledOnce).to.be.true; expect(userStorageStub.listItems.calledOnce).to.be.true;
expect(userStorageStub.storeList.calledOnce).to.be.true; expect(userStorageStub.storeList.calledOnce).to.be.true;
@ -450,10 +442,10 @@ describe('UpdateHandler', function() {
}); });
it('should fail when persisting folders fails', function(done) { it('should fail when persisting folders fails', function(done) {
userStorageStub.listItems.yieldsAsync(null, []); userStorageStub.listItems.returns(resolves([]));
userStorageStub.storeList.yieldsAsync(new Error()); userStorageStub.storeList.returns(rejects(new Error()));
updateHandler.update(function(error) { updateHandler.update().catch(function(error) {
expect(error).to.exist; expect(error).to.exist;
expect(userStorageStub.listItems.calledOnce).to.be.true; expect(userStorageStub.listItems.calledOnce).to.be.true;
expect(userStorageStub.storeList.calledOnce).to.be.true; expect(userStorageStub.storeList.calledOnce).to.be.true;
@ -464,9 +456,9 @@ describe('UpdateHandler', function() {
}); });
it('should fail when listing folders fails', function(done) { it('should fail when listing folders fails', function(done) {
userStorageStub.listItems.yieldsAsync(new Error()); userStorageStub.listItems.returns(rejects(new Error()));
updateHandler.update(function(error) { updateHandler.update().catch(function(error) {
expect(error).to.exist; expect(error).to.exist;
expect(userStorageStub.listItems.calledOnce).to.be.true; expect(userStorageStub.listItems.calledOnce).to.be.true;
expect(userStorageStub.storeList.called).to.be.false; expect(userStorageStub.storeList.called).to.be.false;