From b5fb5331fcc5f7c505f595e0173be3dc13bca3d1 Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Wed, 8 Jun 2016 14:01:30 +0200 Subject: [PATCH] Unified mongo and email style --- src/dao/mongo.js | 14 ++++---------- src/email/email.js | 15 ++++----------- test/integration/app-test.js | 4 ++-- test/integration/email-test.js | 4 +--- test/integration/mongo-test.js | 4 ++-- test/integration/public-key-test.js | 4 ++-- test/integration/user-id-test.js | 4 ++-- 7 files changed, 17 insertions(+), 32 deletions(-) diff --git a/src/dao/mongo.js b/src/dao/mongo.js index 5ee6846..2416d16 100644 --- a/src/dao/mongo.js +++ b/src/dao/mongo.js @@ -25,21 +25,15 @@ const MongoClient = require('mongodb').MongoClient; 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} user The databse user * @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} */ - *connect() { - this._db = yield MongoClient.connect(this._uri); + *init(options) { + let uri = 'mongodb://' + options.user + ':' + options.pass + '@' + options.uri; + this._db = yield MongoClient.connect(uri); } /** diff --git a/src/email/email.js b/src/email/email.js index 2767da9..326494d 100644 --- a/src/email/email.js +++ b/src/email/email.js @@ -19,21 +19,14 @@ const log = require('npmlog'); const util = require('../service/util'); +const nodemailer = require('nodemailer'); +const openpgpEncrypt = require('nodemailer-openpgp').openpgpEncrypt; /** * A simple wrapper around Nodemailer to send verification emails */ 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. * @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. */ init(options) { - this._transport = this._mailer.createTransport({ + this._transport = nodemailer.createTransport({ host: options.host, port: options.port || 465, auth: options.auth, @@ -53,7 +46,7 @@ class Email { requireTLS: (options.starttls !== undefined) ? util.isTrue(options.starttls) : true, }); if (util.isTrue(options.pgp)) { - this._transport.use('stream', this._openpgpEncrypt()); + this._transport.use('stream', openpgpEncrypt()); } this._sender = options.sender; } diff --git a/test/integration/app-test.js b/test/integration/app-test.js index 177b209..b826572 100644 --- a/test/integration/app-test.js +++ b/test/integration/app-test.js @@ -23,8 +23,8 @@ describe('Koa App (HTTP Server) Integration Tests', function() { before(function *() { publicKeyArmored = fs.readFileSync(__dirname + '/../key1.asc', 'utf8'); - mongo = new Mongo(config.mongo); - yield mongo.connect(); + mongo = new Mongo(); + yield mongo.init(config.mongo); sendEmailStub = sinon.stub().returns(Promise.resolve({ response:'250' })); sendEmailStub.withArgs(sinon.match(recipient => { diff --git a/test/integration/email-test.js b/test/integration/email-test.js index 25757c0..45ab949 100644 --- a/test/integration/email-test.js +++ b/test/integration/email-test.js @@ -5,8 +5,6 @@ require('co-mocha')(require('mocha')); // monkey patch mocha for generators const expect = require('chai').expect; const config = require('config'); const Email = require('../../src/email/email'); -const nodemailer = require('nodemailer'); -const openpgpEncrypt = require('nodemailer-openpgp').openpgpEncrypt; const tpl = require('../../src/email/templates.json'); describe('Email Integration Tests', function() { @@ -22,7 +20,7 @@ describe('Email Integration Tests', function() { protocol: 'http', host: 'localhost:' + config.server.port }; - email = new Email(nodemailer, openpgpEncrypt); + email = new Email(); email.init(config.email); }); diff --git a/test/integration/mongo-test.js b/test/integration/mongo-test.js index 4ebafce..e7a7316 100644 --- a/test/integration/mongo-test.js +++ b/test/integration/mongo-test.js @@ -13,8 +13,8 @@ describe('Mongo Integration Tests', function() { let mongo; before(function *() { - mongo = new Mongo(config.mongo); - yield mongo.connect(); + mongo = new Mongo(); + yield mongo.init(config.mongo); }); beforeEach(function *() { diff --git a/test/integration/public-key-test.js b/test/integration/public-key-test.js index abc1978..d747d7b 100644 --- a/test/integration/public-key-test.js +++ b/test/integration/public-key-test.js @@ -25,8 +25,8 @@ describe('Public Key Integration Tests', function() { before(function *() { publicKeyArmored = require('fs').readFileSync(__dirname + '/../key1.asc', 'utf8'); - mongo = new Mongo(config.mongo); - yield mongo.connect(); + mongo = new Mongo(); + yield mongo.init(config.mongo); }); beforeEach(function *() { diff --git a/test/integration/user-id-test.js b/test/integration/user-id-test.js index aca6415..85796cb 100644 --- a/test/integration/user-id-test.js +++ b/test/integration/user-id-test.js @@ -15,8 +15,8 @@ describe('User ID Integration Tests', function() { let mongo, userId, uid1, uid2; before(function *() { - mongo = new Mongo(config.mongo); - yield mongo.connect(); + mongo = new Mongo(); + yield mongo.init(config.mongo); userId = new UserId(mongo); });