From 33d3ad737e466ff4e4faa6747daa530392a151fe Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Tue, 31 May 2016 17:17:17 +0200 Subject: [PATCH] Implement and test key removal in user-id.js --- src/service/user-id.js | 43 +++++++++++++++++++++++++------- test/integration/user-id-test.js | 34 +++++++++++++++++++++++-- 2 files changed, 66 insertions(+), 11 deletions(-) diff --git a/src/service/user-id.js b/src/service/user-id.js index 98b4c4a..131499b 100644 --- a/src/service/user-id.js +++ b/src/service/user-id.js @@ -109,19 +109,44 @@ class UserId { /** * Flag all user IDs of a key for removal by generating a new nonce and - * saving it. - * @param {String} keyid The public key id + * saving it. Either a key id or email address must be provided + * @param {String} keyid (optional) The public key id + * @param {String} email (optional) The user's email address * @yield {Array} A list of user ids with nonces */ *flagForRemove(options) { - let keyid = options.keyid; - let uids = yield this._mongo.list({ keyid }, DB_TYPE); - for (let uid of uids) { - let nonce = uuid.v4(); - yield this._mongo.update(uid, { nonce }, DB_TYPE); - uid.nonce = nonce; + let keyid = options.keyid, email = options.email; + if (email) { + let uid = yield this._mongo.get({ email }, DB_TYPE); + if (uid) { + let nonce = uuid.v4(); + yield this._mongo.update(uid, { nonce }, DB_TYPE); + uid.nonce = nonce; + return [uid]; + } } - return uids; + if (keyid) { + let uids = yield this._mongo.list({ keyid }, DB_TYPE); + for (let uid of uids) { + let nonce = uuid.v4(); + yield this._mongo.update(uid, { nonce }, DB_TYPE); + uid.nonce = nonce; + } + return uids; + } + return []; + } + + /** + * get user id which has been flagged for removal by proving knowledge of + * the nonce. + * @param {string} keyid public key id + * @param {string} nonce The verification nonce proving email address ownership + * @yield {Object} The matching user id document + */ + *getFlaggedForRemove(options) { + let keyid = options.keyid, nonce = options.nonce; + return yield this._mongo.get({ keyid, nonce }, DB_TYPE); } /** diff --git a/test/integration/user-id-test.js b/test/integration/user-id-test.js index e3a0388..7f842cd 100644 --- a/test/integration/user-id-test.js +++ b/test/integration/user-id-test.js @@ -101,14 +101,44 @@ describe('User ID Integration Tests', function() { }); describe("flagForRemove", () => { - it('should flag all documents', function *() { - let stored = yield userId.batch({ userIds:[uid1, uid2], keyid }); + let stored; + beforeEach(function *() { + stored = yield userId.batch({ userIds:[uid1, uid2], keyid }); + }); + + it('should flag one documents for email param', function *() { + let flagged = yield userId.flagForRemove({ email:uid1.email }); + expect(flagged.length).to.equal(1); + expect(flagged[0]._id.toHexString()).to.equal(stored[0]._id.toHexString()); + expect(flagged[0].nonce).to.not.equal(stored[0].nonce); + let gotten = yield mongo.list({ email:uid1.email }, DB_TYPE); + expect(gotten).to.deep.equal(flagged); + }); + + it('should flag all documents for key id param', function *() { let flagged = yield userId.flagForRemove({ keyid }); + expect(flagged.length).to.equal(2); expect(flagged[0]._id.toHexString()).to.equal(stored[0]._id.toHexString()); expect(flagged[0].nonce).to.not.equal(stored[0].nonce); let gotten = yield mongo.list({ keyid }, DB_TYPE); expect(gotten).to.deep.equal(flagged); }); + + it('should flag no documents no param', function *() { + let flagged = yield userId.flagForRemove({}); + expect(flagged.length).to.equal(0); + let gotten = yield mongo.list({ keyid }, DB_TYPE); + expect(gotten).to.deep.equal(stored); + }); + }); + + describe("getFlaggedForRemove", () => { + it('should find flagged document', function *() { + yield userId.batch({ userIds:[uid1, uid2], keyid }); + let flagged = yield userId.flagForRemove({ keyid }); + let gotten = yield userId.getFlaggedForRemove({ keyid, nonce:flagged[0].nonce }); + expect(gotten).to.exist; + }); }); describe("remove", () => {