@tiledesk/tiledesk-server 3.3.4 → 3.5.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.
Files changed (48) hide show
  1. package/CHANGELOG.md +20 -1
  2. package/app.js +7 -0
  3. package/docs/activities-client.md +27 -0
  4. package/docs/mcp-oauth2-it.md +279 -0
  5. package/docs/mcp-oauth2.md +279 -0
  6. package/docs/superpowers/plans/2026-11-10-mcp-oauth2-server-it.md +155 -0
  7. package/docs/superpowers/plans/2026-11-10-mcp-oauth2-server.md +155 -0
  8. package/docs/superpowers/plans/2026-11-10-mcp-oauth2-tybot-it.md +63 -0
  9. package/docs/superpowers/plans/2026-11-10-mcp-oauth2-tybot.md +63 -0
  10. package/docs/superpowers/specs/2026-11-10-mcp-oauth2-design-it.md +207 -0
  11. package/docs/superpowers/specs/2026-11-10-mcp-oauth2-design.md +207 -0
  12. package/jobs.js +15 -0
  13. package/models/mcpOAuthConnection.js +131 -0
  14. package/package.json +2 -2
  15. package/pubmodules/activities/activityMessageUtil.js +137 -0
  16. package/pubmodules/activities/models/activity.js +2 -0
  17. package/pubmodules/activities/routes/activity.js +30 -1
  18. package/pubmodules/activities/test/execution-filter.test.js +133 -0
  19. package/pubmodules/activities/test/execution-messages.test.js +158 -0
  20. package/pubmodules/executions-publisher/index.js +457 -0
  21. package/pubmodules/executions-publisher/test/audit-consumer.test.js +384 -0
  22. package/pubmodules/executions-publisher/test/executions-publisher.test.js +204 -0
  23. package/pubmodules/pubModulesManager.js +12 -0
  24. package/routes/executions-refire.js +148 -0
  25. package/routes/mcp.js +11 -8
  26. package/routes/mcpOAuth.js +133 -0
  27. package/routes/mcpOAuthCallback.js +75 -0
  28. package/routes/request.js +5 -0
  29. package/routes/webhook.js +120 -54
  30. package/services/mcpOAuthDiscovery.js +248 -0
  31. package/services/mcpOAuthGoogleScopes.js +55 -0
  32. package/services/mcpOAuthService.js +732 -0
  33. package/services/mcpService.js +111 -15
  34. package/services/requestService.js +4 -2
  35. package/services/webhookService.js +4 -2
  36. package/test/executions-refire.test.js +362 -0
  37. package/test/mcpOAuthDiscovery.test.js +331 -0
  38. package/test/mcpOAuthGoogleScopes.test.js +25 -0
  39. package/test/mcpOAuthRoute.test.js +455 -0
  40. package/test/mcpOAuthService.test.js +693 -0
  41. package/test/mcpOAuthUtils.test.js +348 -0
  42. package/test/mcpServiceOAuth.test.js +267 -0
  43. package/test/requestRoute.js +57 -0
  44. package/test/webhookConversationRoute.js +51 -0
  45. package/utils/logRedactor.js +4 -2
  46. package/utils/mcpOAuthCrypto.js +69 -0
  47. package/utils/mcpOAuthPkce.js +20 -0
  48. package/utils/outboundUrlGuard.js +131 -0
@@ -7,6 +7,7 @@ const winston = require('../config/winston');
7
7
  const cacheUtil = require('../utils/cacheUtil');
8
8
  const Integration = require('../models/integrations');
9
9
  const integrationEvent = require('../event/integrationEvent');
10
+ const mcpOAuthService = require('./mcpOAuthService');
10
11
 
11
12
  const NATIVE_MCP_CACHE_KEY = 'native_mcp:servers';
12
13
  const NATIVE_MCP_CONFIG_PATH = path.join(__dirname, '../config/native_mcp/native_mcp_servers.json');
@@ -79,6 +80,64 @@ class MCPService {
79
80
  return fromCustom || fromAuth;
80
81
  }
81
82
 
83
+ /**
84
+ * Reads the reference to an MCP OAuth connection from a request body or a stored entry
85
+ * @param {Object} oauth - { connection_id }
86
+ * @returns {Object|undefined} - { connection_id } or undefined when absent
87
+ */
88
+ parseOAuthReference(oauth) {
89
+ if (!oauth || typeof oauth !== 'object' || oauth.connection_id == null || oauth.connection_id === '') {
90
+ return undefined;
91
+ }
92
+ return { connection_id: String(oauth.connection_id) };
93
+ }
94
+
95
+ sameServerUrl(a, b) {
96
+ try {
97
+ return new URL(a).toString() === new URL(b).toString();
98
+ } catch (error) {
99
+ return false;
100
+ }
101
+ }
102
+
103
+ /**
104
+ * Loads an OAuth connection of the project and checks it was authorized for this MCP server,
105
+ * so a token is never sent to a server other than the one it was issued for.
106
+ * @returns {Promise<Object>} - The connection document
107
+ */
108
+ async loadOAuthConnection(projectId, connectionId, url) {
109
+ const connection = await mcpOAuthService.getConnection(projectId, connectionId);
110
+ if (!this.sameServerUrl(connection.server_url, url)) {
111
+ const error = new Error('The MCP OAuth connection was authorized for another MCP server');
112
+ error.statusCode = 400;
113
+ throw error;
114
+ }
115
+ return connection;
116
+ }
117
+
118
+ /**
119
+ * Applies customHeaders and legacy auth, then the OAuth bearer token, which overrides any Authorization header
120
+ * @param {Object} headers - HTTP headers object
121
+ * @param {Object} serverConfig - { url, projectId, auth?, customHeaders?, oauth? }
122
+ * @param {Object} options - { forceRefresh }
123
+ * @returns {Promise<Boolean>} - Whether any header was applied
124
+ */
125
+ async resolveRequestHeaders(headers, serverConfig, { forceRefresh } = {}) {
126
+ const applied = this.applyRequestHeaders(headers, serverConfig);
127
+ const connectionId = serverConfig && serverConfig.oauth && serverConfig.oauth.connection_id;
128
+ if (!connectionId) {
129
+ return applied;
130
+ }
131
+
132
+ await this.loadOAuthConnection(serverConfig.projectId, connectionId, serverConfig.url);
133
+ const token = await mcpOAuthService.getAccessToken(serverConfig.projectId, connectionId, { force: forceRefresh });
134
+ Object.keys(headers)
135
+ .filter((name) => name.toLowerCase() === 'authorization')
136
+ .forEach((name) => delete headers[name]);
137
+ headers['Authorization'] = `Bearer ${token.access_token}`;
138
+ return true;
139
+ }
140
+
82
141
  /**
83
142
  * Builds serverConfig from the client request body
84
143
  * @param {String} projectId - Tiledesk project ID
@@ -94,7 +153,8 @@ class MCPService {
94
153
 
95
154
  const hasCustomHeaders = Array.isArray(customHeaders) && customHeaders.length > 0;
96
155
  const hasAuth = auth && typeof auth === 'object' && Object.keys(auth).length > 0;
97
- const hasOAuth = oauth && typeof oauth === 'object' && (
156
+ const oauthReference = this.parseOAuthReference(oauth);
157
+ const hasOAuth = !oauthReference && oauth && typeof oauth === 'object' && (
98
158
  oauth.clientId || oauth.clientSecret || oauth.redirectUrl || oauth.scope
99
159
  );
100
160
 
@@ -110,7 +170,8 @@ class MCPService {
110
170
  url,
111
171
  projectId,
112
172
  auth: hasAuth ? auth : undefined,
113
- customHeaders: hasCustomHeaders ? customHeaders : undefined
173
+ customHeaders: hasCustomHeaders ? customHeaders : undefined,
174
+ oauth: oauthReference
114
175
  };
115
176
  }
116
177
 
@@ -292,9 +353,8 @@ class MCPService {
292
353
  }
293
354
  };
294
355
 
295
- this.applyRequestHeaders(config.headers, serverConfig);
296
-
297
356
  try {
357
+ await this.resolveRequestHeaders(config.headers, serverConfig);
298
358
  const response = await axios(config);
299
359
 
300
360
  if (response.status >= 400) {
@@ -341,10 +401,17 @@ class MCPService {
341
401
  config.headers['mcp-session-id'] = sessionId;
342
402
  }
343
403
 
344
- this.applyRequestHeaders(config.headers, serverConfig);
404
+ await this.resolveRequestHeaders(config.headers, serverConfig);
345
405
 
346
406
  try {
347
- const response = await axios(config);
407
+ let response = await axios(config);
408
+
409
+ // An expired or revoked access token: refresh it once and retry, never loop.
410
+ if (response.status === 401 && serverConfig && serverConfig.oauth) {
411
+ winston.debug(`MCP server ${url} answered 401: refreshing the OAuth token and retrying once`);
412
+ await this.resolveRequestHeaders(config.headers, serverConfig, { forceRefresh: true });
413
+ response = await axios(config);
414
+ }
348
415
 
349
416
  if (response.status >= 400) {
350
417
  const details = response.data ? ` - ${JSON.stringify(response.data)}` : '';
@@ -382,6 +449,10 @@ class MCPService {
382
449
 
383
450
  return responseData;
384
451
  } catch (error) {
452
+ if (error.status || error.statusCode) {
453
+ // MCP OAuth errors (not found, needs reauthorization, provider unavailable) keep their status
454
+ throw error;
455
+ }
385
456
  if (error.response) {
386
457
  // HTTP error
387
458
  const status = error.response.status;
@@ -401,13 +472,19 @@ class MCPService {
401
472
  }
402
473
 
403
474
  /**
404
- * Generates a unique cache key based on URL and projectId
475
+ * Generates a unique cache key based on URL, projectId and OAuth connection
405
476
  * @param {String} url - MCP server URL
406
477
  * @param {String} projectId - Project ID (optional)
478
+ * @param {String} connectionId - MCP OAuth connection ID (optional)
407
479
  * @returns {String} - Unique cache key
408
480
  */
409
- getCacheKey(url, projectId) {
410
- return projectId ? `${projectId}_${url}` : url;
481
+ getCacheKey(url, projectId, connectionId) {
482
+ const key = projectId ? `${projectId}_${url}` : url;
483
+ return connectionId ? `${key}_oauth_${connectionId}` : key;
484
+ }
485
+
486
+ getServerCacheKey(serverConfig) {
487
+ return this.getCacheKey(serverConfig.url, serverConfig.projectId, serverConfig.oauth && serverConfig.oauth.connection_id);
411
488
  }
412
489
 
413
490
  async getNativeServersCatalog() {
@@ -521,8 +598,15 @@ class MCPService {
521
598
  }
522
599
 
523
600
  const type = serverEntry.type || (serverEntry.nativeId ? 'native' : 'custom');
601
+ const oauthReference = this.parseOAuthReference(serverEntry.oauth);
524
602
 
525
603
  if (type === 'native') {
604
+ if (oauthReference) {
605
+ // Native server URLs are never exposed, so they cannot be authorized through /mcp/oauth/authorize.
606
+ const error = new Error('OAuth connections are not supported for native MCP servers');
607
+ error.statusCode = 400;
608
+ throw error;
609
+ }
526
610
  if (!serverEntry.nativeId) {
527
611
  throw new Error("Missing required field 'nativeId'");
528
612
  }
@@ -554,16 +638,23 @@ class MCPService {
554
638
  throw new Error("Missing required field 'url'");
555
639
  }
556
640
 
641
+ let oauth;
642
+ if (oauthReference) {
643
+ const connection = await this.loadOAuthConnection(projectId, oauthReference.connection_id, serverEntry.url);
644
+ oauth = { connection_id: oauthReference.connection_id, mode: connection.mode };
645
+ }
646
+
557
647
  let tools = serverEntry.tools;
558
648
  if (!tools || !tools.length) {
559
649
  tools = await this.listTools({
560
650
  url: serverEntry.url,
561
651
  projectId,
562
- auth: serverEntry.auth || undefined
652
+ auth: serverEntry.auth || undefined,
653
+ oauth: oauthReference
563
654
  });
564
655
  }
565
656
 
566
- return {
657
+ const entry = {
567
658
  type: 'custom',
568
659
  name: serverEntry.name || serverEntry.url,
569
660
  url: serverEntry.url,
@@ -572,6 +663,10 @@ class MCPService {
572
663
  tools: this.formatToolsForIntegration(tools),
573
664
  selectedTools: this.formatSelectedTools(serverEntry.selectedTools)
574
665
  };
666
+ if (oauth) {
667
+ entry.oauth = oauth;
668
+ }
669
+ return entry;
575
670
  }
576
671
 
577
672
  async upsertMcpServerInIntegration(projectId, serverEntry) {
@@ -637,7 +732,7 @@ class MCPService {
637
732
  throw new Error('Server MCP configuration is missing or invalid');
638
733
  }
639
734
 
640
- const serverId = this.getCacheKey(serverConfig.url, serverConfig.projectId);
735
+ const serverId = this.getServerCacheKey(serverConfig);
641
736
 
642
737
  try {
643
738
  // Initialize session (some MCP servers require session ID)
@@ -696,7 +791,7 @@ class MCPService {
696
791
  throw new Error('Server MCP configuration is missing or invalid');
697
792
  }
698
793
 
699
- const serverId = this.getCacheKey(serverConfig.url, serverConfig.projectId);
794
+ const serverId = this.getServerCacheKey(serverConfig);
700
795
 
701
796
  try {
702
797
  winston.info(`MCP listTools request for ${serverId}`);
@@ -749,9 +844,10 @@ class MCPService {
749
844
  * Removes a connection from cache
750
845
  * @param {String} url - MCP server URL
751
846
  * @param {String} projectId - Project ID (optional)
847
+ * @param {String} connectionId - MCP OAuth connection ID (optional)
752
848
  */
753
- removeConnection(url, projectId) {
754
- const serverId = this.getCacheKey(url, projectId);
849
+ removeConnection(url, projectId, connectionId) {
850
+ const serverId = this.getCacheKey(url, projectId, connectionId);
755
851
  this.connections.delete(serverId);
756
852
  }
757
853
 
@@ -1758,7 +1758,8 @@ class RequestService {
1758
1758
  beforeRequest: request,
1759
1759
  removedParticipants: removedParticipants,
1760
1760
  addedParticipants: addedParticipants,
1761
- request: requestComplete
1761
+ request: requestComplete,
1762
+ reason: options && options.reason
1762
1763
  });
1763
1764
 
1764
1765
  that.emitAssignedEvent(requestComplete, {
@@ -2600,7 +2601,8 @@ class RequestService {
2600
2601
  beforeRequest,
2601
2602
  removedParticipants,
2602
2603
  addedParticipants,
2603
- request: requestComplete
2604
+ request: requestComplete,
2605
+ reason: options && options.reason
2604
2606
  });
2605
2607
 
2606
2608
  that.emitAssignedEvent(requestComplete, {
@@ -16,7 +16,9 @@ winston.info("[RequestService] TILEBOT_ENDPOINT: " + TILEBOT_ENDPOINT);
16
16
 
17
17
  class WebhookService {
18
18
 
19
- async run(webhook, payload, dev, redis_client) {
19
+ // options.forceAsync: tybot answers once the run is handed over, never after the flow (the executions refire:
20
+ // a sync automation webhook would block the caller past its timeout and the run could start twice).
21
+ async run(webhook, payload, dev, redis_client, options) {
20
22
 
21
23
  return new Promise(async (resolve, reject) => {
22
24
 
@@ -56,7 +58,7 @@ class WebhookService {
56
58
  let url = TILEBOT_ENDPOINT + 'block/' + webhook.id_project + "/" + chatbot_id + "/" + webhook.block_id;
57
59
  winston.verbose("Webhook chatbot URL: " + url);
58
60
 
59
- payload.async = webhook.async;
61
+ payload.async = (options && options.forceAsync === true) ? true : webhook.async;
60
62
  payload.token = token;
61
63
 
62
64
  if (process.env.NODE_ENV === 'test') {
@@ -0,0 +1,362 @@
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
+ // Only EXECUTIONS_AMQP_URL is touched (set in before, restored in after): the refire needs no scheduler env,
6
+ // and other files in the same run set theirs at load time.
7
+ const savedAmqpUrl = process.env.EXECUTIONS_AMQP_URL;
8
+
9
+ const chai = require('chai');
10
+ const chaiHttp = require('chai-http');
11
+ const server = require('../app');
12
+ const expect = chai.expect;
13
+ chai.use(chaiHttp);
14
+
15
+ const express = require('express');
16
+ const bodyParser = require('body-parser');
17
+ const passport = require('passport');
18
+
19
+ const Request = require('../models/request');
20
+ const Project_user = require('../models/project_user');
21
+ const Role = require('../models/role');
22
+ const RoleConstants = require('../models/roleConstants');
23
+ const { Webhook } = require('../models/webhook');
24
+ const webhookService = require('../services/webhookService');
25
+ const scheduledStartService = require('../services/scheduledStartService');
26
+ const executionsRefire = require('../routes/executions-refire');
27
+ const userService = require('../services/userService');
28
+ const { setupProjectWithBot, uniqueEmail } = require('./webhookConversationTestUtils');
29
+
30
+ const SCHEDULE = { frequency: 'daily', time: '09:00', timezone: 'Europe/Rome' };
31
+
32
+ // The same mount app.js does (mountIfEnabled reads EXECUTIONS_AMQP_URL when it is called), on a
33
+ // fresh express app: the shared server was loaded once, possibly before this file set the env.
34
+ function buildApp() {
35
+ const app = express();
36
+ app.set('rate_manager', server.get('rate_manager'));
37
+ app.set('redis_client', server.get('redis_client'));
38
+ app.use(bodyParser.json({ limit: process.env.JSON_BODY_LIMIT || '500KB' }));
39
+ app.use(passport.initialize());
40
+ executionsRefire.mountIfEnabled(app);
41
+ return app;
42
+ }
43
+
44
+ function liveFor(overrides) {
45
+ return Object.assign({
46
+ enabled: true,
47
+ block_id: 'live-block',
48
+ mapping: { source_name: 'Live Source', payload: { env: 'live' } },
49
+ schedule: SCHEDULE,
50
+ published_bot_id: 'pub1',
51
+ workflow_code: 11,
52
+ schedule_id: 22
53
+ }, overrides);
54
+ }
55
+
56
+ function saveWebhook(ctx, fields) {
57
+ return new Webhook(Object.assign({
58
+ id_project: ctx.project._id.toString(),
59
+ chatbot_id: ctx.chatbot_id,
60
+ block_id: 'start-intent-id'
61
+ }, fields)).save();
62
+ }
63
+
64
+ function refire(app, ctx, body, auth) {
65
+ const req = chai.request(app).post('/' + ctx.project._id + '/executions/refire');
66
+ if (auth && auth.jwt) {
67
+ req.set('Authorization', auth.jwt);
68
+ } else {
69
+ req.auth(ctx.email, ctx.pwd);
70
+ }
71
+ return req.send(body);
72
+ }
73
+
74
+ function body(ctx, trigger, extra) {
75
+ return Object.assign({ chatbot_id: ctx.chatbot_id, trigger: trigger, payload: { alert: 'again' }, retry_of: 'exec-old-1' }, extra);
76
+ }
77
+
78
+ describe('Executions refire route', function () {
79
+
80
+ this.timeout(30000);
81
+
82
+ let app;
83
+ let originalDeactivate;
84
+ let deactivated;
85
+
86
+ before(() => {
87
+ process.env.EXECUTIONS_AMQP_URL = 'amqp://unused.invalid';
88
+ app = buildApp();
89
+ });
90
+
91
+ beforeEach(() => {
92
+ originalDeactivate = scheduledStartService.deactivate;
93
+ deactivated = [];
94
+ scheduledStartService.deactivate = async (live) => { deactivated.push(live); };
95
+ });
96
+
97
+ afterEach(() => {
98
+ scheduledStartService.deactivate = originalDeactivate;
99
+ });
100
+
101
+ after(() => {
102
+ if (savedAmqpUrl === undefined) delete process.env.EXECUTIONS_AMQP_URL; else process.env.EXECUTIONS_AMQP_URL = savedAmqpUrl;
103
+ });
104
+
105
+ it('route absent without EXECUTIONS_AMQP_URL', async () => {
106
+ const ctx = await setupProjectWithBot(server, { waitStart: false });
107
+ delete process.env.EXECUTIONS_AMQP_URL;
108
+ let offApp;
109
+ try {
110
+ offApp = buildApp();
111
+ } finally {
112
+ process.env.EXECUTIONS_AMQP_URL = 'amqp://unused.invalid';
113
+ }
114
+ const res = await refire(offApp, ctx, body(ctx, { type: 'scheduled' }));
115
+ expect(res).to.have.status(404);
116
+ });
117
+
118
+ it('refire scheduled creates a conversation with retry_of', async () => {
119
+ const ctx = await setupProjectWithBot(server, { waitStart: false });
120
+ await saveWebhook(ctx, { mode: 'conversation', enabled: false, scheduled_live: liveFor() });
121
+
122
+ const res = await refire(app, ctx, body(ctx, { type: 'scheduled', source_id: ctx.chatbot_id, start_point_id: null }));
123
+
124
+ expect(res).to.have.status(202);
125
+ expect(res.body.request_id).to.be.a('string');
126
+ const request = await Request.findOne({ request_id: res.body.request_id, id_project: ctx.project._id.toString() }).lean().exec();
127
+ expect(request.attributes.start_type).to.equal('scheduled');
128
+ expect(request.attributes.retry_of).to.equal('exec-old-1');
129
+ // the caller's input (U2), not the live mapping's payload
130
+ expect(request.attributes.payload).to.deep.equal({ alert: 'again' });
131
+ // a user refire never takes the scheduler workflow offline
132
+ expect(deactivated).to.have.length(0);
133
+ });
134
+
135
+ it('refire scheduled with the user JWT', async () => {
136
+ const ctx = await setupProjectWithBot(server, { waitStart: false });
137
+ await saveWebhook(ctx, { mode: 'conversation', enabled: false, scheduled_live: liveFor() });
138
+ const signin = await chai.request(server).post('/auth/signin').send({ email: ctx.email, password: ctx.pwd });
139
+ expect(signin).to.have.status(200);
140
+
141
+ const res = await refire(app, ctx, body(ctx, { type: 'scheduled' }), { jwt: signin.body.token });
142
+
143
+ expect(res).to.have.status(202);
144
+ expect(res.body.request_id).to.be.a('string');
145
+ });
146
+
147
+ it('refire scheduled: 410 when the scheduled start is gone, nothing taken offline', async () => {
148
+ const ctx = await setupProjectWithBot(server, { waitStart: false });
149
+ await saveWebhook(ctx, { mode: 'conversation', scheduled_live: liveFor({ enabled: false }) });
150
+
151
+ const res = await refire(app, ctx, body(ctx, { type: 'scheduled' }));
152
+
153
+ expect(res).to.have.status(410);
154
+ expect(deactivated).to.have.length(0);
155
+ });
156
+
157
+ it('refire webhook uses the stored webhook', async () => {
158
+ const ctx = await setupProjectWithBot(server);
159
+ const webhook = await saveWebhook(ctx, {
160
+ mode: 'conversation',
161
+ start_points: [{ type: 'webhook', block_id: ctx.startIntent.intent_id, enabled: true, mapping: { source_name: 'Grafana' } }]
162
+ });
163
+
164
+ const res = await refire(app, ctx, body(ctx, { type: 'webhook', source_id: webhook.webhook_id, start_point_id: null }));
165
+
166
+ expect(res).to.have.status(202);
167
+ expect(res.body.request_id).to.be.a('string');
168
+ const request = await Request.findOne({ request_id: res.body.request_id }).lean().exec();
169
+ expect(request.attributes.start_type).to.equal('webhook');
170
+ expect(request.attributes.webhook_id).to.equal(webhook.webhook_id);
171
+ expect(request.attributes.retry_of).to.equal('exec-old-1');
172
+ expect(request.attributes.payload).to.deep.equal({ alert: 'again' });
173
+ });
174
+
175
+ it('refire webhook: 429 when the project rate limit is exceeded', async () => {
176
+ const ctx = await setupProjectWithBot(server);
177
+ const webhook = await saveWebhook(ctx, {
178
+ mode: 'conversation',
179
+ start_points: [{ type: 'webhook', block_id: ctx.startIntent.intent_id, enabled: true }]
180
+ });
181
+ const rateManager = server.get('rate_manager');
182
+ const original = rateManager.canExecute;
183
+ rateManager.canExecute = async () => false;
184
+ try {
185
+ const res = await refire(app, ctx, body(ctx, { type: 'webhook', source_id: webhook.webhook_id }));
186
+ expect(res).to.have.status(429);
187
+ } finally {
188
+ rateManager.canExecute = original;
189
+ }
190
+ });
191
+
192
+ it('refire webhook: 404 webhook_not_found for an unknown source_id', async () => {
193
+ const ctx = await setupProjectWithBot(server, { waitStart: false });
194
+ const res = await refire(app, ctx, body(ctx, { type: 'webhook', source_id: 'nope' }));
195
+ expect(res).to.have.status(404);
196
+ expect(res.body.error).to.equal('webhook_not_found');
197
+ });
198
+
199
+ describe('automation', function () {
200
+ let originalRun;
201
+ let calls;
202
+
203
+ beforeEach(() => {
204
+ originalRun = webhookService.run;
205
+ calls = [];
206
+ webhookService.run = function (webhook, payload, dev, redis_client, options) {
207
+ calls.push({ webhook: webhook, payload: payload, dev: dev, options: options });
208
+ return originalRun.call(webhookService, webhook, payload, dev, redis_client, options);
209
+ };
210
+ });
211
+
212
+ afterEach(() => {
213
+ webhookService.run = originalRun;
214
+ });
215
+
216
+ it('refire automation calls webhookService.run with retry_of', async () => {
217
+ const ctx = await setupProjectWithBot(server, { waitStart: false });
218
+ const webhook = await saveWebhook(ctx, { block_id: 'automation-block' });
219
+
220
+ const res = await refire(app, ctx, body(ctx, { type: 'automation', source_id: webhook.webhook_id, start_point_id: null }));
221
+
222
+ expect(res).to.have.status(202);
223
+ expect(calls).to.have.length(1);
224
+ expect(calls[0].webhook.webhook_id).to.equal(webhook.webhook_id);
225
+ expect(calls[0].dev).to.not.equal(true);
226
+ expect(calls[0].payload.alert).to.equal('again');
227
+ expect(calls[0].payload.retry_of).to.equal('exec-old-1');
228
+ expect(res.body.request_id).to.be.a('string');
229
+ expect(res.body.request_id).to.equal(calls[0].payload.preloaded_request_id);
230
+ expect(res.body.request_id.endsWith('-' + webhook.webhook_id)).to.equal(true);
231
+ });
232
+
233
+ it('refire automation never waits for the flow: async forced even on a sync webhook (I2)', async () => {
234
+ const ctx = await setupProjectWithBot(server, { waitStart: false });
235
+ const webhook = await saveWebhook(ctx, { block_id: 'automation-block', async: false });
236
+ const res = await refire(app, ctx, body(ctx, { type: 'automation', source_id: webhook.webhook_id }));
237
+ expect(res).to.have.status(202);
238
+ expect(calls).to.have.length(1);
239
+ expect(calls[0].payload.async).to.equal(true);
240
+ });
241
+
242
+ it('refire automation strips the server-added keys from the confirmed payload (M2)', async () => {
243
+ const ctx = await setupProjectWithBot(server, { waitStart: false });
244
+ const webhook = await saveWebhook(ctx, { block_id: 'automation-block' });
245
+ const payload = {
246
+ alert: 'again', request_id: 'old-req', preloaded_request_id: 'old-pre', token: 'old-token', async: false, draft: true,
247
+ webhook_http_method: 'POST', webhook_query_params: { env: 'prod' }
248
+ };
249
+ const res = await refire(app, ctx, body(ctx, { type: 'automation', source_id: webhook.webhook_id }, { payload: payload }));
250
+ expect(res).to.have.status(202);
251
+ const p = calls[0].payload;
252
+ expect(p.alert).to.equal('again');
253
+ expect(p.request_id).to.not.equal('old-req');
254
+ expect(p.request_id.startsWith('automation-request-')).to.equal(true);
255
+ expect(p.preloaded_request_id).to.not.equal('old-pre');
256
+ expect(p.token).to.not.equal('old-token');
257
+ expect(p.async).to.equal(true);
258
+ expect(p).to.not.have.property('draft');
259
+ expect(p.webhook_http_method).to.equal('POST');
260
+ expect(p.webhook_query_params).to.deep.equal({ env: 'prod' });
261
+ });
262
+
263
+ it('unknown webhook -> 404 webhook_not_found', async () => {
264
+ const ctx = await setupProjectWithBot(server, { waitStart: false });
265
+ const res = await refire(app, ctx, body(ctx, { type: 'automation', source_id: 'missing-webhook' }));
266
+ expect(res).to.have.status(404);
267
+ expect(res.body.error).to.equal('webhook_not_found');
268
+ expect(calls).to.have.length(0);
269
+ });
270
+
271
+ it('a webhook of another project is not found', async () => {
272
+ const ctx = await setupProjectWithBot(server, { waitStart: false });
273
+ const other = await setupProjectWithBot(server, { waitStart: false });
274
+ const webhook = await saveWebhook(other, { block_id: 'automation-block' });
275
+ const res = await refire(app, ctx, body(ctx, { type: 'automation', source_id: webhook.webhook_id }));
276
+ expect(res).to.have.status(404);
277
+ expect(res.body.error).to.equal('webhook_not_found');
278
+ expect(calls).to.have.length(0);
279
+ });
280
+ });
281
+
282
+ it('a trigger type that does not match the webhook mode -> 409 trigger_mismatch', async () => {
283
+ const ctx = await setupProjectWithBot(server, { waitStart: false });
284
+ const webhook = await saveWebhook(ctx, { block_id: 'automation-block' });
285
+ const res = await refire(app, ctx, body(ctx, { type: 'webhook', source_id: webhook.webhook_id }));
286
+ expect(res).to.have.status(409);
287
+ expect(res.body.error).to.equal('trigger_mismatch');
288
+ });
289
+
290
+ it('chatbot of another project -> 404', async () => {
291
+ const ctx = await setupProjectWithBot(server, { waitStart: false });
292
+ const other = await setupProjectWithBot(server, { waitStart: false });
293
+ const res = await refire(app, ctx, body(ctx, { type: 'scheduled' }, { chatbot_id: other.chatbot_id }));
294
+ expect(res).to.have.status(404);
295
+ expect(res.body.error).to.equal('chatbot_not_found');
296
+ });
297
+
298
+ it('unknown trigger type -> 400', async () => {
299
+ const ctx = await setupProjectWithBot(server, { waitStart: false });
300
+ for (const trigger of [{ type: 'chat' }, { type: 'trigger' }, undefined]) {
301
+ const res = await refire(app, ctx, body(ctx, trigger));
302
+ expect(res).to.have.status(400);
303
+ }
304
+ });
305
+
306
+ // A second member with the given role (a fresh user: no project_user cached by earlier calls).
307
+ async function member(ctx, role) {
308
+ const email = uniqueEmail('test-refire-member');
309
+ const user = await userService.signup(email, ctx.pwd, 'Member', 'Refire');
310
+ await new Project_user({ id_project: ctx.project._id, id_user: user._id, role: role, status: 'active', user_available: true, createdBy: ctx.user._id.toString() }).save();
311
+ return Object.assign({}, ctx, { email: email, user: user });
312
+ }
313
+
314
+ it('agent role -> 403', async () => {
315
+ const ctx = await setupProjectWithBot(server, { waitStart: false });
316
+ await saveWebhook(ctx, { mode: 'conversation', scheduled_live: liveFor() });
317
+ const agent = await member(ctx, RoleConstants.AGENT);
318
+ const res = await refire(app, agent, body(ctx, { type: 'scheduled' }));
319
+ expect(res).to.have.status(403);
320
+ expect(await Request.countDocuments({ id_project: ctx.project._id.toString() })).to.equal(0);
321
+ });
322
+
323
+ it('supervisor role -> 403', async () => {
324
+ const ctx = await setupProjectWithBot(server, { waitStart: false });
325
+ await saveWebhook(ctx, { mode: 'conversation', scheduled_live: liveFor() });
326
+ const supervisor = await member(ctx, 'supervisor');
327
+ expect(await refire(app, supervisor, body(ctx, { type: 'scheduled' }))).to.have.status(403);
328
+ });
329
+
330
+ it('admin role is accepted', async () => {
331
+ const ctx = await setupProjectWithBot(server, { waitStart: false });
332
+ await saveWebhook(ctx, { mode: 'conversation', scheduled_live: liveFor() });
333
+ const admin = await member(ctx, RoleConstants.ADMIN);
334
+ expect(await refire(app, admin, body(ctx, { type: 'scheduled' }))).to.have.status(202);
335
+ });
336
+
337
+ it('custom role: 403 without executions_control, accepted with it', async () => {
338
+ const ctx = await setupProjectWithBot(server, { waitStart: false });
339
+ await saveWebhook(ctx, { mode: 'conversation', scheduled_live: liveFor() });
340
+ const pid = ctx.project._id.toString();
341
+ await new Role({ name: 'ops_viewer', id_project: pid, permissions: ['executions_audit_read'] }).save();
342
+ await new Role({ name: 'ops_operator', id_project: pid, permissions: ['executions_control'] }).save();
343
+
344
+ const viewer = await member(ctx, 'ops_viewer');
345
+ expect(await refire(app, viewer, body(ctx, { type: 'scheduled' }))).to.have.status(403);
346
+
347
+ const operator = await member(ctx, 'ops_operator');
348
+ const res = await refire(app, operator, body(ctx, { type: 'scheduled' }));
349
+ expect(res).to.have.status(202);
350
+ expect(res.body.request_id).to.be.a('string');
351
+ });
352
+
353
+ it('the server parser accepts a ~300 KB refire body (no 413)', async () => {
354
+ const ctx = await setupProjectWithBot(server, { waitStart: false });
355
+ const big = { blob: 'x'.repeat(300 * 1024) };
356
+ const res = await chai.request(server).post('/' + ctx.project._id + '/executions/refire')
357
+ .auth(ctx.email, ctx.pwd).send(body(ctx, { type: 'scheduled' }, { payload: big }));
358
+ expect(res.status).to.not.equal(413);
359
+ const viaRoute = await refire(app, ctx, body(ctx, { type: 'scheduled' }, { payload: big }));
360
+ expect(viaRoute.status).to.not.equal(413);
361
+ });
362
+ });