Merge pull request #345 from whiteout-io/dev/WO-986

[WO-986] Use proper path for key download
This commit is contained in:
Tankred Hase 2015-05-18 16:59:43 +02:00
commit e7fb3bcf6d
2 changed files with 86 additions and 56 deletions

View File

@ -165,9 +165,13 @@ PrivateKey.prototype.upload = function(options) {
* Check if matching private key is stored in IMAP. * Check if matching private key is stored in IMAP.
*/ */
PrivateKey.prototype.isSynced = function() { PrivateKey.prototype.isSynced = function() {
return this._fetchMessage({ var self = this;
userId: this._auth.emailAddress,
keyId: this._pgp.getKeyId() return self._getFolder().then(function(path) {
return self._fetchMessage({
keyId: self._pgp.getKeyId(),
path: path
});
}).then(function(msg) { }).then(function(msg) {
return !!msg; return !!msg;
}).catch(function() { }).catch(function() {
@ -183,18 +187,24 @@ PrivateKey.prototype.isSynced = function() {
*/ */
PrivateKey.prototype.download = function(options) { PrivateKey.prototype.download = function(options) {
var self = this, var self = this,
message; path, message;
return self._fetchMessage(options).then(function(msg) { return self._getFolder().then(function(fullPath) {
if (!msg) { path = fullPath;
throw new Error('Private key not synced!'); return self._fetchMessage({
} keyId: options.keyId,
path: path
}).then(function(msg) {
if (!msg) {
throw new Error('Private key not synced!');
}
message = msg; message = msg;
});
}).then(function() { }).then(function() {
// get the body for the message // get the body for the message
return self._imap.getBodyParts({ return self._imap.getBodyParts({
path: IMAP_KEYS_FOLDER, path: path,
uid: message.uid, uid: message.uid,
bodyParts: message.bodyParts bodyParts: message.bodyParts
}); });
@ -282,17 +292,9 @@ PrivateKey.prototype.decrypt = function(options) {
}); });
}; };
PrivateKey.prototype._fetchMessage = function(options) { PrivateKey.prototype._getFolder = function() {
var self = this; var self = this;
if (!options.userId || !options.keyId) {
return new Promise(function() {
throw new Error('Incomplete arguments!');
});
}
// get the metadata for the message
return self._imap.listWellKnownFolders().then(function(wellKnownFolders) { return self._imap.listWellKnownFolders().then(function(wellKnownFolders) {
var paths = []; // gathers paths var paths = []; // gathers paths
@ -318,12 +320,21 @@ PrivateKey.prototype._fetchMessage = function(options) {
} }
return paths[0]; return paths[0];
});
};
}).then(function(path) { PrivateKey.prototype._fetchMessage = function(options) {
return self._imap.listMessages({ var self = this;
path: path,
if (!options.keyId) {
return new Promise(function() {
throw new Error('Incomplete arguments!');
}); });
}
// get the metadata for the message
return self._imap.listMessages({
path: options.path
}).then(function(messages) { }).then(function(messages) {
if (!messages.length) { if (!messages.length) {
// message has been deleted in the meantime // message has been deleted in the meantime
@ -369,4 +380,4 @@ function filterBodyParts(bodyParts, type, result) {
} }
}); });
return result; return result;
} }

View File

@ -104,35 +104,61 @@ describe('Private Key DAO unit tests', function() {
describe('isSynced', function() { describe('isSynced', function() {
beforeEach(function() { beforeEach(function() {
sinon.stub(privkeyDao, '_getFolder');
sinon.stub(privkeyDao, '_fetchMessage'); sinon.stub(privkeyDao, '_fetchMessage');
}); });
afterEach(function() { afterEach(function() {
privkeyDao._getFolder.restore();
privkeyDao._fetchMessage.restore(); privkeyDao._fetchMessage.restore();
}); });
it('should be synced', function(done) { it('should be synced', function(done) {
privkeyDao._getFolder.returns(resolves('foo'));
privkeyDao._fetchMessage.returns(resolves({})); privkeyDao._fetchMessage.returns(resolves({}));
privkeyDao.isSynced().then(function(synced) { privkeyDao.isSynced().then(function(synced) {
expect(synced).to.be.true; expect(synced).to.be.true;
expect(privkeyDao._getFolder.calledOnce).to.be.true;
expect(privkeyDao._fetchMessage.calledOnce).to.be.true;
done(); done();
}); });
}); });
it('should not be synced', function(done) { it('should not be synced', function(done) {
privkeyDao._getFolder.returns(resolves());
privkeyDao._fetchMessage.returns(resolves()); privkeyDao._fetchMessage.returns(resolves());
privkeyDao.isSynced().then(function(synced) { privkeyDao.isSynced().then(function(synced) {
expect(synced).to.be.false; expect(synced).to.be.false;
expect(privkeyDao._getFolder.calledOnce).to.be.true;
expect(privkeyDao._fetchMessage.calledOnce).to.be.true;
done(); done();
}); });
}); });
it('should not be synced in case of error', function(done) { it('should not be synced in case of error', function(done) {
privkeyDao._getFolder.returns(rejects(new Error()));
privkeyDao.isSynced().then(function(synced) {
expect(synced).to.be.false;
expect(privkeyDao._getFolder.calledOnce).to.be.true;
done();
});
});
it('should not be synced in case of error', function(done) {
privkeyDao._getFolder.returns(resolves('foo'));
privkeyDao._fetchMessage.returns(rejects(new Error())); privkeyDao._fetchMessage.returns(rejects(new Error()));
privkeyDao.isSynced().then(function(synced) { privkeyDao.isSynced().then(function(synced) {
expect(synced).to.be.false; expect(synced).to.be.false;
expect(privkeyDao._getFolder.calledOnce).to.be.true;
expect(privkeyDao._fetchMessage.calledOnce).to.be.true;
done(); done();
}); });
}); });
@ -146,15 +172,18 @@ describe('Private Key DAO unit tests', function() {
}]; }];
beforeEach(function() { beforeEach(function() {
sinon.stub(privkeyDao, '_getFolder');
sinon.stub(privkeyDao, '_fetchMessage'); sinon.stub(privkeyDao, '_fetchMessage');
sinon.stub(privkeyDao, '_parse'); sinon.stub(privkeyDao, '_parse');
}); });
afterEach(function() { afterEach(function() {
privkeyDao._getFolder.restore();
privkeyDao._fetchMessage.restore(); privkeyDao._fetchMessage.restore();
privkeyDao._parse.restore(); privkeyDao._parse.restore();
}); });
it('should fail if key not synced', function(done) { it('should fail if key not synced', function(done) {
privkeyDao._getFolder.returns(resolves('foo'));
privkeyDao._fetchMessage.returns(resolves()); privkeyDao._fetchMessage.returns(resolves());
privkeyDao.download({ privkeyDao.download({
@ -167,6 +196,7 @@ describe('Private Key DAO unit tests', function() {
}); });
it('should work', function(done) { it('should work', function(done) {
privkeyDao._getFolder.returns(resolves('foo'));
privkeyDao._fetchMessage.returns(resolves({})); privkeyDao._fetchMessage.returns(resolves({}));
imapClientStub.getBodyParts.returns(resolves()); imapClientStub.getBodyParts.returns(resolves());
privkeyDao._parse.returns(resolves(root)); privkeyDao._parse.returns(resolves(root));
@ -253,14 +283,7 @@ describe('Private Key DAO unit tests', function() {
}); });
}); });
describe('_fetchMessage', function() { describe('_getFolder', function() {
it('should fail due to invalid args', function(done) {
privkeyDao._fetchMessage({}).catch(function(err) {
expect(err.message).to.match(/Incomplete/);
done();
});
});
it('should fail if imap folder does not exist', function(done) { it('should fail if imap folder does not exist', function(done) {
imapClientStub.listWellKnownFolders.returns(resolves({ imapClientStub.listWellKnownFolders.returns(resolves({
Inbox: [{ Inbox: [{
@ -270,13 +293,10 @@ describe('Private Key DAO unit tests', function() {
path: 'foo' path: 'foo'
}] }]
})); }));
imapClientStub.listMessages.returns(rejects(new Error()));
privkeyDao._fetchMessage({ privkeyDao._getFolder().catch(function(err) {
userId: emailAddress,
keyId: keyId
}).catch(function(err) {
expect(err.message).to.match(/Imap folder/); expect(err.message).to.match(/Imap folder/);
expect(imapClientStub.listWellKnownFolders.calledOnce).to.be.true;
done(); done();
}); });
}); });
@ -290,6 +310,25 @@ describe('Private Key DAO unit tests', function() {
path: 'openpgp_keys' path: 'openpgp_keys'
}] }]
})); }));
privkeyDao._getFolder().then(function(path) {
expect(path).to.equal('openpgp_keys');
expect(imapClientStub.listWellKnownFolders.calledOnce).to.be.true;
done();
});
});
});
describe('_fetchMessage', function() {
it('should fail due to invalid args', function(done) {
privkeyDao._fetchMessage({}).catch(function(err) {
expect(err.message).to.match(/Incomplete/);
done();
});
});
it('should work', function(done) {
imapClientStub.listMessages.returns(resolves([{ imapClientStub.listMessages.returns(resolves([{
subject: keyId subject: keyId
}])); }]));
@ -299,21 +338,12 @@ describe('Private Key DAO unit tests', function() {
keyId: keyId keyId: keyId
}).then(function(msg) { }).then(function(msg) {
expect(msg.subject).to.equal(keyId); expect(msg.subject).to.equal(keyId);
expect(imapClientStub.listWellKnownFolders.calledOnce).to.be.true;
expect(imapClientStub.listMessages.calledOnce).to.be.true; expect(imapClientStub.listMessages.calledOnce).to.be.true;
done(); done();
}); });
}); });
it('should work with path prefix', function(done) { it('should work with path prefix', function(done) {
imapClientStub.listWellKnownFolders.returns(resolves({
Inbox: [{
path: 'INBOX'
}],
Other: [{
path: 'INBOX.openpgp_keys'
}]
}));
imapClientStub.listMessages.returns(resolves([{ imapClientStub.listMessages.returns(resolves([{
subject: keyId subject: keyId
}])); }]));
@ -323,18 +353,12 @@ describe('Private Key DAO unit tests', function() {
keyId: keyId keyId: keyId
}).then(function(msg) { }).then(function(msg) {
expect(msg.subject).to.equal(keyId); expect(msg.subject).to.equal(keyId);
expect(imapClientStub.listWellKnownFolders.calledOnce).to.be.true;
expect(imapClientStub.listMessages.calledOnce).to.be.true; expect(imapClientStub.listMessages.calledOnce).to.be.true;
done(); done();
}); });
}); });
it('should work for not matching message', function(done) { it('should work for not matching message', function(done) {
imapClientStub.listWellKnownFolders.returns(resolves({
Other: [{
path: 'INBOX.openpgp_keys'
}]
}));
imapClientStub.listMessages.returns(resolves([{ imapClientStub.listMessages.returns(resolves([{
subject: '7890' subject: '7890'
}])); }]));
@ -349,11 +373,6 @@ describe('Private Key DAO unit tests', function() {
}); });
it('should work for no messages', function(done) { it('should work for no messages', function(done) {
imapClientStub.listWellKnownFolders.returns(resolves({
Other: [{
path: 'INBOX.openpgp_keys'
}]
}));
imapClientStub.listMessages.returns(resolves([])); imapClientStub.listMessages.returns(resolves([]));
privkeyDao._fetchMessage({ privkeyDao._fetchMessage({