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',
|
|
|
|
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.state.ui).to.equal(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-31 15:39:43 -04:00
|
|
|
describe('signUpToNewsletter', function() {
|
|
|
|
var xhrMock, requests;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
xhrMock = sinon.useFakeXMLHttpRequest();
|
|
|
|
requests = [];
|
|
|
|
|
|
|
|
xhrMock.onCreate = function(xhr) {
|
|
|
|
requests.push(xhr);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
|
|
|
xhrMock.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not signup', function() {
|
|
|
|
scope.state.newsletter = false;
|
|
|
|
|
|
|
|
scope.signUpToNewsletter();
|
|
|
|
expect(requests.length).to.equal(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail', function(done) {
|
|
|
|
scope.state.newsletter = true;
|
|
|
|
|
|
|
|
scope.signUpToNewsletter(function(err, xhr) {
|
|
|
|
expect(err).to.exist;
|
|
|
|
expect(xhr).to.not.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(requests.length).to.equal(1);
|
|
|
|
requests[0].onerror('err');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work without callback', function() {
|
|
|
|
scope.state.newsletter = true;
|
|
|
|
|
|
|
|
scope.signUpToNewsletter();
|
|
|
|
|
|
|
|
expect(requests.length).to.equal(1);
|
|
|
|
requests[0].respond(200, {
|
|
|
|
"Content-Type": "text/plain"
|
|
|
|
}, 'foobar!');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-04-15 11:43:33 -04:00
|
|
|
describe('go to import key', function() {
|
2014-07-31 15:39:43 -04:00
|
|
|
var signUpToNewsletterStub;
|
|
|
|
beforeEach(function() {
|
|
|
|
signUpToNewsletterStub = sinon.stub(scope, 'signUpToNewsletter');
|
|
|
|
});
|
|
|
|
afterEach(function() {
|
|
|
|
signUpToNewsletterStub.restore();
|
|
|
|
});
|
|
|
|
|
2014-04-15 11:43:33 -04:00
|
|
|
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');
|
2014-07-31 15:39:43 -04:00
|
|
|
expect(signUpToNewsletterStub.called).to.be.false;
|
2014-04-15 11:43:33 -04:00
|
|
|
done();
|
|
|
|
};
|
|
|
|
|
|
|
|
scope.importKey();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work', function() {
|
|
|
|
scope.state.agree = true;
|
|
|
|
scope.importKey();
|
2014-07-31 15:39:43 -04:00
|
|
|
expect(signUpToNewsletterStub.calledOnce).to.be.true;
|
2014-04-15 11:43:33 -04:00
|
|
|
expect(location.$$path).to.equal('/login-new-device');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-09-12 11:11:05 -04:00
|
|
|
describe('generate key', function() {
|
2014-07-31 15:39:43 -04:00
|
|
|
var signUpToNewsletterStub;
|
|
|
|
beforeEach(function() {
|
|
|
|
signUpToNewsletterStub = sinon.stub(scope, 'signUpToNewsletter');
|
|
|
|
});
|
|
|
|
afterEach(function() {
|
|
|
|
signUpToNewsletterStub.restore();
|
|
|
|
});
|
|
|
|
|
2014-04-15 11:43:33 -04:00
|
|
|
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');
|
2014-09-12 11:11:05 -04:00
|
|
|
expect(scope.state.ui).to.equal(1);
|
2014-07-31 15:39:43 -04:00
|
|
|
expect(signUpToNewsletterStub.called).to.be.false;
|
2014-04-15 11:43:33 -04:00
|
|
|
done();
|
|
|
|
};
|
|
|
|
|
2014-09-12 11:11:05 -04:00
|
|
|
scope.generateKey();
|
2014-04-15 11:43:33 -04:00
|
|
|
});
|
|
|
|
|
2014-09-12 11:11:05 -04:00
|
|
|
it('should fail due to error in emailDao.unlock', function(done) {
|
2014-07-31 13:08:21 -04:00
|
|
|
scope.state.agree = true;
|
|
|
|
|
2014-09-12 11:11:05 -04:00
|
|
|
emailDaoMock.unlock.withArgs({
|
|
|
|
passphrase: undefined
|
|
|
|
}).yields(new Error());
|
|
|
|
authMock.storeCredentials.yields();
|
|
|
|
|
|
|
|
scope.onError = function(err) {
|
|
|
|
expect(err).to.exist;
|
|
|
|
expect(scope.state.ui).to.equal(1);
|
|
|
|
expect(signUpToNewsletterStub.called).to.be.true;
|
2014-07-31 13:08:21 -04:00
|
|
|
done();
|
2014-09-12 11:11:05 -04:00
|
|
|
};
|
2014-07-31 13:08:21 -04:00
|
|
|
|
2014-09-12 11:11:05 -04:00
|
|
|
scope.generateKey();
|
|
|
|
expect(scope.state.ui).to.equal(2);
|
2014-07-31 13:08:21 -04:00
|
|
|
});
|
|
|
|
|
2013-11-11 11:56:51 -05:00
|
|
|
it('should unlock crypto', function(done) {
|
2014-09-12 11:11:05 -04:00
|
|
|
scope.state.agree = true;
|
2014-04-15 11:43:33 -04:00
|
|
|
|
2013-12-05 10:32:12 -05:00
|
|
|
emailDaoMock.unlock.withArgs({
|
2014-09-12 11:11:05 -04:00
|
|
|
passphrase: undefined
|
2013-12-05 10:32:12 -05:00
|
|
|
}).yields();
|
2014-07-01 13:49:19 -04:00
|
|
|
authMock.storeCredentials.yields();
|
2014-03-31 15:35:40 -04:00
|
|
|
|
|
|
|
scope.$apply = function() {
|
2014-09-12 11:11:05 -04:00
|
|
|
expect(scope.state.ui).to.equal(2);
|
2014-03-31 15:35:40 -04:00
|
|
|
expect(location.$$path).to.equal('/desktop');
|
|
|
|
expect(emailDaoMock.unlock.calledOnce).to.be.true;
|
|
|
|
done();
|
|
|
|
};
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2014-09-12 11:11:05 -04:00
|
|
|
scope.generateKey();
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|