mirror of
https://github.com/log4js-node/log4js-node.git
synced 2025-12-08 19:26:01 +00:00
fix(test): moved mailgunAppender, hipchatAppender test to tap
This commit is contained in:
parent
c99165cf2a
commit
582df9fb12
@ -1,7 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
const vows = require('vows');
|
||||
const assert = require('assert');
|
||||
const test = require('tap').test;
|
||||
const log4js = require('../../lib/log4js');
|
||||
const sandbox = require('sandboxed-module');
|
||||
|
||||
@ -65,66 +64,68 @@ function setupLogging(category, options) {
|
||||
};
|
||||
}
|
||||
|
||||
vows.describe('HipChat appender').addBatch({
|
||||
'when logging to HipChat v2 API': {
|
||||
topic: function () {
|
||||
const customCallback = function () {
|
||||
return 'works';
|
||||
};
|
||||
test('HipChat appender', (batch) => {
|
||||
batch.test('when logging to HipChat v2 API', (t) => {
|
||||
const customCallback = function () {
|
||||
return 'works';
|
||||
};
|
||||
|
||||
const setup = setupLogging('myCategory', {
|
||||
type: 'hipchat',
|
||||
hipchat_token: 'User_Token_With_Notification_Privs',
|
||||
hipchat_room: 'Room_ID_Or_Name',
|
||||
hipchat_from: 'Log4js_Test',
|
||||
hipchat_notify: true,
|
||||
hipchat_host: 'hipchat.your-company.tld',
|
||||
hipchat_response_callback: customCallback
|
||||
});
|
||||
setup.logger.warn('Log event #1');
|
||||
return setup;
|
||||
},
|
||||
'a request to hipchat_host should be sent': function (topic) {
|
||||
const topic = setupLogging('myCategory', {
|
||||
type: 'hipchat',
|
||||
hipchat_token: 'User_Token_With_Notification_Privs',
|
||||
hipchat_room: 'Room_ID_Or_Name',
|
||||
hipchat_from: 'Log4js_Test',
|
||||
hipchat_notify: true,
|
||||
hipchat_host: 'hipchat.your-company.tld',
|
||||
hipchat_response_callback: customCallback
|
||||
});
|
||||
topic.logger.warn('Log event #1');
|
||||
|
||||
t.test('a request to hipchat_host should be sent', (assert) => {
|
||||
assert.equal(topic.lastRequest.notifier.host, 'hipchat.your-company.tld');
|
||||
assert.equal(topic.lastRequest.notifier.notify, true);
|
||||
assert.equal(topic.lastRequest.body, 'Log event #1');
|
||||
assert.equal(topic.lastRequest.level, 'warning');
|
||||
},
|
||||
'a custom callback to the HipChat response is supported': function (topic) {
|
||||
assert.equal(topic.lastRequest.callback(), 'works');
|
||||
}
|
||||
},
|
||||
'when missing options': {
|
||||
topic: function () {
|
||||
const setup = setupLogging('myLogger', {
|
||||
type: 'hipchat',
|
||||
});
|
||||
setup.logger.error('Log event #2');
|
||||
return setup;
|
||||
},
|
||||
'it sets some defaults': function (topic) {
|
||||
assert.end();
|
||||
});
|
||||
|
||||
t.equal(topic.lastRequest.callback(), 'works', 'a custom callback to the HipChat response is supported');
|
||||
t.end();
|
||||
});
|
||||
|
||||
batch.test('when missing options', (t) => {
|
||||
const topic = setupLogging('myLogger', {
|
||||
type: 'hipchat',
|
||||
});
|
||||
topic.logger.error('Log event #2');
|
||||
|
||||
t.test('it sets some defaults', (assert) => {
|
||||
assert.equal(topic.lastRequest.notifier.host, 'api.hipchat.com');
|
||||
assert.equal(topic.lastRequest.notifier.notify, false);
|
||||
assert.equal(topic.lastRequest.body, 'Log event #2');
|
||||
assert.equal(topic.lastRequest.level, 'failure');
|
||||
}
|
||||
},
|
||||
'when basicLayout is provided': {
|
||||
topic: function () {
|
||||
const setup = setupLogging('myLogger', {
|
||||
type: 'hipchat',
|
||||
layout: log4js.layouts.basicLayout
|
||||
});
|
||||
setup.logger.debug('Log event #3');
|
||||
return setup;
|
||||
},
|
||||
'it should include the timestamp': function (topic) {
|
||||
assert.end();
|
||||
});
|
||||
t.end();
|
||||
});
|
||||
|
||||
batch.test('when basicLayout is provided', (t) => {
|
||||
const topic = setupLogging('myLogger', {
|
||||
type: 'hipchat',
|
||||
layout: log4js.layouts.basicLayout
|
||||
});
|
||||
topic.logger.debug('Log event #3');
|
||||
|
||||
t.test('it should include the timestamp', (assert) => {
|
||||
// basicLayout adds [TIMESTAMP] [LEVEL] category - message
|
||||
// e.g. [2016-06-10 11:50:53.819] [DEBUG] myLogger - Log event #23
|
||||
|
||||
assert.match(topic.lastRequest.body, /^\[[^\]]+] \[[^\]]+].*Log event #3$/);
|
||||
assert.equal(topic.lastRequest.level, 'info');
|
||||
}
|
||||
}
|
||||
assert.end();
|
||||
});
|
||||
t.end();
|
||||
});
|
||||
|
||||
}).export(module);
|
||||
batch.end();
|
||||
});
|
||||
182
test/tap/mailgunAppender-test.js
Normal file
182
test/tap/mailgunAppender-test.js
Normal file
@ -0,0 +1,182 @@
|
||||
'use strict';
|
||||
|
||||
const test = require('tap').test;
|
||||
const log4js = require('../../lib/log4js');
|
||||
const sandbox = require('sandboxed-module');
|
||||
|
||||
function setupLogging(category, options) {
|
||||
const msgs = [];
|
||||
|
||||
const mailgunCredentials = {
|
||||
apiKey: options.apikey,
|
||||
domain: options.domain
|
||||
};
|
||||
|
||||
const fakeMailgun = function () {
|
||||
return {
|
||||
messages: function () {
|
||||
return {
|
||||
config: options,
|
||||
send: function (data, callback) {
|
||||
msgs.push(data);
|
||||
callback(false, { status: 'OK' });
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
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: [],
|
||||
logs: [],
|
||||
error: function (msg, value) {
|
||||
this.errors.push({ msg: msg, value: value });
|
||||
},
|
||||
log: function (msg, value) {
|
||||
this.logs.push({ msg: msg, value: value });
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const mailgunModule = sandbox.require('../../lib/appenders/mailgun', {
|
||||
requires: {
|
||||
'mailgun-js': fakeMailgun,
|
||||
'../layouts': fakeLayouts
|
||||
},
|
||||
globals: {
|
||||
console: fakeConsole
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
log4js.addAppender(mailgunModule.configure(options), category);
|
||||
|
||||
return {
|
||||
logger: log4js.getLogger(category),
|
||||
mailer: fakeMailgun,
|
||||
layouts: fakeLayouts,
|
||||
console: fakeConsole,
|
||||
mails: msgs,
|
||||
credentials: mailgunCredentials
|
||||
};
|
||||
}
|
||||
|
||||
function checkMessages(assert, result) {
|
||||
for (let i = 0; i < result.mails.length; ++i) {
|
||||
assert.equal(result.mails[i].from, 'sender@domain.com');
|
||||
assert.equal(result.mails[i].to, 'recepient@domain.com');
|
||||
assert.equal(result.mails[i].subject, 'This is subject');
|
||||
assert.ok(new RegExp(`.+Log event #${i + 1}`).test(result.mails[i].text));
|
||||
}
|
||||
}
|
||||
|
||||
log4js.clearAppenders();
|
||||
|
||||
test('log4js mailgunAppender', (batch) => {
|
||||
batch.test('mailgun setup', (t) => {
|
||||
const result = setupLogging('mailgun setup', {
|
||||
apikey: 'APIKEY',
|
||||
domain: 'DOMAIN',
|
||||
from: 'sender@domain.com',
|
||||
to: 'recepient@domain.com',
|
||||
subject: 'This is subject'
|
||||
});
|
||||
|
||||
t.test('mailgun credentials should match', (assert) => {
|
||||
assert.equal(result.credentials.apiKey, 'APIKEY');
|
||||
assert.equal(result.credentials.domain, 'DOMAIN');
|
||||
assert.end();
|
||||
});
|
||||
t.end();
|
||||
});
|
||||
|
||||
batch.test('basic usage', (t) => {
|
||||
const result = setupLogging('basic usage', {
|
||||
apikey: 'APIKEY',
|
||||
domain: 'DOMAIN',
|
||||
from: 'sender@domain.com',
|
||||
to: 'recepient@domain.com',
|
||||
subject: 'This is subject'
|
||||
});
|
||||
|
||||
result.logger.info('Log event #1');
|
||||
|
||||
t.equal(result.mails.length, 1, 'should be one message only');
|
||||
checkMessages(t, result);
|
||||
t.end();
|
||||
});
|
||||
|
||||
batch.test('config with layout', (t) => {
|
||||
const result = setupLogging('config with layout', {
|
||||
layout: {
|
||||
type: 'tester'
|
||||
}
|
||||
});
|
||||
t.equal(result.layouts.type, 'tester', 'should configure layout');
|
||||
t.end();
|
||||
});
|
||||
|
||||
batch.test('error when sending email', (t) => {
|
||||
const setup = setupLogging('separate email for each event', {
|
||||
apikey: 'APIKEY',
|
||||
domain: 'DOMAIN',
|
||||
from: 'sender@domain.com',
|
||||
to: 'recepient@domain.com',
|
||||
subject: 'This is subject'
|
||||
});
|
||||
|
||||
setup.mailer.messages = function () {
|
||||
return {
|
||||
send: function (msg, cb) {
|
||||
cb({ msg: 'log4js.mailgunAppender - Error happened' }, null);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
setup.logger.info('This will break');
|
||||
const cons = setup.console;
|
||||
|
||||
t.test('should be logged to console', (assert) => {
|
||||
assert.equal(cons.errors.length, 1);
|
||||
assert.equal(cons.errors[0].msg, 'log4js.mailgunAppender - Error happened');
|
||||
assert.end();
|
||||
});
|
||||
t.end();
|
||||
});
|
||||
|
||||
batch.test('separate email for each event', (t) => {
|
||||
const setup = setupLogging('separate email for each event', {
|
||||
apikey: 'APIKEY',
|
||||
domain: 'DOMAIN',
|
||||
from: 'sender@domain.com',
|
||||
to: 'recepient@domain.com',
|
||||
subject: 'This is subject'
|
||||
});
|
||||
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.mails.length, 3, 'should be three messages');
|
||||
checkMessages(t, setup);
|
||||
t.end();
|
||||
}, 3000);
|
||||
});
|
||||
|
||||
batch.end();
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user