2016-05-31 09:02:27 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
require('co-mocha')(require('mocha')); // monkey patch mocha for generators
|
|
|
|
|
2016-06-07 12:56:55 +00:00
|
|
|
const config = require('config');
|
2016-05-31 09:02:27 +00:00
|
|
|
const UserId = require('../../src/service/user-id');
|
|
|
|
const Mongo = require('../../src/dao/mongo');
|
|
|
|
const expect = require('chai').expect;
|
|
|
|
|
|
|
|
describe('User ID Integration Tests', function() {
|
|
|
|
this.timeout(20000);
|
|
|
|
|
|
|
|
const DB_TYPE = 'userid';
|
2016-05-31 10:16:46 +00:00
|
|
|
const keyid = '0123456789ABCDEF';
|
2016-05-31 09:02:27 +00:00
|
|
|
let mongo, userId, uid1, uid2;
|
|
|
|
|
|
|
|
before(function *() {
|
2016-06-08 12:01:30 +00:00
|
|
|
mongo = new Mongo();
|
|
|
|
yield mongo.init(config.mongo);
|
2016-05-31 09:02:27 +00:00
|
|
|
userId = new UserId(mongo);
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(function *() {
|
|
|
|
uid1 = {
|
|
|
|
name: 'name1',
|
|
|
|
email: 'email1'
|
|
|
|
};
|
|
|
|
uid2 = {
|
|
|
|
name: 'name2',
|
|
|
|
email: 'email2'
|
|
|
|
};
|
|
|
|
yield mongo.clear(DB_TYPE);
|
|
|
|
});
|
|
|
|
|
|
|
|
after(function *() {
|
|
|
|
yield mongo.clear(DB_TYPE);
|
|
|
|
yield mongo.disconnect();
|
|
|
|
});
|
|
|
|
|
2016-05-31 11:52:18 +00:00
|
|
|
describe("batch", () => {
|
2016-05-31 09:02:27 +00:00
|
|
|
it('should persist all the things', function *() {
|
2016-05-31 10:16:46 +00:00
|
|
|
let uids = yield userId.batch({ userIds:[uid1, uid2], keyid });
|
|
|
|
expect(uids[0].keyid).to.equal(keyid);
|
|
|
|
expect(uids[1].keyid).to.equal(keyid);
|
2016-05-31 09:02:27 +00:00
|
|
|
expect(uids[0].nonce).to.exist;
|
|
|
|
expect(uids[1].nonce).to.exist;
|
|
|
|
expect(uids[0]._id).to.exist;
|
|
|
|
expect(uids[1]._id).to.exist;
|
2016-05-31 10:16:46 +00:00
|
|
|
let gotten = yield mongo.list({ keyid }, DB_TYPE);
|
2016-05-31 09:02:27 +00:00
|
|
|
expect(gotten).to.deep.equal(uids);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-31 11:52:18 +00:00
|
|
|
describe("verify", () => {
|
2016-05-31 09:02:27 +00:00
|
|
|
it('should update the document', function *() {
|
2016-05-31 10:16:46 +00:00
|
|
|
let uids = yield userId.batch({ userIds:[uid1], keyid });
|
|
|
|
yield userId.verify({ keyid, nonce:uids[0].nonce });
|
2016-05-31 09:02:27 +00:00
|
|
|
let gotten = yield mongo.get({ _id:uid1._id }, DB_TYPE);
|
|
|
|
expect(gotten.verified).to.be.true;
|
|
|
|
expect(gotten.nonce).to.be.null;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not find the document', function *() {
|
2016-05-31 10:16:46 +00:00
|
|
|
yield userId.batch({ userIds:[uid1], keyid });
|
2016-05-31 09:02:27 +00:00
|
|
|
try {
|
2016-05-31 10:16:46 +00:00
|
|
|
yield userId.verify({ keyid, nonce:'fake_nonce' });
|
2016-05-31 09:02:27 +00:00
|
|
|
} catch(e) {
|
|
|
|
expect(e.status).to.equal(404);
|
|
|
|
}
|
|
|
|
let gotten = yield mongo.get({ _id:uid1._id }, DB_TYPE);
|
|
|
|
expect(gotten.verified).to.be.undefined;
|
|
|
|
expect(gotten.nonce).to.exist;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-31 11:52:18 +00:00
|
|
|
describe("getVerfied", () => {
|
2016-05-31 09:02:27 +00:00
|
|
|
beforeEach(function *() {
|
2016-05-31 10:16:46 +00:00
|
|
|
let uids = yield userId.batch({ userIds:[uid1], keyid });
|
|
|
|
yield userId.verify({ keyid, nonce:uids[0].nonce });
|
2016-05-31 09:02:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should find verified by key id', function *() {
|
2016-05-31 10:16:46 +00:00
|
|
|
let gotten = yield userId.getVerfied({ keyid });
|
2016-05-31 09:02:27 +00:00
|
|
|
expect(gotten).to.exist;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find verified by email address', function *() {
|
|
|
|
let gotten = yield userId.getVerfied({ userIds:[uid2,uid1] });
|
|
|
|
expect(gotten).to.exist;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-31 11:52:18 +00:00
|
|
|
describe("flagForRemove", () => {
|
2016-05-31 15:17:17 +00:00
|
|
|
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 *() {
|
2016-05-31 10:16:46 +00:00
|
|
|
let flagged = yield userId.flagForRemove({ keyid });
|
2016-05-31 15:17:17 +00:00
|
|
|
expect(flagged.length).to.equal(2);
|
2016-05-31 10:16:46 +00:00
|
|
|
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);
|
|
|
|
});
|
2016-05-31 15:17:17 +00:00
|
|
|
|
2016-06-01 07:32:29 +00:00
|
|
|
it('should flag no documents for wrong key id param', function *() {
|
|
|
|
let flagged = yield userId.flagForRemove({ keyid:'4' });
|
|
|
|
expect(flagged.length).to.equal(0);
|
|
|
|
});
|
|
|
|
|
2016-05-31 15:17:17 +00:00
|
|
|
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;
|
|
|
|
});
|
2016-05-31 10:16:46 +00:00
|
|
|
});
|
|
|
|
|
2016-05-31 11:52:18 +00:00
|
|
|
describe("remove", () => {
|
2016-05-31 09:02:27 +00:00
|
|
|
it('should delete all documents', function *() {
|
2016-05-31 10:16:46 +00:00
|
|
|
yield userId.batch({ userIds:[uid1, uid2], keyid });
|
|
|
|
yield userId.remove({ keyid });
|
|
|
|
let gotten = yield mongo.get({ keyid }, DB_TYPE);
|
2016-05-31 09:02:27 +00:00
|
|
|
expect(gotten).to.not.exist;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|