mail/test/unit/controller/login/login-new-device-ctrl-test.js

198 lines
6.7 KiB
JavaScript
Raw Normal View History

2014-10-07 14:32:23 -04:00
'use strict';
2014-11-25 12:19:40 -05:00
var PGP = require('../../../../src/js/crypto/pgp'),
LoginNewDeviceCtrl = require('../../../../src/js/controller/login/login-new-device'),
KeychainDAO = require('../../../../src/js/service/keychain'),
2015-02-20 11:55:11 -05:00
PublicKeyVerifier = require('../../../../src/js/service/publickey-verifier'),
2014-11-25 12:19:40 -05:00
EmailDAO = require('../../../../src/js/email/email'),
Auth = require('../../../../src/js/service/auth');
2014-10-07 14:32:23 -04:00
describe('Login (new device) Controller unit test', function() {
2014-11-25 12:19:40 -05:00
var scope, ctrl, emailMock, pgpMock, authMock,
2014-10-07 14:32:23 -04:00
emailAddress = 'fred@foo.com',
passphrase = 'asd',
keyId,
2015-02-20 11:55:11 -05:00
location,
keychainMock,
verifierMock;
2014-10-07 14:32:23 -04:00
beforeEach(function() {
2014-11-25 12:19:40 -05:00
emailMock = sinon.createStubInstance(EmailDAO);
authMock = sinon.createStubInstance(Auth);
2015-02-20 11:55:11 -05:00
verifierMock = sinon.createStubInstance(PublicKeyVerifier);
2014-10-07 14:32:23 -04:00
keyId = '9FEB47936E712926';
2014-11-25 12:19:40 -05:00
keychainMock = sinon.createStubInstance(KeychainDAO);
pgpMock = sinon.createStubInstance(PGP);
2014-10-07 14:32:23 -04:00
pgpMock.extractPublicKey.returns('publicKeyArmored');
2014-11-25 12:19:40 -05:00
authMock.emailAddress = emailAddress;
2014-10-07 14:32:23 -04:00
2014-11-25 12:19:40 -05:00
angular.module('loginnewdevicetest', ['woServices']);
angular.mock.module('loginnewdevicetest');
2015-02-20 11:55:11 -05:00
angular.mock.inject(function($rootScope, $location, $controller) {
2014-10-07 14:32:23 -04:00
scope = $rootScope.$new();
2015-02-20 11:55:11 -05:00
location = $location;
2014-10-07 14:32:23 -04:00
scope.state = {
ui: {}
};
scope.form = {};
2014-10-07 14:32:23 -04:00
ctrl = $controller(LoginNewDeviceCtrl, {
$scope: scope,
2014-11-25 12:19:40 -05:00
$routeParams: {},
2014-12-17 16:41:57 -05:00
$q: window.qMock,
2014-11-25 12:19:40 -05:00
email: emailMock,
auth: authMock,
pgp: pgpMock,
2015-02-20 11:55:11 -05:00
publickeyVerifier: verifierMock,
2014-11-25 12:19:40 -05:00
keychain: keychainMock
});
});
2014-10-07 14:32:23 -04:00
});
2014-11-25 12:19:40 -05:00
afterEach(function() {});
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;
});
2014-10-07 14:32:23 -04:00
});
describe('pasteKey', function() {
it('should work', function() {
var keyStr = '-----BEGIN PGP PRIVATE KEY BLOCK----- asdf -----END PGP PRIVATE KEY BLOCK-----';
scope.pasteKey(keyStr);
expect(scope.key.privateKeyArmored).to.equal(keyStr);
});
});
2014-10-07 14:32:23 -04:00
describe('confirm passphrase', function() {
2014-12-17 16:41:57 -05:00
it('should unlock crypto with a public key on the server', function(done) {
2014-10-07 14:32:23 -04:00
scope.passphrase = passphrase;
scope.key = {
privateKeyArmored: 'b'
};
pgpMock.getKeyParams.returns({
_id: 'id',
userIds: []
});
2014-12-17 16:41:57 -05:00
keychainMock.getUserKeyPair.withArgs(emailAddress).returns(resolves({
2014-10-07 14:32:23 -04:00
_id: keyId,
publicKey: 'a'
2014-12-17 16:41:57 -05:00
}));
2015-02-20 11:55:11 -05:00
emailMock.unlock.returns(resolves('asd'));
2014-12-17 16:41:57 -05:00
keychainMock.putUserKeyPair.returns(resolves());
scope.confirmPassphrase().then(function() {
expect(emailMock.unlock.calledOnce).to.be.true;
expect(keychainMock.getUserKeyPair.calledOnce).to.be.true;
2015-02-20 11:55:11 -05:00
expect(location.$$path).to.equal('/account');
2014-12-17 16:41:57 -05:00
done();
});
});
2014-12-17 16:41:57 -05:00
it('should unlock crypto with no key on the server', function(done) {
2014-10-07 14:32:23 -04:00
scope.passphrase = passphrase;
scope.key = {
privateKeyArmored: 'b',
publicKeyArmored: 'a'
};
pgpMock.getKeyParams.returns({
_id: 'id',
userIds: []
});
2014-12-17 16:41:57 -05:00
keychainMock.getUserKeyPair.withArgs(emailAddress).returns(resolves());
2015-02-20 11:55:11 -05:00
keychainMock.uploadPublicKey.returns(resolves());
emailMock.unlock.returns(resolves('asd'));
2014-12-17 16:41:57 -05:00
keychainMock.putUserKeyPair.returns(resolves());
2014-10-07 14:32:23 -04:00
2014-12-17 16:41:57 -05:00
scope.confirmPassphrase().then(function() {
expect(emailMock.unlock.calledOnce).to.be.true;
expect(keychainMock.getUserKeyPair.calledOnce).to.be.true;
expect(location.$$path).to.equal('/login-privatekey-upload');
2014-12-17 16:41:57 -05:00
done();
});
2014-10-07 14:32:23 -04:00
});
2014-12-17 16:41:57 -05:00
it('should not work when keypair upload fails', function(done) {
2014-10-07 14:32:23 -04:00
scope.passphrase = passphrase;
scope.key = {
privateKeyArmored: 'b'
};
2014-10-07 14:32:23 -04:00
pgpMock.getKeyParams.returns({
_id: 'id',
userIds: []
});
2014-12-17 16:41:57 -05:00
keychainMock.getUserKeyPair.withArgs(emailAddress).returns(resolves({
2014-10-07 14:32:23 -04:00
_id: keyId,
publicKey: 'a'
2014-12-17 16:41:57 -05:00
}));
emailMock.unlock.returns(resolves());
keychainMock.putUserKeyPair.returns(rejects({
2014-10-07 14:32:23 -04:00
errMsg: 'yo mamma.'
2014-12-17 16:41:57 -05:00
}));
scope.confirmPassphrase().then(function() {
expect(keychainMock.getUserKeyPair.calledOnce).to.be.true;
expect(emailMock.unlock.calledOnce).to.be.true;
expect(keychainMock.putUserKeyPair.calledOnce).to.be.true;
expect(scope.errMsg).to.equal('yo mamma.');
done();
});
2014-10-07 14:32:23 -04:00
});
2014-12-17 16:41:57 -05:00
it('should not work when unlock fails', function(done) {
2014-10-07 14:32:23 -04:00
scope.passphrase = passphrase;
scope.key = {
privateKeyArmored: 'b'
};
pgpMock.getKeyParams.returns({
_id: 'id',
userIds: []
});
2014-12-17 16:41:57 -05:00
keychainMock.getUserKeyPair.withArgs(emailAddress).returns(resolves({
2014-10-07 14:32:23 -04:00
_id: keyId,
publicKey: 'a'
2014-12-17 16:41:57 -05:00
}));
emailMock.unlock.returns(rejects({
2014-10-07 14:32:23 -04:00
errMsg: 'yo mamma.'
2014-12-17 16:41:57 -05:00
}));
scope.confirmPassphrase().then(function() {
expect(scope.incorrect).to.be.true;
expect(keychainMock.getUserKeyPair.calledOnce).to.be.true;
expect(emailMock.unlock.calledOnce).to.be.true;
expect(scope.errMsg).to.equal('yo mamma.');
done();
});
2014-10-07 14:32:23 -04:00
});
2014-04-24 10:45:09 -04:00
2014-12-17 16:41:57 -05:00
it('should not work when keypair retrieval', function(done) {
2014-10-07 14:32:23 -04:00
scope.passphrase = passphrase;
scope.key = {
privateKeyArmored: 'b'
};
2014-12-17 16:41:57 -05:00
keychainMock.getUserKeyPair.withArgs(emailAddress).returns(rejects({
2014-10-07 14:32:23 -04:00
errMsg: 'yo mamma.'
2014-12-17 16:41:57 -05:00
}));
2014-10-07 14:32:23 -04:00
2014-12-17 16:41:57 -05:00
scope.confirmPassphrase().then(function() {
expect(keychainMock.getUserKeyPair.calledOnce).to.be.true;
expect(scope.errMsg).to.equal('yo mamma.');
done();
});
});
});
});