2013-11-11 11:56:51 -05:00
|
|
|
define(function(require) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var expect = chai.expect,
|
|
|
|
angular = require('angular'),
|
|
|
|
mocks = require('angularMocks'),
|
2014-03-05 14:14:23 -05:00
|
|
|
PGP = require('js/crypto/pgp'),
|
2013-11-11 11:56:51 -05:00
|
|
|
LoginNewDeviceCtrl = require('js/controller/login-new-device'),
|
|
|
|
KeychainDAO = require('js/dao/keychain-dao'),
|
|
|
|
EmailDAO = require('js/dao/email-dao'),
|
|
|
|
appController = require('js/app-controller');
|
|
|
|
|
|
|
|
describe('Login (new device) Controller unit test', function() {
|
2014-03-05 14:14:23 -05:00
|
|
|
var scope, ctrl, origEmailDao, emailDaoMock, pgpMock,
|
2013-11-11 11:56:51 -05:00
|
|
|
emailAddress = 'fred@foo.com',
|
|
|
|
passphrase = 'asd',
|
|
|
|
keyId,
|
|
|
|
keychainMock;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
// remember original module to restore later
|
|
|
|
origEmailDao = appController._emailDao;
|
|
|
|
|
|
|
|
emailDaoMock = sinon.createStubInstance(EmailDAO);
|
|
|
|
appController._emailDao = emailDaoMock;
|
|
|
|
|
|
|
|
keyId = '9FEB47936E712926';
|
2014-03-05 14:14:23 -05:00
|
|
|
emailDaoMock._keychain = keychainMock = sinon.createStubInstance(KeychainDAO);
|
|
|
|
appController._crypto = pgpMock = sinon.createStubInstance(PGP);
|
2013-11-11 11:56:51 -05:00
|
|
|
|
|
|
|
emailDaoMock._account = {
|
|
|
|
emailAddress: emailAddress,
|
|
|
|
};
|
|
|
|
|
|
|
|
angular.module('loginnewdevicetest', []);
|
|
|
|
mocks.module('loginnewdevicetest');
|
|
|
|
mocks.inject(function($rootScope, $controller) {
|
|
|
|
scope = $rootScope.$new();
|
|
|
|
scope.state = {
|
|
|
|
ui: {}
|
|
|
|
};
|
|
|
|
ctrl = $controller(LoginNewDeviceCtrl, {
|
|
|
|
$scope: scope
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
|
|
|
// restore the module
|
|
|
|
appController._emailDao = origEmailDao;
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('initial state', function() {
|
|
|
|
it('should be well defined', function() {
|
|
|
|
expect(scope.incorrect).to.be.false;
|
|
|
|
expect(scope.confirmPassphrase).to.exist;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('confirm passphrase', function() {
|
2014-03-05 14:14:23 -05:00
|
|
|
it('should unlock crypto with a public key on the server', function() {
|
2013-11-11 11:56:51 -05:00
|
|
|
scope.passphrase = passphrase;
|
|
|
|
scope.key = {
|
|
|
|
privateKeyArmored: 'b'
|
|
|
|
};
|
2014-03-05 14:14:23 -05:00
|
|
|
|
|
|
|
pgpMock.getKeyId.returns(keyId);
|
|
|
|
|
2013-11-11 11:56:51 -05:00
|
|
|
keychainMock.getUserKeyPair.withArgs(emailAddress).yields(null, {
|
|
|
|
_id: keyId,
|
|
|
|
publicKey: 'a'
|
|
|
|
});
|
|
|
|
emailDaoMock.unlock.withArgs(sinon.match.any, passphrase).yields();
|
|
|
|
keychainMock.putUserKeyPair.yields();
|
|
|
|
|
|
|
|
scope.confirmPassphrase();
|
|
|
|
|
|
|
|
expect(emailDaoMock.unlock.calledOnce).to.be.true;
|
|
|
|
expect(keychainMock.getUserKeyPair.calledOnce).to.be.true;
|
|
|
|
});
|
|
|
|
|
2014-03-05 14:14:23 -05:00
|
|
|
it('should unlock crypto with no key on the server', function() {
|
|
|
|
scope.passphrase = passphrase;
|
|
|
|
scope.key = {
|
|
|
|
privateKeyArmored: 'b',
|
|
|
|
publicKeyArmored: 'a'
|
|
|
|
};
|
|
|
|
|
|
|
|
pgpMock.getKeyId.returns(keyId);
|
|
|
|
|
|
|
|
keychainMock.getUserKeyPair.withArgs(emailAddress).yields();
|
|
|
|
emailDaoMock.unlock.withArgs(sinon.match.any, passphrase).yields();
|
|
|
|
keychainMock.putUserKeyPair.yields();
|
|
|
|
|
|
|
|
scope.confirmPassphrase();
|
|
|
|
|
|
|
|
expect(emailDaoMock.unlock.calledOnce).to.be.true;
|
|
|
|
expect(keychainMock.getUserKeyPair.calledOnce).to.be.true;
|
|
|
|
});
|
|
|
|
|
2014-04-24 10:45:09 -04:00
|
|
|
it('should not work when keypair upload fails', function(done) {
|
2013-11-11 11:56:51 -05:00
|
|
|
scope.passphrase = passphrase;
|
|
|
|
scope.key = {
|
|
|
|
privateKeyArmored: 'b'
|
|
|
|
};
|
|
|
|
|
|
|
|
keychainMock.getUserKeyPair.withArgs(emailAddress).yields(null, {
|
|
|
|
_id: keyId,
|
|
|
|
publicKey: 'a'
|
|
|
|
});
|
2013-12-04 05:49:36 -05:00
|
|
|
emailDaoMock.unlock.yields();
|
2013-11-11 11:56:51 -05:00
|
|
|
keychainMock.putUserKeyPair.yields({
|
|
|
|
errMsg: 'yo mamma.'
|
|
|
|
});
|
|
|
|
|
2014-04-24 10:45:09 -04:00
|
|
|
scope.onError = function(err) {
|
|
|
|
expect(err.errMsg).to.equal('yo mamma.');
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
|
2013-11-11 11:56:51 -05:00
|
|
|
scope.confirmPassphrase();
|
|
|
|
|
|
|
|
expect(keychainMock.getUserKeyPair.calledOnce).to.be.true;
|
|
|
|
expect(emailDaoMock.unlock.calledOnce).to.be.true;
|
|
|
|
expect(keychainMock.putUserKeyPair.calledOnce).to.be.true;
|
|
|
|
});
|
|
|
|
|
2014-04-24 10:45:09 -04:00
|
|
|
it('should not work when unlock fails', function(done) {
|
2013-11-11 11:56:51 -05:00
|
|
|
scope.passphrase = passphrase;
|
|
|
|
scope.key = {
|
|
|
|
privateKeyArmored: 'b'
|
|
|
|
};
|
|
|
|
|
|
|
|
keychainMock.getUserKeyPair.withArgs(emailAddress).yields(null, {
|
|
|
|
_id: keyId,
|
|
|
|
publicKey: 'a'
|
|
|
|
});
|
2013-12-04 05:49:36 -05:00
|
|
|
emailDaoMock.unlock.yields({
|
2013-11-11 11:56:51 -05:00
|
|
|
errMsg: 'yo mamma.'
|
|
|
|
});
|
|
|
|
|
2014-04-24 10:45:09 -04:00
|
|
|
scope.onError = function(err) {
|
|
|
|
expect(err.errMsg).to.equal('yo mamma.');
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
|
2013-11-11 11:56:51 -05:00
|
|
|
scope.confirmPassphrase();
|
|
|
|
|
|
|
|
expect(scope.incorrect).to.be.true;
|
|
|
|
expect(keychainMock.getUserKeyPair.calledOnce).to.be.true;
|
|
|
|
expect(emailDaoMock.unlock.calledOnce).to.be.true;
|
|
|
|
});
|
|
|
|
|
2014-04-24 10:45:09 -04:00
|
|
|
it('should not work when keypair retrieval', function(done) {
|
2013-11-11 11:56:51 -05:00
|
|
|
scope.passphrase = passphrase;
|
|
|
|
|
|
|
|
keychainMock.getUserKeyPair.withArgs(emailAddress).yields({
|
|
|
|
errMsg: 'yo mamma.'
|
|
|
|
});
|
|
|
|
|
2014-04-24 10:45:09 -04:00
|
|
|
scope.onError = function(err) {
|
|
|
|
expect(err.errMsg).to.equal('yo mamma.');
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
|
2013-11-11 11:56:51 -05:00
|
|
|
scope.confirmPassphrase();
|
|
|
|
|
|
|
|
expect(keychainMock.getUserKeyPair.calledOnce).to.be.true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|