2014-10-07 14:32:23 -04:00
|
|
|
'use strict';
|
|
|
|
|
2014-11-25 12:19:40 -05:00
|
|
|
var Auth = require('../../../../src/js/service/auth'),
|
|
|
|
LoginPrivateKeyDownloadCtrl = require('../../../../src/js/controller/login/login-privatekey-download'),
|
|
|
|
Email = require('../../../../src/js/email/email'),
|
2015-02-27 14:04:44 -05:00
|
|
|
Keychain = require('../../../../src/js/service/keychain'),
|
|
|
|
PrivateKey = require('../../../../src/js/service/privatekey');
|
2014-10-07 14:32:23 -04:00
|
|
|
|
|
|
|
describe('Login Private Key Download Controller unit test', function() {
|
|
|
|
var scope, location, ctrl,
|
2015-02-27 14:04:44 -05:00
|
|
|
emailDaoMock, authMock, keychainMock, privateKeyStub,
|
2014-10-07 14:32:23 -04:00
|
|
|
emailAddress = 'fred@foo.com';
|
|
|
|
|
|
|
|
beforeEach(function(done) {
|
2014-11-25 12:19:40 -05:00
|
|
|
emailDaoMock = sinon.createStubInstance(Email);
|
|
|
|
keychainMock = sinon.createStubInstance(Keychain);
|
2015-02-27 14:04:44 -05:00
|
|
|
privateKeyStub = sinon.createStubInstance(PrivateKey);
|
2014-11-25 12:19:40 -05:00
|
|
|
authMock = sinon.createStubInstance(Auth);
|
2014-10-07 14:32:23 -04:00
|
|
|
|
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('login-privatekey-download-test', ['woServices']);
|
|
|
|
angular.mock.module('login-privatekey-download-test');
|
|
|
|
angular.mock.inject(function($controller, $rootScope, $location) {
|
2014-10-07 14:32:23 -04:00
|
|
|
scope = $rootScope.$new();
|
|
|
|
scope.state = {};
|
2015-02-27 14:04:44 -05:00
|
|
|
scope.form = {};
|
2014-11-25 12:19:40 -05:00
|
|
|
location = $location;
|
2014-10-07 14:32:23 -04:00
|
|
|
ctrl = $controller(LoginPrivateKeyDownloadCtrl, {
|
|
|
|
$location: location,
|
|
|
|
$scope: scope,
|
2014-11-25 12:19:40 -05:00
|
|
|
$routeParams: {},
|
2014-12-18 09:19:06 -05:00
|
|
|
$q: window.qMock,
|
2014-11-25 12:19:40 -05:00
|
|
|
auth: authMock,
|
|
|
|
email: emailDaoMock,
|
2015-02-27 14:04:44 -05:00
|
|
|
keychain: keychainMock,
|
|
|
|
privateKey: privateKeyStub
|
2014-06-13 06:33:30 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
done();
|
2014-06-13 06:33:30 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-11-25 12:19:40 -05:00
|
|
|
afterEach(function() {});
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
describe('checkCode', function() {
|
|
|
|
var encryptedPrivateKey = {
|
|
|
|
encryptedPrivateKey: 'encryptedPrivateKey'
|
|
|
|
};
|
|
|
|
var cachedKeypair = {
|
2014-12-18 09:19:06 -05:00
|
|
|
publicKey: {
|
2015-02-27 14:04:44 -05:00
|
|
|
_id: 'keyId'
|
2014-12-18 09:19:06 -05:00
|
|
|
}
|
|
|
|
};
|
2015-02-27 14:04:44 -05:00
|
|
|
var privkey = {
|
|
|
|
_id: cachedKeypair.publicKey._id,
|
|
|
|
userId: emailAddress,
|
|
|
|
encryptedKey: 'PRIVATE PGP BLOCK'
|
|
|
|
};
|
2014-11-12 10:12:26 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
beforeEach(function() {
|
2014-12-01 07:25:25 -05:00
|
|
|
scope.code = '012345';
|
2014-10-07 14:32:23 -04:00
|
|
|
|
2014-12-18 09:19:06 -05:00
|
|
|
sinon.stub(scope, 'goTo');
|
|
|
|
});
|
|
|
|
afterEach(function() {
|
|
|
|
scope.goTo.restore();
|
|
|
|
});
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
it('should fail on privateKey.init', function(done) {
|
|
|
|
privateKeyStub.init.returns(rejects(new Error('asdf')));
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-12-18 09:19:06 -05:00
|
|
|
scope.checkCode().then(function() {
|
2015-02-27 14:04:44 -05:00
|
|
|
expect(scope.errMsg).to.match(/asdf/);
|
|
|
|
expect(privateKeyStub.init.calledOnce).to.be.true;
|
2014-12-18 09:19:06 -05:00
|
|
|
done();
|
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
it('should work with empty passphrase', function(done) {
|
|
|
|
privateKeyStub.init.returns(resolves());
|
|
|
|
keychainMock.getUserKeyPair.withArgs(emailAddress).returns(resolves(cachedKeypair));
|
|
|
|
privateKeyStub.download.withArgs({
|
|
|
|
userId: emailAddress,
|
|
|
|
keyId: cachedKeypair.publicKey._id
|
|
|
|
}).returns(resolves(encryptedPrivateKey));
|
|
|
|
privateKeyStub.decrypt.returns(resolves(privkey));
|
|
|
|
emailDaoMock.unlock.returns(resolves());
|
|
|
|
authMock.storeCredentials.returns(resolves());
|
|
|
|
privateKeyStub.destroy.returns(resolves());
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-12-18 09:19:06 -05:00
|
|
|
scope.checkCode().then(function() {
|
2015-02-27 14:04:44 -05:00
|
|
|
expect(scope.errMsg).to.not.exist;
|
|
|
|
expect(scope.goTo.withArgs('/account').calledOnce).to.be.true;
|
2014-10-07 14:32:23 -04:00
|
|
|
done();
|
2014-12-18 09:19:06 -05:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
it('should work with passphrase', function(done) {
|
|
|
|
privateKeyStub.init.returns(resolves());
|
|
|
|
keychainMock.getUserKeyPair.withArgs(emailAddress).returns(resolves(cachedKeypair));
|
|
|
|
privateKeyStub.download.withArgs({
|
|
|
|
userId: emailAddress,
|
|
|
|
keyId: cachedKeypair.publicKey._id
|
|
|
|
}).returns(resolves(encryptedPrivateKey));
|
|
|
|
privateKeyStub.decrypt.returns(resolves(privkey));
|
2015-04-01 08:24:46 -04:00
|
|
|
emailDaoMock.unlock.returns(rejects(new Error()));
|
2014-12-18 09:19:06 -05:00
|
|
|
authMock.storeCredentials.returns(resolves());
|
2015-02-27 14:04:44 -05:00
|
|
|
privateKeyStub.destroy.returns(resolves());
|
2014-10-07 14:32:23 -04:00
|
|
|
|
2014-12-18 09:19:06 -05:00
|
|
|
scope.checkCode().then(function() {
|
2015-02-27 14:04:44 -05:00
|
|
|
expect(scope.goTo.withArgs('/login-existing').calledOnce).to.be.true;
|
2014-10-07 14:32:23 -04:00
|
|
|
done();
|
2014-12-18 09:19:06 -05:00
|
|
|
});
|
2014-06-13 06:33:30 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('goTo', function() {
|
2014-11-25 12:19:40 -05:00
|
|
|
it('should work', function() {
|
|
|
|
sinon.stub(location, 'path', function(path) {
|
2014-12-04 10:24:42 -05:00
|
|
|
expect(path).to.equal('/account');
|
2014-06-13 06:33:30 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
|
2014-12-04 10:24:42 -05:00
|
|
|
scope.goTo('/account');
|
2014-06-13 06:33:30 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|