mail/test/unit/controller/login/login-ctrl-test.js

233 lines
7.7 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;
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
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,
2014-12-17 16:41:57 -05:00
dialog: dialogMock,
appConfig: {
preventAutoStart: true
}
2014-11-25 12:19:40 -05:00
});
2014-10-07 14:32:23 -04:00
});
2014-11-25 12:19:40 -05:00
2014-12-17 16:41:57 -05:00
scope.goTo = function() {};
goToStub = sinon.stub(scope, 'goTo');
});
2014-11-25 12:19:40 -05:00
2014-12-17 16:41:57 -05:00
afterEach(function() {});
2014-11-25 12:19:40 -05:00
2014-12-17 16:41:57 -05:00
it('should fail for auth.getEmailAddress', function(done) {
authMock.init.returns(resolves());
authMock.getEmailAddress.returns(rejects(new Error()));
2014-11-25 12:19:40 -05:00
2014-12-17 16:41:57 -05:00
scope.init().then(function() {
expect(updateHandlerMock.checkForUpdate.calledOnce).to.be.true;
expect(authMock.init.calledOnce).to.be.true;
expect(dialogMock.error.calledOnce).to.be.true;
done();
});
2014-11-25 12:19:40 -05:00
});
2014-12-17 16:41:57 -05:00
it('should fail for auth.init', function(done) {
authMock.init.returns(rejects(new Error()));
authMock.getEmailAddress.returns(resolves({
2014-11-25 12:19:40 -05:00
emailAddress: emailAddress
2014-12-17 16:41:57 -05:00
}));
2014-03-31 15:35:40 -04:00
2014-12-17 16:41:57 -05:00
scope.init().then(function() {
expect(authMock.init.calledOnce).to.be.true;
expect(accountMock.init.called).to.be.false;
expect(dialogMock.error.calledOnce).to.be.true;
done();
});
2014-11-25 12:19:40 -05:00
});
2014-12-17 16:41:57 -05:00
it('should redirect to /add-account', function(done) {
authMock.init.returns(resolves());
authMock.getEmailAddress.returns(resolves({}));
2014-12-17 16:41:57 -05:00
scope.init().then(function() {
expect(goToStub.calledWith('/add-account')).to.be.true;
2014-12-17 18:27:03 -05:00
expect(goToStub.calledOnce).to.be.true;
2014-12-17 16:41:57 -05:00
done();
});
});
2014-12-17 16:41:57 -05:00
it('should redirect to /login-existing', function(done) {
authMock.init.returns(resolves());
authMock.getEmailAddress.returns(resolves({
2014-11-25 12:19:40 -05:00
emailAddress: emailAddress
2014-12-17 16:41:57 -05:00
}));
accountMock.init.returns(resolves({
2014-11-25 12:19:40 -05:00
publicKey: 'publicKey',
privateKey: 'privateKey'
2014-12-17 16:41:57 -05:00
}));
emailMock.unlock.returns(rejects(new Error()));
2014-03-31 15:35:40 -04:00
2014-12-17 16:41:57 -05:00
scope.init().then(function() {
expect(goToStub.calledWith('/login-existing')).to.be.true;
2014-12-17 18:27:03 -05:00
expect(goToStub.calledOnce).to.be.true;
expect(authMock.storeCredentials.called).to.be.false;
2014-12-17 16:41:57 -05:00
done();
});
2014-11-25 12:19:40 -05:00
});
2014-12-17 16:41:57 -05:00
it('should fail for auth.storeCredentials', function(done) {
authMock.init.returns(resolves());
authMock.getEmailAddress.returns(resolves({
2014-11-25 12:19:40 -05:00
emailAddress: emailAddress
2014-12-17 16:41:57 -05:00
}));
accountMock.init.returns(resolves({
2014-11-25 12:19:40 -05:00
publicKey: 'publicKey',
privateKey: 'privateKey'
2014-12-17 16:41:57 -05:00
}));
emailMock.unlock.returns(resolves());
authMock.storeCredentials.returns(rejects(new Error()));
2014-11-25 12:19:40 -05:00
2014-12-17 16:41:57 -05:00
scope.init().then(function() {
expect(dialogMock.error.calledOnce).to.be.true;
done();
});
2014-11-25 12:19:40 -05:00
});
2014-12-17 16:41:57 -05:00
it('should redirect to /account', function(done) {
authMock.init.returns(resolves());
authMock.getEmailAddress.returns(resolves({
2014-11-25 12:19:40 -05:00
emailAddress: emailAddress
2014-12-17 16:41:57 -05:00
}));
accountMock.init.returns(resolves({
2014-11-25 12:19:40 -05:00
publicKey: 'publicKey',
privateKey: 'privateKey'
2014-12-17 16:41:57 -05:00
}));
emailMock.unlock.returns(resolves());
authMock.storeCredentials.returns(resolves());
2014-11-25 12:19:40 -05:00
2014-12-17 16:41:57 -05:00
scope.init().then(function() {
expect(goToStub.calledWith('/account')).to.be.true;
2014-12-17 18:27:03 -05:00
expect(goToStub.calledOnce).to.be.true;
2014-12-17 16:41:57 -05:00
done();
});
2014-11-25 12:19:40 -05:00
});
2014-12-17 16:41:57 -05:00
it('should fail for keychain.requestPrivateKeyDownload', function(done) {
authMock.init.returns(resolves());
authMock.getEmailAddress.returns(resolves({
2014-11-25 12:19:40 -05:00
emailAddress: emailAddress
2014-12-17 16:41:57 -05:00
}));
accountMock.init.returns(resolves({
2014-11-25 12:19:40 -05:00
publicKey: 'publicKey'
2014-12-17 16:41:57 -05:00
}));
keychainMock.requestPrivateKeyDownload.returns(rejects(new Error()));
2014-12-17 16:41:57 -05:00
scope.init().then(function() {
expect(dialogMock.error.calledOnce).to.be.true;
done();
});
2014-11-25 12:19:40 -05:00
});
2014-12-17 16:41:57 -05:00
it('should redirect to /login-privatekey-download', function(done) {
authMock.init.returns(resolves());
authMock.getEmailAddress.returns(resolves({
2014-11-25 12:19:40 -05:00
emailAddress: emailAddress
2014-12-17 16:41:57 -05:00
}));
accountMock.init.returns(resolves({
2014-11-25 12:19:40 -05:00
publicKey: 'publicKey'
2014-12-17 16:41:57 -05:00
}));
keychainMock.requestPrivateKeyDownload.returns(resolves(true));
2014-11-25 12:19:40 -05:00
2014-12-17 16:41:57 -05:00
scope.init().then(function() {
expect(goToStub.calledWith('/login-privatekey-download')).to.be.true;
2014-12-17 18:27:03 -05:00
expect(goToStub.calledOnce).to.be.true;
2014-12-17 16:41:57 -05:00
done();
});
2014-11-25 12:19:40 -05:00
});
2014-12-17 16:41:57 -05:00
it('should redirect to /login-new-device', function(done) {
authMock.init.returns(resolves());
authMock.getEmailAddress.returns(resolves({
2014-11-25 12:19:40 -05:00
emailAddress: emailAddress
2014-12-17 16:41:57 -05:00
}));
accountMock.init.returns(resolves({
2014-11-25 12:19:40 -05:00
publicKey: 'publicKey'
2014-12-17 16:41:57 -05:00
}));
keychainMock.requestPrivateKeyDownload.returns(resolves());
2014-11-25 12:19:40 -05:00
2014-12-17 16:41:57 -05:00
scope.init().then(function() {
expect(goToStub.calledWith('/login-new-device')).to.be.true;
expect(goToStub.calledOnce).to.be.true;
done();
});
});
it('should redirect to /login-new-device', function(done) {
authMock.init.returns(resolves());
authMock.getEmailAddress.returns(resolves({
emailAddress: emailAddress
}));
accountMock.init.returns(resolves({
publicKey: {
source: 'foo'
}
}));
scope.init().then(function() {
expect(goToStub.calledWith('/login-initial')).to.be.true;
2014-12-17 18:27:03 -05:00
expect(goToStub.calledOnce).to.be.true;
2014-12-17 16:41:57 -05:00
done();
});
});
2014-11-25 12:19:40 -05:00
2014-12-17 16:41:57 -05:00
it('should redirect to /login-initial', function(done) {
authMock.init.returns(resolves());
authMock.getEmailAddress.returns(resolves({
2014-11-25 12:19:40 -05:00
emailAddress: emailAddress
2014-12-17 16:41:57 -05:00
}));
accountMock.init.returns(resolves({}));
2014-11-25 12:19:40 -05:00
2014-12-17 16:41:57 -05:00
scope.init().then(function() {
expect(goToStub.calledWith('/login-initial')).to.be.true;
2014-12-17 18:27:03 -05:00
expect(goToStub.calledOnce).to.be.true;
2014-12-17 16:41:57 -05:00
done();
});
2014-11-25 12:19:40 -05:00
});
});