Skip to content
This repository was archived by the owner on Jul 10, 2019. It is now read-only.

Commit e7fb3bc

Browse files
author
Tankred Hase
committed
Merge pull request #345 from whiteout-io/dev/WO-986
[WO-986] Use proper path for key download
2 parents 0bfaba3 + ce740b2 commit e7fb3bc

File tree

2 files changed

+87
-57
lines changed

2 files changed

+87
-57
lines changed

src/js/service/privatekey.js

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,13 @@ PrivateKey.prototype.upload = function(options) {
165165
* Check if matching private key is stored in IMAP.
166166
*/
167167
PrivateKey.prototype.isSynced = function() {
168-
return this._fetchMessage({
169-
userId: this._auth.emailAddress,
170-
keyId: this._pgp.getKeyId()
168+
var self = this;
169+
170+
return self._getFolder().then(function(path) {
171+
return self._fetchMessage({
172+
keyId: self._pgp.getKeyId(),
173+
path: path
174+
});
171175
}).then(function(msg) {
172176
return !!msg;
173177
}).catch(function() {
@@ -183,18 +187,24 @@ PrivateKey.prototype.isSynced = function() {
183187
*/
184188
PrivateKey.prototype.download = function(options) {
185189
var self = this,
186-
message;
187-
188-
return self._fetchMessage(options).then(function(msg) {
189-
if (!msg) {
190-
throw new Error('Private key not synced!');
191-
}
190+
path, message;
191+
192+
return self._getFolder().then(function(fullPath) {
193+
path = fullPath;
194+
return self._fetchMessage({
195+
keyId: options.keyId,
196+
path: path
197+
}).then(function(msg) {
198+
if (!msg) {
199+
throw new Error('Private key not synced!');
200+
}
192201

193-
message = msg;
202+
message = msg;
203+
});
194204
}).then(function() {
195205
// get the body for the message
196206
return self._imap.getBodyParts({
197-
path: IMAP_KEYS_FOLDER,
207+
path: path,
198208
uid: message.uid,
199209
bodyParts: message.bodyParts
200210
});
@@ -282,17 +292,9 @@ PrivateKey.prototype.decrypt = function(options) {
282292
});
283293
};
284294

285-
PrivateKey.prototype._fetchMessage = function(options) {
295+
PrivateKey.prototype._getFolder = function() {
286296
var self = this;
287297

288-
if (!options.userId || !options.keyId) {
289-
return new Promise(function() {
290-
throw new Error('Incomplete arguments!');
291-
});
292-
}
293-
294-
// get the metadata for the message
295-
296298
return self._imap.listWellKnownFolders().then(function(wellKnownFolders) {
297299
var paths = []; // gathers paths
298300

@@ -318,12 +320,21 @@ PrivateKey.prototype._fetchMessage = function(options) {
318320
}
319321

320322
return paths[0];
323+
});
324+
};
321325

322-
}).then(function(path) {
323-
return self._imap.listMessages({
324-
path: path,
326+
PrivateKey.prototype._fetchMessage = function(options) {
327+
var self = this;
328+
329+
if (!options.keyId) {
330+
return new Promise(function() {
331+
throw new Error('Incomplete arguments!');
325332
});
333+
}
326334

335+
// get the metadata for the message
336+
return self._imap.listMessages({
337+
path: options.path
327338
}).then(function(messages) {
328339
if (!messages.length) {
329340
// message has been deleted in the meantime
@@ -369,4 +380,4 @@ function filterBodyParts(bodyParts, type, result) {
369380
}
370381
});
371382
return result;
372-
}
383+
}

test/unit/service/privatekey-dao-test.js

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -104,35 +104,61 @@ describe('Private Key DAO unit tests', function() {
104104

105105
describe('isSynced', function() {
106106
beforeEach(function() {
107+
sinon.stub(privkeyDao, '_getFolder');
107108
sinon.stub(privkeyDao, '_fetchMessage');
108109
});
109110
afterEach(function() {
111+
privkeyDao._getFolder.restore();
110112
privkeyDao._fetchMessage.restore();
111113
});
112114

113115
it('should be synced', function(done) {
116+
117+
privkeyDao._getFolder.returns(resolves('foo'));
114118
privkeyDao._fetchMessage.returns(resolves({}));
115119

116120
privkeyDao.isSynced().then(function(synced) {
117121
expect(synced).to.be.true;
122+
expect(privkeyDao._getFolder.calledOnce).to.be.true;
123+
expect(privkeyDao._fetchMessage.calledOnce).to.be.true;
124+
118125
done();
119126
});
120127
});
121128

122129
it('should not be synced', function(done) {
130+
privkeyDao._getFolder.returns(resolves());
123131
privkeyDao._fetchMessage.returns(resolves());
124132

125133
privkeyDao.isSynced().then(function(synced) {
126134
expect(synced).to.be.false;
135+
expect(privkeyDao._getFolder.calledOnce).to.be.true;
136+
expect(privkeyDao._fetchMessage.calledOnce).to.be.true;
137+
127138
done();
128139
});
129140
});
130141

131142
it('should not be synced in case of error', function(done) {
143+
privkeyDao._getFolder.returns(rejects(new Error()));
144+
145+
privkeyDao.isSynced().then(function(synced) {
146+
expect(synced).to.be.false;
147+
expect(privkeyDao._getFolder.calledOnce).to.be.true;
148+
149+
done();
150+
});
151+
});
152+
153+
it('should not be synced in case of error', function(done) {
154+
privkeyDao._getFolder.returns(resolves('foo'));
132155
privkeyDao._fetchMessage.returns(rejects(new Error()));
133156

134157
privkeyDao.isSynced().then(function(synced) {
135158
expect(synced).to.be.false;
159+
expect(privkeyDao._getFolder.calledOnce).to.be.true;
160+
expect(privkeyDao._fetchMessage.calledOnce).to.be.true;
161+
136162
done();
137163
});
138164
});
@@ -146,15 +172,18 @@ describe('Private Key DAO unit tests', function() {
146172
}];
147173

148174
beforeEach(function() {
175+
sinon.stub(privkeyDao, '_getFolder');
149176
sinon.stub(privkeyDao, '_fetchMessage');
150177
sinon.stub(privkeyDao, '_parse');
151178
});
152179
afterEach(function() {
180+
privkeyDao._getFolder.restore();
153181
privkeyDao._fetchMessage.restore();
154182
privkeyDao._parse.restore();
155183
});
156184

157185
it('should fail if key not synced', function(done) {
186+
privkeyDao._getFolder.returns(resolves('foo'));
158187
privkeyDao._fetchMessage.returns(resolves());
159188

160189
privkeyDao.download({
@@ -167,6 +196,7 @@ describe('Private Key DAO unit tests', function() {
167196
});
168197

169198
it('should work', function(done) {
199+
privkeyDao._getFolder.returns(resolves('foo'));
170200
privkeyDao._fetchMessage.returns(resolves({}));
171201
imapClientStub.getBodyParts.returns(resolves());
172202
privkeyDao._parse.returns(resolves(root));
@@ -253,14 +283,7 @@ describe('Private Key DAO unit tests', function() {
253283
});
254284
});
255285

256-
describe('_fetchMessage', function() {
257-
it('should fail due to invalid args', function(done) {
258-
privkeyDao._fetchMessage({}).catch(function(err) {
259-
expect(err.message).to.match(/Incomplete/);
260-
done();
261-
});
262-
});
263-
286+
describe('_getFolder', function() {
264287
it('should fail if imap folder does not exist', function(done) {
265288
imapClientStub.listWellKnownFolders.returns(resolves({
266289
Inbox: [{
@@ -270,13 +293,10 @@ describe('Private Key DAO unit tests', function() {
270293
path: 'foo'
271294
}]
272295
}));
273-
imapClientStub.listMessages.returns(rejects(new Error()));
274296

275-
privkeyDao._fetchMessage({
276-
userId: emailAddress,
277-
keyId: keyId
278-
}).catch(function(err) {
297+
privkeyDao._getFolder().catch(function(err) {
279298
expect(err.message).to.match(/Imap folder/);
299+
expect(imapClientStub.listWellKnownFolders.calledOnce).to.be.true;
280300
done();
281301
});
282302
});
@@ -290,6 +310,25 @@ describe('Private Key DAO unit tests', function() {
290310
path: 'openpgp_keys'
291311
}]
292312
}));
313+
314+
privkeyDao._getFolder().then(function(path) {
315+
expect(path).to.equal('openpgp_keys');
316+
expect(imapClientStub.listWellKnownFolders.calledOnce).to.be.true;
317+
done();
318+
});
319+
});
320+
});
321+
322+
describe('_fetchMessage', function() {
323+
it('should fail due to invalid args', function(done) {
324+
privkeyDao._fetchMessage({}).catch(function(err) {
325+
expect(err.message).to.match(/Incomplete/);
326+
done();
327+
});
328+
});
329+
330+
331+
it('should work', function(done) {
293332
imapClientStub.listMessages.returns(resolves([{
294333
subject: keyId
295334
}]));
@@ -299,21 +338,12 @@ describe('Private Key DAO unit tests', function() {
299338
keyId: keyId
300339
}).then(function(msg) {
301340
expect(msg.subject).to.equal(keyId);
302-
expect(imapClientStub.listWellKnownFolders.calledOnce).to.be.true;
303341
expect(imapClientStub.listMessages.calledOnce).to.be.true;
304342
done();
305343
});
306344
});
307345

308346
it('should work with path prefix', function(done) {
309-
imapClientStub.listWellKnownFolders.returns(resolves({
310-
Inbox: [{
311-
path: 'INBOX'
312-
}],
313-
Other: [{
314-
path: 'INBOX.openpgp_keys'
315-
}]
316-
}));
317347
imapClientStub.listMessages.returns(resolves([{
318348
subject: keyId
319349
}]));
@@ -323,18 +353,12 @@ describe('Private Key DAO unit tests', function() {
323353
keyId: keyId
324354
}).then(function(msg) {
325355
expect(msg.subject).to.equal(keyId);
326-
expect(imapClientStub.listWellKnownFolders.calledOnce).to.be.true;
327356
expect(imapClientStub.listMessages.calledOnce).to.be.true;
328357
done();
329358
});
330359
});
331360

332361
it('should work for not matching message', function(done) {
333-
imapClientStub.listWellKnownFolders.returns(resolves({
334-
Other: [{
335-
path: 'INBOX.openpgp_keys'
336-
}]
337-
}));
338362
imapClientStub.listMessages.returns(resolves([{
339363
subject: '7890'
340364
}]));
@@ -349,11 +373,6 @@ describe('Private Key DAO unit tests', function() {
349373
});
350374

351375
it('should work for no messages', function(done) {
352-
imapClientStub.listWellKnownFolders.returns(resolves({
353-
Other: [{
354-
path: 'INBOX.openpgp_keys'
355-
}]
356-
}));
357376
imapClientStub.listMessages.returns(resolves([]));
358377

359378
privkeyDao._fetchMessage({

0 commit comments

Comments
 (0)