2013-08-20 09:19:13 -04:00
|
|
|
define(function(require) {
|
2013-08-20 07:30:35 -04:00
|
|
|
'use strict';
|
|
|
|
|
2013-08-20 09:19:13 -04:00
|
|
|
var controller = require('js/app-controller'),
|
|
|
|
EmailDAO = require('js/dao/email-dao'),
|
2013-12-09 13:21:52 -05:00
|
|
|
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'),
|
2013-08-20 09:19:13 -04:00
|
|
|
expect = chai.expect;
|
|
|
|
|
2013-08-20 07:30:35 -04:00
|
|
|
describe('App Controller unit tests', function() {
|
2014-03-11 12:49:47 -04:00
|
|
|
var emailDaoStub, outboxStub, updateHandlerStub, appConfigStoreStub, devicestorageStub, isOnlineStub,
|
2013-12-09 13:21:52 -05:00
|
|
|
identityStub;
|
2013-08-20 07:30:35 -04:00
|
|
|
|
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);
|
2013-12-09 13:21:52 -05:00
|
|
|
|
|
|
|
isOnlineStub = sinon.stub(controller, 'isOnline');
|
2013-08-20 09:19:13 -04:00
|
|
|
|
|
|
|
window.chrome = window.chrome || {};
|
|
|
|
window.chrome.identity = window.chrome.identity || {};
|
|
|
|
if (typeof window.chrome.identity.getAuthToken !== 'function') {
|
|
|
|
window.chrome.identity.getAuthToken = function() {};
|
|
|
|
}
|
2013-12-09 13:21:52 -05:00
|
|
|
identityStub = sinon.stub(window.chrome.identity, 'getAuthToken');
|
2013-08-20 09:19:13 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
2013-12-09 13:21:52 -05:00
|
|
|
identityStub.restore();
|
|
|
|
isOnlineStub.restore();
|
2013-08-20 09:19:13 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('start', function() {
|
|
|
|
it('should not explode', function(done) {
|
2013-12-09 13:21:52 -05:00
|
|
|
controller.start({
|
|
|
|
onError: function() {}
|
|
|
|
}, function(err) {
|
2013-08-20 09:19:13 -04:00
|
|
|
expect(err).to.not.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-08-20 07:30:35 -04:00
|
|
|
|
2013-12-09 13:21:52 -05:00
|
|
|
describe('onDisconnect', function() {
|
|
|
|
it('should work', function(done) {
|
|
|
|
emailDaoStub.onDisconnect.yields();
|
|
|
|
|
|
|
|
controller.onDisconnect(function(err) {
|
|
|
|
expect(err).to.not.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('onConnect', function() {
|
|
|
|
var fetchOAuthTokenStub, getCertficateStub;
|
|
|
|
|
2013-10-09 10:40:36 -04:00
|
|
|
beforeEach(function() {
|
2013-12-09 13:21:52 -05:00
|
|
|
// buildModules
|
|
|
|
fetchOAuthTokenStub = sinon.stub(controller, 'fetchOAuthToken');
|
|
|
|
getCertficateStub = sinon.stub(controller, 'getCertficate');
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
|
|
|
fetchOAuthTokenStub.restore();
|
|
|
|
getCertficateStub.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not connect if offline', function(done) {
|
|
|
|
isOnlineStub.returns(false);
|
|
|
|
|
|
|
|
controller.onConnect(function(err) {
|
|
|
|
expect(err).to.not.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail due to error in certificate', function(done) {
|
|
|
|
isOnlineStub.returns(true);
|
|
|
|
getCertficateStub.yields({});
|
|
|
|
|
|
|
|
controller.onConnect(function(err) {
|
|
|
|
expect(err).to.exist;
|
|
|
|
expect(getCertficateStub.calledOnce).to.be.true;
|
|
|
|
done();
|
|
|
|
});
|
2013-10-09 10:40:36 -04:00
|
|
|
});
|
|
|
|
|
2013-12-09 13:21:52 -05:00
|
|
|
it('should fail due to error in fetch oauth', function(done) {
|
|
|
|
isOnlineStub.returns(true);
|
|
|
|
getCertficateStub.yields(null, 'PEM');
|
|
|
|
fetchOAuthTokenStub.yields({});
|
|
|
|
|
|
|
|
controller.onConnect(function(err) {
|
|
|
|
expect(err).to.exist;
|
|
|
|
expect(fetchOAuthTokenStub.calledOnce).to.be.true;
|
|
|
|
expect(getCertficateStub.calledOnce).to.be.true;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work', function(done) {
|
|
|
|
isOnlineStub.returns(true);
|
|
|
|
fetchOAuthTokenStub.yields(null, {
|
|
|
|
emailAddress: 'asfd@example.com'
|
|
|
|
});
|
|
|
|
getCertficateStub.yields(null, 'PEM');
|
|
|
|
emailDaoStub.onConnect.yields();
|
|
|
|
|
|
|
|
controller.onConnect(function(err) {
|
|
|
|
expect(err).to.not.exist;
|
|
|
|
expect(fetchOAuthTokenStub.calledOnce).to.be.true;
|
|
|
|
expect(getCertficateStub.calledOnce).to.be.true;
|
|
|
|
expect(emailDaoStub.onConnect.calledOnce).to.be.true;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getEmailAddress', function() {
|
|
|
|
var fetchOAuthTokenStub;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
// buildModules
|
|
|
|
fetchOAuthTokenStub = sinon.stub(controller, 'fetchOAuthToken');
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
|
|
|
fetchOAuthTokenStub.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail due to error in config list items', function(done) {
|
|
|
|
appConfigStoreStub.listItems.yields({});
|
|
|
|
|
|
|
|
controller.getEmailAddress(function(err, emailAddress) {
|
|
|
|
expect(err).to.exist;
|
|
|
|
expect(emailAddress).to.not.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work if address is already cached', function(done) {
|
|
|
|
appConfigStoreStub.listItems.yields(null, ['asdf']);
|
|
|
|
|
|
|
|
controller.getEmailAddress(function(err, emailAddress) {
|
|
|
|
expect(err).to.not.exist;
|
|
|
|
expect(emailAddress).to.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail first time if app is offline', function(done) {
|
|
|
|
appConfigStoreStub.listItems.yields(null, []);
|
|
|
|
isOnlineStub.returns(false);
|
|
|
|
|
|
|
|
controller.getEmailAddress(function(err, emailAddress) {
|
|
|
|
expect(err).to.exist;
|
|
|
|
expect(emailAddress).to.not.exist;
|
|
|
|
expect(isOnlineStub.calledOnce).to.be.true;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('fetchOAuthToken', function() {
|
2014-02-28 14:14:32 -05:00
|
|
|
var queryEmailAddressStub;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
// buildModules
|
|
|
|
queryEmailAddressStub = sinon.stub(controller, 'queryEmailAddress');
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
|
|
|
queryEmailAddressStub.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work', function(done) {
|
2013-12-09 13:21:52 -05:00
|
|
|
identityStub.yields('token42');
|
2014-02-28 14:14:32 -05:00
|
|
|
queryEmailAddressStub.yields(null, 'bob@asdf.com');
|
2013-09-26 11:17:47 -04:00
|
|
|
|
2014-02-28 14:14:32 -05:00
|
|
|
controller.fetchOAuthToken(function(err, res) {
|
2013-10-09 10:40:36 -04:00
|
|
|
expect(err).to.not.exist;
|
2014-02-28 14:14:32 -05:00
|
|
|
expect(res.emailAddress).to.equal('bob@asdf.com');
|
|
|
|
expect(res.token).to.equal('token42');
|
|
|
|
expect(queryEmailAddressStub.calledOnce).to.be.true;
|
2013-12-09 13:21:52 -05:00
|
|
|
expect(identityStub.calledOnce).to.be.true;
|
2013-10-09 10:40:36 -04:00
|
|
|
done();
|
2013-09-26 11:17:47 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-02-28 14:14:32 -05:00
|
|
|
it('should fail due to chrome api error', function(done) {
|
|
|
|
identityStub.yields();
|
|
|
|
|
|
|
|
controller.fetchOAuthToken(function(err) {
|
|
|
|
expect(err).to.exist;
|
|
|
|
expect(identityStub.calledOnce).to.be.true;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail due error querying email address', function(done) {
|
2013-12-09 13:21:52 -05:00
|
|
|
identityStub.yields('token42');
|
2014-02-28 14:14:32 -05:00
|
|
|
queryEmailAddressStub.yields();
|
2013-10-09 10:40:36 -04:00
|
|
|
|
2013-10-21 07:10:42 -04:00
|
|
|
controller.fetchOAuthToken(function(err) {
|
2014-02-28 14:14:32 -05:00
|
|
|
expect(err).to.exist;
|
|
|
|
expect(queryEmailAddressStub.calledOnce).to.be.true;
|
2013-12-09 13:21:52 -05:00
|
|
|
expect(identityStub.calledOnce).to.be.true;
|
2013-09-04 15:01:32 -04:00
|
|
|
done();
|
2013-08-21 07:43:19 -04:00
|
|
|
});
|
|
|
|
});
|
2013-08-20 07:30:35 -04:00
|
|
|
});
|
|
|
|
|
2013-12-09 13:21:52 -05:00
|
|
|
describe('buildModules', function() {
|
|
|
|
it('should work', function() {
|
|
|
|
controller.buildModules();
|
2014-03-11 13:57:03 -04:00
|
|
|
expect(controller._userStorage).to.exist;
|
|
|
|
expect(controller._invitationDao).to.exist;
|
2013-12-09 13:21:52 -05:00
|
|
|
expect(controller._keychain).to.exist;
|
|
|
|
expect(controller._crypto).to.exist;
|
2014-03-11 13:57:03 -04:00
|
|
|
expect(controller._pgpbuilder).to.exist;
|
2013-12-09 13:21:52 -05:00
|
|
|
expect(controller._emailDao).to.exist;
|
|
|
|
expect(controller._outboxBo).to.exist;
|
2014-03-11 13:57:03 -04:00
|
|
|
expect(controller._updateHandler).to.exist;
|
2013-12-09 13:21:52 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('init', function() {
|
2014-03-11 12:49:47 -04:00
|
|
|
var buildModulesStub, onConnectStub, emailAddress;
|
2013-12-09 13:21:52 -05:00
|
|
|
|
|
|
|
beforeEach(function() {
|
2014-03-11 12:49:47 -04:00
|
|
|
emailAddress = 'alice@bob.com';
|
|
|
|
|
2013-12-09 13:21:52 -05:00
|
|
|
// buildModules
|
|
|
|
buildModulesStub = sinon.stub(controller, 'buildModules');
|
|
|
|
buildModulesStub.returns();
|
|
|
|
// onConnect
|
|
|
|
onConnectStub = sinon.stub(controller, 'onConnect');
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
|
|
|
buildModulesStub.restore();
|
|
|
|
onConnectStub.restore();
|
|
|
|
});
|
|
|
|
|
2014-03-11 13:27:02 -04:00
|
|
|
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({});
|
|
|
|
|
2014-03-11 13:27:02 -04:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-12-09 13:21:52 -05:00
|
|
|
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();
|
2013-12-09 13:21:52 -05:00
|
|
|
emailDaoStub.init.yields({});
|
|
|
|
|
2014-03-11 13:27:02 -04:00
|
|
|
controller.init({
|
|
|
|
emailAddress: emailAddress
|
|
|
|
}, function(err, keypair) {
|
2013-12-09 13:21:52 -05:00
|
|
|
expect(err).to.exist;
|
2013-12-10 17:05:17 -05:00
|
|
|
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;
|
2013-12-09 13:21:52 -05:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail due to error in onConnect', function(done) {
|
2014-03-11 12:49:47 -04:00
|
|
|
devicestorageStub.init.yields();
|
|
|
|
updateHandlerStub.update.yields();
|
2013-12-09 13:21:52 -05:00
|
|
|
emailDaoStub.init.yields();
|
|
|
|
|
|
|
|
onConnectStub.yields({});
|
|
|
|
|
2014-03-11 13:27:02 -04:00
|
|
|
controller.init({
|
|
|
|
emailAddress: emailAddress
|
|
|
|
}, function(err) {
|
2013-12-09 13:21:52 -05:00
|
|
|
expect(err).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;
|
2013-12-09 13:21:52 -05:00
|
|
|
expect(onConnectStub.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();
|
2013-12-09 13:21:52 -05:00
|
|
|
emailDaoStub.init.yields(null, {});
|
2014-03-11 12:49:47 -04:00
|
|
|
updateHandlerStub.update.yields();
|
2013-12-09 13:21:52 -05:00
|
|
|
|
|
|
|
onConnectStub.yields();
|
|
|
|
|
2014-03-11 12:49:47 -04:00
|
|
|
controller.init({
|
|
|
|
emailAddress: emailAddress
|
|
|
|
}, function(err, keypair) {
|
2013-12-09 13:21:52 -05:00
|
|
|
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;
|
2013-12-09 13:21:52 -05:00
|
|
|
expect(onConnectStub.calledOnce).to.be.true;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-08-20 07:30:35 -04:00
|
|
|
});
|
|
|
|
});
|