mirror of
https://github.com/moparisthebest/mail
synced 2024-12-17 21:12:52 -05:00
[WO-1026] Fix broken key upload after mail server error
This commit is contained in:
parent
e0663ab8d8
commit
ad3691fae9
@ -102,24 +102,39 @@ PrivateKey.prototype.upload = function(options) {
|
|||||||
|
|
||||||
return new Promise(function(resolve) {
|
return new Promise(function(resolve) {
|
||||||
if (!options._id || !options.userId || !options.encryptedPrivateKey || !options.salt || !options.iv) {
|
if (!options._id || !options.userId || !options.encryptedPrivateKey || !options.salt || !options.iv) {
|
||||||
throw new Error('Incomplete arguments!');
|
throw new Error('Incomplete arguments for key upload!');
|
||||||
}
|
}
|
||||||
resolve();
|
resolve();
|
||||||
|
|
||||||
}).then(function() {
|
}).then(function() {
|
||||||
// create imap folder
|
|
||||||
return self._imap.createFolder({
|
// Some servers (Exchange, Cyrus) error when creating an existing IMAP mailbox instead of
|
||||||
path: IMAP_KEYS_FOLDER
|
// responding with ALREADYEXISTS. Hence we search for the folder before uploading.
|
||||||
}).then(function(fullPath) {
|
|
||||||
|
self._axe.debug('Searching imap folder for key upload...');
|
||||||
|
|
||||||
|
return self._getFolder().then(function(fullPath) {
|
||||||
path = fullPath;
|
path = fullPath;
|
||||||
self._axe.debug('Successfully created imap folder ' + path);
|
}).catch(function() {
|
||||||
}).catch(function(err) {
|
|
||||||
var prettyErr = new Error('Creating imap folder ' + IMAP_KEYS_FOLDER + ' failed: ' + err.message);
|
// create imap folder
|
||||||
self._axe.error(prettyErr);
|
self._axe.debug('Folder not found, creating imap folder.');
|
||||||
throw prettyErr;
|
return self._imap.createFolder({
|
||||||
|
path: IMAP_KEYS_FOLDER
|
||||||
|
}).then(function(fullPath) {
|
||||||
|
path = fullPath;
|
||||||
|
self._axe.debug('Successfully created imap folder ' + path);
|
||||||
|
}).catch(function(err) {
|
||||||
|
var prettyErr = new Error('Creating imap folder ' + IMAP_KEYS_FOLDER + ' failed: ' + err.message);
|
||||||
|
self._axe.error(prettyErr);
|
||||||
|
throw prettyErr;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
}).then(createMessage).then(function(message) {
|
}).then(createMessage).then(function(message) {
|
||||||
|
|
||||||
// upload to imap folder
|
// upload to imap folder
|
||||||
|
self._axe.debug('Uploading key...');
|
||||||
return self._imap.uploadMessage({
|
return self._imap.uploadMessage({
|
||||||
path: path,
|
path: path,
|
||||||
message: message
|
message: message
|
||||||
|
@ -68,6 +68,14 @@ describe('Private Key DAO unit tests', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('upload', function() {
|
describe('upload', function() {
|
||||||
|
beforeEach(function() {
|
||||||
|
sinon.stub(privkeyDao, '_getFolder');
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(function() {
|
||||||
|
privkeyDao._getFolder.restore();
|
||||||
|
});
|
||||||
|
|
||||||
it('should fail due to invalid args', function(done) {
|
it('should fail due to invalid args', function(done) {
|
||||||
privkeyDao.upload({}).catch(function(err) {
|
privkeyDao.upload({}).catch(function(err) {
|
||||||
expect(err.message).to.match(/Incomplete/);
|
expect(err.message).to.match(/Incomplete/);
|
||||||
@ -75,10 +83,11 @@ describe('Private Key DAO unit tests', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should work', function(done) {
|
it('should work without existing folder', function(done) {
|
||||||
var IMAP_KEYS_FOLDER = 'openpgp_keys';
|
var IMAP_KEYS_FOLDER = 'openpgp_keys';
|
||||||
var fullPath = 'INBOX.' + IMAP_KEYS_FOLDER;
|
var fullPath = 'INBOX.' + IMAP_KEYS_FOLDER;
|
||||||
|
|
||||||
|
privkeyDao._getFolder.returns(rejects(new Error()));
|
||||||
imapClientStub.createFolder.withArgs({
|
imapClientStub.createFolder.withArgs({
|
||||||
path: IMAP_KEYS_FOLDER
|
path: IMAP_KEYS_FOLDER
|
||||||
}).returns(resolves(fullPath));
|
}).returns(resolves(fullPath));
|
||||||
@ -95,11 +104,37 @@ describe('Private Key DAO unit tests', function() {
|
|||||||
salt: salt,
|
salt: salt,
|
||||||
iv: iv
|
iv: iv
|
||||||
}).then(function() {
|
}).then(function() {
|
||||||
|
expect(privkeyDao._getFolder.calledOnce).to.be.true;
|
||||||
expect(imapClientStub.createFolder.calledOnce).to.be.true;
|
expect(imapClientStub.createFolder.calledOnce).to.be.true;
|
||||||
expect(imapClientStub.uploadMessage.calledOnce).to.be.true;
|
expect(imapClientStub.uploadMessage.calledOnce).to.be.true;
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should work with existing folder', function(done) {
|
||||||
|
var IMAP_KEYS_FOLDER = 'openpgp_keys';
|
||||||
|
var fullPath = 'INBOX.' + IMAP_KEYS_FOLDER;
|
||||||
|
|
||||||
|
privkeyDao._getFolder.returns(resolves(fullPath));
|
||||||
|
imapClientStub.uploadMessage.withArgs(sinon.match(function(arg) {
|
||||||
|
expect(arg.path).to.equal(fullPath);
|
||||||
|
expect(arg.message).to.exist;
|
||||||
|
return true;
|
||||||
|
})).returns(resolves());
|
||||||
|
|
||||||
|
privkeyDao.upload({
|
||||||
|
_id: keyId,
|
||||||
|
userId: emailAddress,
|
||||||
|
encryptedPrivateKey: encryptedPrivateKey,
|
||||||
|
salt: salt,
|
||||||
|
iv: iv
|
||||||
|
}).then(function() {
|
||||||
|
expect(privkeyDao._getFolder.calledOnce).to.be.true;
|
||||||
|
expect(imapClientStub.createFolder.called).to.be.false;
|
||||||
|
expect(imapClientStub.uploadMessage.calledOnce).to.be.true;
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('isSynced', function() {
|
describe('isSynced', function() {
|
||||||
|
Loading…
Reference in New Issue
Block a user