@app-connect/core 1.7.21 → 1.7.22

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 (42) hide show
  1. package/README.md +8 -1
  2. package/connector/developerPortal.js +4 -4
  3. package/docs/README.md +50 -0
  4. package/docs/architecture.md +93 -0
  5. package/docs/connectors.md +117 -0
  6. package/docs/handlers.md +125 -0
  7. package/docs/libraries.md +101 -0
  8. package/docs/models.md +144 -0
  9. package/docs/routes.md +115 -0
  10. package/docs/tests.md +73 -0
  11. package/handlers/admin.js +22 -2
  12. package/handlers/auth.js +51 -10
  13. package/handlers/log.js +4 -4
  14. package/handlers/managedAuth.js +446 -0
  15. package/index.js +264 -34
  16. package/lib/jwt.js +1 -1
  17. package/mcp/tools/createCallLog.js +5 -1
  18. package/mcp/tools/createContact.js +5 -1
  19. package/mcp/tools/createMessageLog.js +5 -1
  20. package/mcp/tools/findContactByName.js +5 -1
  21. package/mcp/tools/findContactByPhone.js +6 -2
  22. package/mcp/tools/getCallLog.js +5 -1
  23. package/mcp/tools/rcGetCallLogs.js +6 -2
  24. package/mcp/tools/updateCallLog.js +5 -1
  25. package/mcp/ui/App/lib/developerPortal.ts +1 -1
  26. package/package.json +72 -72
  27. package/releaseNotes.json +8 -0
  28. package/test/handlers/admin.test.js +34 -0
  29. package/test/handlers/auth.test.js +402 -6
  30. package/test/handlers/managedAuth.test.js +458 -0
  31. package/test/index.test.js +105 -0
  32. package/test/lib/jwt.test.js +15 -0
  33. package/test/mcp/tools/createCallLog.test.js +11 -0
  34. package/test/mcp/tools/createContact.test.js +58 -0
  35. package/test/mcp/tools/createMessageLog.test.js +15 -0
  36. package/test/mcp/tools/findContactByName.test.js +12 -0
  37. package/test/mcp/tools/findContactByPhone.test.js +12 -0
  38. package/test/mcp/tools/getCallLog.test.js +12 -0
  39. package/test/mcp/tools/rcGetCallLogs.test.js +56 -0
  40. package/test/mcp/tools/updateCallLog.test.js +14 -0
  41. package/test/routes/managedAuthRoutes.test.js +132 -0
  42. package/test/setup.js +2 -0
@@ -0,0 +1,458 @@
1
+ // Use in-memory SQLite for isolated model tests
2
+ jest.mock('../../models/sequelize', () => {
3
+ const { Sequelize } = require('sequelize');
4
+ return {
5
+ sequelize: new Sequelize({
6
+ dialect: 'sqlite',
7
+ storage: ':memory:',
8
+ logging: false,
9
+ }),
10
+ };
11
+ });
12
+
13
+ jest.mock('../../connector/registry');
14
+ jest.mock('../../connector/developerPortal', () => ({
15
+ getConnectorManifest: jest.fn()
16
+ }));
17
+
18
+ const managedAuthHandler = require('../../handlers/managedAuth');
19
+ const connectorRegistry = require('../../connector/registry');
20
+ const developerPortal = require('../../connector/developerPortal');
21
+ const { AccountDataModel } = require('../../models/accountDataModel');
22
+ const { sequelize } = require('../../models/sequelize');
23
+
24
+ describe('Managed Auth Handler', () => {
25
+ beforeAll(async () => {
26
+ process.env.APP_SERVER_SECRET_KEY = 'test-app-server-secret-key-123456';
27
+ await AccountDataModel.sync({ force: true });
28
+ });
29
+
30
+ afterEach(async () => {
31
+ await AccountDataModel.destroy({ where: {} });
32
+ jest.clearAllMocks();
33
+ });
34
+
35
+ afterAll(async () => {
36
+ await sequelize.close();
37
+ });
38
+
39
+ test('getManagedAuthState reports all required fields satisfied when shared values exist', async () => {
40
+ connectorRegistry.getManifest.mockReturnValue({
41
+ platforms: {
42
+ testCRM: {
43
+ auth: {
44
+ type: 'apiKey',
45
+ apiKey: {
46
+ page: {
47
+ content: [
48
+ { const: 'tenantId', required: true, managed: true, managedScope: 'account' },
49
+ { const: 'apiKey', required: true, managed: true, managedScope: 'user' }
50
+ ]
51
+ }
52
+ }
53
+ }
54
+ }
55
+ }
56
+ });
57
+
58
+ await managedAuthHandler.upsertOrgManagedAuthValues({
59
+ rcAccountId: 'acc-1',
60
+ platform: 'testCRM',
61
+ values: { tenantId: 'tenant-1' }
62
+ });
63
+ await managedAuthHandler.upsertUserManagedAuthValues({
64
+ rcAccountId: 'acc-1',
65
+ platform: 'testCRM',
66
+ rcExtensionId: '101',
67
+ rcUserName: 'Agent 101',
68
+ values: { apiKey: 'user-api-key' }
69
+ });
70
+
71
+ const state = await managedAuthHandler.getManagedAuthState({
72
+ platform: 'testCRM',
73
+ rcAccountId: 'acc-1',
74
+ rcExtensionId: '101'
75
+ });
76
+
77
+ expect(state.hasManagedAuth).toBe(true);
78
+ expect(state.allRequiredFieldsSatisfied).toBe(true);
79
+ expect(state.visibleFieldConsts).toEqual([]);
80
+ });
81
+
82
+ test('getManagedAuthAdminSettings returns configured field values and keeps user records separate', async () => {
83
+ connectorRegistry.getManifest.mockReturnValue({
84
+ platforms: {
85
+ testCRM: {
86
+ auth: {
87
+ type: 'apiKey',
88
+ apiKey: {
89
+ page: {
90
+ content: [
91
+ { const: 'tenantId', managed: true, managedScope: 'account' },
92
+ { const: 'apiKey', managed: true, managedScope: 'user' }
93
+ ]
94
+ }
95
+ }
96
+ }
97
+ }
98
+ }
99
+ });
100
+
101
+ await managedAuthHandler.upsertOrgManagedAuthValues({
102
+ rcAccountId: 'acc-2',
103
+ platform: 'testCRM',
104
+ values: { tenantId: 'tenant-secret' }
105
+ });
106
+ await managedAuthHandler.upsertUserManagedAuthValues({
107
+ rcAccountId: 'acc-2',
108
+ platform: 'testCRM',
109
+ rcExtensionId: '102',
110
+ rcUserName: 'Agent 102',
111
+ values: { apiKey: 'user-key' }
112
+ });
113
+
114
+ const settings = await managedAuthHandler.getManagedAuthAdminSettings({
115
+ platform: 'testCRM',
116
+ rcAccountId: 'acc-2'
117
+ });
118
+
119
+ expect(settings.orgValues.tenantId.hasValue).toBe(true);
120
+ expect(settings.orgValues.tenantId.value).toBe('tenant-secret');
121
+ expect(settings.userValues[0].rcExtensionId).toBe('102');
122
+ expect(settings.userValues[0].fields.apiKey.value).toBe('user-key');
123
+ });
124
+
125
+ test('upsertUserManagedAuthValues stores one row per extension with scoped dataKey', async () => {
126
+ await managedAuthHandler.upsertUserManagedAuthValues({
127
+ rcAccountId: 'acc-scope',
128
+ platform: 'testCRM',
129
+ rcExtensionId: '201',
130
+ rcUserName: 'Agent 201',
131
+ values: { apiKey: 'key-201' }
132
+ });
133
+ await managedAuthHandler.upsertUserManagedAuthValues({
134
+ rcAccountId: 'acc-scope',
135
+ platform: 'testCRM',
136
+ rcExtensionId: '202',
137
+ rcUserName: 'Agent 202',
138
+ values: { apiKey: 'key-202' }
139
+ });
140
+
141
+ const records = await AccountDataModel.findAll({
142
+ where: {
143
+ rcAccountId: 'acc-scope',
144
+ platformName: 'testCRM'
145
+ }
146
+ });
147
+ const dataKeys = records.map(r => r.dataKey).sort();
148
+
149
+ expect(dataKeys).toEqual(['managed-auth-user:201', 'managed-auth-user:202']);
150
+ expect(records).toHaveLength(2);
151
+ });
152
+
153
+ test('getManagedAuthState loads field definitions from Developer Portal when connectorId is provided', async () => {
154
+ developerPortal.getConnectorManifest.mockResolvedValue({
155
+ platforms: {
156
+ testCRM: {
157
+ auth: {
158
+ type: 'apiKey',
159
+ apiKey: {
160
+ page: {
161
+ content: [
162
+ { const: 'orgToken', required: true, managed: true, managedScope: 'account' }
163
+ ]
164
+ }
165
+ }
166
+ }
167
+ }
168
+ }
169
+ });
170
+
171
+ await managedAuthHandler.upsertOrgManagedAuthValues({
172
+ rcAccountId: 'acc-3',
173
+ platform: 'testCRM',
174
+ values: { orgToken: 'portal-token' }
175
+ });
176
+
177
+ const state = await managedAuthHandler.getManagedAuthState({
178
+ platform: 'testCRM',
179
+ connectorId: 'connector-123',
180
+ rcAccountId: 'acc-3'
181
+ });
182
+
183
+ expect(developerPortal.getConnectorManifest).toHaveBeenCalledWith({ connectorId: 'connector-123', isPrivate: false });
184
+ expect(state.hasManagedAuth).toBe(true);
185
+ expect(state.allRequiredFieldsSatisfied).toBe(true);
186
+ expect(state.visibleFieldConsts).toEqual([]);
187
+ });
188
+
189
+ test('getManagedAuthState surfaces missing required fields for unshared and missing shared values', async () => {
190
+ connectorRegistry.getManifest.mockReturnValue({
191
+ platforms: {
192
+ testCRM: {
193
+ auth: {
194
+ type: 'apiKey',
195
+ apiKey: {
196
+ page: {
197
+ content: [
198
+ { const: 'tenantId', required: true, managed: true, managedScope: 'account' },
199
+ { const: 'userToken', required: true, managed: true, managedScope: 'user' },
200
+ { const: 'apiSecret', required: true }
201
+ ]
202
+ }
203
+ }
204
+ }
205
+ }
206
+ }
207
+ });
208
+
209
+ await managedAuthHandler.upsertOrgManagedAuthValues({
210
+ rcAccountId: 'acc-4',
211
+ platform: 'testCRM',
212
+ values: { tenantId: 'tenant-4' }
213
+ });
214
+
215
+ const state = await managedAuthHandler.getManagedAuthState({
216
+ platform: 'testCRM',
217
+ rcAccountId: 'acc-4',
218
+ rcExtensionId: '404'
219
+ });
220
+
221
+ expect(state.hasManagedAuth).toBe(true);
222
+ expect(state.allRequiredFieldsSatisfied).toBe(false);
223
+ expect(state.visibleFieldConsts).toEqual(['userToken', 'apiSecret']);
224
+ expect(state.missingRequiredFieldConsts).toEqual(['userToken', 'apiSecret']);
225
+ });
226
+
227
+ test('getManagedAuthState returns full-form behavior when connector has no shared fields', async () => {
228
+ connectorRegistry.getManifest.mockReturnValue({
229
+ platforms: {
230
+ testCRM: {
231
+ auth: {
232
+ type: 'apiKey',
233
+ apiKey: {
234
+ page: {
235
+ content: [
236
+ { const: 'apiKey', required: true },
237
+ { const: 'tenantId', required: true },
238
+ { const: 'region', required: false }
239
+ ]
240
+ }
241
+ }
242
+ }
243
+ }
244
+ }
245
+ });
246
+
247
+ const state = await managedAuthHandler.getManagedAuthState({
248
+ platform: 'testCRM',
249
+ rcAccountId: 'acc-plain',
250
+ rcExtensionId: '100'
251
+ });
252
+
253
+ expect(state.hasManagedAuth).toBe(false);
254
+ expect(state.allRequiredFieldsSatisfied).toBe(false);
255
+ expect(state.visibleFieldConsts).toBeNull();
256
+ expect(state.missingRequiredFieldConsts).toEqual(['apiKey', 'tenantId']);
257
+ });
258
+
259
+ test('getManagedAuthState falls back to the full auth form after managed auto-login fails', async () => {
260
+ connectorRegistry.getManifest.mockReturnValue({
261
+ platforms: {
262
+ testCRM: {
263
+ auth: {
264
+ type: 'apiKey',
265
+ apiKey: {
266
+ page: {
267
+ content: [
268
+ { const: 'tenantId', required: true, managed: true, managedScope: 'account' },
269
+ { const: 'apiKey', required: true, managed: true, managedScope: 'user' },
270
+ { const: 'region', required: false }
271
+ ]
272
+ }
273
+ }
274
+ }
275
+ }
276
+ }
277
+ });
278
+
279
+ await managedAuthHandler.upsertOrgManagedAuthValues({
280
+ rcAccountId: 'acc-fallback',
281
+ platform: 'testCRM',
282
+ values: { tenantId: 'tenant-1' }
283
+ });
284
+ await managedAuthHandler.upsertUserManagedAuthValues({
285
+ rcAccountId: 'acc-fallback',
286
+ platform: 'testCRM',
287
+ rcExtensionId: '501',
288
+ rcUserName: 'Agent 501',
289
+ values: { apiKey: 'bad-key' }
290
+ });
291
+ await managedAuthHandler.markManagedAuthLoginFailure({
292
+ rcAccountId: 'acc-fallback',
293
+ platform: 'testCRM',
294
+ rcExtensionId: '501'
295
+ });
296
+
297
+ const state = await managedAuthHandler.getManagedAuthState({
298
+ platform: 'testCRM',
299
+ rcAccountId: 'acc-fallback',
300
+ rcExtensionId: '501'
301
+ });
302
+
303
+ expect(state.hasManagedAuth).toBe(true);
304
+ expect(state.allRequiredFieldsSatisfied).toBe(false);
305
+ expect(state.visibleFieldConsts).toBeNull();
306
+ expect(state.missingRequiredFieldConsts).toEqual(['tenantId', 'apiKey']);
307
+ expect(state.fallbackToManualAuth).toBe(true);
308
+ });
309
+
310
+ test('resolveApiKeyLoginFields keeps submitted shared values when managed values are missing', async () => {
311
+ connectorRegistry.getManifest.mockReturnValue({
312
+ platforms: {
313
+ testCRM: {
314
+ auth: {
315
+ type: 'apiKey',
316
+ apiKey: {
317
+ page: {
318
+ content: [
319
+ { const: 'companyId', required: true, managed: true, managedScope: 'account' },
320
+ { const: 'userToken', required: true, managed: true, managedScope: 'user' },
321
+ { const: 'region', required: false, managed: true, managedScope: 'account' }
322
+ ]
323
+ }
324
+ }
325
+ }
326
+ }
327
+ }
328
+ });
329
+
330
+ const result = await managedAuthHandler.resolveApiKeyLoginFields({
331
+ platform: 'testCRM',
332
+ rcAccountId: 'acc-shared-fallback',
333
+ rcExtensionId: '201',
334
+ additionalInfo: {
335
+ companyId: 'company-123',
336
+ userToken: 'user-token-123',
337
+ region: 'us'
338
+ }
339
+ });
340
+
341
+ expect(result.resolvedAdditionalInfo).toEqual({
342
+ companyId: 'company-123',
343
+ userToken: 'user-token-123',
344
+ region: 'us'
345
+ });
346
+ expect(result.missingRequiredFieldConsts).toEqual([]);
347
+ });
348
+
349
+ test('resolveApiKeyLoginFields prefers submitted managed values during manual fallback', async () => {
350
+ connectorRegistry.getManifest.mockReturnValue({
351
+ platforms: {
352
+ testCRM: {
353
+ auth: {
354
+ type: 'apiKey',
355
+ apiKey: {
356
+ page: {
357
+ content: [
358
+ { const: 'companyId', required: true, managed: true, managedScope: 'account' },
359
+ { const: 'apiKey', required: true, managed: true, managedScope: 'user' }
360
+ ]
361
+ }
362
+ }
363
+ }
364
+ }
365
+ }
366
+ });
367
+
368
+ await managedAuthHandler.upsertOrgManagedAuthValues({
369
+ rcAccountId: 'acc-override',
370
+ platform: 'testCRM',
371
+ values: { companyId: 'stored-company' }
372
+ });
373
+ await managedAuthHandler.upsertUserManagedAuthValues({
374
+ rcAccountId: 'acc-override',
375
+ platform: 'testCRM',
376
+ rcExtensionId: '777',
377
+ rcUserName: 'Agent 777',
378
+ values: { apiKey: 'stored-key' }
379
+ });
380
+
381
+ const result = await managedAuthHandler.resolveApiKeyLoginFields({
382
+ platform: 'testCRM',
383
+ rcAccountId: 'acc-override',
384
+ rcExtensionId: '777',
385
+ additionalInfo: {
386
+ companyId: 'manual-company',
387
+ apiKey: 'manual-key'
388
+ },
389
+ preferSubmittedValuesForManagedFields: true
390
+ });
391
+
392
+ expect(result.resolvedAdditionalInfo).toEqual({
393
+ companyId: 'manual-company',
394
+ apiKey: 'manual-key'
395
+ });
396
+ expect(result.resolvedApiKey).toBe('manual-key');
397
+ expect(result.missingRequiredFieldConsts).toEqual([]);
398
+ });
399
+
400
+ test('upsertUserManagedAuthValues throws when rcExtensionId is missing', async () => {
401
+ await expect(managedAuthHandler.upsertUserManagedAuthValues({
402
+ rcAccountId: 'acc-5',
403
+ platform: 'testCRM',
404
+ values: { apiKey: 'x' }
405
+ })).rejects.toThrow('rcExtensionId is required for user managed auth values');
406
+ });
407
+
408
+ test('upsertOrgManagedAuthValues removes specified fields', async () => {
409
+ connectorRegistry.getManifest.mockReturnValue({
410
+ platforms: {
411
+ testCRM: {
412
+ auth: {
413
+ type: 'apiKey',
414
+ apiKey: {
415
+ page: {
416
+ content: [
417
+ { const: 'tenantId', managed: true, managedScope: 'account' },
418
+ { const: 'region', managed: true, managedScope: 'account' }
419
+ ]
420
+ }
421
+ }
422
+ }
423
+ }
424
+ }
425
+ });
426
+
427
+ await managedAuthHandler.upsertOrgManagedAuthValues({
428
+ rcAccountId: 'acc-6',
429
+ platform: 'testCRM',
430
+ values: { tenantId: 'tenant-6', region: 'us' }
431
+ });
432
+
433
+ await managedAuthHandler.upsertOrgManagedAuthValues({
434
+ rcAccountId: 'acc-6',
435
+ platform: 'testCRM',
436
+ values: {},
437
+ fieldsToRemove: ['tenantId']
438
+ });
439
+
440
+ const settings = await managedAuthHandler.getManagedAuthAdminSettings({
441
+ platform: 'testCRM',
442
+ rcAccountId: 'acc-6'
443
+ });
444
+ expect(settings.orgValues.tenantId.hasValue).toBe(false);
445
+ expect(settings.orgValues.region.value).toBe('us');
446
+
447
+ const record = await AccountDataModel.findOne({
448
+ where: {
449
+ rcAccountId: 'acc-6',
450
+ platformName: 'testCRM',
451
+ dataKey: 'managed-auth-org'
452
+ }
453
+ });
454
+ expect(record.data.fields.tenantId).toBeUndefined();
455
+ expect(record.data.fields.region).toBeDefined();
456
+ });
457
+ });
458
+
@@ -0,0 +1,105 @@
1
+ const express = require('express');
2
+ const request = require('supertest');
3
+
4
+ jest.mock('../lib/jwt', () => ({
5
+ decodeJwt: jest.fn(),
6
+ generateJwt: jest.fn(),
7
+ }));
8
+ jest.mock('../handlers/auth', () => ({
9
+ authValidation: jest.fn(),
10
+ }));
11
+ jest.mock('../lib/analytics', () => ({
12
+ init: jest.fn(),
13
+ track: jest.fn(),
14
+ }));
15
+
16
+ const jwt = require('../lib/jwt');
17
+ const authCore = require('../handlers/auth');
18
+ const { createCoreRouter, createCoreMiddleware } = require('../index');
19
+
20
+ function buildApp() {
21
+ const app = express();
22
+ createCoreMiddleware().forEach((m) => app.use(m));
23
+ app.use('/', createCoreRouter());
24
+ return app;
25
+ }
26
+
27
+ describe('Core Router JWT normalization', () => {
28
+ beforeEach(() => {
29
+ jest.clearAllMocks();
30
+ });
31
+
32
+ test('should accept query jwtToken without refreshing it', async () => {
33
+ jwt.decodeJwt.mockReturnValue({ id: 'user-1', platform: 'testCRM' });
34
+ authCore.authValidation.mockResolvedValue({
35
+ successful: true,
36
+ returnMessage: { message: 'ok' },
37
+ failReason: null,
38
+ status: 200,
39
+ });
40
+ const app = buildApp();
41
+
42
+ const response = await request(app).get('/authValidation?jwtToken=query-token');
43
+
44
+ expect(response.status).toBe(200);
45
+ expect(response.headers['x-refreshed-jwt-token']).toBeUndefined();
46
+ expect(authCore.authValidation).toHaveBeenCalledWith({
47
+ platform: 'testCRM',
48
+ userId: 'user-1',
49
+ });
50
+ expect(jwt.generateJwt).not.toHaveBeenCalled();
51
+ });
52
+
53
+ test('should refresh near-expiry bearer token and expose header', async () => {
54
+ const nowMs = 1700000000000;
55
+ const nowSeconds = Math.floor(nowMs / 1000);
56
+ const nowSpy = jest.spyOn(Date, 'now').mockReturnValue(nowMs);
57
+ jwt.decodeJwt.mockImplementation((token) => {
58
+ if (token === 'old-token') {
59
+ return { id: 'user-1', platform: 'testCRM', exp: nowSeconds + 60 };
60
+ }
61
+ if (token === 'new-token') {
62
+ return { id: 'user-1', platform: 'testCRM', exp: nowSeconds + (14 * 24 * 60 * 60) };
63
+ }
64
+ return null;
65
+ });
66
+ jwt.generateJwt.mockReturnValue('new-token');
67
+ const app = buildApp();
68
+
69
+ const response = await request(app)
70
+ .get('/isAlive')
71
+ .set('Authorization', 'Bearer old-token')
72
+ .set('Origin', 'https://example.com');
73
+
74
+ expect(response.status).toBe(200);
75
+ expect(response.headers['x-refreshed-jwt-token']).toBe('new-token');
76
+ expect(response.headers['access-control-expose-headers']).toContain('x-refreshed-jwt-token');
77
+ expect(jwt.generateJwt).toHaveBeenCalledWith({ id: 'user-1', platform: 'testCRM' });
78
+ nowSpy.mockRestore();
79
+ });
80
+
81
+ test('should treat invalid bearer token as unauthenticated for authValidation route', async () => {
82
+ jwt.decodeJwt.mockReturnValue(null);
83
+ const app = buildApp();
84
+
85
+ const response = await request(app)
86
+ .get('/authValidation?jwtToken=query-token')
87
+ .set('Authorization', 'Bearer invalid-token');
88
+
89
+ expect(response.status).toBe(400);
90
+ expect(response.text).toContain('authorize CRM platform');
91
+ expect(authCore.authValidation).not.toHaveBeenCalled();
92
+ });
93
+
94
+ test('should bypass normalization for /mcp routes', async () => {
95
+ const app = buildApp();
96
+
97
+ const response = await request(app)
98
+ .get('/mcp')
99
+ .set('Authorization', 'Bearer maybe-token');
100
+
101
+ expect(response.status).toBe(404);
102
+ expect(jwt.decodeJwt).not.toHaveBeenCalled();
103
+ });
104
+ });
105
+
@@ -35,6 +35,21 @@ describe('JWT Utility', () => {
35
35
  // Assert
36
36
  expect(token1).not.toBe(token2);
37
37
  });
38
+
39
+ test('should generate token with about 2 weeks lifetime', () => {
40
+ // Arrange
41
+ const payload = { id: 'user-ttl', platform: 'testCRM' };
42
+
43
+ // Act
44
+ const token = jwt.generateJwt(payload);
45
+ const decoded = jwt.decodeJwt(token);
46
+ const lifetimeSeconds = decoded.exp - decoded.iat;
47
+
48
+ // Assert
49
+ // Keep a tiny tolerance to avoid timing flakiness.
50
+ expect(lifetimeSeconds).toBeGreaterThanOrEqual((14 * 24 * 60 * 60) - 2);
51
+ expect(lifetimeSeconds).toBeLessThanOrEqual((14 * 24 * 60 * 60) + 2);
52
+ });
38
53
  });
39
54
 
40
55
  describe('decodeJwt', () => {
@@ -292,6 +292,17 @@ describe('MCP Tool: createCallLog', () => {
292
292
  expect(result.error).toContain('Invalid JWT token');
293
293
  });
294
294
 
295
+ test('should return error when decodeJwt returns null', async () => {
296
+ jwt.decodeJwt.mockReturnValue(null);
297
+ const result = await createCallLog.execute({
298
+ jwtToken: 'invalid-token',
299
+ incomingData: { logInfo: { sessionId: 'session-123' } }
300
+ });
301
+
302
+ expect(result.success).toBe(false);
303
+ expect(result.error).toContain('Invalid JWT token');
304
+ });
305
+
295
306
  test('should return error when platform connector not found', async () => {
296
307
  // Arrange
297
308
  const mockIncomingData = {
@@ -0,0 +1,58 @@
1
+ const createContact = require('../../../mcp/tools/createContact');
2
+ const jwt = require('../../../lib/jwt');
3
+ const connectorRegistry = require('../../../connector/registry');
4
+ const contactCore = require('../../../handlers/contact');
5
+
6
+ jest.mock('../../../lib/jwt');
7
+ jest.mock('../../../connector/registry');
8
+ jest.mock('../../../handlers/contact');
9
+
10
+ describe('MCP Tool: createContact', () => {
11
+ beforeEach(() => {
12
+ jest.clearAllMocks();
13
+ });
14
+
15
+ test('should have correct tool definition', () => {
16
+ expect(createContact.definition).toBeDefined();
17
+ expect(createContact.definition.name).toBe('createContact');
18
+ expect(createContact.definition.inputSchema.required).toContain('phoneNumber');
19
+ });
20
+
21
+ test('should create contact successfully', async () => {
22
+ jwt.decodeJwt.mockReturnValue({ id: 'user-123', platform: 'testCRM' });
23
+ connectorRegistry.getConnector.mockReturnValue({ createContact: jest.fn() });
24
+ contactCore.createContact.mockResolvedValue({
25
+ successful: true,
26
+ returnMessage: { message: 'Created' },
27
+ contact: { id: 'contact-1' }
28
+ });
29
+
30
+ const result = await createContact.execute({
31
+ jwtToken: 'mock-jwt-token',
32
+ phoneNumber: '+14155551234',
33
+ newContactName: 'John Doe'
34
+ });
35
+
36
+ expect(result).toEqual({
37
+ success: true,
38
+ data: {
39
+ contact: { id: 'contact-1' },
40
+ message: 'Created'
41
+ }
42
+ });
43
+ });
44
+
45
+ test('should return error when decodeJwt returns null', async () => {
46
+ jwt.decodeJwt.mockReturnValue(null);
47
+
48
+ const result = await createContact.execute({
49
+ jwtToken: 'invalid-jwt',
50
+ phoneNumber: '+14155551234',
51
+ newContactName: 'John Doe'
52
+ });
53
+
54
+ expect(result.success).toBe(false);
55
+ expect(result.error).toContain('Invalid JWT token');
56
+ });
57
+ });
58
+
@@ -427,6 +427,21 @@ describe('MCP Tool: createMessageLog', () => {
427
427
  expect(result.error).toContain('Invalid JWT token');
428
428
  });
429
429
 
430
+ test('should return error when decodeJwt returns null', async () => {
431
+ jwt.decodeJwt.mockReturnValue(null);
432
+
433
+ const result = await createMessageLog.execute({
434
+ jwtToken: 'invalid-token',
435
+ incomingData: {
436
+ sessionId: 'session-123',
437
+ messageInfo: { from: { phoneNumber: '+1234567890' }, to: [{ phoneNumber: '+1098765432' }] }
438
+ }
439
+ });
440
+
441
+ expect(result.success).toBe(false);
442
+ expect(result.error).toContain('Invalid JWT token');
443
+ });
444
+
430
445
  test('should return error when platform connector not found', async () => {
431
446
  // Arrange
432
447
  const mockIncomingData = {
@@ -162,6 +162,18 @@ describe('MCP Tool: findContactByName', () => {
162
162
  expect(result.error).toContain('Invalid JWT token');
163
163
  });
164
164
 
165
+ test('should return error when decodeJwt returns null', async () => {
166
+ jwt.decodeJwt.mockReturnValue(null);
167
+
168
+ const result = await findContactByName.execute({
169
+ jwtToken: 'invalid-token',
170
+ name: 'John Doe'
171
+ });
172
+
173
+ expect(result.success).toBe(false);
174
+ expect(result.error).toContain('Invalid JWT token');
175
+ });
176
+
165
177
  test('should return error when platform connector not found', async () => {
166
178
  // Arrange
167
179
  jwt.decodeJwt.mockReturnValue({