From 11fe5bde5f0dfc0c009c11ece0f21ecbd870553c Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Wed, 5 Jun 2013 18:30:11 +1000 Subject: [PATCH] increased test coverage for smtp appender --- lib/appenders/smtp.js | 47 +++++++++++++-------------- test/smtpAppender-test.js | 68 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 89 insertions(+), 26 deletions(-) diff --git a/lib/appenders/smtp.js b/lib/appenders/smtp.js index 5cfd713..2023604 100644 --- a/lib/appenders/smtp.js +++ b/lib/appenders/smtp.js @@ -22,32 +22,31 @@ function smtpAppender(config, layout) { var sendTimer; function sendBuffer() { - if (logEventBuffer.length === 0) { - return; - } - - var transport = mailer.createTransport(config.transport, config[config.transport]); - var firstEvent = logEventBuffer[0]; - var body = ""; - while (logEventBuffer.length > 0) { - body += layout(logEventBuffer.shift()) + "\n"; - } + if (logEventBuffer.length > 0) { - var msg = { - to: config.recipients, - subject: config.subject || subjectLayout(firstEvent), - text: body, - headers: {"Hostname": os.hostname()} - }; - if (config.sender) { - msg.from = config.sender; + var transport = mailer.createTransport(config.transport, config[config.transport]); + var firstEvent = logEventBuffer[0]; + var body = logEventBuffer.map(layout).join('\n'); + var msg = { + to: config.recipients, + subject: config.subject || subjectLayout(firstEvent), + text: body, + headers: { "Hostname": os.hostname() } + }; + + if (config.sender) { + msg.from = config.sender; + } + + transport.sendMail(msg, function(error, success) { + if (error) { + console.error("log4js.smtpAppender - Error happened", error); + } + transport.close(); + }); + + logEventBuffer = []; } - transport.sendMail(msg, function(error, success) { - if (error) { - console.error("log4js.smtpAppender - Error happened ", error); - } - transport.close(); - }); } function scheduleSend() { diff --git a/test/smtpAppender-test.js b/test/smtpAppender-test.js index 1fd5bb9..27cc179 100644 --- a/test/smtpAppender-test.js +++ b/test/smtpAppender-test.js @@ -21,10 +21,31 @@ function setupLogging(category, options) { } }; + var fakeLayouts = { + layout: function(type, config) { + this.type = type; + this.config = config; + return log4js.layouts.messagePassThroughLayout; + }, + basicLayout: log4js.layouts.basicLayout, + messagePassThroughLayout: log4js.layouts.messagePassThroughLayout + }; + + var fakeConsole = { + errors: [], + error: function(msg, value) { + this.errors.push({ msg: msg, value: value }); + } + }; + var smtpModule = sandbox.require('../lib/appenders/smtp', { requires: { - 'nodemailer': fakeMailer - } + 'nodemailer': fakeMailer, + '../layouts': fakeLayouts + }, + globals: { + console: fakeConsole + } }); log4js.addAppender(smtpModule.configure(options), category); @@ -32,6 +53,8 @@ function setupLogging(category, options) { return { logger: log4js.getLogger(category), mailer: fakeMailer, + layouts: fakeLayouts, + console: fakeConsole, results: msgs }; } @@ -93,6 +116,19 @@ vows.describe('log4js smtpAppender').addBatch({ checkMessages(result, 'sender@domain.com', 'This is subject'); } }, + 'config with layout': { + topic: function() { + var setup = setupLogging('config with layout', { + layout: { + type: "tester" + } + }); + return setup; + }, + 'should configure layout': function(result) { + assert.equal(result.layouts.type, 'tester'); + } + }, 'separate email for each event': { topic: function() { var self = this; @@ -164,6 +200,34 @@ vows.describe('log4js smtpAppender').addBatch({ assert.equal(result.results[1].subject, 'Log event #3'); assert.ok(new RegExp('.+Log event #3\n$').test(result.results[1].text)); } + }, + 'error when sending email': { + topic: function() { + var setup = setupLogging('error when sending email', { + recipients: 'recipient@domain.com', + sendInterval: 0, + transport: 'SMTP', + SMTP: { port: 25, auth: { user: 'user@domain.com' } } + }); + + setup.mailer.createTransport = function() { + return { + sendMail: function(msg, cb) { + cb({ message: "oh noes" }); + }, + close: function() { } + }; + }; + + setup.logger.info("This will break"); + return setup.console; + }, + 'should be logged to console': function(cons) { + assert.equal(cons.errors.length, 1); + assert.equal(cons.errors[0].msg, "log4js.smtpAppender - Error happened"); + assert.equal(cons.errors[0].value.message, 'oh noes'); + } } + }).export(module);