@app-connect/core 1.7.24 → 1.7.26

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 (137) hide show
  1. package/.env.test +5 -5
  2. package/README.md +441 -441
  3. package/connector/developerPortal.js +31 -42
  4. package/connector/mock.js +84 -77
  5. package/connector/proxy/engine.js +164 -163
  6. package/connector/proxy/index.js +500 -500
  7. package/connector/registry.js +252 -252
  8. package/docs/README.md +50 -50
  9. package/docs/architecture.md +93 -93
  10. package/docs/connectors.md +116 -117
  11. package/docs/handlers.md +125 -125
  12. package/docs/libraries.md +101 -101
  13. package/docs/models.md +144 -144
  14. package/docs/routes.md +115 -115
  15. package/docs/tests.md +73 -73
  16. package/handlers/admin.js +523 -523
  17. package/handlers/appointment.js +193 -0
  18. package/handlers/auth.js +296 -296
  19. package/handlers/calldown.js +99 -99
  20. package/handlers/contact.js +280 -280
  21. package/handlers/disposition.js +82 -80
  22. package/handlers/log.js +984 -973
  23. package/handlers/managedAuth.js +446 -446
  24. package/handlers/plugin.js +208 -208
  25. package/handlers/user.js +142 -142
  26. package/index.js +3140 -2652
  27. package/jest.config.js +56 -56
  28. package/lib/analytics.js +54 -54
  29. package/lib/authSession.js +109 -109
  30. package/lib/cacheCleanup.js +21 -0
  31. package/lib/callLogComposer.js +898 -898
  32. package/lib/callLogLookup.js +34 -0
  33. package/lib/constants.js +8 -8
  34. package/lib/debugTracer.js +177 -177
  35. package/lib/encode.js +30 -30
  36. package/lib/errorHandler.js +218 -206
  37. package/lib/generalErrorMessage.js +41 -41
  38. package/lib/jwt.js +18 -18
  39. package/lib/logger.js +190 -190
  40. package/lib/migrateCallLogsSchema.js +116 -0
  41. package/lib/ringcentral.js +266 -266
  42. package/lib/s3ErrorLogReport.js +65 -65
  43. package/lib/sharedSMSComposer.js +471 -471
  44. package/lib/util.js +67 -67
  45. package/mcp/README.md +412 -395
  46. package/mcp/lib/validator.js +91 -91
  47. package/mcp/mcpHandler.js +425 -425
  48. package/mcp/tools/cancelAppointment.js +101 -0
  49. package/mcp/tools/checkAuthStatus.js +105 -105
  50. package/mcp/tools/confirmAppointment.js +101 -0
  51. package/mcp/tools/createAppointment.js +157 -0
  52. package/mcp/tools/createCallLog.js +327 -316
  53. package/mcp/tools/createContact.js +117 -117
  54. package/mcp/tools/createMessageLog.js +287 -287
  55. package/mcp/tools/doAuth.js +60 -60
  56. package/mcp/tools/findContactByName.js +93 -93
  57. package/mcp/tools/findContactByPhone.js +101 -101
  58. package/mcp/tools/getCallLog.js +111 -102
  59. package/mcp/tools/getGoogleFilePicker.js +99 -99
  60. package/mcp/tools/getHelp.js +43 -43
  61. package/mcp/tools/getPublicConnectors.js +94 -94
  62. package/mcp/tools/getSessionInfo.js +90 -90
  63. package/mcp/tools/index.js +51 -41
  64. package/mcp/tools/listAppointments.js +163 -0
  65. package/mcp/tools/logout.js +96 -96
  66. package/mcp/tools/rcGetCallLogs.js +65 -65
  67. package/mcp/tools/updateAppointment.js +154 -0
  68. package/mcp/tools/updateCallLog.js +130 -126
  69. package/mcp/ui/App/App.tsx +358 -358
  70. package/mcp/ui/App/components/AuthInfoForm.tsx +113 -113
  71. package/mcp/ui/App/components/AuthSuccess.tsx +22 -22
  72. package/mcp/ui/App/components/ConnectorList.tsx +82 -82
  73. package/mcp/ui/App/components/DebugPanel.tsx +43 -43
  74. package/mcp/ui/App/components/OAuthConnect.tsx +270 -270
  75. package/mcp/ui/App/lib/callTool.ts +130 -130
  76. package/mcp/ui/App/lib/debugLog.ts +41 -41
  77. package/mcp/ui/App/lib/developerPortal.ts +111 -111
  78. package/mcp/ui/App/main.css +5 -5
  79. package/mcp/ui/App/root.tsx +13 -13
  80. package/mcp/ui/index.html +13 -13
  81. package/mcp/ui/package-lock.json +6356 -6356
  82. package/mcp/ui/package.json +25 -25
  83. package/mcp/ui/tsconfig.json +26 -26
  84. package/mcp/ui/vite.config.ts +16 -16
  85. package/models/accountDataModel.js +33 -33
  86. package/models/adminConfigModel.js +35 -35
  87. package/models/cacheModel.js +30 -26
  88. package/models/callDownListModel.js +34 -34
  89. package/models/callLogModel.js +33 -27
  90. package/models/dynamo/connectorSchema.js +146 -146
  91. package/models/dynamo/lockSchema.js +24 -24
  92. package/models/dynamo/noteCacheSchema.js +29 -29
  93. package/models/llmSessionModel.js +17 -17
  94. package/models/messageLogModel.js +25 -25
  95. package/models/sequelize.js +16 -16
  96. package/models/userModel.js +45 -45
  97. package/package.json +72 -72
  98. package/releaseNotes.json +1093 -1073
  99. package/test/connector/proxy/engine.test.js +126 -93
  100. package/test/connector/proxy/index.test.js +279 -279
  101. package/test/connector/proxy/sample.json +161 -161
  102. package/test/connector/registry.test.js +415 -415
  103. package/test/handlers/admin.test.js +616 -616
  104. package/test/handlers/auth.test.js +1018 -1015
  105. package/test/handlers/contact.test.js +1014 -1014
  106. package/test/handlers/log.test.js +1298 -1160
  107. package/test/handlers/managedAuth.test.js +458 -458
  108. package/test/handlers/plugin.test.js +380 -380
  109. package/test/index.test.js +105 -105
  110. package/test/lib/cacheCleanup.test.js +42 -0
  111. package/test/lib/callLogComposer.test.js +1231 -1231
  112. package/test/lib/debugTracer.test.js +328 -328
  113. package/test/lib/jwt.test.js +176 -176
  114. package/test/lib/logger.test.js +206 -206
  115. package/test/lib/oauth.test.js +359 -359
  116. package/test/lib/ringcentral.test.js +467 -467
  117. package/test/lib/sharedSMSComposer.test.js +1084 -1084
  118. package/test/lib/util.test.js +329 -329
  119. package/test/mcp/tools/checkAuthStatus.test.js +83 -82
  120. package/test/mcp/tools/createCallLog.test.js +436 -436
  121. package/test/mcp/tools/createContact.test.js +58 -58
  122. package/test/mcp/tools/createMessageLog.test.js +595 -595
  123. package/test/mcp/tools/doAuth.test.js +113 -113
  124. package/test/mcp/tools/findContactByName.test.js +275 -275
  125. package/test/mcp/tools/findContactByPhone.test.js +296 -296
  126. package/test/mcp/tools/getCallLog.test.js +298 -298
  127. package/test/mcp/tools/getGoogleFilePicker.test.js +281 -281
  128. package/test/mcp/tools/getPublicConnectors.test.js +107 -107
  129. package/test/mcp/tools/getSessionInfo.test.js +127 -127
  130. package/test/mcp/tools/logout.test.js +233 -233
  131. package/test/mcp/tools/rcGetCallLogs.test.js +56 -56
  132. package/test/mcp/tools/updateCallLog.test.js +360 -360
  133. package/test/models/accountDataModel.test.js +98 -98
  134. package/test/models/dynamo/connectorSchema.test.js +189 -189
  135. package/test/models/models.test.js +568 -539
  136. package/test/routes/managedAuthRoutes.test.js +104 -129
  137. package/test/setup.js +178 -178
@@ -1,1015 +1,1018 @@
1
- const authHandler = require('../../handlers/auth');
2
- const connectorRegistry = require('../../connector/registry');
3
-
4
- // Mock the connector registry
5
- jest.mock('../../connector/registry');
6
- jest.mock('../../lib/oauth');
7
- jest.mock('../../models/dynamo/connectorSchema', () => ({
8
- Connector: {
9
- getProxyConfig: jest.fn()
10
- }
11
- }));
12
- jest.mock('../../lib/ringcentral', () => ({
13
- RingCentral: jest.fn().mockImplementation(() => ({
14
- generateToken: jest.fn()
15
- }))
16
- }));
17
- jest.mock('../../handlers/admin', () => ({
18
- updateAdminRcTokens: jest.fn()
19
- }));
20
-
21
- const oauth = require('../../lib/oauth');
22
- const { Connector } = require('../../models/dynamo/connectorSchema');
23
- const { RingCentral } = require('../../lib/ringcentral');
24
- const adminCore = require('../../handlers/admin');
25
- const { AccountDataModel } = require('../../models/accountDataModel');
26
- const { encode } = require('../../lib/encode');
27
-
28
- describe('Auth Handler', () => {
29
- const originalEnv = process.env;
30
-
31
- beforeEach(() => {
32
- // Reset mocks
33
- jest.clearAllMocks();
34
- global.testUtils.resetConnectorRegistry();
35
- process.env = { ...originalEnv };
36
- process.env.APP_SERVER_SECRET_KEY = 'test-app-server-secret-key-123456';
37
- });
38
-
39
- afterEach(() => {
40
- process.env = originalEnv;
41
- });
42
-
43
- describe('onApiKeyLogin', () => {
44
- afterEach(async () => {
45
- await AccountDataModel.destroy({ where: {} });
46
- });
47
-
48
- test('should handle successful API key login', async () => {
49
- // Arrange
50
- const mockUserInfo = {
51
- successful: true,
52
- platformUserInfo: {
53
- id: 'test-user-id',
54
- name: 'Test User',
55
- timezoneName: 'America/Los_Angeles',
56
- timezoneOffset: 0,
57
- platformAdditionalInfo: {}
58
- },
59
- returnMessage: {
60
- messageType: 'success',
61
- message: 'Login successful',
62
- ttl: 1000
63
- }
64
- };
65
-
66
- const mockConnector = global.testUtils.createMockConnector({
67
- getBasicAuth: jest.fn().mockReturnValue('dGVzdC1hcGkta2V5Og=='),
68
- getUserInfo: jest.fn().mockResolvedValue(mockUserInfo)
69
- });
70
-
71
- connectorRegistry.getConnector.mockReturnValue(mockConnector);
72
-
73
- const requestData = {
74
- platform: 'testCRM',
75
- hostname: 'test.example.com',
76
- apiKey: 'test-api-key',
77
- additionalInfo: {}
78
- };
79
-
80
- // Act
81
- const result = await authHandler.onApiKeyLogin(requestData);
82
-
83
- // Assert
84
- expect(result.userInfo).toBeDefined();
85
- expect(result.userInfo.id).toBe('test-user-id');
86
- expect(result.userInfo.name).toBe('Test User');
87
- expect(result.returnMessage).toEqual(mockUserInfo.returnMessage);
88
- expect(mockConnector.getBasicAuth).toHaveBeenCalledWith({ apiKey: 'test-api-key' });
89
- expect(mockConnector.getUserInfo).toHaveBeenCalledWith({
90
- authHeader: 'Basic dGVzdC1hcGkta2V5Og==',
91
- hostname: 'test.example.com',
92
- additionalInfo: { apiKey: 'test-api-key' },
93
- apiKey: 'test-api-key',
94
- platform: 'testCRM',
95
- proxyId: undefined
96
- });
97
- });
98
-
99
- test('should handle failed API key login', async () => {
100
- // Arrange
101
- const mockUserInfo = {
102
- successful: false,
103
- platformUserInfo: null,
104
- returnMessage: {
105
- messageType: 'error',
106
- message: 'Invalid API key',
107
- ttl: 3000
108
- }
109
- };
110
-
111
- const mockConnector = global.testUtils.createMockConnector({
112
- getBasicAuth: jest.fn().mockReturnValue('dGVzdC1hcGkta2V5Og=='),
113
- getUserInfo: jest.fn().mockResolvedValue(mockUserInfo)
114
- });
115
-
116
- connectorRegistry.getConnector.mockReturnValue(mockConnector);
117
-
118
- const requestData = {
119
- platform: 'testCRM',
120
- hostname: 'test.example.com',
121
- apiKey: 'invalid-api-key',
122
- additionalInfo: {}
123
- };
124
-
125
- // Act
126
- const result = await authHandler.onApiKeyLogin(requestData);
127
-
128
- // Assert
129
- expect(result.userInfo).toBeNull();
130
- expect(result.returnMessage).toEqual(mockUserInfo.returnMessage);
131
- });
132
-
133
- test('should mark managed auth auto-login failure so the next attempt can fall back to manual auth', async () => {
134
- connectorRegistry.getManifest.mockReturnValue({
135
- platforms: {
136
- testCRM: {
137
- auth: {
138
- type: 'apiKey',
139
- apiKey: {
140
- page: {
141
- content: [
142
- { const: 'tenantId', required: true, managed: true, managedScope: 'account' },
143
- { const: 'apiKey', required: true, managed: true, managedScope: 'user' }
144
- ]
145
- }
146
- }
147
- }
148
- }
149
- }
150
- });
151
-
152
- await AccountDataModel.create({
153
- rcAccountId: 'rc-account-fail',
154
- platformName: 'testCRM',
155
- dataKey: 'managed-auth-org',
156
- data: {
157
- fields: {
158
- tenantId: { version: 1, encrypted: true, value: encode(JSON.stringify('tenant-1')) }
159
- }
160
- }
161
- });
162
- await AccountDataModel.create({
163
- rcAccountId: 'rc-account-fail',
164
- platformName: 'testCRM',
165
- dataKey: 'managed-auth-user:101',
166
- data: {
167
- rcExtensionId: '101',
168
- rcUserName: 'Agent 101',
169
- fields: {
170
- apiKey: { version: 1, encrypted: true, value: encode(JSON.stringify('bad-stored-key')) }
171
- }
172
- }
173
- });
174
-
175
- const mockConnector = global.testUtils.createMockConnector({
176
- getBasicAuth: jest.fn().mockReturnValue('encoded-bad-key'),
177
- getUserInfo: jest.fn().mockResolvedValue({
178
- successful: false,
179
- platformUserInfo: null,
180
- returnMessage: {
181
- messageType: 'error',
182
- message: 'Invalid API key',
183
- ttl: 3000
184
- }
185
- })
186
- });
187
- connectorRegistry.getConnector.mockReturnValue(mockConnector);
188
-
189
- const result = await authHandler.onApiKeyLogin({
190
- platform: 'testCRM',
191
- hostname: 'test.example.com',
192
- rcAccountId: 'rc-account-fail',
193
- rcExtensionId: '101',
194
- additionalInfo: {}
195
- });
196
-
197
- expect(result.userInfo).toBeNull();
198
- const failureRecord = await AccountDataModel.findOne({
199
- where: {
200
- rcAccountId: 'rc-account-fail',
201
- platformName: 'testCRM',
202
- dataKey: 'managed-auth-login-failure:101'
203
- }
204
- });
205
- expect(failureRecord).not.toBeNull();
206
- });
207
-
208
- test('should merge stored org managed auth values into additionalInfo', async () => {
209
- connectorRegistry.getManifest.mockReturnValue({
210
- platforms: {
211
- testCRM: {
212
- auth: {
213
- type: 'apiKey',
214
- apiKey: {
215
- page: {
216
- content: [
217
- { const: 'apiKey', required: true, managed: true, managedScope: 'account' },
218
- { const: 'tenantId', required: true, managed: true, managedScope: 'account' },
219
- { const: 'userToken', required: true }
220
- ]
221
- }
222
- }
223
- }
224
- }
225
- }
226
- });
227
- await AccountDataModel.create({
228
- rcAccountId: 'rc-account-1',
229
- platformName: 'testCRM',
230
- dataKey: 'managed-auth-org',
231
- data: {
232
- fields: {
233
- apiKey: { version: 1, encrypted: true, value: encode(JSON.stringify('stored-api-key')) },
234
- tenantId: { version: 1, encrypted: true, value: encode(JSON.stringify('tenant-1')) }
235
- }
236
- }
237
- });
238
-
239
- const mockUserInfo = {
240
- successful: true,
241
- platformUserInfo: {
242
- id: 'test-user-id',
243
- name: 'Test User',
244
- platformAdditionalInfo: {}
245
- },
246
- returnMessage: { messageType: 'success', message: 'ok' }
247
- };
248
- const mockConnector = global.testUtils.createMockConnector({
249
- getBasicAuth: jest.fn().mockReturnValue('encoded-shared'),
250
- getUserInfo: jest.fn().mockResolvedValue(mockUserInfo)
251
- });
252
- connectorRegistry.getConnector.mockReturnValue(mockConnector);
253
-
254
- await authHandler.onApiKeyLogin({
255
- platform: 'testCRM',
256
- hostname: 'test.example.com',
257
- rcAccountId: 'rc-account-1',
258
- additionalInfo: { userToken: 'user-token-1' }
259
- });
260
-
261
- expect(mockConnector.getBasicAuth).toHaveBeenCalledWith({ apiKey: 'stored-api-key' });
262
- expect(mockConnector.getUserInfo).toHaveBeenCalledWith(expect.objectContaining({
263
- additionalInfo: expect.objectContaining({
264
- apiKey: 'stored-api-key',
265
- tenantId: 'tenant-1',
266
- userToken: 'user-token-1'
267
- })
268
- }));
269
- });
270
-
271
- test('should allow submitted shared fields to satisfy missing required managed auth values', async () => {
272
- connectorRegistry.getManifest.mockReturnValue({
273
- platforms: {
274
- testCRM: {
275
- auth: {
276
- type: 'apiKey',
277
- apiKey: {
278
- page: {
279
- content: [
280
- { const: 'companyId', required: true, managed: true, managedScope: 'account' },
281
- { const: 'userToken', required: true }
282
- ]
283
- }
284
- }
285
- }
286
- }
287
- }
288
- });
289
-
290
- const mockConnector = global.testUtils.createMockConnector({
291
- getBasicAuth: jest.fn(),
292
- getUserInfo: jest.fn().mockResolvedValue({
293
- successful: true,
294
- platformUserInfo: {
295
- id: 'test-user-id',
296
- name: 'Test User',
297
- platformAdditionalInfo: {}
298
- },
299
- returnMessage: { messageType: 'success', message: 'ok' }
300
- })
301
- });
302
- connectorRegistry.getConnector.mockReturnValue(mockConnector);
303
-
304
- const result = await authHandler.onApiKeyLogin({
305
- platform: 'testCRM',
306
- hostname: 'test.example.com',
307
- rcAccountId: 'rc-account-2',
308
- additionalInfo: {
309
- companyId: 'company-123',
310
- userToken: 'user-token-1'
311
- }
312
- });
313
-
314
- expect(result.userInfo).not.toBeNull();
315
- expect(mockConnector.getUserInfo).toHaveBeenCalledWith(expect.objectContaining({
316
- additionalInfo: expect.objectContaining({
317
- companyId: 'company-123',
318
- userToken: 'user-token-1'
319
- })
320
- }));
321
- });
322
-
323
- test('should not persist submitted managed auth values from end users', async () => {
324
- connectorRegistry.getManifest.mockReturnValue({
325
- platforms: {
326
- testCRM: {
327
- auth: {
328
- type: 'apiKey',
329
- apiKey: {
330
- page: {
331
- content: [
332
- { const: 'companyId', required: false, managed: true, managedScope: 'account' },
333
- { const: 'userToken', required: true }
334
- ]
335
- }
336
- }
337
- }
338
- }
339
- }
340
- });
341
-
342
- const mockUserInfo = {
343
- successful: true,
344
- platformUserInfo: {
345
- id: 'test-user-id',
346
- name: 'Test User',
347
- platformAdditionalInfo: {}
348
- },
349
- returnMessage: { messageType: 'success', message: 'ok' }
350
- };
351
- const mockConnector = global.testUtils.createMockConnector({
352
- getBasicAuth: jest.fn().mockReturnValue('encoded'),
353
- getUserInfo: jest.fn().mockResolvedValue(mockUserInfo)
354
- });
355
- connectorRegistry.getConnector.mockReturnValue(mockConnector);
356
-
357
- await authHandler.onApiKeyLogin({
358
- platform: 'testCRM',
359
- hostname: 'test.example.com',
360
- rcAccountId: 'rc-account-2',
361
- additionalInfo: {
362
- companyId: 'company-123',
363
- userToken: 'user-token-1'
364
- }
365
- });
366
-
367
- expect(mockConnector.getUserInfo).toHaveBeenCalledWith(expect.objectContaining({
368
- additionalInfo: expect.objectContaining({
369
- companyId: 'company-123',
370
- userToken: 'user-token-1'
371
- })
372
- }));
373
-
374
- const stored = await AccountDataModel.findOne({
375
- where: {
376
- rcAccountId: 'rc-account-2',
377
- platformName: 'testCRM',
378
- dataKey: 'managed-auth-org'
379
- }
380
- });
381
- expect(stored).toBeNull();
382
- });
383
-
384
- test('should allow manual fallback values to override stored managed credentials and clear failure state after success', async () => {
385
- connectorRegistry.getManifest.mockReturnValue({
386
- platforms: {
387
- testCRM: {
388
- auth: {
389
- type: 'apiKey',
390
- apiKey: {
391
- page: {
392
- content: [
393
- { const: 'apiKey', required: true, managed: true, managedScope: 'user' },
394
- { const: 'tenantId', required: true, managed: true, managedScope: 'account' }
395
- ]
396
- }
397
- }
398
- }
399
- }
400
- }
401
- });
402
-
403
- await AccountDataModel.create({
404
- rcAccountId: 'rc-account-recover',
405
- platformName: 'testCRM',
406
- dataKey: 'managed-auth-org',
407
- data: {
408
- fields: {
409
- tenantId: { version: 1, encrypted: true, value: encode(JSON.stringify('stored-tenant')) }
410
- }
411
- }
412
- });
413
- await AccountDataModel.create({
414
- rcAccountId: 'rc-account-recover',
415
- platformName: 'testCRM',
416
- dataKey: 'managed-auth-user:202',
417
- data: {
418
- rcExtensionId: '202',
419
- rcUserName: 'Agent 202',
420
- fields: {
421
- apiKey: { version: 1, encrypted: true, value: encode(JSON.stringify('stored-bad-key')) }
422
- }
423
- }
424
- });
425
- await AccountDataModel.create({
426
- rcAccountId: 'rc-account-recover',
427
- platformName: 'testCRM',
428
- dataKey: 'managed-auth-login-failure:202',
429
- data: {
430
- failedAt: '2026-04-07T00:00:00.000Z'
431
- }
432
- });
433
-
434
- const mockConnector = global.testUtils.createMockConnector({
435
- getBasicAuth: jest.fn().mockReturnValue('encoded-manual-key'),
436
- getUserInfo: jest.fn().mockResolvedValue({
437
- successful: true,
438
- platformUserInfo: {
439
- id: 'test-user-id',
440
- name: 'Recovered User',
441
- platformAdditionalInfo: {}
442
- },
443
- returnMessage: { messageType: 'success', message: 'ok' }
444
- })
445
- });
446
- connectorRegistry.getConnector.mockReturnValue(mockConnector);
447
-
448
- const result = await authHandler.onApiKeyLogin({
449
- platform: 'testCRM',
450
- hostname: 'test.example.com',
451
- rcAccountId: 'rc-account-recover',
452
- rcExtensionId: '202',
453
- additionalInfo: {
454
- apiKey: 'manual-good-key',
455
- tenantId: 'manual-tenant'
456
- }
457
- });
458
-
459
- expect(result.userInfo).not.toBeNull();
460
- expect(mockConnector.getBasicAuth).toHaveBeenCalledWith({ apiKey: 'manual-good-key' });
461
- expect(mockConnector.getUserInfo).toHaveBeenCalledWith(expect.objectContaining({
462
- additionalInfo: {
463
- apiKey: 'manual-good-key',
464
- tenantId: 'manual-tenant'
465
- }
466
- }));
467
-
468
- const failureRecord = await AccountDataModel.findOne({
469
- where: {
470
- rcAccountId: 'rc-account-recover',
471
- platformName: 'testCRM',
472
- dataKey: 'managed-auth-login-failure:202'
473
- }
474
- });
475
- expect(failureRecord).toBeNull();
476
- });
477
-
478
- test('should return warning when required auth fields are missing', async () => {
479
- connectorRegistry.getManifest.mockReturnValue({
480
- platforms: {
481
- testCRM: {
482
- auth: {
483
- type: 'apiKey',
484
- apiKey: {
485
- page: {
486
- content: [
487
- { const: 'tenantId', required: true, managed: true, managedScope: 'account' },
488
- { const: 'userToken', required: true }
489
- ]
490
- }
491
- }
492
- }
493
- }
494
- }
495
- });
496
-
497
- const mockConnector = global.testUtils.createMockConnector({
498
- getBasicAuth: jest.fn(),
499
- getUserInfo: jest.fn()
500
- });
501
- connectorRegistry.getConnector.mockReturnValue(mockConnector);
502
-
503
- const result = await authHandler.onApiKeyLogin({
504
- platform: 'testCRM',
505
- hostname: 'test.example.com',
506
- rcAccountId: 'rc-account-4',
507
- additionalInfo: {}
508
- });
509
-
510
- expect(result.userInfo).toBeNull();
511
- expect(result.returnMessage).toEqual({
512
- messageType: 'warning',
513
- message: 'Missing required authentication fields.',
514
- ttl: 3000,
515
- missingRequiredFieldConsts: ['tenantId', 'userToken']
516
- });
517
- expect(mockConnector.getBasicAuth).not.toHaveBeenCalled();
518
- expect(mockConnector.getUserInfo).not.toHaveBeenCalled();
519
- });
520
-
521
- test('should throw error when connector not found', async () => {
522
- // Arrange
523
- connectorRegistry.getConnector.mockImplementation(() => {
524
- throw new Error('Connector not found for platform: testCRM');
525
- });
526
-
527
- const requestData = {
528
- platform: 'testCRM',
529
- hostname: 'test.example.com',
530
- apiKey: 'test-api-key',
531
- additionalInfo: {}
532
- };
533
-
534
- // Act & Assert
535
- await expect(authHandler.onApiKeyLogin(requestData))
536
- .rejects.toThrow('Connector not found for platform: testCRM');
537
- });
538
- });
539
-
540
- describe('authValidation', () => {
541
- test('should validate user authentication successfully', async () => {
542
- // Arrange
543
- const mockUser = global.testUtils.createMockUser();
544
- const mockValidationResponse = {
545
- successful: true,
546
- returnMessage: {
547
- messageType: 'success',
548
- message: 'Authentication valid',
549
- ttl: 1000
550
- },
551
- status: 200
552
- };
553
-
554
- const mockConnector = global.testUtils.createMockConnector({
555
- getOauthInfo: jest.fn().mockResolvedValue({}),
556
- authValidation: jest.fn().mockResolvedValue(mockValidationResponse)
557
- });
558
-
559
- connectorRegistry.getConnector.mockReturnValue(mockConnector);
560
-
561
- // Mock UserModel.findOne to return a user
562
- const { UserModel } = require('../../models/userModel');
563
- jest.spyOn(UserModel, 'findOne').mockResolvedValue(mockUser);
564
-
565
- // Mock oauth.checkAndRefreshAccessToken
566
- const oauth = require('../../lib/oauth');
567
- jest.spyOn(oauth, 'checkAndRefreshAccessToken').mockResolvedValue(mockUser);
568
-
569
- const requestData = {
570
- platform: 'testCRM',
571
- userId: 'test-user-id'
572
- };
573
-
574
- // Act
575
- const result = await authHandler.authValidation(requestData);
576
-
577
- // Assert
578
- expect(result).toEqual({
579
- ...mockValidationResponse,
580
- failReason: ''
581
- });
582
- expect(mockConnector.authValidation).toHaveBeenCalledWith({ user: mockUser });
583
- });
584
-
585
- test('should handle user not found in database', async () => {
586
- // Arrange
587
- const mockConnector = global.testUtils.createMockConnector();
588
- connectorRegistry.getConnector.mockReturnValue(mockConnector);
589
-
590
- // Mock UserModel.findOne to return null (user not found)
591
- const { UserModel } = require('../../models/userModel');
592
- jest.spyOn(UserModel, 'findOne').mockResolvedValue(null);
593
-
594
- const requestData = {
595
- platform: 'testCRM',
596
- userId: 'non-existent-user'
597
- };
598
-
599
- // Act
600
- const result = await authHandler.authValidation(requestData);
601
-
602
- // Assert
603
- expect(result).toEqual({
604
- successful: false,
605
- status: 404,
606
- failReason: 'App Connect. User not found in database'
607
- });
608
- });
609
-
610
- test('should handle validation failure', async () => {
611
- // Arrange
612
- const mockUser = global.testUtils.createMockUser();
613
- const mockValidationResponse = {
614
- successful: false,
615
- returnMessage: {
616
- messageType: 'error',
617
- message: 'Authentication failed',
618
- ttl: 3000
619
- },
620
- status: 401
621
- };
622
-
623
- const mockConnector = global.testUtils.createMockConnector({
624
- getOauthInfo: jest.fn().mockResolvedValue({}),
625
- authValidation: jest.fn().mockResolvedValue(mockValidationResponse)
626
- });
627
-
628
- connectorRegistry.getConnector.mockReturnValue(mockConnector);
629
-
630
- // Mock UserModel.findOne to return a user
631
- const { UserModel } = require('../../models/userModel');
632
- jest.spyOn(UserModel, 'findOne').mockResolvedValue(mockUser);
633
-
634
- // Mock oauth.checkAndRefreshAccessToken
635
- const oauth = require('../../lib/oauth');
636
- jest.spyOn(oauth, 'checkAndRefreshAccessToken').mockResolvedValue(mockUser);
637
-
638
- const requestData = {
639
- platform: 'testCRM',
640
- userId: 'test-user-id'
641
- };
642
-
643
- // Act
644
- const result = await authHandler.authValidation(requestData);
645
-
646
- // Assert
647
- expect(result).toEqual({
648
- ...mockValidationResponse,
649
- failReason: 'CRM. API failed'
650
- });
651
- expect(result.successful).toBe(false);
652
- });
653
- });
654
-
655
- describe('onOAuthCallback', () => {
656
- const mockOAuthApp = {
657
- code: {
658
- getToken: jest.fn()
659
- }
660
- };
661
-
662
- beforeEach(() => {
663
- oauth.getOAuthApp.mockReturnValue(mockOAuthApp);
664
- });
665
-
666
- test('should handle successful OAuth callback', async () => {
667
- // Arrange
668
- const mockUserInfo = {
669
- successful: true,
670
- platformUserInfo: {
671
- id: 'oauth-user-id',
672
- name: 'OAuth User',
673
- timezoneName: 'America/New_York',
674
- timezoneOffset: -300,
675
- platformAdditionalInfo: {}
676
- },
677
- returnMessage: {
678
- messageType: 'success',
679
- message: 'Connected successfully'
680
- }
681
- };
682
-
683
- const mockConnector = global.testUtils.createMockConnector({
684
- getOauthInfo: jest.fn().mockResolvedValue({
685
- clientId: 'client-id',
686
- clientSecret: 'client-secret',
687
- accessTokenUri: 'https://api.example.com/oauth/token',
688
- authorizationUri: 'https://api.example.com/oauth/authorize'
689
- }),
690
- getUserInfo: jest.fn().mockResolvedValue(mockUserInfo)
691
- });
692
-
693
- connectorRegistry.getConnector.mockReturnValue(mockConnector);
694
-
695
- mockOAuthApp.code.getToken.mockResolvedValue({
696
- accessToken: 'new-access-token',
697
- refreshToken: 'new-refresh-token',
698
- expires: new Date()
699
- });
700
-
701
- const requestData = {
702
- platform: 'testCRM',
703
- hostname: 'api.example.com',
704
- tokenUrl: 'https://api.example.com/oauth/token',
705
- query: {
706
- callbackUri: 'https://app.example.com/callback?code=auth-code-123',
707
- rcAccountId: 'rc-account-123'
708
- }
709
- };
710
-
711
- // Act
712
- const result = await authHandler.onOAuthCallback(requestData);
713
-
714
- // Assert
715
- expect(result.userInfo).toBeDefined();
716
- expect(result.userInfo.id).toBe('oauth-user-id');
717
- expect(oauth.getOAuthApp).toHaveBeenCalled();
718
- expect(mockOAuthApp.code.getToken).toHaveBeenCalled();
719
- });
720
-
721
- test('should return fail message when oauthInfo has error', async () => {
722
- // Arrange
723
- const mockConnector = global.testUtils.createMockConnector({
724
- getOauthInfo: jest.fn().mockResolvedValue({
725
- failMessage: 'OAuth configuration not found'
726
- })
727
- });
728
-
729
- connectorRegistry.getConnector.mockReturnValue(mockConnector);
730
-
731
- const requestData = {
732
- platform: 'testCRM',
733
- hostname: 'api.example.com',
734
- tokenUrl: '',
735
- query: { callbackUri: 'https://app.example.com/callback', rcAccountId: 'rc-123' }
736
- };
737
-
738
- // Act
739
- const result = await authHandler.onOAuthCallback(requestData);
740
-
741
- // Assert
742
- expect(result.userInfo).toBeNull();
743
- expect(result.returnMessage.messageType).toBe('danger');
744
- expect(result.returnMessage.message).toBe('OAuth configuration not found');
745
- });
746
-
747
- test('should handle failed user info retrieval', async () => {
748
- // Arrange
749
- const mockConnector = global.testUtils.createMockConnector({
750
- getOauthInfo: jest.fn().mockResolvedValue({ clientId: 'id', clientSecret: 'secret' }),
751
- getUserInfo: jest.fn().mockResolvedValue({
752
- successful: false,
753
- returnMessage: { messageType: 'error', message: 'User not authorized' }
754
- })
755
- });
756
-
757
- connectorRegistry.getConnector.mockReturnValue(mockConnector);
758
-
759
- mockOAuthApp.code.getToken.mockResolvedValue({
760
- accessToken: 'token',
761
- refreshToken: 'refresh',
762
- expires: new Date()
763
- });
764
-
765
- const requestData = {
766
- platform: 'testCRM',
767
- hostname: 'api.example.com',
768
- tokenUrl: '',
769
- query: { callbackUri: 'https://app.example.com/callback', rcAccountId: 'rc-123' }
770
- };
771
-
772
- // Act
773
- const result = await authHandler.onOAuthCallback(requestData);
774
-
775
- // Assert
776
- expect(result.userInfo).toBeNull();
777
- expect(result.returnMessage.message).toBe('User not authorized');
778
- });
779
-
780
- test('should handle proxyId in OAuth callback', async () => {
781
- // Arrange
782
- const proxyConfig = { name: 'Proxy Config', settings: {} };
783
- Connector.getProxyConfig.mockResolvedValue(proxyConfig);
784
-
785
- const mockConnector = global.testUtils.createMockConnector({
786
- getOauthInfo: jest.fn().mockResolvedValue({ clientId: 'id', clientSecret: 'secret' }),
787
- getUserInfo: jest.fn().mockResolvedValue({
788
- successful: true,
789
- platformUserInfo: { id: 'proxy-user', name: 'Proxy User' },
790
- returnMessage: { messageType: 'success', message: 'OK' }
791
- })
792
- });
793
-
794
- connectorRegistry.getConnector.mockReturnValue(mockConnector);
795
-
796
- mockOAuthApp.code.getToken.mockResolvedValue({
797
- accessToken: 'token',
798
- refreshToken: 'refresh',
799
- expires: new Date()
800
- });
801
-
802
- const requestData = {
803
- platform: 'testCRM',
804
- hostname: 'api.example.com',
805
- tokenUrl: '',
806
- query: {
807
- callbackUri: 'https://app.example.com/callback',
808
- proxyId: 'proxy-123',
809
- rcAccountId: 'rc-123'
810
- }
811
- };
812
-
813
- // Act
814
- await authHandler.onOAuthCallback(requestData);
815
-
816
- // Assert
817
- expect(Connector.getProxyConfig).toHaveBeenCalledWith('proxy-123');
818
- });
819
-
820
- test('should call postSaveUserInfo if platform implements it', async () => {
821
- // Arrange
822
- const postSaveResult = { id: 'user-id', name: 'User', extra: 'data' };
823
- const mockConnector = global.testUtils.createMockConnector({
824
- getOauthInfo: jest.fn().mockResolvedValue({ clientId: 'id', clientSecret: 'secret' }),
825
- getUserInfo: jest.fn().mockResolvedValue({
826
- successful: true,
827
- platformUserInfo: { id: 'user-id', name: 'User' },
828
- returnMessage: { messageType: 'success', message: 'OK' }
829
- }),
830
- postSaveUserInfo: jest.fn().mockResolvedValue(postSaveResult)
831
- });
832
-
833
- connectorRegistry.getConnector.mockReturnValue(mockConnector);
834
-
835
- mockOAuthApp.code.getToken.mockResolvedValue({
836
- accessToken: 'token',
837
- refreshToken: 'refresh',
838
- expires: new Date()
839
- });
840
-
841
- const requestData = {
842
- platform: 'testCRM',
843
- hostname: 'api.example.com',
844
- tokenUrl: '',
845
- query: { callbackUri: 'https://app.example.com/callback', rcAccountId: 'rc-123' }
846
- };
847
-
848
- // Act
849
- const result = await authHandler.onOAuthCallback(requestData);
850
-
851
- // Assert
852
- expect(mockConnector.postSaveUserInfo).toHaveBeenCalled();
853
- expect(result.userInfo).toEqual(postSaveResult);
854
- });
855
-
856
- test('should use overriding OAuth option if provided', async () => {
857
- // Arrange
858
- const overridingOption = { redirect_uri: 'custom-redirect' };
859
- const mockConnector = global.testUtils.createMockConnector({
860
- getOauthInfo: jest.fn().mockResolvedValue({ clientId: 'id', clientSecret: 'secret' }),
861
- getOverridingOAuthOption: jest.fn().mockReturnValue(overridingOption),
862
- getUserInfo: jest.fn().mockResolvedValue({
863
- successful: true,
864
- platformUserInfo: { id: 'user-id', name: 'User' },
865
- returnMessage: { messageType: 'success', message: 'OK' }
866
- })
867
- });
868
-
869
- connectorRegistry.getConnector.mockReturnValue(mockConnector);
870
-
871
- mockOAuthApp.code.getToken.mockResolvedValue({
872
- accessToken: 'token',
873
- refreshToken: 'refresh',
874
- expires: new Date()
875
- });
876
-
877
- const requestData = {
878
- platform: 'testCRM',
879
- hostname: 'api.example.com',
880
- tokenUrl: '',
881
- query: { callbackUri: 'https://app.example.com/callback?code=code123', rcAccountId: 'rc-123' }
882
- };
883
-
884
- // Act
885
- await authHandler.onOAuthCallback(requestData);
886
-
887
- // Assert
888
- expect(mockConnector.getOverridingOAuthOption).toHaveBeenCalledWith({ code: 'code123' });
889
- expect(mockOAuthApp.code.getToken).toHaveBeenCalledWith(
890
- expect.any(String),
891
- overridingOption
892
- );
893
- });
894
- });
895
-
896
- describe('getLicenseStatus', () => {
897
- test('should return license status from platform module', async () => {
898
- // Arrange
899
- const mockUser = global.testUtils.createMockUser({ id: 'user-123' });
900
- const mockLicenseStatus = {
901
- isValid: true,
902
- expiresAt: '2025-12-31',
903
- features: ['call_logging', 'sms_logging']
904
- };
905
-
906
- const mockConnector = global.testUtils.createMockConnector({
907
- getLicenseStatus: jest.fn().mockResolvedValue(mockLicenseStatus)
908
- });
909
-
910
- connectorRegistry.getConnector.mockReturnValue(mockConnector);
911
-
912
- const { UserModel } = require('../../models/userModel');
913
- jest.spyOn(UserModel, 'findByPk').mockResolvedValue(mockUser);
914
-
915
- // Act
916
- const result = await authHandler.getLicenseStatus({
917
- userId: 'user-123',
918
- platform: 'testCRM'
919
- });
920
-
921
- // Assert
922
- expect(result).toEqual(mockLicenseStatus);
923
- expect(mockConnector.getLicenseStatus).toHaveBeenCalledWith({
924
- userId: 'user-123',
925
- platform: 'testCRM',
926
- user: mockUser
927
- });
928
- });
929
-
930
- test('should return invalid license status when user not found', async () => {
931
- // Arrange
932
- const mockConnector = global.testUtils.createMockConnector({
933
- getLicenseStatus: jest.fn()
934
- });
935
- connectorRegistry.getConnector.mockReturnValue(mockConnector);
936
-
937
- const { UserModel } = require('../../models/userModel');
938
- jest.spyOn(UserModel, 'findByPk').mockResolvedValue(null);
939
-
940
- // Act
941
- const result = await authHandler.getLicenseStatus({
942
- userId: 'missing-user',
943
- platform: 'testCRM'
944
- });
945
-
946
- // Assert
947
- expect(result).toEqual({
948
- isLicenseValid: false,
949
- licenseStatus: 'Invalid (User not found)',
950
- licenseStatusDescription: ''
951
- });
952
- expect(connectorRegistry.getConnector).not.toHaveBeenCalled();
953
- expect(mockConnector.getLicenseStatus).not.toHaveBeenCalled();
954
- });
955
- });
956
-
957
- describe('onRingcentralOAuthCallback', () => {
958
- beforeEach(() => {
959
- process.env.RINGCENTRAL_SERVER = 'https://platform.ringcentral.com';
960
- process.env.RINGCENTRAL_CLIENT_ID = 'rc-client-id';
961
- process.env.RINGCENTRAL_CLIENT_SECRET = 'rc-client-secret';
962
- process.env.APP_SERVER = 'https://app.example.com';
963
- });
964
-
965
- test('should handle successful RingCentral OAuth callback', async () => {
966
- // Arrange
967
- const mockGenerateToken = jest.fn().mockResolvedValue({
968
- access_token: 'rc-access-token',
969
- refresh_token: 'rc-refresh-token',
970
- expire_time: Date.now() + 3600000
971
- });
972
-
973
- RingCentral.mockImplementation(() => ({
974
- generateToken: mockGenerateToken
975
- }));
976
-
977
- // Act
978
- await authHandler.onRingcentralOAuthCallback({
979
- code: 'rc-auth-code',
980
- rcAccountId: 'hashed-rc-account-id'
981
- });
982
-
983
- // Assert
984
- expect(RingCentral).toHaveBeenCalledWith({
985
- server: 'https://platform.ringcentral.com',
986
- clientId: 'rc-client-id',
987
- clientSecret: 'rc-client-secret',
988
- redirectUri: 'https://app.example.com/ringcentral/oauth/callback'
989
- });
990
- expect(mockGenerateToken).toHaveBeenCalledWith({ code: 'rc-auth-code' });
991
- expect(adminCore.updateAdminRcTokens).toHaveBeenCalledWith({
992
- hashedRcAccountId: 'hashed-rc-account-id',
993
- adminAccessToken: 'rc-access-token',
994
- adminRefreshToken: 'rc-refresh-token',
995
- adminTokenExpiry: expect.any(Number)
996
- });
997
- });
998
-
999
- test('should return early if environment variables are not set', async () => {
1000
- // Arrange
1001
- delete process.env.RINGCENTRAL_SERVER;
1002
-
1003
- // Act
1004
- const result = await authHandler.onRingcentralOAuthCallback({
1005
- code: 'rc-auth-code',
1006
- rcAccountId: 'hashed-rc-account-id'
1007
- });
1008
-
1009
- // Assert
1010
- expect(result).toBeUndefined();
1011
- expect(RingCentral).not.toHaveBeenCalled();
1012
- });
1013
- });
1014
- });
1015
-
1
+ const authHandler = require('../../handlers/auth');
2
+ const connectorRegistry = require('../../connector/registry');
3
+
4
+ // Mock the connector registry
5
+ jest.mock('../../connector/registry');
6
+ jest.mock('../../lib/oauth');
7
+ jest.mock('../../models/dynamo/connectorSchema', () => ({
8
+ Connector: {
9
+ getProxyConfig: jest.fn()
10
+ }
11
+ }));
12
+ jest.mock('../../lib/ringcentral', () => ({
13
+ RingCentral: jest.fn().mockImplementation(() => ({
14
+ generateToken: jest.fn()
15
+ }))
16
+ }));
17
+ jest.mock('../../handlers/admin', () => ({
18
+ updateAdminRcTokens: jest.fn()
19
+ }));
20
+
21
+ const oauth = require('../../lib/oauth');
22
+ const { Connector } = require('../../models/dynamo/connectorSchema');
23
+ const { RingCentral } = require('../../lib/ringcentral');
24
+ const adminCore = require('../../handlers/admin');
25
+ const { AccountDataModel } = require('../../models/accountDataModel');
26
+ const { encode } = require('../../lib/encode');
27
+ const { getHashValue } = require('../../lib/util');
28
+
29
+ describe('Auth Handler', () => {
30
+ const originalEnv = process.env;
31
+
32
+ beforeEach(() => {
33
+ // Reset mocks
34
+ jest.clearAllMocks();
35
+ global.testUtils.resetConnectorRegistry();
36
+ process.env = { ...originalEnv };
37
+ process.env.APP_SERVER_SECRET_KEY = 'test-app-server-secret-key-123456';
38
+ });
39
+
40
+ afterEach(() => {
41
+ process.env = originalEnv;
42
+ });
43
+
44
+ describe('onApiKeyLogin', () => {
45
+ afterEach(async () => {
46
+ await AccountDataModel.destroy({ where: {} });
47
+ });
48
+
49
+ test('should handle successful API key login', async () => {
50
+ // Arrange
51
+ const mockUserInfo = {
52
+ successful: true,
53
+ platformUserInfo: {
54
+ id: 'test-user-id',
55
+ name: 'Test User',
56
+ timezoneName: 'America/Los_Angeles',
57
+ timezoneOffset: 0,
58
+ platformAdditionalInfo: {}
59
+ },
60
+ returnMessage: {
61
+ messageType: 'success',
62
+ message: 'Login successful',
63
+ ttl: 1000
64
+ }
65
+ };
66
+
67
+ const mockConnector = global.testUtils.createMockConnector({
68
+ getBasicAuth: jest.fn().mockReturnValue('dGVzdC1hcGkta2V5Og=='),
69
+ getUserInfo: jest.fn().mockResolvedValue(mockUserInfo)
70
+ });
71
+
72
+ connectorRegistry.getConnector.mockReturnValue(mockConnector);
73
+
74
+ const requestData = {
75
+ platform: 'testCRM',
76
+ hostname: 'test.example.com',
77
+ apiKey: 'test-api-key',
78
+ additionalInfo: {}
79
+ };
80
+
81
+ // Act
82
+ const result = await authHandler.onApiKeyLogin(requestData);
83
+
84
+ // Assert
85
+ expect(result.userInfo).toBeDefined();
86
+ expect(result.userInfo.id).toBe('test-user-id');
87
+ expect(result.userInfo.name).toBe('Test User');
88
+ expect(result.returnMessage).toEqual(mockUserInfo.returnMessage);
89
+ expect(mockConnector.getBasicAuth).toHaveBeenCalledWith({ apiKey: 'test-api-key' });
90
+ expect(mockConnector.getUserInfo).toHaveBeenCalledWith({
91
+ authHeader: 'Basic dGVzdC1hcGkta2V5Og==',
92
+ hostname: 'test.example.com',
93
+ additionalInfo: { apiKey: 'test-api-key' },
94
+ apiKey: 'test-api-key',
95
+ platform: 'testCRM',
96
+ proxyId: undefined
97
+ });
98
+ });
99
+
100
+ test('should handle failed API key login', async () => {
101
+ // Arrange
102
+ const mockUserInfo = {
103
+ successful: false,
104
+ platformUserInfo: null,
105
+ returnMessage: {
106
+ messageType: 'error',
107
+ message: 'Invalid API key',
108
+ ttl: 3000
109
+ }
110
+ };
111
+
112
+ const mockConnector = global.testUtils.createMockConnector({
113
+ getBasicAuth: jest.fn().mockReturnValue('dGVzdC1hcGkta2V5Og=='),
114
+ getUserInfo: jest.fn().mockResolvedValue(mockUserInfo)
115
+ });
116
+
117
+ connectorRegistry.getConnector.mockReturnValue(mockConnector);
118
+
119
+ const requestData = {
120
+ platform: 'testCRM',
121
+ hostname: 'test.example.com',
122
+ apiKey: 'invalid-api-key',
123
+ additionalInfo: {}
124
+ };
125
+
126
+ // Act
127
+ const result = await authHandler.onApiKeyLogin(requestData);
128
+
129
+ // Assert
130
+ expect(result.userInfo).toBeNull();
131
+ expect(result.returnMessage).toEqual(mockUserInfo.returnMessage);
132
+ });
133
+
134
+ test('should mark managed auth auto-login failure so the next attempt can fall back to manual auth', async () => {
135
+ connectorRegistry.getManifest.mockReturnValue({
136
+ platforms: {
137
+ testCRM: {
138
+ auth: {
139
+ type: 'apiKey',
140
+ apiKey: {
141
+ page: {
142
+ content: [
143
+ { const: 'tenantId', required: true, managed: true, managedScope: 'account' },
144
+ { const: 'apiKey', required: true, managed: true, managedScope: 'user' }
145
+ ]
146
+ }
147
+ }
148
+ }
149
+ }
150
+ }
151
+ });
152
+
153
+ await AccountDataModel.create({
154
+ rcAccountId: 'rc-account-fail',
155
+ platformName: 'testCRM',
156
+ dataKey: 'managed-auth-org',
157
+ data: {
158
+ fields: {
159
+ tenantId: { version: 1, encrypted: true, value: encode(JSON.stringify('tenant-1')) }
160
+ }
161
+ }
162
+ });
163
+ await AccountDataModel.create({
164
+ rcAccountId: 'rc-account-fail',
165
+ platformName: 'testCRM',
166
+ dataKey: 'managed-auth-user:101',
167
+ data: {
168
+ rcExtensionId: '101',
169
+ rcUserName: 'Agent 101',
170
+ fields: {
171
+ apiKey: { version: 1, encrypted: true, value: encode(JSON.stringify('bad-stored-key')) }
172
+ }
173
+ }
174
+ });
175
+
176
+ const mockConnector = global.testUtils.createMockConnector({
177
+ getBasicAuth: jest.fn().mockReturnValue('encoded-bad-key'),
178
+ getUserInfo: jest.fn().mockResolvedValue({
179
+ successful: false,
180
+ platformUserInfo: null,
181
+ returnMessage: {
182
+ messageType: 'error',
183
+ message: 'Invalid API key',
184
+ ttl: 3000
185
+ }
186
+ })
187
+ });
188
+ connectorRegistry.getConnector.mockReturnValue(mockConnector);
189
+
190
+ const result = await authHandler.onApiKeyLogin({
191
+ platform: 'testCRM',
192
+ hostname: 'test.example.com',
193
+ rcAccountId: 'rc-account-fail',
194
+ rcExtensionId: '101',
195
+ additionalInfo: {}
196
+ });
197
+
198
+ expect(result.userInfo).toBeNull();
199
+ const failureRecord = await AccountDataModel.findOne({
200
+ where: {
201
+ rcAccountId: 'rc-account-fail',
202
+ platformName: 'testCRM',
203
+ dataKey: 'managed-auth-login-failure:101'
204
+ }
205
+ });
206
+ expect(failureRecord).not.toBeNull();
207
+ });
208
+
209
+ test('should merge stored org managed auth values into additionalInfo', async () => {
210
+ connectorRegistry.getManifest.mockReturnValue({
211
+ platforms: {
212
+ testCRM: {
213
+ auth: {
214
+ type: 'apiKey',
215
+ apiKey: {
216
+ page: {
217
+ content: [
218
+ { const: 'apiKey', required: true, managed: true, managedScope: 'account' },
219
+ { const: 'tenantId', required: true, managed: true, managedScope: 'account' },
220
+ { const: 'userToken', required: true }
221
+ ]
222
+ }
223
+ }
224
+ }
225
+ }
226
+ }
227
+ });
228
+ await AccountDataModel.create({
229
+ rcAccountId: 'rc-account-1',
230
+ platformName: 'testCRM',
231
+ dataKey: 'managed-auth-org',
232
+ data: {
233
+ fields: {
234
+ apiKey: { version: 1, encrypted: true, value: encode(JSON.stringify('stored-api-key')) },
235
+ tenantId: { version: 1, encrypted: true, value: encode(JSON.stringify('tenant-1')) }
236
+ }
237
+ }
238
+ });
239
+
240
+ const mockUserInfo = {
241
+ successful: true,
242
+ platformUserInfo: {
243
+ id: 'test-user-id',
244
+ name: 'Test User',
245
+ platformAdditionalInfo: {}
246
+ },
247
+ returnMessage: { messageType: 'success', message: 'ok' }
248
+ };
249
+ const mockConnector = global.testUtils.createMockConnector({
250
+ getBasicAuth: jest.fn().mockReturnValue('encoded-shared'),
251
+ getUserInfo: jest.fn().mockResolvedValue(mockUserInfo)
252
+ });
253
+ connectorRegistry.getConnector.mockReturnValue(mockConnector);
254
+
255
+ await authHandler.onApiKeyLogin({
256
+ platform: 'testCRM',
257
+ hostname: 'test.example.com',
258
+ rcAccountId: 'rc-account-1',
259
+ additionalInfo: { userToken: 'user-token-1' }
260
+ });
261
+
262
+ expect(mockConnector.getBasicAuth).toHaveBeenCalledWith({ apiKey: 'stored-api-key' });
263
+ expect(mockConnector.getUserInfo).toHaveBeenCalledWith(expect.objectContaining({
264
+ additionalInfo: expect.objectContaining({
265
+ apiKey: 'stored-api-key',
266
+ tenantId: 'tenant-1',
267
+ userToken: 'user-token-1'
268
+ })
269
+ }));
270
+ });
271
+
272
+ test('should allow submitted shared fields to satisfy missing required managed auth values', async () => {
273
+ connectorRegistry.getManifest.mockReturnValue({
274
+ platforms: {
275
+ testCRM: {
276
+ auth: {
277
+ type: 'apiKey',
278
+ apiKey: {
279
+ page: {
280
+ content: [
281
+ { const: 'companyId', required: true, managed: true, managedScope: 'account' },
282
+ { const: 'userToken', required: true }
283
+ ]
284
+ }
285
+ }
286
+ }
287
+ }
288
+ }
289
+ });
290
+
291
+ const mockConnector = global.testUtils.createMockConnector({
292
+ getBasicAuth: jest.fn(),
293
+ getUserInfo: jest.fn().mockResolvedValue({
294
+ successful: true,
295
+ platformUserInfo: {
296
+ id: 'test-user-id',
297
+ name: 'Test User',
298
+ platformAdditionalInfo: {}
299
+ },
300
+ returnMessage: { messageType: 'success', message: 'ok' }
301
+ })
302
+ });
303
+ connectorRegistry.getConnector.mockReturnValue(mockConnector);
304
+
305
+ const result = await authHandler.onApiKeyLogin({
306
+ platform: 'testCRM',
307
+ hostname: 'test.example.com',
308
+ rcAccountId: 'rc-account-2',
309
+ additionalInfo: {
310
+ companyId: 'company-123',
311
+ userToken: 'user-token-1'
312
+ }
313
+ });
314
+
315
+ expect(result.userInfo).not.toBeNull();
316
+ expect(mockConnector.getUserInfo).toHaveBeenCalledWith(expect.objectContaining({
317
+ additionalInfo: expect.objectContaining({
318
+ companyId: 'company-123',
319
+ userToken: 'user-token-1'
320
+ })
321
+ }));
322
+ });
323
+
324
+ test('should not persist submitted managed auth values from end users', async () => {
325
+ connectorRegistry.getManifest.mockReturnValue({
326
+ platforms: {
327
+ testCRM: {
328
+ auth: {
329
+ type: 'apiKey',
330
+ apiKey: {
331
+ page: {
332
+ content: [
333
+ { const: 'companyId', required: false, managed: true, managedScope: 'account' },
334
+ { const: 'userToken', required: true }
335
+ ]
336
+ }
337
+ }
338
+ }
339
+ }
340
+ }
341
+ });
342
+
343
+ const mockUserInfo = {
344
+ successful: true,
345
+ platformUserInfo: {
346
+ id: 'test-user-id',
347
+ name: 'Test User',
348
+ platformAdditionalInfo: {}
349
+ },
350
+ returnMessage: { messageType: 'success', message: 'ok' }
351
+ };
352
+ const mockConnector = global.testUtils.createMockConnector({
353
+ getBasicAuth: jest.fn().mockReturnValue('encoded'),
354
+ getUserInfo: jest.fn().mockResolvedValue(mockUserInfo)
355
+ });
356
+ connectorRegistry.getConnector.mockReturnValue(mockConnector);
357
+
358
+ await authHandler.onApiKeyLogin({
359
+ platform: 'testCRM',
360
+ hostname: 'test.example.com',
361
+ rcAccountId: 'rc-account-2',
362
+ additionalInfo: {
363
+ companyId: 'company-123',
364
+ userToken: 'user-token-1'
365
+ }
366
+ });
367
+
368
+ expect(mockConnector.getUserInfo).toHaveBeenCalledWith(expect.objectContaining({
369
+ additionalInfo: expect.objectContaining({
370
+ companyId: 'company-123',
371
+ userToken: 'user-token-1'
372
+ })
373
+ }));
374
+
375
+ const stored = await AccountDataModel.findOne({
376
+ where: {
377
+ rcAccountId: 'rc-account-2',
378
+ platformName: 'testCRM',
379
+ dataKey: 'managed-auth-org'
380
+ }
381
+ });
382
+ expect(stored).toBeNull();
383
+ });
384
+
385
+ test('should allow manual fallback values to override stored managed credentials and clear failure state after success', async () => {
386
+ connectorRegistry.getManifest.mockReturnValue({
387
+ platforms: {
388
+ testCRM: {
389
+ auth: {
390
+ type: 'apiKey',
391
+ apiKey: {
392
+ page: {
393
+ content: [
394
+ { const: 'apiKey', required: true, managed: true, managedScope: 'user' },
395
+ { const: 'tenantId', required: true, managed: true, managedScope: 'account' }
396
+ ]
397
+ }
398
+ }
399
+ }
400
+ }
401
+ }
402
+ });
403
+
404
+ await AccountDataModel.create({
405
+ rcAccountId: 'rc-account-recover',
406
+ platformName: 'testCRM',
407
+ dataKey: 'managed-auth-org',
408
+ data: {
409
+ fields: {
410
+ tenantId: { version: 1, encrypted: true, value: encode(JSON.stringify('stored-tenant')) }
411
+ }
412
+ }
413
+ });
414
+ await AccountDataModel.create({
415
+ rcAccountId: 'rc-account-recover',
416
+ platformName: 'testCRM',
417
+ dataKey: 'managed-auth-user:202',
418
+ data: {
419
+ rcExtensionId: '202',
420
+ rcUserName: 'Agent 202',
421
+ fields: {
422
+ apiKey: { version: 1, encrypted: true, value: encode(JSON.stringify('stored-bad-key')) }
423
+ }
424
+ }
425
+ });
426
+ await AccountDataModel.create({
427
+ rcAccountId: 'rc-account-recover',
428
+ platformName: 'testCRM',
429
+ dataKey: 'managed-auth-login-failure:202',
430
+ data: {
431
+ failedAt: '2026-04-07T00:00:00.000Z'
432
+ }
433
+ });
434
+
435
+ const mockConnector = global.testUtils.createMockConnector({
436
+ getBasicAuth: jest.fn().mockReturnValue('encoded-manual-key'),
437
+ getUserInfo: jest.fn().mockResolvedValue({
438
+ successful: true,
439
+ platformUserInfo: {
440
+ id: 'test-user-id',
441
+ name: 'Recovered User',
442
+ platformAdditionalInfo: {}
443
+ },
444
+ returnMessage: { messageType: 'success', message: 'ok' }
445
+ })
446
+ });
447
+ connectorRegistry.getConnector.mockReturnValue(mockConnector);
448
+
449
+ const result = await authHandler.onApiKeyLogin({
450
+ platform: 'testCRM',
451
+ hostname: 'test.example.com',
452
+ rcAccountId: 'rc-account-recover',
453
+ rcExtensionId: '202',
454
+ additionalInfo: {
455
+ apiKey: 'manual-good-key',
456
+ tenantId: 'manual-tenant'
457
+ }
458
+ });
459
+
460
+ expect(result.userInfo).not.toBeNull();
461
+ expect(mockConnector.getBasicAuth).toHaveBeenCalledWith({ apiKey: 'manual-good-key' });
462
+ expect(mockConnector.getUserInfo).toHaveBeenCalledWith(expect.objectContaining({
463
+ additionalInfo: {
464
+ apiKey: 'manual-good-key',
465
+ tenantId: 'manual-tenant'
466
+ }
467
+ }));
468
+
469
+ const failureRecord = await AccountDataModel.findOne({
470
+ where: {
471
+ rcAccountId: 'rc-account-recover',
472
+ platformName: 'testCRM',
473
+ dataKey: 'managed-auth-login-failure:202'
474
+ }
475
+ });
476
+ expect(failureRecord).toBeNull();
477
+ });
478
+
479
+ test('should return warning when required auth fields are missing', async () => {
480
+ connectorRegistry.getManifest.mockReturnValue({
481
+ platforms: {
482
+ testCRM: {
483
+ auth: {
484
+ type: 'apiKey',
485
+ apiKey: {
486
+ page: {
487
+ content: [
488
+ { const: 'tenantId', required: true, managed: true, managedScope: 'account' },
489
+ { const: 'userToken', required: true }
490
+ ]
491
+ }
492
+ }
493
+ }
494
+ }
495
+ }
496
+ });
497
+
498
+ const mockConnector = global.testUtils.createMockConnector({
499
+ getBasicAuth: jest.fn(),
500
+ getUserInfo: jest.fn()
501
+ });
502
+ connectorRegistry.getConnector.mockReturnValue(mockConnector);
503
+
504
+ const result = await authHandler.onApiKeyLogin({
505
+ platform: 'testCRM',
506
+ hostname: 'test.example.com',
507
+ rcAccountId: 'rc-account-4',
508
+ additionalInfo: {}
509
+ });
510
+
511
+ expect(result.userInfo).toBeNull();
512
+ expect(result.returnMessage).toEqual({
513
+ messageType: 'warning',
514
+ message: 'Missing required authentication fields.',
515
+ ttl: 3000,
516
+ missingRequiredFieldConsts: ['tenantId', 'userToken']
517
+ });
518
+ expect(mockConnector.getBasicAuth).not.toHaveBeenCalled();
519
+ expect(mockConnector.getUserInfo).not.toHaveBeenCalled();
520
+ });
521
+
522
+ test('should throw error when connector not found', async () => {
523
+ // Arrange
524
+ connectorRegistry.getConnector.mockImplementation(() => {
525
+ throw new Error('Connector not found for platform: testCRM');
526
+ });
527
+
528
+ const requestData = {
529
+ platform: 'testCRM',
530
+ hostname: 'test.example.com',
531
+ apiKey: 'test-api-key',
532
+ additionalInfo: {}
533
+ };
534
+
535
+ // Act & Assert
536
+ await expect(authHandler.onApiKeyLogin(requestData))
537
+ .rejects.toThrow('Connector not found for platform: testCRM');
538
+ });
539
+ });
540
+
541
+ describe('authValidation', () => {
542
+ test('should validate user authentication successfully', async () => {
543
+ // Arrange
544
+ const mockUser = global.testUtils.createMockUser();
545
+ const mockValidationResponse = {
546
+ successful: true,
547
+ returnMessage: {
548
+ messageType: 'success',
549
+ message: 'Authentication valid',
550
+ ttl: 1000
551
+ },
552
+ status: 200
553
+ };
554
+
555
+ const mockConnector = global.testUtils.createMockConnector({
556
+ getOauthInfo: jest.fn().mockResolvedValue({}),
557
+ authValidation: jest.fn().mockResolvedValue(mockValidationResponse)
558
+ });
559
+
560
+ connectorRegistry.getConnector.mockReturnValue(mockConnector);
561
+
562
+ // Mock UserModel.findOne to return a user
563
+ const { UserModel } = require('../../models/userModel');
564
+ jest.spyOn(UserModel, 'findOne').mockResolvedValue(mockUser);
565
+
566
+ // Mock oauth.checkAndRefreshAccessToken
567
+ const oauth = require('../../lib/oauth');
568
+ jest.spyOn(oauth, 'checkAndRefreshAccessToken').mockResolvedValue(mockUser);
569
+
570
+ const requestData = {
571
+ platform: 'testCRM',
572
+ userId: 'test-user-id'
573
+ };
574
+
575
+ // Act
576
+ const result = await authHandler.authValidation(requestData);
577
+
578
+ // Assert
579
+ expect(result).toEqual({
580
+ ...mockValidationResponse,
581
+ failReason: ''
582
+ });
583
+ expect(mockConnector.authValidation).toHaveBeenCalledWith({ user: mockUser });
584
+ });
585
+
586
+ test('should handle user not found in database', async () => {
587
+ // Arrange
588
+ const mockConnector = global.testUtils.createMockConnector();
589
+ connectorRegistry.getConnector.mockReturnValue(mockConnector);
590
+
591
+ // Mock UserModel.findOne to return null (user not found)
592
+ const { UserModel } = require('../../models/userModel');
593
+ jest.spyOn(UserModel, 'findOne').mockResolvedValue(null);
594
+
595
+ const requestData = {
596
+ platform: 'testCRM',
597
+ userId: 'non-existent-user'
598
+ };
599
+
600
+ // Act
601
+ const result = await authHandler.authValidation(requestData);
602
+
603
+ // Assert
604
+ expect(result).toEqual({
605
+ successful: false,
606
+ status: 404,
607
+ failReason: 'App Connect. User not found in database'
608
+ });
609
+ });
610
+
611
+ test('should handle validation failure', async () => {
612
+ // Arrange
613
+ const mockUser = global.testUtils.createMockUser();
614
+ const mockValidationResponse = {
615
+ successful: false,
616
+ returnMessage: {
617
+ messageType: 'error',
618
+ message: 'Authentication failed',
619
+ ttl: 3000
620
+ },
621
+ status: 401
622
+ };
623
+
624
+ const mockConnector = global.testUtils.createMockConnector({
625
+ getOauthInfo: jest.fn().mockResolvedValue({}),
626
+ authValidation: jest.fn().mockResolvedValue(mockValidationResponse)
627
+ });
628
+
629
+ connectorRegistry.getConnector.mockReturnValue(mockConnector);
630
+
631
+ // Mock UserModel.findOne to return a user
632
+ const { UserModel } = require('../../models/userModel');
633
+ jest.spyOn(UserModel, 'findOne').mockResolvedValue(mockUser);
634
+
635
+ // Mock oauth.checkAndRefreshAccessToken
636
+ const oauth = require('../../lib/oauth');
637
+ jest.spyOn(oauth, 'checkAndRefreshAccessToken').mockResolvedValue(mockUser);
638
+
639
+ const requestData = {
640
+ platform: 'testCRM',
641
+ userId: 'test-user-id'
642
+ };
643
+
644
+ // Act
645
+ const result = await authHandler.authValidation(requestData);
646
+
647
+ // Assert
648
+ expect(result).toEqual({
649
+ ...mockValidationResponse,
650
+ failReason: 'CRM. API failed'
651
+ });
652
+ expect(result.successful).toBe(false);
653
+ });
654
+ });
655
+
656
+ describe('onOAuthCallback', () => {
657
+ const mockOAuthApp = {
658
+ code: {
659
+ getToken: jest.fn()
660
+ }
661
+ };
662
+
663
+ beforeEach(() => {
664
+ oauth.getOAuthApp.mockReturnValue(mockOAuthApp);
665
+ });
666
+
667
+ test('should handle successful OAuth callback', async () => {
668
+ // Arrange
669
+ const mockUserInfo = {
670
+ successful: true,
671
+ platformUserInfo: {
672
+ id: 'oauth-user-id',
673
+ name: 'OAuth User',
674
+ timezoneName: 'America/New_York',
675
+ timezoneOffset: -300,
676
+ platformAdditionalInfo: {}
677
+ },
678
+ returnMessage: {
679
+ messageType: 'success',
680
+ message: 'Connected successfully'
681
+ }
682
+ };
683
+
684
+ const mockConnector = global.testUtils.createMockConnector({
685
+ getOauthInfo: jest.fn().mockResolvedValue({
686
+ clientId: 'client-id',
687
+ clientSecret: 'client-secret',
688
+ accessTokenUri: 'https://api.example.com/oauth/token',
689
+ authorizationUri: 'https://api.example.com/oauth/authorize'
690
+ }),
691
+ getUserInfo: jest.fn().mockResolvedValue(mockUserInfo)
692
+ });
693
+
694
+ connectorRegistry.getConnector.mockReturnValue(mockConnector);
695
+
696
+ mockOAuthApp.code.getToken.mockResolvedValue({
697
+ accessToken: 'new-access-token',
698
+ refreshToken: 'new-refresh-token',
699
+ expires: new Date()
700
+ });
701
+
702
+ const requestData = {
703
+ platform: 'testCRM',
704
+ hostname: 'api.example.com',
705
+ tokenUrl: 'https://api.example.com/oauth/token',
706
+ query: {
707
+ callbackUri: 'https://app.example.com/callback?code=auth-code-123',
708
+ rcAccountId: 'rc-account-123'
709
+ }
710
+ };
711
+
712
+ // Act
713
+ const result = await authHandler.onOAuthCallback(requestData);
714
+
715
+ // Assert
716
+ expect(result.userInfo).toBeDefined();
717
+ expect(result.userInfo.id).toBe('oauth-user-id');
718
+ expect(oauth.getOAuthApp).toHaveBeenCalled();
719
+ expect(mockOAuthApp.code.getToken).toHaveBeenCalled();
720
+ });
721
+
722
+ test('should return fail message when oauthInfo has error', async () => {
723
+ // Arrange
724
+ const mockConnector = global.testUtils.createMockConnector({
725
+ getOauthInfo: jest.fn().mockResolvedValue({
726
+ failMessage: 'OAuth configuration not found'
727
+ })
728
+ });
729
+
730
+ connectorRegistry.getConnector.mockReturnValue(mockConnector);
731
+
732
+ const requestData = {
733
+ platform: 'testCRM',
734
+ hostname: 'api.example.com',
735
+ tokenUrl: '',
736
+ query: { callbackUri: 'https://app.example.com/callback', rcAccountId: 'rc-123' }
737
+ };
738
+
739
+ // Act
740
+ const result = await authHandler.onOAuthCallback(requestData);
741
+
742
+ // Assert
743
+ expect(result.userInfo).toBeNull();
744
+ expect(result.returnMessage.messageType).toBe('danger');
745
+ expect(result.returnMessage.message).toBe('OAuth configuration not found');
746
+ });
747
+
748
+ test('should handle failed user info retrieval', async () => {
749
+ // Arrange
750
+ const mockConnector = global.testUtils.createMockConnector({
751
+ getOauthInfo: jest.fn().mockResolvedValue({ clientId: 'id', clientSecret: 'secret' }),
752
+ getUserInfo: jest.fn().mockResolvedValue({
753
+ successful: false,
754
+ returnMessage: { messageType: 'error', message: 'User not authorized' }
755
+ })
756
+ });
757
+
758
+ connectorRegistry.getConnector.mockReturnValue(mockConnector);
759
+
760
+ mockOAuthApp.code.getToken.mockResolvedValue({
761
+ accessToken: 'token',
762
+ refreshToken: 'refresh',
763
+ expires: new Date()
764
+ });
765
+
766
+ const requestData = {
767
+ platform: 'testCRM',
768
+ hostname: 'api.example.com',
769
+ tokenUrl: '',
770
+ query: { callbackUri: 'https://app.example.com/callback', rcAccountId: 'rc-123' }
771
+ };
772
+
773
+ // Act
774
+ const result = await authHandler.onOAuthCallback(requestData);
775
+
776
+ // Assert
777
+ expect(result.userInfo).toBeNull();
778
+ expect(result.returnMessage.message).toBe('User not authorized');
779
+ });
780
+
781
+ test('should handle proxyId in OAuth callback', async () => {
782
+ // Arrange
783
+ const proxyConfig = { name: 'Proxy Config', settings: {} };
784
+ Connector.getProxyConfig.mockResolvedValue(proxyConfig);
785
+
786
+ const mockConnector = global.testUtils.createMockConnector({
787
+ getOauthInfo: jest.fn().mockResolvedValue({ clientId: 'id', clientSecret: 'secret' }),
788
+ getUserInfo: jest.fn().mockResolvedValue({
789
+ successful: true,
790
+ platformUserInfo: { id: 'proxy-user', name: 'Proxy User' },
791
+ returnMessage: { messageType: 'success', message: 'OK' }
792
+ })
793
+ });
794
+
795
+ connectorRegistry.getConnector.mockReturnValue(mockConnector);
796
+
797
+ mockOAuthApp.code.getToken.mockResolvedValue({
798
+ accessToken: 'token',
799
+ refreshToken: 'refresh',
800
+ expires: new Date()
801
+ });
802
+
803
+ const requestData = {
804
+ platform: 'testCRM',
805
+ hostname: 'api.example.com',
806
+ tokenUrl: '',
807
+ query: {
808
+ callbackUri: 'https://app.example.com/callback',
809
+ proxyId: 'proxy-123',
810
+ rcAccountId: 'rc-123'
811
+ }
812
+ };
813
+
814
+ // Act
815
+ await authHandler.onOAuthCallback(requestData);
816
+
817
+ // Assert
818
+ expect(Connector.getProxyConfig).toHaveBeenCalledWith('proxy-123');
819
+ });
820
+
821
+ test('should call postSaveUserInfo if platform implements it', async () => {
822
+ // Arrange
823
+ const postSaveResult = { id: 'user-id', name: 'User', extra: 'data' };
824
+ const mockConnector = global.testUtils.createMockConnector({
825
+ getOauthInfo: jest.fn().mockResolvedValue({ clientId: 'id', clientSecret: 'secret' }),
826
+ getUserInfo: jest.fn().mockResolvedValue({
827
+ successful: true,
828
+ platformUserInfo: { id: 'user-id', name: 'User' },
829
+ returnMessage: { messageType: 'success', message: 'OK' }
830
+ }),
831
+ postSaveUserInfo: jest.fn().mockResolvedValue(postSaveResult)
832
+ });
833
+
834
+ connectorRegistry.getConnector.mockReturnValue(mockConnector);
835
+
836
+ mockOAuthApp.code.getToken.mockResolvedValue({
837
+ accessToken: 'token',
838
+ refreshToken: 'refresh',
839
+ expires: new Date()
840
+ });
841
+
842
+ const requestData = {
843
+ platform: 'testCRM',
844
+ hostname: 'api.example.com',
845
+ tokenUrl: '',
846
+ query: { callbackUri: 'https://app.example.com/callback', rcAccountId: 'rc-123' }
847
+ };
848
+
849
+ // Act
850
+ const result = await authHandler.onOAuthCallback(requestData);
851
+
852
+ // Assert
853
+ expect(mockConnector.postSaveUserInfo).toHaveBeenCalled();
854
+ expect(result.userInfo).toEqual(postSaveResult);
855
+ });
856
+
857
+ test('should use overriding OAuth option if provided', async () => {
858
+ // Arrange
859
+ const overridingOption = { redirect_uri: 'custom-redirect' };
860
+ const mockConnector = global.testUtils.createMockConnector({
861
+ getOauthInfo: jest.fn().mockResolvedValue({ clientId: 'id', clientSecret: 'secret' }),
862
+ getOverridingOAuthOption: jest.fn().mockReturnValue(overridingOption),
863
+ getUserInfo: jest.fn().mockResolvedValue({
864
+ successful: true,
865
+ platformUserInfo: { id: 'user-id', name: 'User' },
866
+ returnMessage: { messageType: 'success', message: 'OK' }
867
+ })
868
+ });
869
+
870
+ connectorRegistry.getConnector.mockReturnValue(mockConnector);
871
+
872
+ mockOAuthApp.code.getToken.mockResolvedValue({
873
+ accessToken: 'token',
874
+ refreshToken: 'refresh',
875
+ expires: new Date()
876
+ });
877
+
878
+ const requestData = {
879
+ platform: 'testCRM',
880
+ hostname: 'api.example.com',
881
+ tokenUrl: '',
882
+ query: { callbackUri: 'https://app.example.com/callback?code=code123', rcAccountId: 'rc-123' }
883
+ };
884
+
885
+ // Act
886
+ await authHandler.onOAuthCallback(requestData);
887
+
888
+ // Assert
889
+ expect(mockConnector.getOverridingOAuthOption).toHaveBeenCalledWith({ code: 'code123' });
890
+ expect(mockOAuthApp.code.getToken).toHaveBeenCalledWith(
891
+ expect.any(String),
892
+ overridingOption
893
+ );
894
+ });
895
+ });
896
+
897
+ describe('getLicenseStatus', () => {
898
+ test('should return license status from platform module', async () => {
899
+ // Arrange
900
+ const mockUser = global.testUtils.createMockUser({ id: 'user-123' });
901
+ const mockLicenseStatus = {
902
+ isValid: true,
903
+ expiresAt: '2025-12-31',
904
+ features: ['call_logging', 'sms_logging']
905
+ };
906
+
907
+ const mockConnector = global.testUtils.createMockConnector({
908
+ getLicenseStatus: jest.fn().mockResolvedValue(mockLicenseStatus)
909
+ });
910
+
911
+ connectorRegistry.getConnector.mockReturnValue(mockConnector);
912
+
913
+ const { UserModel } = require('../../models/userModel');
914
+ jest.spyOn(UserModel, 'findByPk').mockResolvedValue(mockUser);
915
+
916
+ // Act
917
+ const result = await authHandler.getLicenseStatus({
918
+ userId: 'user-123',
919
+ platform: 'testCRM'
920
+ });
921
+
922
+ // Assert
923
+ expect(result).toEqual(mockLicenseStatus);
924
+ expect(mockConnector.getLicenseStatus).toHaveBeenCalledWith({
925
+ userId: 'user-123',
926
+ platform: 'testCRM',
927
+ user: mockUser
928
+ });
929
+ });
930
+
931
+ test('should return invalid license status when user not found', async () => {
932
+ // Arrange
933
+ const mockConnector = global.testUtils.createMockConnector({
934
+ getLicenseStatus: jest.fn()
935
+ });
936
+ connectorRegistry.getConnector.mockReturnValue(mockConnector);
937
+
938
+ const { UserModel } = require('../../models/userModel');
939
+ jest.spyOn(UserModel, 'findByPk').mockResolvedValue(null);
940
+
941
+ // Act
942
+ const result = await authHandler.getLicenseStatus({
943
+ userId: 'missing-user',
944
+ platform: 'testCRM'
945
+ });
946
+
947
+ // Assert
948
+ expect(result).toEqual({
949
+ isLicenseValid: false,
950
+ licenseStatus: 'Invalid (User not found)',
951
+ licenseStatusDescription: ''
952
+ });
953
+ expect(connectorRegistry.getConnector).not.toHaveBeenCalled();
954
+ expect(mockConnector.getLicenseStatus).not.toHaveBeenCalled();
955
+ });
956
+ });
957
+
958
+ describe('onRingcentralOAuthCallback', () => {
959
+ beforeEach(() => {
960
+ process.env.RINGCENTRAL_SERVER = 'https://platform.ringcentral.com';
961
+ process.env.RINGCENTRAL_CLIENT_ID = 'rc-client-id';
962
+ process.env.RINGCENTRAL_CLIENT_SECRET = 'rc-client-secret';
963
+ process.env.APP_SERVER = 'https://app.example.com';
964
+ });
965
+
966
+ test('should handle successful RingCentral OAuth callback', async () => {
967
+ // Arrange
968
+ process.env.HASH_KEY = 'test-hash-key';
969
+ const rcAccountId = 'rc-account-id';
970
+ const mockGenerateToken = jest.fn().mockResolvedValue({
971
+ access_token: 'rc-access-token',
972
+ refresh_token: 'rc-refresh-token',
973
+ expire_time: Date.now() + 3600000
974
+ });
975
+
976
+ RingCentral.mockImplementation(() => ({
977
+ generateToken: mockGenerateToken
978
+ }));
979
+
980
+ // Act
981
+ await authHandler.onRingcentralOAuthCallback({
982
+ code: 'rc-auth-code',
983
+ rcAccountId
984
+ });
985
+
986
+ // Assert
987
+ expect(RingCentral).toHaveBeenCalledWith({
988
+ server: 'https://platform.ringcentral.com',
989
+ clientId: 'rc-client-id',
990
+ clientSecret: 'rc-client-secret',
991
+ redirectUri: 'https://app.example.com/ringcentral/oauth/callback'
992
+ });
993
+ expect(mockGenerateToken).toHaveBeenCalledWith({ code: 'rc-auth-code' });
994
+ expect(adminCore.updateAdminRcTokens).toHaveBeenCalledWith({
995
+ hashedRcAccountId: getHashValue(rcAccountId, 'test-hash-key'),
996
+ adminAccessToken: 'rc-access-token',
997
+ adminRefreshToken: 'rc-refresh-token',
998
+ adminTokenExpiry: expect.any(Number)
999
+ });
1000
+ });
1001
+
1002
+ test('should return early if environment variables are not set', async () => {
1003
+ // Arrange
1004
+ delete process.env.RINGCENTRAL_SERVER;
1005
+
1006
+ // Act
1007
+ const result = await authHandler.onRingcentralOAuthCallback({
1008
+ code: 'rc-auth-code',
1009
+ rcAccountId: 'hashed-rc-account-id'
1010
+ });
1011
+
1012
+ // Assert
1013
+ expect(result).toBeUndefined();
1014
+ expect(RingCentral).not.toHaveBeenCalled();
1015
+ });
1016
+ });
1017
+ });
1018
+