Skip to content

Commit 8fe85d4

Browse files
author
Ben Brown
committed
Merge branch '0614'
2 parents a381142 + bb2f395 commit 8fe85d4

File tree

7 files changed

+9228
-22
lines changed

7 files changed

+9228
-22
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
'use strict';
2+
3+
const util = require('util');
4+
const EventEmitter = require('events').EventEmitter;
5+
6+
let mockWs;
7+
function mockWebSocket(url) {
8+
this.url = url;
9+
this.close = function() { };
10+
this.ping = function() { };
11+
mockWs = this;
12+
}
13+
util.inherits(mockWebSocket, EventEmitter);
14+
15+
jest.mock('ws', () => mockWebSocket);
16+
17+
let mockResponse;
18+
let mockRequest = {
19+
post: jest.fn().mockImplementation((params, cb) => {
20+
cb(null, mockResponse, mockResponse.body);
21+
})
22+
};
23+
jest.mock('request', () => mockRequest);
24+
25+
// these need to be required after mocks are set up
26+
const CoreBot = require('../../lib/CoreBot');
27+
const Slackbot_worker = require('../../lib/Slackbot_worker');
28+
29+
beforeEach(() => {
30+
jest.clearAllMocks();
31+
});
32+
33+
describe('startRTM', () => {
34+
it('connects to rtm url and calls callback function', () => {
35+
const botkit = CoreBot({});
36+
const bot = Slackbot_worker(botkit, {});
37+
38+
mockResponse = {
39+
statusCode: 200,
40+
body: JSON.stringify({
41+
ok: true,
42+
url: 'http://mockurl',
43+
self: {
44+
name: 'botkit'
45+
}
46+
})
47+
};
48+
49+
const cb = jest.fn();
50+
bot.startRTM(cb);
51+
mockWs.emit('open');
52+
expect(mockRequest.post).toHaveBeenCalledTimes(1);
53+
expect(mockWs.url).toBe('http://mockurl');
54+
expect(cb).toHaveBeenCalledTimes(1);
55+
56+
bot.closeRTM();
57+
58+
const errorArg = cb.mock.calls[0][0];
59+
expect(errorArg).toBeNull();
60+
});
61+
it('handles Slack API rtm.connect errors', () => {
62+
const botkit = CoreBot({});
63+
const bot = Slackbot_worker(botkit, {});
64+
65+
mockResponse = {
66+
statusCode: 200,
67+
body: JSON.stringify({
68+
ok: false,
69+
error: 'test_error'
70+
})
71+
};
72+
73+
const cb = jest.fn();
74+
bot.startRTM(cb);
75+
expect(mockRequest.post).toHaveBeenCalledTimes(1);
76+
expect(cb).toHaveBeenCalledTimes(1);
77+
bot.closeRTM();
78+
79+
const errorArg = cb.mock.calls[0][0];
80+
expect(errorArg).toBe('test_error');
81+
});
82+
it('handles websocket connection errors', () => {
83+
const botkit = CoreBot({});
84+
const bot = Slackbot_worker(botkit, {});
85+
86+
mockResponse = {
87+
statusCode: 200,
88+
body: JSON.stringify({
89+
ok: true,
90+
url: 'http://mockurl',
91+
self: {
92+
name: 'botkit'
93+
}
94+
})
95+
};
96+
97+
const cb = jest.fn();
98+
bot.startRTM(cb);
99+
mockWs.emit('error', 'test websocket error');
100+
expect(mockRequest.post).toHaveBeenCalledTimes(1);
101+
expect(cb).toHaveBeenCalledTimes(1);
102+
103+
bot.closeRTM();
104+
105+
const errorArg = cb.mock.calls[0][0];
106+
expect(errorArg).toBe('test websocket error');
107+
});
108+
});

changelog.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@
44

55
[Want to contribute? Read our guide!](https://github.com/howdyai/botkit/blob/master/CONTRIBUTING.md)
66

7+
# 0.6.14
8+
9+
* Fix for require_delivery option in Facebook bots. [PR #1312](https://github.com/howdyai/botkit/pull/1312)
10+
11+
* Errors encountered during Slack RTM connection process will now be reprorted to the callback function [PR #1335](https://github.com/howdyai/botkit/pull/1335)
12+
13+
* Updated methodology used to validate email addresses when restricting access to Cisco Spark bots
14+
15+
* Updated dependencies to latest stable versions
16+
17+
18+
719
# 0.6.13
820

921
* Fix bugs and refactor handling of message actions, particularly as they relate to Botkit Studio scripts

lib/CiscoSparkbot.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,12 @@ function Sparkbot(configuration) {
157157
domains = controller.config.limit_to_domain;
158158
}
159159

160+
var addressParser = require('email-addresses');
161+
var a = addressParser.parseOneAddress(message.raw_message.data.personEmail);
162+
160163
var allowed = false;
161164
for (var d = 0; d < domains.length; d++) {
162-
if (message.user.toLowerCase().indexOf(domains[d]) >= 0) {
165+
if (a.domain.toLowerCase() == domains[d]) {
163166
allowed = true;
164167
}
165168
}

lib/Facebook.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -489,24 +489,19 @@ function Facebookbot(configuration) {
489489
facebook_botkit.middleware.receive.use(function handleDelivery(bot, message, next) {
490490

491491
if (message.type === 'message_delivered' && facebook_botkit.config.require_delivery) {
492-
// get list of mids in this message
493-
for (var m = 0; m < message.delivery.mids.length; m++) {
494-
var mid = message.delivery.mids[m];
495-
496-
// loop through all active conversations this bot is having
497-
// and mark messages in conversations as delivered = true
498-
// note: we don't pass the real event in here because message_delivered events are excluded from conversations and won't ever match!
499-
bot.findConversation({user: message.user}, function(convo) {
500-
if (convo) {
501-
for (var s = 0; s < convo.sent.length; s++) {
502-
if (convo.sent[s].sent_timestamp <= message.delivery.watermark ||
503-
(convo.sent[s].api_response && convo.sent[s].api_response.message_id == mid)) {
504-
convo.sent[s].delivered = true;
505-
}
492+
// loop through all active conversations this bot is having
493+
// and mark messages in conversations as delivered = true
494+
// note: we don't pass the real event in here because message_delivered events are excluded from conversations and won't ever match!
495+
// also note: we only use message.delivery.watermark since message.delivery.mids can sometimes not be in the payload (#1311)
496+
bot.findConversation({user: message.user}, function(convo) {
497+
if (convo) {
498+
for (var s = 0; s < convo.sent.length; s++) {
499+
if (convo.sent[s].sent_timestamp <= message.delivery.watermark) {
500+
convo.sent[s].delivered = true;
506501
}
507502
}
508-
});
509-
}
503+
}
504+
});
510505
}
511506

512507
next();

lib/Slackbot_worker.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,22 @@ module.exports = function(botkit, config) {
128128
botkit.shutdown();
129129
};
130130

131-
bot.startRTM = function(cb) {
131+
bot.startRTM = function(userCb) {
132+
var userCbCalled = false;
133+
var cb = function() {
134+
if (!userCbCalled) {
135+
userCbCalled = true;
136+
userCb && userCb.apply(this, arguments);
137+
}
138+
};
132139
var lastPong = 0;
133140
bot.api.rtm.connect({}, function(err, res) {
134141
if (err) {
135-
return cb && cb(err);
142+
return cb(err);
136143
}
137144

138145
if (!res) {
139-
return cb && cb('Invalid response from rtm.start');
146+
return cb('Invalid response from rtm.start');
140147
}
141148

142149
bot.identity = res.self;
@@ -206,14 +213,16 @@ module.exports = function(botkit, config) {
206213

207214
botkit.startTicking();
208215

209-
cb && cb(null, bot, res);
216+
cb(null, bot, res);
210217
});
211218

212219
bot.rtm.on('error', function(err) {
213220
botkit.log.error('RTM websocket error!', err);
214221
if (pingTimeoutId) {
215222
clearTimeout(pingTimeoutId);
216223
}
224+
225+
cb(err);
217226
botkit.trigger('rtm_close', [bot, err]);
218227
});
219228

@@ -222,6 +231,8 @@ module.exports = function(botkit, config) {
222231
if (pingTimeoutId) {
223232
clearTimeout(pingTimeoutId);
224233
}
234+
235+
cb(message);
225236
botkit.trigger('rtm_close', [bot]);
226237

227238
/**

0 commit comments

Comments
 (0)