Implement and test key removal in user-id.js

This commit is contained in:
Tankred Hase 2016-05-31 17:17:17 +02:00
parent 27155278a8
commit 33d3ad737e
2 changed files with 66 additions and 11 deletions

View File

@ -109,12 +109,23 @@ class UserId {
/** /**
* Flag all user IDs of a key for removal by generating a new nonce and * Flag all user IDs of a key for removal by generating a new nonce and
* saving it. * saving it. Either a key id or email address must be provided
* @param {String} keyid The public key id * @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 * @yield {Array} A list of user ids with nonces
*/ */
*flagForRemove(options) { *flagForRemove(options) {
let keyid = options.keyid; 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];
}
}
if (keyid) {
let uids = yield this._mongo.list({ keyid }, DB_TYPE); let uids = yield this._mongo.list({ keyid }, DB_TYPE);
for (let uid of uids) { for (let uid of uids) {
let nonce = uuid.v4(); let nonce = uuid.v4();
@ -123,6 +134,20 @@ class UserId {
} }
return uids; 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);
}
/** /**
* Remove all user ids for a public key. * Remove all user ids for a public key.

View File

@ -101,14 +101,44 @@ describe('User ID Integration Tests', function() {
}); });
describe("flagForRemove", () => { describe("flagForRemove", () => {
it('should flag all documents', function *() { let stored;
let stored = yield userId.batch({ userIds:[uid1, uid2], keyid }); 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 }); 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]._id.toHexString()).to.equal(stored[0]._id.toHexString());
expect(flagged[0].nonce).to.not.equal(stored[0].nonce); expect(flagged[0].nonce).to.not.equal(stored[0].nonce);
let gotten = yield mongo.list({ keyid }, DB_TYPE); let gotten = yield mongo.list({ keyid }, DB_TYPE);
expect(gotten).to.deep.equal(flagged); 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", () => { describe("remove", () => {