1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-17 22:55:11 -05:00
mail/test/unit/controller/login/login-ctrl-test.js

196 lines
6.0 KiB
JavaScript
Raw Normal View History

2014-10-07 14:32:23 -04:00
'use strict';
2014-11-25 12:19:40 -05:00
var LoginCtrl = require('../../../../src/js/controller/login/login'),
Email = require('../../../../src/js/email/email'),
Account = require('../../../../src/js/email/account'),
Dialog = require('../../../../src/js/util/dialog'),
UpdateHandler = require('../../../../src/js/util/update/update-handler'),
Auth = require('../../../../src/js/service/auth'),
Keychain = require('../../../../src/js/service/keychain');
2014-10-07 14:32:23 -04:00
describe('Login Controller unit test', function() {
var scope, location, ctrl,
2014-11-26 13:51:15 -05:00
emailMock, keychainMock, authMock, accountMock, dialogMock, updateHandlerMock, goToStub,
2014-11-25 12:19:40 -05:00
emailAddress = 'fred@foo.com';
beforeEach(function() {
emailMock = sinon.createStubInstance(Email);
accountMock = sinon.createStubInstance(Account);
authMock = sinon.createStubInstance(Auth);
keychainMock = sinon.createStubInstance(Keychain);
dialogMock = sinon.createStubInstance(Dialog);
updateHandlerMock = sinon.createStubInstance(UpdateHandler);
location = {
path: function() {}
};
authMock.emailAddress = emailAddress;
});
function createController() {
angular.module('login-test', ['woServices', 'woEmail', 'woUtil']);
angular.mock.module('login-test');
angular.mock.inject(function($rootScope, $controller) {
scope = $rootScope.$new();
scope.state = {};
scope.form = {};
2014-11-26 13:51:15 -05:00
scope.goTo = function() {};
goToStub = sinon.stub(scope, 'goTo');
2014-11-25 12:19:40 -05:00
ctrl = $controller(LoginCtrl, {
$scope: scope,
$location: location,
updateHandler: updateHandlerMock,
account: accountMock,
auth: authMock,
email: emailMock,
keychain: keychainMock,
dialog: dialogMock
});
2014-10-07 14:32:23 -04:00
});
2014-11-25 12:19:40 -05:00
}
afterEach(function() {});
it('should fail for auth.getEmailAddress', function() {
authMock.init.yields();
2014-11-25 12:19:40 -05:00
authMock.getEmailAddress.yields(new Error());
createController();
expect(updateHandlerMock.checkForUpdate.calledOnce).to.be.true;
expect(authMock.init.calledOnce).to.be.true;
expect(dialogMock.error.calledOnce).to.be.true;
});
it('should fail for auth.init', function() {
authMock.init.yields(new Error());
2014-11-25 12:19:40 -05:00
authMock.getEmailAddress.yields(null, {
emailAddress: emailAddress
2014-10-07 14:32:23 -04:00
});
2014-11-25 12:19:40 -05:00
createController();
2014-03-31 15:35:40 -04:00
expect(authMock.init.calledOnce).to.be.true;
expect(accountMock.init.called).to.be.false;
2014-11-25 12:19:40 -05:00
expect(dialogMock.error.calledOnce).to.be.true;
});
it('should redirect to /add-account', function() {
authMock.init.yields();
authMock.getEmailAddress.yields(null, {});
createController();
expect(goToStub.withArgs('/add-account').calledOnce).to.be.true;
});
2014-11-25 12:19:40 -05:00
it('should redirect to /login-existing', function() {
authMock.init.yields();
2014-11-25 12:19:40 -05:00
authMock.getEmailAddress.yields(null, {
emailAddress: emailAddress
2014-10-07 14:32:23 -04:00
});
2014-11-25 12:19:40 -05:00
accountMock.init.yields(null, {
publicKey: 'publicKey',
privateKey: 'privateKey'
});
emailMock.unlock.yields(new Error());
2014-03-31 15:35:40 -04:00
2014-11-25 12:19:40 -05:00
createController();
2014-03-31 15:35:40 -04:00
2014-11-26 13:51:15 -05:00
expect(goToStub.withArgs('/login-existing').calledOnce).to.be.true;
2014-11-25 12:19:40 -05:00
});
it('should fail for auth.storeCredentials', function() {
authMock.init.yields();
2014-11-25 12:19:40 -05:00
authMock.getEmailAddress.yields(null, {
emailAddress: emailAddress
2014-10-07 14:32:23 -04:00
});
2014-11-25 12:19:40 -05:00
accountMock.init.yields(null, {
publicKey: 'publicKey',
privateKey: 'privateKey'
});
emailMock.unlock.yields();
authMock.storeCredentials.yields(new Error());
2014-11-25 12:19:40 -05:00
createController();
expect(dialogMock.error.calledOnce).to.be.true;
});
it('should redirect to /account', function() {
authMock.init.yields();
2014-11-25 12:19:40 -05:00
authMock.getEmailAddress.yields(null, {
emailAddress: emailAddress
2014-10-07 14:32:23 -04:00
});
2014-11-25 12:19:40 -05:00
accountMock.init.yields(null, {
publicKey: 'publicKey',
privateKey: 'privateKey'
});
emailMock.unlock.yields();
authMock.storeCredentials.yields();
2014-11-25 12:19:40 -05:00
createController();
expect(goToStub.withArgs('/account').calledOnce).to.be.true;
2014-11-25 12:19:40 -05:00
});
it('should fail for keychain.requestPrivateKeyDownload', function() {
authMock.init.yields();
2014-11-25 12:19:40 -05:00
authMock.getEmailAddress.yields(null, {
emailAddress: emailAddress
2014-10-07 14:32:23 -04:00
});
2014-11-25 12:19:40 -05:00
accountMock.init.yields(null, {
publicKey: 'publicKey'
});
keychainMock.requestPrivateKeyDownload.yields(new Error());
2014-11-25 12:19:40 -05:00
createController();
expect(dialogMock.error.calledOnce).to.be.true;
});
it('should redirect to /login-privatekey-download', function() {
authMock.init.yields();
2014-11-25 12:19:40 -05:00
authMock.getEmailAddress.yields(null, {
emailAddress: emailAddress
});
accountMock.init.yields(null, {
publicKey: 'publicKey'
});
2014-11-25 12:19:40 -05:00
keychainMock.requestPrivateKeyDownload.yields(null, true);
createController();
2014-11-26 13:51:15 -05:00
expect(goToStub.withArgs('/login-privatekey-download').calledOnce).to.be.true;
2014-11-25 12:19:40 -05:00
});
it('should redirect to /login-new-device', function() {
authMock.init.yields();
2014-11-25 12:19:40 -05:00
authMock.getEmailAddress.yields(null, {
emailAddress: emailAddress
});
accountMock.init.yields(null, {
publicKey: 'publicKey'
});
keychainMock.requestPrivateKeyDownload.yields();
createController();
2014-11-26 13:51:15 -05:00
expect(goToStub.withArgs('/login-new-device').calledOnce).to.be.true;
});
2014-11-25 12:19:40 -05:00
it('should redirect to /login-initial', function() {
authMock.init.yields();
2014-11-25 12:19:40 -05:00
authMock.getEmailAddress.yields(null, {
emailAddress: emailAddress
});
accountMock.init.yields(null, {});
createController();
2014-11-26 13:51:15 -05:00
expect(goToStub.withArgs('/login-initial').calledOnce).to.be.true;
2014-11-25 12:19:40 -05:00
});
});