Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:howdyai/botkit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Brown committed Jun 19, 2017
2 parents c1afb76 + 9dd0980 commit 672aaad
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 21 deletions.
2 changes: 2 additions & 0 deletions __test__/lib/Botkit.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

let botkit;

jest.mock('../../lib/CoreBot', () => 'corebot');
Expand Down
41 changes: 28 additions & 13 deletions __test__/lib/Slack_web_api.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

let slackWebApi;
let mockRequest;
let mockResponse;
Expand Down Expand Up @@ -172,7 +174,20 @@ describe('postForm', () => {
expect(cb).toHaveBeenCalledWith(error);
});

test(`${methodName}: handles non 200 response code`, () => {
test(`${methodName}: handles 429 response code`, () => {
mockRequest.post.mockImplementation((params, callback) => {
callback(null, { statusCode: 429 }, null);
});

method('some.action', 'data', cb);

expect(mockRequest.post).toHaveBeenCalledTimes(1);
expect(cb).toHaveBeenCalledTimes(1);
const firstArg = cb.mock.calls[0][0];
expect(firstArg.message).toBe('Rate limit exceeded');
});

test(`${methodName}: handles other response codes`, () => {
mockRequest.post.mockImplementation((params, callback) => {
callback(null, { statusCode: 400 }, null);
});
Expand Down Expand Up @@ -206,7 +221,7 @@ describe('postForm', () => {
method('some.action', 'data', cb);

expect(mockRequest.post).toHaveBeenCalledTimes(1);
expect(cb).toHaveBeenCalledWith('not ok', { ok: false, error: 'not ok'});
expect(cb).toHaveBeenCalledWith('not ok', { ok: false, error: 'not ok' });
});
});
});
Expand All @@ -219,7 +234,7 @@ describe('api methods', () => {
instance = slackWebApi(mockBot, {});
cb = jest.fn();
jest.spyOn(instance, 'callAPI');
instance.callAPI.mockImplementation(() => {});
instance.callAPI.mockImplementation(() => { });
});

afterEach(() => {
Expand Down Expand Up @@ -253,41 +268,41 @@ describe('api methods', () => {
describe('special cases', () => {

test('chat.postMessage stringifies attachments', () => {
instance.chat.postMessage({attachments: []}, cb);
expect(instance.callAPI).toHaveBeenCalledWith('chat.postMessage', {attachments: '[]'}, cb);
instance.chat.postMessage({ attachments: [] }, cb);
expect(instance.callAPI).toHaveBeenCalledWith('chat.postMessage', { attachments: '[]' }, cb);
});

test('chat.postMessage handles attachments as Strings', () => {
jest.spyOn(JSON, 'stringify');
instance.chat.postMessage({attachments: 'string'}, cb);
expect(instance.callAPI).toHaveBeenCalledWith('chat.postMessage', {attachments: 'string'}, cb);
instance.chat.postMessage({ attachments: 'string' }, cb);
expect(instance.callAPI).toHaveBeenCalledWith('chat.postMessage', { attachments: 'string' }, cb);
expect(JSON.stringify).not.toHaveBeenCalled();
});

test('chat.postMessage handles attachments stringification errors', () => {
const error = new Error('WHOOPSIE');
jest.spyOn(JSON, 'stringify').mockImplementation(() => { throw error; });
instance.chat.postMessage({attachments: []}, cb);
instance.chat.postMessage({ attachments: [] }, cb);
expect(instance.callAPI).toHaveBeenCalledWith('chat.postMessage', {}, cb);
expect(JSON.stringify).toHaveBeenCalled();
});

test('chat.update stringifies attachments', () => {
instance.chat.update({attachments: []}, cb);
expect(instance.callAPI).toHaveBeenCalledWith('chat.update', {attachments: '[]'}, cb);
instance.chat.update({ attachments: [] }, cb);
expect(instance.callAPI).toHaveBeenCalledWith('chat.update', { attachments: '[]' }, cb);
});

test('chat.update handles attachments as Strings', () => {
jest.spyOn(JSON, 'stringify');
instance.chat.update({attachments: 'string'}, cb);
expect(instance.callAPI).toHaveBeenCalledWith('chat.update', {attachments: 'string'}, cb);
instance.chat.update({ attachments: 'string' }, cb);
expect(instance.callAPI).toHaveBeenCalledWith('chat.update', { attachments: 'string' }, cb);
expect(JSON.stringify).not.toHaveBeenCalled();
});

test('chat.postMessage handles attachments stringification errors', () => {
const error = new Error('WHOOPSIE');
jest.spyOn(JSON, 'stringify').mockImplementation(() => { throw error; });
instance.chat.update({attachments: []}, cb);
instance.chat.update({ attachments: [] }, cb);
expect(instance.callAPI).toHaveBeenCalledWith('chat.update', {}, cb);
expect(JSON.stringify).toHaveBeenCalled();
});
Expand Down
4 changes: 3 additions & 1 deletion docs/provisioning/cisco-spark.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ Take note of the bot username, you'll need it later.

**Note about your icon**: Cisco requires you host an avatar for your bot before you can create it in their portal. This bot needs to be a 512x512px image icon hosted anywhere on the web. This can be changed later.

You can copy and paste this URL for a Botkit icon you can use right away: `https://raw.githubusercontent.com/howdyai/botkit-starter-ciscospark/master/public/default_icon.png`
You can copy and paste this URL for a Botkit icon you can use right away:

https://raw.githubusercontent.com/howdyai/botkit-starter-ciscospark/master/public/default_icon.png

### 3. Copy your access token

Expand Down
2 changes: 1 addition & 1 deletion lib/CoreBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ function Botkit(configuration) {
}

if (typeof(events) == 'string') {
events = events.split(/\,/g).map(function(str) { return str.trim() })
events = events.split(/\,/g).map(function(str) { return str.trim(); });
}

for (var e = 0; e < events.length; e++) {
Expand Down
2 changes: 2 additions & 0 deletions lib/SlackBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ function Slackbot(configuration) {
cb(null, found_team);
}
});
} else {
cb(new Error(`could not find team ${team_id}`));
}
}
});
Expand Down
16 changes: 10 additions & 6 deletions lib/Slack_web_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var request = require('request');
/**
* Does nothing. Takes no params, returns nothing. It's a no-op!
*/
function noop() {}
function noop() { }

/**
* Returns an interface to the Slack API in the context of the given bot
Expand Down Expand Up @@ -187,7 +187,7 @@ module.exports = function(bot, config) {
};

function sanitizeOptions(options) {
if (options.attachments && typeof(options.attachments) != 'string') {
if (options.attachments && typeof (options.attachments) != 'string') {
try {
options.attachments = JSON.stringify(options.attachments);
} catch (err) {
Expand Down Expand Up @@ -227,10 +227,11 @@ module.exports = function(bot, config) {

request.post(params, function(error, response, body) {
bot.debug('Got response', error, body);
if(response.statusCode == 429) {
return cb(new Error('Rate limit exceeded'));
if (error) {
return cb(error);
}
if (!error && response.statusCode == 200) {

if (response.statusCode == 200) {
var json;
try {
json = JSON.parse(body);
Expand All @@ -239,8 +240,11 @@ module.exports = function(bot, config) {
}

return cb((json.ok ? null : json.error), json);
} else if (response.statusCode == 429) {
return cb(new Error('Rate limit exceeded'));
} else {
return cb(new Error('Invalid response'));
}
return cb(error || new Error('Invalid response'));
});
}
};

0 comments on commit 672aaad

Please sign in to comment.