2013-11-11 11:56:51 -05:00
|
|
|
define(function(require) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var expect = chai.expect,
|
|
|
|
angular = require('angular'),
|
2014-07-01 13:49:19 -04:00
|
|
|
Auth = require('js/bo/auth'),
|
2013-11-11 11:56:51 -05:00
|
|
|
mocks = require('angularMocks'),
|
|
|
|
LoginInitialCtrl = require('js/controller/login-initial'),
|
|
|
|
PGP = require('js/crypto/pgp'),
|
|
|
|
EmailDAO = require('js/dao/email-dao'),
|
|
|
|
appController = require('js/app-controller');
|
|
|
|
|
|
|
|
describe('Login (initial user) Controller unit test', function() {
|
|
|
|
var scope, ctrl, location, origEmailDao, emailDaoMock,
|
2014-07-01 13:49:19 -04:00
|
|
|
origAuth, authMock,
|
2013-11-11 11:56:51 -05:00
|
|
|
emailAddress = 'fred@foo.com',
|
|
|
|
passphrase = 'asd',
|
|
|
|
keyId, expectedKeyId,
|
|
|
|
cryptoMock;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
// remember original module to restore later
|
|
|
|
origEmailDao = appController._emailDao;
|
2014-07-01 13:49:19 -04:00
|
|
|
origAuth = appController._auth;
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2014-07-01 13:49:19 -04:00
|
|
|
appController._emailDao = emailDaoMock = sinon.createStubInstance(EmailDAO);
|
|
|
|
appController._auth = authMock = sinon.createStubInstance(Auth);
|
2013-11-11 11:56:51 -05:00
|
|
|
|
|
|
|
keyId = '9FEB47936E712926';
|
|
|
|
expectedKeyId = '6E712926';
|
|
|
|
cryptoMock = sinon.createStubInstance(PGP);
|
|
|
|
emailDaoMock._crypto = cryptoMock;
|
|
|
|
|
|
|
|
emailDaoMock._account = {
|
|
|
|
emailAddress: emailAddress,
|
|
|
|
};
|
|
|
|
|
|
|
|
angular.module('logininitialtest', []);
|
|
|
|
mocks.module('logininitialtest');
|
|
|
|
mocks.inject(function($rootScope, $controller, $location) {
|
|
|
|
scope = $rootScope.$new();
|
|
|
|
location = $location;
|
|
|
|
scope.state = {
|
|
|
|
ui: {}
|
|
|
|
};
|
|
|
|
ctrl = $controller(LoginInitialCtrl, {
|
|
|
|
$scope: scope
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
|
|
|
// restore the module
|
|
|
|
appController._emailDao = origEmailDao;
|
2014-07-01 13:49:19 -04:00
|
|
|
appController._auth = origAuth;
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('initial state', function() {
|
|
|
|
it('should be well defined', function() {
|
|
|
|
expect(scope.confirmPassphrase).to.exist;
|
|
|
|
expect(scope.state.ui).to.equal(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-04-15 11:43:33 -04:00
|
|
|
describe('go to import key', function() {
|
|
|
|
it('should not continue if terms are not accepted', function(done) {
|
|
|
|
scope.state.agree = undefined;
|
|
|
|
|
|
|
|
scope.onError = function(err) {
|
|
|
|
expect(err.message).to.contain('Terms');
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
|
|
|
|
scope.importKey();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work', function() {
|
|
|
|
scope.state.agree = true;
|
|
|
|
scope.importKey();
|
|
|
|
expect(location.$$path).to.equal('/login-new-device');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-08 13:54:02 -05:00
|
|
|
describe('check passphrase quality', function() {
|
|
|
|
it('should be too short', function() {
|
2014-03-31 15:35:40 -04:00
|
|
|
scope.state.passphrase = '&§DG36';
|
2014-01-08 13:54:02 -05:00
|
|
|
scope.checkPassphraseQuality();
|
|
|
|
|
2014-03-31 15:35:40 -04:00
|
|
|
expect(scope.passphraseMsg).to.equal('Very weak');
|
2014-01-08 13:54:02 -05:00
|
|
|
expect(scope.passphraseRating).to.equal(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be very weak', function() {
|
|
|
|
scope.state.passphrase = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
|
|
|
|
scope.checkPassphraseQuality();
|
|
|
|
|
|
|
|
expect(scope.passphraseMsg).to.equal('Very weak');
|
|
|
|
expect(scope.passphraseRating).to.equal(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be weak', function() {
|
|
|
|
scope.state.passphrase = 'asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdf';
|
|
|
|
scope.checkPassphraseQuality();
|
|
|
|
|
|
|
|
expect(scope.passphraseMsg).to.equal('Weak');
|
|
|
|
expect(scope.passphraseRating).to.equal(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be good', function() {
|
|
|
|
scope.state.passphrase = 'asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdf5';
|
|
|
|
scope.checkPassphraseQuality();
|
|
|
|
|
|
|
|
expect(scope.passphraseMsg).to.equal('Good');
|
|
|
|
expect(scope.passphraseRating).to.equal(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be strong', function() {
|
|
|
|
scope.state.passphrase = '&§DG36abcd';
|
|
|
|
scope.checkPassphraseQuality();
|
|
|
|
|
|
|
|
expect(scope.passphraseMsg).to.equal('Strong');
|
|
|
|
expect(scope.passphraseRating).to.equal(3);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-11-11 11:56:51 -05:00
|
|
|
describe('confirm passphrase', function() {
|
|
|
|
var setStateStub;
|
|
|
|
|
2014-04-15 11:43:33 -04:00
|
|
|
it('should not continue if terms are not accepted', function(done) {
|
|
|
|
scope.state.passphrase = passphrase;
|
|
|
|
scope.state.confirmation = passphrase;
|
|
|
|
scope.state.agree = undefined;
|
|
|
|
|
|
|
|
scope.onError = function(err) {
|
|
|
|
expect(err.message).to.contain('Terms');
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
|
|
|
|
scope.confirmPassphrase();
|
|
|
|
});
|
|
|
|
|
2013-11-11 11:56:51 -05:00
|
|
|
it('should unlock crypto', function(done) {
|
|
|
|
scope.state.passphrase = passphrase;
|
|
|
|
scope.state.confirmation = passphrase;
|
2014-04-15 11:43:33 -04:00
|
|
|
scope.state.agree = true;
|
|
|
|
|
2013-12-05 10:32:12 -05:00
|
|
|
emailDaoMock.unlock.withArgs({
|
|
|
|
passphrase: passphrase
|
|
|
|
}).yields();
|
2014-07-01 13:49:19 -04:00
|
|
|
authMock.storeCredentials.yields();
|
2014-03-31 15:35:40 -04:00
|
|
|
|
|
|
|
scope.$apply = function() {
|
|
|
|
expect(location.$$path).to.equal('/desktop');
|
|
|
|
expect(emailDaoMock.unlock.calledOnce).to.be.true;
|
|
|
|
done();
|
|
|
|
};
|
2013-11-11 11:56:51 -05:00
|
|
|
|
|
|
|
scope.confirmPassphrase();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not do anything matching passphrases', function() {
|
|
|
|
scope.state.passphrase = 'a';
|
|
|
|
scope.state.confirmation = 'b';
|
|
|
|
|
|
|
|
scope.confirmPassphrase();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not work when keypair generation fails', function(done) {
|
|
|
|
scope.state.passphrase = passphrase;
|
|
|
|
scope.state.confirmation = passphrase;
|
2014-04-15 11:43:33 -04:00
|
|
|
scope.state.agree = true;
|
|
|
|
|
2013-12-05 10:32:12 -05:00
|
|
|
emailDaoMock.unlock.withArgs({
|
|
|
|
passphrase: passphrase
|
|
|
|
}).yields(new Error('asd'));
|
2014-03-31 15:35:40 -04:00
|
|
|
|
2013-11-11 11:56:51 -05:00
|
|
|
setStateStub = sinon.stub(scope, 'setState', function(state) {
|
|
|
|
if (setStateStub.calledOnce) {
|
|
|
|
expect(state).to.equal(2);
|
|
|
|
} else if (setStateStub.calledTwice) {
|
|
|
|
expect(state).to.equal(1);
|
|
|
|
expect(emailDaoMock.unlock.calledOnce).to.be.true;
|
|
|
|
scope.setState.restore();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-04-24 10:45:09 -04:00
|
|
|
scope.onError = function(err) {
|
|
|
|
expect(err.message).to.equal('asd');
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
|
2013-11-11 11:56:51 -05:00
|
|
|
scope.confirmPassphrase();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|