@tiledesk/tiledesk-server 2.10.65 → 2.10.67

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,376 @@
1
+ //During the test the env variable is set to test
2
+ process.env.NODE_ENV = 'test';
3
+ process.env.LOG_LEVEL = 'error';
4
+
5
+ var projectService = require('../services/projectService');
6
+ var userService = require('../services/userService');
7
+ let log = false;
8
+
9
+ //Require the dev-dependencies
10
+ let chai = require('chai');
11
+ let chaiHttp = require('chai-http');
12
+ let server = require('../app');
13
+ let should = chai.should();
14
+ var fs = require('fs');
15
+ const path = require('path');
16
+
17
+ var expect = chai.expect;
18
+ var assert = chai.assert;
19
+
20
+ chai.use(chaiHttp);
21
+
22
+ describe('WebhookRoute', () => {
23
+
24
+ it('create-new-webhook', (done) => {
25
+
26
+ var email = "test-signup-" + Date.now() + "@email.com";
27
+ var pwd = "pwd";
28
+
29
+ userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
30
+ projectService.create("test-webhook-create", savedUser._id).then(function (savedProject) {
31
+
32
+ chai.request(server)
33
+ .post('/' + savedProject._id + '/faq_kb')
34
+ .auth(email, pwd)
35
+ .send({ "name": "testbot", type: "tilebot", language: "en", template: "blank" })
36
+ .end((err, res) => {
37
+
38
+ if (err) { console.error("err: ", err); }
39
+ if (log) { console.log("res.body", res.body); }
40
+
41
+ res.should.have.status(200);
42
+ res.body.should.be.a('object');
43
+
44
+ let chatbot_id = res.body._id;
45
+ let webhook_intent_id = "3bfda939-ff76-4762-bbe0-fc0f0dc4c777"
46
+
47
+ chai.request(server)
48
+ .post('/' + savedProject._id + '/webhooks/')
49
+ .auth(email, pwd)
50
+ .send({ chatbot_id: chatbot_id, block_id: webhook_intent_id })
51
+ .end((err, res) => {
52
+
53
+ if (err) { console.error("err: ", err); }
54
+ if (log) { console.log("res.body", res.body); }
55
+
56
+ res.should.have.status(200);
57
+ res.body.should.be.a('object');
58
+ expect(res.body.async).to.equal(true);
59
+ expect(res.body.id_project).to.equal(savedProject._id.toString());
60
+ expect(res.body.chatbot_id).to.equal(chatbot_id);
61
+ expect(res.body.block_id).to.equal(webhook_intent_id);
62
+ should.exist(res.body.webhook_id)
63
+ expect(res.body).to.haveOwnProperty('webhook_id')
64
+ expect(res.body.webhook_id).to.have.length(32)
65
+
66
+ done();
67
+
68
+ });
69
+ });
70
+ });
71
+ });
72
+ })
73
+
74
+ it('create-and-get-webhook', (done) => {
75
+
76
+ var email = "test-signup-" + Date.now() + "@email.com";
77
+ var pwd = "pwd";
78
+
79
+ userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
80
+ projectService.create("test-webhook-create", savedUser._id).then(function (savedProject) {
81
+
82
+ chai.request(server)
83
+ .post('/' + savedProject._id + '/faq_kb')
84
+ .auth(email, pwd)
85
+ .send({ "name": "testbot", type: "tilebot", language: "en", template: "blank" })
86
+ .end((err, res) => {
87
+
88
+ if (err) { console.error("err: ", err); }
89
+ if (log) { console.log("res.body", res.body); }
90
+
91
+ res.should.have.status(200);
92
+ res.body.should.be.a('object');
93
+
94
+ let chatbot_id = res.body._id;
95
+ let webhook_intent_id = "3bfda939-ff76-4762-bbe0-fc0f0dc4c777"
96
+
97
+ chai.request(server)
98
+ .post('/' + savedProject._id + '/webhooks/')
99
+ .auth(email, pwd)
100
+ .send({ chatbot_id: chatbot_id, block_id: webhook_intent_id })
101
+ .end((err, res) => {
102
+
103
+ if (err) { console.error("err: ", err); }
104
+ if (log) { console.log("res.body", res.body); }
105
+
106
+ res.should.have.status(200);
107
+ res.body.should.be.a('object');
108
+
109
+ chai.request(server)
110
+ .get('/' + savedProject._id + '/webhooks/' + chatbot_id)
111
+ .auth(email, pwd)
112
+ .end((err, res) => {
113
+
114
+ if (err) { console.error("err: ", err); }
115
+ if (log) { console.log("res.body", res.body); }
116
+
117
+ res.should.have.status(200);
118
+ res.body.should.be.a('object');
119
+ expect(res.body.async).to.equal(true);
120
+ expect(res.body.id_project).to.equal(savedProject._id.toString());
121
+ expect(res.body.chatbot_id).to.equal(chatbot_id);
122
+ expect(res.body.block_id).to.equal(webhook_intent_id);
123
+ should.exist(res.body.webhook_id)
124
+ expect(res.body).to.haveOwnProperty('webhook_id')
125
+ expect(res.body.webhook_id).to.have.length(32)
126
+
127
+ })
128
+
129
+ done();
130
+
131
+ });
132
+ });
133
+ });
134
+ });
135
+ })
136
+
137
+ it('update-webhook', (done) => {
138
+
139
+ var email = "test-signup-" + Date.now() + "@email.com";
140
+ var pwd = "pwd";
141
+
142
+ userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
143
+ projectService.create("test-webhook-create", savedUser._id).then(function (savedProject) {
144
+
145
+ chai.request(server)
146
+ .post('/' + savedProject._id + '/faq_kb')
147
+ .auth(email, pwd)
148
+ .send({ "name": "testbot", type: "tilebot", language: "en", template: "blank" })
149
+ .end((err, res) => {
150
+
151
+ if (err) { console.error("err: ", err); }
152
+ if (log) { console.log("res.body", res.body); }
153
+
154
+ res.should.have.status(200);
155
+ res.body.should.be.a('object');
156
+
157
+ let chatbot_id = res.body._id;
158
+ let webhook_intent_id = "3bfda939-ff76-4762-bbe0-fc0f0dc4c777"
159
+
160
+ chai.request(server)
161
+ .post('/' + savedProject._id + '/webhooks/')
162
+ .auth(email, pwd)
163
+ .send({ chatbot_id: chatbot_id, block_id: webhook_intent_id })
164
+ .end((err, res) => {
165
+
166
+ if (err) { console.error("err: ", err); }
167
+ if (log) { console.log("res.body", res.body); }
168
+
169
+ res.should.have.status(200);
170
+ res.body.should.be.a('object');
171
+ expect(res.body.async).to.equal(true);
172
+
173
+ chai.request(server)
174
+ .put('/' + savedProject._id + '/webhooks/' + chatbot_id)
175
+ .auth(email, pwd)
176
+ .send({ async: false })
177
+ .end((err, res) => {
178
+
179
+ if (err) { console.error("err: ", err); }
180
+ if (log) { console.log("res.body", res.body); }
181
+
182
+ res.should.have.status(200);
183
+ res.body.should.be.a('object');
184
+ expect(res.body.async).to.equal(false);
185
+
186
+ done();
187
+
188
+ });
189
+ });
190
+
191
+ });
192
+ });
193
+ });
194
+ })
195
+
196
+ it('rigenerate-url-webhook', (done) => {
197
+
198
+ var email = "test-signup-" + Date.now() + "@email.com";
199
+ var pwd = "pwd";
200
+
201
+ userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
202
+ projectService.create("test-webhook-create", savedUser._id).then(function (savedProject) {
203
+
204
+ chai.request(server)
205
+ .post('/' + savedProject._id + '/faq_kb')
206
+ .auth(email, pwd)
207
+ .send({ "name": "testbot", type: "tilebot", language: "en", template: "blank" })
208
+ .end((err, res) => {
209
+
210
+ if (err) { console.error("err: ", err); }
211
+ if (log) { console.log("res.body", res.body); }
212
+
213
+ res.should.have.status(200);
214
+ res.body.should.be.a('object');
215
+
216
+ let chatbot_id = res.body._id;
217
+ let webhook_intent_id = "3bfda939-ff76-4762-bbe0-fc0f0dc4c777"
218
+
219
+ chai.request(server)
220
+ .post('/' + savedProject._id + '/webhooks/')
221
+ .auth(email, pwd)
222
+ .send({ chatbot_id: chatbot_id, block_id: webhook_intent_id })
223
+ .end((err, res) => {
224
+
225
+ if (err) { console.error("err: ", err); }
226
+ if (log) { console.log("res.body", res.body); }
227
+
228
+ res.should.have.status(200);
229
+ res.body.should.be.a('object');
230
+
231
+ let old_webhook_id = res.body.webhook_id;
232
+
233
+ chai.request(server)
234
+ .put('/' + savedProject._id + '/webhooks/' + chatbot_id + "/regenerate")
235
+ .auth(email, pwd)
236
+ .send()
237
+ .end((err, res) => {
238
+
239
+ if (err) { console.error("err: ", err); }
240
+ if (log) { console.log("res.body", res.body); }
241
+
242
+ res.should.have.status(200);
243
+ res.body.should.be.a('object');
244
+ should.exist(res.body.webhook_id)
245
+ expect(res.body.webhook_id).to.have.length(32)
246
+ expect(res.body.webhook_id).to.not.equal(old_webhook_id);
247
+
248
+ done();
249
+
250
+ });
251
+ });
252
+
253
+ });
254
+ });
255
+ });
256
+ })
257
+
258
+ it('delete-webhook', (done) => {
259
+
260
+ var email = "test-signup-" + Date.now() + "@email.com";
261
+ var pwd = "pwd";
262
+
263
+ userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
264
+ projectService.create("test-webhook-create", savedUser._id).then(function (savedProject) {
265
+
266
+ chai.request(server)
267
+ .post('/' + savedProject._id + '/faq_kb')
268
+ .auth(email, pwd)
269
+ .send({ "name": "testbot", type: "tilebot", language: "en", template: "blank" })
270
+ .end((err, res) => {
271
+
272
+ if (err) { console.error("err: ", err); }
273
+ if (log) { console.log("res.body", res.body); }
274
+
275
+ res.should.have.status(200);
276
+ res.body.should.be.a('object');
277
+
278
+ let chatbot_id = res.body._id;
279
+ let webhook_intent_id = "3bfda939-ff76-4762-bbe0-fc0f0dc4c777"
280
+
281
+ chai.request(server)
282
+ .post('/' + savedProject._id + '/webhooks/')
283
+ .auth(email, pwd)
284
+ .send({ chatbot_id: chatbot_id, block_id: webhook_intent_id })
285
+ .end((err, res) => {
286
+
287
+ if (err) { console.error("err: ", err); }
288
+ if (log) { console.log("res.body", res.body); }
289
+
290
+ res.should.have.status(200);
291
+ res.body.should.be.a('object');
292
+
293
+ chai.request(server)
294
+ .delete('/' + savedProject._id + '/webhooks/' + chatbot_id)
295
+ .auth(email, pwd)
296
+ .end((err, res) => {
297
+
298
+ if (err) { console.error("err: ", err); }
299
+ if (log) { console.log("res.body", res.body); }
300
+
301
+ res.should.have.status(200);
302
+ res.body.should.be.a('object');
303
+ expect(res.body.success).to.equal(true);
304
+ expect(res.body.message).to.equal("Webhook for chatbot " + chatbot_id + " deleted successfully")
305
+
306
+ done();
307
+
308
+ });
309
+ });
310
+
311
+ });
312
+ });
313
+ });
314
+ })
315
+
316
+ it('run-webhook', (done) => {
317
+
318
+ var email = "test-signup-" + Date.now() + "@email.com";
319
+ var pwd = "pwd";
320
+
321
+ userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
322
+ projectService.create("test-webhook-create", savedUser._id).then(function (savedProject) {
323
+
324
+ chai.request(server)
325
+ .post('/' + savedProject._id + '/faq_kb')
326
+ .auth(email, pwd)
327
+ .send({ "name": "testbot", type: "tilebot", language: "en", template: "blank" })
328
+ .end((err, res) => {
329
+
330
+ if (err) { console.error("err: ", err); }
331
+ if (log) { console.log("res.body", res.body); }
332
+
333
+ res.should.have.status(200);
334
+ res.body.should.be.a('object');
335
+
336
+ let chatbot_id = res.body._id;
337
+ let webhook_intent_id = "3bfda939-ff76-4762-bbe0-fc0f0dc4c777"
338
+
339
+ chai.request(server)
340
+ .post('/' + savedProject._id + '/webhooks/')
341
+ .auth(email, pwd)
342
+ .send({ chatbot_id: chatbot_id, block_id: webhook_intent_id })
343
+ .end((err, res) => {
344
+
345
+ if (err) { console.error("err: ", err); }
346
+ if (log) { console.log("res.body", res.body); }
347
+
348
+ res.should.have.status(200);
349
+ res.body.should.be.a('object');
350
+
351
+ let webhook_id = res.body.webhook_id;
352
+
353
+ chai.request(server)
354
+ .post('/webhook/' + webhook_id)
355
+ .auth(email, pwd)
356
+ .end((err, res) => {
357
+
358
+ if (err) { console.error("err: ", err); }
359
+ if (log) { console.log("res.body", res.body); }
360
+
361
+ console.log("res.body", res.body);
362
+ res.should.have.status(200);
363
+ res.body.should.be.a('object');
364
+ expect(res.body.success).to.equal(true);
365
+ expect(res.body.message).to.equal("Webhook disabled in test mode");
366
+
367
+ done();
368
+
369
+ });
370
+ });
371
+
372
+ });
373
+ });
374
+ });
375
+ })
376
+ });
package/test-int/bot.js CHANGED
@@ -25,6 +25,7 @@ var express = require('express');
25
25
  const bodyParser = require('body-parser');
26
26
 
27
27
  var leadService = require('../services/leadService');
28
+ const chatbotTypes = require('../models/chatbotTypes');
28
29
 
29
30
  // var http = require('http');
30
31
  // const { parse } = require('querystring');
@@ -1317,7 +1318,7 @@ describe('bot', () => {
1317
1318
  userService.signup( email ,pwd, "Test Firstname", "Test lastname").then(function(savedUser) {
1318
1319
  projectService.create("test-bot", savedUser._id).then(function(savedProject) {
1319
1320
  // create(name, url, projectid, user_id, type)
1320
- faqService.create("testbot", undefined, savedProject._id, savedUser._id, "internal", undefined, "http://localhost:3019/", true).then(function(savedBot) {
1321
+ faqService.create("testbot", undefined, savedProject._id, savedUser._id, "internal", undefined, undefined, "http://localhost:3019/", true).then(function(savedBot) {
1321
1322
 
1322
1323
  var newFaq = new Faq({
1323
1324
  id_faq_kb: savedBot._id,
@@ -1429,7 +1430,7 @@ describe('bot', () => {
1429
1430
  userService.signup( email ,pwd, "Test Firstname", "Test lastname").then(function(savedUser) {
1430
1431
  projectService.create("test-bot", savedUser._id).then(function(savedProject) {
1431
1432
  // create(name, url, projectid, user_id, type)
1432
- faqService.create("testbot", undefined, savedProject._id, savedUser._id, "internal", undefined, "http://localhost:3029/", true).then(function(savedBot) {
1433
+ faqService.create("testbot", undefined, savedProject._id, savedUser._id, "internal", undefined, undefined, "http://localhost:3029/", true).then(function(savedBot) {
1433
1434
 
1434
1435
  Faq.findOneAndUpdate({id_project:savedProject._id,id_faq_kb:savedBot._id, question: "defaultFallback" }, {webhook_enabled: true},{new: true, upsert:false}, function (err, savedFaq) {
1435
1436
  console.log("savedFaq",savedFaq);
@@ -1534,7 +1535,7 @@ describe('bot', () => {
1534
1535
  userService.signup( email ,pwd, "Test Firstname", "Test lastname").then(function(savedUser) {
1535
1536
  projectService.create("test-bot", savedUser._id).then(function(savedProject) {
1536
1537
  // create(name, url, projectid, user_id, type)
1537
- faqService.create("testbot", undefined, savedProject._id, savedUser._id, "internal", undefined, "http://localhost:3028/", true).then(function(savedBot) {
1538
+ faqService.create("testbot", undefined, savedProject._id, savedUser._id, "internal", undefined, undefined, "http://localhost:3028/", true).then(function(savedBot) {
1538
1539
 
1539
1540
  Faq.findOneAndUpdate({id_project:savedProject._id,id_faq_kb:savedBot._id, question: "defaultFallback" }, {webhook_enabled: true},{new: true, upsert:false}, function (err, savedFaq) {
1540
1541
  console.log("savedFaq",savedFaq);
@@ -1666,7 +1667,7 @@ describe('bot', () => {
1666
1667
  userService.signup( email ,pwd, "Test Firstname", "Test lastname").then(function(savedUser) {
1667
1668
  projectService.create("test-bot", savedUser._id).then(function(savedProject) {
1668
1669
  // create(name, url, projectid, user_id, type)
1669
- faqService.create("testbot", undefined, savedProject._id, savedUser._id, "internal", undefined, "http://localhost:3030/", true).then(function(savedBot) {
1670
+ faqService.create("testbot", undefined, savedProject._id, savedUser._id, "internal", undefined, undefined, "http://localhost:3030/", true).then(function(savedBot) {
1670
1671
 
1671
1672
  Faq.findOneAndUpdate({id_project:savedProject._id,id_faq_kb:savedBot._id, question: "defaultFallback" }, {webhook_enabled: true},{new: true, upsert:false}, function (err, savedFaq) {
1672
1673
  console.log("savedFaq",savedFaq);
@@ -1802,7 +1803,7 @@ describe('bot', () => {
1802
1803
  userService.signup( email ,pwd, "Test Firstname", "Test lastname").then(function(savedUser) {
1803
1804
  projectService.create("test-bot", savedUser._id).then(function(savedProject) {
1804
1805
  // create(name, url, projectid, user_id, type)
1805
- faqService.create("testbot", null, savedProject._id, savedUser._id, "internal", undefined, undefined, true).then(function(savedBot) {
1806
+ faqService.create("testbot", null, savedProject._id, savedUser._id, "internal", undefined, undefined, undefined, true).then(function(savedBot) {
1806
1807
 
1807
1808
  var newFaq = new Faq({
1808
1809
  id_faq_kb: savedBot._id,
package/utils/httpUtil.js CHANGED
@@ -2,29 +2,61 @@
2
2
  'use strict';
3
3
 
4
4
  var request = require('retry-request', {
5
- request: require('request')
6
- });
5
+ request: require('request')
6
+ });
7
+ const axios = require("axios").default;
8
+
9
+ let defaultHeaders = {
10
+ 'Content-Type': 'application/json'
11
+ }
7
12
 
8
13
 
9
14
  class HttpUtil {
10
15
 
11
- call(url, headers, json, method) {
12
- return new Promise(function (resolve, reject) {
13
- request({
14
- url: url,
15
- headers: headers,
16
- json: json,
17
- method: method
18
- }, function(err, result, json){
19
- //console.log("SENT notify for bot with url " + url + " with err " + err);
20
- if (err) {
21
- //console.log("Error sending notify for bot with url " + url + " with err " + err);
22
- return reject(err);
23
- }
24
- return resolve(json);
25
- });
26
- });
27
- }
16
+ call(url, headers, json, method) {
17
+ return new Promise(function (resolve, reject) {
18
+ request({
19
+ url: url,
20
+ headers: headers,
21
+ json: json,
22
+ method: method
23
+ }, function (err, result, json) {
24
+ //console.log("SENT notify for bot with url " + url + " with err " + err);
25
+ if (err) {
26
+ //console.log("Error sending notify for bot with url " + url + " with err " + err);
27
+ return reject(err);
28
+ }
29
+ return resolve(json);
30
+ });
31
+ });
32
+ }
33
+
34
+ async post(url, payload, customHeaders, auth) {
35
+
36
+ return new Promise((resolve, reject) => {
37
+
38
+ let headers = customHeaders ? customHeaders : defaultHeaders;
39
+ if (auth) headers.Authorization = auth;
40
+
41
+ axios({
42
+ url: url,
43
+ headers: {
44
+ ...headers
45
+ },
46
+ data: payload,
47
+ method: 'POST'
48
+ }).then((resbody) => {
49
+ resolve(resbody);
50
+ }).catch((err) => {
51
+ if (err.response) {
52
+ reject(err.response);
53
+ } else {
54
+ reject(err);
55
+ }
56
+ })
57
+
58
+ })
59
+ }
28
60
  }
29
61
 
30
62