-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathbot.test.js
More file actions
419 lines (362 loc) · 13.2 KB
/
Copy pathbot.test.js
File metadata and controls
419 lines (362 loc) · 13.2 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/* eslint-disable prefer-arrow-callback, no-unused-expressions */
import chai from 'chai';
import sinon from 'sinon';
import logger from 'winston';
import sinonChai from 'sinon-chai';
import irc from 'irc-upd';
import Bot from '../lib/bot';
import SlackStub from './stubs/slack-stub';
import ChannelStub from './stubs/channel-stub';
import ClientStub from './stubs/irc-client-stub';
import config from './fixtures/single-test-config.json';
chai.should();
chai.use(sinonChai);
describe('Bot', function () {
const sandbox = sinon.sandbox.create({
useFakeTimers: false,
useFakeServer: false
});
const createBot = (cfg = config) => {
const bot = new Bot(cfg);
bot.slack = new SlackStub();
bot.slack.rtm.start = sandbox.stub();
bot.slack.web.chat.postMessage = sandbox.stub();
bot.connect();
return bot;
};
beforeEach(function () {
sandbox.stub(logger, 'info');
sandbox.stub(logger, 'debug');
sandbox.stub(logger, 'error');
sandbox.stub(irc, 'Client', ClientStub);
ClientStub.prototype.say = sandbox.stub();
ClientStub.prototype.send = sandbox.stub();
ClientStub.prototype.join = sandbox.stub();
this.bot = createBot();
});
afterEach(function () {
sandbox.restore();
});
it('should invert the channel mapping', function () {
this.bot.invertedMapping['#irc'].should.equal('#slack');
});
it('should send correct message objects to slack', function () {
const text = 'testmessage';
const message = {
username: 'testuser (IRC)',
parse: 'full',
icon_url: 'http://api.adorable.io/avatars/48/testuser.png'
};
this.bot.sendToSlack('testuser', '#irc', text);
this.bot.slack.web.chat.postMessage.should.have.been.calledWith(1, text, message);
});
it('should send messages to slack groups if the bot is in the channel', function () {
this.bot.slack.rtm.dataStore.getChannelOrGroupByName = () => {
const channel = new ChannelStub();
delete channel.is_member;
channel.is_group = true;
return channel;
};
const text = 'testmessage';
const message = {
username: 'testuser (IRC)',
parse: 'full',
icon_url: 'http://api.adorable.io/avatars/48/testuser.png'
};
this.bot.sendToSlack('testuser', '#irc', text);
this.bot.slack.web.chat.postMessage.should.have.been.calledWith(1, text, message);
});
it('should not include an avatar for the bot\'s own messages', function () {
const text = 'testmessage';
const message = {
username: `${config.nickname} (IRC)`,
parse: 'full',
icon_url: undefined
};
this.bot.sendToSlack(config.nickname, '#irc', text);
this.bot.slack.web.chat.postMessage.should.have.been.calledWith(1, text, message);
});
it('should not include an avatar if avatarUrl is set to false', function () {
const noAvatarConfig = {
...config,
avatarUrl: false
};
const bot = createBot(noAvatarConfig);
const text = 'testmessage';
const message = {
username: 'testuser (IRC)',
parse: 'full',
icon_url: undefined
};
bot.sendToSlack('testuser', '#irc', text);
bot.slack.web.chat.postMessage.should.have.been.calledWith(1, text, message);
});
it('should use a custom icon url if given', function () {
const avatarUrl = 'https://cat.com';
const customAvatarConfig = {
...config,
avatarUrl
};
const bot = createBot(customAvatarConfig);
const text = 'testmessage';
const message = {
username: 'testuser (IRC)',
parse: 'full',
icon_url: avatarUrl
};
bot.sendToSlack('testuser', '#irc', text);
bot.slack.web.chat.postMessage.should.have.been.calledWith(1, text, message);
});
it('should replace $username in the given avatarUrl', function () {
const avatarUrl = 'https://robohash.org/$username.png';
const customAvatarConfig = {
...config,
avatarUrl
};
const bot = createBot(customAvatarConfig);
const text = 'testmessage';
const message = {
username: 'testuser (IRC)',
parse: 'full',
icon_url: 'https://robohash.org/testuser.png'
};
bot.sendToSlack('testuser', '#irc', text);
bot.slack.web.chat.postMessage.should.have.been.calledWith(1, text, message);
});
it('should lowercase channel names before sending to slack', function () {
const text = 'testmessage';
const message = {
username: 'testuser (IRC)',
parse: 'full',
icon_url: 'http://api.adorable.io/avatars/48/testuser.png'
};
this.bot.sendToSlack('testuser', '#IRC', text);
this.bot.slack.web.chat.postMessage.should.have.been.calledWith(1, text, message);
});
it('should not send messages to slack if the channel isn\'t in the channel mapping', function () {
this.bot.sendToSlack('user', '#wrongchan', 'message');
this.bot.slack.web.chat.postMessage.should.not.have.been.called;
});
it('should not send messages to slack if the bot isn\'t in the channel', function () {
this.bot.slack.rtm.dataStore.getChannelOrGroupByName = () => null;
this.bot.sendToSlack('user', '#irc', 'message');
this.bot.slack.web.chat.postMessage.should.not.have.been.called;
});
it('should not send messages to slack if the channel\'s is_member is false', function () {
this.bot.slack.rtm.dataStore.getChannelOrGroupByName = () => {
const channel = new ChannelStub();
channel.is_member = false;
return channel;
};
this.bot.sendToSlack('user', '#irc', 'message');
this.bot.slack.web.chat.postMessage.should.not.have.been.called;
});
it('should replace a bare username if the user is in-channel', function () {
const message = {
username: 'testuser (IRC)',
parse: 'full',
icon_url: 'http://api.adorable.io/avatars/48/testuser.png'
};
const before = 'testuser should be replaced in the message';
const after = '@testuser should be replaced in the message';
this.bot.sendToSlack('testuser', '#IRC', before);
this.bot.slack.web.chat.postMessage.should.have.been.calledWith(1, after, message);
});
it('should allow disabling Slack username suffix', function () {
const customSlackUsernameFormatConfig = {
...config,
slackUsernameFormat: '$username'
};
const bot = createBot(customSlackUsernameFormatConfig);
const text = 'textmessage';
const message = {
username: 'testuser',
parse: 'full',
icon_url: 'http://api.adorable.io/avatars/48/testuser.png'
};
bot.sendToSlack('testuser', '#irc', text);
bot.slack.web.chat.postMessage.should.have.been.calledWith(1, text, message);
});
it('should replace $username in custom Slack username format if given', function () {
const customSlackUsernameFormatConfig = {
...config,
slackUsernameFormat: 'prefix $username suffix'
};
const bot = createBot(customSlackUsernameFormatConfig);
const text = 'textmessage';
const message = {
username: 'prefix testuser suffix',
parse: 'full',
icon_url: 'http://api.adorable.io/avatars/48/testuser.png'
};
bot.sendToSlack('testuser', '#irc', text);
bot.slack.web.chat.postMessage.should.have.been.calledWith(1, text, message);
});
it('should send correct messages to irc', function () {
const text = 'testmessage';
const message = {
text,
channel: 'slack'
};
this.bot.sendToIRC(message);
const ircText = `<testuser> ${text}`;
ClientStub.prototype.say.should.have.been.calledWith('#irc', ircText);
});
it('should allow custom user formats for irc', function () {
const customIRCUsernameFormatConfig = {
...config,
ircUsernameFormat: '$username: '
};
const bot = createBot(customIRCUsernameFormatConfig);
const text = 'testmessage';
const message = {
text,
channel: 'slack'
};
bot.sendToIRC(message);
const ircText = `testuser: ${text}`;
ClientStub.prototype.say.should.have.been.calledWith('#irc', ircText);
});
it('should allow to remove user name in formats for irc', function () {
const customIRCUsernameFormatConfig = {
...config,
ircUsernameFormat: ''
};
const bot = createBot(customIRCUsernameFormatConfig);
const text = 'testmessage';
const message = {
text,
channel: 'slack'
};
bot.sendToIRC(message);
const ircText = `${text}`;
ClientStub.prototype.say.should.have.been.calledWith('#irc', ircText);
});
it('should send /me messages to irc', function () {
const text = 'testmessage';
const message = {
text,
channel: 'slack',
subtype: 'me_message'
};
this.bot.sendToIRC(message);
const ircText = `Action: testuser ${text}`;
ClientStub.prototype.say.should.have.been.calledWith('#irc', ircText);
});
it('should send files to irc', function () {
const link1 = 'test1';
const link2 = 'test2';
const text = 'testcomment';
const message = {
text: '',
channel: 'slack',
subtype: 'file_share',
file: {
permalink: link1,
permalink_public: link2,
initial_comment: {
comment: text
}
}
};
this.bot.sendToIRC(message);
const ircText = `<testuser> File uploaded ${link1} / ${link2} - ${text}`;
ClientStub.prototype.say.should.have.been.calledWith('#irc', ircText);
});
it('should not send messages to irc if the channel isn\'t in the channel mapping', function () {
this.bot.slack.rtm.dataStore.getChannelGroupOrDMById = () => null;
const message = {
channel: 'wrongchannel'
};
this.bot.sendToIRC(message);
ClientStub.prototype.say.should.not.have.been.called;
});
it('should send messages from slackbot if slackbot muting is off', function () {
const text = 'A message from Slackbot';
const message = {
text,
user: 'USLACKBOT'
};
this.bot.sendToIRC(message);
const ircText = `<testuser> ${text}`;
ClientStub.prototype.say.should.have.been.calledWith('#irc', ircText);
});
it('should not send messages from slackbot to irc if slackbot muting is on', function () {
this.bot.muteSlackbot = true;
const message = {
user: 'USLACKBOT',
getBody() {
return 'A message from Slackbot';
}
};
this.bot.sendToIRC(message);
ClientStub.prototype.say.should.not.have.been.called;
});
it('should parse text from slack when sending messages', function () {
const text = '<@USOMEID> <@USOMEID|readable>';
const message = {
text,
channel: 'slack'
};
this.bot.sendToIRC(message);
ClientStub.prototype.say.should.have.been.calledWith('#irc', '<testuser> @testuser readable');
});
it('should parse text from slack', function () {
this.bot.parseText('hi\nhi\r\nhi\r').should.equal('hi hi hi ');
this.bot.parseText('>><<').should.equal('>><<');
this.bot.parseText('<!channel> <!group> <!everyone>')
.should.equal('@channel @group @everyone');
this.bot.parseText('<#CSOMEID> <#CSOMEID|readable>')
.should.equal('#slack readable');
this.bot.parseText('<@USOMEID> <@USOMEID|readable>')
.should.equal('@testuser readable');
this.bot.parseText('<https://example.com>').should.equal('https://example.com');
this.bot.parseText('<https://example.com> <https://ap.no>')
.should.equal('https://example.com https://ap.no');
this.bot.parseText('<https://example.com|example.com> <https://ap.no|ap.no>')
.should.equal('example.com ap.no');
this.bot.parseText('<!somecommand> <!somecommand|readable>')
.should.equal('<somecommand> <readable>');
});
it('should handle entity-encoded messages from slack', function () {
this.bot.parseText('&lt;&gt;').should.equal('<>');
this.bot.parseText('<@UNONEID>').should.equal('<@UNONEID>');
this.bot.parseText('<#CNONEID>').should.equal('<#CNONEID>');
this.bot.parseText('<!channel>').should.equal('<!channel>');
this.bot.parseText('<<http://example.com|example.com>>').should.equal('<example.com>');
this.bot.parseText('java.util.List<java.lang.String>')
.should.equal('java.util.List<java.lang.String>');
});
it('should parse emojis correctly', function () {
this.bot.parseText(':smile:').should.equal(':)');
this.bot.parseText(':train:').should.equal(':train:');
});
it('should hide usernames for commands', function () {
const text = '!test command';
const message = {
text,
channel: 'slack'
};
this.bot.sendToIRC(message);
ClientStub.prototype.say.getCall(0).args.should.deep.equal([
'#irc', 'Command sent from Slack by testuser:'
]);
ClientStub.prototype.say.getCall(1).args.should.deep.equal(['#irc', text]);
});
it('should not forward messages from users in slack mute list', function () {
this.bot.muteUsers.slack = ['testuser'];
const text = 'testmessage';
const message = {
text,
channel: 'slack'
};
this.bot.sendToIRC(message);
ClientStub.prototype.say.should.not.have.been.called;
});
it('should not forward messages from users in irc mute list', function () {
this.bot.muteUsers.irc = ['testuser'];
const text = 'testmessage';
this.bot.sendToSlack('testuser', '#irc', text);
this.bot.slack.web.chat.postMessage.should.not.have.been.called;
});
});