@tiledesk/tiledesk-server 3.0.0 → 3.1.0
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.
- package/.circleci/config.yml +1 -1
- package/CHANGELOG.md +37 -0
- package/Dockerfile +3 -2
- package/Dockerfile-en +3 -2
- package/Dockerfile-jobs +3 -2
- package/Dockerfile-profiler +3 -2
- package/README.md +15 -0
- package/app.js +14 -1
- package/bin/quota-reset +68 -0
- package/bin/quota-restore +86 -0
- package/channels/chat21/chat21Handler.js +50 -34
- package/channels/chat21/chat21JoinTracker.js +43 -0
- package/config/database.js +5 -5
- package/config/native_mcp/native_mcp_servers.json +1 -0
- package/controllers/chatbot.controller.js +80 -27
- package/controllers/intents.controller.js +42 -0
- package/docs/superpowers/plans/2026-09-15-webhook-chatbot-hitl-chat21-ionic.md +468 -0
- package/docs/superpowers/plans/2026-09-15-webhook-chatbot-hitl-design-studio.md +1349 -0
- package/docs/superpowers/plans/2026-09-15-webhook-chatbot-hitl-server.md +1694 -0
- package/docs/superpowers/plans/2026-09-15-webhook-chatbot-hitl-tybot.md +789 -0
- package/docs/superpowers/plans/2026-09-29-hitl-known-limits-server.md +83 -0
- package/docs/superpowers/plans/2026-09-29-hitl-known-limits-tybot.md +76 -0
- package/docs/superpowers/plans/2026-09-29-hitl-participant-removal-chat21-ionic.md +97 -0
- package/docs/superpowers/plans/2026-09-29-hitl-participant-removal-design-studio.md +72 -0
- package/docs/superpowers/plans/2026-09-29-hitl-participant-removal-server.md +223 -0
- package/docs/superpowers/plans/2026-09-29-hitl-participant-removal-tybot.md +121 -0
- package/docs/superpowers/plans/2026-09-29-hitl-pre-branch-ports.md +65 -0
- package/docs/superpowers/plans/2026-09-30-flow-start-points-design-studio.md +88 -0
- package/docs/superpowers/plans/2026-09-30-flow-start-points-server.md +103 -0
- package/docs/superpowers/plans/2026-09-30-flow-start-points-tybot.md +43 -0
- package/docs/superpowers/plans/2026-10-01-scheduled-start-point-design-studio.md +80 -0
- package/docs/superpowers/plans/2026-10-01-scheduled-start-point-runtime.md +39 -0
- package/docs/superpowers/plans/2026-10-01-scheduled-start-point-server.md +106 -0
- package/docs/superpowers/specs/2026-09-15-webhook-chatbot-hitl-design.md +715 -0
- package/docs/superpowers/specs/2026-09-30-flow-start-points-design.md +156 -0
- package/docs/superpowers/specs/2026-10-01-scheduled-start-point-design.md +303 -0
- package/docs/superpowers/todo/2026-09-14-webhook-chatbot-shared-code-fixes.md +191 -0
- package/event/botEvent.js +12 -1
- package/event/emailEvent.js +3 -0
- package/event/messageEvent.js +44 -0
- package/jobs.js +10 -0
- package/jobsManager.js +19 -0
- package/middleware/errorHandler.js +6 -2
- package/models/agentPrompt.js +104 -0
- package/models/analyticResult.js +1 -1
- package/models/channelConstants.js +3 -2
- package/models/quotaUsage.js +71 -0
- package/models/request.js +6 -0
- package/models/requestSnapshot.js +12 -0
- package/models/routerLogger.js +1 -1
- package/models/webhook.js +44 -0
- package/package.json +7 -5
- package/pubmodules/emailNotification/requestNotification.js +14 -22
- package/pubmodules/events/test/eventRoute.js +7 -6
- package/pubmodules/rules/conciergeBot.js +9 -4
- package/routes/agentPrompts.js +132 -0
- package/routes/auth.js +1 -12
- package/routes/campaigns.js +0 -13
- package/routes/dataTable.js +0 -3
- package/routes/faq_kb.js +12 -1
- package/routes/messagesRoot.js +0 -1
- package/routes/quotes.js +38 -1
- package/routes/request.js +81 -5
- package/routes/users.js +0 -1
- package/routes/webhook.js +160 -8
- package/routes/webhooks.js +276 -6
- package/routes/widgetLoader.js +6 -3
- package/services/QuoteManager.js +77 -16
- package/services/aiService.js +1 -1
- package/services/chatbotService.js +55 -1
- package/services/departmentService.js +60 -43
- package/services/dolphinSchedulerClient.js +116 -0
- package/services/emailService.js +6 -1
- package/services/hitlParticipantListener.js +260 -0
- package/services/participantInviteService.js +302 -0
- package/services/participantRemoveService.js +79 -0
- package/services/quotaSnapshotService.js +1167 -0
- package/services/requestService.js +102 -144
- package/services/scheduledStart/dolphinBackend.js +246 -0
- package/services/scheduledStart/schedulerBackend.js +101 -0
- package/services/scheduledStart/schedulerServiceClient.js +41 -0
- package/services/scheduledStartPublishListener.js +186 -0
- package/services/scheduledStartRules.js +173 -0
- package/services/scheduledStartService.js +130 -0
- package/services/startPointCleanupListener.js +30 -0
- package/services/startPointService.js +158 -0
- package/services/testWsService.js +1 -4
- package/services/updateRequestSnapshotQueued.js +8 -9
- package/services/webhookConversationService.js +185 -0
- package/services/webhookSettingsValidator.js +61 -0
- package/template/email/checkpointReachedEmail.html +5 -0
- package/test/agentPromptsRoute.test.js +154 -0
- package/test/agentTokensQuota.js +237 -0
- package/test/botEventHitlFallback.js +19 -0
- package/test/chat21HandlerJoinWiring.js +59 -0
- package/test/dataTablesRoute.test.js +10 -2
- package/test/dateUtils.test.js +0 -3
- package/test/departmentService.js +8 -12
- package/test/{fileRoute.js → deprecated/_deprecated_fileRoute} +2 -2
- package/test/{kbsettingsRoute.js → deprecated/_deprecated_kbsettingsRoute} +3 -3
- package/test/faqRoute.js +6 -1
- package/test/faqkbRoute.js +14 -10
- package/test/faqkbV3Route.js +1 -1
- package/test/filepRoute.js +4 -50
- package/test/hitlParticipantListener.js +446 -0
- package/test/hitlQueueWiring.js +191 -0
- package/test/hitlWaitingReset.js +124 -0
- package/test/inviteParticipantsRoute.js +492 -0
- package/test/kbRoute.js +1 -1
- package/test/messageRoute.js +33 -21
- package/test/nativemcp.test.js +7 -1
- package/test/openaiRoute.js +2 -8
- package/test/publishFailLoud.test.js +414 -0
- package/test/quotaSnapshots.js +1044 -0
- package/test/removeParticipantsRoute.js +186 -0
- package/test/requestService.js +24 -16
- package/test/requestServiceRemoveParticipant.js +144 -0
- package/test/requestVisibility.test.js +186 -0
- package/test/scheduledStartFacade.js +184 -0
- package/test/scheduledStartHandover.js +95 -0
- package/test/scheduledStartPublish.js +320 -0
- package/test/scheduledStartRoute.js +163 -0
- package/test/scheduledStartRules.js +177 -0
- package/test/scheduledStartRun.js +299 -0
- package/test/scheduledStartSchedulerBackend.js +116 -0
- package/test/scheduledStartService.js +290 -0
- package/test/startPointsCleanup.js +69 -0
- package/test/startPointsRegression.js +169 -0
- package/test/startPointsRoute.js +158 -0
- package/test/webhookConversationRoute.js +347 -0
- package/test/webhookConversationService.js +261 -0
- package/test/webhookConversationTestUtils.js +55 -0
- package/utils/aiUtils.js +95 -1
- package/utils/intentFieldNames.js +192 -0
- package/utils/quotaCliUtil.js +43 -0
- package/utils/requestVisibility.js +355 -0
- package/websocket/webSocketServer.js +36 -31
- /package/test/{imageRoute.js → deprecated/_deprecated_imageRoute} +0 -0
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
process.env.NODE_ENV = 'test';
|
|
2
|
+
process.env.LOG_LEVEL = 'error';
|
|
3
|
+
|
|
4
|
+
const chai = require('chai');
|
|
5
|
+
const chaiHttp = require('chai-http');
|
|
6
|
+
const server = require('../app');
|
|
7
|
+
const expect = chai.expect;
|
|
8
|
+
chai.use(chaiHttp);
|
|
9
|
+
|
|
10
|
+
const { Webhook } = require('../models/webhook');
|
|
11
|
+
const { setupProjectWithBot } = require('./webhookConversationTestUtils');
|
|
12
|
+
|
|
13
|
+
const ENV = ['SCHEDULER_BASEURL', 'SCHEDULER_TOKEN', 'SCHEDULER_PROJECT', 'SCHEDULED_START_TOKEN'];
|
|
14
|
+
const SCHEDULE = { frequency: 'weekly', time: '08:30', weekdays: ['mon', 'tue', 'wed', 'thu', 'fri'], timezone: 'Europe/Rome' };
|
|
15
|
+
|
|
16
|
+
function url(ctx, suffix) {
|
|
17
|
+
return '/' + ctx.project._id + '/webhooks/' + ctx.chatbot_id + (suffix || '');
|
|
18
|
+
}
|
|
19
|
+
function put(ctx, type, body) {
|
|
20
|
+
return chai.request(server).put(url(ctx, '/start_points/' + type)).auth(ctx.email, ctx.pwd).send(body);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
describe('ScheduledStartRoute', function () {
|
|
24
|
+
|
|
25
|
+
this.timeout(30000);
|
|
26
|
+
|
|
27
|
+
let saved;
|
|
28
|
+
beforeEach(() => {
|
|
29
|
+
saved = {};
|
|
30
|
+
ENV.forEach((n) => { saved[n] = process.env[n]; process.env[n] = 'test-' + n; });
|
|
31
|
+
});
|
|
32
|
+
afterEach(() => {
|
|
33
|
+
ENV.forEach((n) => { if (saved[n] === undefined) { delete process.env[n]; } else { process.env[n] = saved[n]; } });
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('stores the draft and answers with schedule_summary and no live data', async () => {
|
|
37
|
+
const ctx = await setupProjectWithBot(server);
|
|
38
|
+
const res = await put(ctx, 'scheduled', {
|
|
39
|
+
block_id: ctx.startIntent.intent_id,
|
|
40
|
+
mapping: { source_name: 'Nightly', payload: { env: 'prod', n: 3 } },
|
|
41
|
+
schedule: SCHEDULE
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
expect(res).to.have.status(200);
|
|
45
|
+
expect(res.body.mode).to.equal('conversation');
|
|
46
|
+
const sp = res.body.start_points.find((s) => s.type === 'scheduled');
|
|
47
|
+
expect(sp.block_id).to.equal(ctx.startIntent.intent_id);
|
|
48
|
+
expect(sp.enabled).to.equal(true);
|
|
49
|
+
expect(sp.mapping).to.deep.equal({ source_name: 'Nightly', payload: { env: 'prod', n: 3 } });
|
|
50
|
+
expect(sp.schedule).to.deep.equal(SCHEDULE);
|
|
51
|
+
expect(res.body.schedule_summary).to.equal('Every weekday at 08:30 (Europe/Rome)');
|
|
52
|
+
expect(res.body.scheduled_live).to.equal(undefined);
|
|
53
|
+
expect(res.body.live_summary).to.equal(undefined);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('updates the existing entry keeping the schedule when omitted, and reports live_summary', async () => {
|
|
57
|
+
const ctx = await setupProjectWithBot(server);
|
|
58
|
+
const block_id = ctx.startIntent.intent_id;
|
|
59
|
+
await put(ctx, 'scheduled', { block_id: block_id, schedule: SCHEDULE });
|
|
60
|
+
await Webhook.updateOne({ chatbot_id: ctx.chatbot_id }, { $set: { scheduled_live: { enabled: true, block_id: block_id, schedule: { frequency: 'daily', time: '09:00', timezone: 'UTC' } } } });
|
|
61
|
+
|
|
62
|
+
const res = await put(ctx, 'scheduled', { block_id: block_id, enabled: false });
|
|
63
|
+
|
|
64
|
+
expect(res).to.have.status(200);
|
|
65
|
+
const sps = res.body.start_points.filter((s) => s.type === 'scheduled');
|
|
66
|
+
expect(sps).to.have.length(1);
|
|
67
|
+
expect(sps[0].enabled).to.equal(false);
|
|
68
|
+
expect(sps[0].schedule).to.deep.equal(SCHEDULE);
|
|
69
|
+
expect(res.body.live_summary).to.equal('Daily at 09:00 (UTC)');
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('keeps the webhook start point untouched', async () => {
|
|
73
|
+
const ctx = await setupProjectWithBot(server);
|
|
74
|
+
const block_id = ctx.startIntent.intent_id;
|
|
75
|
+
await put(ctx, 'webhook', { block_id: block_id });
|
|
76
|
+
const res = await put(ctx, 'scheduled', { block_id: block_id, schedule: SCHEDULE });
|
|
77
|
+
expect(res.body.start_points.map((s) => s.type).sort()).to.deep.equal(['scheduled', 'webhook']);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('answers 400 when creating without schedule', async () => {
|
|
81
|
+
const ctx = await setupProjectWithBot(server);
|
|
82
|
+
const res = await put(ctx, 'scheduled', { block_id: ctx.startIntent.intent_id });
|
|
83
|
+
expect(res).to.have.status(400);
|
|
84
|
+
expect(res.body.error).to.contain('schedule');
|
|
85
|
+
expect(await Webhook.countDocuments({ chatbot_id: ctx.chatbot_id })).to.equal(0);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('answers 400 when creating without schedule on an existing webhook', async () => {
|
|
89
|
+
const ctx = await setupProjectWithBot(server);
|
|
90
|
+
await put(ctx, 'webhook', { block_id: ctx.startIntent.intent_id });
|
|
91
|
+
const res = await put(ctx, 'scheduled', { block_id: ctx.startIntent.intent_id });
|
|
92
|
+
expect(res).to.have.status(400);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('answers 400 for an invalid schedule or payload', async () => {
|
|
96
|
+
const ctx = await setupProjectWithBot(server);
|
|
97
|
+
const block_id = ctx.startIntent.intent_id;
|
|
98
|
+
let res = await put(ctx, 'scheduled', { block_id: block_id, schedule: Object.assign({}, SCHEDULE, { timezone: 'Mars/Base' }) });
|
|
99
|
+
expect(res).to.have.status(400);
|
|
100
|
+
expect(res.body.error).to.contain('timezone');
|
|
101
|
+
res = await put(ctx, 'scheduled', { block_id: block_id, schedule: SCHEDULE, mapping: { payload: { 'a.b': 1 } } });
|
|
102
|
+
expect(res).to.have.status(400);
|
|
103
|
+
expect(res.body.error).to.contain('payload');
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('answers 503 for PUT and DELETE scheduled when not configured, while webhook still works', async () => {
|
|
107
|
+
const ctx = await setupProjectWithBot(server);
|
|
108
|
+
delete process.env.SCHEDULED_START_TOKEN;
|
|
109
|
+
|
|
110
|
+
let res = await put(ctx, 'scheduled', { block_id: ctx.startIntent.intent_id, schedule: SCHEDULE });
|
|
111
|
+
expect(res).to.have.status(503);
|
|
112
|
+
expect(res.body.error).to.equal('Scheduled starts are not available on this installation');
|
|
113
|
+
|
|
114
|
+
res = await chai.request(server).delete(url(ctx, '/start_points/scheduled')).auth(ctx.email, ctx.pwd);
|
|
115
|
+
expect(res).to.have.status(503);
|
|
116
|
+
|
|
117
|
+
res = await put(ctx, 'webhook', { block_id: ctx.startIntent.intent_id });
|
|
118
|
+
expect(res).to.have.status(200);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('reports scheduled_available on GET', async () => {
|
|
122
|
+
const ctx = await setupProjectWithBot(server);
|
|
123
|
+
await put(ctx, 'webhook', { block_id: ctx.startIntent.intent_id });
|
|
124
|
+
|
|
125
|
+
let res = await chai.request(server).get(url(ctx)).auth(ctx.email, ctx.pwd);
|
|
126
|
+
expect(res).to.have.status(200);
|
|
127
|
+
expect(res.body.scheduled_available).to.equal(true);
|
|
128
|
+
|
|
129
|
+
delete process.env.SCHEDULER_BASEURL;
|
|
130
|
+
res = await chai.request(server).get(url(ctx)).auth(ctx.email, ctx.pwd);
|
|
131
|
+
expect(res.body.scheduled_available).to.equal(false);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('reports scheduled_available on the GET 404 when the chatbot has no webhook', async () => {
|
|
135
|
+
const ctx = await setupProjectWithBot(server);
|
|
136
|
+
|
|
137
|
+
let res = await chai.request(server).get(url(ctx)).auth(ctx.email, ctx.pwd);
|
|
138
|
+
expect(res).to.have.status(404);
|
|
139
|
+
expect(res.body.success).to.equal(false);
|
|
140
|
+
expect(res.body.error).to.equal('Webhook not found for chatbot ' + ctx.chatbot_id);
|
|
141
|
+
expect(res.body.scheduled_available).to.equal(true);
|
|
142
|
+
|
|
143
|
+
delete process.env.SCHEDULER_BASEURL;
|
|
144
|
+
res = await chai.request(server).get(url(ctx)).auth(ctx.email, ctx.pwd);
|
|
145
|
+
expect(res).to.have.status(404);
|
|
146
|
+
expect(res.body.scheduled_available).to.equal(false);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('DELETE removes the draft entry and leaves scheduled_live untouched', async () => {
|
|
150
|
+
const ctx = await setupProjectWithBot(server);
|
|
151
|
+
const block_id = ctx.startIntent.intent_id;
|
|
152
|
+
await put(ctx, 'scheduled', { block_id: block_id, schedule: SCHEDULE });
|
|
153
|
+
const live = { enabled: true, block_id: block_id, workflow_code: 42, schedule_id: 7 };
|
|
154
|
+
await Webhook.updateOne({ chatbot_id: ctx.chatbot_id }, { $set: { scheduled_live: live } });
|
|
155
|
+
|
|
156
|
+
const res = await chai.request(server).delete(url(ctx, '/start_points/scheduled')).auth(ctx.email, ctx.pwd);
|
|
157
|
+
|
|
158
|
+
expect(res).to.have.status(200);
|
|
159
|
+
expect(res.body.start_points.filter((s) => s.type === 'scheduled')).to.have.length(0);
|
|
160
|
+
const stored = await Webhook.findOne({ chatbot_id: ctx.chatbot_id }).lean();
|
|
161
|
+
expect(stored.scheduled_live).to.deep.equal(live);
|
|
162
|
+
});
|
|
163
|
+
});
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
process.env.NODE_ENV = 'test';
|
|
2
|
+
|
|
3
|
+
const expect = require('chai').expect;
|
|
4
|
+
const rules = require('../services/scheduledStartRules');
|
|
5
|
+
|
|
6
|
+
const base = { timezone: 'Europe/Rome' };
|
|
7
|
+
|
|
8
|
+
describe('scheduledStartRules', function () {
|
|
9
|
+
|
|
10
|
+
describe('toQuartzCron', () => {
|
|
11
|
+
const cases = [
|
|
12
|
+
[{ frequency: 'interval', unit: 'minutes', every: 15, timezone: 'UTC' }, '0 0/15 * * * ? *'],
|
|
13
|
+
[{ frequency: 'interval', unit: 'hours', every: 2, timezone: 'UTC' }, '0 0 0/2 * * ? *'],
|
|
14
|
+
[{ frequency: 'daily', time: '09:05', timezone: 'UTC' }, '0 5 9 * * ? *'],
|
|
15
|
+
[{ frequency: 'weekly', time: '08:30', weekdays: ['fri', 'mon', 'wed'], timezone: 'UTC' }, '0 30 8 ? * MON,WED,FRI *'],
|
|
16
|
+
[{ frequency: 'monthly', time: '07:00', day_of_month: 15, timezone: 'UTC' }, '0 0 7 15 * ? *'],
|
|
17
|
+
[{ frequency: 'monthly', time: '07:00', day_of_month: 'last', timezone: 'UTC' }, '0 0 7 L * ? *']
|
|
18
|
+
];
|
|
19
|
+
cases.forEach(([schedule, cron]) => {
|
|
20
|
+
it(cron, () => expect(rules.toQuartzCron(schedule)).to.equal(cron));
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
describe('toCron5', () => {
|
|
25
|
+
const cases = [
|
|
26
|
+
[{ frequency: 'interval', unit: 'minutes', every: 15, timezone: 'UTC' }, '*/15 * * * *'],
|
|
27
|
+
[{ frequency: 'interval', unit: 'hours', every: 2, timezone: 'UTC' }, '0 */2 * * *'],
|
|
28
|
+
[{ frequency: 'daily', time: '09:30', timezone: 'UTC' }, '30 9 * * *'],
|
|
29
|
+
[{ frequency: 'weekly', time: '07:05', weekdays: ['fri', 'mon', 'sun'], timezone: 'UTC' }, '5 7 * * 1,5,0'],
|
|
30
|
+
[{ frequency: 'monthly', time: '23:00', day_of_month: 'last', timezone: 'UTC' }, '0 23 L * *'],
|
|
31
|
+
[{ frequency: 'monthly', time: '08:00', day_of_month: 15, timezone: 'UTC' }, '0 8 15 * *']
|
|
32
|
+
];
|
|
33
|
+
cases.forEach(([schedule, cron]) => {
|
|
34
|
+
it(cron, () => expect(rules.toCron5(schedule)).to.equal(cron));
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe('validateSchedule', () => {
|
|
39
|
+
const valid = [
|
|
40
|
+
{ frequency: 'interval', unit: 'minutes', every: 5 },
|
|
41
|
+
{ frequency: 'interval', unit: 'hours', every: 12 },
|
|
42
|
+
{ frequency: 'daily', time: '23:59' },
|
|
43
|
+
{ frequency: 'weekly', time: '00:00', weekdays: ['sun'] },
|
|
44
|
+
{ frequency: 'monthly', time: '10:10', day_of_month: 28 },
|
|
45
|
+
{ frequency: 'monthly', time: '10:10', day_of_month: 'last' }
|
|
46
|
+
];
|
|
47
|
+
valid.forEach((s) => {
|
|
48
|
+
it('accepts ' + JSON.stringify(s), () => expect(rules.validateSchedule(Object.assign({}, base, s))).to.equal(null));
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const invalid = [
|
|
52
|
+
[{ frequency: 'interval', unit: 'minutes', every: 7 }, 'every'],
|
|
53
|
+
[{ frequency: 'interval', unit: 'minutes', every: 60 }, 'every'],
|
|
54
|
+
[{ frequency: 'interval', unit: 'hours', every: 13 }, 'every'],
|
|
55
|
+
[{ frequency: 'interval', unit: 'hours', every: 0 }, 'every'],
|
|
56
|
+
[{ frequency: 'interval', unit: 'days', every: 1 }, 'unit'],
|
|
57
|
+
[{ frequency: 'yearly' }, 'frequency'],
|
|
58
|
+
[{ frequency: 'daily', time: '24:00' }, 'time'],
|
|
59
|
+
[{ frequency: 'daily', time: '9:00' }, 'time'],
|
|
60
|
+
[{ frequency: 'daily' }, 'time'],
|
|
61
|
+
[{ frequency: 'weekly', time: '08:00', weekdays: [] }, 'weekdays'],
|
|
62
|
+
[{ frequency: 'weekly', time: '08:00', weekdays: ['funday'] }, 'weekdays'],
|
|
63
|
+
[{ frequency: 'weekly', time: '08:00' }, 'weekdays'],
|
|
64
|
+
[{ frequency: 'monthly', time: '08:00', day_of_month: 29 }, 'day_of_month'],
|
|
65
|
+
[{ frequency: 'monthly', time: '08:00', day_of_month: 0 }, 'day_of_month'],
|
|
66
|
+
[{ frequency: 'monthly', time: '08:00' }, 'day_of_month'],
|
|
67
|
+
[{ frequency: 'daily', time: '08:00', timezone: 'Mars/Base' }, 'timezone'],
|
|
68
|
+
[{ frequency: 'daily', time: '08:00', timezone: '' }, 'timezone']
|
|
69
|
+
];
|
|
70
|
+
invalid.forEach(([s, field]) => {
|
|
71
|
+
it('rejects ' + JSON.stringify(s) + ' naming ' + field, () => {
|
|
72
|
+
const msg = rules.validateSchedule(Object.assign({}, base, s));
|
|
73
|
+
expect(msg).to.be.a('string');
|
|
74
|
+
expect(msg).to.contain(field);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('rejects a non-object', () => {
|
|
79
|
+
expect(rules.validateSchedule(null)).to.contain('schedule');
|
|
80
|
+
expect(rules.validateSchedule([])).to.contain('schedule');
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
describe('validatePayload', () => {
|
|
85
|
+
it('accepts a flat payload', () => {
|
|
86
|
+
expect(rules.validatePayload({ a: 'x', b_1: 2, _c: true })).to.equal(null);
|
|
87
|
+
expect(rules.validatePayload({})).to.equal(null);
|
|
88
|
+
});
|
|
89
|
+
it('rejects non-objects', () => {
|
|
90
|
+
expect(rules.validatePayload([1])).to.contain('payload');
|
|
91
|
+
expect(rules.validatePayload('x')).to.contain('payload');
|
|
92
|
+
});
|
|
93
|
+
it('rejects prototype-polluting keys', () => {
|
|
94
|
+
['__proto__', 'constructor', 'prototype'].forEach((k) => {
|
|
95
|
+
const payload = JSON.parse('{"' + k + '": "x"}');
|
|
96
|
+
expect(rules.validatePayload(payload)).to.contain(k);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
it('rejects bad keys', () => {
|
|
100
|
+
expect(rules.validatePayload({ 'a.b': 1 })).to.contain('a.b');
|
|
101
|
+
expect(rules.validatePayload({ '1a': 1 })).to.contain('1a');
|
|
102
|
+
});
|
|
103
|
+
it('rejects bad values', () => {
|
|
104
|
+
expect(rules.validatePayload({ a: { b: 1 } })).to.contain('value');
|
|
105
|
+
expect(rules.validatePayload({ a: null })).to.contain('value');
|
|
106
|
+
expect(rules.validatePayload({ a: Infinity })).to.contain('value');
|
|
107
|
+
});
|
|
108
|
+
it('rejects more than 50 keys', () => {
|
|
109
|
+
const p = {};
|
|
110
|
+
for (let i = 0; i < 51; i++) { p['k' + i] = 1; }
|
|
111
|
+
expect(rules.validatePayload(p)).to.contain('50');
|
|
112
|
+
});
|
|
113
|
+
it('rejects more than 8192 bytes', () => {
|
|
114
|
+
expect(rules.validatePayload({ a: 'x'.repeat(8200) })).to.contain('8192');
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
describe('summarize', () => {
|
|
119
|
+
it('describes schedules', () => {
|
|
120
|
+
expect(rules.summarize({ frequency: 'weekly', time: '08:30', weekdays: ['fri', 'mon', 'wed', 'thu', 'tue'], timezone: 'Europe/Rome' })).to.equal('Every weekday at 08:30 (Europe/Rome)');
|
|
121
|
+
expect(rules.summarize({ frequency: 'weekly', time: '08:30', weekdays: ['fri', 'mon', 'wed'], timezone: 'UTC' })).to.equal('Every Mon, Wed, Fri at 08:30 (UTC)');
|
|
122
|
+
expect(rules.summarize({ frequency: 'interval', unit: 'minutes', every: 15, timezone: 'UTC' })).to.equal('Every 15 minutes (UTC)');
|
|
123
|
+
expect(rules.summarize({ frequency: 'interval', unit: 'hours', every: 3, timezone: 'UTC' })).to.equal('Every 3 hours (UTC)');
|
|
124
|
+
expect(rules.summarize({ frequency: 'daily', time: '09:00', timezone: 'Europe/Rome' })).to.equal('Daily at 09:00 (Europe/Rome)');
|
|
125
|
+
expect(rules.summarize({ frequency: 'monthly', time: '07:00', day_of_month: 'last', timezone: 'UTC' })).to.equal('Monthly on the last day at 07:00 (UTC)');
|
|
126
|
+
expect(rules.summarize({ frequency: 'monthly', time: '07:00', day_of_month: 15, timezone: 'UTC' })).to.equal('Monthly on day 15 at 07:00 (UTC)');
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
describe('isConfigured', () => {
|
|
131
|
+
const names = ['SCHEDULER_BASEURL', 'SCHEDULER_TOKEN', 'SCHEDULER_PROJECT', 'SCHEDULED_START_TOKEN'];
|
|
132
|
+
let saved;
|
|
133
|
+
beforeEach(() => { saved = {}; names.forEach((n) => { saved[n] = process.env[n]; process.env[n] = 'x'; }); });
|
|
134
|
+
afterEach(() => { names.forEach((n) => { if (saved[n] === undefined) { delete process.env[n]; } else { process.env[n] = saved[n]; } }); });
|
|
135
|
+
|
|
136
|
+
it('is true only when all four are set', () => {
|
|
137
|
+
expect(rules.isConfigured()).to.equal(true);
|
|
138
|
+
names.forEach((n) => {
|
|
139
|
+
process.env[n] = '';
|
|
140
|
+
expect(rules.isConfigured(), n).to.equal(false);
|
|
141
|
+
process.env[n] = 'x';
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
describe('backend', () => {
|
|
147
|
+
const names = ['SCHEDULED_START_BACKEND', 'SCHEDULER_SERVICE_URL', 'SCHEDULER_API_TOKEN', 'SCHEDULED_START_TOKEN',
|
|
148
|
+
'SCHEDULER_BASEURL', 'SCHEDULER_TOKEN', 'SCHEDULER_PROJECT'];
|
|
149
|
+
let saved;
|
|
150
|
+
beforeEach(() => { saved = {}; names.forEach((n) => { saved[n] = process.env[n]; delete process.env[n]; }); });
|
|
151
|
+
afterEach(() => { names.forEach((n) => { if (saved[n] === undefined) { delete process.env[n]; } else { process.env[n] = saved[n]; } }); });
|
|
152
|
+
|
|
153
|
+
it('defaults to dolphin', () => expect(rules.backend()).to.equal('dolphin'));
|
|
154
|
+
|
|
155
|
+
it('selects the scheduler service', () => {
|
|
156
|
+
process.env.SCHEDULED_START_BACKEND = 'scheduler';
|
|
157
|
+
expect(rules.backend()).to.equal('scheduler');
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('falls back to dolphin on an unknown value', () => {
|
|
161
|
+
process.env.SCHEDULED_START_BACKEND = 'cron-o-matic';
|
|
162
|
+
expect(rules.backend()).to.equal('dolphin');
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('checks the scheduler service env when that backend is selected', () => {
|
|
166
|
+
process.env.SCHEDULED_START_BACKEND = 'scheduler';
|
|
167
|
+
const needed = ['SCHEDULER_SERVICE_URL', 'SCHEDULER_API_TOKEN', 'SCHEDULED_START_TOKEN'];
|
|
168
|
+
needed.forEach((n) => { process.env[n] = 'x'; });
|
|
169
|
+
expect(rules.isConfigured()).to.equal(true);
|
|
170
|
+
needed.forEach((n) => {
|
|
171
|
+
process.env[n] = '';
|
|
172
|
+
expect(rules.isConfigured(), n).to.equal(false);
|
|
173
|
+
process.env[n] = 'x';
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
});
|
|
@@ -0,0 +1,299 @@
|
|
|
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
|
+
const ENV_KEYS = ['SCHEDULER_BASEURL', 'SCHEDULER_TOKEN', 'SCHEDULER_PROJECT', 'SCHEDULED_START_TOKEN'];
|
|
6
|
+
const savedEnv = {};
|
|
7
|
+
ENV_KEYS.forEach((k) => { savedEnv[k] = process.env[k]; });
|
|
8
|
+
process.env.SCHEDULER_BASEURL = 'http://scheduler.test:12345/dolphinscheduler';
|
|
9
|
+
process.env.SCHEDULER_TOKEN = 'sched-token';
|
|
10
|
+
process.env.SCHEDULER_PROJECT = '1234';
|
|
11
|
+
process.env.SCHEDULED_START_TOKEN = 'run-token';
|
|
12
|
+
|
|
13
|
+
const chai = require('chai');
|
|
14
|
+
const chaiHttp = require('chai-http');
|
|
15
|
+
const server = require('../app');
|
|
16
|
+
const expect = chai.expect;
|
|
17
|
+
chai.use(chaiHttp);
|
|
18
|
+
|
|
19
|
+
const Faq_kb = require('../models/faq_kb');
|
|
20
|
+
const Request = require('../models/request');
|
|
21
|
+
const Message = require('../models/message');
|
|
22
|
+
const Lead = require('../models/lead');
|
|
23
|
+
const requestService = require('../services/requestService');
|
|
24
|
+
const scheduledStartService = require('../services/scheduledStartService');
|
|
25
|
+
const { Webhook } = require('../models/webhook');
|
|
26
|
+
const nock = require('nock');
|
|
27
|
+
const { setupProjectWithBot, waitFor } = require('./webhookConversationTestUtils');
|
|
28
|
+
|
|
29
|
+
const SCHEDULE = { frequency: 'daily', time: '09:00', timezone: 'Europe/Rome' };
|
|
30
|
+
|
|
31
|
+
function liveFor(overrides) {
|
|
32
|
+
return Object.assign({
|
|
33
|
+
enabled: true,
|
|
34
|
+
block_id: 'live-block',
|
|
35
|
+
mapping: { source_name: 'Live Source', payload: { env: 'live' } },
|
|
36
|
+
schedule: SCHEDULE,
|
|
37
|
+
published_bot_id: 'pub1',
|
|
38
|
+
workflow_code: 11,
|
|
39
|
+
schedule_id: 22
|
|
40
|
+
}, overrides);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function createWebhook(ctx, fields) {
|
|
44
|
+
return new Webhook(Object.assign({
|
|
45
|
+
id_project: ctx.project._id.toString(),
|
|
46
|
+
chatbot_id: ctx.chatbot_id,
|
|
47
|
+
block_id: 'start-intent-id',
|
|
48
|
+
mode: 'conversation',
|
|
49
|
+
enabled: false,
|
|
50
|
+
start_points: [{ type: 'scheduled', block_id: 'draft-block', enabled: true, mapping: { source_name: 'Draft Source', payload: { env: 'draft' } }, schedule: SCHEDULE }]
|
|
51
|
+
}, fields)).save();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function run(ctx, { token = 'run-token', body } = {}) {
|
|
55
|
+
const req = chai.request(server).post('/webhook/scheduled-start');
|
|
56
|
+
if (token) req.set('x-auth-token', token);
|
|
57
|
+
return req.send(body || { id_project: ctx.project._id.toString(), chatbot_id: ctx.chatbot_id });
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
describe('Scheduled start run', function () {
|
|
61
|
+
|
|
62
|
+
this.timeout(30000);
|
|
63
|
+
|
|
64
|
+
let originalDeactivate;
|
|
65
|
+
let deactivated;
|
|
66
|
+
|
|
67
|
+
beforeEach(() => {
|
|
68
|
+
originalDeactivate = scheduledStartService.deactivate;
|
|
69
|
+
deactivated = [];
|
|
70
|
+
scheduledStartService.deactivate = async (live) => { deactivated.push(live); };
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
afterEach(() => {
|
|
74
|
+
scheduledStartService.deactivate = originalDeactivate;
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
after(() => {
|
|
78
|
+
ENV_KEYS.forEach((k) => {
|
|
79
|
+
if (savedEnv[k] === undefined) delete process.env[k]; else process.env[k] = savedEnv[k];
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('401 without a token header and with a wrong token', async () => {
|
|
84
|
+
const ctx = await setupProjectWithBot(server);
|
|
85
|
+
expect(await run(ctx, { token: null })).to.have.status(401);
|
|
86
|
+
expect(await run(ctx, { token: 'wrong' })).to.have.status(401);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('503 when SCHEDULED_START_TOKEN is unset', async () => {
|
|
90
|
+
const ctx = await setupProjectWithBot(server);
|
|
91
|
+
delete process.env.SCHEDULED_START_TOKEN;
|
|
92
|
+
try {
|
|
93
|
+
const res = await run(ctx);
|
|
94
|
+
expect(res).to.have.status(503);
|
|
95
|
+
} finally {
|
|
96
|
+
process.env.SCHEDULED_START_TOKEN = 'run-token';
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('410 and workflow offline when the chatbot is trashed', async () => {
|
|
101
|
+
const ctx = await setupProjectWithBot(server);
|
|
102
|
+
const live = liveFor();
|
|
103
|
+
await createWebhook(ctx, { scheduled_live: live });
|
|
104
|
+
await Faq_kb.updateOne({ _id: ctx.chatbot_id }, { $set: { trashed: true } }).exec();
|
|
105
|
+
|
|
106
|
+
const res = await run(ctx);
|
|
107
|
+
|
|
108
|
+
expect(res).to.have.status(410);
|
|
109
|
+
expect(deactivated).to.have.length(1);
|
|
110
|
+
expect(deactivated[0].schedule_id).to.equal(22);
|
|
111
|
+
expect(await Request.countDocuments({ id_project: ctx.project._id.toString() })).to.equal(0);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('410 and a failing offline never becomes a 500', async () => {
|
|
115
|
+
const ctx = await setupProjectWithBot(server);
|
|
116
|
+
await createWebhook(ctx, { scheduled_live: liveFor() });
|
|
117
|
+
await Faq_kb.updateOne({ _id: ctx.chatbot_id }, { $set: { trashed: true } }).exec();
|
|
118
|
+
scheduledStartService.deactivate = async () => { throw new Error('scheduler down'); };
|
|
119
|
+
|
|
120
|
+
expect(await run(ctx)).to.have.status(410);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('410 and workflow offline when scheduled_live.enabled is false', async () => {
|
|
124
|
+
const ctx = await setupProjectWithBot(server);
|
|
125
|
+
await createWebhook(ctx, { scheduled_live: liveFor({ enabled: false }) });
|
|
126
|
+
|
|
127
|
+
const res = await run(ctx);
|
|
128
|
+
|
|
129
|
+
expect(res).to.have.status(410);
|
|
130
|
+
expect(deactivated).to.have.length(1);
|
|
131
|
+
expect(await Request.countDocuments({ id_project: ctx.project._id.toString() })).to.equal(0);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('410 with no live ids falls back to the workflow name and takes its schedules offline', async () => {
|
|
135
|
+
const ctx = await setupProjectWithBot(server);
|
|
136
|
+
await createWebhook(ctx, { scheduled_live: { enabled: false, error: 'boom' } });
|
|
137
|
+
const base = 'http://scheduler.test:12345/dolphinscheduler/projects/1234';
|
|
138
|
+
const ok = (data) => ({ code: 0, msg: 'success', data, success: true });
|
|
139
|
+
const s = nock('http://scheduler.test:12345');
|
|
140
|
+
s.get('/dolphinscheduler/projects/1234/process-definition').query((q) => q.searchVal === 'auto-start-' + ctx.chatbot_id)
|
|
141
|
+
.reply(200, ok({ totalList: [{ code: 888, name: 'auto-start-' + ctx.chatbot_id }] }));
|
|
142
|
+
s.get('/dolphinscheduler/projects/1234/schedules').query((q) => String(q.processDefinitionCode) === '888')
|
|
143
|
+
.reply(200, ok({ totalList: [{ id: 333 }] }));
|
|
144
|
+
const off = s.post('/dolphinscheduler/projects/1234/schedules/333/offline').reply(200, ok(true));
|
|
145
|
+
try {
|
|
146
|
+
const res = await run(ctx);
|
|
147
|
+
expect(res).to.have.status(410);
|
|
148
|
+
expect(off.isDone()).to.equal(true);
|
|
149
|
+
expect(deactivated).to.have.length(0);
|
|
150
|
+
} finally {
|
|
151
|
+
nock.cleanAll();
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('500 (not an unhandled rejection) when the rate manager throws', async () => {
|
|
156
|
+
const ctx = await setupProjectWithBot(server);
|
|
157
|
+
await createWebhook(ctx, { scheduled_live: liveFor() });
|
|
158
|
+
const rateManager = server.get('rate_manager');
|
|
159
|
+
const original = rateManager.canExecute;
|
|
160
|
+
rateManager.canExecute = async () => { throw new Error('redis down'); };
|
|
161
|
+
try {
|
|
162
|
+
const res = await run(ctx);
|
|
163
|
+
expect(res).to.have.status(500);
|
|
164
|
+
expect(res.body.success).to.equal(false);
|
|
165
|
+
} finally {
|
|
166
|
+
rateManager.canExecute = original;
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('410 without a scheduled_live (nothing to take offline)', async () => {
|
|
171
|
+
const ctx = await setupProjectWithBot(server);
|
|
172
|
+
await createWebhook(ctx);
|
|
173
|
+
|
|
174
|
+
expect(await run(ctx)).to.have.status(410);
|
|
175
|
+
expect(deactivated).to.have.length(0);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('429 when the project rate limit is exceeded', async () => {
|
|
179
|
+
const ctx = await setupProjectWithBot(server);
|
|
180
|
+
await createWebhook(ctx, { scheduled_live: liveFor() });
|
|
181
|
+
const rateManager = server.get('rate_manager');
|
|
182
|
+
const original = rateManager.canExecute;
|
|
183
|
+
rateManager.canExecute = async () => false;
|
|
184
|
+
try {
|
|
185
|
+
const res = await run(ctx);
|
|
186
|
+
expect(res).to.have.status(429);
|
|
187
|
+
expect(res.body.message).to.equal('Rate limit exceeded');
|
|
188
|
+
} finally {
|
|
189
|
+
rateManager.canExecute = original;
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it('202 starts a scheduled conversation from the live snapshot (webhook disabled, no webhook start point)', async () => {
|
|
194
|
+
const ctx = await setupProjectWithBot(server);
|
|
195
|
+
await createWebhook(ctx, { scheduled_live: liveFor() });
|
|
196
|
+
|
|
197
|
+
const res = await run(ctx);
|
|
198
|
+
|
|
199
|
+
expect(res).to.have.status(202);
|
|
200
|
+
expect(res.body.success).to.equal(true);
|
|
201
|
+
const request = await Request.findOne({ request_id: res.body.request_id, id_project: ctx.project._id.toString() }).lean().exec();
|
|
202
|
+
expect(request.channel.name).to.equal('webhook');
|
|
203
|
+
expect(request.attributes.start_type).to.equal('scheduled');
|
|
204
|
+
expect(request.attributes.payload).to.deep.equal({ env: 'live' });
|
|
205
|
+
expect(new Date(request.attributes.scheduled_at).toISOString()).to.equal(request.attributes.scheduled_at);
|
|
206
|
+
expect(request.attributes.sourcePage).to.equal(undefined);
|
|
207
|
+
expect(request.sourcePage || '').to.not.include('td_draft');
|
|
208
|
+
|
|
209
|
+
const lead = await Lead.findOne({ id_project: ctx.project._id.toString(), lead_id: 'scheduled_' + ctx.chatbot_id }).lean().exec();
|
|
210
|
+
expect(lead.fullname).to.equal('Live Source');
|
|
211
|
+
|
|
212
|
+
const message = await waitFor(() => Message.findOne({ recipient: res.body.request_id, text: '/start' }).lean().exec());
|
|
213
|
+
expect(message.attributes.action).to.equal('#live-block');
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it('names the lead after the chatbot when the live source name is empty', async () => {
|
|
217
|
+
const ctx = await setupProjectWithBot(server);
|
|
218
|
+
await createWebhook(ctx, { scheduled_live: liveFor({ mapping: { payload: {} } }) });
|
|
219
|
+
|
|
220
|
+
const res = await run(ctx);
|
|
221
|
+
|
|
222
|
+
expect(res).to.have.status(202);
|
|
223
|
+
const lead = await Lead.findOne({ id_project: ctx.project._id.toString(), lead_id: 'scheduled_' + ctx.chatbot_id }).lean().exec();
|
|
224
|
+
expect(lead.fullname).to.equal('hitl-bot');
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it('test route: 202 with the draft mapping, dev sourcePage, and no scheduled_live needed', async () => {
|
|
228
|
+
const ctx = await setupProjectWithBot(server);
|
|
229
|
+
await createWebhook(ctx);
|
|
230
|
+
|
|
231
|
+
const res = await chai.request(server)
|
|
232
|
+
.post('/' + ctx.project._id + '/webhooks/' + ctx.chatbot_id + '/start_points/scheduled/test')
|
|
233
|
+
.auth(ctx.email, ctx.pwd).send({});
|
|
234
|
+
|
|
235
|
+
expect(res).to.have.status(202);
|
|
236
|
+
const request = await Request.findOne({ request_id: res.body.request_id, id_project: ctx.project._id.toString() }).lean().exec();
|
|
237
|
+
expect(request.attributes.start_type).to.equal('scheduled');
|
|
238
|
+
expect(request.attributes.payload).to.deep.equal({ env: 'draft' });
|
|
239
|
+
expect(request.attributes.sourcePage).to.include('dev=true&td_draft=true');
|
|
240
|
+
const message = await waitFor(() => Message.findOne({ recipient: res.body.request_id, text: '/start' }).lean().exec());
|
|
241
|
+
expect(message.attributes.action).to.equal('#draft-block');
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it('test route: 404 without a scheduled draft', async () => {
|
|
245
|
+
const ctx = await setupProjectWithBot(server);
|
|
246
|
+
await createWebhook(ctx, { start_points: [] });
|
|
247
|
+
|
|
248
|
+
const res = await chai.request(server)
|
|
249
|
+
.post('/' + ctx.project._id + '/webhooks/' + ctx.chatbot_id + '/start_points/scheduled/test')
|
|
250
|
+
.auth(ctx.email, ctx.pwd).send({});
|
|
251
|
+
|
|
252
|
+
expect(res).to.have.status(404);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it('test route: 503 when the feature is not configured', async () => {
|
|
256
|
+
const ctx = await setupProjectWithBot(server);
|
|
257
|
+
delete process.env.SCHEDULER_TOKEN;
|
|
258
|
+
try {
|
|
259
|
+
const res = await chai.request(server)
|
|
260
|
+
.post('/' + ctx.project._id + '/webhooks/' + ctx.chatbot_id + '/start_points/scheduled/test')
|
|
261
|
+
.auth(ctx.email, ctx.pwd).send({});
|
|
262
|
+
expect(res).to.have.status(503);
|
|
263
|
+
} finally {
|
|
264
|
+
process.env.SCHEDULER_TOKEN = 'sched-token';
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
describe('invite human', function () {
|
|
269
|
+
async function startedConversation(channel) {
|
|
270
|
+
const ctx = await setupProjectWithBot(server);
|
|
271
|
+
const request = await requestService.create({
|
|
272
|
+
request_id: 'support-group-' + ctx.project._id + '-' + Date.now() + Math.floor(Math.random() * 1000),
|
|
273
|
+
id_project: ctx.project._id.toString(),
|
|
274
|
+
first_text: '/start',
|
|
275
|
+
participants: ['bot_' + ctx.chatbot_id],
|
|
276
|
+
channel: { name: channel },
|
|
277
|
+
preflight: false,
|
|
278
|
+
createdBy: 'system',
|
|
279
|
+
attributes: channel === 'webhook' ? { start_type: 'scheduled' } : {}
|
|
280
|
+
});
|
|
281
|
+
return { ctx, request };
|
|
282
|
+
}
|
|
283
|
+
function invite({ ctx, request }) {
|
|
284
|
+
return chai.request(server)
|
|
285
|
+
.post('/' + ctx.project._id + '/requests/' + request.request_id + '/participants/invite')
|
|
286
|
+
.auth(ctx.email, ctx.pwd).send({});
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
it('200 on a scheduled (webhook channel) conversation', async () => {
|
|
290
|
+
expect(await invite(await startedConversation('webhook'))).to.have.status(200);
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
it('422 with the new message on a widget conversation', async () => {
|
|
294
|
+
const res = await invite(await startedConversation('chat21'));
|
|
295
|
+
expect(res).to.have.status(422);
|
|
296
|
+
expect(res.body.error).to.equal('Invite human requires a conversation started by a webhook or a schedule');
|
|
297
|
+
});
|
|
298
|
+
});
|
|
299
|
+
});
|