2013-05-26 14:41:31 +08:00
|
|
|
"use strict";
|
|
|
|
var vows = require('vows')
|
|
|
|
, assert = require('assert')
|
|
|
|
, log4js = require('../lib/log4js')
|
|
|
|
, sandbox = require('sandboxed-module')
|
|
|
|
;
|
2011-11-08 15:56:21 +08:00
|
|
|
|
|
|
|
function setupLogging(category, options) {
|
2013-05-26 14:41:31 +08:00
|
|
|
var msgs = [];
|
2014-08-15 18:06:35 +08:00
|
|
|
|
2013-05-26 14:41:31 +08:00
|
|
|
var fakeMailer = {
|
2012-06-28 00:00:32 +08:00
|
|
|
createTransport: function (name, options) {
|
|
|
|
return {
|
|
|
|
config: options,
|
|
|
|
sendMail: function (msg, callback) {
|
2013-05-26 14:41:31 +08:00
|
|
|
msgs.push(msg);
|
|
|
|
callback(null, true);
|
|
|
|
},
|
2013-05-25 11:08:43 +08:00
|
|
|
close: function() {}
|
2012-06-28 00:00:32 +08:00
|
|
|
};
|
|
|
|
}
|
2013-05-26 14:41:31 +08:00
|
|
|
};
|
2012-02-10 15:14:50 +08:00
|
|
|
|
2013-06-05 16:30:11 +08:00
|
|
|
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 });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-26 14:41:31 +08:00
|
|
|
var smtpModule = sandbox.require('../lib/appenders/smtp', {
|
2012-06-28 00:00:32 +08:00
|
|
|
requires: {
|
2013-06-05 16:30:11 +08:00
|
|
|
'nodemailer': fakeMailer,
|
|
|
|
'../layouts': fakeLayouts
|
|
|
|
},
|
|
|
|
globals: {
|
|
|
|
console: fakeConsole
|
|
|
|
}
|
2013-05-26 14:41:31 +08:00
|
|
|
});
|
2012-02-10 15:14:50 +08:00
|
|
|
|
2013-05-26 14:41:31 +08:00
|
|
|
log4js.addAppender(smtpModule.configure(options), category);
|
2014-08-15 18:06:35 +08:00
|
|
|
|
2013-05-26 14:41:31 +08:00
|
|
|
return {
|
2012-06-28 00:00:32 +08:00
|
|
|
logger: log4js.getLogger(category),
|
|
|
|
mailer: fakeMailer,
|
2013-06-05 16:30:11 +08:00
|
|
|
layouts: fakeLayouts,
|
|
|
|
console: fakeConsole,
|
2012-06-28 00:00:32 +08:00
|
|
|
results: msgs
|
2013-05-26 14:41:31 +08:00
|
|
|
};
|
2011-11-08 15:56:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function checkMessages (result, sender, subject) {
|
2013-05-26 14:41:31 +08:00
|
|
|
for (var i = 0; i < result.results.length; ++i) {
|
2012-06-28 00:00:32 +08:00
|
|
|
assert.equal(result.results[i].from, sender);
|
|
|
|
assert.equal(result.results[i].to, 'recipient@domain.com');
|
|
|
|
assert.equal(result.results[i].subject, subject ? subject : 'Log event #' + (i+1));
|
|
|
|
assert.ok(new RegExp('.+Log event #' + (i+1) + '\n$').test(result.results[i].text));
|
2013-05-26 14:41:31 +08:00
|
|
|
}
|
2011-11-08 15:56:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
log4js.clearAppenders();
|
|
|
|
vows.describe('log4js smtpAppender').addBatch({
|
2013-05-26 14:41:31 +08:00
|
|
|
'minimal config': {
|
2012-06-28 00:00:32 +08:00
|
|
|
topic: function() {
|
2013-05-26 14:41:31 +08:00
|
|
|
var setup = setupLogging('minimal config', {
|
|
|
|
recipients: 'recipient@domain.com',
|
|
|
|
SMTP: {
|
|
|
|
port: 25,
|
|
|
|
auth: {
|
|
|
|
user: 'user@domain.com'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
setup.logger.info('Log event #1');
|
|
|
|
return setup;
|
2012-06-28 00:00:32 +08:00
|
|
|
},
|
|
|
|
'there should be one message only': function (result) {
|
2013-05-26 14:41:31 +08:00
|
|
|
assert.equal(result.results.length, 1);
|
2012-06-28 00:00:32 +08:00
|
|
|
},
|
|
|
|
'message should contain proper data': function (result) {
|
2013-05-26 14:41:31 +08:00
|
|
|
checkMessages(result);
|
2011-11-08 15:56:21 +08:00
|
|
|
}
|
2013-05-26 14:41:31 +08:00
|
|
|
},
|
|
|
|
'fancy config': {
|
|
|
|
topic: function() {
|
|
|
|
var setup = setupLogging('fancy config', {
|
|
|
|
recipients: 'recipient@domain.com',
|
|
|
|
sender: 'sender@domain.com',
|
|
|
|
subject: 'This is subject',
|
|
|
|
SMTP: {
|
|
|
|
port: 25,
|
|
|
|
auth: {
|
|
|
|
user: 'user@domain.com'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
setup.logger.info('Log event #1');
|
|
|
|
return setup;
|
2012-02-10 15:14:50 +08:00
|
|
|
},
|
2013-05-26 14:41:31 +08:00
|
|
|
'there should be one message only': function (result) {
|
|
|
|
assert.equal(result.results.length, 1);
|
2012-02-10 15:14:50 +08:00
|
|
|
},
|
2013-05-26 14:41:31 +08:00
|
|
|
'message should contain proper data': function (result) {
|
|
|
|
checkMessages(result, 'sender@domain.com', 'This is subject');
|
|
|
|
}
|
|
|
|
},
|
2013-06-05 16:30:11 +08:00
|
|
|
'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');
|
|
|
|
}
|
|
|
|
},
|
2013-05-26 14:41:31 +08:00
|
|
|
'separate email for each event': {
|
|
|
|
topic: function() {
|
|
|
|
var self = this;
|
|
|
|
var setup = setupLogging('separate email for each event', {
|
|
|
|
recipients: 'recipient@domain.com',
|
|
|
|
SMTP: {
|
|
|
|
port: 25,
|
|
|
|
auth: {
|
|
|
|
user: 'user@domain.com'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
setTimeout(function () {
|
|
|
|
setup.logger.info('Log event #1');
|
|
|
|
}, 0);
|
|
|
|
setTimeout(function () {
|
|
|
|
setup.logger.info('Log event #2');
|
|
|
|
}, 500);
|
|
|
|
setTimeout(function () {
|
|
|
|
setup.logger.info('Log event #3');
|
2014-08-15 18:06:35 +08:00
|
|
|
}, 1100);
|
2013-05-26 14:41:31 +08:00
|
|
|
setTimeout(function () {
|
|
|
|
self.callback(null, setup);
|
2014-08-15 18:06:35 +08:00
|
|
|
}, 3000);
|
2012-02-10 15:14:50 +08:00
|
|
|
},
|
2013-05-26 14:41:31 +08:00
|
|
|
'there should be three messages': function (result) {
|
|
|
|
assert.equal(result.results.length, 3);
|
|
|
|
},
|
|
|
|
'messages should contain proper data': function (result) {
|
|
|
|
checkMessages(result);
|
2012-02-10 15:14:50 +08:00
|
|
|
}
|
2013-05-26 14:41:31 +08:00
|
|
|
},
|
|
|
|
'multiple events in one email': {
|
|
|
|
topic: function() {
|
|
|
|
var self = this;
|
|
|
|
var setup = setupLogging('multiple events in one email', {
|
|
|
|
recipients: 'recipient@domain.com',
|
|
|
|
sendInterval: 1,
|
|
|
|
SMTP: {
|
|
|
|
port: 25,
|
|
|
|
auth: {
|
|
|
|
user: 'user@domain.com'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
setTimeout(function () {
|
|
|
|
setup.logger.info('Log event #1');
|
|
|
|
}, 0);
|
|
|
|
setTimeout(function () {
|
|
|
|
setup.logger.info('Log event #2');
|
2014-08-15 18:06:35 +08:00
|
|
|
}, 100);
|
2013-05-26 14:41:31 +08:00
|
|
|
setTimeout(function () {
|
|
|
|
setup.logger.info('Log event #3');
|
2014-08-15 18:06:35 +08:00
|
|
|
}, 1500);
|
2013-05-26 14:41:31 +08:00
|
|
|
setTimeout(function () {
|
|
|
|
self.callback(null, setup);
|
2014-08-15 18:06:35 +08:00
|
|
|
}, 3000);
|
2013-05-26 14:41:31 +08:00
|
|
|
},
|
|
|
|
'there should be two messages': function (result) {
|
|
|
|
assert.equal(result.results.length, 2);
|
|
|
|
},
|
|
|
|
'messages should contain proper data': function (result) {
|
|
|
|
assert.equal(result.results[0].to, 'recipient@domain.com');
|
|
|
|
assert.equal(result.results[0].subject, 'Log event #1');
|
|
|
|
assert.equal(result.results[0].text.match(new RegExp('.+Log event #[1-2]$', 'gm')).length, 2);
|
|
|
|
assert.equal(result.results[1].to, 'recipient@domain.com');
|
|
|
|
assert.equal(result.results[1].subject, 'Log event #3');
|
|
|
|
assert.ok(new RegExp('.+Log event #3\n$').test(result.results[1].text));
|
|
|
|
}
|
2013-06-05 16:30:11 +08:00
|
|
|
},
|
|
|
|
'error when sending email': {
|
|
|
|
topic: function() {
|
|
|
|
var setup = setupLogging('error when sending email', {
|
|
|
|
recipients: 'recipient@domain.com',
|
|
|
|
sendInterval: 0,
|
|
|
|
SMTP: { port: 25, auth: { user: 'user@domain.com' } }
|
|
|
|
});
|
|
|
|
|
|
|
|
setup.mailer.createTransport = function() {
|
|
|
|
return {
|
|
|
|
sendMail: function(msg, cb) {
|
|
|
|
cb({ message: "oh noes" });
|
|
|
|
},
|
|
|
|
close: function() { }
|
|
|
|
};
|
|
|
|
};
|
2014-08-15 18:06:35 +08:00
|
|
|
|
2013-06-05 16:30:11 +08:00
|
|
|
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');
|
|
|
|
}
|
2013-05-26 14:41:31 +08:00
|
|
|
}
|
2011-11-08 15:56:21 +08:00
|
|
|
}).export(module);
|