2013-11-11 11:56:51 -05:00
|
|
|
define(function(require) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var expect = chai.expect,
|
|
|
|
angular = require('angular'),
|
|
|
|
mocks = require('angularMocks'),
|
|
|
|
LoginCtrl = require('js/controller/login'),
|
|
|
|
EmailDAO = require('js/dao/email-dao'),
|
2014-04-01 07:16:39 -04:00
|
|
|
Auth = require('js/bo/auth'),
|
2013-11-11 11:56:51 -05:00
|
|
|
appController = require('js/app-controller');
|
|
|
|
|
|
|
|
describe('Login Controller unit test', function() {
|
|
|
|
var scope, location, ctrl, origEmailDao, emailDaoMock,
|
|
|
|
emailAddress = 'fred@foo.com',
|
|
|
|
startAppStub,
|
|
|
|
checkForUpdateStub,
|
2014-04-01 07:16:39 -04:00
|
|
|
authStub,
|
2013-11-11 11:56:51 -05:00
|
|
|
initStub;
|
|
|
|
|
|
|
|
describe('initialization', function() {
|
|
|
|
var hasChrome, hasIdentity;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
2013-12-03 13:21:50 -05:00
|
|
|
hasChrome = !! window.chrome;
|
|
|
|
hasIdentity = !! window.chrome.identity;
|
2013-11-11 11:56:51 -05:00
|
|
|
window.chrome = window.chrome || {};
|
|
|
|
window.chrome.identity = window.chrome.identity || {};
|
|
|
|
|
|
|
|
// remember original module to restore later, then replace it
|
|
|
|
origEmailDao = appController._emailDao;
|
2014-04-01 07:16:39 -04:00
|
|
|
appController._emailDao = emailDaoMock = sinon.createStubInstance(EmailDAO);
|
|
|
|
appController._auth = authStub = sinon.createStubInstance(Auth);
|
2013-12-09 13:21:52 -05:00
|
|
|
|
|
|
|
startAppStub = sinon.stub(appController, 'start');
|
|
|
|
checkForUpdateStub = sinon.stub(appController, 'checkForUpdate');
|
|
|
|
initStub = sinon.stub(appController, 'init');
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
|
|
|
// restore the browser
|
|
|
|
if (!hasIdentity) {
|
|
|
|
delete window.chrome.identity;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasChrome) {
|
|
|
|
delete window.chrome;
|
|
|
|
}
|
|
|
|
|
|
|
|
// restore the app controller module
|
|
|
|
appController._emailDao = origEmailDao;
|
|
|
|
appController.start.restore && appController.start.restore();
|
|
|
|
appController.checkForUpdate.restore && appController.checkForUpdate.restore();
|
|
|
|
appController.init.restore && appController.init.restore();
|
|
|
|
location.path.restore && location.path.restore();
|
2013-12-09 13:21:52 -05:00
|
|
|
|
|
|
|
startAppStub.restore();
|
|
|
|
checkForUpdateStub.restore();
|
|
|
|
initStub.restore();
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
|
|
|
|
2014-03-31 15:35:40 -04:00
|
|
|
it('should forward directly to desktop for empty passphrase', function(done) {
|
|
|
|
var testKeys = {
|
|
|
|
privateKey: 'a',
|
|
|
|
publicKey: 'b'
|
|
|
|
};
|
|
|
|
|
2013-11-11 11:56:51 -05:00
|
|
|
startAppStub.yields();
|
2014-04-01 07:16:39 -04:00
|
|
|
authStub.getEmailAddress.yields(null, emailAddress);
|
2014-03-31 15:35:40 -04:00
|
|
|
initStub.yields(null, testKeys);
|
|
|
|
|
|
|
|
emailDaoMock.unlock.withArgs({
|
|
|
|
keypair: testKeys,
|
|
|
|
passphrase: undefined
|
|
|
|
}).yields();
|
|
|
|
|
|
|
|
angular.module('logintest', []);
|
|
|
|
mocks.module('logintest');
|
|
|
|
mocks.inject(function($controller, $rootScope, $location) {
|
|
|
|
location = $location;
|
|
|
|
sinon.stub(location, 'path', function(path) {
|
|
|
|
expect(path).to.equal('/desktop');
|
|
|
|
expect(startAppStub.calledOnce).to.be.true;
|
|
|
|
expect(checkForUpdateStub.calledOnce).to.be.true;
|
2014-04-01 07:16:39 -04:00
|
|
|
expect(authStub.getEmailAddress.calledOnce).to.be.true;
|
2014-03-31 15:35:40 -04:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
scope = $rootScope.$new();
|
|
|
|
scope.state = {};
|
|
|
|
ctrl = $controller(LoginCtrl, {
|
|
|
|
$location: location,
|
|
|
|
$scope: scope
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should forward to existing user login', function(done) {
|
|
|
|
var testKeys = {
|
2013-11-11 11:56:51 -05:00
|
|
|
privateKey: 'a',
|
|
|
|
publicKey: 'b'
|
2014-03-31 15:35:40 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
startAppStub.yields();
|
2014-04-01 07:16:39 -04:00
|
|
|
authStub.getEmailAddress.yields(null, emailAddress);
|
2014-03-31 15:35:40 -04:00
|
|
|
initStub.yields(null, testKeys);
|
|
|
|
|
|
|
|
emailDaoMock.unlock.withArgs({
|
|
|
|
keypair: testKeys,
|
|
|
|
passphrase: undefined
|
|
|
|
}).yields({});
|
2013-11-11 11:56:51 -05:00
|
|
|
|
|
|
|
angular.module('logintest', []);
|
|
|
|
mocks.module('logintest');
|
|
|
|
mocks.inject(function($controller, $rootScope, $location) {
|
|
|
|
location = $location;
|
|
|
|
sinon.stub(location, 'path', function(path) {
|
|
|
|
expect(path).to.equal('/login-existing');
|
|
|
|
expect(startAppStub.calledOnce).to.be.true;
|
|
|
|
expect(checkForUpdateStub.calledOnce).to.be.true;
|
2014-04-01 07:16:39 -04:00
|
|
|
expect(authStub.getEmailAddress.calledOnce).to.be.true;
|
2013-11-11 11:56:51 -05:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
scope = $rootScope.$new();
|
|
|
|
scope.state = {};
|
|
|
|
ctrl = $controller(LoginCtrl, {
|
|
|
|
$location: location,
|
|
|
|
$scope: scope
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should forward to new device login', function(done) {
|
|
|
|
startAppStub.yields();
|
2014-04-01 07:16:39 -04:00
|
|
|
authStub.getEmailAddress.yields(null, emailAddress);
|
2013-11-11 11:56:51 -05:00
|
|
|
initStub.yields(null, {
|
|
|
|
publicKey: 'b'
|
|
|
|
});
|
|
|
|
|
|
|
|
angular.module('logintest', []);
|
|
|
|
mocks.module('logintest');
|
|
|
|
mocks.inject(function($controller, $rootScope, $location) {
|
|
|
|
location = $location;
|
|
|
|
sinon.stub(location, 'path', function(path) {
|
|
|
|
expect(path).to.equal('/login-new-device');
|
|
|
|
expect(startAppStub.calledOnce).to.be.true;
|
|
|
|
expect(checkForUpdateStub.calledOnce).to.be.true;
|
2014-04-01 07:16:39 -04:00
|
|
|
expect(authStub.getEmailAddress.calledOnce).to.be.true;
|
2013-11-11 11:56:51 -05:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
scope = $rootScope.$new();
|
|
|
|
scope.state = {};
|
|
|
|
ctrl = $controller(LoginCtrl, {
|
|
|
|
$location: location,
|
|
|
|
$scope: scope
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should forward to initial login', function(done) {
|
|
|
|
startAppStub.yields();
|
2014-04-01 07:16:39 -04:00
|
|
|
authStub.getEmailAddress.yields(null, emailAddress);
|
2013-11-11 11:56:51 -05:00
|
|
|
initStub.yields();
|
|
|
|
|
|
|
|
angular.module('logintest', []);
|
|
|
|
mocks.module('logintest');
|
|
|
|
mocks.inject(function($controller, $rootScope, $location) {
|
|
|
|
location = $location;
|
|
|
|
sinon.stub(location, 'path', function(path) {
|
|
|
|
expect(path).to.equal('/login-initial');
|
|
|
|
expect(startAppStub.calledOnce).to.be.true;
|
|
|
|
expect(checkForUpdateStub.calledOnce).to.be.true;
|
2014-04-01 07:16:39 -04:00
|
|
|
expect(authStub.getEmailAddress.calledOnce).to.be.true;
|
2013-11-11 11:56:51 -05:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
scope = $rootScope.$new();
|
|
|
|
scope.state = {};
|
|
|
|
ctrl = $controller(LoginCtrl, {
|
|
|
|
$location: location,
|
|
|
|
$scope: scope
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|