1
0
mirror of https://github.com/moparisthebest/mail synced 2024-12-01 13:22:16 -05:00
mail/test/unit/util/update-handler-test.js

472 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-11-21 07:33:22 -05:00
var DeviceStorageDAO = require('../../../src/js/service/devicestorage'),
Auth = require('../../../src/js/service/auth'),
cfg = require('../../../src/js/app-config').config,
UpdateHandler = require('../../../src/js/util/update/update-handler'),
Dialog = require('../../../src/js/util/dialog');
2014-03-11 11:06:19 -04:00
2014-10-07 14:32:23 -04:00
describe('UpdateHandler', function() {
2014-11-21 07:33:22 -05:00
var updateHandler, appConfigStorageStub, authStub, userStorageStub, dialogStub, origDbVersion;
2014-03-11 11:06:19 -04:00
2014-11-05 08:27:34 -05:00
chai.config.includeStack = true;
2014-10-07 14:32:23 -04:00
beforeEach(function() {
origDbVersion = cfg.dbVersion;
appConfigStorageStub = sinon.createStubInstance(DeviceStorageDAO);
userStorageStub = sinon.createStubInstance(DeviceStorageDAO);
authStub = sinon.createStubInstance(Auth);
2014-11-21 07:33:22 -05:00
dialogStub = sinon.createStubInstance(Dialog);
2014-12-11 12:12:37 -05:00
updateHandler = new UpdateHandler(appConfigStorageStub, userStorageStub, authStub, dialogStub);
2014-10-07 14:32:23 -04:00
});
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
2014-12-11 07:57:40 -05:00
appConfigStorageStub.listItems.withArgs(versionDbType).returns(resolves(['10'])); // database version is 10
2014-03-11 11:06:19 -04:00
2014-12-11 07:57:40 -05:00
updateHandler.update().then(function() {
2014-10-07 14:32:23 -04:00
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;
2014-12-11 07:57:40 -05:00
appConfigStorageStub.listItems.withArgs(versionDbType).returns(resolves(['2'])); // database version is 0
2014-10-07 14:32:23 -04:00
});
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
2014-12-11 07:57:40 -05:00
function dummyUpdate() {
2014-10-07 14:32:23 -04:00
updateCounter++;
2014-12-11 07:57:40 -05:00
return resolves();
2014-10-07 14:32:23 -04:00
}
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
2014-12-11 07:57:40 -05:00
updateHandler.update().then(function() {
2014-10-07 14:32:23 -04:00
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-12-11 07:57:40 -05:00
function dummyUpdate() {
2014-10-07 14:32:23 -04:00
updateCounter++;
2014-12-11 07:57:40 -05:00
return resolves();
2014-10-07 14:32:23 -04:00
}
2014-03-11 11:06:19 -04:00
2014-12-11 07:57:40 -05:00
function failingUpdate() {
return rejects({});
2014-10-07 14:32:23 -04:00
}
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
2014-12-11 07:57:40 -05:00
updateHandler.update().catch(function(error) {
2014-10-07 14:32:23 -04:00
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
2014-12-11 07:57:40 -05:00
appConfigStorageStub.listItems.withArgs(versionDbType).returns(resolves()); // database version is 0
2014-10-07 14:32:23 -04:00
});
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) {
2014-12-11 07:57:40 -05:00
userStorageStub.removeList.withArgs(emailDbType).returns(resolves());
appConfigStorageStub.storeList.withArgs([1], versionDbType).returns(resolves());
2014-10-07 14:32:23 -04:00
2014-12-11 07:57:40 -05:00
updateHandler.update().then(function() {
2014-10-07 14:32:23 -04:00
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) {
2014-12-11 07:57:40 -05:00
userStorageStub.removeList.returns(resolves());
appConfigStorageStub.storeList.returns(rejects(new Error()));
2014-12-11 07:57:40 -05:00
updateHandler.update().catch(function(error) {
2014-10-07 14:32:23 -04:00
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) {
2014-12-11 07:57:40 -05:00
userStorageStub.removeList.returns(rejects(new Error()));
2014-12-11 07:57:40 -05:00
updateHandler.update().catch(function(error) {
2014-10-07 14:32:23 -04:00
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
2014-12-11 07:57:40 -05:00
appConfigStorageStub.listItems.withArgs(versionDbType).returns(resolves([1])); // database version is 0
2014-10-07 14:32:23 -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-10-07 14:32:23 -04:00
it('should work', function(done) {
2014-12-11 07:57:40 -05:00
userStorageStub.removeList.withArgs(emailDbType).returns(resolves());
appConfigStorageStub.storeList.withArgs([2], versionDbType).returns(resolves());
2014-12-11 07:57:40 -05:00
updateHandler.update().then(function() {
2014-10-07 14:32:23 -04:00
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) {
2014-12-11 07:57:40 -05:00
userStorageStub.removeList.returns(resolves());
appConfigStorageStub.storeList.returns(rejects(new Error()));
2014-12-11 07:57:40 -05:00
updateHandler.update().catch(function(error) {
2014-10-07 14:32:23 -04:00
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) {
2014-12-11 07:57:40 -05:00
userStorageStub.removeList.returns(rejects(new Error()));
2014-12-11 07:57:40 -05:00
updateHandler.update().catch(function(error) {
2014-10-07 14:32:23 -04:00
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
2014-12-11 07:57:40 -05:00
appConfigStorageStub.listItems.withArgs(versionDbType).returns(resolves([2])); // database version is 0
2014-10-07 14:32:23 -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-10-07 14:32:23 -04:00
it('should work', function(done) {
2014-12-11 07:57:40 -05:00
userStorageStub.removeList.withArgs(emailDbType).returns(resolves());
appConfigStorageStub.storeList.withArgs([3], versionDbType).returns(resolves());
2014-12-11 07:57:40 -05:00
updateHandler.update().then(function() {
2014-10-07 14:32:23 -04:00
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) {
2014-12-11 07:57:40 -05:00
userStorageStub.removeList.returns(resolves());
appConfigStorageStub.storeList.returns(rejects(new Error()));
2014-03-11 11:06:19 -04:00
2014-12-11 07:57:40 -05:00
updateHandler.update().catch(function(error) {
2014-10-07 14:32:23 -04:00
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) {
2014-12-11 07:57:40 -05:00
userStorageStub.removeList.returns(rejects(new Error()));
2014-03-11 11:06:19 -04:00
2014-12-11 07:57:40 -05:00
updateHandler.update().catch(function(error) {
2014-10-07 14:32:23 -04:00
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 = {
host: 'imap.gmail.com',
port: 993,
secure: true
},
smtp = {
host: 'smtp.gmail.com',
port: 465,
secure: true
};
2014-10-07 14:32:23 -04:00
beforeEach(function() {
cfg.dbVersion = 4; // app requires database version 4
2014-12-11 07:57:40 -05:00
appConfigStorageStub.listItems.withArgs(versionDbType).returns(resolves([3])); // database version is 3
2014-10-07 14:32:23 -04:00
});
it('should add gmail as mail service provider with email address and no provider present in db', function(done) {
2014-12-11 07:57:40 -05:00
appConfigStorageStub.listItems.withArgs(EMAIL_ADDR_DB_KEY).returns(resolves([emailaddress]));
appConfigStorageStub.listItems.withArgs(PROVIDER_DB_KEY).returns(resolves([]));
appConfigStorageStub.storeList.withArgs([4], versionDbType).returns(resolves());
appConfigStorageStub.storeList.withArgs(['gmail'], PROVIDER_DB_KEY).returns(resolves());
appConfigStorageStub.storeList.withArgs([emailaddress], USERNAME_DB_KEY).returns(resolves());
appConfigStorageStub.storeList.withArgs([imap], IMAP_DB_KEY).returns(resolves());
appConfigStorageStub.storeList.withArgs([smtp], SMTP_DB_KEY).returns(resolves());
appConfigStorageStub.storeList.withArgs([''], REALNAME_DB_KEY).returns(resolves());
authStub._loadCredentials.returns(resolves());
updateHandler.update().then(function() {
2014-10-07 14:32:23 -04:00
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) {
2014-12-11 07:57:40 -05:00
appConfigStorageStub.listItems.withArgs(EMAIL_ADDR_DB_KEY).returns(resolves([]));
appConfigStorageStub.listItems.withArgs(PROVIDER_DB_KEY).returns(resolves([]));
appConfigStorageStub.storeList.withArgs([4], versionDbType).returns(resolves());
2014-12-11 07:57:40 -05:00
updateHandler.update().then(function() {
2014-10-07 14:32:23 -04:00
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) {
2014-12-11 07:57:40 -05:00
appConfigStorageStub.listItems.returns(resolves([]));
appConfigStorageStub.storeList.returns(rejects(new Error()));
2014-12-11 07:57:40 -05:00
updateHandler.update().catch(function(error) {
2014-10-07 14:32:23 -04:00
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) {
2014-12-11 07:57:40 -05:00
appConfigStorageStub.listItems.withArgs(EMAIL_ADDR_DB_KEY).returns(rejects(new Error()));
appConfigStorageStub.storeList.returns(rejects(new Error()));
2014-12-11 07:57:40 -05:00
updateHandler.update().catch(function(error) {
2014-10-07 14:32:23 -04:00
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
2014-12-11 07:57:40 -05:00
appConfigStorageStub.listItems.withArgs(VERSION_DB_TYPE).returns(resolves([4])); // database version is 4
2014-10-07 14:32:23 -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;
});
it('should work', function(done) {
2014-12-11 07:57:40 -05:00
userStorageStub.listItems.withArgs(FOLDER_DB_TYPE).returns(resolves([
2014-10-07 14:32:23 -04:00
[{
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
}]
2014-12-11 07:57:40 -05:00
]));
2014-10-07 14:32:23 -04:00
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
}]
2014-12-11 07:57:40 -05:00
], FOLDER_DB_TYPE).returns(resolves());
2014-10-07 14:32:23 -04:00
2014-12-11 07:57:40 -05:00
appConfigStorageStub.storeList.withArgs([POST_UPDATE_DB_VERSION], VERSION_DB_TYPE).returns(resolves());
2014-12-11 07:57:40 -05:00
updateHandler.update().then(function() {
2014-10-07 14:32:23 -04:00
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) {
2014-12-11 07:57:40 -05:00
userStorageStub.listItems.returns(resolves([]));
userStorageStub.storeList.returns(resolves());
appConfigStorageStub.storeList.returns(rejects(new Error()));
2014-12-11 07:57:40 -05:00
updateHandler.update().catch(function(error) {
2014-10-07 14:32:23 -04:00
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) {
2014-12-11 07:57:40 -05:00
userStorageStub.listItems.returns(resolves([]));
userStorageStub.storeList.returns(rejects(new Error()));
2014-12-11 07:57:40 -05:00
updateHandler.update().catch(function(error) {
2014-10-07 14:32:23 -04:00
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) {
2014-12-11 07:57:40 -05:00
userStorageStub.listItems.returns(rejects(new Error()));
2014-12-11 07:57:40 -05:00
updateHandler.update().catch(function(error) {
2014-10-07 14:32:23 -04:00
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
});
});
});