2014-10-07 14:32:23 -04:00
|
|
|
'use strict';
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-11-21 09:06:29 -05:00
|
|
|
var LawnchairDAO = require('../../../src/js/service/lawnchair'),
|
|
|
|
PublicKeyDAO = require('../../../src/js/service/publickey'),
|
|
|
|
KeychainDAO = require('../../../src/js/service/keychain'),
|
|
|
|
PrivateKeyDAO = require('../../../src/js/service/privatekey'),
|
|
|
|
Crypto = require('../../../src/js/crypto/crypto'),
|
|
|
|
PGP = require('../../../src/js/crypto/pgp'),
|
|
|
|
Dialog = require('../../../src/js/util/dialog'),
|
|
|
|
appConfig = require('../../../src/js/app-config');
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
var testUser = 'test@example.com';
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('Keychain DAO unit tests', function() {
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-11-21 09:06:29 -05:00
|
|
|
var keychainDao, lawnchairDaoStub, pubkeyDaoStub, privkeyDaoStub, cryptoStub, pgpStub, dialogStub;
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
beforeEach(function() {
|
|
|
|
lawnchairDaoStub = sinon.createStubInstance(LawnchairDAO);
|
|
|
|
pubkeyDaoStub = sinon.createStubInstance(PublicKeyDAO);
|
|
|
|
privkeyDaoStub = sinon.createStubInstance(PrivateKeyDAO);
|
|
|
|
cryptoStub = sinon.createStubInstance(Crypto);
|
|
|
|
pgpStub = sinon.createStubInstance(PGP);
|
2014-11-21 09:06:29 -05:00
|
|
|
dialogStub = sinon.createStubInstance(Dialog);
|
|
|
|
keychainDao = new KeychainDAO(lawnchairDaoStub, pubkeyDaoStub, privkeyDaoStub, cryptoStub, pgpStub, dialogStub, appConfig);
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
afterEach(function() {});
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-11-21 09:06:29 -05:00
|
|
|
describe('requestPermissionForKeyUpdate', function() {
|
|
|
|
it('should work', function() {
|
|
|
|
var opt = {
|
|
|
|
newKey: {},
|
|
|
|
userId: 'asdf@example.com'
|
|
|
|
};
|
|
|
|
|
|
|
|
keychainDao.requestPermissionForKeyUpdate(opt, function() {
|
|
|
|
expect(dialogStub.confirm.calledOnce).to.be.true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('verify public key', function() {
|
|
|
|
it('should verify public key', function(done) {
|
|
|
|
var uuid = 'asdfasdfasdfasdf';
|
2014-12-12 12:02:13 -05:00
|
|
|
pubkeyDaoStub.verify.returns(resolves());
|
2013-11-08 03:29:04 -05:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.verifyPublicKey(uuid).then(function() {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(pubkeyDaoStub.verify.calledWith(uuid)).to.be.true;
|
|
|
|
done();
|
2013-11-08 03:29:04 -05:00
|
|
|
});
|
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2013-11-08 03:29:04 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('listLocalPublicKeys', function() {
|
|
|
|
it('should work', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
lawnchairDaoStub.list.withArgs('publickey', 0, null).returns(resolves());
|
2014-03-06 12:19:51 -05:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.listLocalPublicKeys().then(function() {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(lawnchairDaoStub.list.callCount).to.equal(1);
|
|
|
|
done();
|
2014-03-06 12:19:51 -05:00
|
|
|
});
|
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-03-06 12:19:51 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('removeLocalPublicKey', function() {
|
|
|
|
it('should work', function(done) {
|
|
|
|
var id = 'asdf';
|
2014-03-06 12:19:51 -05:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
lawnchairDaoStub.remove.withArgs('publickey_' + id).returns(resolves());
|
2014-03-06 12:19:51 -05:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.removeLocalPublicKey(id).then(function() {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(lawnchairDaoStub.remove.callCount).to.equal(1);
|
|
|
|
done();
|
2014-03-06 12:19:51 -05:00
|
|
|
});
|
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-03-06 12:19:51 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('refreshKeyForUserId', function() {
|
|
|
|
var getPubKeyStub,
|
|
|
|
oldKey = {
|
|
|
|
_id: 123
|
|
|
|
},
|
|
|
|
newKey = {
|
|
|
|
_id: 456
|
|
|
|
},
|
|
|
|
importedKey = {
|
|
|
|
_id: 789,
|
|
|
|
imported: true
|
|
|
|
};
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
beforeEach(function() {
|
|
|
|
getPubKeyStub = sinon.stub(keychainDao, 'getReceiverPublicKey');
|
|
|
|
});
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
afterEach(function() {
|
|
|
|
keychainDao.getReceiverPublicKey.restore();
|
|
|
|
delete keychainDao.requestPermissionForKeyUpdate;
|
|
|
|
});
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should not find a key', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
getPubKeyStub.returns(resolves());
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-11-06 08:28:14 -05:00
|
|
|
keychainDao.refreshKeyForUserId({
|
|
|
|
userId: testUser
|
2014-12-12 12:02:13 -05:00
|
|
|
}).then(function(key) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(key).to.not.exist;
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
done();
|
2014-05-23 04:52:34 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should not update the key when up to date', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
getPubKeyStub.returns(resolves(oldKey));
|
2015-01-28 09:01:18 -05:00
|
|
|
pubkeyDaoStub.getByUserId.withArgs(testUser).returns(resolves(oldKey));
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-11-06 08:28:14 -05:00
|
|
|
keychainDao.refreshKeyForUserId({
|
|
|
|
userId: testUser
|
2014-12-12 12:02:13 -05:00
|
|
|
}).then(function(key) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(key).to.to.equal(oldKey);
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(getPubKeyStub.calledOnce).to.be.true;
|
2015-01-28 09:01:18 -05:00
|
|
|
expect(pubkeyDaoStub.getByUserId.calledOnce).to.be.true;
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
done();
|
2014-05-23 04:52:34 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should update key', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
getPubKeyStub.returns(resolves(oldKey));
|
|
|
|
pubkeyDaoStub.getByUserId.withArgs(testUser).returns(resolves(newKey));
|
2014-10-07 14:32:23 -04:00
|
|
|
keychainDao.requestPermissionForKeyUpdate = function(opts, cb) {
|
|
|
|
expect(opts.userId).to.equal(testUser);
|
|
|
|
expect(opts.newKey).to.equal(newKey);
|
|
|
|
cb(true);
|
|
|
|
};
|
2014-12-12 12:02:13 -05:00
|
|
|
lawnchairDaoStub.remove.withArgs('publickey_' + oldKey._id).returns(resolves());
|
|
|
|
lawnchairDaoStub.persist.withArgs('publickey_' + newKey._id, newKey).returns(resolves());
|
2014-10-07 14:32:23 -04:00
|
|
|
|
2014-11-06 08:28:14 -05:00
|
|
|
keychainDao.refreshKeyForUserId({
|
|
|
|
userId: testUser
|
2014-12-12 12:02:13 -05:00
|
|
|
}).then(function(key) {
|
2014-11-06 08:28:14 -05:00
|
|
|
expect(key).to.equal(newKey);
|
|
|
|
|
|
|
|
expect(getPubKeyStub.calledOnce).to.be.true;
|
|
|
|
expect(pubkeyDaoStub.getByUserId.calledOnce).to.be.true;
|
|
|
|
expect(lawnchairDaoStub.remove.calledOnce).to.be.true;
|
|
|
|
expect(lawnchairDaoStub.persist.calledOnce).to.be.true;
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should update key without approval', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
getPubKeyStub.returns(resolves(oldKey));
|
|
|
|
pubkeyDaoStub.getByUserId.withArgs(testUser).returns(resolves(newKey));
|
|
|
|
lawnchairDaoStub.remove.withArgs('publickey_' + oldKey._id).returns(resolves());
|
|
|
|
lawnchairDaoStub.persist.withArgs('publickey_' + newKey._id, newKey).returns(resolves());
|
2014-11-06 08:28:14 -05:00
|
|
|
|
|
|
|
keychainDao.refreshKeyForUserId({
|
|
|
|
userId: testUser,
|
|
|
|
overridePermission: true
|
2014-12-12 12:02:13 -05:00
|
|
|
}).then(function(key) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(key).to.equal(newKey);
|
|
|
|
|
|
|
|
expect(getPubKeyStub.calledOnce).to.be.true;
|
|
|
|
expect(pubkeyDaoStub.getByUserId.calledOnce).to.be.true;
|
|
|
|
expect(lawnchairDaoStub.remove.calledOnce).to.be.true;
|
|
|
|
expect(lawnchairDaoStub.persist.calledOnce).to.be.true;
|
|
|
|
|
|
|
|
done();
|
2014-05-23 04:52:34 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should remove key', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
getPubKeyStub.returns(resolves(oldKey));
|
2015-01-28 09:01:18 -05:00
|
|
|
pubkeyDaoStub.getByUserId.withArgs(testUser).returns(resolves());
|
2014-12-12 12:02:13 -05:00
|
|
|
pubkeyDaoStub.getByUserId.withArgs(testUser).returns(resolves());
|
2014-10-07 14:32:23 -04:00
|
|
|
keychainDao.requestPermissionForKeyUpdate = function(opts, cb) {
|
|
|
|
expect(opts.userId).to.equal(testUser);
|
|
|
|
expect(opts.newKey).to.not.exist;
|
|
|
|
cb(true);
|
|
|
|
};
|
2014-12-12 12:02:13 -05:00
|
|
|
lawnchairDaoStub.remove.withArgs('publickey_' + oldKey._id).returns(resolves());
|
2014-10-07 14:32:23 -04:00
|
|
|
|
2014-11-06 08:28:14 -05:00
|
|
|
keychainDao.refreshKeyForUserId({
|
|
|
|
userId: testUser
|
2014-12-12 12:02:13 -05:00
|
|
|
}).then(function(key) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(key).to.not.exist;
|
|
|
|
|
|
|
|
expect(getPubKeyStub.calledOnce).to.be.true;
|
|
|
|
expect(pubkeyDaoStub.getByUserId.calledOnce).to.be.true;
|
|
|
|
expect(lawnchairDaoStub.remove.calledOnce).to.be.true;
|
|
|
|
expect(lawnchairDaoStub.persist.called).to.be.false;
|
|
|
|
|
|
|
|
done();
|
2014-05-23 04:52:34 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should go offline while fetching new key', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
getPubKeyStub.returns(resolves(oldKey));
|
2015-01-28 09:01:18 -05:00
|
|
|
pubkeyDaoStub.getByUserId.withArgs(testUser).returns(resolves());
|
2014-12-12 12:02:13 -05:00
|
|
|
pubkeyDaoStub.getByUserId.withArgs(testUser).returns(rejects({
|
2014-10-07 14:32:23 -04:00
|
|
|
code: 42
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-11-06 08:28:14 -05:00
|
|
|
keychainDao.refreshKeyForUserId({
|
|
|
|
userId: testUser
|
2014-12-12 12:02:13 -05:00
|
|
|
}).then(function(key) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(key).to.to.equal(oldKey);
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(getPubKeyStub.calledOnce).to.be.true;
|
|
|
|
expect(pubkeyDaoStub.getByUserId.calledOnce).to.be.true;
|
|
|
|
expect(lawnchairDaoStub.remove.called).to.be.false;
|
|
|
|
expect(lawnchairDaoStub.persist.called).to.be.false;
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
done();
|
2014-05-23 04:52:34 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should not remove old key on user rejection', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
getPubKeyStub.returns(resolves(oldKey));
|
2015-01-28 09:01:18 -05:00
|
|
|
pubkeyDaoStub.getByUserId.withArgs(testUser).returns(resolves());
|
2014-12-12 12:02:13 -05:00
|
|
|
pubkeyDaoStub.getByUserId.withArgs(testUser).returns(resolves(newKey));
|
2014-10-07 14:32:23 -04:00
|
|
|
keychainDao.requestPermissionForKeyUpdate = function(opts, cb) {
|
|
|
|
expect(opts.userId).to.equal(testUser);
|
|
|
|
expect(opts.newKey).to.exist;
|
|
|
|
cb(false);
|
|
|
|
};
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-11-06 08:28:14 -05:00
|
|
|
keychainDao.refreshKeyForUserId({
|
|
|
|
userId: testUser
|
2014-12-12 12:02:13 -05:00
|
|
|
}).then(function(key) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(key).to.equal(oldKey);
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(getPubKeyStub.calledOnce).to.be.true;
|
|
|
|
expect(pubkeyDaoStub.getByUserId.calledOnce).to.be.true;
|
|
|
|
expect(lawnchairDaoStub.remove.called).to.be.false;
|
|
|
|
expect(lawnchairDaoStub.persist.called).to.be.false;
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
done();
|
2014-05-23 04:52:34 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should not remove manually imported key', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
getPubKeyStub.returns(resolves(importedKey));
|
2014-06-18 04:02:33 -04:00
|
|
|
|
2014-11-06 08:28:14 -05:00
|
|
|
keychainDao.refreshKeyForUserId({
|
|
|
|
userId: testUser
|
2014-12-12 12:02:13 -05:00
|
|
|
}).then(function(key) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(key).to.equal(importedKey);
|
2014-06-18 04:02:33 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(getPubKeyStub.calledOnce).to.be.true;
|
|
|
|
expect(pubkeyDaoStub.get.calledOnce).to.be.false;
|
2014-06-18 04:02:33 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
done();
|
2014-06-18 04:02:33 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-18 04:02:33 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should update not the key when offline', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
getPubKeyStub.returns(resolves(oldKey));
|
2015-01-28 09:01:18 -05:00
|
|
|
pubkeyDaoStub.getByUserId.withArgs(testUser).returns(rejects({
|
2014-10-07 14:32:23 -04:00
|
|
|
code: 42
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-11-06 08:28:14 -05:00
|
|
|
keychainDao.refreshKeyForUserId({
|
|
|
|
userId: testUser
|
2014-12-12 12:02:13 -05:00
|
|
|
}).then(function(key) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(key).to.to.equal(oldKey);
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(getPubKeyStub.calledOnce).to.be.true;
|
2015-01-28 09:01:18 -05:00
|
|
|
expect(pubkeyDaoStub.getByUserId.calledOnce).to.be.true;
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(lawnchairDaoStub.remove.called).to.be.false;
|
|
|
|
expect(lawnchairDaoStub.persist.called).to.be.false;
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
done();
|
2014-05-23 04:52:34 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should error while persisting new key', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
getPubKeyStub.returns(resolves(oldKey));
|
2015-01-28 09:01:18 -05:00
|
|
|
pubkeyDaoStub.getByUserId.withArgs(testUser).returns(resolves());
|
2014-12-12 12:02:13 -05:00
|
|
|
pubkeyDaoStub.getByUserId.withArgs(testUser).returns(resolves(newKey));
|
2014-10-07 14:32:23 -04:00
|
|
|
keychainDao.requestPermissionForKeyUpdate = function(opts, cb) {
|
|
|
|
expect(opts.userId).to.equal(testUser);
|
|
|
|
expect(opts.newKey).to.equal(newKey);
|
|
|
|
cb(true);
|
|
|
|
};
|
2014-12-12 12:02:13 -05:00
|
|
|
lawnchairDaoStub.remove.withArgs('publickey_' + oldKey._id).returns(resolves());
|
|
|
|
lawnchairDaoStub.persist.returns(rejects({}));
|
2014-10-07 14:32:23 -04:00
|
|
|
|
2014-11-06 08:28:14 -05:00
|
|
|
keychainDao.refreshKeyForUserId({
|
|
|
|
userId: testUser
|
2014-12-12 12:02:13 -05:00
|
|
|
}).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
|
|
|
|
expect(getPubKeyStub.calledOnce).to.be.true;
|
|
|
|
expect(pubkeyDaoStub.getByUserId.calledOnce).to.be.true;
|
|
|
|
expect(lawnchairDaoStub.remove.calledOnce).to.be.true;
|
|
|
|
expect(lawnchairDaoStub.persist.calledOnce).to.be.true;
|
|
|
|
|
|
|
|
done();
|
2014-05-23 04:52:34 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should error while deleting old key', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
getPubKeyStub.returns(resolves(oldKey));
|
2015-01-28 09:01:18 -05:00
|
|
|
pubkeyDaoStub.getByUserId.withArgs(testUser).returns(resolves());
|
2014-12-12 12:02:13 -05:00
|
|
|
pubkeyDaoStub.getByUserId.withArgs(testUser).returns(resolves());
|
2014-10-07 14:32:23 -04:00
|
|
|
keychainDao.requestPermissionForKeyUpdate = function(opts, cb) {
|
|
|
|
expect(opts.userId).to.equal(testUser);
|
|
|
|
cb(true);
|
|
|
|
};
|
2014-12-12 12:02:13 -05:00
|
|
|
lawnchairDaoStub.remove.returns(rejects({}));
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-11-06 08:28:14 -05:00
|
|
|
keychainDao.refreshKeyForUserId({
|
|
|
|
userId: testUser
|
2014-12-12 12:02:13 -05:00
|
|
|
}).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(getPubKeyStub.calledOnce).to.be.true;
|
|
|
|
expect(lawnchairDaoStub.remove.calledOnce).to.be.true;
|
|
|
|
expect(pubkeyDaoStub.getByUserId.calledOnce).to.be.true;
|
|
|
|
expect(lawnchairDaoStub.persist.called).to.be.false;
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
done();
|
2014-05-23 04:52:34 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should error while persisting new key', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
getPubKeyStub.returns(resolves(oldKey));
|
2015-01-28 09:01:18 -05:00
|
|
|
pubkeyDaoStub.getByUserId.withArgs(testUser).returns(resolves());
|
2014-12-12 12:02:13 -05:00
|
|
|
pubkeyDaoStub.getByUserId.withArgs(testUser).returns(resolves(newKey));
|
2014-10-07 14:32:23 -04:00
|
|
|
keychainDao.requestPermissionForKeyUpdate = function(opts, cb) {
|
|
|
|
expect(opts.userId).to.equal(testUser);
|
|
|
|
expect(opts.newKey).to.equal(newKey);
|
|
|
|
cb(true);
|
|
|
|
};
|
2014-12-12 12:02:13 -05:00
|
|
|
lawnchairDaoStub.remove.withArgs('publickey_' + oldKey._id).returns(resolves());
|
|
|
|
lawnchairDaoStub.persist.returns(rejects({}));
|
2014-10-07 14:32:23 -04:00
|
|
|
|
2014-11-06 08:28:14 -05:00
|
|
|
keychainDao.refreshKeyForUserId({
|
|
|
|
userId: testUser
|
2014-12-12 12:02:13 -05:00
|
|
|
}).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
|
|
|
|
expect(getPubKeyStub.calledOnce).to.be.true;
|
|
|
|
expect(pubkeyDaoStub.getByUserId.calledOnce).to.be.true;
|
|
|
|
expect(lawnchairDaoStub.remove.calledOnce).to.be.true;
|
|
|
|
expect(lawnchairDaoStub.persist.calledOnce).to.be.true;
|
|
|
|
|
|
|
|
done();
|
2014-05-23 04:52:34 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should error when get failed', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
getPubKeyStub.returns(resolves(oldKey));
|
2015-01-28 09:01:18 -05:00
|
|
|
pubkeyDaoStub.getByUserId.withArgs(testUser).returns(rejects({}));
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-11-06 08:28:14 -05:00
|
|
|
keychainDao.refreshKeyForUserId({
|
|
|
|
userId: testUser
|
2014-12-12 12:02:13 -05:00
|
|
|
}).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
done();
|
2014-05-23 04:52:34 -04:00
|
|
|
});
|
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-05-23 04:52:34 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('lookup public key', function() {
|
|
|
|
it('should fail', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.lookupPublicKey(undefined).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
done();
|
2013-10-29 07:19:27 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
lawnchairDaoStub.read.returns(rejects(42));
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.lookupPublicKey('12345').catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
expect(lawnchairDaoStub.read.calledOnce).to.be.true;
|
|
|
|
done();
|
2013-10-29 07:19:27 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work from local storage', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
lawnchairDaoStub.read.returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
_id: '12345',
|
|
|
|
publicKey: 'asdf'
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.lookupPublicKey('12345').then(function(key) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(key).to.exist;
|
|
|
|
expect(lawnchairDaoStub.read.calledOnce).to.be.true;
|
|
|
|
done();
|
2013-10-29 07:19:27 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work from cloud', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
lawnchairDaoStub.read.returns(resolves());
|
|
|
|
pubkeyDaoStub.get.returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
_id: '12345',
|
|
|
|
publicKey: 'asdf'
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
|
|
|
lawnchairDaoStub.persist.returns(resolves());
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.lookupPublicKey('12345').then(function(key) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(key).to.exist;
|
|
|
|
expect(key._id).to.equal('12345');
|
|
|
|
expect(lawnchairDaoStub.read.calledOnce).to.be.true;
|
|
|
|
expect(pubkeyDaoStub.get.calledOnce).to.be.true;
|
|
|
|
expect(lawnchairDaoStub.persist.calledOnce).to.be.true;
|
|
|
|
done();
|
2013-10-29 07:19:27 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
|
|
|
});
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('get public keys by id', function() {
|
|
|
|
it('should fail', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.getPublicKeys([]).then(function(keys) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(keys.length).to.equal(0);
|
|
|
|
done();
|
2013-10-29 07:19:27 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
lawnchairDaoStub.read.returns(rejects(42));
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
var ids = [{
|
|
|
|
_id: '12345'
|
|
|
|
}];
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.getPublicKeys(ids).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
expect(lawnchairDaoStub.read.calledOnce).to.be.true;
|
|
|
|
done();
|
2013-10-29 07:19:27 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work from local storage', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
lawnchairDaoStub.read.returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
_id: '12345',
|
|
|
|
publicKey: 'asdf'
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
var ids = [{
|
|
|
|
_id: '12345'
|
|
|
|
}];
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.getPublicKeys(ids).then(function(keys) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(keys.length).to.equal(1);
|
|
|
|
expect(keys[0]._id).to.equal('12345');
|
|
|
|
expect(lawnchairDaoStub.read.calledOnce).to.be.true;
|
|
|
|
done();
|
2014-06-18 04:02:33 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
|
|
|
});
|
2014-06-18 04:02:33 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('get receiver public key', function() {
|
|
|
|
it('should fail due to error in lawnchair list', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
lawnchairDaoStub.list.returns(rejects(42));
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.getReceiverPublicKey(testUser).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
expect(lawnchairDaoStub.list.calledOnce).to.be.true;
|
|
|
|
done();
|
2013-10-29 07:19:27 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work from lawnchair list', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
lawnchairDaoStub.list.returns(resolves([{
|
2014-10-07 14:32:23 -04:00
|
|
|
_id: '12345',
|
|
|
|
userId: testUser,
|
|
|
|
publicKey: 'asdf'
|
2014-12-12 12:02:13 -05:00
|
|
|
}]));
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.getReceiverPublicKey(testUser).then(function(key) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(key).to.exist;
|
|
|
|
expect(key._id).to.equal('12345');
|
|
|
|
expect(lawnchairDaoStub.list.calledOnce).to.be.true;
|
|
|
|
done();
|
2013-10-29 07:19:27 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work for keys with secondary userIds', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
lawnchairDaoStub.list.returns(resolves([{
|
2014-10-07 14:32:23 -04:00
|
|
|
_id: '12345',
|
|
|
|
userId: 'not testUser',
|
|
|
|
publicKey: 'asdf'
|
2014-12-12 12:02:13 -05:00
|
|
|
}]));
|
2015-01-28 09:01:18 -05:00
|
|
|
pgpStub.getKeyParams.returns({
|
|
|
|
userIds: [{
|
|
|
|
emailAddress: testUser
|
|
|
|
}]
|
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.getReceiverPublicKey(testUser).then(function(key) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(key).to.exist;
|
|
|
|
expect(key._id).to.equal('12345');
|
|
|
|
expect(lawnchairDaoStub.list.calledOnce).to.be.true;
|
|
|
|
done();
|
2013-10-29 07:19:27 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail due to error in pubkey dao', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
lawnchairDaoStub.list.returns(resolves([]));
|
|
|
|
pubkeyDaoStub.getByUserId.returns(rejects({}));
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.getReceiverPublicKey(testUser).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
expect(lawnchairDaoStub.list.calledOnce).to.be.true;
|
|
|
|
expect(pubkeyDaoStub.getByUserId.calledOnce).to.be.true;
|
|
|
|
done();
|
2013-10-29 07:19:27 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work from pubkey dao with empty result', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
lawnchairDaoStub.list.returns(resolves([]));
|
|
|
|
pubkeyDaoStub.getByUserId.returns(resolves());
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.getReceiverPublicKey(testUser).then(function(key) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(key).to.not.exist;
|
|
|
|
expect(lawnchairDaoStub.list.calledOnce).to.be.true;
|
|
|
|
expect(pubkeyDaoStub.getByUserId.calledOnce).to.be.true;
|
|
|
|
done();
|
2013-10-29 07:19:27 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work from pubkey dao', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
lawnchairDaoStub.list.returns(resolves([]));
|
|
|
|
pubkeyDaoStub.getByUserId.returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
_id: '12345',
|
|
|
|
publicKey: 'asdf'
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
|
|
|
lawnchairDaoStub.persist.returns(resolves());
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.getReceiverPublicKey(testUser).then(function(key) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(key).to.exist;
|
|
|
|
expect(key._id).to.equal('12345');
|
|
|
|
expect(lawnchairDaoStub.list.calledOnce).to.be.true;
|
|
|
|
expect(pubkeyDaoStub.getByUserId.calledOnce).to.be.true;
|
|
|
|
expect(lawnchairDaoStub.persist.calledOnce).to.be.true;
|
|
|
|
done();
|
2013-10-29 07:19:27 -04:00
|
|
|
});
|
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('get user key pair', function() {
|
|
|
|
it('should work if local keys are already present', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
lawnchairDaoStub.list.returns(resolves([{
|
2014-10-07 14:32:23 -04:00
|
|
|
_id: '12345',
|
|
|
|
userId: testUser,
|
|
|
|
publicKey: 'asdf'
|
2014-12-12 12:02:13 -05:00
|
|
|
}]));
|
|
|
|
lawnchairDaoStub.read.returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
_id: '12345',
|
|
|
|
publicKey: 'asdf',
|
|
|
|
encryptedKey: 'qwer'
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
2014-10-07 14:32:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.getUserKeyPair(testUser).then(function(keys) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(keys).to.exist;
|
|
|
|
expect(keys.publicKey).to.exist;
|
|
|
|
expect(keys.privateKey).to.exist;
|
|
|
|
expect(lawnchairDaoStub.list.calledOnce).to.be.true;
|
|
|
|
expect(lawnchairDaoStub.read.calledTwice).to.be.true;
|
|
|
|
done();
|
2014-06-05 11:14:46 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work if local keys are not already present', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
lawnchairDaoStub.list.returns(resolves());
|
|
|
|
pubkeyDaoStub.getByUserId.returns(resolves());
|
2014-06-05 11:14:46 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.getUserKeyPair(testUser).then(function(keys) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(keys).to.not.exist;
|
|
|
|
expect(lawnchairDaoStub.list.calledOnce).to.be.true;
|
|
|
|
expect(pubkeyDaoStub.getByUserId.calledOnce).to.be.true;
|
|
|
|
done();
|
2014-06-05 11:14:46 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-05 11:14:46 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work if local keys are not already present', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
lawnchairDaoStub.list.returns(resolves());
|
|
|
|
pubkeyDaoStub.getByUserId.returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
_id: '12345',
|
|
|
|
publicKey: 'asdf'
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
|
|
|
lawnchairDaoStub.read.returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
_id: '12345',
|
|
|
|
publicKey: 'asdf',
|
|
|
|
encryptedKey: 'qwer'
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
2014-06-05 11:14:46 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.getUserKeyPair(testUser).then(function(keys) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(keys).to.exist;
|
|
|
|
expect(keys.publicKey).to.exist;
|
|
|
|
expect(keys.privateKey).to.exist;
|
|
|
|
expect(lawnchairDaoStub.list.calledOnce).to.be.true;
|
|
|
|
expect(lawnchairDaoStub.read.calledTwice).to.be.true;
|
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('setDeviceName', function() {
|
|
|
|
it('should work', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
lawnchairDaoStub.persist.returns(resolves());
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.setDeviceName('iPhone').then(done);
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getDeviceName', function() {
|
|
|
|
it('should fail when device name is not set', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
lawnchairDaoStub.read.withArgs('devicename').returns(resolves());
|
2014-10-07 14:32:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.getDeviceName().catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err.message).to.equal('Device name not set!');
|
|
|
|
done();
|
2014-06-05 11:14:46 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-06-05 11:14:46 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail due to error when reading device name', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
lawnchairDaoStub.read.withArgs('devicename').returns(rejects(42));
|
2014-06-05 11:14:46 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.getDeviceName().catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.equal(42);
|
|
|
|
done();
|
2014-06-05 11:14:46 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-05 11:14:46 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
lawnchairDaoStub.read.withArgs('devicename').returns(resolves('iPhone'));
|
2014-06-05 11:14:46 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.getDeviceName().then(function(deviceName) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(deviceName).to.equal('iPhone');
|
|
|
|
done();
|
2014-06-05 11:14:46 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
|
|
|
});
|
2014-06-05 11:14:46 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('getDeviceSecret', function() {
|
|
|
|
it('should fail due to error when reading device secret', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
lawnchairDaoStub.read.withArgs('devicename').returns(resolves('iPhone'));
|
|
|
|
lawnchairDaoStub.read.withArgs('devicesecret').returns(rejects(42));
|
2014-06-05 11:14:46 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.getDeviceSecret().catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.equal(42);
|
|
|
|
done();
|
2014-06-05 11:14:46 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-05 11:14:46 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail due to error when storing device secret', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
lawnchairDaoStub.read.withArgs('devicename').returns(resolves('iPhone'));
|
|
|
|
lawnchairDaoStub.read.withArgs('devicesecret').returns(resolves());
|
|
|
|
lawnchairDaoStub.persist.withArgs('devicesecret').returns(rejects(42));
|
2014-06-05 11:14:46 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.getDeviceSecret().catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.equal(42);
|
|
|
|
done();
|
2014-06-05 11:14:46 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work when device secret is not set', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
lawnchairDaoStub.read.withArgs('devicename').returns(resolves('iPhone'));
|
|
|
|
lawnchairDaoStub.read.withArgs('devicesecret').returns(resolves());
|
|
|
|
lawnchairDaoStub.persist.withArgs('devicesecret').returns(resolves());
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.getDeviceSecret().then(function(deviceSecret) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(deviceSecret).to.exist;
|
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should work when device secret is set', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
lawnchairDaoStub.read.withArgs('devicename').returns(resolves('iPhone'));
|
|
|
|
lawnchairDaoStub.read.withArgs('devicesecret').returns(resolves('secret'));
|
2014-10-07 14:32:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.getDeviceSecret().then(function(deviceSecret) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(deviceSecret).to.equal('secret');
|
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('registerDevice', function() {
|
|
|
|
var getDeviceNameStub, lookupPublicKeyStub, getDeviceSecretStub;
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
beforeEach(function() {
|
|
|
|
getDeviceNameStub = sinon.stub(keychainDao, 'getDeviceName');
|
|
|
|
lookupPublicKeyStub = sinon.stub(keychainDao, 'lookupPublicKey');
|
|
|
|
getDeviceSecretStub = sinon.stub(keychainDao, 'getDeviceSecret');
|
|
|
|
});
|
|
|
|
afterEach(function() {
|
|
|
|
getDeviceNameStub.restore();
|
|
|
|
lookupPublicKeyStub.restore();
|
|
|
|
getDeviceSecretStub.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail when reading devicename', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
getDeviceNameStub.returns(rejects(42));
|
2014-10-07 14:32:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.registerDevice({}).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.equal(42);
|
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail in requestDeviceRegistration', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
getDeviceNameStub.returns(resolves('iPhone'));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
privkeyDaoStub.requestDeviceRegistration.withArgs({
|
|
|
|
userId: testUser,
|
|
|
|
deviceName: 'iPhone'
|
2014-12-12 12:02:13 -05:00
|
|
|
}).returns(rejects(42));
|
2014-10-07 14:32:23 -04:00
|
|
|
|
|
|
|
keychainDao.registerDevice({
|
|
|
|
userId: testUser
|
2014-12-12 12:02:13 -05:00
|
|
|
}).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.equal(42);
|
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail due to invalid requestDeviceRegistration return value', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
getDeviceNameStub.returns(resolves('iPhone'));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
privkeyDaoStub.requestDeviceRegistration.withArgs({
|
|
|
|
userId: testUser,
|
|
|
|
deviceName: 'iPhone'
|
2014-12-12 12:02:13 -05:00
|
|
|
}).returns(resolves({}));
|
2014-10-07 14:32:23 -04:00
|
|
|
|
|
|
|
keychainDao.registerDevice({
|
|
|
|
userId: testUser
|
2014-12-12 12:02:13 -05:00
|
|
|
}).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err.message).to.equal('Invalid format for session key!');
|
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail in lookupPublicKey', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
getDeviceNameStub.returns(resolves('iPhone'));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
privkeyDaoStub.requestDeviceRegistration.withArgs({
|
|
|
|
userId: testUser,
|
|
|
|
deviceName: 'iPhone'
|
2014-12-12 12:02:13 -05:00
|
|
|
}).returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
encryptedRegSessionKey: 'asdf'
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
lookupPublicKeyStub.returns(rejects(42));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
keychainDao.registerDevice({
|
|
|
|
userId: testUser
|
2014-12-12 12:02:13 -05:00
|
|
|
}).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.equal(42);
|
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail when server public key not found', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
getDeviceNameStub.returns(resolves('iPhone'));
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
privkeyDaoStub.requestDeviceRegistration.withArgs({
|
|
|
|
userId: testUser,
|
|
|
|
deviceName: 'iPhone'
|
2014-12-12 12:02:13 -05:00
|
|
|
}).returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
encryptedRegSessionKey: 'asdf'
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
lookupPublicKeyStub.returns(resolves());
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
keychainDao.registerDevice({
|
|
|
|
userId: testUser
|
2014-12-12 12:02:13 -05:00
|
|
|
}).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
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 fail in decrypt', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
getDeviceNameStub.returns(resolves('iPhone'));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
privkeyDaoStub.requestDeviceRegistration.withArgs({
|
|
|
|
userId: testUser,
|
|
|
|
deviceName: 'iPhone'
|
2014-12-12 12:02:13 -05:00
|
|
|
}).returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
encryptedRegSessionKey: 'asdf'
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
lookupPublicKeyStub.returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
publicKey: 'pubkey'
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
|
|
|
pgpStub.decrypt.withArgs('asdf', 'pubkey').returns(rejects(42));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
keychainDao.registerDevice({
|
|
|
|
userId: testUser
|
2014-12-12 12:02:13 -05:00
|
|
|
}).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.equal(42);
|
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail in getDeviceSecret', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
getDeviceNameStub.returns(resolves('iPhone'));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
privkeyDaoStub.requestDeviceRegistration.withArgs({
|
|
|
|
userId: testUser,
|
|
|
|
deviceName: 'iPhone'
|
2014-12-12 12:02:13 -05:00
|
|
|
}).returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
encryptedRegSessionKey: 'asdf'
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
lookupPublicKeyStub.returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
publicKey: 'pubkey'
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
|
|
|
pgpStub.decrypt.withArgs('asdf', 'pubkey').returns(resolves({
|
|
|
|
decrypted: 'decrypted',
|
|
|
|
signaturesValid: true
|
|
|
|
}));
|
|
|
|
getDeviceSecretStub.returns(rejects(42));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
keychainDao.registerDevice({
|
|
|
|
userId: testUser
|
2014-12-12 12:02:13 -05:00
|
|
|
}).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.equal(42);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail in encrypt', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
getDeviceNameStub.returns(resolves('iPhone'));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
privkeyDaoStub.requestDeviceRegistration.withArgs({
|
|
|
|
userId: testUser,
|
|
|
|
deviceName: 'iPhone'
|
2014-12-12 12:02:13 -05:00
|
|
|
}).returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
encryptedRegSessionKey: 'asdf'
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
lookupPublicKeyStub.returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
publicKey: 'pubkey'
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
|
|
|
pgpStub.decrypt.withArgs('asdf', 'pubkey').returns(resolves({
|
|
|
|
decrypted: 'decrypted',
|
|
|
|
signaturesValid: true
|
|
|
|
}));
|
|
|
|
getDeviceSecretStub.returns(resolves('secret'));
|
|
|
|
cryptoStub.encrypt.withArgs('secret', 'decrypted').returns(rejects(42));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
keychainDao.registerDevice({
|
|
|
|
userId: testUser
|
2014-12-12 12:02:13 -05:00
|
|
|
}).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.equal(42);
|
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
getDeviceNameStub.returns(resolves('iPhone'));
|
2014-10-07 14:32:23 -04:00
|
|
|
|
|
|
|
privkeyDaoStub.requestDeviceRegistration.withArgs({
|
|
|
|
userId: testUser,
|
|
|
|
deviceName: 'iPhone'
|
2014-12-12 12:02:13 -05:00
|
|
|
}).returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
encryptedRegSessionKey: 'asdf'
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
lookupPublicKeyStub.returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
publicKey: 'pubkey'
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
|
|
|
pgpStub.decrypt.withArgs('asdf', 'pubkey').returns(resolves({
|
|
|
|
decrypted: 'decrypted',
|
|
|
|
signaturesValid: true
|
|
|
|
}));
|
|
|
|
getDeviceSecretStub.returns(resolves('secret'));
|
|
|
|
cryptoStub.encrypt.withArgs('secret', 'decrypted').returns(resolves('encryptedDeviceSecret'));
|
|
|
|
privkeyDaoStub.uploadDeviceSecret.returns(resolves());
|
2014-10-07 14:32:23 -04:00
|
|
|
|
|
|
|
keychainDao.registerDevice({
|
|
|
|
userId: testUser
|
2014-12-12 12:02:13 -05:00
|
|
|
}).then(function() {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(privkeyDaoStub.uploadDeviceSecret.calledOnce).to.be.true;
|
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('_authenticateToPrivateKeyServer', function() {
|
|
|
|
var lookupPublicKeyStub, getDeviceSecretStub;
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
beforeEach(function() {
|
|
|
|
lookupPublicKeyStub = sinon.stub(keychainDao, 'lookupPublicKey');
|
|
|
|
getDeviceSecretStub = sinon.stub(keychainDao, 'getDeviceSecret');
|
|
|
|
});
|
|
|
|
afterEach(function() {
|
|
|
|
lookupPublicKeyStub.restore();
|
|
|
|
getDeviceSecretStub.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail due to privkeyDao.requestAuthSessionKey', function(done) {
|
|
|
|
privkeyDaoStub.requestAuthSessionKey.withArgs({
|
|
|
|
userId: testUser
|
2014-12-12 12:02:13 -05:00
|
|
|
}).returns(rejects(42));
|
2014-10-07 14:32:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao._authenticateToPrivateKeyServer(testUser).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.equal(42);
|
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail due to privkeyDao.requestAuthSessionKey response', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
privkeyDaoStub.requestAuthSessionKey.returns(resolves({}));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao._authenticateToPrivateKeyServer(testUser).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail due to lookupPublicKey', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
privkeyDaoStub.requestAuthSessionKey.returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
encryptedAuthSessionKey: 'encryptedAuthSessionKey',
|
|
|
|
encryptedChallenge: 'encryptedChallenge',
|
|
|
|
sessionId: 'sessionId'
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
lookupPublicKeyStub.returns(rejects(42));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao._authenticateToPrivateKeyServer(testUser).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail due to pgp.decrypt', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
privkeyDaoStub.requestAuthSessionKey.returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
encryptedAuthSessionKey: 'encryptedAuthSessionKey',
|
|
|
|
encryptedChallenge: 'encryptedChallenge',
|
|
|
|
sessionId: 'sessionId'
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
lookupPublicKeyStub.returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
publickKey: 'publicKey'
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
pgpStub.decrypt.returns(rejects(42));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao._authenticateToPrivateKeyServer(testUser).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail due to getDeviceSecret', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
privkeyDaoStub.requestAuthSessionKey.returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
encryptedAuthSessionKey: 'encryptedAuthSessionKey',
|
|
|
|
encryptedChallenge: 'encryptedChallenge',
|
|
|
|
sessionId: 'sessionId'
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
lookupPublicKeyStub.returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
publickKey: 'publicKey'
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
pgpStub.decrypt.returns(resolves({
|
|
|
|
decrypted: 'decryptedStuff'
|
|
|
|
}));
|
|
|
|
getDeviceSecretStub.returns(rejects(42));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao._authenticateToPrivateKeyServer(testUser).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail due to crypto.encrypt', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
privkeyDaoStub.requestAuthSessionKey.returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
encryptedAuthSessionKey: 'encryptedAuthSessionKey',
|
|
|
|
encryptedChallenge: 'encryptedChallenge',
|
|
|
|
sessionId: 'sessionId'
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
lookupPublicKeyStub.returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
publickKey: 'publicKey'
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
pgpStub.decrypt.returns(resolves({
|
|
|
|
decrypted: 'decryptedStuff'
|
|
|
|
}));
|
|
|
|
getDeviceSecretStub.returns(resolves('deviceSecret'));
|
|
|
|
cryptoStub.encrypt.returns(rejects(42));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao._authenticateToPrivateKeyServer(testUser).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail due to privkeyDao.verifyAuthentication', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
privkeyDaoStub.requestAuthSessionKey.returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
encryptedAuthSessionKey: 'encryptedAuthSessionKey',
|
|
|
|
encryptedChallenge: 'encryptedChallenge',
|
|
|
|
sessionId: 'sessionId'
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
lookupPublicKeyStub.returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
publickKey: 'publicKey'
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
pgpStub.decrypt.returns(resolves({
|
|
|
|
decrypted: 'decryptedStuff',
|
|
|
|
signaturesValid: true
|
|
|
|
}));
|
|
|
|
getDeviceSecretStub.returns(resolves('deviceSecret'));
|
|
|
|
cryptoStub.encrypt.returns(resolves('encryptedStuff'));
|
|
|
|
privkeyDaoStub.verifyAuthentication.returns(rejects(42));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao._authenticateToPrivateKeyServer(testUser).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail due to server public key nto found', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
privkeyDaoStub.requestAuthSessionKey.returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
encryptedAuthSessionKey: 'encryptedAuthSessionKey',
|
|
|
|
encryptedChallenge: 'encryptedChallenge',
|
|
|
|
sessionId: 'sessionId'
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
lookupPublicKeyStub.returns(resolves());
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
pgpStub.decrypt.returns(resolves({
|
|
|
|
decrypted: 'decryptedStuff',
|
|
|
|
signaturesValid: true
|
|
|
|
}));
|
|
|
|
getDeviceSecretStub.returns(resolves('deviceSecret'));
|
|
|
|
cryptoStub.encrypt.returns(resolves('encryptedStuff'));
|
|
|
|
privkeyDaoStub.verifyAuthentication.returns(resolves());
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao._authenticateToPrivateKeyServer(testUser).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
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 work', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
privkeyDaoStub.requestAuthSessionKey.returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
encryptedAuthSessionKey: 'encryptedAuthSessionKey',
|
|
|
|
encryptedChallenge: 'encryptedChallenge',
|
|
|
|
sessionId: 'sessionId'
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
lookupPublicKeyStub.returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
publicKey: 'publicKey'
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
2014-10-07 14:32:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
pgpStub.decrypt.returns(resolves({
|
|
|
|
decrypted: 'decryptedStuff',
|
|
|
|
signaturesValid: true
|
|
|
|
}));
|
|
|
|
getDeviceSecretStub.returns(resolves('deviceSecret'));
|
|
|
|
cryptoStub.encrypt.returns(resolves('encryptedStuff'));
|
|
|
|
privkeyDaoStub.verifyAuthentication.returns(resolves());
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao._authenticateToPrivateKeyServer(testUser).then(function(authSessionKey) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(authSessionKey).to.deep.equal({
|
|
|
|
sessionKey: 'decryptedStuff',
|
|
|
|
sessionId: 'sessionId'
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('uploadPrivateKey', function() {
|
|
|
|
var getUserKeyPairStub, _authenticateToPrivateKeyServerStub;
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
beforeEach(function() {
|
|
|
|
getUserKeyPairStub = sinon.stub(keychainDao, 'getUserKeyPair');
|
|
|
|
_authenticateToPrivateKeyServerStub = sinon.stub(keychainDao, '_authenticateToPrivateKeyServer');
|
|
|
|
});
|
|
|
|
afterEach(function() {
|
|
|
|
getUserKeyPairStub.restore();
|
|
|
|
_authenticateToPrivateKeyServerStub.restore();
|
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail due to missing args', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.uploadPrivateKey({}).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail due to error in derive key', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
cryptoStub.deriveKey.returns(rejects(42));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
keychainDao.uploadPrivateKey({
|
|
|
|
code: 'code',
|
|
|
|
userId: testUser
|
2014-12-12 12:02:13 -05:00
|
|
|
}).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
expect(cryptoStub.deriveKey.calledOnce).to.be.true;
|
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail due to error in getUserKeyPair', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
cryptoStub.deriveKey.returns(resolves('derivedKey'));
|
|
|
|
getUserKeyPairStub.returns(rejects(42));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
keychainDao.uploadPrivateKey({
|
|
|
|
code: 'code',
|
|
|
|
userId: testUser
|
2014-12-12 12:02:13 -05:00
|
|
|
}).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
expect(cryptoStub.deriveKey.calledOnce).to.be.true;
|
|
|
|
expect(getUserKeyPairStub.calledOnce).to.be.true;
|
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail due to error in crypto.encrypt', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
cryptoStub.deriveKey.returns(resolves('derivedKey'));
|
|
|
|
getUserKeyPairStub.returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
privateKey: {
|
|
|
|
_id: 'pgpKeyId',
|
|
|
|
encryptedKey: 'pgpKey'
|
|
|
|
}
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
|
|
|
cryptoStub.encrypt.returns(rejects(42));
|
2014-10-07 14:32:23 -04:00
|
|
|
|
|
|
|
keychainDao.uploadPrivateKey({
|
|
|
|
code: 'code',
|
|
|
|
userId: testUser
|
2014-12-12 12:02:13 -05:00
|
|
|
}).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
expect(cryptoStub.deriveKey.calledOnce).to.be.true;
|
|
|
|
expect(getUserKeyPairStub.calledOnce).to.be.true;
|
|
|
|
expect(cryptoStub.encrypt.calledOnce).to.be.true;
|
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail due to error in _authenticateToPrivateKeyServer', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
cryptoStub.deriveKey.returns(resolves('derivedKey'));
|
|
|
|
getUserKeyPairStub.returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
privateKey: {
|
|
|
|
_id: 'pgpKeyId',
|
|
|
|
encryptedKey: 'pgpKey'
|
|
|
|
}
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
|
|
|
cryptoStub.encrypt.returns(resolves('encryptedPgpKey'));
|
|
|
|
_authenticateToPrivateKeyServerStub.returns(rejects(42));
|
2014-10-07 14:32:23 -04:00
|
|
|
|
|
|
|
keychainDao.uploadPrivateKey({
|
|
|
|
code: 'code',
|
|
|
|
userId: testUser
|
2014-12-12 12:02:13 -05:00
|
|
|
}).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
expect(cryptoStub.deriveKey.calledOnce).to.be.true;
|
|
|
|
expect(getUserKeyPairStub.calledOnce).to.be.true;
|
|
|
|
expect(cryptoStub.encrypt.calledOnce).to.be.true;
|
|
|
|
expect(_authenticateToPrivateKeyServerStub.calledOnce).to.be.true;
|
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail due to error in cryptoStub.encrypt', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
cryptoStub.deriveKey.returns(resolves('derivedKey'));
|
|
|
|
getUserKeyPairStub.returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
privateKey: {
|
|
|
|
_id: 'pgpKeyId',
|
|
|
|
encryptedKey: 'pgpKey'
|
|
|
|
}
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
|
|
|
cryptoStub.encrypt.withArgs('pgpKey').returns(resolves('encryptedPgpKey'));
|
|
|
|
_authenticateToPrivateKeyServerStub.returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
sessionId: 'sessionId',
|
|
|
|
sessionKey: 'sessionKey'
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
|
|
|
cryptoStub.encrypt.withArgs('encryptedPgpKey').returns(rejects(42));
|
2014-10-07 14:32:23 -04:00
|
|
|
|
|
|
|
keychainDao.uploadPrivateKey({
|
|
|
|
code: 'code',
|
|
|
|
userId: testUser
|
2014-12-12 12:02:13 -05:00
|
|
|
}).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
expect(cryptoStub.deriveKey.calledOnce).to.be.true;
|
|
|
|
expect(getUserKeyPairStub.calledOnce).to.be.true;
|
|
|
|
expect(cryptoStub.encrypt.calledTwice).to.be.true;
|
|
|
|
expect(_authenticateToPrivateKeyServerStub.calledOnce).to.be.true;
|
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
cryptoStub.deriveKey.returns(resolves('derivedKey'));
|
|
|
|
getUserKeyPairStub.returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
privateKey: {
|
|
|
|
_id: 'pgpKeyId',
|
|
|
|
encryptedKey: 'pgpKey'
|
|
|
|
}
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
|
|
|
cryptoStub.encrypt.withArgs('pgpKey').returns(resolves('encryptedPgpKey'));
|
|
|
|
_authenticateToPrivateKeyServerStub.returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
sessionId: 'sessionId',
|
|
|
|
sessionKey: 'sessionKey'
|
2014-12-12 12:02:13 -05:00
|
|
|
}));
|
|
|
|
cryptoStub.encrypt.withArgs('encryptedPgpKey').returns(resolves('doubleEncryptedPgpKey'));
|
|
|
|
privkeyDaoStub.upload.returns(resolves());
|
2014-10-07 14:32:23 -04:00
|
|
|
|
|
|
|
keychainDao.uploadPrivateKey({
|
|
|
|
code: 'code',
|
|
|
|
userId: testUser
|
2014-12-12 12:02:13 -05:00
|
|
|
}).then(function() {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(cryptoStub.deriveKey.calledOnce).to.be.true;
|
|
|
|
expect(getUserKeyPairStub.calledOnce).to.be.true;
|
|
|
|
expect(cryptoStub.encrypt.calledTwice).to.be.true;
|
|
|
|
expect(_authenticateToPrivateKeyServerStub.calledOnce).to.be.true;
|
|
|
|
expect(privkeyDaoStub.upload.calledOnce).to.be.true;
|
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('requestPrivateKeyDownload', function() {
|
|
|
|
it('should work', function(done) {
|
|
|
|
var options = {
|
|
|
|
userId: testUser,
|
|
|
|
keyId: 'someId'
|
|
|
|
};
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
privkeyDaoStub.requestDownload.withArgs(options).returns(resolves());
|
|
|
|
keychainDao.requestPrivateKeyDownload(options).then(done);
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('hasPrivateKey', function() {
|
|
|
|
it('should work', function(done) {
|
|
|
|
var options = {
|
|
|
|
userId: testUser,
|
|
|
|
keyId: 'someId'
|
|
|
|
};
|
2014-08-28 12:17:34 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
privkeyDaoStub.hasPrivateKey.withArgs(options).returns(resolves());
|
|
|
|
keychainDao.hasPrivateKey(options).then(done);
|
2014-08-28 12:17:34 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-08-28 12:17:34 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('downloadPrivateKey', function() {
|
|
|
|
it('should work', function(done) {
|
|
|
|
var options = {
|
|
|
|
recoveryToken: 'token'
|
|
|
|
};
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
privkeyDaoStub.download.withArgs(options).returns(resolves());
|
|
|
|
keychainDao.downloadPrivateKey(options).then(done);
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('decryptAndStorePrivateKeyLocally', function() {
|
|
|
|
var saveLocalPrivateKeyStub, testData;
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
beforeEach(function() {
|
|
|
|
testData = {
|
|
|
|
_id: 'keyId',
|
|
|
|
userId: testUser,
|
|
|
|
encryptedPrivateKey: 'encryptedPrivateKey',
|
|
|
|
code: 'code',
|
|
|
|
salt: 'salt',
|
|
|
|
iv: 'iv'
|
|
|
|
};
|
|
|
|
|
|
|
|
saveLocalPrivateKeyStub = sinon.stub(keychainDao, 'saveLocalPrivateKey');
|
|
|
|
});
|
|
|
|
afterEach(function() {
|
|
|
|
saveLocalPrivateKeyStub.restore();
|
|
|
|
});
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail due to invlaid args', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.decryptAndStorePrivateKeyLocally({}).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail due to crypto.deriveKey', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
cryptoStub.deriveKey.returns(rejects(42));
|
2014-10-07 14:32:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.decryptAndStorePrivateKeyLocally(testData).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
expect(cryptoStub.deriveKey.calledOnce).to.be.true;
|
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail due to crypto.decrypt', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
cryptoStub.deriveKey.returns(resolves('derivedKey'));
|
|
|
|
cryptoStub.decrypt.returns(rejects(42));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.decryptAndStorePrivateKeyLocally(testData).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
expect(cryptoStub.deriveKey.calledOnce).to.be.true;
|
|
|
|
expect(cryptoStub.decrypt.calledOnce).to.be.true;
|
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail due to pgp.getKeyParams', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
cryptoStub.deriveKey.returns(resolves('derivedKey'));
|
|
|
|
cryptoStub.decrypt.returns(resolves('privateKeyArmored'));
|
|
|
|
pgpStub.getKeyParams.returns(rejects(new Error()));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.decryptAndStorePrivateKeyLocally(testData).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
expect(cryptoStub.deriveKey.calledOnce).to.be.true;
|
|
|
|
expect(cryptoStub.decrypt.calledOnce).to.be.true;
|
|
|
|
expect(pgpStub.getKeyParams.calledOnce).to.be.true;
|
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail due to saveLocalPrivateKey', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
cryptoStub.deriveKey.returns(resolves('derivedKey'));
|
|
|
|
cryptoStub.decrypt.returns(resolves('privateKeyArmored'));
|
2014-10-07 14:32:23 -04:00
|
|
|
pgpStub.getKeyParams.returns(testData);
|
2014-12-12 12:02:13 -05:00
|
|
|
saveLocalPrivateKeyStub.returns(rejects(42));
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.decryptAndStorePrivateKeyLocally(testData).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
expect(cryptoStub.deriveKey.calledOnce).to.be.true;
|
|
|
|
expect(cryptoStub.decrypt.calledOnce).to.be.true;
|
|
|
|
expect(pgpStub.getKeyParams.calledOnce).to.be.true;
|
|
|
|
expect(saveLocalPrivateKeyStub.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
|
|
|
it('should work', function(done) {
|
2014-12-12 12:02:13 -05:00
|
|
|
cryptoStub.deriveKey.returns(resolves('derivedKey'));
|
|
|
|
cryptoStub.decrypt.returns(resolves('privateKeyArmored'));
|
2014-10-07 14:32:23 -04:00
|
|
|
pgpStub.getKeyParams.returns(testData);
|
2014-12-12 12:02:13 -05:00
|
|
|
saveLocalPrivateKeyStub.returns(resolves());
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.decryptAndStorePrivateKeyLocally(testData).then(function(keyObject) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(keyObject).to.deep.equal({
|
|
|
|
_id: 'keyId',
|
|
|
|
userId: testUser,
|
|
|
|
encryptedKey: 'privateKeyArmored'
|
2014-06-13 06:33:30 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(cryptoStub.deriveKey.calledOnce).to.be.true;
|
|
|
|
expect(cryptoStub.decrypt.calledOnce).to.be.true;
|
|
|
|
expect(pgpStub.getKeyParams.calledOnce).to.be.true;
|
|
|
|
expect(saveLocalPrivateKeyStub.calledOnce).to.be.true;
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('put user keypair', function() {
|
|
|
|
it('should fail', function(done) {
|
|
|
|
var keypair = {
|
|
|
|
publicKey: {
|
|
|
|
_id: '12345',
|
|
|
|
userId: testUser,
|
|
|
|
publicKey: 'asdf'
|
|
|
|
},
|
|
|
|
privateKey: {
|
|
|
|
_id: '12345',
|
|
|
|
encryptedKey: 'qwer'
|
|
|
|
}
|
|
|
|
};
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.putUserKeyPair(keypair).catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
done();
|
2013-10-29 07:19:27 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work', function(done) {
|
|
|
|
var keypair = {
|
|
|
|
publicKey: {
|
|
|
|
_id: '12345',
|
|
|
|
userId: testUser,
|
|
|
|
publicKey: 'asdf'
|
|
|
|
},
|
|
|
|
privateKey: {
|
|
|
|
_id: '12345',
|
|
|
|
userId: testUser,
|
|
|
|
encryptedKey: 'qwer'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
lawnchairDaoStub.persist.returns(resolves());
|
|
|
|
pubkeyDaoStub.put.returns(resolves());
|
2014-10-07 14:32:23 -04:00
|
|
|
|
2014-12-12 12:02:13 -05:00
|
|
|
keychainDao.putUserKeyPair(keypair).then(function() {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(lawnchairDaoStub.persist.calledTwice).to.be.true;
|
|
|
|
expect(pubkeyDaoStub.put.calledOnce).to.be.true;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2013-10-29 07:19:27 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|