Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1523 commits
Select commit Hold shift + click to select a range
aec1812
improves slack_web_api mocking
colestrode May 21, 2017
bb202e1
Update slack-events-api.md
May 22, 2017
350d04f
Merge pull request #824 from jonchurch/accept-spaces-message-events
May 22, 2017
ba3cb37
Merge pull request #830 from xavi-/patch-1
May 22, 2017
ec44c16
Correct url for Cisco Spark
hongily25 May 26, 2017
2227d4b
Merge pull request #860 from hongily25/patch-1
May 26, 2017
2700aad
Merge pull request #844 from colestrode/examples
Jun 5, 2017
a2236ba
Merge branch 'master' into unit
colestrode Jun 5, 2017
046f130
Merge pull request #857 from colestrode/unit
Jun 5, 2017
ec3c11d
Add missing error callback from team not found
alecl Jun 18, 2017
9a7eb66
Merge branch 'pipeline' of git.botkit.ai:benbrown/teamsbot into dev/p…
Jun 19, 2017
0509371
adds use strict directive to test files that use ES6
colestrode Jun 19, 2017
c5d0fa1
adds use strict
colestrode Jun 19, 2017
aeb1b02
fixes linting errors
colestrode Jun 19, 2017
c0769d3
adds test coverage for 429 rate limiting
colestrode Jun 19, 2017
7e03e63
Update cisco-spark.md
Jun 19, 2017
023d9a6
Merge pull request #891 from colestrode/strict-tests
Jun 19, 2017
23b2a48
add some docs
Jun 19, 2017
267e025
Merge branch 'master' of github.com:howdyai/botkit into dev/pipeline
Jun 19, 2017
9dd0980
Merge pull request #887 from alecl/master
Jun 19, 2017
c1afb76
Fixes to console bot
Jun 19, 2017
672aaad
Merge branch 'master' of github.com:howdyai/botkit
Jun 19, 2017
ca5010d
Merge pull request #854 from mehamasum/patch-1
Jun 19, 2017
966c06c
Merge pull request #843 from marchfederico/patch-1
Jun 19, 2017
f9256d5
Merge pull request #819 from jonchurch/consoleBotConvo
Jun 19, 2017
5212ce1
Fix issue with consolebot
Jun 19, 2017
af69623
Merge branch 'master' of github.com:howdyai/botkit
Jun 19, 2017
d9eb91f
Merge branch 'master' of github.com:howdyai/botkit into dev/pipeline
Jun 19, 2017
f9ae1c2
move slash commands and incming webhooks into ingestion pipeline
Jun 21, 2017
ce01915
Add some debug
Jun 21, 2017
df659a5
Add some debug
Jun 21, 2017
9e8a343
Add some debug
Jun 21, 2017
0c4db01
fix issue with slack ingest middleware
Jun 21, 2017
69303e6
fix issue with including express res object in bot
Jun 21, 2017
6e953f1
apply pipeline processing to rtm events
Jun 21, 2017
dfdf0d0
Refactor Facebook channel to use new processing pipeline
Jun 22, 2017
afc0ec4
refactor Cisco channel to use new pipeline middlewares
Jun 22, 2017
a5cc982
updates to cisco connector
Jun 22, 2017
4917990
consolebot uses ingest
Jun 22, 2017
53271a8
update botframework version
Jun 22, 2017
2e2e4d5
move setupWebserver out of each platform connector and into core
Jun 23, 2017
7e16b49
add attachment_upload to facebook_botkit.api
ouadie-lahdioui Jun 24, 2017
a4a4c2a
Add upload and postApi methods
ouadie-lahdioui Jun 24, 2017
0aadb15
Add upload method
ouadie-lahdioui Jun 24, 2017
8af4916
Change message structure
ouadie-lahdioui Jun 24, 2017
10debc2
Add attachment upload api example
ouadie-lahdioui Jun 24, 2017
a91d6c1
Change image url
ouadie-lahdioui Jun 24, 2017
1da1b3d
add an example to facebook readme file
ouadie-lahdioui Jun 24, 2017
a960326
add error handling to middlewares
Jun 26, 2017
4f51083
Merge branch 'dev/pipeline' of github.com:howdyai/botkit into dev/pip…
Jun 26, 2017
a692b8f
remove some dead code
Jun 26, 2017
e08ae80
linter fixes
Jun 26, 2017
ded57de
Move startConversation and createConversation into core
Jun 26, 2017
529928f
remove express and setupWebserver from Twilio and Botframework connec…
Jun 26, 2017
a4d274e
remove some whitespace, add in some code separating comments
Jun 26, 2017
27699d4
provide backwards compatibiltiy with the receive_via_postback config …
Jun 26, 2017
a13fcbd
emit self_message events instead of hiding them. allow interactive_m…
Jun 26, 2017
2903405
remove processing of message text from findConversation in slack conn…
Jun 26, 2017
0a39e07
add handler for slack message menus behind heard as text
Jun 26, 2017
b985118
linter fixes
Jun 26, 2017
4885eaf
move the hook validation
Jun 27, 2017
0738c77
Merge branch 'dev/pipeline' of github.com:howdyai/botkit into dev/pip…
Jun 27, 2017
f8ba945
Merge branch 'add-sms-support' of https://github.com/krismuniz/botkit…
Jun 27, 2017
7a52332
Merge in twilio sms bot
Jun 27, 2017
6e3cb2d
add links to twilio sms docs
Jun 27, 2017
ffd14d9
formatting of markdown
Jun 27, 2017
79361d9
adjustments to readme
Jun 27, 2017
a7cb5c8
adjustments to readme
Jun 27, 2017
4f8ddfa
Update readme-twiliosms.md
Jun 27, 2017
571a3e0
add twilio sms provisioning doc
Jun 27, 2017
379027d
Update readme.md
Jun 27, 2017
62bed34
add changelog
Jun 27, 2017
e50324c
updates to changelog
Jun 27, 2017
7e9a1cd
update dependency versions
Jun 27, 2017
ba64ec6
Clean up facebook_bot.js
ouadie-lahdioui Jun 27, 2017
b2f8334
reverse out change to twilio dependency for now due to conflicts with…
Jun 27, 2017
d43bb00
Merge branch 'facebookAttachmentUploadAPI' of https://github.com/ouad…
Jun 27, 2017
07b7833
Merge in new facebook api
Jun 27, 2017
8d7bc4b
Fix for issue #768
Jun 27, 2017
f31e796
update changelog
Jun 27, 2017
945da7b
better handling of slack message menus
Jun 27, 2017
bee9c5c
Merge pull request #908 from howdyai/dev/twiliosms
Jun 27, 2017
4d09303
typo in changelog
Jun 27, 2017
54944c5
Linked to Twilio prov doc
Jun 27, 2017
f67a671
typo
Jun 27, 2017
4361ec0
Merge branch 'master' of github.com:howdyai/botkit into teams
Jun 28, 2017
5070bae
add sample manifest file
Jun 28, 2017
dbaee23
Merge branch 'dev/pipeline' of github.com:howdyai/botkit into teams
Jun 28, 2017
0aa6c63
support for mention, direct_mention, direct_message events
Jun 28, 2017
073bf33
Added files.sharedPublicURL to slack_web_api
rubadeau Jun 28, 2017
01d109d
ability for bot to start a new convresation in the channel
Jun 28, 2017
c53d906
CiscoSpark version : inform when message come from not authorized dom…
MathRobin Jul 4, 2017
9b7b559
Merge branch 'master' of github.com:howdyai/botkit into dev/pipeline
Jul 5, 2017
6bc526a
updated by linter
Jul 5, 2017
abcf4c8
Adding example icons for the manifest
peterswimm Jul 11, 2017
7c00f03
passing full blown conversation object into run trigger if its available
Jul 12, 2017
f310adb
Merge branch 'teams' of git.botkit.ai:benbrown/teamsbot into teams
Jul 12, 2017
c1ab29c
support for teams attachments in studio
Jul 12, 2017
694526e
Merge branch 'teams' of git.botkit.ai:benbrown/teamsbot into teams
Jul 12, 2017
ce62252
basic working support for studio attachments
Jul 12, 2017
02b1faf
setting up teh compose extension stuff
Jul 14, 2017
bb7a065
setting up teh compose extension stuff resolved merge conflict
Jul 14, 2017
10be6de
fix on invoke
Jul 14, 2017
bc86d57
updated manifest, fixed spelling
Jul 14, 2017
f02c313
blah
Jul 14, 2017
947831a
made less chatty
Jul 14, 2017
cac33f7
blah
Jul 14, 2017
eaf61e0
capture extension query paarams
Jul 14, 2017
73bb3bf
fixed a thing
Jul 14, 2017
73b0517
fixed a thing
Jul 14, 2017
e1dcc0f
support for attachmentLayout field
Jul 20, 2017
e6e3aa7
Merge branch 'teams' of git.botkit.ai:benbrown/teamsbot into teams
Jul 20, 2017
5404cdd
remove some consoles
Jul 20, 2017
5965b4e
Merge branch 'master' of github.com:xoxco/botkit
Jul 24, 2017
c20fd16
Merge branch 'master' of github.com:howdyai/botkit into dev/bug936
Jul 24, 2017
0d5c1ee
Fix for issue #963: prune empty button arrays off of facebook carouse…
Jul 24, 2017
68847ba
update to handle team and user channel joins, etc
Jul 26, 2017
5087fc2
remove events api presence caveat from slack docs
jonchurch Jul 26, 2017
a78b3f2
Update to teams API calls
Jul 27, 2017
4ad7401
Fix issue with startPrivateConversationWithPersonId
Jul 27, 2017
581f079
Merge pull request #941 from jonchurch/fix-presence-docs
Jul 27, 2017
4e8d764
Fix mentions in Cisco Spark (fix for #940 #749 #661)
Jul 27, 2017
8f0031e
Merge pull request #942 from howdyai/dev/bug864
Jul 27, 2017
46b83c2
Merge pull request #937 from howdyai/dev/bug936
Jul 27, 2017
9e7502f
fix some linter errors
Jul 27, 2017
e705125
update package and changelog
Jul 27, 2017
4cfe0f5
Merge branch 'master' into dev/pipeline
Jul 27, 2017
c38ece5
Bring in teams changes from Glitch
Jul 28, 2017
53f90b1
Wit was missing! We have it!
Aug 4, 2017
3515370
wrap teams API calls into bot.api and controller.api objects
Aug 8, 2017
3b125d0
Add updateMessage function
Aug 9, 2017
2616aa1
fix readme
Aug 9, 2017
b15c8d4
Fix issue with ciscospark version
Aug 9, 2017
0566fc4
Merge branch 'master' of github.com:howdyai/botkit into dev/pipeline
Aug 9, 2017
458082f
Add support for Twilio SMS media attachments
krismuniz Aug 10, 2017
202bf2c
Fix typo in documentation for Twilio SMS bots
krismuniz Aug 10, 2017
1565ac5
add format middleware endpoint for preparing an outgoing botkit messa…
Aug 10, 2017
50fbf6a
add additional parameter to format middleware in order to not corrupt…
Aug 10, 2017
d36f602
Adjust method for building outbound facebook message
Aug 10, 2017
30adfc5
change method for building slack message
Aug 10, 2017
7b83d87
change name of `original_message` field to `raw_message` to be more d…
Aug 10, 2017
c12c5e5
in reply functions, set message.to field to identify recipient
Aug 10, 2017
468a017
add to to botframework replies
Aug 10, 2017
70b176c
Typescript Declaration (#1)
uny Aug 11, 2017
b03bbfa
Fix typo in docs
krismuniz Aug 11, 2017
9500fb5
Fix error when mediaUrl is undefined
krismuniz Aug 14, 2017
833ed87
Merge branch 'dev/pipeline' of github.com:howdyai/botkit into teams
Aug 14, 2017
f10da16
draft readme files for teams facing features
Aug 14, 2017
4243f28
Update "deploy to glitch" links to take advantage of glitch gallery
Aug 14, 2017
97fd4d0
Add middleware property to Controller interface
uny Aug 15, 2017
5951c74
fix import error
uny Aug 15, 2017
042dfde
first pass ephem
jonchurch Aug 15, 2017
a5f082b
Add get all tags function
ouadie-lahdioui Aug 17, 2017
9904ad9
Remove target folder
ouadie-lahdioui Aug 17, 2017
03bb181
Add tag to the message to send
ouadie-lahdioui Aug 17, 2017
aaecd39
Remove message and change HTTP verbe to Get
ouadie-lahdioui Aug 17, 2017
054603f
Add some exampls of how using tags
ouadie-lahdioui Aug 17, 2017
32f5727
Add documentattion
ouadie-lahdioui Aug 17, 2017
b9800bc
Typescript example
uny Aug 17, 2017
c53e135
Merge branch 'master' of github.com:howdyai/botkit into dev/pipeline
Aug 24, 2017
eb00881
Adjust how the send/format middlewares work
Aug 24, 2017
eb1500b
Merge branch 'dev/pipeline' into teams
Aug 24, 2017
3063d1b
incorporate changes from Glitch
Aug 24, 2017
f99def0
updates from glitch
Aug 24, 2017
edec735
updates to syntax to no longer require serviceUrl parameter on every …
Aug 24, 2017
9dc91c8
update use of original_message to reflect new raw_message field name
Aug 24, 2017
3bf26e5
make raw_message available in the conversation context
Aug 24, 2017
07af1ff
add format middleware
Aug 24, 2017
02f3811
add format middleware for teams
Aug 25, 2017
0e31fc4
facebook formatter
Aug 25, 2017
0af2df7
update readme
jonchurch Aug 27, 2017
d92d161
Merge branch 'slack-ephemeral' of https://github.com/jonchurch/botkit
Aug 29, 2017
baa959f
Merge branch 'feature/facebook_tags' of https://github.com/ouadie-lah…
Aug 29, 2017
50c8b36
merge in some pull requests
Aug 29, 2017
66a28ac
Merge branch 'feature/types' of https://github.com/uny/botkit
Aug 29, 2017
c2d4981
Update typescript declaration to include new tags api on Facebook
Aug 29, 2017
54994d2
Merge branch 'twilio-sms-media-att' of https://github.com/krismuniz/b…
Aug 29, 2017
250a7d5
Merge branch 'master' of https://github.com/GitTristan/botkit
Aug 29, 2017
2fba7ac
Merge branch 'master' of https://github.com/MathRobin/botkit
Aug 29, 2017
fac122b
Add support for facebook's built-in nlp
Aug 29, 2017
a9d6b70
Merge branch 'master' into dev/pipeline
Aug 29, 2017
f877d1b
Merge branch 'dev/pipeline' of github.com:howdyai/botkit into teams
Aug 29, 2017
b2c0043
update readme
Aug 29, 2017
97eaee2
remove temp file
Aug 29, 2017
ac4a0b4
update teams docs with examples and explanation of the various api fe…
Aug 29, 2017
8c1c3be
more docs for teams apis and events
Aug 29, 2017
b134bbc
more docs for teams apis and events
Aug 29, 2017
2fc3e26
more docs for teams apis and events
Aug 29, 2017
88b7757
more docs for teams apis and events
Aug 29, 2017
0f24376
readme
Aug 30, 2017
795bee8
more readme stuff
Aug 30, 2017
4a8d4c3
formatting
Aug 30, 2017
0928339
readme work
Aug 30, 2017
b9b5dbe
readme
Aug 30, 2017
f1dd131
readme
Aug 30, 2017
909d092
readme
Aug 30, 2017
825b09f
readme
Aug 30, 2017
726baef
readme
Aug 30, 2017
4831f52
readme
Aug 31, 2017
45de25b
readme
Aug 31, 2017
0a53c03
readme
Aug 31, 2017
10da6db
readme
Aug 31, 2017
d94adda
readme
Aug 31, 2017
262eb7e
readme
Aug 31, 2017
3efd520
readme
Sep 1, 2017
d7c3696
readme
Sep 1, 2017
d06d73a
readme
Sep 1, 2017
4edce35
remove old sample bot
Sep 3, 2017
5aab27c
remove sample file
Sep 3, 2017
48edb13
Add quit utterance to docs
jonchurch Sep 5, 2017
9e815b8
Merge pull request #995 from jonchurch/patch-3
Sep 5, 2017
cfe7404
Make clientId and clientSecret variable names consistent
Sep 6, 2017
b9ddabb
add hero and thumbnail card builders
Sep 6, 2017
92af29b
todid the todos
Sep 7, 2017
d542aa8
Changed header order
Sep 7, 2017
ac89ac9
add async
Sep 7, 2017
6081fb5
Merge branch 'dev/teams' of github.com:howdyai/botkit into teams
Sep 7, 2017
25615fe
URL fix pass
Sep 7, 2017
8c4bbeb
fixed subheaders
Sep 7, 2017
e5d31fc
add some error handling around bad tokens
Sep 7, 2017
fee3a72
Merge branch 'dev/teams' of github.com:howdyai/botkit into teams
Sep 7, 2017
f2d51b6
filled in urls
Sep 7, 2017
82e7cf0
Enact Step Zero Protocols
Sep 7, 2017
3b5c022
added studio button
Sep 8, 2017
aaa66a1
Made channel its own subsection
Sep 8, 2017
08324e1
One more change on framework
Sep 8, 2017
c2b89c3
yay more steps!
Sep 8, 2017
c873b5a
Nothing is automatic
Sep 8, 2017
0356919
remove console.log
Sep 8, 2017
36324cd
Merge branch 'master' of github.com:howdyai/botkit into teams
Sep 10, 2017
e69e5cc
update version in package
Sep 10, 2017
6a20eed
a lot more documentation for the pipeline process
Sep 10, 2017
319032f
more about pipeline
Sep 10, 2017
98c1554
more about pipeline
Sep 10, 2017
d37297c
more about pipeline
Sep 10, 2017
ca66807
finishing details for the pipeline release
Sep 11, 2017
6918a6a
update pipeline implementation on twilio sms, botframework, consolebot
Sep 11, 2017
52fbf09
add more changelog info
Sep 11, 2017
b29c1ff
fix link to howto
Sep 11, 2017
541da37
Merge branch 'dev/teams' of github.com:howdyai/botkit into teams
Sep 11, 2017
66c6515
Merge pull request #1014 from howdyai/dev/teams
Sep 11, 2017
69cab97
add links to new docs
Sep 11, 2017
1ca7a16
Merge branch 'teams'
Sep 11, 2017
f30a004
typo
Sep 11, 2017
11dd4d7
typo
Sep 11, 2017
b3b206e
Update readme.md
Sep 11, 2017
a24e37e
Fix a bad url!
Sep 12, 2017
5485de4
Fix typo
ouadie-lahdioui Sep 12, 2017
3304b43
Merge pull request #1020 from ouadie-lahdioui/patch-2
Sep 12, 2017
b578f9b
Update Slack_web_api.test.js
DarkSoulCool Oct 27, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ examples/db_team_bot/
.DS_Store
*/.DS_Store
.env
.idea
.vscode
coverage
2 changes: 1 addition & 1 deletion .jscsrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"disallowKeywords": ["with"],
"disallowMultipleLineBreaks": null,
"disallowMultipleVarDecl": null,
"maximumLineLength": 120,
"maximumLineLength": 180,
"disallowSpacesInsideObjectBrackets": null,
"requireCamelCaseOrUpperCaseIdentifiers": null,
"requireCurlyBraces": null,
Expand Down
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docs/provisioning/IMG
4 changes: 4 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ possible with your report. If you can, please include:

## Submitting Pull Requests

* Create, or link to an existing issue identifying the need driving your PR request. The issue can contain more details of the need for the PR as well as host debate as to which course of action the PR will take that will most serve the common good.
* Include screenshots and animated GIFs in your pull request whenever possible.
* Follow the JavaScript coding style with details from `.jscsrc` and `.editorconfig` files and use necessary plugins for your text editor.
* Run `npm test` before submitting and fix any issues.
* Add tests to cover any new functionality. Add and/or update tests for any updates to the code.
* Write documentation in [Markdown](https://daringfireball.net/projects/markdown).
* Please follow, [JSDoc](http://usejsdoc.org/) for proper documentation.
* Use short, present tense commit messages. See [Commit Message Styleguide](#git-commit-messages).

## Styleguides
Expand Down
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM library/node:slim

COPY . /app

RUN cd /app \
&& npm install --production

WORKDIR /app
30 changes: 30 additions & 0 deletions __test__/lib/Botkit.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

let botkit;

jest.mock('../../lib/CoreBot', () => 'corebot');
jest.mock('../../lib/SlackBot', () => 'slackbot');
jest.mock('../../lib/Facebook', () => 'facebook');
jest.mock('../../lib/TwilioIPMBot', () => 'twilioipm');
jest.mock('../../lib/TwilioSMSBot', () => 'twiliosms');
jest.mock('../../lib/BotFramework', () => 'botframework');
jest.mock('../../lib/CiscoSparkbot', () => 'spark');
jest.mock('../../lib/ConsoleBot', () => 'console');
jest.mock('../../lib/Teams', () => 'teams');

beforeEach(() => {
jest.clearAllMocks();
botkit = require('../../lib/Botkit');
});

test('exports bot interfaces', () => {
expect(botkit.core).toBe('corebot');
expect(botkit.slackbot).toBe('slackbot');
expect(botkit.facebookbot).toBe('facebook');
expect(botkit.twilioipmbot).toBe('twilioipm');
expect(botkit.twiliosmsbot).toBe('twiliosms');
expect(botkit.botframeworkbot).toBe('botframework');
expect(botkit.sparkbot).toBe('spark');
expect(botkit.teamsbot).toBe('teams');
expect(botkit.consolebot).toBe('console');
});
321 changes: 321 additions & 0 deletions __test__/lib/Slack_web_api.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,321 @@
'use strict';

let slackWebApi;
let mockRequest;
let mockResponse;
let mockBot;

mockRequest = {};

jest.mock('request', () => mockRequest);

beforeEach(() => {
mockResponse = {
statusCode: 200,
body: '{"ok": true}'
};

mockBot = {
config: {},
debug: jest.fn(),
log: jest.fn(),
userAgent: jest.fn().mockReturnValue('jesting')
};

mockBot.log.error = jest.fn();

mockRequest.post = jest.fn().mockImplementation((params, cb) => {
cb(null, mockResponse, mockResponse.body);
});

slackWebApi = require('../../lib/Slack_web_api');
});

describe('config', () => {
test('default api_root', () => {
const instance = slackWebApi(mockBot, {});
expect(instance.api_url).toBe('https://slack.com/api/');
});

test('setting api_root', () => {
mockBot.config.api_root = 'http://www.somethingelse.com';
const instance = slackWebApi(mockBot, {});
expect(instance.api_url).toBe('http://www.somethingelse.com/api/');
});
});

describe('callApi', () => {
let instance;

test('uses data.token by default and post', () => {
const data = {
token: 'abc123'
};
const cb = jest.fn();

instance = slackWebApi(mockBot, {});
instance.callAPI('some.method', data, cb);

expect(mockRequest.post).toHaveBeenCalledTimes(1);
const firstArg = mockRequest.post.mock.calls[0][0];
expect(firstArg.form.token).toBe('abc123');
});

test('uses config.token if data.token is missing', () => {
const data = {};
const cb = jest.fn();

instance = slackWebApi(mockBot, { token: 'abc123' });
instance.callAPI('some.method', data, cb);

expect(mockRequest.post).toHaveBeenCalledTimes(1);
const firstArg = mockRequest.post.mock.calls[0][0];
expect(firstArg.form.token).toBe('abc123');
});

// this case is specific to callAPI, shared cases will be tested below
test(`handles multipart data`, () => {
const cb = jest.fn();
instance = slackWebApi(mockBot, {});
instance.callAPI('some.method', 'data', cb, true);

expect(mockRequest.post).toHaveBeenCalledTimes(1);
const firstArg = mockRequest.post.mock.calls[0][0];

expect(firstArg.formData).toBe('data');
expect(firstArg.form).toBeUndefined();
expect(cb).toHaveBeenCalledWith(null, { ok: true });
});
});

describe('callApiWithoutToken', () => {
let instance;

test('uses data values by default', () => {
const data = {
client_id: 'id',
client_secret: 'secret',
redirect_uri: 'redirectUri'
};
const cb = jest.fn();

instance = slackWebApi(mockBot, {});
instance.callAPIWithoutToken('some.method', data, cb);

expect(mockRequest.post.mock.calls.length).toBe(1);
const firstArg = mockRequest.post.mock.calls[0][0];
expect(firstArg.form.client_id).toBe('id');
expect(firstArg.form.client_secret).toBe('secret');
expect(firstArg.form.redirect_uri).toBe('redirectUri');
});

test('uses config values if not set in data', () => {
const config = {
clientId: 'id',
clientSecret: 'secret',
redirectUri: 'redirectUri'
};
const cb = jest.fn();

// this seems to be an API inconsistency:
// callAPIWithoutToken uses bot.config, but callAPI uses that passed config
mockBot.config = config;

instance = slackWebApi(mockBot, {});
instance.callAPIWithoutToken('some.method', {}, cb);

expect(mockRequest.post.mock.calls.length).toBe(1);
const firstArg = mockRequest.post.mock.calls[0][0];
expect(firstArg.form.client_id).toBe('id');
expect(firstArg.form.client_secret).toBe('secret');
expect(firstArg.form.redirect_uri).toBe('redirectUri');
});
});

describe('postForm', () => {

['callAPI', 'callAPIWithoutToken'].forEach((methodName) => {
let method;
let cb;

beforeEach(() => {
const instance = slackWebApi(mockBot, {});
method = instance[methodName];
cb = jest.fn();
});

test(`${methodName}: handles success`, () => {
method('some.action', 'data', cb);
expect(mockRequest.post).toHaveBeenCalledTimes(1);
const firstArg = mockRequest.post.mock.calls[0][0];

// do some thorough assertions here for a baseline
expect(firstArg.url).toMatch(/some.action$/);
expect(firstArg.form).toBe('data');
expect(firstArg.formData).toBeUndefined();
expect(firstArg.headers).toEqual({ 'User-Agent': 'jesting' });
expect(cb).toHaveBeenCalledWith(null, { ok: true });
});

test(`${methodName}: defaults callback`, () => {
method('some.action', 'data');
expect(mockRequest.post).toHaveBeenCalledTimes(1);
});

test(`${methodName}: handles request lib error`, () => {
const error = new Error('WHOOPS!');
mockRequest.post.mockImplementation((params, callback) => {
callback(error, null, null);
});

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

expect(mockRequest.post).toHaveBeenCalledTimes(1);
expect(cb).toHaveBeenCalledWith(error);
});

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);
});

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('Invalid response');
});

test(`${methodName}: handles error parsing body`, () => {
mockRequest.post.mockImplementation((params, callback) => {
callback(null, { statusCode: 200 }, '{');
});

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

expect(mockRequest.post).toHaveBeenCalledTimes(1);
expect(cb).toHaveBeenCalledTimes(1);
const firstArg = cb.mock.calls[0][0];
expect(firstArg).toBeInstanceOf(Error);
});

test(`${methodName}: handles ok.false response`, () => {
mockRequest.post.mockImplementation((params, callback) => {
callback(null, { statusCode: 200 }, '{ "ok": false, "error": "not ok"}');
});

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

expect(mockRequest.post).toHaveBeenCalledTimes(1);
expect(cb).toHaveBeenCalledWith('not ok', { ok: false, error: 'not ok' });
});
});
});

describe('api methods', () => {
let instance;
let cb;

beforeEach(() => {
instance = slackWebApi(mockBot, {});
cb = jest.fn();
jest.spyOn(instance, 'callAPI');
instance.callAPI.mockImplementation(() => { });
});

afterEach(() => {
if (jest.isMockFunction(JSON.stringify)) {
JSON.stringify.mockRestore();
}
instance.callAPI.mockRestore();
});

test('spot check api methods ', () => {
// testing for all methods seems wasteful, but let's confirm the methods got built correctly and test the following scenarios
// two levels
expect(instance.auth).toBeDefined();
expect(instance.auth.test).toBeDefined();

instance.auth.test('options', 'cb');
const firstCallArgs = instance.callAPI.mock.calls[0];
expect(firstCallArgs).toEqual(['auth.test', 'options', 'cb']);

// three levels
expect(instance.users).toBeDefined();
expect(instance.users.profile).toBeDefined();
expect(instance.users.profile.get).toBeDefined();

instance.users.profile.get('options', 'cb');
const secondCallArgs = instance.callAPI.mock.calls[1];
expect(secondCallArgs).toEqual(['users.profile.get', 'options', 'cb']);
});

describe('special cases', () => {

test('chat.postMessage stringifies attachments', () => {
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);
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);
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);
});

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);
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);
expect(instance.callAPI).toHaveBeenCalledWith('chat.update', {}, cb);
expect(JSON.stringify).toHaveBeenCalled();
});

test('files.upload should not use multipart if file is false', () => {
const options = { file: false, token: 'abc123' };
instance.files.upload(options, cb);
expect(instance.callAPI).toHaveBeenCalledWith('files.upload', options, cb, false);
});

test('files.upload should use multipart if file is true', () => {
const options = { file: true, token: 'abc123' };
instance.files.upload(options, cb);
expect(instance.callAPI).toHaveBeenCalledWith('files.upload', options, cb, true);
});
});
});
Loading