2015-02-20 11:55:11 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var Auth = require('../../../../src/js/service/auth'),
|
|
|
|
Dialog = require('../../../../src/js/util/dialog'),
|
|
|
|
PublicKeyVerifier = require('../../../../src/js/service/publickey-verifier'),
|
2015-02-27 14:04:44 -05:00
|
|
|
PublicKey = require('../../../../src/js/service/publickey'),
|
2015-02-20 11:55:11 -05:00
|
|
|
PublicKeyVerifierCtrl = require('../../../../src/js/controller/login/login-verify-public-key');
|
|
|
|
|
|
|
|
describe('Public Key Verification Controller unit test', function() {
|
|
|
|
// Angular parameters
|
|
|
|
var scope, location;
|
|
|
|
|
|
|
|
// Stubs & Fixture
|
2015-02-27 14:04:44 -05:00
|
|
|
var auth, verifier, dialogStub, publicKeyStub;
|
2015-02-20 11:55:11 -05:00
|
|
|
var emailAddress = 'foo@foo.com';
|
|
|
|
|
|
|
|
// SUT
|
|
|
|
var verificationCtrl;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
// remeber pre-test state to restore later
|
|
|
|
auth = sinon.createStubInstance(Auth);
|
|
|
|
verifier = sinon.createStubInstance(PublicKeyVerifier);
|
|
|
|
dialogStub = sinon.createStubInstance(Dialog);
|
2015-02-27 14:04:44 -05:00
|
|
|
publicKeyStub = sinon.createStubInstance(PublicKey);
|
2015-02-20 11:55:11 -05:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
verifier.uploadPublicKey.returns(resolves());
|
2015-02-20 11:55:11 -05:00
|
|
|
auth.emailAddress = emailAddress;
|
|
|
|
|
|
|
|
// setup the controller
|
|
|
|
angular.module('publickeyverificationtest', []);
|
|
|
|
angular.mock.module('publickeyverificationtest');
|
|
|
|
angular.mock.inject(function($rootScope, $controller, $location) {
|
|
|
|
scope = $rootScope.$new();
|
|
|
|
location = $location;
|
|
|
|
|
|
|
|
verificationCtrl = $controller(PublicKeyVerifierCtrl, {
|
|
|
|
$scope: scope,
|
|
|
|
$q: window.qMock,
|
|
|
|
auth: auth,
|
|
|
|
publickeyVerifier: verifier,
|
|
|
|
dialog: dialogStub,
|
2015-02-27 14:04:44 -05:00
|
|
|
publicKey: publicKeyStub,
|
2015-02-20 11:55:11 -05:00
|
|
|
appConfig: {
|
|
|
|
string: {
|
|
|
|
publickeyVerificationSkipTitle: 'foo',
|
|
|
|
publickeyVerificationSkipMessage: 'bar'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {});
|
|
|
|
|
|
|
|
describe('#verify', function() {
|
|
|
|
it('should verify', function(done) {
|
|
|
|
var credentials = {};
|
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
publicKeyStub.getByUserId.withArgs(emailAddress).returns(resolves());
|
2015-02-20 11:55:11 -05:00
|
|
|
auth.getCredentials.returns(resolves(credentials));
|
|
|
|
verifier.configure.withArgs(credentials).returns(resolves());
|
|
|
|
verifier.verify.withArgs().returns(resolves());
|
|
|
|
verifier.persistKeypair.returns(resolves());
|
|
|
|
|
|
|
|
scope.verify().then(function() {
|
2015-02-27 14:04:44 -05:00
|
|
|
expect(publicKeyStub.getByUserId.calledOnce).to.be.true;
|
2015-02-20 11:55:11 -05:00
|
|
|
expect(auth.getCredentials.calledOnce).to.be.true;
|
|
|
|
expect(verifier.configure.calledOnce).to.be.true;
|
|
|
|
expect(verifier.verify.calledOnce).to.be.true;
|
|
|
|
expect(verifier.persistKeypair.calledOnce).to.be.true;
|
|
|
|
expect(location.$$path).to.equal('/account');
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should skip verification when key is already verified', function(done) {
|
2015-02-27 14:04:44 -05:00
|
|
|
publicKeyStub.getByUserId.withArgs(emailAddress).returns(resolves({
|
2015-02-20 11:55:11 -05:00
|
|
|
publicKey: {}
|
|
|
|
}));
|
|
|
|
|
|
|
|
scope.verify().then(function() {
|
2015-02-27 14:04:44 -05:00
|
|
|
expect(publicKeyStub.getByUserId.calledOnce).to.be.true;
|
2015-02-20 11:55:11 -05:00
|
|
|
expect(auth.getCredentials.called).to.be.false;
|
|
|
|
expect(verifier.configure.called).to.be.false;
|
|
|
|
expect(verifier.verify.called).to.be.false;
|
|
|
|
expect(location.$$path).to.equal('/account');
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not verify', function(done) {
|
|
|
|
var credentials = {};
|
|
|
|
|
2015-04-14 12:46:08 -04:00
|
|
|
auth.getCredentials.returns(resolves(credentials));
|
|
|
|
verifier.configure.withArgs(credentials).returns(resolves());
|
|
|
|
verifier.verify.withArgs().returns(rejects(new Error()));
|
|
|
|
|
|
|
|
scope.verify().then(function() {
|
|
|
|
expect(auth.getCredentials.calledOnce).to.be.true;
|
|
|
|
expect(verifier.configure.calledOnce).to.be.true;
|
|
|
|
expect(verifier.verify.calledOnce).to.be.true;
|
|
|
|
expect(scope.errMsg).to.equal('');
|
|
|
|
|
|
|
|
clearTimeout(scope.timeout);
|
|
|
|
clearInterval(scope.countdownDecrement);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not verify with error message', function(done) {
|
|
|
|
var credentials = {};
|
|
|
|
|
2015-02-20 11:55:11 -05:00
|
|
|
auth.getCredentials.returns(resolves(credentials));
|
|
|
|
verifier.configure.withArgs(credentials).returns(resolves());
|
|
|
|
verifier.verify.withArgs().returns(rejects(new Error('foo')));
|
|
|
|
|
|
|
|
scope.verify().then(function() {
|
|
|
|
expect(auth.getCredentials.calledOnce).to.be.true;
|
|
|
|
expect(verifier.configure.calledOnce).to.be.true;
|
|
|
|
expect(verifier.verify.calledOnce).to.be.true;
|
|
|
|
expect(scope.errMsg).to.equal('foo');
|
|
|
|
|
|
|
|
clearTimeout(scope.timeout);
|
|
|
|
clearInterval(scope.countdownDecrement);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|