increased test coverage for smtp appender
This commit is contained in:
parent
41ddf5eea7
commit
11fe5bde5f
@ -22,32 +22,31 @@ function smtpAppender(config, layout) {
|
||||
var sendTimer;
|
||||
|
||||
function sendBuffer() {
|
||||
if (logEventBuffer.length === 0) {
|
||||
return;
|
||||
}
|
||||
if (logEventBuffer.length > 0) {
|
||||
|
||||
var transport = mailer.createTransport(config.transport, config[config.transport]);
|
||||
var firstEvent = logEventBuffer[0];
|
||||
var body = "";
|
||||
while (logEventBuffer.length > 0) {
|
||||
body += layout(logEventBuffer.shift()) + "\n";
|
||||
}
|
||||
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() }
|
||||
};
|
||||
|
||||
var msg = {
|
||||
to: config.recipients,
|
||||
subject: config.subject || subjectLayout(firstEvent),
|
||||
text: body,
|
||||
headers: {"Hostname": os.hostname()}
|
||||
};
|
||||
if (config.sender) {
|
||||
msg.from = config.sender;
|
||||
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() {
|
||||
|
@ -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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user