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

Commit 45ad27e

Browse files
author
Ben Brown
authored
Merge pull request #678 from agamrafaeli/facebook-bot-tests
Add tests for handleWebhookPayload in Facebook bot flow
2 parents 3754bf6 + 3970774 commit 45ad27e

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
"jscs": "^2.7.0",
3232
"mocha": "^2.4.5",
3333
"should": "^8.0.2",
34+
"should-sinon": "0.0.5",
35+
"sinon": "^1.17.7",
3436
"winston": "^2.1.1"
3537
},
3638
"scripts": {

tests/Facebook.js

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
var should = require('should');
2+
var sinon = require('sinon');
3+
require('should-sinon');
4+
var winston = require('winston');
5+
var Botkit = require('lib/Botkit.js');
6+
7+
describe('FacebookBot', function() {
8+
describe('constructor()', function(done) {
9+
it('Should have a facebookbot function', function(done) {
10+
Botkit.should.have.property('facebookbot').which.is.a.Function();
11+
done();
12+
});
13+
14+
it('FacebookBot should be an Object', function(done) {
15+
var facebook_bot = Botkit.facebookbot({});
16+
facebook_bot.should.be.an.Object();
17+
done();
18+
});
19+
});
20+
21+
22+
describe('handleWebhookPayload()', function(done) {
23+
it('Should be function', function(done) {
24+
//Setup
25+
var facebook_bot = Botkit.facebookbot({});
26+
27+
//Assertions
28+
facebook_bot.handleWebhookPayload.should.be.a.Function();
29+
done();
30+
});
31+
32+
function mock_entry() {
33+
return {
34+
sender: {id: "SENDER_ID"},
35+
recipient: {id: "RECIPIENT_ID"},
36+
timestamp: "TIMESTAMP"
37+
}
38+
};
39+
var res = {};
40+
41+
it('Should call receiveMessage on facebook_message.message', function(done) {
42+
//Setup
43+
var facebook_bot = Botkit.facebookbot({});
44+
45+
//Spies
46+
facebook_bot.receiveMessage = sinon.spy();
47+
48+
//Request
49+
var entry = mock_entry();
50+
entry.message = {
51+
text: "TEXT",
52+
seq:"SEQ",
53+
is_echo:"IS_ECHO",
54+
mid:"MID",
55+
sticker_id:"STICKER_ID",
56+
attachments:"ATTACHMENTS",
57+
quick_reply:"QUICK_REPLY"
58+
};
59+
var req = { body: { entry: [ { messaging: [ entry ] } ] } };
60+
facebook_bot.handleWebhookPayload(req, res, facebook_bot);
61+
62+
//Assertions
63+
facebook_bot.receiveMessage.should.be.called();
64+
done();
65+
});
66+
67+
it('Should trigger \'facebook_postback\' on facebook_message.postback', function(done) {
68+
//Setup
69+
var facebook_bot = Botkit.facebookbot({});
70+
71+
//Spies
72+
facebook_bot.trigger = sinon.spy();
73+
74+
//Request
75+
var entry = mock_entry();
76+
entry.postback = {
77+
payload: "PAYLOAD",
78+
referral: "REFERRAL"
79+
};
80+
var req = { body: { entry: [ { messaging: [ entry ] } ] } };
81+
facebook_bot.handleWebhookPayload(req, res, facebook_bot);
82+
83+
//Assertions
84+
facebook_bot.trigger.should.be.calledWithMatch('facebook_postback');
85+
done();
86+
});
87+
88+
it('Should trigger \'facebook_optin\' on facebook_message.optin', function(done) {
89+
//Setup
90+
var facebook_bot = Botkit.facebookbot({});
91+
92+
//Spies
93+
facebook_bot.trigger = sinon.spy();
94+
95+
//Request
96+
var entry = mock_entry();
97+
entry.optin = true;
98+
var req = { body: { entry: [ { messaging: [ entry ] } ] } };
99+
facebook_bot.handleWebhookPayload(req, res, facebook_bot);
100+
101+
//Assertions
102+
facebook_bot.trigger.should.be.calledWithMatch('facebook_optin');
103+
done();
104+
});
105+
106+
it('Should trigger \'message_delivered\' on facebook_message.delivery', function(done) {
107+
//Setup
108+
var facebook_bot = Botkit.facebookbot({});
109+
110+
//Spies
111+
facebook_bot.trigger = sinon.spy();
112+
113+
//Request
114+
var entry = mock_entry();
115+
entry.delivery = true;
116+
var req = { body: { entry: [ { messaging: [ entry ] } ] } };
117+
facebook_bot.handleWebhookPayload(req, res, facebook_bot);
118+
119+
//Assertions
120+
facebook_bot.trigger.should.be.calledWithMatch('message_delivered');
121+
done();
122+
});
123+
124+
it('Should trigger \'message_read\' on facebook_message.referral', function(done) {
125+
//Setup
126+
var facebook_bot = Botkit.facebookbot({});
127+
128+
//Spies
129+
facebook_bot.trigger = sinon.spy();
130+
131+
//Request
132+
var entry = mock_entry();
133+
entry.referral = true;
134+
var req = { body: { entry: [ { messaging: [ entry ] } ] } };
135+
facebook_bot.handleWebhookPayload(req, res, facebook_bot);
136+
137+
//Assertions
138+
facebook_bot.trigger.should.be.calledWithMatch('facebook_referral');
139+
done();
140+
});
141+
});
142+
});

0 commit comments

Comments
 (0)