1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-24 18:02:15 -05:00

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.
*/
PrivateKey.prototype.isSynced = function() {
return this._fetchMessage({
userId: this._auth.emailAddress,
keyId: this._pgp.getKeyId()
var self = this;
return self._getFolder().then(function(path) {
return self._fetchMessage({
keyId: self._pgp.getKeyId(),
path: path
});
}).then(function(msg) {
return !!msg;
}).catch(function() {
@ -183,18 +187,24 @@ PrivateKey.prototype.isSynced = function() {
*/
PrivateKey.prototype.download = function(options) {
var self = this,
message;
path, message;
return self._fetchMessage(options).then(function(msg) {
return self._getFolder().then(function(fullPath) {
path = fullPath;
return self._fetchMessage({
keyId: options.keyId,
path: path
}).then(function(msg) {
if (!msg) {
throw new Error('Private key not synced!');
}
message = msg;
});
}).then(function() {
// get the body for the message
return self._imap.getBodyParts({
path: IMAP_KEYS_FOLDER,
path: path,
uid: message.uid,
bodyParts: message.bodyParts
});
@ -282,17 +292,9 @@ PrivateKey.prototype.decrypt = function(options) {
});
};
PrivateKey.prototype._fetchMessage = function(options) {
PrivateKey.prototype._getFolder = function() {
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) {
var paths = []; // gathers paths
@ -318,12 +320,21 @@ PrivateKey.prototype._fetchMessage = function(options) {
}
return paths[0];
}).then(function(path) {
return self._imap.listMessages({
path: path,
});
};
PrivateKey.prototype._fetchMessage = function(options) {
var self = this;
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) {
if (!messages.length) {
// message has been deleted in the meantime

View File

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