From 5d7cf079eaeca211ee3b150eebbcbb063f7ea45a Mon Sep 17 00:00:00 2001 From: vladikoff Date: Tue, 8 Nov 2016 14:13:18 -0500 Subject: [PATCH] fix(tests): increase testing coverage Fixes #230 --- test/local/db_tests.js | 59 ++++++++++++++++++++++++++++++++++ test/local/legacy_log_tests.js | 40 +++++++++++++++++++++++ test/local/mailer_tests.js | 51 ++++++++++++++++++++++++++++- 3 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 test/local/legacy_log_tests.js diff --git a/test/local/db_tests.js b/test/local/db_tests.js index c8681fe..4fa465f 100644 --- a/test/local/db_tests.js +++ b/test/local/db_tests.js @@ -69,6 +69,65 @@ test( } ) +test( + 'get account by uid', + function (t) { + t.plan(1) + + var accountData + var db + + return dbConn + .then(function (dbObj) { + db = dbObj + accountData = testHelpers.createTestAccount() + + return db.pool.put( + '/account/' + accountData.uid.toString('hex'), + unbuffer(accountData) + ) + }) + .then(function () { + return db.account(accountData.uid) + }) + .then(function (account) { + t.false(account.emailVerified, false) + t.end() + }, function (err) { + throw err + }) + } +) + +test( + 'throws db errors', + function (t) { + t.plan(2) + var db + + return dbConn + .then(function (dbObj) { + db = dbObj + return db.emailRecord('unknownEmail@restmail.net') + }) + .then(function () { + t.notOk() + }, function (err) { + t.ok(err) + return db.emailRecord('unknownEmail@restmail.net') + + }) + .then(function () { + t.notOk() + }, function (err) { + t.ok(err) + t.end() + }) + + + } +) + test( 'teardown', function (t) { diff --git a/test/local/legacy_log_tests.js b/test/local/legacy_log_tests.js new file mode 100644 index 0000000..829cadc --- /dev/null +++ b/test/local/legacy_log_tests.js @@ -0,0 +1,40 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +var sinon = require('sinon') +var tap = require('tap') +var test = tap.test +var legacyLog = require('../../legacy_log') + +var spyLog = { + critical: sinon.spy(), + debug: sinon.spy(), + error: sinon.spy(), + info: sinon.spy(), + warn: sinon.spy() +} + +test('legacy_log unit tests', function (t) { + var data = { + op: 'testOp', + err: 'Nooo!' + } + var log = legacyLog(spyLog) + log.trace(data) + t.equal(spyLog.debug.args[0][0], data.op) + t.equal(spyLog.debug.args[0][1], data) + log.error(data) + t.equal(spyLog.error.args[0][0], data.op) + t.equal(spyLog.error.args[0][1], data) + log.fatal(data) + t.equal(spyLog.critical.args[0][0], data.op) + t.equal(spyLog.critical.args[0][1], data) + log.warn(data) + t.equal(spyLog.warn.args[0][0], data.op) + t.equal(spyLog.warn.args[0][1], data) + log.info(data) + t.equal(spyLog.info.args[0][0], data.op) + t.equal(spyLog.info.args[0][1], data) + t.done() +}) diff --git a/test/local/mailer_tests.js b/test/local/mailer_tests.js index 52e8c29..766a97c 100644 --- a/test/local/mailer_tests.js +++ b/test/local/mailer_tests.js @@ -4,12 +4,14 @@ var extend = require('util')._extend +var sinon = require('sinon') var P = require('bluebird') var test = require('tap').test var nullLog = { trace: function () {}, - info: function () {} + info: function () {}, + error: function () {} } var config = require('../../config') @@ -525,5 +527,52 @@ P.all( } ) + test( + 'resolves sendMail status', + function (t) { + var mailer = new Mailer(translator, templates, config.get('mail')) + sinon.stub(mailer.mailer, 'sendMail', function (config, cb) { + cb(null, { resp: 'ok' }) + }) + + var message = { + email: 'test@restmail.net', + subject: 'subject', + template: 'suspiciousLocationEmail', + uid: 'foo' + } + + return mailer.send(message) + .then(function (status) { + t.equal(status.resp, 'ok') + t.done() + }) + } + ) + + test( + 'rejects sendMail status', + function (t) { + var mailer = new Mailer(translator, templates, config.get('mail')) + sinon.stub(mailer.mailer, 'sendMail', function (config, cb) { + cb(new Error('Fail')) + }) + + var message = { + email: 'test@restmail.net', + subject: 'subject', + template: 'suspiciousLocationEmail', + uid: 'foo' + } + + return mailer.send(message) + .then(t.notOk, function (err) { + t.equal(err.message, 'Fail') + t.done() + }) + } + ) + + } )