mirror of
https://github.com/log4js-node/log4js-node.git
synced 2025-12-08 19:26:01 +00:00
fix(test): moved smtpAppender test to tap
This commit is contained in:
parent
d685b18a97
commit
5975159470
287
test/tap/smtpAppender-test.js
Normal file
287
test/tap/smtpAppender-test.js
Normal file
@ -0,0 +1,287 @@
|
||||
'use strict';
|
||||
|
||||
const test = require('tap').test;
|
||||
const log4js = require('../../lib/log4js');
|
||||
const sandbox = require('sandboxed-module');
|
||||
|
||||
function setupLogging(category, options) {
|
||||
const msgs = [];
|
||||
|
||||
const fakeMailer = {
|
||||
createTransport: function (name, opts) {
|
||||
return {
|
||||
config: opts,
|
||||
sendMail: function (msg, callback) {
|
||||
msgs.push(msg);
|
||||
callback(null, true);
|
||||
},
|
||||
close: function () {
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
const fakeLayouts = {
|
||||
layout: function (type, config) {
|
||||
this.type = type;
|
||||
this.config = config;
|
||||
return log4js.layouts.messagePassThroughLayout;
|
||||
},
|
||||
basicLayout: log4js.layouts.basicLayout,
|
||||
messagePassThroughLayout: log4js.layouts.messagePassThroughLayout
|
||||
};
|
||||
|
||||
const fakeConsole = {
|
||||
errors: [],
|
||||
error: function (msg, value) {
|
||||
this.errors.push({ msg: msg, value: value });
|
||||
}
|
||||
};
|
||||
|
||||
const fakeTransportPlugin = function () {
|
||||
};
|
||||
|
||||
const smtpModule = sandbox.require('../../lib/appenders/smtp', {
|
||||
singleOnly: true,
|
||||
requires: {
|
||||
nodemailer: fakeMailer,
|
||||
'nodemailer-sendmail-transport': fakeTransportPlugin,
|
||||
'nodemailer-smtp-transport': fakeTransportPlugin,
|
||||
'../layouts': fakeLayouts
|
||||
},
|
||||
globals: {
|
||||
console: fakeConsole
|
||||
}
|
||||
});
|
||||
|
||||
log4js.addAppender(smtpModule.configure(options), category);
|
||||
|
||||
return {
|
||||
logger: log4js.getLogger(category),
|
||||
mailer: fakeMailer,
|
||||
layouts: fakeLayouts,
|
||||
console: fakeConsole,
|
||||
results: msgs
|
||||
};
|
||||
}
|
||||
|
||||
function checkMessages(assert, result, sender, subject) {
|
||||
for (let i = 0; i < result.results.length; ++i) {
|
||||
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}`); // eslint-disable-line
|
||||
assert.ok(new RegExp(`.+Log event #${i + 1}\n$`).test(result.results[i].text));
|
||||
}
|
||||
}
|
||||
|
||||
log4js.clearAppenders();
|
||||
|
||||
test('log4js smtpAppender', (batch) => {
|
||||
batch.test('minimal config', (t) => {
|
||||
const setup = setupLogging('minimal config', {
|
||||
recipients: 'recipient@domain.com',
|
||||
SMTP: {
|
||||
port: 25,
|
||||
auth: {
|
||||
user: 'user@domain.com'
|
||||
}
|
||||
}
|
||||
});
|
||||
setup.logger.info('Log event #1');
|
||||
|
||||
t.equal(setup.results.length, 1, 'should be one message only');
|
||||
checkMessages(t, setup);
|
||||
t.end();
|
||||
});
|
||||
|
||||
batch.test('fancy config', (t) => {
|
||||
const 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');
|
||||
|
||||
t.equal(setup.results.length, 1, 'should be one message only');
|
||||
checkMessages(t, setup, 'sender@domain.com', 'This is subject');
|
||||
t.end();
|
||||
});
|
||||
|
||||
batch.test('config with layout', (t) => {
|
||||
const setup = setupLogging('config with layout', {
|
||||
layout: {
|
||||
type: 'tester'
|
||||
}
|
||||
});
|
||||
t.equal(setup.layouts.type, 'tester', 'should configure layout');
|
||||
t.end();
|
||||
});
|
||||
|
||||
batch.test('separate email for each event', (t) => {
|
||||
const setup = setupLogging('separate email for each event', {
|
||||
recipients: 'recipient@domain.com',
|
||||
SMTP: {
|
||||
port: 25,
|
||||
auth: {
|
||||
user: 'user@domain.com'
|
||||
}
|
||||
}
|
||||
});
|
||||
setTimeout(() => {
|
||||
setup.logger.info('Log event #1');
|
||||
}, 0);
|
||||
setTimeout(() => {
|
||||
setup.logger.info('Log event #2');
|
||||
}, 500);
|
||||
setTimeout(() => {
|
||||
setup.logger.info('Log event #3');
|
||||
}, 1100);
|
||||
setTimeout(() => {
|
||||
t.equal(setup.results.length, 3, 'there should be three messages');
|
||||
checkMessages(t, setup);
|
||||
t.end();
|
||||
}, 3000);
|
||||
});
|
||||
|
||||
batch.test('multiple events in one email', (t) => {
|
||||
const setup = setupLogging('multiple events in one email', {
|
||||
recipients: 'recipient@domain.com',
|
||||
sendInterval: 1,
|
||||
SMTP: {
|
||||
port: 25,
|
||||
auth: {
|
||||
user: 'user@domain.com'
|
||||
}
|
||||
}
|
||||
});
|
||||
setTimeout(() => {
|
||||
setup.logger.info('Log event #1');
|
||||
}, 0);
|
||||
setTimeout(() => {
|
||||
setup.logger.info('Log event #2');
|
||||
}, 100);
|
||||
setTimeout(() => {
|
||||
setup.logger.info('Log event #3');
|
||||
}, 1500);
|
||||
setTimeout(() => {
|
||||
t.equal(setup.results.length, 2, 'there should be two messages');
|
||||
t.equal(setup.results[0].to, 'recipient@domain.com');
|
||||
t.equal(setup.results[0].subject, 'Log event #1');
|
||||
t.equal(
|
||||
setup.results[0].text.match(new RegExp('.+Log event #[1-2]$', 'gm')).length,
|
||||
2
|
||||
);
|
||||
t.equal(setup.results[1].to, 'recipient@domain.com');
|
||||
t.equal(setup.results[1].subject, 'Log event #3');
|
||||
t.ok(/.+Log event #3\n$/.test(setup.results[1].text));
|
||||
t.end();
|
||||
}, 3000);
|
||||
});
|
||||
|
||||
batch.test('error when sending email', (t) => {
|
||||
const 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 () {
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
setup.logger.info('This will break');
|
||||
|
||||
t.test('should be logged to console', (assert) => {
|
||||
assert.equal(setup.console.errors.length, 1);
|
||||
assert.equal(setup.console.errors[0].msg, 'log4js.smtpAppender - Error happened');
|
||||
assert.equal(setup.console.errors[0].value.message, 'oh noes');
|
||||
assert.end();
|
||||
});
|
||||
t.end();
|
||||
});
|
||||
|
||||
batch.test('transport full config', (t) => {
|
||||
const setup = setupLogging('transport full config', {
|
||||
recipients: 'recipient@domain.com',
|
||||
transport: {
|
||||
plugin: 'sendmail',
|
||||
options: {
|
||||
path: '/usr/sbin/sendmail'
|
||||
}
|
||||
}
|
||||
});
|
||||
setup.logger.info('Log event #1');
|
||||
|
||||
t.equal(setup.results.length, 1, 'should be one message only');
|
||||
checkMessages(t, setup);
|
||||
t.end();
|
||||
});
|
||||
|
||||
batch.test('transport no-options config', (t) => {
|
||||
const setup = setupLogging('transport no-options config', {
|
||||
recipients: 'recipient@domain.com',
|
||||
transport: {
|
||||
plugin: 'sendmail'
|
||||
}
|
||||
});
|
||||
setup.logger.info('Log event #1');
|
||||
|
||||
t.equal(setup.results.length, 1, 'should be one message only');
|
||||
checkMessages(t, setup);
|
||||
t.end();
|
||||
});
|
||||
|
||||
batch.test('transport no-plugin config', (t) => {
|
||||
const setup = setupLogging('transport no-plugin config', {
|
||||
recipients: 'recipient@domain.com',
|
||||
transport: {}
|
||||
});
|
||||
setup.logger.info('Log event #1');
|
||||
|
||||
t.equal(setup.results.length, 1, 'should be one message only');
|
||||
checkMessages(t, setup);
|
||||
t.end();
|
||||
});
|
||||
|
||||
batch.test('attachment config', (t) => {
|
||||
const setup = setupLogging('attachment config', {
|
||||
recipients: 'recipient@domain.com',
|
||||
attachment: {
|
||||
enable: true
|
||||
},
|
||||
SMTP: {
|
||||
port: 25,
|
||||
auth: {
|
||||
user: 'user@domain.com'
|
||||
}
|
||||
}
|
||||
});
|
||||
setup.logger.info('Log event #1');
|
||||
|
||||
t.test('message should contain proper data', (assert) => {
|
||||
assert.equal(setup.results.length, 1);
|
||||
assert.equal(setup.results[0].attachments.length, 1);
|
||||
const attachment = setup.results[0].attachments[0];
|
||||
assert.equal(setup.results[0].text, 'See logs as attachment');
|
||||
assert.equal(attachment.filename, 'default.log');
|
||||
assert.equal(attachment.contentType, 'text/x-log');
|
||||
assert.ok(new RegExp(`.+Log event #${1}\n$`).test(attachment.content));
|
||||
assert.end();
|
||||
});
|
||||
t.end();
|
||||
});
|
||||
|
||||
batch.end();
|
||||
});
|
||||
@ -1,319 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
const vows = require('vows');
|
||||
const assert = require('assert');
|
||||
const log4js = require('../../lib/log4js');
|
||||
const sandbox = require('sandboxed-module');
|
||||
|
||||
function setupLogging(category, options) {
|
||||
const msgs = [];
|
||||
|
||||
const fakeMailer = {
|
||||
createTransport: function (name, opts) {
|
||||
return {
|
||||
config: opts,
|
||||
sendMail: function (msg, callback) {
|
||||
msgs.push(msg);
|
||||
callback(null, true);
|
||||
},
|
||||
close: function () {
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
const fakeLayouts = {
|
||||
layout: function (type, config) {
|
||||
this.type = type;
|
||||
this.config = config;
|
||||
return log4js.layouts.messagePassThroughLayout;
|
||||
},
|
||||
basicLayout: log4js.layouts.basicLayout,
|
||||
messagePassThroughLayout: log4js.layouts.messagePassThroughLayout
|
||||
};
|
||||
|
||||
const fakeConsole = {
|
||||
errors: [],
|
||||
error: function (msg, value) {
|
||||
this.errors.push({ msg: msg, value: value });
|
||||
}
|
||||
};
|
||||
|
||||
const fakeTransportPlugin = function () {
|
||||
};
|
||||
|
||||
const smtpModule = sandbox.require('../../lib/appenders/smtp', {
|
||||
singleOnly: true,
|
||||
requires: {
|
||||
nodemailer: fakeMailer,
|
||||
'nodemailer-sendmail-transport': fakeTransportPlugin,
|
||||
'nodemailer-smtp-transport': fakeTransportPlugin,
|
||||
'../layouts': fakeLayouts
|
||||
},
|
||||
globals: {
|
||||
console: fakeConsole
|
||||
}
|
||||
});
|
||||
|
||||
log4js.addAppender(smtpModule.configure(options), category);
|
||||
|
||||
return {
|
||||
logger: log4js.getLogger(category),
|
||||
mailer: fakeMailer,
|
||||
layouts: fakeLayouts,
|
||||
console: fakeConsole,
|
||||
results: msgs
|
||||
};
|
||||
}
|
||||
|
||||
function checkMessages(result, sender, subject) {
|
||||
for (let i = 0; i < result.results.length; ++i) {
|
||||
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}`); // eslint-disable-line
|
||||
assert.ok(new RegExp(`.+Log event #${i + 1}\n$`).test(result.results[i].text));
|
||||
}
|
||||
}
|
||||
|
||||
log4js.clearAppenders();
|
||||
vows.describe('log4js smtpAppender').addBatch({
|
||||
'minimal config': {
|
||||
topic: function () {
|
||||
const setup = setupLogging('minimal config', {
|
||||
recipients: 'recipient@domain.com',
|
||||
SMTP: {
|
||||
port: 25,
|
||||
auth: {
|
||||
user: 'user@domain.com'
|
||||
}
|
||||
}
|
||||
});
|
||||
setup.logger.info('Log event #1');
|
||||
return setup;
|
||||
},
|
||||
'there should be one message only': function (result) {
|
||||
assert.equal(result.results.length, 1);
|
||||
},
|
||||
'message should contain proper data': function (result) {
|
||||
checkMessages(result);
|
||||
}
|
||||
},
|
||||
'fancy config': {
|
||||
topic: function () {
|
||||
const 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;
|
||||
},
|
||||
'there should be one message only': function (result) {
|
||||
assert.equal(result.results.length, 1);
|
||||
},
|
||||
'message should contain proper data': function (result) {
|
||||
checkMessages(result, 'sender@domain.com', 'This is subject');
|
||||
}
|
||||
},
|
||||
'config with layout': {
|
||||
topic: function () {
|
||||
const 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 () {
|
||||
const self = this;
|
||||
const setup = setupLogging('separate email for each event', {
|
||||
recipients: 'recipient@domain.com',
|
||||
SMTP: {
|
||||
port: 25,
|
||||
auth: {
|
||||
user: 'user@domain.com'
|
||||
}
|
||||
}
|
||||
});
|
||||
setTimeout(() => {
|
||||
setup.logger.info('Log event #1');
|
||||
}, 0);
|
||||
setTimeout(() => {
|
||||
setup.logger.info('Log event #2');
|
||||
}, 500);
|
||||
setTimeout(() => {
|
||||
setup.logger.info('Log event #3');
|
||||
}, 1100);
|
||||
setTimeout(() => {
|
||||
self.callback(null, setup);
|
||||
}, 3000);
|
||||
},
|
||||
'there should be three messages': function (result) {
|
||||
assert.equal(result.results.length, 3);
|
||||
},
|
||||
'messages should contain proper data': function (result) {
|
||||
checkMessages(result);
|
||||
}
|
||||
},
|
||||
'multiple events in one email': {
|
||||
topic: function () {
|
||||
const self = this;
|
||||
const setup = setupLogging('multiple events in one email', {
|
||||
recipients: 'recipient@domain.com',
|
||||
sendInterval: 1,
|
||||
SMTP: {
|
||||
port: 25,
|
||||
auth: {
|
||||
user: 'user@domain.com'
|
||||
}
|
||||
}
|
||||
});
|
||||
setTimeout(() => {
|
||||
setup.logger.info('Log event #1');
|
||||
}, 0);
|
||||
setTimeout(() => {
|
||||
setup.logger.info('Log event #2');
|
||||
}, 100);
|
||||
setTimeout(() => {
|
||||
setup.logger.info('Log event #3');
|
||||
}, 1500);
|
||||
setTimeout(() => {
|
||||
self.callback(null, setup);
|
||||
}, 3000);
|
||||
},
|
||||
'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(/.+Log event #3\n$/.test(result.results[1].text));
|
||||
}
|
||||
},
|
||||
'error when sending email': {
|
||||
topic: function () {
|
||||
const 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 () {
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
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');
|
||||
}
|
||||
},
|
||||
'transport full config': {
|
||||
topic: function () {
|
||||
const setup = setupLogging('transport full config', {
|
||||
recipients: 'recipient@domain.com',
|
||||
transport: {
|
||||
plugin: 'sendmail',
|
||||
options: {
|
||||
path: '/usr/sbin/sendmail'
|
||||
}
|
||||
}
|
||||
});
|
||||
setup.logger.info('Log event #1');
|
||||
return setup;
|
||||
},
|
||||
'there should be one message only': function (result) {
|
||||
assert.equal(result.results.length, 1);
|
||||
},
|
||||
'message should contain proper data': function (result) {
|
||||
checkMessages(result);
|
||||
}
|
||||
},
|
||||
'transport no-options config': {
|
||||
topic: function () {
|
||||
const setup = setupLogging('transport no-options config', {
|
||||
recipients: 'recipient@domain.com',
|
||||
transport: {
|
||||
plugin: 'sendmail'
|
||||
}
|
||||
});
|
||||
setup.logger.info('Log event #1');
|
||||
return setup;
|
||||
},
|
||||
'there should be one message only': function (result) {
|
||||
assert.equal(result.results.length, 1);
|
||||
},
|
||||
'message should contain proper data': function (result) {
|
||||
checkMessages(result);
|
||||
}
|
||||
},
|
||||
'transport no-plugin config': {
|
||||
topic: function () {
|
||||
const setup = setupLogging('transport no-plugin config', {
|
||||
recipients: 'recipient@domain.com',
|
||||
transport: {}
|
||||
});
|
||||
setup.logger.info('Log event #1');
|
||||
return setup;
|
||||
},
|
||||
'there should be one message only': function (result) {
|
||||
assert.equal(result.results.length, 1);
|
||||
},
|
||||
'message should contain proper data': function (result) {
|
||||
checkMessages(result);
|
||||
}
|
||||
},
|
||||
'attachment config': {
|
||||
topic: function () {
|
||||
const setup = setupLogging('attachment config', {
|
||||
recipients: 'recipient@domain.com',
|
||||
attachment: {
|
||||
enable: true
|
||||
},
|
||||
SMTP: {
|
||||
port: 25,
|
||||
auth: {
|
||||
user: 'user@domain.com'
|
||||
}
|
||||
}
|
||||
});
|
||||
setup.logger.info('Log event #1');
|
||||
return setup;
|
||||
},
|
||||
'message should contain proper data': function (result) {
|
||||
assert.equal(result.results.length, 1);
|
||||
assert.equal(result.results[0].attachments.length, 1);
|
||||
const attachment = result.results[0].attachments[0];
|
||||
assert.equal(result.results[0].text, 'See logs as attachment');
|
||||
assert.equal(attachment.filename, 'default.log');
|
||||
assert.equal(attachment.contentType, 'text/x-log');
|
||||
assert.ok(new RegExp(`.+Log event #${1}\n$`).test(attachment.content));
|
||||
}
|
||||
}
|
||||
}).export(module);
|
||||
Loading…
x
Reference in New Issue
Block a user