2 var vows = require('vows');
3 var assert = require('assert');
4 var log4js = require('../lib/log4js');
5 var sandbox = require('sandboxed-module');
7 function setupLogging(category, options) {
10 var mailgunCredentials = {
11 apiKey: options.apikey,
12 domain: options.domain
15 var fakeMailgun = function (conf) {
17 messages: function () {
20 send: function (data, callback) {
22 callback(false, {status:"OK"});
30 layout: function (type, config) {
33 return log4js.layouts.messagePassThroughLayout;
35 basicLayout: log4js.layouts.basicLayout,
36 messagePassThroughLayout: log4js.layouts.messagePassThroughLayout
42 error: function (msg, value) {
43 this.errors.push({msg: msg, value: value});
45 log: function (msg, value) {
46 this.logs.push({msg: msg, value: value});
51 var mailgunModule = sandbox.require('../lib/appenders/mailgun', {
53 'mailgun-js': fakeMailgun,
54 '../layouts': fakeLayouts
62 log4js.addAppender(mailgunModule.configure(options), category);
65 logger: log4js.getLogger(category),
70 credentials: mailgunCredentials
74 function checkMessages(result) {
75 for (var i = 0; i < result.mails.length; ++i) {
76 assert.equal(result.mails[i].from, 'sender@domain.com');
77 assert.equal(result.mails[i].to, 'recepient@domain.com');
78 assert.equal(result.mails[i].subject, 'This is subject');
79 assert.ok(new RegExp('.+Log event #' + (i + 1)).test(result.mails[i].text));
83 log4js.clearAppenders();
85 vows.describe('log4js mailgunAppender').addBatch({
87 topic: setupLogging('mailgun setup', {
90 from: 'sender@domain.com',
91 to: 'recepient@domain.com',
92 subject: 'This is subject'
94 'mailgun credentials should match': function(result){
95 assert.equal(result.credentials.apiKey, 'APIKEY');
96 assert.equal(result.credentials.domain, 'DOMAIN');
102 var setup = setupLogging('basic usage', {
105 from: 'sender@domain.com',
106 to: 'recepient@domain.com',
107 subject: 'This is subject'
110 setup.logger.info("Log event #1");
113 'there should be one message only': function (result) {
114 assert.equal(result.mails.length, 1);
116 'message should contain proper data': function (result) {
117 checkMessages(result);
120 'config with layout': {
122 var setup = setupLogging('config with layout', {
129 'should configure layout': function (result) {
130 assert.equal(result.layouts.type, 'tester');
133 'error when sending email': {
135 var setup = setupLogging('separate email for each event', {
138 from: 'sender@domain.com',
139 to: 'recepient@domain.com',
140 subject: 'This is subject'
143 setup.mailer.messages = function () {
145 send: function (msg, cb) {
146 cb({msg: "log4js.mailgunAppender - Error happened"}, null);
151 setup.logger.info("This will break");
152 return setup.console;
154 'should be logged to console': function (cons) {
155 assert.equal(cons.errors.length, 1);
156 assert.equal(cons.errors[0].msg, 'log4js.mailgunAppender - Error happened');
159 'separate email for each event': {
162 var setup = setupLogging('separate email for each event', {
165 from: 'sender@domain.com',
166 to: 'recepient@domain.com',
167 subject: 'This is subject'
169 setTimeout(function () {
170 setup.logger.info('Log event #1');
172 setTimeout(function () {
173 setup.logger.info('Log event #2');
175 setTimeout(function () {
176 setup.logger.info('Log event #3');
178 setTimeout(function () {
179 self.callback(null, setup);
182 'there should be three messages': function (result) {
183 assert.equal(result.mails.length, 3);
185 'messages should contain proper data': function (result) {
186 checkMessages(result);