2014-10-07 14:32:23 -04:00
|
|
|
'use strict';
|
|
|
|
|
2014-10-09 09:56:35 -04:00
|
|
|
var mocks = angular.mock,
|
2014-10-07 14:32:23 -04:00
|
|
|
PGP = require('../../src/js/crypto/pgp'),
|
|
|
|
LoginNewDeviceCtrl = require('../../src/js/controller/login-new-device'),
|
|
|
|
KeychainDAO = require('../../src/js/dao/keychain-dao'),
|
|
|
|
EmailDAO = require('../../src/js/dao/email-dao'),
|
|
|
|
appController = require('../../src/js/app-controller');
|
|
|
|
|
|
|
|
describe('Login (new device) Controller unit test', function() {
|
|
|
|
var scope, ctrl, origEmailDao, emailDaoMock, pgpMock,
|
|
|
|
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';
|
|
|
|
emailDaoMock._keychain = keychainMock = sinon.createStubInstance(KeychainDAO);
|
|
|
|
appController._pgp = pgpMock = sinon.createStubInstance(PGP);
|
|
|
|
pgpMock.extractPublicKey.returns('publicKeyArmored');
|
|
|
|
|
|
|
|
emailDaoMock._account = {
|
|
|
|
emailAddress: emailAddress,
|
|
|
|
};
|
|
|
|
|
|
|
|
angular.module('loginnewdevicetest', []);
|
|
|
|
mocks.module('loginnewdevicetest');
|
|
|
|
mocks.inject(function($rootScope, $controller) {
|
|
|
|
scope = $rootScope.$new();
|
|
|
|
scope.state = {
|
|
|
|
ui: {}
|
2013-11-11 11:56:51 -05:00
|
|
|
};
|
2014-11-12 10:12:26 -05:00
|
|
|
scope.form = {};
|
2014-10-07 14:32:23 -04:00
|
|
|
ctrl = $controller(LoginNewDeviceCtrl, {
|
|
|
|
$scope: scope,
|
|
|
|
$routeParams: {}
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
|
|
|
// restore the module
|
|
|
|
appController._emailDao = origEmailDao;
|
|
|
|
});
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('initial state', function() {
|
|
|
|
it('should be well defined', function() {
|
|
|
|
expect(scope.incorrect).to.be.false;
|
|
|
|
expect(scope.confirmPassphrase).to.exist;
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('confirm passphrase', function() {
|
|
|
|
it('should unlock crypto with a public key on the server', function() {
|
|
|
|
scope.passphrase = passphrase;
|
|
|
|
scope.key = {
|
|
|
|
privateKeyArmored: 'b'
|
|
|
|
};
|
|
|
|
|
|
|
|
pgpMock.getKeyParams.returns({
|
|
|
|
_id: 'id',
|
|
|
|
userIds: []
|
|
|
|
});
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
keychainMock.getUserKeyPair.withArgs(emailAddress).yields(null, {
|
|
|
|
_id: keyId,
|
|
|
|
publicKey: 'a'
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
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;
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should unlock crypto with no key on the server', function() {
|
|
|
|
scope.passphrase = passphrase;
|
|
|
|
scope.key = {
|
|
|
|
privateKeyArmored: 'b',
|
|
|
|
publicKeyArmored: 'a'
|
|
|
|
};
|
|
|
|
|
|
|
|
pgpMock.getKeyParams.returns({
|
|
|
|
_id: 'id',
|
|
|
|
userIds: []
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
keychainMock.getUserKeyPair.withArgs(emailAddress).yields();
|
|
|
|
emailDaoMock.unlock.withArgs(sinon.match.any, passphrase).yields();
|
|
|
|
keychainMock.putUserKeyPair.yields();
|
|
|
|
|
|
|
|
scope.confirmPassphrase();
|
2014-03-05 14:14:23 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(emailDaoMock.unlock.calledOnce).to.be.true;
|
|
|
|
expect(keychainMock.getUserKeyPair.calledOnce).to.be.true;
|
|
|
|
});
|
2014-03-05 14:14:23 -05:00
|
|
|
|
2014-11-12 10:12:26 -05:00
|
|
|
it('should not work when keypair upload fails', function() {
|
2014-10-07 14:32:23 -04:00
|
|
|
scope.passphrase = passphrase;
|
|
|
|
scope.key = {
|
|
|
|
privateKeyArmored: 'b'
|
|
|
|
};
|
2014-03-05 14:14:23 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
pgpMock.getKeyParams.returns({
|
|
|
|
_id: 'id',
|
|
|
|
userIds: []
|
|
|
|
});
|
2014-03-05 14:14:23 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
keychainMock.getUserKeyPair.withArgs(emailAddress).yields(null, {
|
|
|
|
_id: keyId,
|
|
|
|
publicKey: 'a'
|
|
|
|
});
|
|
|
|
emailDaoMock.unlock.yields();
|
|
|
|
keychainMock.putUserKeyPair.yields({
|
|
|
|
errMsg: 'yo mamma.'
|
2014-03-05 14:14:23 -05:00
|
|
|
});
|
|
|
|
|
2014-10-07 14:32:23 -04: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-11-12 10:12:26 -05:00
|
|
|
expect(scope.errMsg).to.equal('yo mamma.');
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
|
|
|
|
2014-11-12 10:12:26 -05:00
|
|
|
it('should not work when unlock fails', function() {
|
2014-10-07 14:32:23 -04:00
|
|
|
scope.passphrase = passphrase;
|
|
|
|
scope.key = {
|
|
|
|
privateKeyArmored: 'b'
|
|
|
|
};
|
|
|
|
|
|
|
|
pgpMock.getKeyParams.returns({
|
|
|
|
_id: 'id',
|
|
|
|
userIds: []
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
keychainMock.getUserKeyPair.withArgs(emailAddress).yields(null, {
|
|
|
|
_id: keyId,
|
|
|
|
publicKey: 'a'
|
|
|
|
});
|
|
|
|
emailDaoMock.unlock.yields({
|
|
|
|
errMsg: 'yo mamma.'
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
scope.confirmPassphrase();
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(scope.incorrect).to.be.true;
|
|
|
|
expect(keychainMock.getUserKeyPair.calledOnce).to.be.true;
|
|
|
|
expect(emailDaoMock.unlock.calledOnce).to.be.true;
|
2014-11-12 10:12:26 -05:00
|
|
|
expect(scope.errMsg).to.equal('yo mamma.');
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-04-24 10:45:09 -04:00
|
|
|
|
2014-11-12 10:12:26 -05:00
|
|
|
it('should not work when keypair retrieval', function() {
|
2014-10-07 14:32:23 -04:00
|
|
|
scope.passphrase = passphrase;
|
2014-11-12 10:12:26 -05:00
|
|
|
scope.key = {
|
|
|
|
privateKeyArmored: 'b'
|
|
|
|
};
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
keychainMock.getUserKeyPair.withArgs(emailAddress).yields({
|
|
|
|
errMsg: 'yo mamma.'
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
|
|
|
|
scope.confirmPassphrase();
|
|
|
|
|
|
|
|
expect(keychainMock.getUserKeyPair.calledOnce).to.be.true;
|
2014-11-12 10:12:26 -05:00
|
|
|
expect(scope.errMsg).to.equal('yo mamma.');
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|