1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-30 04:42:15 -05:00
mail/test/unit/update-handler-test.js

471 lines
19 KiB
JavaScript
Raw Normal View History

2014-10-07 14:32:23 -04:00
'use strict';
2014-03-11 11:06:19 -04:00
2014-10-07 14:32:23 -04:00
var DeviceStorageDAO = require('../../src/js/dao/devicestorage-dao'),
Auth = require('../../src/js/bo/auth'),
cfg = require('../../src/js/app-config').config,
UpdateHandler = require('../../src/js/util/update/update-handler'),
config = require('../../src/js/app-config').config;
2014-03-11 11:06:19 -04:00
2014-10-07 14:32:23 -04:00
describe('UpdateHandler', function() {
var updateHandler, appConfigStorageStub, authStub, userStorageStub, origDbVersion;
2014-03-11 11:06:19 -04:00
2014-10-07 14:32:23 -04:00
chai.Assertion.includeStack = true;
beforeEach(function() {
origDbVersion = cfg.dbVersion;
appConfigStorageStub = sinon.createStubInstance(DeviceStorageDAO);
userStorageStub = sinon.createStubInstance(DeviceStorageDAO);
authStub = sinon.createStubInstance(Auth);
updateHandler = new UpdateHandler(appConfigStorageStub, userStorageStub, authStub);
});
afterEach(function() {
cfg.dbVersion = origDbVersion;
});
describe('#constructor', function() {
it('should create instance', function() {
expect(updateHandler).to.exist;
expect(updateHandler._appConfigStorage).to.equal(appConfigStorageStub);
expect(updateHandler._userStorage).to.equal(userStorageStub);
// the update handler must contain as many db update sripts as there are database versions
expect(updateHandler._updateScripts.length).to.equal(cfg.dbVersion);
2014-03-11 11:06:19 -04:00
});
2014-10-07 14:32:23 -04:00
});
2014-03-11 11:06:19 -04:00
2014-10-07 14:32:23 -04:00
describe('#update', function() {
var versionDbType = 'dbVersion';
2014-03-11 11:06:19 -04:00
2014-10-07 14:32:23 -04:00
it('should not update when up to date', function(done) {
cfg.dbVersion = 10; // app requires database version 10
appConfigStorageStub.listItems.withArgs(versionDbType).yieldsAsync(null, ['10']); // database version is 10
2014-03-11 11:06:19 -04:00
2014-10-07 14:32:23 -04:00
updateHandler.update(function(error) {
expect(error).to.not.exist;
expect(appConfigStorageStub.listItems.calledOnce).to.be.true;
2014-03-11 11:06:19 -04:00
2014-10-07 14:32:23 -04:00
done();
2014-03-11 11:06:19 -04:00
});
2014-10-07 14:32:23 -04:00
});
2014-03-11 11:06:19 -04:00
2014-10-07 14:32:23 -04:00
describe('dummy updates for v2 to v4', function() {
var updateCounter;
2014-03-11 11:06:19 -04:00
2014-10-07 14:32:23 -04:00
beforeEach(function() {
updateCounter = 0;
appConfigStorageStub.listItems.withArgs(versionDbType).yieldsAsync(null, ['2']); // database version is 0
});
2014-03-11 11:06:19 -04:00
2014-10-07 14:32:23 -04:00
afterEach(function() {
expect(appConfigStorageStub.listItems.calledOnce).to.be.true;
});
2014-03-11 11:06:19 -04:00
2014-10-07 14:32:23 -04:00
it('should work', function(done) {
cfg.dbVersion = 4; // app requires database version 4
2014-03-11 11:06:19 -04:00
2014-10-07 14:32:23 -04:00
// a simple dummy update to executed that only increments the update counter
function dummyUpdate(options, callback) {
updateCounter++;
callback();
}
2014-03-11 11:06:19 -04:00
2014-10-07 14:32:23 -04:00
// inject the dummy updates instead of live ones
updateHandler._updateScripts = [dummyUpdate, dummyUpdate, dummyUpdate, dummyUpdate];
2014-03-11 11:06:19 -04:00
2014-10-07 14:32:23 -04:00
// execute test
updateHandler.update(function(error) {
expect(error).to.not.exist;
expect(updateCounter).to.equal(2);
2014-03-11 11:06:19 -04:00
2014-10-07 14:32:23 -04:00
done();
2014-03-11 11:06:19 -04:00
});
2014-10-07 14:32:23 -04:00
});
2014-03-11 11:06:19 -04:00
2014-10-07 14:32:23 -04:00
it('should fail while updating to v3', function(done) {
cfg.dbVersion = 4; // app requires database version 4
2014-03-11 11:06:19 -04:00
2014-10-07 14:32:23 -04:00
function dummyUpdate(options, callback) {
updateCounter++;
callback();
}
2014-03-11 11:06:19 -04:00
2014-10-07 14:32:23 -04:00
function failingUpdate(options, callback) {
callback({});
}
2014-03-11 11:06:19 -04:00
2014-10-07 14:32:23 -04:00
// inject the dummy updates instead of live ones
updateHandler._updateScripts = [dummyUpdate, dummyUpdate, failingUpdate, dummyUpdate];
2014-03-11 11:06:19 -04:00
2014-10-07 14:32:23 -04:00
// execute test
updateHandler.update(function(error) {
expect(error).to.exist;
expect(updateCounter).to.equal(0);
2014-03-11 11:06:19 -04:00
2014-10-07 14:32:23 -04:00
done();
2014-03-11 11:06:19 -04:00
});
});
2014-10-07 14:32:23 -04:00
});
2014-03-11 11:06:19 -04:00
2014-10-07 14:32:23 -04:00
describe('v0 -> v1', function() {
var emailDbType = 'email_';
2014-03-11 11:06:19 -04:00
2014-10-07 14:32:23 -04:00
beforeEach(function() {
cfg.dbVersion = 1; // app requires database version 1
appConfigStorageStub.listItems.withArgs(versionDbType).yieldsAsync(); // database version is 0
});
2014-03-11 11:06:19 -04:00
2014-10-07 14:32:23 -04:00
afterEach(function() {
// database version is only queried for version checking prior to the update script
// so no need to check this in case-specific tests
expect(appConfigStorageStub.listItems.calledOnce).to.be.true;
});
2014-03-11 11:06:19 -04:00
2014-10-07 14:32:23 -04:00
it('should work', function(done) {
userStorageStub.removeList.withArgs(emailDbType).yieldsAsync();
appConfigStorageStub.storeList.withArgs([1], versionDbType).yieldsAsync();
updateHandler.update(function(error) {
expect(error).to.not.exist;
expect(userStorageStub.removeList.calledOnce).to.be.true;
expect(appConfigStorageStub.storeList.calledOnce).to.be.true;
2014-03-11 11:06:19 -04:00
2014-10-07 14:32:23 -04:00
done();
2014-03-11 11:06:19 -04:00
});
2014-10-07 14:32:23 -04:00
});
2014-03-11 11:06:19 -04:00
2014-10-07 14:32:23 -04:00
it('should fail when persisting database version fails', function(done) {
userStorageStub.removeList.yieldsAsync();
appConfigStorageStub.storeList.yieldsAsync(new Error());
2014-10-07 14:32:23 -04:00
updateHandler.update(function(error) {
expect(error).to.exist;
expect(userStorageStub.removeList.calledOnce).to.be.true;
expect(appConfigStorageStub.storeList.calledOnce).to.be.true;
2014-10-07 14:32:23 -04:00
done();
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
it('should fail when wiping emails from database fails', function(done) {
userStorageStub.removeList.yieldsAsync(new Error());
2014-10-07 14:32:23 -04:00
updateHandler.update(function(error) {
expect(error).to.exist;
expect(userStorageStub.removeList.calledOnce).to.be.true;
expect(appConfigStorageStub.storeList.called).to.be.false;
2014-10-07 14:32:23 -04:00
done();
});
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
describe('v1 -> v2', function() {
var emailDbType = 'email_';
2014-10-07 14:32:23 -04:00
beforeEach(function() {
cfg.dbVersion = 2; // app requires database version 2
appConfigStorageStub.listItems.withArgs(versionDbType).yieldsAsync(null, [1]); // database version is 0
});
2014-10-07 14:32:23 -04:00
afterEach(function() {
// database version is only queried for version checking prior to the update script
// so no need to check this in case-specific tests
expect(appConfigStorageStub.listItems.calledOnce).to.be.true;
});
2014-10-07 14:32:23 -04:00
it('should work', function(done) {
userStorageStub.removeList.withArgs(emailDbType).yieldsAsync();
appConfigStorageStub.storeList.withArgs([2], versionDbType).yieldsAsync();
2014-10-07 14:32:23 -04:00
updateHandler.update(function(error) {
expect(error).to.not.exist;
expect(userStorageStub.removeList.calledOnce).to.be.true;
expect(appConfigStorageStub.storeList.calledOnce).to.be.true;
2014-10-07 14:32:23 -04:00
done();
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
it('should fail when persisting database version fails', function(done) {
userStorageStub.removeList.yieldsAsync();
appConfigStorageStub.storeList.yieldsAsync(new Error());
2014-10-07 14:32:23 -04:00
updateHandler.update(function(error) {
expect(error).to.exist;
expect(userStorageStub.removeList.calledOnce).to.be.true;
expect(appConfigStorageStub.storeList.calledOnce).to.be.true;
2014-10-07 14:32:23 -04:00
done();
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
it('should fail when wiping emails from database fails', function(done) {
userStorageStub.removeList.yieldsAsync(new Error());
2014-10-07 14:32:23 -04:00
updateHandler.update(function(error) {
expect(error).to.exist;
expect(userStorageStub.removeList.calledOnce).to.be.true;
expect(appConfigStorageStub.storeList.called).to.be.false;
2014-10-07 14:32:23 -04:00
done();
});
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
describe('v2 -> v3', function() {
var emailDbType = 'email_';
2014-10-07 14:32:23 -04:00
beforeEach(function() {
cfg.dbVersion = 3; // app requires database version 2
appConfigStorageStub.listItems.withArgs(versionDbType).yieldsAsync(null, [2]); // database version is 0
});
2014-10-07 14:32:23 -04:00
afterEach(function() {
// database version is only queried for version checking prior to the update script
// so no need to check this in case-specific tests
expect(appConfigStorageStub.listItems.calledOnce).to.be.true;
});
2014-10-07 14:32:23 -04:00
it('should work', function(done) {
userStorageStub.removeList.withArgs(emailDbType).yieldsAsync();
appConfigStorageStub.storeList.withArgs([3], versionDbType).yieldsAsync();
2014-10-07 14:32:23 -04:00
updateHandler.update(function(error) {
expect(error).to.not.exist;
expect(userStorageStub.removeList.calledOnce).to.be.true;
expect(appConfigStorageStub.storeList.calledOnce).to.be.true;
2014-10-07 14:32:23 -04:00
done();
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
it('should fail when persisting database version fails', function(done) {
userStorageStub.removeList.yieldsAsync();
appConfigStorageStub.storeList.yieldsAsync(new Error());
2014-03-11 11:06:19 -04:00
2014-10-07 14:32:23 -04:00
updateHandler.update(function(error) {
expect(error).to.exist;
expect(userStorageStub.removeList.calledOnce).to.be.true;
expect(appConfigStorageStub.storeList.calledOnce).to.be.true;
2014-03-11 11:06:19 -04:00
2014-10-07 14:32:23 -04:00
done();
2014-03-11 11:06:19 -04:00
});
2014-10-07 14:32:23 -04:00
});
2014-03-11 11:06:19 -04:00
2014-10-07 14:32:23 -04:00
it('should fail when wiping emails from database fails', function(done) {
userStorageStub.removeList.yieldsAsync(new Error());
2014-03-11 11:06:19 -04:00
2014-10-07 14:32:23 -04:00
updateHandler.update(function(error) {
expect(error).to.exist;
expect(userStorageStub.removeList.calledOnce).to.be.true;
expect(appConfigStorageStub.storeList.called).to.be.false;
2014-03-11 11:06:19 -04:00
2014-10-07 14:32:23 -04:00
done();
});
});
2014-10-07 14:32:23 -04:00
});
describe('v3 -> v4', function() {
var EMAIL_ADDR_DB_KEY = 'emailaddress';
var USERNAME_DB_KEY = 'username';
var PROVIDER_DB_KEY = 'provider';
var IMAP_DB_KEY = 'imap';
var SMTP_DB_KEY = 'smtp';
var REALNAME_DB_KEY = 'realname';
var emailaddress = 'bla@blubb.io';
var imap = config.gmail.imap,
smtp = config.gmail.smtp;
beforeEach(function() {
cfg.dbVersion = 4; // app requires database version 4
appConfigStorageStub.listItems.withArgs(versionDbType).yieldsAsync(null, [3]); // database version is 3
});
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(PROVIDER_DB_KEY).yieldsAsync(null, []);
appConfigStorageStub.storeList.withArgs([4], versionDbType).yieldsAsync();
appConfigStorageStub.storeList.withArgs(['gmail'], PROVIDER_DB_KEY).yieldsAsync();
appConfigStorageStub.storeList.withArgs([emailaddress], USERNAME_DB_KEY).yieldsAsync();
appConfigStorageStub.storeList.withArgs([imap], IMAP_DB_KEY).yieldsAsync();
appConfigStorageStub.storeList.withArgs([smtp], SMTP_DB_KEY).yieldsAsync();
appConfigStorageStub.storeList.withArgs([''], REALNAME_DB_KEY).yieldsAsync();
authStub._loadCredentials.yieldsAsync();
2014-10-07 14:32:23 -04:00
updateHandler.update(function(error) {
expect(error).to.not.exist;
expect(appConfigStorageStub.storeList.callCount).to.equal(6);
expect(appConfigStorageStub.listItems.calledThrice).to.be.true;
expect(authStub._loadCredentials.calledOnce).to.be.true;
done();
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
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(PROVIDER_DB_KEY).yieldsAsync(null, []);
appConfigStorageStub.storeList.withArgs([4], versionDbType).yieldsAsync();
2014-10-07 14:32:23 -04:00
updateHandler.update(function(error) {
expect(error).to.not.exist;
expect(appConfigStorageStub.storeList.calledOnce).to.be.true;
expect(appConfigStorageStub.listItems.calledThrice).to.be.true;
2014-10-07 14:32:23 -04:00
done();
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
it('should fail when appConfigStore write fails', function(done) {
appConfigStorageStub.listItems.yieldsAsync(null, []);
appConfigStorageStub.storeList.yieldsAsync(new Error());
2014-10-07 14:32:23 -04:00
updateHandler.update(function(error) {
expect(error).to.exist;
expect(appConfigStorageStub.listItems.calledThrice).to.be.true;
expect(appConfigStorageStub.storeList.calledOnce).to.be.true;
2014-10-07 14:32:23 -04:00
done();
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
it('should fail when appConfigStore read fails', function(done) {
appConfigStorageStub.listItems.withArgs(EMAIL_ADDR_DB_KEY).yieldsAsync(new Error());
appConfigStorageStub.storeList.yieldsAsync(new Error());
2014-10-07 14:32:23 -04:00
updateHandler.update(function(error) {
expect(error).to.exist;
expect(appConfigStorageStub.listItems.calledTwice).to.be.true;
expect(appConfigStorageStub.storeList.called).to.be.false;
2014-10-07 14:32:23 -04:00
done();
2014-03-11 11:06:19 -04:00
});
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
describe('v4 -> v5', function() {
var FOLDER_TYPE_INBOX = 'Inbox';
var FOLDER_TYPE_SENT = 'Sent';
var FOLDER_TYPE_DRAFTS = 'Drafts';
var FOLDER_TYPE_TRASH = 'Trash';
2014-10-07 14:32:23 -04:00
var FOLDER_DB_TYPE = 'folders';
var VERSION_DB_TYPE = 'dbVersion';
2014-10-07 14:32:23 -04:00
var POST_UPDATE_DB_VERSION = 5;
2014-10-07 14:32:23 -04:00
beforeEach(function() {
cfg.dbVersion = 5; // app requires database version 4
appConfigStorageStub.listItems.withArgs(VERSION_DB_TYPE).yieldsAsync(null, [4]); // database version is 4
});
2014-10-07 14:32:23 -04:00
afterEach(function() {
// database version is only queried for version checking prior to the update script
// so no need to check this in case-specific tests
expect(appConfigStorageStub.listItems.calledOnce).to.be.true;
});
it('should work', function(done) {
userStorageStub.listItems.withArgs(FOLDER_DB_TYPE).yieldsAsync(null, [
[{
name: 'inbox1',
type: FOLDER_TYPE_INBOX
}, {
name: 'inbox2',
type: FOLDER_TYPE_INBOX
}, {
name: 'sent1',
type: FOLDER_TYPE_SENT
}, {
name: 'sent2',
type: FOLDER_TYPE_SENT
}, {
name: 'drafts1',
type: FOLDER_TYPE_DRAFTS
}, {
name: 'drafts2',
type: FOLDER_TYPE_DRAFTS
}, {
name: 'trash1',
type: FOLDER_TYPE_TRASH
}, {
name: 'trash2',
type: FOLDER_TYPE_TRASH
}]
]);
userStorageStub.storeList.withArgs([
[{
name: 'inbox1',
type: FOLDER_TYPE_INBOX
}, {
name: 'sent1',
type: FOLDER_TYPE_SENT
}, {
name: 'drafts1',
type: FOLDER_TYPE_DRAFTS
}, {
name: 'trash1',
type: FOLDER_TYPE_TRASH
}]
], FOLDER_DB_TYPE).yieldsAsync();
appConfigStorageStub.storeList.withArgs([POST_UPDATE_DB_VERSION], VERSION_DB_TYPE).yieldsAsync();
2014-10-07 14:32:23 -04:00
updateHandler.update(function(error) {
expect(error).to.not.exist;
expect(userStorageStub.listItems.calledOnce).to.be.true;
expect(userStorageStub.storeList.calledOnce).to.be.true;
expect(appConfigStorageStub.storeList.calledOnce).to.be.true;
done();
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
it('should fail when persisting database version fails', function(done) {
userStorageStub.listItems.yieldsAsync(null, []);
userStorageStub.storeList.yieldsAsync();
appConfigStorageStub.storeList.yieldsAsync(new Error());
2014-10-07 14:32:23 -04:00
updateHandler.update(function(error) {
expect(error).to.exist;
expect(userStorageStub.listItems.calledOnce).to.be.true;
expect(userStorageStub.storeList.calledOnce).to.be.true;
expect(appConfigStorageStub.storeList.calledOnce).to.be.true;
2014-10-07 14:32:23 -04:00
done();
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
it('should fail when persisting folders fails', function(done) {
userStorageStub.listItems.yieldsAsync(null, []);
userStorageStub.storeList.yieldsAsync(new Error());
2014-10-07 14:32:23 -04:00
updateHandler.update(function(error) {
expect(error).to.exist;
expect(userStorageStub.listItems.calledOnce).to.be.true;
expect(userStorageStub.storeList.calledOnce).to.be.true;
expect(appConfigStorageStub.storeList.called).to.be.false;
2014-10-07 14:32:23 -04:00
done();
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
it('should fail when listing folders fails', function(done) {
userStorageStub.listItems.yieldsAsync(new Error());
2014-10-07 14:32:23 -04:00
updateHandler.update(function(error) {
expect(error).to.exist;
expect(userStorageStub.listItems.calledOnce).to.be.true;
expect(userStorageStub.storeList.called).to.be.false;
expect(appConfigStorageStub.storeList.called).to.be.false;
2014-10-07 14:32:23 -04:00
done();
});
});
2014-03-11 11:06:19 -04:00
});
});
});