1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-23 09:22:23 -05:00
mail/test/new-unit/app-controller-test.js

196 lines
7.4 KiB
JavaScript
Raw Normal View History

2013-08-20 09:19:13 -04:00
define(function(require) {
'use strict';
2013-08-20 09:19:13 -04:00
var controller = require('js/app-controller'),
EmailDAO = require('js/dao/email-dao'),
OutboxBO = require('js/bo/outbox'),
2013-09-26 11:17:47 -04:00
DeviceStorageDAO = require('js/dao/devicestorage-dao'),
2014-03-11 12:49:47 -04:00
UpdateHandler = require('js/util/update/update-handler'),
Auth = require('js/bo/auth'),
2013-08-20 09:19:13 -04:00
expect = chai.expect;
describe('App Controller unit tests', function() {
var emailDaoStub, outboxStub, updateHandlerStub, appConfigStoreStub, devicestorageStub, isOnlineStub, authStub;
2013-08-20 09:19:13 -04:00
beforeEach(function() {
2014-03-11 12:49:47 -04:00
controller._emailDao = emailDaoStub = sinon.createStubInstance(EmailDAO);
controller._outboxBo = outboxStub = sinon.createStubInstance(OutboxBO);
controller._appConfigStore = appConfigStoreStub = sinon.createStubInstance(DeviceStorageDAO);
2014-03-11 13:15:33 -04:00
controller._userStorage = devicestorageStub = sinon.createStubInstance(DeviceStorageDAO);
2014-03-11 12:49:47 -04:00
controller._updateHandler = updateHandlerStub = sinon.createStubInstance(UpdateHandler);
controller._auth = authStub = sinon.createStubInstance(Auth);
isOnlineStub = sinon.stub(controller, 'isOnline');
2013-08-20 09:19:13 -04:00
});
afterEach(function() {
isOnlineStub.restore();
2013-08-20 09:19:13 -04:00
});
describe('buildModules', function() {
it('should work', function() {
controller.buildModules();
expect(controller._appConfigStore).to.exist;
expect(controller._auth).to.exist;
expect(controller._userStorage).to.exist;
expect(controller._invitationDao).to.exist;
expect(controller._keychain).to.exist;
expect(controller._crypto).to.exist;
expect(controller._pgpbuilder).to.exist;
expect(controller._emailDao).to.exist;
expect(controller._outboxBo).to.exist;
expect(controller._updateHandler).to.exist;
});
});
2013-08-20 09:19:13 -04:00
describe('start', function() {
it('should not explode', function(done) {
controller.start({
onError: function() {}
}, function(err) {
2013-08-20 09:19:13 -04:00
expect(err).to.not.exist;
done();
});
});
});
describe('onDisconnect', function() {
it('should work', function(done) {
emailDaoStub.onDisconnect.yields();
controller.onDisconnect(function(err) {
expect(err).to.not.exist;
done();
});
});
});
describe('onConnect', function() {
beforeEach(function() {
controller._emailDao._account = {};
});
it('should not connect if offline', function(done) {
isOnlineStub.returns(false);
controller.onConnect(function(err) {
expect(err).to.not.exist;
done();
});
});
it('should not connect if account is not initialized', function(done) {
controller._emailDao._account = null;
controller.onConnect(function(err) {
expect(err).to.not.exist;
done();
});
});
it('should fail due to error in auth.getCredentials', function(done) {
isOnlineStub.returns(true);
authStub.getCredentials.withArgs({}).yields(new Error());
controller.onConnect(function(err) {
expect(err).to.exist;
expect(authStub.getCredentials.calledOnce).to.be.true;
done();
});
});
it('should work', function(done) {
isOnlineStub.returns(true);
authStub.getCredentials.withArgs({}).yields(null, {
emailAddress: 'asdf@example.com',
oauthToken: 'token',
sslCert: 'cert'
});
emailDaoStub.onConnect.yields();
controller.onConnect(function(err) {
expect(err).to.not.exist;
expect(authStub.getCredentials.calledOnce).to.be.true;
expect(emailDaoStub.onConnect.calledOnce).to.be.true;
done();
});
});
});
describe('init', function() {
var onConnectStub, emailAddress;
beforeEach(function() {
2014-03-11 12:49:47 -04:00
emailAddress = 'alice@bob.com';
// onConnect
onConnectStub = sinon.stub(controller, 'onConnect');
});
afterEach(function() {
onConnectStub.restore();
});
it('should fail due to error in storage initialization', function(done) {
devicestorageStub.init.withArgs(undefined).yields({});
controller.init({}, function(err, keypair) {
expect(err).to.exist;
expect(keypair).to.not.exist;
expect(devicestorageStub.init.calledOnce).to.be.true;
expect(updateHandlerStub.update.calledOnce).to.be.false;
done();
});
});
2014-03-11 12:49:47 -04:00
it('should fail due to error in update handler', function(done) {
devicestorageStub.init.yields();
updateHandlerStub.update.yields({});
controller.init({
emailAddress: emailAddress
}, function(err, keypair) {
2014-03-11 12:49:47 -04:00
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) {
2014-03-11 12:49:47 -04:00
devicestorageStub.init.yields();
updateHandlerStub.update.yields();
emailDaoStub.init.yields({});
controller.init({
emailAddress: emailAddress
}, function(err, keypair) {
expect(err).to.exist;
expect(keypair).to.not.exist;
2014-03-11 12:49:47 -04:00
expect(updateHandlerStub.update.calledOnce).to.be.true;
expect(emailDaoStub.init.calledOnce).to.be.true;
expect(devicestorageStub.init.calledOnce).to.be.true;
done();
});
});
it('should work and return a keypair', function(done) {
2014-03-11 12:49:47 -04:00
devicestorageStub.init.withArgs(emailAddress).yields();
emailDaoStub.init.yields(null, {});
2014-03-11 12:49:47 -04:00
updateHandlerStub.update.yields();
2014-03-11 12:49:47 -04:00
controller.init({
emailAddress: emailAddress
}, function(err, keypair) {
expect(err).to.not.exist;
expect(keypair).to.exist;
2014-03-11 12:49:47 -04:00
expect(updateHandlerStub.update.calledOnce).to.be.true;
expect(emailDaoStub.init.calledOnce).to.be.true;
expect(devicestorageStub.init.calledOnce).to.be.true;
done();
});
});
});
});
});