slackAppender-test.js
5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"use strict";
var vows = require('vows');
var assert = require('assert');
var log4js = require('../lib/log4js');
var sandbox = require('sandboxed-module');
function setupLogging(category, options) {
var msgs = [];
var slackCredentials = {
token: options.token,
channel_id: options.channel_id,
username: options.username,
format: options.format,
icon_url: options.icon_url
};
var fakeSlack = (function (key) {
function constructor() {
return {
options: key,
api: function (action, data, callback) {
msgs.push(data);
callback(false, {status: "sent"});
}
};
}
return constructor(key);
});
var fakeLayouts = {
layout: function (type, config) {
this.type = type;
this.config = config;
return log4js.layouts.messagePassThroughLayout;
},
basicLayout: log4js.layouts.basicLayout,
coloredLayout: log4js.layouts.coloredLayout,
messagePassThroughLayout: log4js.layouts.messagePassThroughLayout
};
var 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});
}
};
var slackModule = sandbox.require('../lib/appenders/slack', {
requires: {
'slack-node': fakeSlack,
'../layouts': fakeLayouts
},
globals: {
console: fakeConsole
}
});
log4js.addAppender(slackModule.configure(options), category);
return {
logger: log4js.getLogger(category),
mailer: fakeSlack,
layouts: fakeLayouts,
console: fakeConsole,
messages: msgs,
credentials: slackCredentials
};
}
function checkMessages(result) {
for (var i = 0; i < result.messages.length; ++i) {
assert.equal(result.messages[i].channel, '#CHANNEL');
assert.equal(result.messages[i].username, 'USERNAME');
assert.ok(new RegExp('.+Log event #' + (i + 1)).test(result.messages[i].text));
}
}
log4js.clearAppenders();
vows.describe('log4js slackAppender').addBatch({
'slack setup': {
topic: setupLogging('slack setup', {
token: 'TOKEN',
channel_id: "#CHANNEL",
username: "USERNAME",
format: "FORMAT",
icon_url: "ICON_URL"
}),
'slack credentials should match': function (result) {
assert.equal(result.credentials.token, 'TOKEN');
assert.equal(result.credentials.channel_id, '#CHANNEL');
assert.equal(result.credentials.username, 'USERNAME');
assert.equal(result.credentials.format, 'FORMAT');
assert.equal(result.credentials.icon_url, 'ICON_URL');
}
},
'basic usage': {
topic: function () {
var setup = setupLogging('basic usage', {
token: 'TOKEN',
channel_id: "#CHANNEL",
username: "USERNAME",
format: "FORMAT",
icon_url: "ICON_URL",
});
setup.logger.info("Log event #1");
return setup;
},
'there should be one message only': function (result) {
assert.equal(result.messages.length, 1);
},
'message should contain proper data': function (result) {
checkMessages(result);
}
},
'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 notification for each event': {
topic: function () {
var self = this;
var setup = setupLogging('separate notification for each event', {
token: 'TOKEN',
channel_id: "#CHANNEL",
username: "USERNAME",
format: "FORMAT",
icon_url: "ICON_URL",
});
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');
}, 1100);
setTimeout(function () {
self.callback(null, setup);
}, 3000);
},
'there should be three messages': function (result) {
assert.equal(result.messages.length, 3);
},
'messages should contain proper data': function (result) {
checkMessages(result);
}
}
}).export(module);