Unified mongo and email style

This commit is contained in:
Tankred Hase 2016-06-08 14:01:30 +02:00
parent 56162a01d9
commit b5fb5331fc
7 changed files with 17 additions and 32 deletions

View File

@ -25,21 +25,15 @@ const MongoClient = require('mongodb').MongoClient;
class Mongo { class Mongo {
/** /**
* Create an instance of the MongoDB client. * Initializes the database client by connecting to the MongoDB.
* @param {String} uri The mongodb uri * @param {String} uri The mongodb uri
* @param {String} user The databse user * @param {String} user The databse user
* @param {String} pass The database user's password * @param {String} pass The database user's password
*/
constructor(options) {
this._uri = 'mongodb://' + options.user + ':' + options.pass + '@' + options.uri;
}
/**
* Initializes the database client by connecting to the MongoDB.
* @yield {undefined} * @yield {undefined}
*/ */
*connect() { *init(options) {
this._db = yield MongoClient.connect(this._uri); let uri = 'mongodb://' + options.user + ':' + options.pass + '@' + options.uri;
this._db = yield MongoClient.connect(uri);
} }
/** /**

View File

@ -19,21 +19,14 @@
const log = require('npmlog'); const log = require('npmlog');
const util = require('../service/util'); const util = require('../service/util');
const nodemailer = require('nodemailer');
const openpgpEncrypt = require('nodemailer-openpgp').openpgpEncrypt;
/** /**
* A simple wrapper around Nodemailer to send verification emails * A simple wrapper around Nodemailer to send verification emails
*/ */
class Email { class Email {
/**
* Create an instance of the email object.
* @param {Object} mailer An instance of nodemailer
*/
constructor(mailer, openpgpEncrypt) {
this._mailer = mailer;
this._openpgpEncrypt = openpgpEncrypt;
}
/** /**
* Create an instance of the reusable nodemailer SMTP transport. * Create an instance of the reusable nodemailer SMTP transport.
* @param {string} host SMTP server's hostname: 'smtp.gmail.com' * @param {string} host SMTP server's hostname: 'smtp.gmail.com'
@ -45,7 +38,7 @@ class Email {
* @param {boolean} pgp (optional) if outgoing emails are encrypted to the user's public key. * @param {boolean} pgp (optional) if outgoing emails are encrypted to the user's public key.
*/ */
init(options) { init(options) {
this._transport = this._mailer.createTransport({ this._transport = nodemailer.createTransport({
host: options.host, host: options.host,
port: options.port || 465, port: options.port || 465,
auth: options.auth, auth: options.auth,
@ -53,7 +46,7 @@ class Email {
requireTLS: (options.starttls !== undefined) ? util.isTrue(options.starttls) : true, requireTLS: (options.starttls !== undefined) ? util.isTrue(options.starttls) : true,
}); });
if (util.isTrue(options.pgp)) { if (util.isTrue(options.pgp)) {
this._transport.use('stream', this._openpgpEncrypt()); this._transport.use('stream', openpgpEncrypt());
} }
this._sender = options.sender; this._sender = options.sender;
} }

View File

@ -23,8 +23,8 @@ describe('Koa App (HTTP Server) Integration Tests', function() {
before(function *() { before(function *() {
publicKeyArmored = fs.readFileSync(__dirname + '/../key1.asc', 'utf8'); publicKeyArmored = fs.readFileSync(__dirname + '/../key1.asc', 'utf8');
mongo = new Mongo(config.mongo); mongo = new Mongo();
yield mongo.connect(); yield mongo.init(config.mongo);
sendEmailStub = sinon.stub().returns(Promise.resolve({ response:'250' })); sendEmailStub = sinon.stub().returns(Promise.resolve({ response:'250' }));
sendEmailStub.withArgs(sinon.match(recipient => { sendEmailStub.withArgs(sinon.match(recipient => {

View File

@ -5,8 +5,6 @@ require('co-mocha')(require('mocha')); // monkey patch mocha for generators
const expect = require('chai').expect; const expect = require('chai').expect;
const config = require('config'); const config = require('config');
const Email = require('../../src/email/email'); const Email = require('../../src/email/email');
const nodemailer = require('nodemailer');
const openpgpEncrypt = require('nodemailer-openpgp').openpgpEncrypt;
const tpl = require('../../src/email/templates.json'); const tpl = require('../../src/email/templates.json');
describe('Email Integration Tests', function() { describe('Email Integration Tests', function() {
@ -22,7 +20,7 @@ describe('Email Integration Tests', function() {
protocol: 'http', protocol: 'http',
host: 'localhost:' + config.server.port host: 'localhost:' + config.server.port
}; };
email = new Email(nodemailer, openpgpEncrypt); email = new Email();
email.init(config.email); email.init(config.email);
}); });

View File

@ -13,8 +13,8 @@ describe('Mongo Integration Tests', function() {
let mongo; let mongo;
before(function *() { before(function *() {
mongo = new Mongo(config.mongo); mongo = new Mongo();
yield mongo.connect(); yield mongo.init(config.mongo);
}); });
beforeEach(function *() { beforeEach(function *() {

View File

@ -25,8 +25,8 @@ describe('Public Key Integration Tests', function() {
before(function *() { before(function *() {
publicKeyArmored = require('fs').readFileSync(__dirname + '/../key1.asc', 'utf8'); publicKeyArmored = require('fs').readFileSync(__dirname + '/../key1.asc', 'utf8');
mongo = new Mongo(config.mongo); mongo = new Mongo();
yield mongo.connect(); yield mongo.init(config.mongo);
}); });
beforeEach(function *() { beforeEach(function *() {

View File

@ -15,8 +15,8 @@ describe('User ID Integration Tests', function() {
let mongo, userId, uid1, uid2; let mongo, userId, uid1, uid2;
before(function *() { before(function *() {
mongo = new Mongo(config.mongo); mongo = new Mongo();
yield mongo.connect(); yield mongo.init(config.mongo);
userId = new UserId(mongo); userId = new UserId(mongo);
}); });