1
0
mirror of https://github.com/moparisthebest/mail synced 2024-08-13 16:43:47 -04:00
mail/test/unit/controller/login/login-privatekey-download-ctrl-test.js

126 lines
4.5 KiB
JavaScript
Raw Permalink Normal View History

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'),
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,
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);
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 = {};
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,
keychain: keychainMock,
privateKey: privateKeyStub
});
2014-10-07 14:32:23 -04:00
done();
});
2014-10-07 14:32:23 -04:00
});
2014-11-25 12:19:40 -05:00
afterEach(function() {});
describe('checkCode', function() {
var encryptedPrivateKey = {
encryptedPrivateKey: 'encryptedPrivateKey'
};
var cachedKeypair = {
2014-12-18 09:19:06 -05:00
publicKey: {
_id: 'keyId'
2014-12-18 09:19:06 -05:00
}
};
var privkey = {
_id: cachedKeypair.publicKey._id,
userId: emailAddress,
encryptedKey: 'PRIVATE PGP BLOCK'
};
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();
});
it('should fail on privateKey.init', function(done) {
privateKeyStub.init.returns(rejects(new Error('asdf')));
2014-12-18 09:19:06 -05:00
scope.checkCode().then(function() {
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
});
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-12-18 09:19:06 -05:00
scope.checkCode().then(function() {
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
});
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());
privateKeyStub.destroy.returns(resolves());
2014-10-07 14:32:23 -04:00
2014-12-18 09:19:06 -05:00
scope.checkCode().then(function() {
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-10-07 14:32:23 -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) {
expect(path).to.equal('/account');
});
2014-10-07 14:32:23 -04:00
scope.goTo('/account');
});
});
});