@app-connect/core 1.7.8 → 1.7.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/connector/developerPortal.js +43 -0
- package/connector/proxy/index.js +10 -3
- package/connector/registry.js +8 -6
- package/handlers/admin.js +44 -21
- package/handlers/auth.js +97 -69
- package/handlers/calldown.js +10 -4
- package/handlers/contact.js +45 -112
- package/handlers/disposition.js +4 -142
- package/handlers/log.js +174 -259
- package/handlers/user.js +19 -6
- package/index.js +310 -122
- package/lib/analytics.js +3 -1
- package/lib/authSession.js +68 -0
- package/lib/callLogComposer.js +498 -420
- package/lib/errorHandler.js +206 -0
- package/lib/jwt.js +2 -0
- package/lib/logger.js +190 -0
- package/lib/oauth.js +21 -12
- package/lib/ringcentral.js +2 -10
- package/lib/sharedSMSComposer.js +471 -0
- package/mcp/SupportedPlatforms.md +12 -0
- package/mcp/lib/validator.js +91 -0
- package/mcp/mcpHandler.js +166 -0
- package/mcp/tools/checkAuthStatus.js +90 -0
- package/mcp/tools/collectAuthInfo.js +86 -0
- package/mcp/tools/createCallLog.js +299 -0
- package/mcp/tools/createMessageLog.js +283 -0
- package/mcp/tools/doAuth.js +185 -0
- package/mcp/tools/findContactByName.js +87 -0
- package/mcp/tools/findContactByPhone.js +96 -0
- package/mcp/tools/getCallLog.js +98 -0
- package/mcp/tools/getHelp.js +39 -0
- package/mcp/tools/getPublicConnectors.js +46 -0
- package/mcp/tools/index.js +58 -0
- package/mcp/tools/logout.js +63 -0
- package/mcp/tools/rcGetCallLogs.js +73 -0
- package/mcp/tools/setConnector.js +64 -0
- package/mcp/tools/updateCallLog.js +122 -0
- package/models/accountDataModel.js +34 -0
- package/models/cacheModel.js +3 -0
- package/package.json +6 -4
- package/releaseNotes.json +36 -0
- package/test/connector/registry.test.js +145 -0
- package/test/handlers/admin.test.js +583 -0
- package/test/handlers/auth.test.js +355 -0
- package/test/handlers/contact.test.js +852 -0
- package/test/handlers/log.test.js +872 -0
- package/test/lib/callLogComposer.test.js +1231 -0
- package/test/lib/debugTracer.test.js +328 -0
- package/test/lib/logger.test.js +206 -0
- package/test/lib/oauth.test.js +359 -0
- package/test/lib/ringcentral.test.js +473 -0
- package/test/lib/sharedSMSComposer.test.js +1084 -0
- package/test/lib/util.test.js +282 -0
- package/test/mcp/tools/collectAuthInfo.test.js +192 -0
- package/test/mcp/tools/createCallLog.test.js +412 -0
- package/test/mcp/tools/createMessageLog.test.js +580 -0
- package/test/mcp/tools/doAuth.test.js +363 -0
- package/test/mcp/tools/findContactByName.test.js +263 -0
- package/test/mcp/tools/findContactByPhone.test.js +284 -0
- package/test/mcp/tools/getCallLog.test.js +286 -0
- package/test/mcp/tools/getPublicConnectors.test.js +128 -0
- package/test/mcp/tools/logout.test.js +169 -0
- package/test/mcp/tools/setConnector.test.js +177 -0
- package/test/mcp/tools/updateCallLog.test.js +346 -0
- package/test/models/accountDataModel.test.js +98 -0
- package/test/models/dynamo/connectorSchema.test.js +189 -0
- package/test/models/models.test.js +539 -0
- package/test/setup.js +176 -176
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
const moment = require('moment');
|
|
2
|
+
|
|
3
|
+
// Mock dependencies before requiring the module
|
|
4
|
+
jest.mock('client-oauth2');
|
|
5
|
+
jest.mock('../../models/userModel');
|
|
6
|
+
jest.mock('../../connector/registry');
|
|
7
|
+
jest.mock('../../models/dynamo/lockSchema', () => ({
|
|
8
|
+
Lock: {
|
|
9
|
+
create: jest.fn(),
|
|
10
|
+
get: jest.fn()
|
|
11
|
+
}
|
|
12
|
+
}));
|
|
13
|
+
|
|
14
|
+
const ClientOAuth2 = require('client-oauth2');
|
|
15
|
+
const { UserModel } = require('../../models/userModel');
|
|
16
|
+
const connectorRegistry = require('../../connector/registry');
|
|
17
|
+
const { getOAuthApp, checkAndRefreshAccessToken } = require('../../lib/oauth');
|
|
18
|
+
|
|
19
|
+
describe('oauth', () => {
|
|
20
|
+
beforeEach(() => {
|
|
21
|
+
jest.clearAllMocks();
|
|
22
|
+
jest.spyOn(console, 'log').mockImplementation(() => {});
|
|
23
|
+
delete process.env.USE_TOKEN_REFRESH_LOCK_PLATFORMS;
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
afterEach(() => {
|
|
27
|
+
jest.restoreAllMocks();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe('getOAuthApp', () => {
|
|
31
|
+
test('should create OAuth app with provided configuration', () => {
|
|
32
|
+
const config = {
|
|
33
|
+
clientId: 'test-client-id',
|
|
34
|
+
clientSecret: 'test-client-secret',
|
|
35
|
+
accessTokenUri: 'https://api.example.com/oauth/token',
|
|
36
|
+
authorizationUri: 'https://api.example.com/oauth/authorize',
|
|
37
|
+
redirectUri: 'https://app.example.com/callback',
|
|
38
|
+
scopes: ['read', 'write']
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const mockOAuthApp = { code: { getToken: jest.fn() } };
|
|
42
|
+
ClientOAuth2.mockReturnValue(mockOAuthApp);
|
|
43
|
+
|
|
44
|
+
const result = getOAuthApp(config);
|
|
45
|
+
|
|
46
|
+
expect(ClientOAuth2).toHaveBeenCalledWith({
|
|
47
|
+
clientId: config.clientId,
|
|
48
|
+
clientSecret: config.clientSecret,
|
|
49
|
+
accessTokenUri: config.accessTokenUri,
|
|
50
|
+
authorizationUri: config.authorizationUri,
|
|
51
|
+
redirectUri: config.redirectUri,
|
|
52
|
+
scopes: config.scopes
|
|
53
|
+
});
|
|
54
|
+
expect(result).toBe(mockOAuthApp);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test('should handle missing optional parameters', () => {
|
|
58
|
+
const config = {
|
|
59
|
+
clientId: 'client-id',
|
|
60
|
+
clientSecret: 'client-secret'
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
ClientOAuth2.mockReturnValue({});
|
|
64
|
+
|
|
65
|
+
getOAuthApp(config);
|
|
66
|
+
|
|
67
|
+
expect(ClientOAuth2).toHaveBeenCalledWith({
|
|
68
|
+
clientId: 'client-id',
|
|
69
|
+
clientSecret: 'client-secret',
|
|
70
|
+
accessTokenUri: undefined,
|
|
71
|
+
authorizationUri: undefined,
|
|
72
|
+
redirectUri: undefined,
|
|
73
|
+
scopes: undefined
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
describe('checkAndRefreshAccessToken', () => {
|
|
79
|
+
const mockOAuthApp = {
|
|
80
|
+
createToken: jest.fn()
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const createMockUser = (overrides = {}) => ({
|
|
84
|
+
id: 'user-123',
|
|
85
|
+
platform: 'testPlatform',
|
|
86
|
+
accessToken: 'old-access-token',
|
|
87
|
+
refreshToken: 'old-refresh-token',
|
|
88
|
+
tokenExpiry: moment().subtract(1, 'minute').toDate(), // Expired
|
|
89
|
+
save: jest.fn().mockResolvedValue(true),
|
|
90
|
+
...overrides
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test('should return user unchanged if token is not expired', async () => {
|
|
94
|
+
const user = createMockUser({
|
|
95
|
+
tokenExpiry: moment().add(10, 'minutes').toDate() // Not expired
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
connectorRegistry.getConnector.mockReturnValue({});
|
|
99
|
+
|
|
100
|
+
const result = await checkAndRefreshAccessToken(mockOAuthApp, user);
|
|
101
|
+
|
|
102
|
+
expect(result).toBe(user);
|
|
103
|
+
expect(mockOAuthApp.createToken).not.toHaveBeenCalled();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test('should refresh token when expired', async () => {
|
|
107
|
+
const user = createMockUser();
|
|
108
|
+
const newExpiry = moment().add(1, 'hour').toDate();
|
|
109
|
+
|
|
110
|
+
connectorRegistry.getConnector.mockReturnValue({});
|
|
111
|
+
|
|
112
|
+
const mockToken = {
|
|
113
|
+
refresh: jest.fn().mockResolvedValue({
|
|
114
|
+
accessToken: 'new-access-token',
|
|
115
|
+
refreshToken: 'new-refresh-token',
|
|
116
|
+
expires: newExpiry
|
|
117
|
+
})
|
|
118
|
+
};
|
|
119
|
+
mockOAuthApp.createToken.mockReturnValue(mockToken);
|
|
120
|
+
|
|
121
|
+
const result = await checkAndRefreshAccessToken(mockOAuthApp, user);
|
|
122
|
+
|
|
123
|
+
expect(mockOAuthApp.createToken).toHaveBeenCalledWith(
|
|
124
|
+
'old-access-token',
|
|
125
|
+
'old-refresh-token'
|
|
126
|
+
);
|
|
127
|
+
expect(mockToken.refresh).toHaveBeenCalled();
|
|
128
|
+
expect(user.accessToken).toBe('new-access-token');
|
|
129
|
+
expect(user.refreshToken).toBe('new-refresh-token');
|
|
130
|
+
expect(user.save).toHaveBeenCalled();
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test('should delegate to platform-specific refresh if available', async () => {
|
|
134
|
+
const user = createMockUser({
|
|
135
|
+
platform: 'bullhorn'
|
|
136
|
+
});
|
|
137
|
+
const platformRefreshedUser = { ...user, accessToken: 'bullhorn-token' };
|
|
138
|
+
|
|
139
|
+
connectorRegistry.getConnector.mockReturnValue({
|
|
140
|
+
checkAndRefreshAccessToken: jest.fn().mockResolvedValue(platformRefreshedUser)
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
const result = await checkAndRefreshAccessToken(mockOAuthApp, user);
|
|
144
|
+
|
|
145
|
+
expect(result).toBe(platformRefreshedUser);
|
|
146
|
+
expect(mockOAuthApp.createToken).not.toHaveBeenCalled();
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
test('should refresh token even if within buffer time (2 minutes)', async () => {
|
|
150
|
+
const user = createMockUser({
|
|
151
|
+
tokenExpiry: moment().add(1.5, 'minutes').toDate() // Within buffer
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
connectorRegistry.getConnector.mockReturnValue({});
|
|
155
|
+
|
|
156
|
+
const mockToken = {
|
|
157
|
+
refresh: jest.fn().mockResolvedValue({
|
|
158
|
+
accessToken: 'new-token',
|
|
159
|
+
refreshToken: 'new-refresh',
|
|
160
|
+
expires: moment().add(1, 'hour').toDate()
|
|
161
|
+
})
|
|
162
|
+
};
|
|
163
|
+
mockOAuthApp.createToken.mockReturnValue(mockToken);
|
|
164
|
+
|
|
165
|
+
await checkAndRefreshAccessToken(mockOAuthApp, user);
|
|
166
|
+
|
|
167
|
+
expect(mockToken.refresh).toHaveBeenCalled();
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
test('should not refresh if missing required tokens', async () => {
|
|
171
|
+
const user = createMockUser({
|
|
172
|
+
accessToken: null,
|
|
173
|
+
refreshToken: null,
|
|
174
|
+
tokenExpiry: moment().subtract(1, 'minute').toDate()
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
connectorRegistry.getConnector.mockReturnValue({});
|
|
178
|
+
|
|
179
|
+
const result = await checkAndRefreshAccessToken(mockOAuthApp, user);
|
|
180
|
+
|
|
181
|
+
expect(result).toBe(user);
|
|
182
|
+
expect(mockOAuthApp.createToken).not.toHaveBeenCalled();
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
describe('with token refresh lock', () => {
|
|
186
|
+
beforeEach(() => {
|
|
187
|
+
process.env.USE_TOKEN_REFRESH_LOCK_PLATFORMS = 'testPlatform,otherPlatform';
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
test('should create lock and refresh token successfully', async () => {
|
|
191
|
+
const { Lock } = require('../../models/dynamo/lockSchema');
|
|
192
|
+
const user = createMockUser();
|
|
193
|
+
const newExpiry = moment().add(1, 'hour').toDate();
|
|
194
|
+
|
|
195
|
+
const mockLock = { delete: jest.fn().mockResolvedValue(true) };
|
|
196
|
+
Lock.create.mockResolvedValue(mockLock);
|
|
197
|
+
|
|
198
|
+
connectorRegistry.getConnector.mockReturnValue({});
|
|
199
|
+
|
|
200
|
+
const mockToken = {
|
|
201
|
+
refresh: jest.fn().mockResolvedValue({
|
|
202
|
+
accessToken: 'new-token',
|
|
203
|
+
refreshToken: 'new-refresh',
|
|
204
|
+
expires: newExpiry
|
|
205
|
+
})
|
|
206
|
+
};
|
|
207
|
+
mockOAuthApp.createToken.mockReturnValue(mockToken);
|
|
208
|
+
|
|
209
|
+
await checkAndRefreshAccessToken(mockOAuthApp, user);
|
|
210
|
+
|
|
211
|
+
expect(Lock.create).toHaveBeenCalledWith(
|
|
212
|
+
expect.objectContaining({
|
|
213
|
+
userId: 'user-123',
|
|
214
|
+
ttl: expect.any(Number)
|
|
215
|
+
}),
|
|
216
|
+
{ overwrite: false }
|
|
217
|
+
);
|
|
218
|
+
expect(mockToken.refresh).toHaveBeenCalled();
|
|
219
|
+
expect(mockLock.delete).toHaveBeenCalled();
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
test('should wait for existing lock and fetch user from DB after lock released', async () => {
|
|
223
|
+
jest.resetModules();
|
|
224
|
+
const { Lock } = require('../../models/dynamo/lockSchema');
|
|
225
|
+
const user = createMockUser();
|
|
226
|
+
|
|
227
|
+
// Simulate lock already exists
|
|
228
|
+
const conditionalError = new Error('Lock exists');
|
|
229
|
+
conditionalError.name = 'ConditionalCheckFailedException';
|
|
230
|
+
Lock.create.mockReset();
|
|
231
|
+
Lock.get.mockReset();
|
|
232
|
+
Lock.create.mockRejectedValue(conditionalError);
|
|
233
|
+
|
|
234
|
+
// Lock exists but not expired (ttl > now), then gets released
|
|
235
|
+
const existingLock = {
|
|
236
|
+
ttl: moment().add(30, 'seconds').unix(),
|
|
237
|
+
delete: jest.fn().mockResolvedValue(true)
|
|
238
|
+
};
|
|
239
|
+
Lock.get.mockResolvedValueOnce(existingLock)
|
|
240
|
+
.mockResolvedValueOnce(null); // Lock released
|
|
241
|
+
|
|
242
|
+
const refreshedUser = { ...user, accessToken: 'refreshed-by-other-process' };
|
|
243
|
+
UserModel.findByPk.mockResolvedValue(refreshedUser);
|
|
244
|
+
|
|
245
|
+
connectorRegistry.getConnector.mockReturnValue({});
|
|
246
|
+
|
|
247
|
+
const result = await checkAndRefreshAccessToken(mockOAuthApp, user, 5);
|
|
248
|
+
|
|
249
|
+
// Verify the lock polling was performed
|
|
250
|
+
expect(Lock.get).toHaveBeenCalled();
|
|
251
|
+
// The result should have the user data (refreshed by another process)
|
|
252
|
+
expect(result).toBeDefined();
|
|
253
|
+
expect(result.id).toBe(user.id);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
test('should handle expired lock by deleting and creating new one', async () => {
|
|
257
|
+
const { Lock } = require('../../models/dynamo/lockSchema');
|
|
258
|
+
const user = createMockUser();
|
|
259
|
+
const newExpiry = moment().add(1, 'hour').toDate();
|
|
260
|
+
|
|
261
|
+
// First create fails with conditional exception
|
|
262
|
+
const conditionalError = new Error('Lock exists');
|
|
263
|
+
conditionalError.name = 'ConditionalCheckFailedException';
|
|
264
|
+
|
|
265
|
+
// Existing lock is expired (ttl < now)
|
|
266
|
+
const expiredLock = {
|
|
267
|
+
ttl: moment().subtract(10, 'seconds').unix(),
|
|
268
|
+
delete: jest.fn().mockResolvedValue(true)
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
// Second create succeeds after deleting expired lock
|
|
272
|
+
const newLock = { delete: jest.fn().mockResolvedValue(true) };
|
|
273
|
+
|
|
274
|
+
Lock.create
|
|
275
|
+
.mockRejectedValueOnce(conditionalError)
|
|
276
|
+
.mockResolvedValueOnce(newLock);
|
|
277
|
+
Lock.get.mockResolvedValue(expiredLock);
|
|
278
|
+
|
|
279
|
+
connectorRegistry.getConnector.mockReturnValue({});
|
|
280
|
+
|
|
281
|
+
const mockToken = {
|
|
282
|
+
refresh: jest.fn().mockResolvedValue({
|
|
283
|
+
accessToken: 'new-token',
|
|
284
|
+
refreshToken: 'new-refresh',
|
|
285
|
+
expires: newExpiry
|
|
286
|
+
})
|
|
287
|
+
};
|
|
288
|
+
mockOAuthApp.createToken.mockReturnValue(mockToken);
|
|
289
|
+
|
|
290
|
+
await checkAndRefreshAccessToken(mockOAuthApp, user);
|
|
291
|
+
|
|
292
|
+
expect(expiredLock.delete).toHaveBeenCalled();
|
|
293
|
+
expect(newLock.delete).toHaveBeenCalled();
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
test('should delete lock if refresh fails', async () => {
|
|
297
|
+
jest.resetModules();
|
|
298
|
+
const { Lock } = require('../../models/dynamo/lockSchema');
|
|
299
|
+
const user = createMockUser();
|
|
300
|
+
|
|
301
|
+
const mockLock = { delete: jest.fn().mockResolvedValue(true) };
|
|
302
|
+
Lock.create.mockReset();
|
|
303
|
+
Lock.get.mockReset();
|
|
304
|
+
Lock.create.mockResolvedValue(mockLock);
|
|
305
|
+
|
|
306
|
+
connectorRegistry.getConnector.mockReturnValue({});
|
|
307
|
+
|
|
308
|
+
const mockToken = {
|
|
309
|
+
refresh: jest.fn().mockRejectedValue(new Error('Refresh failed'))
|
|
310
|
+
};
|
|
311
|
+
mockOAuthApp.createToken.mockReturnValue(mockToken);
|
|
312
|
+
|
|
313
|
+
await checkAndRefreshAccessToken(mockOAuthApp, user);
|
|
314
|
+
|
|
315
|
+
expect(mockLock.delete).toHaveBeenCalled();
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
test('should throw on lock timeout', async () => {
|
|
319
|
+
jest.resetModules();
|
|
320
|
+
const { Lock } = require('../../models/dynamo/lockSchema');
|
|
321
|
+
const user = createMockUser();
|
|
322
|
+
|
|
323
|
+
const conditionalError = new Error('Lock exists');
|
|
324
|
+
conditionalError.name = 'ConditionalCheckFailedException';
|
|
325
|
+
Lock.create.mockReset();
|
|
326
|
+
Lock.get.mockReset();
|
|
327
|
+
Lock.create.mockRejectedValue(conditionalError);
|
|
328
|
+
|
|
329
|
+
// Lock never gets released (ttl is in the future, not expired)
|
|
330
|
+
const permanentLock = {
|
|
331
|
+
ttl: moment().add(1, 'hour').unix(),
|
|
332
|
+
delete: jest.fn().mockResolvedValue(true)
|
|
333
|
+
};
|
|
334
|
+
Lock.get.mockResolvedValue(permanentLock);
|
|
335
|
+
|
|
336
|
+
connectorRegistry.getConnector.mockReturnValue({});
|
|
337
|
+
|
|
338
|
+
await expect(
|
|
339
|
+
checkAndRefreshAccessToken(mockOAuthApp, user, 1) // 1 second timeout
|
|
340
|
+
).rejects.toThrow('Token lock timeout');
|
|
341
|
+
}, 10000);
|
|
342
|
+
|
|
343
|
+
test('should rethrow non-conditional errors', async () => {
|
|
344
|
+
const { Lock } = require('../../models/dynamo/lockSchema');
|
|
345
|
+
const user = createMockUser();
|
|
346
|
+
|
|
347
|
+
const randomError = new Error('Database connection failed');
|
|
348
|
+
Lock.create.mockRejectedValue(randomError);
|
|
349
|
+
|
|
350
|
+
connectorRegistry.getConnector.mockReturnValue({});
|
|
351
|
+
|
|
352
|
+
await expect(
|
|
353
|
+
checkAndRefreshAccessToken(mockOAuthApp, user)
|
|
354
|
+
).rejects.toThrow('Database connection failed');
|
|
355
|
+
});
|
|
356
|
+
});
|
|
357
|
+
});
|
|
358
|
+
});
|
|
359
|
+
|