@app-connect/core 1.7.25 → 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 -31
  4. package/connector/mock.js +84 -77
  5. package/connector/proxy/engine.js +164 -164
  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 -116
  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 +1 -1
  98. package/releaseNotes.json +1093 -1081
  99. package/test/connector/proxy/engine.test.js +126 -126
  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 -1018
  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 +457 -457
  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 -83
  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 -104
  137. package/test/setup.js +178 -178
@@ -1,1018 +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
- 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
-
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
+