-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest-send.js
112 lines (94 loc) · 3.28 KB
/
test-send.js
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
var test = require('tape')
, common = require('./common.js')
, Fbbot = require('../')
;
// reusable attachment
// https://developers.facebook.com/docs/messenger-platform/send-api-reference
// {
// "recipient_id": "USER_ID",
// "message_id": "mid.1473372944816:94f72b88c597657974",
// "attachment_id": "1745504518999123"
// }
// regular response
// {
// "recipient_id": "1008372609250235",
// "message_id": "mid.1456970487936:c34767dfe57ee6e339"
// }
common.iterateSendings(function(sending, handle, callback)
{
var type = handle.split('-')[0];
test.test('send ' + handle + ' with ' + Object.keys(sending.arguments).join(', ') + ' arguments', function(t)
{
// three checks for erroneous requests, and six checks for successful requests
// with addition or expected extra tests
t.plan((sending.error ? 3 : 6) + (sending.expectedTests || 0) + (sending.count ? sending.count.expected : 0));
// Id takes over phone_humber
var expectedPhone = sending.arguments.user['phone_number']
, expectedId = sending.arguments.user.id || (expectedPhone ? null : sending.arguments.user)
;
common.startApiServer(function(request, respond)
{
t.equal(request.query.access_token, common.fbbot.pageAccessToken, 'should supply proper access token');
t.deepEqual(request.body, sending.expected, 'expects to have proper payload for message type: ' + type);
respond(sending.response);
},
function(fbbotOptions, done)
{
var args = []
, fbbot
;
// custom per test endpoint
if (sending.endpoint)
{
fbbotOptions.apiUrl = sending.endpoint;
}
fbbot = new Fbbot(fbbotOptions);
t.equal(fbbot.options.apiUrl, fbbotOptions.apiUrl + fbbotOptions.pageAccessToken, 'respect custom apiUrl and augment it with correct token');
if (sending.arguments.user)
{
args.push(sending.arguments.user);
}
if (sending.arguments.type)
{
args.push(fbbot[sending.arguments.type] || sending.arguments.type); // e.g. fbbot['MARK_SEEN'] or 'custom_thing'
}
if (sending.arguments.data)
{
args.push(sending.arguments.data);
}
// count events
if (sending.count)
{
fbbot.on(sending.count.hook, function(payload)
{
t.equal(typeof payload, 'object', 'expect event ' + sending.count.hook + ' payload to be object');
});
}
// check events
fbbot.on('send.message', function(data)
{
if (expectedId)
{
t.equal(data.parent.recipient.id, expectedId, 'should have recipient id available via linked parent object');
}
else
{
t.equal(data.parent.recipient['phone_number'], expectedPhone, 'should have recipient phone_number available via linked parent object');
}
});
fbbot.send.apply(fbbot, args.concat(function(error, result)
{
if (sending.error)
{
t.ok(error.message.match(sending.error), 'expect to error with message: ' + sending.error);
}
else
{
t.error(error, 'should result in no error');
}
t.deepEqual(result, sending.response, 'expect to pass response all the way through');
done(callback);
}));
});
});
});