forked from boybundit/linebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinebot.test.js
136 lines (124 loc) · 3.92 KB
/
linebot.test.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const linebot = require('../index.js');
const assert = require('assert');
const crypto = require('crypto');
const fetch = require('node-fetch');
const nock = require('nock');
const line = 'https://api.line.me/v2/bot';
const bot = linebot({
channelId: 1234567890,
channelSecret: 'secret',
channelAccessToken: 'token'
});
const req = {};
req.headers = {
'Content-Type': 'application/json',
Authorization: 'Bearer token',
'X-Line-Signature': 'signature'
};
req.body = {
events: [{
replyToken: 'nHuyWiB7yP5Zw52FIkcQobQuGDXCTA',
type: 'message',
timestamp: 1462629479859,
source: {
type: 'user',
userId: 'U206d25c2ea6bd87c17655609a1c37cb8'
},
message: {
id: '325708',
type: 'text',
text: 'Hello, world'
}
}]
};
req.rawBody = JSON.stringify(req.body);
req.headers['X-Line-Signature'] = crypto.createHmac('sha256', 'secret').update(req.rawBody, 'utf8').digest('base64');
describe('linebot', function () {
describe('#constructor()', function () {
it('should create a new LineBot instance.', function () {
assert.equal(linebot.LineBot, bot.constructor);
});
it('should have options as specified.', function () {
assert.equal(bot.options.verify, true);
});
});
describe('#verify()', function () {
it('should return true when the signature is correct.', function () {
const res = bot.verify(req.rawBody, req.headers['X-Line-Signature']);
assert.equal(res, true);
});
it('should return false when the signature is incorrect.', function () {
const res = bot.verify(req.rawBody, 'random signature');
assert.equal(res, false);
});
});
describe('#parse()', function () {
it('should raise message event.', function (done) {
const localBot = linebot({});
localBot.on('message', function (event) {
assert.equal(event, req.body.events[0]);
assert.equal(typeof event.reply, 'function');
if (event.source) {
assert.equal(typeof event.source.profile, 'function');
}
if (event.message) {
assert.equal(typeof event.message.content, 'function');
}
done();
});
localBot.parse(req.body);
});
});
describe('#get()', function () {
it('should return a promise.', function () {
const path = '/a/random/path';
nock(line).get(path).reply(404);
const res = bot.get(path).then(function (res) {
assert.equal(res.status, 404);
});
assert.equal(res.constructor, Promise);
return res;
});
});
describe('#post()', function () {
it('should return a promise.', function () {
const path = '/a/random/path';
const body = {
head: 'This is the head of the body. Do you not like it?'
};
nock(line).post(path, body).reply(200);
const res = bot.post(path, body).then(function (res) {
assert.equal(res.status, 200);
});
assert.equal(res.constructor, Promise);
return res;
});
});
describe('#parser()', function () {
it('should return a function that expects 2 arguments.', function () {
const parser = bot.parser();
assert.equal(typeof parser, 'function');
assert.equal(parser.length, 2);
});
});
describe('#listen()', function () {
it('should expect 3 arguments.', function () {
assert.equal(typeof bot.listen, 'function');
assert.equal(bot.listen.length, 3);
});
it('should start http server.', function (done) {
bot.listen('/linewebhook', 3000, function () {
done();
});
});
it('should handle POST request and return empty object.', function (done) {
fetch('http://localhost:3000/linewebhook', { method: 'POST', headers: req.headers, body: JSON.stringify(req.body) }).then(function (res) {
assert.equal(res.status, 200);
return res.json();
}).then(function (data) {
assert.deepEqual(data, {});
done();
});
});
});
});