Fix eslint errors

This commit is contained in:
Tankred Hase
2017-08-15 16:03:06 +08:00
parent 750cf3d897
commit e9251d5203
20 changed files with 355 additions and 347 deletions

View File

@@ -9,8 +9,11 @@ const fs = require('fs');
describe('Koa App (HTTP Server) Integration Tests', function() {
this.timeout(20000);
let app, mongo,
sendEmailStub, publicKeyArmored, emailParams;
let app;
let mongo;
let sendEmailStub;
let publicKeyArmored;
let emailParams;
const DB_TYPE_PUB_KEY = 'publickey';
const DB_TYPE_USER_ID = 'userid';
@@ -18,24 +21,22 @@ describe('Koa App (HTTP Server) Integration Tests', function() {
const fingerprint = '4277257930867231CE393FB8DBC0B3D92B1B86E9';
before(function *() {
publicKeyArmored = fs.readFileSync(__dirname + '/../key1.asc', 'utf8');
publicKeyArmored = fs.readFileSync(`${__dirname}/../key1.asc`, 'utf8');
mongo = new Mongo();
yield mongo.init(config.mongo);
sendEmailStub = sinon.stub().returns(Promise.resolve({ response:'250' }));
sendEmailStub.withArgs(sinon.match(recipient => {
return recipient.to.address === primaryEmail;
}), sinon.match(params => {
sendEmailStub = sinon.stub().returns(Promise.resolve({response: '250'}));
sendEmailStub.withArgs(sinon.match(recipient => recipient.to.address === primaryEmail), sinon.match(params => {
emailParams = params;
return !!params.nonce;
return Boolean(params.nonce);
}));
sinon.stub(nodemailer, 'createTransport').returns({
templateSender: () => { return sendEmailStub; },
use: function() {}
templateSender: () => sendEmailStub,
use() {}
});
global.testing = true;
let init = require('../../src/app');
const init = require('../../src/app');
app = yield init();
});
@@ -57,7 +58,7 @@ describe('Koa App (HTTP Server) Integration Tests', function() {
it('should return 400 for an invalid pgp key', done => {
request(app.listen())
.post('/api/v1/key')
.send({ publicKeyArmored:'foo' })
.send({publicKeyArmored: 'foo'})
.expect(400)
.end(done);
});
@@ -65,7 +66,7 @@ describe('Koa App (HTTP Server) Integration Tests', function() {
it('should return 400 for an invalid primaryEmail', done => {
request(app.listen())
.post('/api/v1/key')
.send({ publicKeyArmored, primaryEmail:'foo' })
.send({publicKeyArmored, primaryEmail: 'foo'})
.expect(400)
.end(done);
});
@@ -73,7 +74,7 @@ describe('Koa App (HTTP Server) Integration Tests', function() {
it('should return 201 with primaryEmail', done => {
request(app.listen())
.post('/api/v1/key')
.send({ publicKeyArmored, primaryEmail })
.send({publicKeyArmored, primaryEmail})
.expect(201)
.end(() => {
expect(emailParams).to.exist;
@@ -84,7 +85,7 @@ describe('Koa App (HTTP Server) Integration Tests', function() {
it('should return 201 without primaryEmail', done => {
request(app.listen())
.post('/api/v1/key')
.send({ publicKeyArmored })
.send({publicKeyArmored})
.expect(201)
.end(() => {
expect(emailParams).to.exist;
@@ -97,28 +98,28 @@ describe('Koa App (HTTP Server) Integration Tests', function() {
beforeEach(done => {
request(app.listen())
.post('/api/v1/key')
.send({ publicKeyArmored, primaryEmail })
.send({publicKeyArmored, primaryEmail})
.expect(201)
.end(done);
});
it('should return 200 for valid params', done => {
request(app.listen())
.get('/api/v1/key?op=verify&keyId=' + emailParams.keyId + '&nonce=' + emailParams.nonce)
.get(`/api/v1/key?op=verify&keyId=${emailParams.keyId}&nonce=${emailParams.nonce}`)
.expect(200)
.end(done);
});
it('should return 400 for missing keyid and', done => {
request(app.listen())
.get('/api/v1/key?op=verify&nonce=' + emailParams.nonce)
.get(`/api/v1/key?op=verify&nonce=${emailParams.nonce}`)
.expect(400)
.end(done);
});
it('should return 400 for missing nonce', done => {
request(app.listen())
.get('/api/v1/key?op=verify&keyId=' + emailParams.keyId)
.get(`/api/v1/key?op=verify&keyId=${emailParams.keyId}`)
.expect(400)
.end(done);
});
@@ -128,7 +129,7 @@ describe('Koa App (HTTP Server) Integration Tests', function() {
beforeEach(done => {
request(app.listen())
.post('/api/v1/key')
.send({ publicKeyArmored, primaryEmail })
.send({publicKeyArmored, primaryEmail})
.expect(201)
.end(done);
});
@@ -136,7 +137,7 @@ describe('Koa App (HTTP Server) Integration Tests', function() {
describe('Not yet verified', () => {
it('should return 404', done => {
request(app.listen())
.get('/api/v1/key?keyId=' + emailParams.keyId)
.get(`/api/v1/key?keyId=${emailParams.keyId}`)
.expect(404).end(done);
});
});
@@ -144,21 +145,21 @@ describe('Koa App (HTTP Server) Integration Tests', function() {
describe('Verified', () => {
beforeEach(done => {
request(app.listen())
.get('/api/v1/key?op=verify&keyId=' + emailParams.keyId + '&nonce=' + emailParams.nonce)
.get(`/api/v1/key?op=verify&keyId=${emailParams.keyId}&nonce=${emailParams.nonce}`)
.expect(200)
.end(done);
});
it('should return 200 and get key by id', done => {
request(app.listen())
.get('/api/v1/key?keyId=' + emailParams.keyId)
.get(`/api/v1/key?keyId=${emailParams.keyId}`)
.expect(200)
.end(done);
});
it('should return 200 and get key email address', done => {
request(app.listen())
.get('/api/v1/key?email=' + primaryEmail)
.get(`/api/v1/key?email=${primaryEmail}`)
.expect(200)
.end(done);
});
@@ -190,21 +191,21 @@ describe('Koa App (HTTP Server) Integration Tests', function() {
beforeEach(done => {
request(app.listen())
.post('/api/v1/key')
.send({ publicKeyArmored, primaryEmail })
.send({publicKeyArmored, primaryEmail})
.expect(201)
.end(done);
});
it('should return 202 for key id', done => {
request(app.listen())
.del('/api/v1/key?keyId=' + emailParams.keyId)
.del(`/api/v1/key?keyId=${emailParams.keyId}`)
.expect(202)
.end(done);
});
it('should return 202 for email address', done => {
request(app.listen())
.del('/api/v1/key?email=' + primaryEmail)
.del(`/api/v1/key?email=${primaryEmail}`)
.expect(202)
.end(done);
});
@@ -228,11 +229,11 @@ describe('Koa App (HTTP Server) Integration Tests', function() {
beforeEach(done => {
request(app.listen())
.post('/api/v1/key')
.send({ publicKeyArmored, primaryEmail })
.send({publicKeyArmored, primaryEmail})
.expect(201)
.end(function() {
.end(() => {
request(app.listen())
.del('/api/v1/key?keyId=' + emailParams.keyId)
.del(`/api/v1/key?keyId=${emailParams.keyId}`)
.expect(202)
.end(done);
});
@@ -240,7 +241,7 @@ describe('Koa App (HTTP Server) Integration Tests', function() {
it('should return 200 for key id', done => {
request(app.listen())
.get('/api/v1/key?op=verifyRemove&keyId=' + emailParams.keyId + '&nonce=' + emailParams.nonce)
.get(`/api/v1/key?op=verifyRemove&keyId=${emailParams.keyId}&nonce=${emailParams.nonce}`)
.expect(200)
.end(done);
});
@@ -254,7 +255,7 @@ describe('Koa App (HTTP Server) Integration Tests', function() {
it('should return 404 for unknown key id', done => {
request(app.listen())
.get('/api/v1/key?op=verifyRemove&keyId=0123456789ABCDEF&nonce=' + emailParams.nonce)
.get(`/api/v1/key?op=verifyRemove&keyId=0123456789ABCDEF&nonce=${emailParams.nonce}`)
.expect(404)
.end(done);
});
@@ -276,7 +277,7 @@ describe('Koa App (HTTP Server) Integration Tests', function() {
request(app.listen())
.post('/pks/add')
.type('form')
.send('keytext=' + encodeURIComponent(publicKeyArmored))
.send(`keytext=${encodeURIComponent(publicKeyArmored)}`)
.expect(201)
.end(done);
});
@@ -287,7 +288,7 @@ describe('Koa App (HTTP Server) Integration Tests', function() {
request(app.listen())
.post('/pks/add')
.type('form')
.send('keytext=' + encodeURIComponent(publicKeyArmored))
.send(`keytext=${encodeURIComponent(publicKeyArmored)}`)
.expect(201)
.end(done);
});
@@ -295,7 +296,7 @@ describe('Koa App (HTTP Server) Integration Tests', function() {
describe('Not yet verified', () => {
it('should return 404', done => {
request(app.listen())
.get('/pks/lookup?op=get&search=0x' + emailParams.keyId)
.get(`/pks/lookup?op=get&search=0x${emailParams.keyId}`)
.expect(404)
.end(done);
});
@@ -304,35 +305,35 @@ describe('Koa App (HTTP Server) Integration Tests', function() {
describe('Verified', () => {
beforeEach(done => {
request(app.listen())
.get('/api/v1/key?op=verify&keyId=' + emailParams.keyId + '&nonce=' + emailParams.nonce)
.get(`/api/v1/key?op=verify&keyId=${emailParams.keyId}&nonce=${emailParams.nonce}`)
.expect(200)
.end(done);
});
it('should return 200 for key id', done => {
request(app.listen())
.get('/pks/lookup?op=get&search=0x' + emailParams.keyId)
.get(`/pks/lookup?op=get&search=0x${emailParams.keyId}`)
.expect(200, publicKeyArmored)
.end(done);
});
it('should return 200 for fingerprint', done => {
request(app.listen())
.get('/pks/lookup?op=get&search=0x' + fingerprint)
.get(`/pks/lookup?op=get&search=0x${fingerprint}`)
.expect(200, publicKeyArmored)
.end(done);
});
it('should return 200 for correct email address', done => {
request(app.listen())
.get('/pks/lookup?op=get&search=' + primaryEmail)
.get(`/pks/lookup?op=get&search=${primaryEmail}`)
.expect(200, publicKeyArmored)
.end(done);
});
it('should return 200 for "mr" option', done => {
request(app.listen())
.get('/pks/lookup?op=get&options=mr&search=' + primaryEmail)
.get(`/pks/lookup?op=get&options=mr&search=${primaryEmail}`)
.expect('Content-Type', 'application/pgp-keys; charset=utf-8')
.expect('Content-Disposition', 'attachment; filename=openpgpkey.asc')
.expect(200, publicKeyArmored)
@@ -341,14 +342,14 @@ describe('Koa App (HTTP Server) Integration Tests', function() {
it('should return 200 for "vindex" op', done => {
request(app.listen())
.get('/pks/lookup?op=vindex&search=0x' + emailParams.keyId)
.get(`/pks/lookup?op=vindex&search=0x${emailParams.keyId}`)
.expect(200)
.end(done);
});
it('should return 200 for "index" with "mr" option', done => {
request(app.listen())
.get('/pks/lookup?op=index&options=mr&search=0x' + emailParams.keyId)
.get(`/pks/lookup?op=index&options=mr&search=0x${emailParams.keyId}`)
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect(200)
.end(done);
@@ -377,7 +378,7 @@ describe('Koa App (HTTP Server) Integration Tests', function() {
it('should return 501 for a invalid key id format', done => {
request(app.listen())
.get('/pks/lookup?op=get&search=' + emailParams.keyId)
.get(`/pks/lookup?op=get&search=${emailParams.keyId}`)
.expect(501)
.end(done);
});
@@ -398,12 +399,11 @@ describe('Koa App (HTTP Server) Integration Tests', function() {
it('should return 501 (Not implemented) for "x-email" op', done => {
request(app.listen())
.get('/pks/lookup?op=x-email&search=0x' + emailParams.keyId)
.get(`/pks/lookup?op=x-email&search=0x${emailParams.keyId}`)
.expect(501)
.end(done);
});
});
});
});
});
});

View File

@@ -7,15 +7,19 @@ const tpl = require('../../src/email/templates.json');
describe('Email Integration Tests', function() {
this.timeout(20000);
let email, keyId, userId, origin, publicKeyArmored;
let email;
let keyId;
let userId;
let origin;
let publicKeyArmored;
const recipient = { name:'Test User', email:'safewithme.testuser@gmail.com' };
const recipient = {name: 'Test User', email: 'safewithme.testuser@gmail.com'};
before(function() {
publicKeyArmored = require('fs').readFileSync(__dirname + '/../key1.asc', 'utf8');
before(() => {
publicKeyArmored = require('fs').readFileSync(`${__dirname}/../key1.asc`, 'utf8');
origin = {
protocol: 'http',
host: 'localhost:' + config.server.port
host: `localhost:${config.server.port}`
};
email = new Email();
email.init(config.email);
@@ -33,14 +37,14 @@ describe('Email Integration Tests', function() {
describe("_sendHelper", () => {
it('should work', function *() {
let mailOptions = {
const mailOptions = {
from: email._sender,
to: recipient,
subject: 'Hello ✔', // Subject line
text: 'Hello world 🐴', // plaintext body
html: '<b>Hello world 🐴</b>' // html body
};
let info = yield email._sendHelper(mailOptions);
const info = yield email._sendHelper(mailOptions);
expect(info).to.exist;
});
});
@@ -48,23 +52,22 @@ describe('Email Integration Tests', function() {
describe("send verifyKey template", () => {
it('should send plaintext email', function *() {
delete userId.publicKeyArmored;
yield email.send({ template:tpl.verifyKey, userId, keyId, origin });
yield email.send({template: tpl.verifyKey, userId, keyId, origin});
});
it('should send pgp encrypted email', function *() {
yield email.send({ template:tpl.verifyKey, userId, keyId, origin });
yield email.send({template: tpl.verifyKey, userId, keyId, origin});
});
});
describe("send verifyRemove template", () => {
it('should send plaintext email', function *() {
delete userId.publicKeyArmored;
yield email.send({ template:tpl.verifyRemove, userId, keyId, origin });
yield email.send({template: tpl.verifyRemove, userId, keyId, origin});
});
it('should send pgp encrypted email', function *() {
yield email.send({ template:tpl.verifyRemove, userId, keyId, origin });
yield email.send({template: tpl.verifyRemove, userId, keyId, origin});
});
});
});
});

View File

@@ -25,16 +25,16 @@ describe('Mongo Integration Tests', function() {
describe("create", () => {
it('should insert a document', function *() {
let r = yield mongo.create({ _id:'0' }, DB_TYPE);
const r = yield mongo.create({_id: '0'}, DB_TYPE);
expect(r.insertedCount).to.equal(1);
});
it('should fail if two with the same ID are inserted', function *() {
let r = yield mongo.create({ _id:'0' }, DB_TYPE);
let r = yield mongo.create({_id: '0'}, DB_TYPE);
expect(r.insertedCount).to.equal(1);
try {
r = yield mongo.create({ _id:'0' }, DB_TYPE);
} catch(e) {
r = yield mongo.create({_id: '0'}, DB_TYPE);
} catch (e) {
expect(e.message).to.match(/duplicate/);
}
});
@@ -42,16 +42,16 @@ describe('Mongo Integration Tests', function() {
describe("batch", () => {
it('should insert a document', function *() {
let r = yield mongo.batch([{ _id:'0' }, { _id:'1' }], DB_TYPE);
const r = yield mongo.batch([{_id: '0'}, {_id: '1'}], DB_TYPE);
expect(r.insertedCount).to.equal(2);
});
it('should fail if docs with the same ID are inserted', function *() {
let r = yield mongo.batch([{ _id:'0' }, { _id:'1' }], DB_TYPE);
let r = yield mongo.batch([{_id: '0'}, {_id: '1'}], DB_TYPE);
expect(r.insertedCount).to.equal(2);
try {
r = yield mongo.batch([{ _id:'0' }, { _id:'1' }], DB_TYPE);
} catch(e) {
r = yield mongo.batch([{_id: '0'}, {_id: '1'}], DB_TYPE);
} catch (e) {
expect(e.message).to.match(/duplicate/);
}
});
@@ -59,37 +59,36 @@ describe('Mongo Integration Tests', function() {
describe("update", () => {
it('should update a document', function *() {
let r = yield mongo.create({ _id:'0' }, DB_TYPE);
r = yield mongo.update({ _id:'0' }, { foo:'bar' }, DB_TYPE);
let r = yield mongo.create({_id: '0'}, DB_TYPE);
r = yield mongo.update({_id: '0'}, {foo: 'bar'}, DB_TYPE);
expect(r.modifiedCount).to.equal(1);
r = yield mongo.get({ _id:'0' }, DB_TYPE);
r = yield mongo.get({_id: '0'}, DB_TYPE);
expect(r.foo).to.equal('bar');
});
});
describe("get", () => {
it('should get a document', function *() {
let r = yield mongo.create({ _id:'0' }, DB_TYPE);
r = yield mongo.get({ _id:'0' }, DB_TYPE);
let r = yield mongo.create({_id: '0'}, DB_TYPE);
r = yield mongo.get({_id: '0'}, DB_TYPE);
expect(r).to.exist;
});
});
describe("list", () => {
it('should list documents', function *() {
let r = yield mongo.batch([{ _id:'0', foo:'bar' }, { _id:'1', foo:'bar' }], DB_TYPE);
r = yield mongo.list({ foo:'bar' }, DB_TYPE);
expect(r).to.deep.equal([{ _id:'0', foo:'bar' }, { _id:'1', foo:'bar' }], DB_TYPE);
let r = yield mongo.batch([{_id: '0', foo: 'bar'}, {_id: '1', foo: 'bar'}], DB_TYPE);
r = yield mongo.list({foo: 'bar'}, DB_TYPE);
expect(r).to.deep.equal([{_id: '0', foo: 'bar'}, {_id: '1', foo: 'bar'}], DB_TYPE);
});
});
describe("remove", () => {
it('should remove a document', function *() {
let r = yield mongo.create({ _id:'0' }, DB_TYPE);
r = yield mongo.remove({ _id:'0' }, DB_TYPE);
r = yield mongo.get({ _id:'0' }, DB_TYPE);
let r = yield mongo.create({_id: '0'}, DB_TYPE);
r = yield mongo.remove({_id: '0'}, DB_TYPE);
r = yield mongo.get({_id: '0'}, DB_TYPE);
expect(r).to.not.exist;
});
});
});
});

View File

@@ -10,17 +10,23 @@ const PublicKey = require('../../src/service/public-key');
describe('Public Key Integration Tests', function() {
this.timeout(20000);
let publicKey, email, mongo, pgp,
sendEmailStub, publicKeyArmored, publicKeyArmored2, mailsSent;
let publicKey;
let email;
let mongo;
let pgp;
let sendEmailStub;
let publicKeyArmored;
let publicKeyArmored2;
let mailsSent;
const DB_TYPE = 'publickey';
const primaryEmail = 'test1@example.com';
const primaryEmail2 = 'test2@example.com';
const origin = { host:'localhost', protocol:'http' };
const origin = {host: 'localhost', protocol: 'http'};
before(function *() {
publicKeyArmored = require('fs').readFileSync(__dirname + '/../key3.asc', 'utf8');
publicKeyArmored2 = require('fs').readFileSync(__dirname + '/../key4.asc', 'utf8');
publicKeyArmored = require('fs').readFileSync(`${__dirname}/../key3.asc`, 'utf8');
publicKeyArmored2 = require('fs').readFileSync(`${__dirname}/../key4.asc`, 'utf8');
mongo = new Mongo();
yield mongo.init(config.mongo);
});
@@ -28,9 +34,9 @@ describe('Public Key Integration Tests', function() {
beforeEach(function *() {
yield mongo.clear(DB_TYPE);
mailsSent = [];
sendEmailStub = sinon.stub().returns(Promise.resolve({ response:'250' }));
sendEmailStub = sinon.stub().returns(Promise.resolve({response: '250'}));
sendEmailStub.withArgs(sinon.match(recipient => {
mailsSent[mailsSent.length] = {to:recipient.to.address};
mailsSent[mailsSent.length] = {to: recipient.to.address};
return true;
}), sinon.match(params => {
mailsSent[mailsSent.length - 1].params = params;
@@ -39,13 +45,13 @@ describe('Public Key Integration Tests', function() {
return true;
}));
sinon.stub(nodemailer, 'createTransport').returns({
templateSender: () => { return sendEmailStub; }
templateSender: () => sendEmailStub
});
email = new Email(nodemailer);
email.init({
host: 'localhost',
auth: { user:'user', pass:'pass' },
sender: { name:'Foo Bar', email:'foo@bar.com' }
auth: {user: 'user', pass: 'pass'},
sender: {name: 'Foo Bar', email: 'foo@bar.com'}
});
pgp = new PGP();
publicKey = new PublicKey(pgp, mongo, email);
@@ -62,31 +68,31 @@ describe('Public Key Integration Tests', function() {
describe('put', () => {
it('should persist key and send verification email with primaryEmail', function *() {
yield publicKey.put({ publicKeyArmored, primaryEmail, origin });
yield publicKey.put({publicKeyArmored, primaryEmail, origin});
expect(mailsSent.length).to.equal(1);
expect(mailsSent[0].to).to.equal(primaryEmail);
expect(mailsSent[0].params.keyId).to.exist;
expect(mailsSent[0].params.nonce).to.exist;
});
it('should persist key and send verification email without primaryEmail', function *() {
yield publicKey.put({ publicKeyArmored, origin });
yield publicKey.put({publicKeyArmored, origin});
expect(mailsSent.length).to.equal(4);
});
it('should work twice if not yet verified', function *() {
yield publicKey.put({ publicKeyArmored, primaryEmail, origin });
yield publicKey.put({publicKeyArmored, primaryEmail, origin});
expect(mailsSent.length).to.equal(1);
yield publicKey.put({ publicKeyArmored, primaryEmail, origin });
yield publicKey.put({publicKeyArmored, primaryEmail, origin});
expect(mailsSent.length).to.equal(2);
});
it('should throw 304 if key already exists', function *() {
yield publicKey.put({ publicKeyArmored, primaryEmail, origin });
yield publicKey.put({publicKeyArmored, primaryEmail, origin});
yield publicKey.verify(mailsSent[0].params);
try {
yield publicKey.put({ publicKeyArmored, primaryEmail, origin });
yield publicKey.put({publicKeyArmored, primaryEmail, origin});
expect(false).to.be.true;
} catch(e) {
} catch (e) {
expect(e.status).to.equal(304);
}
});
@@ -94,10 +100,10 @@ describe('Public Key Integration Tests', function() {
describe('verify', () => {
it('should update the document', function *() {
yield publicKey.put({ publicKeyArmored, primaryEmail, origin });
let emailParams = mailsSent[0].params;
yield publicKey.put({publicKeyArmored, primaryEmail, origin});
const emailParams = mailsSent[0].params;
yield publicKey.verify(emailParams);
let gotten = yield mongo.get({ keyId:emailParams.keyId }, DB_TYPE);
const 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;
@@ -105,15 +111,15 @@ describe('Public Key Integration Tests', function() {
});
it('should not find the document', function *() {
yield publicKey.put({ publicKeyArmored, primaryEmail, origin });
let emailParams = mailsSent[0].params;
yield publicKey.put({publicKeyArmored, primaryEmail, origin});
const emailParams = mailsSent[0].params;
try {
yield publicKey.verify({ keyId:emailParams.keyId, nonce:'fake_nonce' });
yield publicKey.verify({keyId: emailParams.keyId, nonce: 'fake_nonce'});
expect(true).to.be.false;
} catch(e) {
} catch (e) {
expect(e.status).to.equal(404);
}
let gotten = yield mongo.get({ keyId:emailParams.keyId }, DB_TYPE);
const 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;
@@ -121,32 +127,32 @@ describe('Public Key Integration Tests', function() {
});
it('should not verify a second key for already verified user id of another key', function *() {
yield publicKey.put({ publicKeyArmored, primaryEmail:primaryEmail2, origin });
yield publicKey.put({publicKeyArmored, primaryEmail: primaryEmail2, origin});
expect(mailsSent.length).to.equal(1);
yield publicKey.put({ publicKeyArmored:publicKeyArmored2, primaryEmail:primaryEmail2, origin });
yield publicKey.put({publicKeyArmored: publicKeyArmored2, primaryEmail: primaryEmail2, origin});
expect(mailsSent.length).to.equal(2);
yield publicKey.verify(mailsSent[1].params);
try {
yield publicKey.verify(mailsSent[0].params);
expect(true).to.be.false;
} catch(e) {
} catch (e) {
expect(e.status).to.equal(304);
}
let gotten = yield mongo.get({ keyId:mailsSent[0].params.keyId }, DB_TYPE);
const gotten = yield mongo.get({keyId: mailsSent[0].params.keyId}, DB_TYPE);
expect(gotten.userIds[1].email).to.equal(primaryEmail2);
expect(gotten.userIds[1].verified).to.be.false;
expect(gotten.userIds[1].nonce).to.equal(mailsSent[0].params.nonce);
});
it('should be able to verify multiple user ids', function *() {
yield publicKey.put({ publicKeyArmored, origin });
yield publicKey.put({publicKeyArmored, origin});
expect(mailsSent.length).to.equal(4);
yield publicKey.verify(mailsSent[0].params);
yield publicKey.verify(mailsSent[1].params);
yield publicKey.verify(mailsSent[2].params);
yield publicKey.verify(mailsSent[3].params);
let gotten = yield mongo.get({ keyId:mailsSent[0].params.keyId }, DB_TYPE);
const gotten = yield mongo.get({keyId: mailsSent[0].params.keyId}, DB_TYPE);
expect(gotten.userIds[0].verified).to.be.true;
expect(gotten.userIds[1].verified).to.be.true;
expect(gotten.userIds[2].verified).to.be.true;
@@ -160,37 +166,37 @@ describe('Public Key Integration Tests', function() {
describe('should find a verified key', () => {
beforeEach(function *() {
key = pgp.parseKey(publicKeyArmored);
yield publicKey.put({ publicKeyArmored, primaryEmail, origin });
yield publicKey.put({publicKeyArmored, primaryEmail, origin});
yield publicKey.verify(mailsSent[0].params);
});
it('by fingerprint', function *() {
let verified = yield publicKey.getVerified({ fingerprint:key.fingerprint });
const verified = yield publicKey.getVerified({fingerprint: key.fingerprint});
expect(verified).to.exist;
});
it('by all userIds', function *() {
let verified = yield publicKey.getVerified({ userIds:key.userIds });
const 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]] });
const 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]] });
const 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 });
const verified = yield publicKey.getVerified({keyId: key.keyId});
expect(verified).to.exist;
});
it('by all params', function *() {
let verified = yield publicKey.getVerified(key);
const verified = yield publicKey.getVerified(key);
expect(verified).to.exist;
});
});
@@ -203,22 +209,22 @@ describe('Public Key Integration Tests', function() {
});
it('by fingerprint', function *() {
let verified = yield publicKey.getVerified({ fingerprint:key.fingerprint });
const verified = yield publicKey.getVerified({fingerprint: key.fingerprint});
expect(verified).to.not.exist;
});
it('by userIds', function *() {
let verified = yield publicKey.getVerified({ userIds:key.userIds });
const verified = yield publicKey.getVerified({userIds: key.userIds});
expect(verified).to.not.exist;
});
it('by keyId', function *() {
let verified = yield publicKey.getVerified({ keyId:key.keyId });
const verified = yield publicKey.getVerified({keyId: key.keyId});
expect(verified).to.not.exist;
});
it('by all params', function *() {
let verified = yield publicKey.getVerified(key);
const verified = yield publicKey.getVerified(key);
expect(verified).to.not.exist;
});
});
@@ -228,53 +234,53 @@ describe('Public Key Integration Tests', function() {
let emailParams;
beforeEach(function *() {
yield publicKey.put({ publicKeyArmored, primaryEmail, origin });
yield publicKey.put({publicKeyArmored, primaryEmail, origin});
emailParams = mailsSent[0].params;
});
it('should return verified key by key id', function *() {
yield publicKey.verify(emailParams);
let key = yield publicKey.get({ keyId:emailParams.keyId });
const 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() });
const 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 });
const fingerprint = pgp.parseKey(publicKeyArmored).fingerprint;
const 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 });
const fingerprint = pgp.parseKey(publicKeyArmored).fingerprint.toUpperCase();
const key = yield publicKey.get({fingerprint});
expect(key.publicKeyArmored).to.exist;
});
it('should return verified key by email address', function *() {
yield publicKey.verify(emailParams);
let key = yield publicKey.get({ email:primaryEmail });
const key = yield publicKey.get({email: primaryEmail});
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() });
const 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) {
} catch (e) {
expect(e.status).to.equal(404);
}
});
@@ -284,32 +290,32 @@ describe('Public Key Integration Tests', function() {
let keyId;
beforeEach(function *() {
yield publicKey.put({ publicKeyArmored, primaryEmail, origin });
yield publicKey.put({publicKeyArmored, primaryEmail, origin});
keyId = mailsSent[0].params.keyId;
});
it('should work for verified key', function *() {
yield publicKey.verify(mailsSent[0].params);
yield publicKey.requestRemove({ keyId, origin });
yield publicKey.requestRemove({keyId, origin});
expect(mailsSent.length).to.equal(5);
});
it('should work for unverified key', function *() {
yield publicKey.requestRemove({ keyId, origin });
yield publicKey.requestRemove({keyId, origin});
expect(mailsSent.length).to.equal(5);
});
it('should work by email address', function *() {
yield publicKey.requestRemove({ email:primaryEmail, origin });
yield publicKey.requestRemove({email: primaryEmail, origin});
expect(mailsSent.length).to.equal(2);
});
it('should throw 404 for no key', function *() {
yield mongo.remove({ keyId }, DB_TYPE);
yield mongo.remove({keyId}, DB_TYPE);
try {
yield publicKey.requestRemove({ keyId, origin });
yield publicKey.requestRemove({keyId, origin});
expect(false).to.be.true;
} catch(e) {
} catch (e) {
expect(e.status).to.equal(404);
}
});
@@ -319,26 +325,25 @@ describe('Public Key Integration Tests', function() {
let keyId;
beforeEach(function *() {
yield publicKey.put({ publicKeyArmored, primaryEmail, origin });
yield publicKey.put({publicKeyArmored, primaryEmail, origin});
keyId = mailsSent[0].params.keyId;
yield publicKey.requestRemove({ keyId, origin });
yield publicKey.requestRemove({keyId, origin});
});
it('should remove key', function *() {
yield publicKey.verifyRemove(mailsSent[1].params);
let key = yield mongo.get({ keyId }, DB_TYPE);
const key = yield mongo.get({keyId}, DB_TYPE);
expect(key).to.not.exist;
});
it('should throw 404 for no key', function *() {
yield mongo.remove({ keyId }, DB_TYPE);
yield mongo.remove({keyId}, DB_TYPE);
try {
yield publicKey.verifyRemove(mailsSent[1].params);
expect(false).to.be.true;
} catch(e) {
} catch (e) {
expect(e.status).to.equal(404);
}
});
});
});
});

View File

@@ -5,28 +5,29 @@ const Email = require('../../src/email/email');
const nodemailer = require('nodemailer');
describe('Email Unit Tests', () => {
let email, sendFnStub;
let email;
let sendFnStub;
let template = {
const template = {
subject: 'foo',
text: 'bar',
html: '<strong>bar</strong>'
};
let sender = {
const sender = {
name: 'Foo Bar',
email: 'foo@bar.com'
};
let userId1 = {
const userId1 = {
name: 'name1',
email: 'email1',
nonce: 'qwertzuioasdfghjkqwertzuio'
};
let keyId = '0123456789ABCDF0';
let origin = {
const keyId = '0123456789ABCDF0';
const origin = {
protocol: 'http',
host: 'localhost:8888'
};
let mailOptions = {
const mailOptions = {
from: sender,
to: sender,
subject: 'Hello ✔', // Subject line
@@ -37,7 +38,7 @@ describe('Email Unit Tests', () => {
beforeEach(() => {
sendFnStub = sinon.stub();
sinon.stub(nodemailer, 'createTransport').returns({
templateSender: () => { return sendFnStub; }
templateSender: () => sendFnStub
});
sinon.stub(log, 'warn');
@@ -46,8 +47,8 @@ describe('Email Unit Tests', () => {
email = new Email(nodemailer);
email.init({
host: 'host',
auth: { user:'user', pass:'pass' },
sender: sender
auth: {user: 'user', pass: 'pass'},
sender
});
expect(email._sender).to.equal(sender);
});
@@ -60,7 +61,7 @@ describe('Email Unit Tests', () => {
describe("send", () => {
beforeEach(() => {
sinon.stub(email, '_sendHelper').returns(Promise.resolve({ response:'250' }));
sinon.stub(email, '_sendHelper').returns(Promise.resolve({response: '250'}));
});
afterEach(() => {
@@ -68,7 +69,7 @@ describe('Email Unit Tests', () => {
});
it('should work', function *() {
let info = yield email.send({ template, userId:userId1, keyId, origin});
const info = yield email.send({template, userId: userId1, keyId, origin});
expect(info.response).to.match(/^250/);
});
@@ -76,17 +77,17 @@ describe('Email Unit Tests', () => {
describe("_sendHelper", () => {
it('should work', function *() {
sendFnStub.returns(Promise.resolve({ response:'250' }));
sendFnStub.returns(Promise.resolve({response: '250'}));
let info = yield email._sendHelper(mailOptions);
const info = yield email._sendHelper(mailOptions);
expect(info.response).to.match(/^250/);
});
it('should log warning for reponse error', function *() {
sendFnStub.returns(Promise.resolve({ response:'554' }));
sendFnStub.returns(Promise.resolve({response: '554'}));
let info = yield email._sendHelper(mailOptions);
const info = yield email._sendHelper(mailOptions);
expect(info.response).to.match(/^554/);
expect(log.warn.calledOnce).to.be.true;
@@ -97,12 +98,11 @@ describe('Email Unit Tests', () => {
try {
yield email._sendHelper(mailOptions);
} catch(e) {
} catch (e) {
expect(log.error.calledOnce).to.be.true;
expect(e.status).to.equal(500);
expect(e.message).to.match(/failed/);
}
});
});
});
});

View File

@@ -6,18 +6,21 @@ const openpgp = require('openpgp');
const PGP = require('../../src/service/pgp');
describe('PGP Unit Tests', () => {
let pgp, key1Armored, key2Armored, key3Armored;
let pgp;
let key1Armored;
let key2Armored;
let key3Armored;
beforeEach(() => {
key1Armored = fs.readFileSync(__dirname + '/../key1.asc', 'utf8');
key2Armored = fs.readFileSync(__dirname + '/../key2.asc', 'utf8');
key3Armored = fs.readFileSync(__dirname + '/../key3.asc', 'utf8');
key1Armored = fs.readFileSync(`${__dirname}/../key1.asc`, 'utf8');
key2Armored = fs.readFileSync(`${__dirname}/../key2.asc`, 'utf8');
key3Armored = fs.readFileSync(`${__dirname}/../key3.asc`, 'utf8');
pgp = new PGP();
});
describe('parseKey', () => {
it('should should throw error on key parsing', () => {
let readStub = sinon.stub(openpgp.key, 'readArmored').returns({err:[new Error()]});
const readStub = sinon.stub(openpgp.key, 'readArmored').returns({err: [new Error()]});
sinon.stub(log, 'error');
expect(pgp.parseKey.bind(pgp, key3Armored)).to.throw(/Failed to parse/);
expect(log.error.calledOnce).to.be.true;
@@ -26,16 +29,16 @@ describe('PGP Unit Tests', () => {
});
it('should should throw error when more than one key', () => {
let readStub = sinon.stub(openpgp.key, 'readArmored').returns({keys:[{},{}]});
const readStub = sinon.stub(openpgp.key, 'readArmored').returns({keys: [{}, {}]});
expect(pgp.parseKey.bind(pgp, key3Armored)).to.throw(/only one key/);
readStub.restore();
});
it('should should throw error when more than one key', () => {
let readStub = sinon.stub(openpgp.key, 'readArmored').returns({
const readStub = sinon.stub(openpgp.key, 'readArmored').returns({
keys: [{
primaryKey: {},
verifyPrimaryKey: function() { return false; }
verifyPrimaryKey() { return false; }
}]
});
expect(pgp.parseKey.bind(pgp, key3Armored)).to.throw(/primary key verification/);
@@ -43,17 +46,17 @@ describe('PGP Unit Tests', () => {
});
it('should only accept 16 char key id', () => {
let readStub = sinon.stub(openpgp.key, 'readArmored').returns({
const readStub = sinon.stub(openpgp.key, 'readArmored').returns({
keys: [{
primaryKey: {
fingerprint: '4277257930867231ce393fb8dbc0b3d92b1b86e9',
getKeyId: function() {
getKeyId() {
return {
toHex:function() { return 'asdf'; }
toHex() { return 'asdf'; }
};
}
},
verifyPrimaryKey: function() { return openpgp.enums.keyStatus.valid; }
verifyPrimaryKey() { return openpgp.enums.keyStatus.valid; }
}]
});
expect(pgp.parseKey.bind(pgp, key3Armored)).to.throw(/only v4 keys/);
@@ -61,17 +64,17 @@ describe('PGP Unit Tests', () => {
});
it('should only accept version 4 fingerprint', () => {
let readStub = sinon.stub(openpgp.key, 'readArmored').returns({
const readStub = sinon.stub(openpgp.key, 'readArmored').returns({
keys: [{
primaryKey: {
fingerprint: '4277257930867231ce393fb8dbc0b3d92b1b86e',
getKeyId: function() {
getKeyId() {
return {
toHex:function() { return 'dbc0b3d92b1b86e9'; }
toHex() { return 'dbc0b3d92b1b86e9'; }
};
}
},
verifyPrimaryKey: function() { return openpgp.enums.keyStatus.valid; }
verifyPrimaryKey() { return openpgp.enums.keyStatus.valid; }
}]
});
expect(pgp.parseKey.bind(pgp, key3Armored)).to.throw(/only v4 keys/);
@@ -84,7 +87,7 @@ describe('PGP Unit Tests', () => {
});
it('should be able to parse RSA key', () => {
let params = pgp.parseKey(key1Armored);
const params = pgp.parseKey(key1Armored);
expect(params.keyId).to.equal('dbc0b3d92b1b86e9');
expect(params.fingerprint).to.equal('4277257930867231ce393fb8dbc0b3d92b1b86e9');
expect(params.userIds[0].name).to.equal('safewithme testuser');
@@ -96,7 +99,7 @@ describe('PGP Unit Tests', () => {
});
it('should be able to parse RSA/ECC key', () => {
let params = pgp.parseKey(key2Armored);
const params = pgp.parseKey(key2Armored);
expect(params.keyId).to.equal('b8e4105cc9dedc77');
expect(params.fingerprint).to.equal('e3317db04d3958fd5f662c37b8e4105cc9dedc77');
expect(params.userIds.length).to.equal(1);
@@ -107,7 +110,7 @@ describe('PGP Unit Tests', () => {
});
it('should be able to parse komplex key', () => {
let params = pgp.parseKey(key3Armored);
const params = pgp.parseKey(key3Armored);
expect(params.keyId).to.equal('4001a127a90de8e1');
expect(params.fingerprint).to.equal('04062c70b446e33016e219a74001a127a90de8e1');
expect(params.userIds.length).to.equal(4);
@@ -120,12 +123,12 @@ describe('PGP Unit Tests', () => {
describe('trimKey', () => {
it('should be the same as key1', () => {
let trimmed = pgp.trimKey(key1Armored);
const trimmed = pgp.trimKey(key1Armored);
expect(trimmed).to.equal(key1Armored);
});
it('should not be the same as key2', () => {
let trimmed = pgp.trimKey(key2Armored);
const trimmed = pgp.trimKey(key2Armored);
expect(trimmed).to.not.equal(key2Armored);
});
});
@@ -135,22 +138,22 @@ describe('PGP Unit Tests', () => {
const KEY_END = '-----END PGP PUBLIC KEY BLOCK-----';
it('should return true for valid key block', () => {
let input = KEY_BEGIN + KEY_END;
const input = KEY_BEGIN + KEY_END;
expect(pgp.validateKeyBlock(input)).to.be.true;
});
it('should return false for invalid key block', () => {
let input = KEY_END + KEY_BEGIN;
const input = KEY_END + KEY_BEGIN;
expect(pgp.validateKeyBlock(input)).to.be.false;
});
it('should return false for invalid key block', () => {
let input = KEY_END;
const input = KEY_END;
expect(pgp.validateKeyBlock(input)).to.be.false;
});
it('should return false for invalid key block', () => {
let input = KEY_BEGIN;
const input = KEY_BEGIN;
expect(pgp.validateKeyBlock(input)).to.be.false;
});
});
@@ -163,7 +166,7 @@ describe('PGP Unit Tests', () => {
});
it('should parse a valid user id', () => {
let parsed = pgp.parseUserIds(key.users, key.primaryKey);
const parsed = pgp.parseUserIds(key.users, key.primaryKey);
expect(parsed[0].name).to.equal('safewithme testuser');
expect(parsed[0].email).to.equal('safewithme.testuser@gmail.com');
});
@@ -174,17 +177,16 @@ describe('PGP Unit Tests', () => {
it('should return no user id for an invalid signature', () => {
key.users[0].userId.userid = 'fake@example.com';
let parsed = pgp.parseUserIds(key.users, key.primaryKey);
const parsed = pgp.parseUserIds(key.users, key.primaryKey);
expect(parsed.length).to.equal(0);
});
it('should throw for a invalid email address', () => {
let verifyStub = sinon.stub(key.users[0], 'isValidSelfCertificate').returns(true);
const verifyStub = sinon.stub(key.users[0], 'isValidSelfCertificate').returns(true);
key.users[0].userId.userid = 'safewithme testuser <safewithme.testusergmail.com>';
let parsed = pgp.parseUserIds(key.users, key.primaryKey);
const parsed = pgp.parseUserIds(key.users, key.primaryKey);
expect(parsed.length).to.equal(0);
verifyStub.restore();
});
});
});
});

View File

@@ -116,7 +116,7 @@ describe('Util Unit Tests', () => {
try {
util.throw(500, 'boom');
expect(true).to.be.false;
} catch(e) {
} catch (e) {
expect(e.message).to.equal('boom');
expect(e.status).to.equal(500);
expect(e.expose).to.be.true;
@@ -136,20 +136,19 @@ describe('Util Unit Tests', () => {
describe('origin', () => {
it('should work', () => {
expect(util.origin({ secure:true, host:'h', protocol:'p' })).to.exist;
expect(util.origin({secure: true, host: 'h', protocol: 'p'})).to.exist;
});
});
describe('url', () => {
it('should work with resource', () => {
let url = util.url({ host:'localhost', protocol:'http'}, '/foo');
const url = util.url({host: 'localhost', protocol: 'http'}, '/foo');
expect(url).to.equal('http://localhost/foo');
});
it('should work without resource', () => {
let url = util.url({ host:'localhost', protocol:'http'});
const url = util.url({host: 'localhost', protocol: 'http'});
expect(url).to.equal('http://localhost');
});
});
});
});