1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-25 18:32:20 -05:00

Fix tests

This commit is contained in:
Tankred Hase 2014-11-10 18:36:26 +01:00
parent 1bfdafd6b1
commit 11d7a26c31
6 changed files with 315 additions and 132 deletions

View File

@ -171,6 +171,8 @@ module.exports = function(grunt) {
'test/unit/devicestorage-dao-test.js', 'test/unit/devicestorage-dao-test.js',
'test/unit/dialog-ctrl-test.js', 'test/unit/dialog-ctrl-test.js',
'test/unit/add-account-ctrl-test.js', 'test/unit/add-account-ctrl-test.js',
'test/unit/create-account-ctrl-test.js',
'test/unit/validate-phone-ctrl-test.js',
'test/unit/account-ctrl-test.js', 'test/unit/account-ctrl-test.js',
'test/unit/set-passphrase-ctrl-test.js', 'test/unit/set-passphrase-ctrl-test.js',
'test/unit/contacts-ctrl-test.js', 'test/unit/contacts-ctrl-test.js',

View File

@ -36,10 +36,10 @@ var AddAccountCtrl = function($scope, $location, $routeParams, $http) {
$scope.connectToGoogle(); $scope.connectToGoogle();
} else if (config.imap.source === 'guess') { } else if (config.imap.source === 'guess') {
// use standard password login... show config details due to guess // use standard password login... show config details due to guess
setCredentials('custom'); $scope.setCredentials('custom');
} else { } else {
// use standard password login... hide config details // use standard password login... hide config details
setCredentials(); $scope.setCredentials();
} }
}).catch(function() { }).catch(function() {
@ -61,44 +61,38 @@ var AddAccountCtrl = function($scope, $location, $routeParams, $http) {
faqLink: 'https://github.com/whiteout-io/mail-html5/wiki/FAQ#how-does-sign-in-with-google-work', faqLink: 'https://github.com/whiteout-io/mail-html5/wiki/FAQ#how-does-sign-in-with-google-work',
callback: function(granted) { callback: function(granted) {
if (granted) { if (granted) {
// query oauth token
useOAuth(); useOAuth();
} else { } else {
setGmailPassword(); // use normal user/password login
$scope.setCredentials('gmail');
$scope.$apply(); $scope.$apply();
} }
} }
}); });
} else { } else {
// no oauth support // no oauth support
setGmailPassword(); // use normal user/password login
$scope.setCredentials('gmail');
}
function useOAuth() {
// fetches the email address from the chrome identity api
appCtrl._auth.getOAuthToken(function(err) {
if (err) {
return $scope.onError(err);
}
$scope.setCredentials('gmail');
$scope.$apply();
});
} }
}; };
// $scope.setCredentials = function(provider) {
// Helper functions
//
function useOAuth() {
// fetches the email address from the chrome identity api
appCtrl._auth.getOAuthToken(function(err) {
if (err) {
return $scope.onError(err);
}
setCredentials('gmail');
$scope.$apply();
});
}
function setGmailPassword() {
// use normal user/password login
setCredentials('gmail');
}
function setCredentials(provider) {
$location.path('/login-set-credentials').search({ $location.path('/login-set-credentials').search({
provider: provider provider: provider
}); });
} };
}; };
module.exports = AddAccountCtrl; module.exports = AddAccountCtrl;

View File

@ -4,8 +4,7 @@ var ENCRYPTION_METHOD_NONE = 0;
var ENCRYPTION_METHOD_STARTTLS = 1; var ENCRYPTION_METHOD_STARTTLS = 1;
var ENCRYPTION_METHOD_TLS = 2; var ENCRYPTION_METHOD_TLS = 2;
var appCtrl = require('../app-controller'), var appCtrl = require('../app-controller');
config = require('../app-config').config;
var SetCredentialsCtrl = function($scope, $location, $routeParams) { var SetCredentialsCtrl = function($scope, $location, $routeParams) {
if (!appCtrl._auth && !$routeParams.dev) { if (!appCtrl._auth && !$routeParams.dev) {

View File

@ -3,22 +3,22 @@
var mocks = angular.mock, var mocks = angular.mock,
AddAccountCtrl = require('../../src/js/controller/add-account'), AddAccountCtrl = require('../../src/js/controller/add-account'),
Auth = require('../../src/js/bo/auth'), Auth = require('../../src/js/bo/auth'),
AdminDao = require('../../src/js/dao/admin-dao'), appController = require('../../src/js/app-controller'),
appController = require('../../src/js/app-controller'); cfg = require('../../src/js/app-config').config;
describe('Add Account Controller unit test', function() { describe('Add Account Controller unit test', function() {
var scope, location, ctrl, authStub, origAuth, adminStub; var scope, location, httpBackend, ctrl, authStub, origAuth;
beforeEach(function() { beforeEach(function() {
// remember original module to restore later, then replace it // remember original module to restore later, then replace it
origAuth = appController._auth; origAuth = appController._auth;
appController._auth = authStub = sinon.createStubInstance(Auth); appController._auth = authStub = sinon.createStubInstance(Auth);
appController._adminDao = adminStub = sinon.createStubInstance(AdminDao);
angular.module('addaccounttest', []); angular.module('addaccounttest', []);
mocks.module('addaccounttest'); mocks.module('addaccounttest');
mocks.inject(function($controller, $rootScope, $location) { mocks.inject(function($controller, $rootScope, $location, $httpBackend) {
location = $location; location = $location;
httpBackend = $httpBackend;
scope = $rootScope.$new(); scope = $rootScope.$new();
scope.state = {}; scope.state = {};
scope.form = {}; scope.form = {};
@ -45,122 +45,117 @@ describe('Add Account Controller unit test', function() {
if (scope.$apply.restore) { if (scope.$apply.restore) {
scope.$apply.restore(); scope.$apply.restore();
} }
httpBackend.verifyNoOutstandingExpectation();
httpBackend.verifyNoOutstandingRequest();
}); });
describe('createWhiteoutAccount', function() { describe('getAccountSettings', function() {
it('should return early for invalid form', function() { var url, connectToGoogleStub, setCredentialsStub, mailConfig;
scope.form.$invalid = true;
scope.createWhiteoutAccount();
expect(adminStub.createUser.called).to.be.false;
});
it('should fail to error creating user', function(done) { beforeEach(function() {
scope.form.$invalid = false; scope.form.$invalid = false;
scope.betaCode = 'asfd'; scope.emailAddress = 'test@example.com';
scope.phone = '12345'; url = cfg.settingsUrl + 'example.com';
adminStub.createUser.yieldsAsync(new Error('asdf')); mailConfig = {
imap: {
scope.$apply = function() { hostname: 'imap.example.com',
expect(scope.busy).to.be.false; source: 'guess'
expect(scope.errMsg).to.equal('asdf'); }
expect(adminStub.createUser.calledOnce).to.be.true;
done();
}; };
connectToGoogleStub = sinon.stub(scope, 'connectToGoogle');
scope.createWhiteoutAccount(); setCredentialsStub = sinon.stub(scope, 'setCredentials');
expect(scope.busy).to.be.true;
}); });
it('should work', function(done) { afterEach(function() {
scope.form.$invalid = false; connectToGoogleStub.restore();
scope.betaCode = 'asfd'; setCredentialsStub.restore();
scope.phone = '12345';
adminStub.createUser.yieldsAsync();
scope.$apply = function() {
expect(scope.busy).to.be.false;
expect(scope.errMsg).to.be.undefined;
expect(scope.step).to.equal(3);
expect(adminStub.createUser.calledOnce).to.be.true;
done();
};
scope.createWhiteoutAccount();
expect(scope.busy).to.be.true;
});
});
describe('validateUser', function() {
it('should return early for invalid form', function() {
scope.formValidate.$invalid = true;
scope.validateUser();
expect(adminStub.validateUser.called).to.be.false;
}); });
it('should fail to error creating user', function(done) { it('should work for gmail', function() {
scope.formValidate.$invalid = false; mailConfig.imap.hostname = 'imap.gmail.com';
scope.token = 'asfd'; httpBackend.expectGET(url).respond(mailConfig);
adminStub.validateUser.yieldsAsync(new Error('asdf'));
scope.$apply = function() { scope.getAccountSettings();
expect(scope.busyValidate).to.be.false; httpBackend.flush();
expect(scope.errMsgValidate).to.equal('asdf');
expect(adminStub.validateUser.calledOnce).to.be.true;
done();
};
scope.validateUser(); expect(connectToGoogleStub.calledOnce).to.be.true;
expect(scope.busyValidate).to.be.true;
}); });
it('should work', function(done) { it('should work for guessed domain', function() {
scope.formValidate.$invalid = false; httpBackend.expectGET(url).respond(mailConfig);
scope.token = 'asfd';
adminStub.validateUser.yieldsAsync();
scope.login = function() { scope.getAccountSettings();
expect(scope.busyValidate).to.be.true; httpBackend.flush();
expect(scope.errMsgValidate).to.be.undefined;
expect(adminStub.validateUser.calledOnce).to.be.true;
done();
};
scope.validateUser(); expect(setCredentialsStub.calledWith('custom')).to.be.true;
expect(scope.busyValidate).to.be.true;
}); });
});
describe('login', function() { it('should work for dns domain', function() {
it('should work', function() { mailConfig.imap.source = 'dns';
scope.form.$invalid = false; httpBackend.expectGET(url).respond(mailConfig);
authStub.setCredentials.returns();
scope.login(); scope.getAccountSettings();
expect(authStub.setCredentials.calledOnce).to.be.true; httpBackend.flush();
expect(location.path.calledWith('/login')).to.be.true;
expect(setCredentialsStub.calledWith(undefined)).to.be.true;
});
it('should fail with http 500', function() {
httpBackend.expectGET(url).respond(500, '');
scope.getAccountSettings();
httpBackend.flush();
expect(scope.errMsg).to.exist;
}); });
}); });
describe('connectToGoogle', function() { describe('connectToGoogle', function() {
it('should forward to login', function() { var setCredentialsStub;
beforeEach(function() {
setCredentialsStub = sinon.stub(scope, 'setCredentials');
});
afterEach(function() {
setCredentialsStub.restore();
});
it('should use oauth', function() {
authStub._oauth = { authStub._oauth = {
isSupported: function() { isSupported: function() {
return true; return true;
} }
}; };
scope.onError = function(options) {
options.callback(true);
};
authStub.getOAuthToken.yields(); authStub.getOAuthToken.yields();
scope.connectToGoogle(); scope.connectToGoogle();
expect(location.path.calledWith('/login-set-credentials')).to.be.true; expect(setCredentialsStub.calledWith('gmail')).to.be.true;
expect(location.search.calledWith({
provider: 'gmail'
})).to.be.true;
expect(authStub.getOAuthToken.calledOnce).to.be.true; expect(authStub.getOAuthToken.calledOnce).to.be.true;
}); });
it('should not use oauth for gmail', function() { it('should not use oauth', function() {
authStub._oauth = {
isSupported: function() {
return true;
}
};
scope.onError = function(options) {
options.callback(false);
};
scope.connectToGoogle();
expect(setCredentialsStub.calledWith('gmail')).to.be.true;
expect(authStub.getOAuthToken.called).to.be.false;
});
it('should not use oauth if not supported', function() {
authStub._oauth = { authStub._oauth = {
isSupported: function() { isSupported: function() {
return false; return false;
@ -169,10 +164,7 @@ describe('Add Account Controller unit test', function() {
scope.connectToGoogle(); scope.connectToGoogle();
expect(location.path.calledWith('/login-set-credentials')).to.be.true; expect(setCredentialsStub.calledWith('gmail')).to.be.true;
expect(location.search.calledWith({
provider: 'gmail'
})).to.be.true;
expect(authStub.getOAuthToken.called).to.be.false; expect(authStub.getOAuthToken.called).to.be.false;
}); });
@ -182,29 +174,28 @@ describe('Add Account Controller unit test', function() {
return true; return true;
} }
}; };
scope.onError = function(options) {
scope.onError = function(err) {
expect(err).to.exist;
expect(setCredentialsStub.called).to.be.false;
done();
};
authStub.getOAuthToken.yields(new Error()); options.callback(true);
scope.onError = function(err) {
expect(err).to.exist;
expect(location.path.called).to.be.false;
expect(location.search.called).to.be.false;
done();
}; };
authStub.getOAuthToken.yields(new Error());
scope.connectToGoogle(); scope.connectToGoogle();
}); });
}); });
describe('connectTo', function() { describe('setCredentials', function() {
it('should forward to login', function() { it('should work', function() {
var provider = 'wmail'; scope.setCredentials('gmail');
scope.connectTo(provider);
expect(location.path.calledWith('/login-set-credentials')).to.be.true; expect(location.path.calledWith('/login-set-credentials')).to.be.true;
expect(location.search.calledWith({ expect(location.search.calledWith({
provider: provider provider: 'gmail'
})).to.be.true; })).to.be.true;
}); });
}); });

View File

@ -0,0 +1,91 @@
'use strict';
var mocks = angular.mock,
CreateAccountCtrl = require('../../src/js/controller/create-account'),
AdminDao = require('../../src/js/dao/admin-dao'),
appController = require('../../src/js/app-controller');
describe('Create Account Controller unit test', function() {
var scope, location, ctrl, authStub, origAuth, adminStub;
beforeEach(function() {
// remember original module to restore later, then replace it
origAuth = appController._auth;
appController._auth = authStub = {};
appController._adminDao = adminStub = sinon.createStubInstance(AdminDao);
angular.module('createaccounttest', []);
mocks.module('createaccounttest');
mocks.inject(function($controller, $rootScope, $location) {
location = $location;
scope = $rootScope.$new();
scope.state = {};
scope.form = {};
scope.formValidate = {};
sinon.stub(location, 'path').returns(location);
sinon.stub(location, 'search').returns(location);
sinon.stub(scope, '$apply', function() {});
ctrl = $controller(CreateAccountCtrl, {
$location: location,
$scope: scope,
$routeParams: {}
});
});
});
afterEach(function() {
// restore the app controller module
appController._auth = origAuth;
location.path.restore();
location.search.restore();
if (scope.$apply.restore) {
scope.$apply.restore();
}
});
describe('createWhiteoutAccount', function() {
it('should return early for invalid form', function() {
scope.form.$invalid = true;
scope.createWhiteoutAccount();
expect(adminStub.createUser.called).to.be.false;
});
it('should fail to error creating user', function(done) {
scope.form.$invalid = false;
scope.betaCode = 'asfd';
scope.phone = '12345';
adminStub.createUser.yieldsAsync(new Error('asdf'));
scope.$apply = function() {
expect(scope.busy).to.be.false;
expect(scope.errMsg).to.equal('asdf');
expect(adminStub.createUser.calledOnce).to.be.true;
done();
};
scope.createWhiteoutAccount();
expect(scope.busy).to.be.true;
});
it('should work', function(done) {
scope.form.$invalid = false;
scope.betaCode = 'asfd';
scope.phone = '12345';
adminStub.createUser.yieldsAsync();
scope.$apply = function() {
expect(scope.busy).to.be.false;
expect(scope.errMsg).to.be.undefined;
expect(adminStub.createUser.calledOnce).to.be.true;
done();
};
scope.createWhiteoutAccount();
expect(scope.busy).to.be.true;
});
});
});

View File

@ -0,0 +1,106 @@
'use strict';
var mocks = angular.mock,
ValidatePhoneCtrl = require('../../src/js/controller/validate-phone'),
Auth = require('../../src/js/bo/auth'),
AdminDao = require('../../src/js/dao/admin-dao'),
appController = require('../../src/js/app-controller');
describe('Validate Phone Controller unit test', function() {
var scope, location, ctrl, authStub, origAuth, adminStub;
beforeEach(function() {
// remember original module to restore later, then replace it
origAuth = appController._auth;
appController._auth = authStub = sinon.createStubInstance(Auth);
appController._adminDao = adminStub = sinon.createStubInstance(AdminDao);
angular.module('validatephonetest', []);
mocks.module('validatephonetest');
mocks.inject(function($controller, $rootScope, $location) {
location = $location;
scope = $rootScope.$new();
scope.state = {
createAccount: {
emailAddress: 'test@example.com',
pass: 'asdf',
realname: 'Test User'
}
};
scope.form = {};
sinon.stub(location, 'path').returns(location);
sinon.stub(location, 'search').returns(location);
sinon.stub(scope, '$apply', function() {});
ctrl = $controller(ValidatePhoneCtrl, {
$location: location,
$scope: scope,
$routeParams: {}
});
});
});
afterEach(function() {
// restore the app controller module
appController._auth = origAuth;
location.path.restore();
location.search.restore();
if (scope.$apply.restore) {
scope.$apply.restore();
}
});
describe('validateUser', function() {
it('should return early for invalid form', function() {
scope.form.$invalid = true;
scope.validateUser();
expect(adminStub.validateUser.called).to.be.false;
});
it('should fail to error creating user', function(done) {
scope.form.$invalid = false;
scope.token = 'asfd';
adminStub.validateUser.yieldsAsync(new Error('asdf'));
scope.$apply = function() {
expect(scope.busy).to.be.false;
expect(scope.errMsg).to.equal('asdf');
expect(adminStub.validateUser.calledOnce).to.be.true;
done();
};
scope.validateUser();
expect(scope.busy).to.be.true;
});
it('should work', function(done) {
scope.form.$invalid = false;
scope.token = 'asfd';
adminStub.validateUser.yieldsAsync();
scope.login = function() {
expect(scope.busy).to.be.true;
expect(scope.errMsg).to.be.undefined;
expect(adminStub.validateUser.calledOnce).to.be.true;
done();
};
scope.validateUser();
expect(scope.busy).to.be.true;
});
});
describe('login', function() {
it('should work', function() {
scope.form.$invalid = false;
authStub.setCredentials.returns();
scope.login();
expect(authStub.setCredentials.calledOnce).to.be.true;
expect(location.path.calledWith('/login')).to.be.true;
});
});
});