2014-10-07 14:32:23 -04:00
|
|
|
'use strict';
|
|
|
|
|
2014-11-25 12:19:40 -05:00
|
|
|
var PrivateKeyUploadCtrl = require('../../../../src/js/controller/app/privatekey-upload'),
|
|
|
|
KeychainDAO = require('../../../../src/js/service/keychain'),
|
|
|
|
PGP = require('../../../../src/js/crypto/pgp'),
|
|
|
|
Dialog = require('../../../../src/js/util/dialog');
|
2014-10-07 14:32:23 -04:00
|
|
|
|
|
|
|
describe('Private Key Upload Controller unit test', function() {
|
|
|
|
var scope, location, ctrl,
|
2014-11-25 12:19:40 -05:00
|
|
|
keychainMock, pgpStub, dialogStub,
|
2014-10-07 14:32:23 -04:00
|
|
|
emailAddress = 'fred@foo.com';
|
|
|
|
|
2014-11-25 12:19:40 -05:00
|
|
|
beforeEach(function() {
|
|
|
|
keychainMock = sinon.createStubInstance(KeychainDAO);
|
|
|
|
pgpStub = sinon.createStubInstance(PGP);
|
|
|
|
dialogStub = sinon.createStubInstance(Dialog);
|
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) {
|
2014-10-07 14:32:23 -04:00
|
|
|
scope = $rootScope.$new();
|
|
|
|
scope.state = {};
|
|
|
|
ctrl = $controller(PrivateKeyUploadCtrl, {
|
|
|
|
$location: location,
|
2014-11-25 12:19:40 -05:00
|
|
|
$scope: scope,
|
|
|
|
keychain: keychainMock,
|
|
|
|
pgp: pgpStub,
|
|
|
|
dialog: dialogStub,
|
|
|
|
auth: {
|
|
|
|
emailAddress: emailAddress
|
|
|
|
}
|
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-10-07 14:32:23 -04:00
|
|
|
|
|
|
|
describe('checkServerForKey', function() {
|
|
|
|
var keyParams = {
|
|
|
|
userId: emailAddress,
|
|
|
|
_id: 'keyId',
|
|
|
|
};
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-11-25 12:19:40 -05:00
|
|
|
it('should fail', function() {
|
2014-10-07 14:32:23 -04:00
|
|
|
pgpStub.getKeyParams.returns(keyParams);
|
|
|
|
keychainMock.hasPrivateKey.yields(42);
|
|
|
|
|
|
|
|
scope.checkServerForKey();
|
2014-11-25 12:19:40 -05:00
|
|
|
|
|
|
|
expect(dialogStub.error.calledOnce).to.be.true;
|
|
|
|
expect(keychainMock.hasPrivateKey.calledOnce).to.be.true;
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should return true', function(done) {
|
|
|
|
pgpStub.getKeyParams.returns(keyParams);
|
|
|
|
keychainMock.hasPrivateKey.withArgs({
|
|
|
|
userId: keyParams.userId,
|
|
|
|
keyId: keyParams._id
|
|
|
|
}).yields(null, true);
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
scope.checkServerForKey(function(privateKeySynced) {
|
|
|
|
expect(privateKeySynced).to.be.true;
|
|
|
|
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-10-07 14:32:23 -04:00
|
|
|
it('should return undefined', function(done) {
|
|
|
|
pgpStub.getKeyParams.returns(keyParams);
|
|
|
|
keychainMock.hasPrivateKey.withArgs({
|
|
|
|
userId: keyParams.userId,
|
|
|
|
keyId: keyParams._id
|
|
|
|
}).yields(null, false);
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
scope.checkServerForKey(function(privateKeySynced) {
|
|
|
|
expect(privateKeySynced).to.be.undefined;
|
|
|
|
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-10-07 14:32:23 -04:00
|
|
|
describe('displayUploadUi', function() {
|
|
|
|
it('should work', function() {
|
|
|
|
// add some artifacts from a previous key input
|
2014-12-01 07:25:25 -05:00
|
|
|
scope.inputCode = 'asdasd';
|
2014-10-07 14:32:23 -04:00
|
|
|
|
|
|
|
scope.displayUploadUi();
|
|
|
|
expect(scope.step).to.equal(1);
|
|
|
|
expect(scope.code.length).to.equal(24);
|
|
|
|
|
|
|
|
// artifacts should be cleared
|
2014-12-01 07:25:25 -05:00
|
|
|
expect(scope.inputCode).to.be.empty;
|
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('verifyCode', function() {
|
|
|
|
it('should fail for wrong code', function() {
|
2014-12-01 07:25:25 -05:00
|
|
|
scope.inputCode = 'bbbbbb';
|
|
|
|
scope.code = 'AAAAAA';
|
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(scope.verifyCode()).to.be.false;
|
|
|
|
});
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work', function() {
|
2014-12-01 07:25:25 -05:00
|
|
|
scope.inputCode = 'aaAaaa';
|
|
|
|
scope.code = 'AAAAAA';
|
|
|
|
|
|
|
|
expect(scope.verifyCode()).to.be.true;
|
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('setDeviceName', function() {
|
|
|
|
it('should work', function(done) {
|
|
|
|
keychainMock.setDeviceName.yields();
|
|
|
|
scope.setDeviceName(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-10-07 14:32:23 -04:00
|
|
|
describe('encryptAndUploadKey', function() {
|
2014-11-25 12:19:40 -05:00
|
|
|
it('should fail due to keychain.registerDevice', function() {
|
2014-10-07 14:32:23 -04:00
|
|
|
keychainMock.registerDevice.yields(42);
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
scope.encryptAndUploadKey();
|
2014-11-25 12:19:40 -05:00
|
|
|
|
|
|
|
expect(dialogStub.error.calledOnce).to.be.true;
|
|
|
|
expect(keychainMock.registerDevice.calledOnce).to.be.true;
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work', function(done) {
|
|
|
|
keychainMock.registerDevice.yields();
|
|
|
|
keychainMock.uploadPrivateKey.yields();
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
scope.encryptAndUploadKey(function(err) {
|
|
|
|
expect(err).to.not.exist;
|
|
|
|
expect(keychainMock.registerDevice.calledOnce).to.be.true;
|
|
|
|
expect(keychainMock.uploadPrivateKey.calledOnce).to.be.true;
|
|
|
|
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-10-07 14:32:23 -04:00
|
|
|
describe('goBack', function() {
|
|
|
|
it('should work', function() {
|
|
|
|
scope.step = 2;
|
|
|
|
scope.goBack();
|
|
|
|
expect(scope.step).to.equal(1);
|
|
|
|
});
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should not work for < 2', function() {
|
|
|
|
scope.step = 1;
|
|
|
|
scope.goBack();
|
|
|
|
expect(scope.step).to.equal(1);
|
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('goForward', function() {
|
|
|
|
var verifyCodeStub, setDeviceNameStub, encryptAndUploadKeyStub;
|
|
|
|
beforeEach(function() {
|
|
|
|
verifyCodeStub = sinon.stub(scope, 'verifyCode');
|
|
|
|
setDeviceNameStub = sinon.stub(scope, 'setDeviceName');
|
|
|
|
encryptAndUploadKeyStub = sinon.stub(scope, 'encryptAndUploadKey');
|
|
|
|
});
|
|
|
|
afterEach(function() {
|
|
|
|
verifyCodeStub.restore();
|
|
|
|
setDeviceNameStub.restore();
|
|
|
|
encryptAndUploadKeyStub.restore();
|
|
|
|
});
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work for < 2', function() {
|
|
|
|
scope.step = 1;
|
|
|
|
scope.goForward();
|
|
|
|
expect(scope.step).to.equal(2);
|
|
|
|
});
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work for 2', function() {
|
|
|
|
verifyCodeStub.returns(true);
|
|
|
|
scope.step = 2;
|
|
|
|
scope.goForward();
|
|
|
|
expect(scope.step).to.equal(3);
|
|
|
|
});
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should not work for 2 when code invalid', function() {
|
|
|
|
verifyCodeStub.returns(false);
|
|
|
|
scope.step = 2;
|
|
|
|
scope.goForward();
|
|
|
|
expect(scope.step).to.equal(2);
|
|
|
|
});
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-11-25 12:19:40 -05:00
|
|
|
it('should fail for 3 due to error in setDeviceName', function() {
|
2014-10-07 14:32:23 -04:00
|
|
|
scope.step = 3;
|
|
|
|
setDeviceNameStub.yields(42);
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
scope.goForward();
|
2014-11-25 12:19:40 -05:00
|
|
|
|
|
|
|
expect(dialogStub.error.calledOnce).to.be.true;
|
|
|
|
expect(scope.step).to.equal(3);
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-11-25 12:19:40 -05:00
|
|
|
it('should fail for 3 due to error in encryptAndUploadKey', function() {
|
2014-10-07 14:32:23 -04:00
|
|
|
scope.step = 3;
|
|
|
|
setDeviceNameStub.yields();
|
|
|
|
encryptAndUploadKeyStub.yields(42);
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
scope.goForward();
|
2014-11-25 12:19:40 -05:00
|
|
|
|
|
|
|
expect(dialogStub.error.calledOnce).to.be.true;
|
|
|
|
expect(scope.step).to.equal(4);
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-11-25 12:19:40 -05:00
|
|
|
it('should work for 3', function() {
|
2014-10-07 14:32:23 -04:00
|
|
|
scope.step = 3;
|
|
|
|
setDeviceNameStub.yields();
|
|
|
|
encryptAndUploadKeyStub.yields();
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
scope.goForward();
|
2014-11-25 12:19:40 -05:00
|
|
|
|
|
|
|
expect(dialogStub.info.calledOnce).to.be.true;
|
|
|
|
expect(scope.step).to.equal(4);
|
2014-06-13 06:33:30 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|