Refactor public-key.js to use embedded user id documents

This commit is contained in:
Tankred Hase
2016-06-09 17:07:51 +02:00
parent 7bc4f8a9d9
commit fe03ae213a
8 changed files with 905 additions and 480 deletions

View File

@@ -3,11 +3,10 @@
require('co-mocha')(require('mocha')); // monkey patch mocha for generators
const config = require('config');
const openpgp = require('openpgp');
const nodemailer = require('nodemailer');
const Email = require('../../src/email/email');
const Mongo = require('../../src/dao/mongo');
const UserId = require('../../src/service/user-id');
const PGP = require('../../src/service/pgp');
const PublicKey = require('../../src/service/public-key');
const expect = require('chai').expect;
const sinon = require('sinon');
@@ -15,23 +14,21 @@ const sinon = require('sinon');
describe('Public Key Integration Tests', function() {
this.timeout(20000);
let publicKey, email, mongo, userId,
let publicKey, email, mongo, pgp,
sendEmailStub, publicKeyArmored, emailParams;
const DB_TYPE_PUB_KEY = 'publickey';
const DB_TYPE_USER_ID = 'userid';
const primaryEmail = 'safewithme.testuser@gmail.com';
const DB_TYPE = 'publickey';
const primaryEmail = 'test1@example.com';
const origin = { host:'localhost', protocol:'http' };
before(function *() {
publicKeyArmored = require('fs').readFileSync(__dirname + '/../key1.asc', 'utf8');
publicKeyArmored = require('fs').readFileSync(__dirname + '/../key3.asc', 'utf8');
mongo = new Mongo();
yield mongo.init(config.mongo);
});
beforeEach(function *() {
yield mongo.clear(DB_TYPE_PUB_KEY);
yield mongo.clear(DB_TYPE_USER_ID);
yield mongo.clear(DB_TYPE);
emailParams = null;
sendEmailStub = sinon.stub().returns(Promise.resolve({ response:'250' }));
sendEmailStub.withArgs(sinon.match(recipient => {
@@ -49,8 +46,8 @@ describe('Public Key Integration Tests', function() {
auth: { user:'user', pass:'pass' },
sender: { name:'Foo Bar', email:'foo@bar.com' }
});
userId = new UserId(mongo);
publicKey = new PublicKey(openpgp, mongo, email, userId);
pgp = new PGP();
publicKey = new PublicKey(pgp, mongo, email);
});
afterEach(() => {
@@ -58,8 +55,7 @@ describe('Public Key Integration Tests', function() {
});
after(function *() {
yield mongo.clear(DB_TYPE_PUB_KEY);
yield mongo.clear(DB_TYPE_USER_ID);
yield mongo.clear(DB_TYPE);
yield mongo.disconnect();
});
@@ -83,7 +79,7 @@ describe('Public Key Integration Tests', function() {
it('should throw 304 if key already exists', function *() {
yield publicKey.put({ publicKeyArmored, primaryEmail, origin });
yield userId.verify(emailParams);
yield publicKey.verify(emailParams);
try {
yield publicKey.put({ publicKeyArmored, primaryEmail, origin });
expect(false).to.be.true;
@@ -93,26 +89,151 @@ describe('Public Key Integration Tests', function() {
});
});
describe('verify', () => {
beforeEach(function *() {
yield publicKey.put({ publicKeyArmored, primaryEmail, origin });
});
it('should update the document', function *() {
yield publicKey.verify(emailParams);
let gotten = yield mongo.get({ keyId:emailParams.keyId }, DB_TYPE);
expect(gotten.userIds[0].verified).to.be.true;
expect(gotten.userIds[0].nonce).to.be.null;
expect(gotten.userIds[1].verified).to.be.false;
expect(gotten.userIds[1].nonce).to.exist;
});
it('should not find the document', function *() {
try {
yield publicKey.verify({ keyId:emailParams.keyId, nonce:'fake_nonce' });
expect(true).to.be.false;
} catch(e) {
expect(e.status).to.equal(404);
}
let gotten = yield mongo.get({ keyId:emailParams.keyId }, DB_TYPE);
expect(gotten.userIds[0].verified).to.be.false;
expect(gotten.userIds[0].nonce).to.equal(emailParams.nonce);
expect(gotten.userIds[1].verified).to.be.false;
expect(gotten.userIds[1].nonce).to.exist;
});
});
describe('getVerified', () => {
let key;
describe('should find a verified key', () => {
beforeEach(function *() {
key = pgp.parseKey(publicKeyArmored);
yield publicKey.put({ publicKeyArmored, primaryEmail, origin });
yield publicKey.verify(emailParams);
});
it('by fingerprint', function *() {
let verified = yield publicKey.getVerified({ fingerprint:key.fingerprint });
expect(verified).to.exist;
});
it('by all userIds', function *() {
let verified = yield publicKey.getVerified({ userIds:key.userIds });
expect(verified).to.exist;
});
it('by verified userId', function *() {
let verified = yield publicKey.getVerified({ userIds:[key.userIds[0]] });
expect(verified).to.exist;
});
it('by unverified userId', function *() {
let verified = yield publicKey.getVerified({ userIds:[key.userIds[1]] });
expect(verified).to.not.exist;
});
it('by keyId', function *() {
let verified = yield publicKey.getVerified({ keyId:key.keyId });
expect(verified).to.exist;
});
it('by all params', function *() {
let verified = yield publicKey.getVerified(key);
expect(verified).to.exist;
});
});
describe('should not find an unverified key', () => {
beforeEach(function *() {
key = pgp.parseKey(publicKeyArmored);
key.userIds[0].verified = false;
yield mongo.create(key, DB_TYPE);
});
it('by fingerprint', function *() {
let verified = yield publicKey.getVerified({ fingerprint:key.fingerprint });
expect(verified).to.not.exist;
});
it('by userIds', function *() {
let verified = yield publicKey.getVerified({ userIds:key.userIds });
expect(verified).to.not.exist;
});
it('by keyId', function *() {
let verified = yield publicKey.getVerified({ keyId:key.keyId });
expect(verified).to.not.exist;
});
it('by all params', function *() {
let verified = yield publicKey.getVerified(key);
expect(verified).to.not.exist;
});
});
});
describe('get', () => {
beforeEach(function *() {
yield publicKey.put({ publicKeyArmored, primaryEmail, origin });
});
it('should return verified key by key id', function *() {
yield userId.verify(emailParams);
let key = yield publicKey.get({ keyid:emailParams.keyid });
expect(key.publicKeyArmored).to.equal(publicKeyArmored);
yield publicKey.verify(emailParams);
let key = yield publicKey.get({ keyId:emailParams.keyId });
expect(key.publicKeyArmored).to.exist;
});
it('should return verified key by key id (uppercase)', function *() {
yield publicKey.verify(emailParams);
let key = yield publicKey.get({ keyId:emailParams.keyId.toUpperCase() });
expect(key.publicKeyArmored).to.exist;
});
it('should return verified key by fingerprint', function *() {
yield publicKey.verify(emailParams);
let fingerprint = pgp.parseKey(publicKeyArmored).fingerprint;
let key = yield publicKey.get({ fingerprint });
expect(key.publicKeyArmored).to.exist;
});
it('should return verified key by fingerprint (uppercase)', function *() {
yield publicKey.verify(emailParams);
let fingerprint = pgp.parseKey(publicKeyArmored).fingerprint.toUpperCase();
let key = yield publicKey.get({ fingerprint });
expect(key.publicKeyArmored).to.exist;
});
it('should return verified key by email address', function *() {
yield userId.verify(emailParams);
yield publicKey.verify(emailParams);
let key = yield publicKey.get({ email:primaryEmail });
expect(key.publicKeyArmored).to.equal(publicKeyArmored);
expect(key.publicKeyArmored).to.exist;
});
it('should return verified key by email address (uppercase)', function *() {
yield publicKey.verify(emailParams);
let key = yield publicKey.get({ email:primaryEmail.toUpperCase() });
expect(key.publicKeyArmored).to.exist;
});
it('should throw 404 for unverified key', function *() {
try {
yield publicKey.get({ keyid:emailParams.keyid });
yield publicKey.get({ keyId:emailParams.keyId });
expect(false).to.be.true;
} catch(e) {
expect(e.status).to.equal(404);
@@ -121,23 +242,23 @@ describe('Public Key Integration Tests', function() {
});
describe('requestRemove', () => {
let keyid;
let keyId;
beforeEach(function *() {
yield publicKey.put({ publicKeyArmored, primaryEmail, origin });
keyid = emailParams.keyid;
keyId = emailParams.keyId;
});
it('should work for verified key', function *() {
yield userId.verify(emailParams);
yield publicKey.verify(emailParams);
emailParams = null;
yield publicKey.requestRemove({ keyid, origin });
yield publicKey.requestRemove({ keyId, origin });
expect(emailParams.nonce).to.exist;
});
it('should work for unverified key', function *() {
emailParams = null;
yield publicKey.requestRemove({ keyid, origin });
yield publicKey.requestRemove({ keyId, origin });
expect(emailParams.nonce).to.exist;
});
@@ -148,9 +269,9 @@ describe('Public Key Integration Tests', function() {
});
it('should throw 404 for no key', function *() {
yield publicKey.remove({ keyid });
yield mongo.remove({ keyId }, DB_TYPE);
try {
yield publicKey.requestRemove({ keyid, origin });
yield publicKey.requestRemove({ keyId, origin });
expect(false).to.be.true;
} catch(e) {
expect(e.status).to.equal(404);
@@ -159,25 +280,23 @@ describe('Public Key Integration Tests', function() {
});
describe('verifyRemove', () => {
let keyid;
let keyId;
beforeEach(function *() {
yield publicKey.put({ publicKeyArmored, primaryEmail, origin });
keyid = emailParams.keyid;
keyId = emailParams.keyId;
emailParams = null;
yield publicKey.requestRemove({ keyid, origin });
yield publicKey.requestRemove({ keyId, origin });
});
it('should remove key', function *() {
yield publicKey.verifyRemove(emailParams);
let uid = yield mongo.get({ keyid }, DB_TYPE_USER_ID);
expect(uid).to.not.exist;
let key = yield mongo.get({ _id:keyid }, DB_TYPE_PUB_KEY);
let key = yield mongo.get({ keyId }, DB_TYPE);
expect(key).to.not.exist;
});
it('should throw 404 for no key', function *() {
yield publicKey.remove({ keyid });
yield mongo.remove({ keyId }, DB_TYPE);
try {
yield publicKey.verifyRemove(emailParams);
expect(false).to.be.true;
@@ -187,21 +306,4 @@ describe('Public Key Integration Tests', function() {
});
});
describe('remove', () => {
let keyid;
beforeEach(function *() {
yield publicKey.put({ publicKeyArmored, primaryEmail, origin });
keyid = emailParams.keyid;
});
it('should remove key', function *() {
yield publicKey.remove({ keyid });
let uid = yield mongo.get({ keyid }, DB_TYPE_USER_ID);
expect(uid).to.not.exist;
let key = yield mongo.get({ _id:keyid }, DB_TYPE_PUB_KEY);
expect(key).to.not.exist;
});
});
});