@tiledesk/tiledesk-server 2.7.25 → 2.7.27

Sign up to get free protection for your applications and to get access to all the features.
package/test/kbRoute.js CHANGED
@@ -5,6 +5,7 @@ process.env.KB_WEBHOOK_TOKEN = "testtoken"
5
5
 
6
6
  var userService = require('../services/userService');
7
7
  var projectService = require('../services/projectService');
8
+ var faqService = require('../services/faqService');
8
9
 
9
10
  let log = false;
10
11
 
@@ -18,6 +19,9 @@ let should = chai.should();
18
19
  var fs = require('fs');
19
20
  const path = require('path');
20
21
  const mongoose = require('mongoose');
22
+ const nock = require('nock');
23
+ const faq = require('../models/faq');
24
+
21
25
 
22
26
  // chai.config.includeStack = true;
23
27
 
@@ -32,7 +36,9 @@ describe('KbRoute', () => {
32
36
 
33
37
  describe('/create', () => {
34
38
 
35
- it('createNewKb', (done) => {
39
+
40
+ // NEW TESTS
41
+ it('create-new-kb', (done) => {
36
42
 
37
43
  var email = "test-signup-" + Date.now() + "@email.com";
38
44
  var pwd = "pwd";
@@ -40,77 +46,169 @@ describe('KbRoute', () => {
40
46
  userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
41
47
  projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
42
48
 
43
- let kb = {
44
- name: "example_name5",
45
- type: "url",
46
- source: "https://www.exampleurl5.com",
47
- content: "",
48
- }
49
-
50
49
  chai.request(server)
51
- .post('/' + savedProject._id + '/kb')
50
+ .get('/' + savedProject._id + '/kb/namespace/all')
52
51
  .auth(email, pwd)
53
- .send(kb) // can be empty
54
52
  .end((err, res) => {
55
-
53
+
56
54
  if (err) { console.error("err: ", err); }
57
- if (log) { console.log("create kb res.body: ", res.body); }
58
-
55
+ if (log) { console.log("get namespaces res.body: ", res.body); }
56
+
59
57
  res.should.have.status(200);
60
- res.body.should.be.a('object');
58
+ expect(res.body.length).to.equal(1);
61
59
 
62
- done();
63
-
60
+ let namespace_id = res.body[0].id;
61
+ console.log("namespace_id: ", namespace_id);
64
62
 
65
- })
63
+ let kb = {
64
+ name: "example_name5",
65
+ type: "url",
66
+ source: "https://www.exampleurl5.com",
67
+ content: "",
68
+ namespace: namespace_id
69
+ }
70
+
71
+ chai.request(server)
72
+ .post('/' + savedProject._id + '/kb')
73
+ .auth(email, pwd)
74
+ .send(kb) // can be empty
75
+ .end((err, res) => {
76
+
77
+ if (err) { console.error("err: ", err); }
78
+ if (log) { console.log("create kb res.body: ", res.body); }
79
+
80
+ res.should.have.status(200);
81
+ res.body.should.be.a('object');
82
+ expect(res.body.value.id_project).to.equal(res.body.value.namespace)
83
+ expect(res.body.value.name).to.equal("example_name5")
84
+ expect(res.body.value.type).to.equal("url")
85
+ expect(res.body.value.source).to.equal("https://www.exampleurl5.com")
86
+ expect(res.body.value.status).to.equal(-1)
66
87
 
88
+ done();
89
+ })
90
+ })
67
91
  });
68
92
  });
69
93
 
70
- }).timeout(10000);
94
+ })
71
95
 
72
- // logic in standby
73
- // it('createNewKb-namespaceNotBelongsProject', (done) => {
96
+ it('get-with-queries', (done) => {
74
97
 
75
- // var email = "test-signup-" + Date.now() + "@email.com";
76
- // var pwd = "pwd";
98
+ var email = "test-signup-" + Date.now() + "@email.com";
99
+ var pwd = "pwd";
77
100
 
78
- // userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
79
- // projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
101
+ userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
102
+ projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
80
103
 
81
- // let kb = {
82
- // name: "example_name5",
83
- // type: "url",
84
- // source: "https://www.exampleurl5.com",
85
- // content: "",
86
- // namespace: "fakenamespace"
87
- // }
104
+ /**
105
+ * Get all namespace. If no namespace exists, a default namespace is created and returned
106
+ */
107
+ chai.request(server)
108
+ .get('/' + savedProject._id + '/kb/namespace/all')
109
+ .auth(email, pwd)
110
+ .end((err, res) => {
88
111
 
89
- // chai.request(server)
90
- // .post('/' + savedProject._id + '/kb')
91
- // .auth(email, pwd)
92
- // .send(kb) // can be empty
93
- // .end((err, res) => {
94
-
95
- // if (err) { console.error("err: ", err); }
96
- // if (log) { console.log("create kb res.body: ", res.body); }
97
-
98
- // res.should.have.status(403);
99
- // res.body.should.be.a('object');
100
- // expect(res.body.success).to.equal(false);
101
- // expect(res.body.error).to.equal("Not allowed. The namespace does not belong to the current project.");
112
+ if (err) { console.error("err: ", err); }
113
+ if (log) { console.log("get namespaces res.body: ", res.body); }
102
114
 
103
- // done();
104
-
115
+ res.should.have.status(200);
116
+ expect(res.body[0].name === 'Default');
117
+ expect(res.body[0].id === savedProject._id);
118
+
119
+ let namespace_id = res.body[0].id;
120
+
121
+ let kb1 = {
122
+ name: "example_name1",
123
+ type: "url",
124
+ namespace: namespace_id,
125
+ source: "https://www.exampleurl1.com",
126
+ content: ""
127
+ }
128
+
129
+ let kb2 = {
130
+ name: "example_name2",
131
+ type: "text",
132
+ namespace: namespace_id,
133
+ source: "example_name2",
134
+ content: "example content"
135
+ }
136
+
137
+ let kb3 = {
138
+ name: "example_name3",
139
+ type: "url",
140
+ namespace: namespace_id,
141
+ source: "https://www.exampleurl3.com",
142
+ content: ""
143
+ }
144
+
145
+
146
+ /**
147
+ * Add contents to default namespace
148
+ */
149
+ chai.request(server)
150
+ .post('/' + savedProject._id + "/kb")
151
+ .auth(email, pwd)
152
+ .send(kb1)
153
+ .end((err, res) => {
154
+ if (log) { console.log("create kb1 res.body: ", res.body); }
155
+ res.should.have.status(200);
105
156
 
106
- // })
157
+ setTimeout(() => {
158
+ chai.request(server)
159
+ .post('/' + savedProject._id + "/kb")
160
+ .auth(email, pwd)
161
+ .send(kb2)
162
+ .end((err, res) => {
163
+ if (log) { console.log("create kb2 res.body: ", res.body); }
164
+ res.should.have.status(200);
107
165
 
108
- // });
109
- // });
166
+ setTimeout(() => {
167
+ chai.request(server)
168
+ .post('/' + savedProject._id + "/kb")
169
+ .auth(email, pwd)
170
+ .send(kb3)
171
+ .end((err, res) => {
172
+ if (log) { console.log("create kb3 res.body: ", res.body); }
173
+ res.should.have.status(200);
110
174
 
111
- //}).timeout(10000);
175
+ let query = "?status=100&type=url&limit=5&page=0&direction=-1&sortField=updatedAt&search=example&namespace=" + namespace_id;
176
+ //let query = "";
177
+ console.log("query: ", query);
178
+
179
+ chai.request(server)
180
+ .get('/' + savedProject._id + "/kb" + query)
181
+ .auth(email, pwd)
182
+ .end((err, res) => {
183
+ if (log) { console.log("getall res.body: ", res.body); }
184
+ res.should.have.status(200);
185
+ res.body.should.be.a('object');
186
+ res.body.kbs.should.be.a('array');
187
+ expect(res.body.kbs.length).to.equal(2);
188
+ expect(res.body.count).to.equal(2);
189
+ res.body.query.should.be.a('object');
190
+ expect(res.body.query.status).to.equal(100);
191
+ expect(res.body.query.limit).to.equal(5);
192
+ expect(res.body.query.page).to.equal(0);
193
+ expect(res.body.query.direction).to.equal(-1);
194
+ expect(res.body.query.sortField).to.equal("updatedAt");
195
+ expect(res.body.query.search).to.equal("example");
196
+
197
+ done();
198
+
199
+ })
200
+
201
+ })
202
+ }, 1000)
203
+ })
204
+ }, 1000)
205
+ })
206
+ })
207
+ })
208
+ })
209
+ }).timeout(20000)
112
210
 
113
- it('createNewKb-replaceNamespace', (done) => {
211
+ it('get-with-queries-namespace-not-belong-project', (done) => {
114
212
 
115
213
  var email = "test-signup-" + Date.now() + "@email.com";
116
214
  var pwd = "pwd";
@@ -118,39 +216,71 @@ describe('KbRoute', () => {
118
216
  userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
119
217
  projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
120
218
 
121
- let kb = {
122
- name: "example_name5",
123
- type: "url",
124
- source: "https://www.exampleurl5.com",
125
- content: "",
126
- namespace: "fakenamespace"
127
- }
128
-
219
+ /**
220
+ * Get all namespace. If no namespace exists, a default namespace is created and returned
221
+ */
129
222
  chai.request(server)
130
- .post('/' + savedProject._id + '/kb')
223
+ .get('/' + savedProject._id + '/kb/namespace/all')
131
224
  .auth(email, pwd)
132
- .send(kb) // can be empty
133
225
  .end((err, res) => {
134
-
226
+
135
227
  if (err) { console.error("err: ", err); }
136
- if (log) { console.log("create kb res.body: ", res.body); }
137
-
228
+ if (log) { console.log("get namespaces res.body: ", res.body); }
229
+
138
230
  res.should.have.status(200);
139
- res.body.should.be.a('object');
140
- expect(res.body.value.namespace).not.equal("fakenamespace");
141
- expect(res.body.value.namespace).to.equal(savedProject._id.toString());
231
+ expect(res.body[0].name === 'Default');
232
+ expect(res.body[0].id === savedProject._id);
233
+
234
+ let namespace_id = res.body[0].id;
235
+
236
+ let kb1 = {
237
+ name: "example_name1",
238
+ type: "url",
239
+ namespace: namespace_id,
240
+ source: "https://www.exampleurl1.com",
241
+ content: ""
242
+ }
243
+
244
+ /**
245
+ * Add contents to default namespace
246
+ */
247
+ chai.request(server)
248
+ .post('/' + savedProject._id + "/kb")
249
+ .auth(email, pwd)
250
+ .send(kb1)
251
+ .end((err, res) => {
252
+ if (log) { console.log("create kb1 res.body: ", res.body); }
253
+ res.should.have.status(200);
142
254
 
143
- done();
144
-
255
+ let namespace_id = "fakenamespaceid";
145
256
 
146
- })
257
+ let query = "?status=100&type=url&limit=5&page=0&direction=-1&sortField=updatedAt&search=example&namespace=" + namespace_id;
258
+ console.log("query: ", query);
147
259
 
148
- });
149
- });
260
+ chai.request(server)
261
+ .get('/' + savedProject._id + "/kb" + query)
262
+ .auth(email, pwd)
263
+ .end((err, res) => {
264
+ if (log) { console.log("getall res.body: ", res.body); }
265
+ res.should.have.status(200);
266
+ res.body.should.be.a('object');
267
+ res.body.kbs.should.be.a('array');
268
+ expect(res.body.kbs.length).to.equal(0);
269
+ expect(res.body.count).to.equal(0);
150
270
 
151
- }).timeout(10000);
271
+ done();
272
+
273
+ })
274
+ })
275
+ })
276
+ })
277
+ })
278
+ }).timeout(20000)
152
279
 
153
- it('getWithQueries', (done) => {
280
+ /**
281
+ * If you try to add content to a project that has no namespace, it returns 403 forbidden.
282
+ */
283
+ it('add-multiple-urls-no-namespaces', (done) => {
154
284
 
155
285
  var email = "test-signup-" + Date.now() + "@email.com";
156
286
  var pwd = "pwd";
@@ -158,92 +288,35 @@ describe('KbRoute', () => {
158
288
  userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
159
289
  projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
160
290
 
161
- let kb1 = {
162
- name: "example_name1",
163
- type: "url",
164
- source: "https://www.exampleurl1.com",
165
- content: ""
166
- }
167
-
168
- let kb2 = {
169
- name: "example_name2",
170
- type: "text",
171
- source: "example_name2",
172
- content: "example content"
173
- }
174
-
175
- let kb3 = {
176
- name: "example_name3",
177
- type: "url",
178
- source: "https://www.exampleurl3.com",
179
- content: ""
180
- }
181
-
182
291
  chai.request(server)
183
- .post('/' + savedProject._id + "/kb")
292
+ .post('/' + savedProject._id + '/kb/multi?namespace=123456')
184
293
  .auth(email, pwd)
185
- .send(kb1)
294
+ .set('Content-Type', 'text/plain')
295
+ .attach('uploadFile', fs.readFileSync(path.resolve(__dirname, './kbUrlsList.txt')), 'kbUrlsList.txt')
186
296
  .end((err, res) => {
187
- if (log) { console.log("create kb res.body: ", res.body); }
188
- res.should.have.status(200);
189
-
190
- setTimeout(() => {
191
- chai.request(server)
192
- .post('/' + savedProject._id + "/kb")
193
- .auth(email, pwd)
194
- .send(kb2)
195
- .end((err, res) => {
196
- if (log) { console.log("create kb res.body: ", res.body); }
197
- res.should.have.status(200);
198
-
199
- setTimeout(() => {
200
- chai.request(server)
201
- .post('/' + savedProject._id + "/kb")
202
- .auth(email, pwd)
203
- .send(kb3)
204
- .end((err, res) => {
205
- if (log) { console.log("create kb res.body: ", res.body); }
206
- res.should.have.status(200);
207
-
208
- let query = "?status=-1&type=url&limit=5&page=0&direction=-1&sortField=updatedAt&search=example";
209
- //let query = "";
210
- console.log("query: ", query);
211
-
212
- chai.request(server)
213
- .get('/' + savedProject._id + "/kb" + query)
214
- .auth(email, pwd)
215
- .end((err, res) => {
216
- if (log) { console.log("getall res.body: ", res.body); }
217
- console.log("getall res.body: ", res.body);
218
- res.should.have.status(200);
219
- res.body.should.be.a('object');
220
- res.body.kbs.should.be.a('array');
221
- expect(res.body.kbs.length).to.equal(2);
222
- expect(res.body.count).to.equal(2);
223
- res.body.query.should.be.a('object');
224
- expect(res.body.query.status).to.equal(-1);
225
- expect(res.body.query.limit).to.equal(5);
226
- expect(res.body.query.page).to.equal(0);
227
- expect(res.body.query.direction).to.equal(-1);
228
- expect(res.body.query.sortField).to.equal("updatedAt");
229
- expect(res.body.query.search).to.equal("example");
230
-
231
- done();
232
297
 
233
- })
298
+ if (err) { console.error("err: ", err); }
299
+ if (log) { console.log("res.body: ", res.body) }
234
300
 
235
- })
236
- }, 1000)
237
- })
238
- }, 1000)
301
+ res.should.have.status(403);
302
+ res.should.be.a('object')
303
+ expect(res.body.success).to.equal(false);
304
+ let error_response = "No namespace found for the selected project " + savedProject._id + ". Cannot add content to a non-existent namespace."
305
+ expect(res.body.error).to.equal(error_response);
239
306
 
307
+ done();
240
308
 
241
309
  })
242
- })
243
- })
244
- }).timeout(20000)
310
+ });
311
+ });
312
+
313
+ }).timeout(10000)
245
314
 
246
- it('scrapeSingle', (done) => {
315
+ /**
316
+ * If you try to add content to a namespace that does not belong to the selected project and
317
+ * the project has at least one namesapce, it returns 403 forbidden.
318
+ */
319
+ it('add-multiple-urls-namespace-not-belong-project', (done) => {
247
320
 
248
321
  var email = "test-signup-" + Date.now() + "@email.com";
249
322
  var pwd = "pwd";
@@ -251,107 +324,86 @@ describe('KbRoute', () => {
251
324
  userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
252
325
  projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
253
326
 
254
- let kb = {
255
- name: "example_name6",
256
- type: "url",
257
- source: "https://www.exampleurl6.com",
258
- content: ""
259
- }
260
-
261
327
  chai.request(server)
262
- .post('/' + savedProject._id + '/kb')
328
+ .get('/' + savedProject._id + '/kb/namespace/all')
263
329
  .auth(email, pwd)
264
- .send(kb) // can be empty
265
330
  .end((err, res) => {
266
- if (log) { console.log("create kb res.body: ", res.body); }
267
- res.should.have.status(200);
268
331
 
269
- let kbid = res.body.value._id;
270
- console.log("kbid: ", kbid)
332
+ if (err) { console.error("err: ", err); }
333
+ if (log) { console.log("res.body: ", res.body) }
334
+
335
+ res.should.have.status(200)
336
+ expect(res.body.length).to.equal(1);
337
+
271
338
  chai.request(server)
272
- .post('/' + savedProject._id + "/kb/scrape/single")
339
+ .post('/' + savedProject._id + '/kb/multi?namespace=fakenamespaceid')
273
340
  .auth(email, pwd)
274
- .send({ id: kbid })
341
+ .set('Content-Type', 'text/plain')
342
+ .attach('uploadFile', fs.readFileSync(path.resolve(__dirname, './kbUrlsList.txt')), 'kbUrlsList.txt')
275
343
  .end((err, res) => {
276
- if (log) { console.log("single scrape res.body: ", res.body); }
277
- //res.should.have.status(200);
278
- // res.body.should.be.a('object');
279
- // expect(res.body.id_project).to.equal(savedProject._id.toString())
280
- // expect(res.body.maxKbsNumber).to.equal(3);
281
- // expect(res.body.maxPagesNumber).to.equal(1000);
282
- // expect(res.body.kbs).is.an('array').that.is.empty;
283
- done();
284
-
285
- })
286
-
287
-
288
- // res.body.should.be.a('object');
289
- // expect(res.body.id_project).to.equal(savedProject._id.toString());
290
344
 
345
+ if (err) { console.error("err: ", err); }
346
+ if (log) { console.log("res.body: ", res.body) }
291
347
 
348
+ res.should.have.status(403);
349
+ res.should.be.a('object');
350
+ expect(res.body.success).to.equal(false);
351
+ let error_response = "Not allowed. The namespace does not belong to the current project."
352
+ expect(res.body.error).to.equal(error_response);
292
353
 
354
+ done();
293
355
 
356
+ })
294
357
  })
295
-
296
358
  });
297
359
  });
298
360
 
299
- });
361
+ }).timeout(10000)
300
362
 
301
- // it('scrapeSingle-namespaceNotBelongsProject', (done) => {
363
+ it('add-multiple-urls-success', (done) => {
302
364
 
303
- // var email = "test-signup-" + Date.now() + "@email.com";
304
- // var pwd = "pwd";
365
+ var email = "test-signup-" + Date.now() + "@email.com";
366
+ var pwd = "pwd";
305
367
 
306
- // userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
307
- // projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
368
+ userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
369
+ projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
308
370
 
309
- // let kb = {
310
- // name: "example_name6",
311
- // type: "url",
312
- // source: "https://www.exampleurl6.com",
313
- // content: ""
314
- // }
371
+ chai.request(server)
372
+ .get('/' + savedProject._id + '/kb/namespace/all')
373
+ .auth(email, pwd)
374
+ .end((err, res) => {
315
375
 
316
- // chai.request(server)
317
- // .post('/' + savedProject._id + '/kb')
318
- // .auth(email, pwd)
319
- // .send(kb) // can be empty
320
- // .end((err, res) => {
321
- // if (log) { console.log("create kb res.body: ", res.body); }
322
- // res.should.have.status(200);
376
+ if (err) { console.error("err: ", err); }
377
+ if (log) { console.log("res.body: ", res.body) }
323
378
 
324
- // let kbid = res.body.value._id;
325
- // console.log("kbid: ", kbid)
326
- // chai.request(server)
327
- // .post('/' + savedProject._id + "/kb/scrape/single")
328
- // .auth(email, pwd)
329
- // .send({ id: kbid })
330
- // .end((err, res) => {
331
- // if (log) { console.log("single scrape res.body: ", res.body); }
332
- // //res.should.have.status(200);
333
- // // res.body.should.be.a('object');
334
- // // expect(res.body.id_project).to.equal(savedProject._id.toString())
335
- // // expect(res.body.maxKbsNumber).to.equal(3);
336
- // // expect(res.body.maxPagesNumber).to.equal(1000);
337
- // // expect(res.body.kbs).is.an('array').that.is.empty;
338
- // done();
379
+ res.should.have.status(200)
380
+ expect(res.body.length).to.equal(1);
339
381
 
340
- // })
382
+ let namespace_id = res.body[0].id;
341
383
 
384
+ chai.request(server)
385
+ .post('/' + savedProject._id + '/kb/multi?namespace=' + namespace_id)
386
+ .auth(email, pwd)
387
+ .set('Content-Type', 'text/plain')
388
+ .attach('uploadFile', fs.readFileSync(path.resolve(__dirname, './kbUrlsList.txt')), 'kbUrlsList.txt')
389
+ .end((err, res) => {
342
390
 
343
- // // res.body.should.be.a('object');
344
- // // expect(res.body.id_project).to.equal(savedProject._id.toString());
391
+ if (err) { console.error("err: ", err); }
392
+ if (log) { console.log("res.body: ", res.body) }
345
393
 
394
+ res.should.have.status(200);
395
+ expect(res.body.length).to.equal(4)
346
396
 
397
+ done();
347
398
 
399
+ })
400
+ })
401
+ });
402
+ });
348
403
 
349
- // })
350
- // });
351
- // });
352
- // });
404
+ }).timeout(10000)
353
405
 
354
- it('multiadd', (done) => {
406
+ it('expand-sitemap', (done) => {
355
407
 
356
408
  var email = "test-signup-" + Date.now() + "@email.com";
357
409
  var pwd = "pwd";
@@ -360,34 +412,20 @@ describe('KbRoute', () => {
360
412
  projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
361
413
 
362
414
  chai.request(server)
363
- .post('/' + savedProject._id + '/kb/multi')
415
+ .post('/' + savedProject._id + '/kb/sitemap')
364
416
  .auth(email, pwd)
365
- .set('Content-Type', 'text/plain')
366
- .attach('uploadFile', fs.readFileSync(path.resolve(__dirname, './kbUrlsList.txt')), 'kbUrlsList.txt')
417
+ // .send({ sitemap: "https://www.wired.it/sitemap.xml" })
418
+ .send({ sitemap: "https://gethelp.tiledesk.com/sitemap.xml" })
367
419
  .end((err, res) => {
368
420
 
369
- // console.log("res.body: ", res.body)
370
- res.should.have.status(200);
371
- expect(res.body.length).to.equal(4)
372
-
373
- done();
374
-
375
- // setTimeout(() => {
376
-
377
- // chai.request(server)
378
- // .post('/' + savedProject._id + '/kb/multi')
379
- // .auth(email, pwd)
380
- // .set('Content-Type', 'text/plain')
381
- // .attach('uploadFile', fs.readFileSync(path.resolve(__dirname, './kbUrlsList.txt')), 'kbUrlsList.txt')
382
- // .end((err, res) => {
421
+ if (err) { console.log("error: ", err) };
422
+ if (log) { console.log("res.body: ", res.body) }
383
423
 
384
- // // console.log("res.body: ", res.body);
385
- // res.should.have.status(200);
386
- // expect(res.body.length).to.equal(4)
424
+ res.should.have.status(200);
425
+ res.body.should.be.a('object');
426
+ res.body.sites.should.be.a('array');
387
427
 
388
- // done()
389
- // })
390
- // }, 2000)
428
+ done();
391
429
 
392
430
  })
393
431
 
@@ -396,7 +434,7 @@ describe('KbRoute', () => {
396
434
 
397
435
  }).timeout(10000)
398
436
 
399
- it('multiaddFail', (done) => {
437
+ it('scrape-single', (done) => {
400
438
 
401
439
  var email = "test-signup-" + Date.now() + "@email.com";
402
440
  var pwd = "pwd";
@@ -405,41 +443,66 @@ describe('KbRoute', () => {
405
443
  projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
406
444
 
407
445
  chai.request(server)
408
- .post('/' + savedProject._id + '/kb/multi')
446
+ .get('/' + savedProject._id + '/kb/namespace/all')
409
447
  .auth(email, pwd)
410
- .set('Content-Type', 'text/plain')
411
- .attach('uploadFile', fs.readFileSync(path.resolve(__dirname, './kbUrlsList.txt')), 'kbUrlsList.txt')
412
448
  .end((err, res) => {
413
449
 
414
- // console.log("res.body: ", res.body)
415
- res.should.have.status(200);
416
- expect(res.body.length).to.equal(4)
450
+ if (err) { console.error("err: ", err); }
451
+ if (log) { console.log("res.body: ", res.body) }
417
452
 
418
- setTimeout(() => {
453
+ res.should.have.status(200)
454
+ expect(res.body.length).to.equal(1);
419
455
 
420
- chai.request(server)
421
- .post('/' + savedProject._id + '/kb/multi')
422
- .auth(email, pwd)
423
- .set('Content-Type', 'text/plain')
424
- .attach('uploadFile', fs.readFileSync(path.resolve(__dirname, './kbUrlsList.txt')), 'kbUrlsList.txt')
425
- .end((err, res) => {
456
+ let namespace_id = res.body[0].id;
426
457
 
427
- // console.log("res.body: ", res.body);
428
- res.should.have.status(200);
429
- expect(res.body.length).to.equal(4)
458
+ let kb = {
459
+ name: "https://www.exampleurl6.com",
460
+ type: "url",
461
+ source: "https://www.exampleurl6.com",
462
+ content: "",
463
+ namespace: namespace_id
464
+ }
430
465
 
431
- done()
432
- })
433
- }, 2000)
466
+ chai.request(server)
467
+ .post('/' + savedProject._id + '/kb')
468
+ .auth(email, pwd)
469
+ .send(kb) // can be empty
470
+ .end((err, res) => {
471
+ if (log) { console.log("create kb res.body: ", res.body); }
472
+ res.should.have.status(200);
434
473
 
435
- })
474
+ let kbid = res.body.value._id;
475
+
476
+ chai.request(server)
477
+ .post('/' + savedProject._id + "/kb/scrape/single")
478
+ .auth(email, pwd)
479
+ .send({ id: kbid })
480
+ .end((err, res) => {
481
+
482
+ if (log) { console.log("single scrape res.body: ", res.body); }
483
+
484
+ /**
485
+ * Unable to verify the response due to an external request
486
+ */
487
+
488
+ //res.should.have.status(200);
489
+ // res.body.should.be.a('object');
490
+ // expect(res.body.id_project).to.equal(savedProject._id.toString())
491
+ // expect(res.body.maxKbsNumber).to.equal(3);
492
+ // expect(res.body.maxPagesNumber).to.equal(1000);
493
+ // expect(res.body.kbs).is.an('array').that.is.empty;
494
+ done();
436
495
 
496
+ })
497
+ })
498
+ })
437
499
  });
438
500
  });
501
+ });
439
502
 
440
- }).timeout(10000)
441
503
 
442
- // it('tooManyUrls', (done) => {
504
+ // OLD TESTS
505
+ // it('createNewKb', (done) => {
443
506
 
444
507
  // var email = "test-signup-" + Date.now() + "@email.com";
445
508
  // var pwd = "pwd";
@@ -447,29 +510,435 @@ describe('KbRoute', () => {
447
510
  // userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
448
511
  // projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
449
512
 
513
+ // let kb = {
514
+ // name: "example_name5",
515
+ // type: "url",
516
+ // source: "https://www.exampleurl5.com",
517
+ // content: "",
518
+ // }
519
+
450
520
  // chai.request(server)
451
- // .post('/' + savedProject._id + '/kb/multi')
521
+ // .post('/' + savedProject._id + '/kb')
452
522
  // .auth(email, pwd)
453
- // .set('Content-Type', 'text/plain')
454
- // .attach('uploadFile', fs.readFileSync(path.resolve(__dirname, './TooManykbUrlsList.txt')), 'TooManykbUrlsList.txt')
523
+ // .send(kb) // can be empty
455
524
  // .end((err, res) => {
456
525
 
457
- // // console.log("res.body: ", res.body)
458
- // res.should.have.status(403);
459
- // expect(res.body.success).to.equal(false);
460
- // expect(res.body.error).to.equal("Too many urls. Can't index more than 300 urls at a time.");
526
+ // if (err) { console.error("err: ", err); }
527
+ // if (log) { console.log("create kb res.body: ", res.body); }
528
+
529
+ // res.should.have.status(200);
530
+ // res.body.should.be.a('object');
531
+
532
+ // done();
461
533
 
462
- // done()
463
534
 
464
535
  // })
465
536
 
466
537
  // });
467
538
  // });
468
539
 
469
- // })
540
+ // }).timeout(10000);
470
541
 
471
542
  // logic in standby
472
- // it('askkb-namespaceNotBelongsProject', (done) => {
543
+ // it('createNewKb-namespaceNotBelongsProject', (done) => {
544
+
545
+ // var email = "test-signup-" + Date.now() + "@email.com";
546
+ // var pwd = "pwd";
547
+
548
+ // userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
549
+ // projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
550
+
551
+ // let kb = {
552
+ // name: "example_name5",
553
+ // type: "url",
554
+ // source: "https://www.exampleurl5.com",
555
+ // content: "",
556
+ // namespace: "fakenamespace"
557
+ // }
558
+
559
+ // chai.request(server)
560
+ // .post('/' + savedProject._id + '/kb')
561
+ // .auth(email, pwd)
562
+ // .send(kb) // can be empty
563
+ // .end((err, res) => {
564
+
565
+ // if (err) { console.error("err: ", err); }
566
+ // if (log) { console.log("create kb res.body: ", res.body); }
567
+
568
+ // res.should.have.status(403);
569
+ // res.body.should.be.a('object');
570
+ // expect(res.body.success).to.equal(false);
571
+ // expect(res.body.error).to.equal("Not allowed. The namespace does not belong to the current project.");
572
+
573
+ // done();
574
+
575
+
576
+ // })
577
+
578
+ // });
579
+ // });
580
+
581
+ //}).timeout(10000);
582
+
583
+ // it('createNewKb-replaceNamespace', (done) => {
584
+
585
+ // var email = "test-signup-" + Date.now() + "@email.com";
586
+ // var pwd = "pwd";
587
+
588
+ // userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
589
+ // projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
590
+
591
+ // let kb = {
592
+ // name: "example_name5",
593
+ // type: "url",
594
+ // source: "https://www.exampleurl5.com",
595
+ // content: "",
596
+ // namespace: "fakenamespace"
597
+ // }
598
+
599
+ // chai.request(server)
600
+ // .post('/' + savedProject._id + '/kb')
601
+ // .auth(email, pwd)
602
+ // .send(kb) // can be empty
603
+ // .end((err, res) => {
604
+
605
+ // if (err) { console.error("err: ", err); }
606
+ // if (log) { console.log("create kb res.body: ", res.body); }
607
+
608
+ // res.should.have.status(200);
609
+ // res.body.should.be.a('object');
610
+ // expect(res.body.value.namespace).not.equal("fakenamespace");
611
+ // expect(res.body.value.namespace).to.equal(savedProject._id.toString());
612
+
613
+ // done();
614
+
615
+
616
+ // })
617
+
618
+ // });
619
+ // });
620
+
621
+ // }).timeout(10000);
622
+
623
+ // it('getWithQueries', (done) => {
624
+
625
+ // var email = "test-signup-" + Date.now() + "@email.com";
626
+ // var pwd = "pwd";
627
+
628
+ // userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
629
+ // projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
630
+
631
+ // let kb1 = {
632
+ // name: "example_name1",
633
+ // type: "url",
634
+ // source: "https://www.exampleurl1.com",
635
+ // content: ""
636
+ // }
637
+
638
+ // let kb2 = {
639
+ // name: "example_name2",
640
+ // type: "text",
641
+ // source: "example_name2",
642
+ // content: "example content"
643
+ // }
644
+
645
+ // let kb3 = {
646
+ // name: "example_name3",
647
+ // type: "url",
648
+ // source: "https://www.exampleurl3.com",
649
+ // content: ""
650
+ // }
651
+
652
+ // chai.request(server)
653
+ // .post('/' + savedProject._id + "/kb")
654
+ // .auth(email, pwd)
655
+ // .send(kb1)
656
+ // .end((err, res) => {
657
+ // if (log) { console.log("create kb res.body: ", res.body); }
658
+ // res.should.have.status(200);
659
+
660
+ // setTimeout(() => {
661
+ // chai.request(server)
662
+ // .post('/' + savedProject._id + "/kb")
663
+ // .auth(email, pwd)
664
+ // .send(kb2)
665
+ // .end((err, res) => {
666
+ // if (log) { console.log("create kb res.body: ", res.body); }
667
+ // res.should.have.status(200);
668
+
669
+ // setTimeout(() => {
670
+ // chai.request(server)
671
+ // .post('/' + savedProject._id + "/kb")
672
+ // .auth(email, pwd)
673
+ // .send(kb3)
674
+ // .end((err, res) => {
675
+ // if (log) { console.log("create kb res.body: ", res.body); }
676
+ // res.should.have.status(200);
677
+
678
+ // let query = "?status=100&type=url&limit=5&page=0&direction=-1&sortField=updatedAt&search=example";
679
+ // //let query = "";
680
+ // console.log("query: ", query);
681
+
682
+ // chai.request(server)
683
+ // .get('/' + savedProject._id + "/kb" + query)
684
+ // .auth(email, pwd)
685
+ // .end((err, res) => {
686
+ // if (log) { console.log("getall res.body: ", res.body); }
687
+ // res.should.have.status(200);
688
+ // res.body.should.be.a('object');
689
+ // res.body.kbs.should.be.a('array');
690
+ // expect(res.body.kbs.length).to.equal(2);
691
+ // expect(res.body.count).to.equal(2);
692
+ // res.body.query.should.be.a('object');
693
+ // expect(res.body.query.status).to.equal(100);
694
+ // expect(res.body.query.limit).to.equal(5);
695
+ // expect(res.body.query.page).to.equal(0);
696
+ // expect(res.body.query.direction).to.equal(-1);
697
+ // expect(res.body.query.sortField).to.equal("updatedAt");
698
+ // expect(res.body.query.search).to.equal("example");
699
+
700
+ // done();
701
+
702
+ // })
703
+
704
+ // })
705
+ // }, 1000)
706
+ // })
707
+ // }, 1000)
708
+
709
+
710
+ // })
711
+ // })
712
+ // })
713
+ // }).timeout(20000)
714
+
715
+ // it('scrapeSingle', (done) => {
716
+
717
+ // var email = "test-signup-" + Date.now() + "@email.com";
718
+ // var pwd = "pwd";
719
+
720
+ // userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
721
+ // projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
722
+
723
+ // let kb = {
724
+ // name: "example_name6",
725
+ // type: "url",
726
+ // source: "https://www.exampleurl6.com",
727
+ // content: ""
728
+ // }
729
+
730
+ // chai.request(server)
731
+ // .post('/' + savedProject._id + '/kb')
732
+ // .auth(email, pwd)
733
+ // .send(kb) // can be empty
734
+ // .end((err, res) => {
735
+ // if (log) { console.log("create kb res.body: ", res.body); }
736
+ // res.should.have.status(200);
737
+
738
+ // let kbid = res.body.value._id;
739
+ // console.log("kbid: ", kbid)
740
+ // chai.request(server)
741
+ // .post('/' + savedProject._id + "/kb/scrape/single")
742
+ // .auth(email, pwd)
743
+ // .send({ id: kbid })
744
+ // .end((err, res) => {
745
+ // if (log) { console.log("single scrape res.body: ", res.body); }
746
+ // //res.should.have.status(200);
747
+ // // res.body.should.be.a('object');
748
+ // // expect(res.body.id_project).to.equal(savedProject._id.toString())
749
+ // // expect(res.body.maxKbsNumber).to.equal(3);
750
+ // // expect(res.body.maxPagesNumber).to.equal(1000);
751
+ // // expect(res.body.kbs).is.an('array').that.is.empty;
752
+ // done();
753
+
754
+ // })
755
+
756
+
757
+ // // res.body.should.be.a('object');
758
+ // // expect(res.body.id_project).to.equal(savedProject._id.toString());
759
+
760
+
761
+
762
+
763
+ // })
764
+
765
+ // });
766
+ // });
767
+
768
+ // });
769
+
770
+ // it('scrapeSingle-namespaceNotBelongsProject', (done) => {
771
+
772
+ // var email = "test-signup-" + Date.now() + "@email.com";
773
+ // var pwd = "pwd";
774
+
775
+ // userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
776
+ // projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
777
+
778
+ // let kb = {
779
+ // name: "example_name6",
780
+ // type: "url",
781
+ // source: "https://www.exampleurl6.com",
782
+ // content: ""
783
+ // }
784
+
785
+ // chai.request(server)
786
+ // .post('/' + savedProject._id + '/kb')
787
+ // .auth(email, pwd)
788
+ // .send(kb) // can be empty
789
+ // .end((err, res) => {
790
+ // if (log) { console.log("create kb res.body: ", res.body); }
791
+ // res.should.have.status(200);
792
+
793
+ // let kbid = res.body.value._id;
794
+ // console.log("kbid: ", kbid)
795
+ // chai.request(server)
796
+ // .post('/' + savedProject._id + "/kb/scrape/single")
797
+ // .auth(email, pwd)
798
+ // .send({ id: kbid })
799
+ // .end((err, res) => {
800
+ // if (log) { console.log("single scrape res.body: ", res.body); }
801
+ // //res.should.have.status(200);
802
+ // // res.body.should.be.a('object');
803
+ // // expect(res.body.id_project).to.equal(savedProject._id.toString())
804
+ // // expect(res.body.maxKbsNumber).to.equal(3);
805
+ // // expect(res.body.maxPagesNumber).to.equal(1000);
806
+ // // expect(res.body.kbs).is.an('array').that.is.empty;
807
+ // done();
808
+
809
+ // })
810
+
811
+
812
+ // // res.body.should.be.a('object');
813
+ // // expect(res.body.id_project).to.equal(savedProject._id.toString());
814
+
815
+
816
+
817
+
818
+ // })
819
+ // });
820
+ // });
821
+ // });
822
+
823
+ // it('multiadd', (done) => {
824
+
825
+ // var email = "test-signup-" + Date.now() + "@email.com";
826
+ // var pwd = "pwd";
827
+
828
+ // userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
829
+ // projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
830
+
831
+ // chai.request(server)
832
+ // .post('/' + savedProject._id + '/kb/multi')
833
+ // .auth(email, pwd)
834
+ // .set('Content-Type', 'text/plain')
835
+ // .attach('uploadFile', fs.readFileSync(path.resolve(__dirname, './kbUrlsList.txt')), 'kbUrlsList.txt')
836
+ // .end((err, res) => {
837
+
838
+ // // console.log("res.body: ", res.body)
839
+ // res.should.have.status(200);
840
+ // expect(res.body.length).to.equal(4)
841
+
842
+ // done();
843
+
844
+ // // setTimeout(() => {
845
+
846
+ // // chai.request(server)
847
+ // // .post('/' + savedProject._id + '/kb/multi')
848
+ // // .auth(email, pwd)
849
+ // // .set('Content-Type', 'text/plain')
850
+ // // .attach('uploadFile', fs.readFileSync(path.resolve(__dirname, './kbUrlsList.txt')), 'kbUrlsList.txt')
851
+ // // .end((err, res) => {
852
+
853
+ // // // console.log("res.body: ", res.body);
854
+ // // res.should.have.status(200);
855
+ // // expect(res.body.length).to.equal(4)
856
+
857
+ // // done()
858
+ // // })
859
+ // // }, 2000)
860
+
861
+ // })
862
+
863
+ // });
864
+ // });
865
+
866
+ // }).timeout(10000)
867
+
868
+ // it('multiaddFail', (done) => {
869
+
870
+ // var email = "test-signup-" + Date.now() + "@email.com";
871
+ // var pwd = "pwd";
872
+
873
+ // userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
874
+ // projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
875
+
876
+ // chai.request(server)
877
+ // .post('/' + savedProject._id + '/kb/multi')
878
+ // .auth(email, pwd)
879
+ // .set('Content-Type', 'text/plain')
880
+ // .attach('uploadFile', fs.readFileSync(path.resolve(__dirname, './kbUrlsList.txt')), 'kbUrlsList.txt')
881
+ // .end((err, res) => {
882
+
883
+ // // console.log("res.body: ", res.body)
884
+ // res.should.have.status(200);
885
+ // expect(res.body.length).to.equal(4)
886
+
887
+ // setTimeout(() => {
888
+
889
+ // chai.request(server)
890
+ // .post('/' + savedProject._id + '/kb/multi')
891
+ // .auth(email, pwd)
892
+ // .set('Content-Type', 'text/plain')
893
+ // .attach('uploadFile', fs.readFileSync(path.resolve(__dirname, './kbUrlsList.txt')), 'kbUrlsList.txt')
894
+ // .end((err, res) => {
895
+
896
+ // // console.log("res.body: ", res.body);
897
+ // res.should.have.status(200);
898
+ // expect(res.body.length).to.equal(4)
899
+
900
+ // done()
901
+ // })
902
+ // }, 2000)
903
+
904
+ // })
905
+
906
+ // });
907
+ // });
908
+
909
+ // }).timeout(10000)
910
+
911
+ // it('tooManyUrls', (done) => {
912
+
913
+ // var email = "test-signup-" + Date.now() + "@email.com";
914
+ // var pwd = "pwd";
915
+
916
+ // userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
917
+ // projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
918
+
919
+ // chai.request(server)
920
+ // .post('/' + savedProject._id + '/kb/multi')
921
+ // .auth(email, pwd)
922
+ // .set('Content-Type', 'text/plain')
923
+ // .attach('uploadFile', fs.readFileSync(path.resolve(__dirname, './TooManykbUrlsList.txt')), 'TooManykbUrlsList.txt')
924
+ // .end((err, res) => {
925
+
926
+ // // console.log("res.body: ", res.body)
927
+ // res.should.have.status(403);
928
+ // expect(res.body.success).to.equal(false);
929
+ // expect(res.body.error).to.equal("Too many urls. Can't index more than 300 urls at a time.");
930
+
931
+ // done()
932
+
933
+ // })
934
+
935
+ // });
936
+ // });
937
+
938
+ // })
939
+
940
+ // logic in standby
941
+ // it('askkb-namespaceNotBelongsProject', (done) => {
473
942
 
474
943
  // var email = "test-signup-" + Date.now() + "@email.com";
475
944
  // var pwd = "pwd";
@@ -495,7 +964,7 @@ describe('KbRoute', () => {
495
964
  // res.body.should.be.a('object');
496
965
  // expect(res.body.success).to.equal(false);
497
966
  // expect(res.body.error).to.equal("Not allowed. The namespace does not belong to the current project.");
498
-
967
+
499
968
  // done();
500
969
  // })
501
970
  // })
@@ -503,7 +972,118 @@ describe('KbRoute', () => {
503
972
  // }).timeout(10000)
504
973
 
505
974
 
506
- it('sitemap', (done) => {
975
+ // it('sitemap', (done) => {
976
+
977
+ // var email = "test-signup-" + Date.now() + "@email.com";
978
+ // var pwd = "pwd";
979
+
980
+ // userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
981
+ // projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
982
+
983
+ // chai.request(server)
984
+ // .post('/' + savedProject._id + '/kb/sitemap')
985
+ // .auth(email, pwd)
986
+ // // .send({ sitemap: "https://www.wired.it/sitemap.xml" })
987
+ // .send({ sitemap: "https://gethelp.tiledesk.com/sitemap.xml" })
988
+ // .end((err, res) => {
989
+
990
+ // if (err) { console.log("error: ", err) };
991
+ // if (log) { console.log("res.body: ", res.body) }
992
+
993
+ // res.should.have.status(200);
994
+ // res.body.should.be.a('object');
995
+ // res.body.sites.should.be.a('array');
996
+
997
+ // done();
998
+
999
+ // })
1000
+
1001
+ // });
1002
+ // });
1003
+
1004
+ // }).timeout(10000)
1005
+
1006
+ it('webhook', (done) => {
1007
+
1008
+ var email = "test-signup-" + Date.now() + "@email.com";
1009
+ var pwd = "pwd";
1010
+
1011
+ userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
1012
+ projectService.create("test-kb-webhook", savedUser._id).then(function (savedProject) {
1013
+
1014
+ chai.request(server)
1015
+ .get('/' + savedProject._id + '/kb/namespace/all')
1016
+ .auth(email, pwd)
1017
+ .end((err, res) => {
1018
+
1019
+ if (err) { console.log("error: ", err) };
1020
+ if (log) { console.log("1 res.body: ", res.body) };
1021
+
1022
+ res.should.have.status(200);
1023
+ res.body.should.be.a('array');
1024
+
1025
+ let namespace_id = res.body[0].id;
1026
+
1027
+ let kb = {
1028
+ name: "example_name6",
1029
+ type: "url",
1030
+ source: "https://www.exampleurl6.com",
1031
+ content: "",
1032
+ namespace: namespace_id
1033
+ }
1034
+
1035
+ chai.request(server)
1036
+ .post('/' + savedProject._id + '/kb/')
1037
+ .auth(email, pwd)
1038
+ .send(kb)
1039
+ .end((err, res) => {
1040
+
1041
+ if (err) { console.log("error: ", err) };
1042
+ if (log) { console.log("2 res.body: ", res.body) };
1043
+
1044
+ res.should.have.status(200);
1045
+ res.body.should.be.a('object');
1046
+
1047
+ let kb_id = res.body.value._id;
1048
+
1049
+ chai.request(server)
1050
+ .post('/webhook/kb/status')
1051
+ .set("x-auth-token", "testtoken")
1052
+ .send({ id: kb_id, status: 300 })
1053
+ .end((err, res) => {
1054
+
1055
+ if (err) { console.error("err: ", err) };
1056
+ if (log) { console.log("3 res.body: ", res.body) };
1057
+
1058
+ res.should.have.status(200);
1059
+ res.body.should.be.a('object');
1060
+ expect(res.body.status).to.equal(300);
1061
+
1062
+ done();
1063
+
1064
+ })
1065
+
1066
+
1067
+ })
1068
+ })
1069
+
1070
+
1071
+
1072
+
1073
+ });
1074
+ });
1075
+ }).timeout(10000)
1076
+
1077
+ })
1078
+
1079
+ describe('namespaces', () => {
1080
+
1081
+
1082
+ /**
1083
+ * Get all namespaces of a project.
1084
+ * If there isn't namespaces for a project_id, the default namespace is created and returned.
1085
+ */
1086
+ it('get-namespaces', (done) => {
507
1087
 
508
1088
  var email = "test-signup-" + Date.now() + "@email.com";
509
1089
  var pwd = "pwd";
@@ -512,82 +1092,377 @@ describe('KbRoute', () => {
512
1092
  projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
513
1093
 
514
1094
  chai.request(server)
515
- .post('/' + savedProject._id + '/kb/sitemap')
1095
+ .get('/' + savedProject._id + '/kb/namespace/all')
516
1096
  .auth(email, pwd)
517
- // .send({ sitemap: "https://www.wired.it/sitemap.xml" })
518
- .send({ sitemap: "https://gethelp.tiledesk.com/sitemap.xml" })
519
1097
  .end((err, res) => {
520
1098
 
521
- if (err) { console.log("error: ", err) };
522
- if (log) { console.log("res.body: ", res.body) }
1099
+ if (err) { console.error("err: ", err); }
1100
+ if (log) { console.log("get all namespaces res.body: ", res.body); }
523
1101
 
524
1102
  res.should.have.status(200);
525
- res.body.should.be.a('object');
526
- res.body.sites.should.be.a('array');
1103
+ res.body.should.be.a('array');
1104
+ expect(res.body.length).to.equal(1);
1105
+ should.not.exist(res.body[0]._id);
1106
+ //expect(res.body[0]._id).to.equal(undefined);
1107
+ expect(res.body[0].id).to.equal(savedProject._id.toString());
1108
+ expect(res.body[0].name).to.equal("Default");
527
1109
 
528
1110
  done();
529
1111
 
1112
+
530
1113
  })
531
1114
 
532
1115
  });
533
1116
  });
534
1117
 
535
- }).timeout(10000)
1118
+ })
536
1119
 
537
- it('webhook', (done) => {
1120
+ /**
1121
+ * Get all namespaces of a project.
1122
+ * If there isn't namespaces for a project_id, the default namespace is created and returned.
1123
+ */
1124
+ it('create-and-get-namespaces', (done) => {
538
1125
 
539
1126
  var email = "test-signup-" + Date.now() + "@email.com";
540
1127
  var pwd = "pwd";
541
1128
 
542
1129
  userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
543
- projectService.create("test-kb-webhook", savedUser._id).then(function (savedProject) {
544
-
545
- let kb = {
546
- name: "example_name6",
547
- type: "url",
548
- source: "https://www.exampleurl6.com",
549
- content: "",
550
- }
1130
+ projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
551
1131
 
1132
+ // Get all namespaces. Create default namespace and return.
552
1133
  chai.request(server)
553
- .post('/' + savedProject._id + '/kb/')
1134
+ .get('/' + savedProject._id + '/kb/namespace/all')
554
1135
  .auth(email, pwd)
555
- .send(kb)
556
1136
  .end((err, res) => {
557
1137
 
558
- if (err) { console.log("error: ", err) };
559
- if (log) { console.log("res.body: ", res.body) };
1138
+ if (err) { console.error("err: ", err); }
1139
+ if (log) { console.log("get all namespaces res.body: ", res.body); }
560
1140
 
561
1141
  res.should.have.status(200);
562
- res.body.should.be.a('object');
1142
+ res.body.should.be.a('array');
1143
+ expect(res.body.length).to.equal(1);
1144
+ should.not.exist(res.body[0]._id);
1145
+ expect(res.body[0].id).to.equal(savedProject._id.toString());
1146
+ expect(res.body[0].name).to.equal("Default");
1147
+
1148
+ // Create another namespace
1149
+ chai.request(server)
1150
+ .post('/' + savedProject._id + '/kb/namespace')
1151
+ .auth(email, pwd)
1152
+ .send({ name: "MyCustomNamespace" })
1153
+ .end((err, res) => {
1154
+
1155
+ if (err) { console.error("err: ", err) }
1156
+ if (log) { console.log("create new namespace res.body: ", res.body) }
1157
+
1158
+ res.should.have.status(200);
1159
+ res.body.should.be.a('object');
1160
+ should.not.exist(res.body._id)
1161
+ should.exist(res.body.id)
1162
+ expect(res.body.name).to.equal('MyCustomNamespace');
1163
+
1164
+ // Get again all namespace. A new default namespace should not be created.
1165
+ chai.request(server)
1166
+ .get('/' + savedProject._id + '/kb/namespace/all')
1167
+ .auth(email, pwd)
1168
+ .end((err, res) => {
1169
+
1170
+ if (err) { console.error("err: ", err); }
1171
+ if (log) { console.log("get all namespaces res.body: ", res.body); }
1172
+
1173
+ res.should.have.status(200);
1174
+ res.body.should.be.a('array');
1175
+ expect(res.body.length).to.equal(2);
1176
+ should.not.exist(res.body[0]._id);
1177
+ should.not.exist(res.body[1]._id);
1178
+ should.exist(res.body[0].id);
1179
+ should.exist(res.body[1].id);
1180
+
1181
+ done();
1182
+ })
1183
+ })
1184
+ })
1185
+ });
1186
+ });
1187
+ })
1188
+
1189
+
1190
+ /**
1191
+ * Update namespaces
1192
+ */
1193
+ it('update-namespace', (done) => {
563
1194
 
564
- let kb_id = res.body.value._id;
1195
+ var email = "test-signup-" + Date.now() + "@email.com";
1196
+ var pwd = "pwd";
1197
+
1198
+ userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
1199
+ projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
1200
+
1201
+ // Get all namespaces. Create default namespace and return.
1202
+ chai.request(server)
1203
+ .get('/' + savedProject._id + '/kb/namespace/all')
1204
+ .auth(email, pwd)
1205
+ .end((err, res) => {
1206
+
1207
+ if (err) { console.error("err: ", err); }
1208
+ if (log) { console.log("get all namespaces res.body: ", res.body); }
565
1209
 
1210
+ res.should.have.status(200);
1211
+ res.body.should.be.a('array');
1212
+ expect(res.body.length).to.equal(1);
1213
+ expect(res.body[0].id).to.equal(savedProject._id.toString());
1214
+ expect(res.body[0].name).to.equal("Default");
1215
+
1216
+ let namespace_id = res.body[0].id;
1217
+ console.log("namespace_id: ", namespace_id);
1218
+
1219
+ let new_settings = {
1220
+ model: 'gpt-4o',
1221
+ max_tokens: 256,
1222
+ temperature: 0.3,
1223
+ top_k: 6,
1224
+ context: "You are an awesome AI Assistant."
1225
+ }
1226
+
1227
+ // Update namespace
566
1228
  chai.request(server)
567
- .post('/webhook/kb/status')
568
- .set("x-auth-token", "testtoken")
569
- .send({ id: kb_id, status: 300 })
1229
+ .put('/' + savedProject._id + '/kb/namespace/' + namespace_id)
1230
+ .auth(email, pwd)
1231
+ .send({ name: "New Name", preview_settings: new_settings })
570
1232
  .end((err, res) => {
571
1233
 
572
- if (err) { console.error("err: ", err) };
573
- if (log) { console.log("res.body: ", res.body) };
1234
+ if (err) { console.error("err: ", err) }
1235
+ if (log) { console.log("update namespace res.body: ", res.body) }
574
1236
 
575
1237
  res.should.have.status(200);
576
1238
  res.body.should.be.a('object');
577
- expect(res.body.status).to.equal(300);
1239
+ should.not.exist(res.body._id);
1240
+ should.exist(res.body.id);
1241
+ expect(res.body.name).to.equal('New Name');
1242
+ expect(res.body.preview_settings.model).to.equal('gpt-4o')
1243
+ expect(res.body.preview_settings.max_tokens).to.equal(256)
1244
+ expect(res.body.preview_settings.temperature).to.equal(0.3)
1245
+ expect(res.body.preview_settings.top_k).to.equal(6)
578
1246
 
579
1247
  done();
580
1248
 
581
1249
  })
1250
+ })
1251
+ });
1252
+ });
1253
+ })
582
1254
 
1255
+ /**
1256
+ * Delete default namespace - Forbidden
1257
+ */
1258
+ it('fail-to-delete-default-namespace', (done) => {
583
1259
 
584
- })
1260
+ var email = "test-signup-" + Date.now() + "@email.com";
1261
+ var pwd = "pwd";
1262
+
1263
+ userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
1264
+ projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
1265
+
1266
+ // Get all namespaces. Create default namespace and return.
1267
+ chai.request(server)
1268
+ .get('/' + savedProject._id + '/kb/namespace/all')
1269
+ .auth(email, pwd)
1270
+ .end((err, res) => {
1271
+
1272
+ if (err) { console.error("err: ", err); }
1273
+ if (log) { console.log("get all namespaces res.body: ", res.body); }
1274
+
1275
+ res.should.have.status(200);
1276
+ res.body.should.be.a('array');
1277
+ expect(res.body.length).to.equal(1);
1278
+ expect(res.body[0].id).to.equal(savedProject._id.toString());
1279
+ expect(res.body[0].name).to.equal("Default");
1280
+
1281
+ let namespace_id = res.body[0].id;
1282
+ console.log("namespace_id: ", namespace_id);
1283
+
1284
+ // Update namespace
1285
+ chai.request(server)
1286
+ .delete('/' + savedProject._id + '/kb/namespace/' + namespace_id)
1287
+ .auth(email, pwd)
1288
+ .end((err, res) => {
1289
+
1290
+ if (err) { console.error("err: ", err) }
1291
+ if (log) { console.log("delete namespace res.body: ", res.body) }
1292
+
1293
+ res.should.have.status(403);
1294
+ res.body.should.be.a('object');
1295
+ expect(res.body.success).to.equal(false);
1296
+ expect(res.body.error).to.equal('Default namespace cannot be deleted');
1297
+
1298
+ done();
585
1299
 
1300
+ })
1301
+ })
586
1302
  });
587
1303
  });
1304
+ })
1305
+
1306
+
1307
+ it('get-chatbots-from-namespace', (done) => {
1308
+
1309
+ var email = "test-signup-" + Date.now() + "@email.com";
1310
+ var pwd = "pwd";
1311
+
1312
+ userService.signup(email, pwd, "Test Firstname", "Test Lastname").then((savedUser) => {
1313
+ projectService.create('test-faqkb-create', savedUser._id).then((savedProject) => {
1314
+ faqService.create("testbot1", null, savedProject._id, savedUser._id).then((savedBot1) => {
1315
+ faqService.create("testbot2", null, savedProject._id, savedUser._id).then((savedBot2) => {
1316
+
1317
+ chai.request(server)
1318
+ .get('/' + savedProject._id + '/kb/namespace/all')
1319
+ .auth(email, pwd)
1320
+ .end((err, res) => {
1321
+
1322
+ if (err) { console.error("err: ", err); }
1323
+ if (log) { console.log("get all namespaces res.body: ", res.body); }
1324
+
1325
+ res.should.have.status(200);
1326
+
1327
+ let namespace_id = res.body[0].id;
1328
+ if (log) { console.log("namespace_id: ", namespace_id) }
1329
+
1330
+ let newFaq1 = new faq({
1331
+ id_faq_kb: savedBot1._id,
1332
+ id_project: savedProject._id,
1333
+ intent_id: "new-faq-1",
1334
+ createdBy: savedUser._id,
1335
+ updatedBy: savedUser._id,
1336
+ actions: [{ "_tdActionType": "askgptv2", "_tdActionId": "f58212f9-1a8c-4623-b6fa-0f34e57d9999", "namespace": namespace_id }]
1337
+ })
1338
+
1339
+ newFaq1.save((err, saved1) => {
1340
+ if (err) { console.error("err1: ", err) };
1341
+ if (log) { console.log("faq1 saved: ", saved1) };
1342
+
1343
+ let newFaq2 = new faq({
1344
+ id_faq_kb: savedBot2._id,
1345
+ id_project: savedProject._id,
1346
+ intent_id: "new-faq-2",
1347
+ createdBy: savedUser._id,
1348
+ updatedBy: savedUser._id,
1349
+ actions: [{ "_tdActionType": "reply", "_tdActionId": "f58212f9-1a8c-4623-b6fa-0f34e57d9998" }]
1350
+ })
1351
+
1352
+ newFaq2.save((err, saved2) => {
1353
+ if (err) { console.error("err2: ", err) };
1354
+ if (log) { console.log("faq2 saved: ", saved2) };
1355
+
1356
+ chai.request(server)
1357
+ .get('/' + savedProject._id + '/kb/namespace/' + namespace_id + '/chatbots')
1358
+ .auth(email, pwd)
1359
+ .end((err, res) => {
1360
+
1361
+ if (err) { console.error("err: ", err) };
1362
+ if (log) { console.log("get chatbots from namespace res.body: ", res.body) };
1363
+
1364
+ res.should.have.status(200);
1365
+ res.body.should.be.a('array');
1366
+ expect(res.body.length).to.equal(1);
1367
+ expect(res.body[0]._id).to.equal((savedBot1._id).toString());
1368
+ expect(res.body[0].name).to.equal('testbot1');
1369
+
1370
+ done();
1371
+ })
1372
+ })
1373
+ })
1374
+ })
1375
+
1376
+ })
1377
+ })
1378
+ })
1379
+ })
588
1380
  }).timeout(10000)
589
1381
 
1382
+ /**
1383
+ * Delete namespace
1384
+ * !! Unable to test it due to external request
1385
+ */
1386
+ // it('delete-namespace', (done) => {
1387
+
1388
+ // var email = "test-signup-" + Date.now() + "@email.com";
1389
+ // var pwd = "pwd";
1390
+
1391
+ // nock('https://api.openai.com')
1392
+ // .post('/v1/namespaces/delete')
1393
+ // .reply(200, { success: true });
1394
+
1395
+ // userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
1396
+ // projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
1397
+
1398
+ // // Get all namespaces. Create default namespace and return.
1399
+ // chai.request(server)
1400
+ // .get('/' + savedProject._id + '/kb/namespace/all')
1401
+ // .auth(email, pwd)
1402
+ // .end((err, res) => {
1403
+
1404
+ // if (err) { console.error("err: ", err); }
1405
+ // if (log) { console.log("get all namespaces res.body: ", res.body); }
1406
+
1407
+ // res.should.have.status(200);
1408
+ // res.body.should.be.a('array');
1409
+ // expect(res.body.length).to.equal(1);
1410
+ // expect(res.body[0].namespace_id).to.equal(savedProject._id.toString());
1411
+ // expect(res.body[0].name).to.equal("Default");
1412
+
1413
+ // // Create another namespace
1414
+ // chai.request(server)
1415
+ // .post('/' + savedProject._id + '/kb/namespace')
1416
+ // .auth(email, pwd)
1417
+ // .send({ name: "MyCustomNamespace" })
1418
+ // .end((err, res) => {
1419
+
1420
+ // if (err) { console.error("err: ", err) }
1421
+ // if (log) { console.log("create new namespace res.body: ", res.body) }
1422
+
1423
+ // res.should.have.status(200);
1424
+ // res.body.should.be.a('object');
1425
+ // expect(res.body.name).to.equal('MyCustomNamespace');
1426
+
1427
+ // let namespace_to_delete = res.body.namespace_id;
1428
+ // console.log("namespace_to_delete: ", namespace_to_delete);
1429
+
1430
+ // // Get again all namespace. A new default namespace should not be created.
1431
+ // chai.request(server)
1432
+ // .get('/' + savedProject._id + '/kb/namespace/all')
1433
+ // .auth(email, pwd)
1434
+ // .end((err, res) => {
1435
+
1436
+ // if (err) { console.error("err: ", err); }
1437
+ // if (log) { console.log("get all namespaces res.body: ", res.body); }
1438
+
1439
+ // res.should.have.status(200);
1440
+ // res.body.should.be.a('array');
1441
+ // expect(res.body.length).to.equal(2);
1442
+
1443
+ // console.log("namespace_to_delete: ", namespace_to_delete);
1444
+
1445
+ // chai.request(server)
1446
+ // .delete('/' + savedProject._id + '/kb/namespace/' + namespace_to_delete)
1447
+ // .auth(email, pwd)
1448
+ // .end((err, res) => {
1449
+
1450
+ // if (err) { console.error("err: ", err); }
1451
+ // if (log) { console.log("delete namespaces res.body: ", res.body); }
1452
+
1453
+ // res.should.have.status(200);
1454
+
1455
+ // done();
1456
+ // })
1457
+ // })
1458
+ // })
1459
+ // })
1460
+ // });
1461
+ // });
1462
+ // })
1463
+
590
1464
  })
591
1465
  });
592
1466
 
593
1467
 
1468
+