@app-connect/core 1.7.8 → 1.7.10

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.
@@ -0,0 +1,473 @@
1
+ jest.mock('node-fetch');
2
+
3
+ const fetch = require('node-fetch');
4
+ const { RingCentral, isRefreshTokenValid, isAccessTokenValid } = require('../../lib/ringcentral');
5
+
6
+ describe('ringcentral', () => {
7
+ beforeEach(() => {
8
+ jest.clearAllMocks();
9
+ });
10
+
11
+ describe('isRefreshTokenValid', () => {
12
+ test('should return true if refresh token is not expired', () => {
13
+ const token = {
14
+ refresh_token_expire_time: Date.now() + 60000 // 1 minute in future
15
+ };
16
+
17
+ expect(isRefreshTokenValid(token)).toBe(true);
18
+ });
19
+
20
+ test('should return false if refresh token is expired', () => {
21
+ const token = {
22
+ refresh_token_expire_time: Date.now() - 1000 // 1 second ago
23
+ };
24
+
25
+ expect(isRefreshTokenValid(token)).toBe(false);
26
+ });
27
+
28
+ test('should respect custom handicap', () => {
29
+ const token = {
30
+ refresh_token_expire_time: Date.now() + 5000 // 5 seconds in future
31
+ };
32
+
33
+ // With 10s handicap, should be invalid
34
+ expect(isRefreshTokenValid(token, 10000)).toBe(false);
35
+
36
+ // With 1s handicap, should be valid
37
+ expect(isRefreshTokenValid(token, 1000)).toBe(true);
38
+ });
39
+ });
40
+
41
+ describe('isAccessTokenValid', () => {
42
+ test('should return true if access token is not expired', () => {
43
+ const token = {
44
+ expire_time: Date.now() + 120000 // 2 minutes in future
45
+ };
46
+
47
+ expect(isAccessTokenValid(token)).toBe(true);
48
+ });
49
+
50
+ test('should return false if access token is expired', () => {
51
+ const token = {
52
+ expire_time: Date.now() - 1000
53
+ };
54
+
55
+ expect(isAccessTokenValid(token)).toBe(false);
56
+ });
57
+
58
+ test('should respect default 1 minute handicap', () => {
59
+ const token = {
60
+ expire_time: Date.now() + 30000 // 30 seconds in future
61
+ };
62
+
63
+ // With default 1 minute handicap, should be invalid
64
+ expect(isAccessTokenValid(token)).toBe(false);
65
+ });
66
+ });
67
+
68
+ describe('RingCentral class', () => {
69
+ let rc;
70
+ const options = {
71
+ server: 'https://platform.ringcentral.com',
72
+ clientId: 'test-client-id',
73
+ clientSecret: 'test-client-secret',
74
+ redirectUri: 'https://app.example.com/callback'
75
+ };
76
+
77
+ beforeEach(() => {
78
+ rc = new RingCentral(options);
79
+ });
80
+
81
+ describe('loginUrl', () => {
82
+ test('should generate login URL with required parameters', () => {
83
+ const url = rc.loginUrl({});
84
+
85
+ expect(url).toContain('https://platform.ringcentral.com/restapi/oauth/authorize');
86
+ expect(url).toContain('response_type=code');
87
+ expect(url).toContain(`client_id=${options.clientId}`);
88
+ expect(url).toContain(`redirect_uri=${encodeURIComponent(options.redirectUri)}`);
89
+ });
90
+
91
+ test('should include state parameter when provided', () => {
92
+ const url = rc.loginUrl({ state: 'custom-state-123' });
93
+
94
+ expect(url).toContain('state=custom-state-123');
95
+ });
96
+ });
97
+
98
+ describe('generateToken', () => {
99
+ test('should generate token successfully', async () => {
100
+ const mockResponse = {
101
+ status: 200,
102
+ json: jest.fn().mockResolvedValue({
103
+ access_token: 'new-access-token',
104
+ refresh_token: 'new-refresh-token',
105
+ token_type: 'bearer',
106
+ expires_in: 3600,
107
+ refresh_token_expires_in: 604800,
108
+ scope: 'ReadAccounts',
109
+ endpoint_id: 'endpoint-123'
110
+ })
111
+ };
112
+ fetch.mockResolvedValue(mockResponse);
113
+
114
+ const result = await rc.generateToken({ code: 'auth-code-123' });
115
+
116
+ expect(fetch).toHaveBeenCalledWith(
117
+ 'https://platform.ringcentral.com/restapi/oauth/token',
118
+ expect.objectContaining({
119
+ method: 'POST',
120
+ headers: expect.objectContaining({
121
+ 'Content-Type': 'application/x-www-form-urlencoded'
122
+ })
123
+ })
124
+ );
125
+ expect(result.access_token).toBe('new-access-token');
126
+ expect(result.refresh_token).toBe('new-refresh-token');
127
+ expect(result.expire_time).toBeDefined();
128
+ expect(result.refresh_token_expire_time).toBeDefined();
129
+ // These should not be included
130
+ expect(result.scope).toBeUndefined();
131
+ expect(result.endpoint_id).toBeUndefined();
132
+ });
133
+
134
+ test('should throw error on failed token generation', async () => {
135
+ const mockResponse = { status: 401 };
136
+ fetch.mockResolvedValue(mockResponse);
137
+
138
+ await expect(rc.generateToken({ code: 'invalid-code' }))
139
+ .rejects.toThrow('Generate Token error');
140
+ });
141
+ });
142
+
143
+ describe('refreshToken', () => {
144
+ test('should refresh token successfully', async () => {
145
+ const existingToken = {
146
+ refresh_token: 'old-refresh-token',
147
+ expires_in: 3600,
148
+ refresh_token_expires_in: 604800
149
+ };
150
+
151
+ const mockResponse = {
152
+ status: 200,
153
+ json: jest.fn().mockResolvedValue({
154
+ access_token: 'refreshed-access-token',
155
+ refresh_token: 'new-refresh-token',
156
+ token_type: 'bearer',
157
+ expires_in: 3600,
158
+ refresh_token_expires_in: 604800,
159
+ scope: 'ReadAccounts',
160
+ endpoint_id: 'endpoint-456'
161
+ })
162
+ };
163
+ fetch.mockResolvedValue(mockResponse);
164
+
165
+ const result = await rc.refreshToken(existingToken);
166
+
167
+ expect(result.access_token).toBe('refreshed-access-token');
168
+ expect(result.refresh_token).toBe('new-refresh-token');
169
+ expect(result.expire_time).toBeDefined();
170
+ });
171
+
172
+ test('should throw error on failed refresh', async () => {
173
+ const mockResponse = { status: 401 };
174
+ fetch.mockResolvedValue(mockResponse);
175
+
176
+ await expect(rc.refreshToken({ refresh_token: 'expired' }))
177
+ .rejects.toThrow('Refresh Token error');
178
+ });
179
+ });
180
+
181
+ describe('revokeToken', () => {
182
+ test('should revoke token successfully', async () => {
183
+ const mockResponse = { status: 200 };
184
+ fetch.mockResolvedValue(mockResponse);
185
+
186
+ await expect(rc.revokeToken({ access_token: 'token-to-revoke' }))
187
+ .resolves.not.toThrow();
188
+
189
+ expect(fetch).toHaveBeenCalledWith(
190
+ 'https://platform.ringcentral.com/restapi/oauth/revoke',
191
+ expect.objectContaining({ method: 'POST' })
192
+ );
193
+ });
194
+
195
+ test('should throw error on failed revocation', async () => {
196
+ const mockResponse = { status: 500 };
197
+ fetch.mockResolvedValue(mockResponse);
198
+
199
+ await expect(rc.revokeToken({ access_token: 'token' }))
200
+ .rejects.toThrow('Revoke Token error');
201
+ });
202
+ });
203
+
204
+ describe('request', () => {
205
+ const token = {
206
+ token_type: 'bearer',
207
+ access_token: 'test-access-token'
208
+ };
209
+
210
+ test('should make authenticated request', async () => {
211
+ const mockResponse = {
212
+ status: 200,
213
+ json: jest.fn().mockResolvedValue({ data: 'test' })
214
+ };
215
+ fetch.mockResolvedValue(mockResponse);
216
+
217
+ const response = await rc.request({
218
+ path: '/restapi/v1.0/account/~',
219
+ method: 'GET'
220
+ }, token);
221
+
222
+ expect(fetch).toHaveBeenCalledWith(
223
+ 'https://platform.ringcentral.com/restapi/v1.0/account/~',
224
+ expect.objectContaining({
225
+ method: 'GET',
226
+ headers: expect.objectContaining({
227
+ 'Authorization': 'bearer test-access-token'
228
+ })
229
+ })
230
+ );
231
+ expect(response).toBe(mockResponse);
232
+ });
233
+
234
+ test('should include query parameters', async () => {
235
+ const mockResponse = { status: 200 };
236
+ fetch.mockResolvedValue(mockResponse);
237
+
238
+ await rc.request({
239
+ path: '/restapi/v1.0/extension/~/call-log',
240
+ method: 'GET',
241
+ query: { dateFrom: '2024-01-01', dateTo: '2024-01-31' }
242
+ }, token);
243
+
244
+ expect(fetch).toHaveBeenCalledWith(
245
+ expect.stringContaining('dateFrom=2024-01-01'),
246
+ expect.any(Object)
247
+ );
248
+ });
249
+
250
+ test('should include JSON body', async () => {
251
+ const mockResponse = { status: 200 };
252
+ fetch.mockResolvedValue(mockResponse);
253
+
254
+ await rc.request({
255
+ path: '/restapi/v1.0/subscription',
256
+ method: 'POST',
257
+ body: { eventFilters: ['/restapi/v1.0/account/~/extension/~/presence'] }
258
+ }, token);
259
+
260
+ expect(fetch).toHaveBeenCalledWith(
261
+ expect.any(String),
262
+ expect.objectContaining({
263
+ method: 'POST',
264
+ body: expect.stringContaining('eventFilters')
265
+ })
266
+ );
267
+ });
268
+
269
+ test('should throw error on failed request', async () => {
270
+ const mockResponse = {
271
+ status: 401,
272
+ text: jest.fn().mockResolvedValue('Unauthorized')
273
+ };
274
+ fetch.mockResolvedValue(mockResponse);
275
+
276
+ await expect(rc.request({ path: '/test', method: 'GET' }, token))
277
+ .rejects.toThrow('Unauthorized');
278
+ });
279
+
280
+ test('should use custom server if provided', async () => {
281
+ const mockResponse = { status: 200 };
282
+ fetch.mockResolvedValue(mockResponse);
283
+
284
+ await rc.request({
285
+ server: 'https://custom-server.com',
286
+ path: '/api/test',
287
+ method: 'GET'
288
+ }, token);
289
+
290
+ expect(fetch).toHaveBeenCalledWith(
291
+ 'https://custom-server.com/api/test',
292
+ expect.any(Object)
293
+ );
294
+ });
295
+ });
296
+
297
+ describe('createSubscription', () => {
298
+ const token = { token_type: 'bearer', access_token: 'token' };
299
+
300
+ test('should create webhook subscription', async () => {
301
+ const mockResponse = {
302
+ status: 200,
303
+ json: jest.fn().mockResolvedValue({
304
+ id: 'sub-123',
305
+ expirationTime: '2024-01-20T00:00:00Z',
306
+ uri: 'https://platform.ringcentral.com/subscription/sub-123',
307
+ creationTime: '2024-01-13T00:00:00Z',
308
+ deliveryMode: { transportType: 'WebHook' },
309
+ status: 'Active'
310
+ })
311
+ };
312
+ fetch.mockResolvedValue(mockResponse);
313
+
314
+ const result = await rc.createSubscription({
315
+ eventFilters: ['/restapi/v1.0/account/~/extension/~/presence'],
316
+ webhookUri: 'https://app.example.com/webhook'
317
+ }, token);
318
+
319
+ expect(result.id).toBe('sub-123');
320
+ // These should not be included in result
321
+ expect(result.uri).toBeUndefined();
322
+ expect(result.creationTime).toBeUndefined();
323
+ });
324
+ });
325
+
326
+ describe('getExtensionInfo', () => {
327
+ const token = { token_type: 'bearer', access_token: 'token' };
328
+
329
+ test('should get extension info', async () => {
330
+ const extensionInfo = {
331
+ id: 12345,
332
+ name: 'John Doe',
333
+ extensionNumber: '101'
334
+ };
335
+ const mockResponse = {
336
+ status: 200,
337
+ json: jest.fn().mockResolvedValue(extensionInfo)
338
+ };
339
+ fetch.mockResolvedValue(mockResponse);
340
+
341
+ const result = await rc.getExtensionInfo('~', token);
342
+
343
+ expect(result).toEqual(extensionInfo);
344
+ });
345
+ });
346
+
347
+ describe('getAccountInfo', () => {
348
+ const token = { token_type: 'bearer', access_token: 'token' };
349
+
350
+ test('should get account info', async () => {
351
+ const accountInfo = {
352
+ id: 'account-123',
353
+ mainNumber: '+1234567890'
354
+ };
355
+ const mockResponse = {
356
+ status: 200,
357
+ json: jest.fn().mockResolvedValue(accountInfo)
358
+ };
359
+ fetch.mockResolvedValue(mockResponse);
360
+
361
+ const result = await rc.getAccountInfo(token);
362
+
363
+ expect(result).toEqual(accountInfo);
364
+ });
365
+ });
366
+
367
+ describe('getCallsAggregationData', () => {
368
+ const token = { token_type: 'bearer', access_token: 'token' };
369
+
370
+ test('should get calls aggregation data', async () => {
371
+ const aggregationData = {
372
+ records: [{ callsCount: 100, duration: 36000 }]
373
+ };
374
+ const mockResponse = {
375
+ status: 200,
376
+ json: jest.fn().mockResolvedValue(aggregationData)
377
+ };
378
+ fetch.mockResolvedValue(mockResponse);
379
+
380
+ const result = await rc.getCallsAggregationData({
381
+ token,
382
+ timezone: 'America/New_York',
383
+ timeFrom: '2024-01-01',
384
+ timeTo: '2024-01-31',
385
+ groupBy: 'Users'
386
+ });
387
+
388
+ expect(result).toEqual(aggregationData);
389
+ expect(fetch).toHaveBeenCalledWith(
390
+ expect.stringContaining('/analytics/calls/v1/accounts/~/aggregation/fetch'),
391
+ expect.objectContaining({ method: 'POST' })
392
+ );
393
+ });
394
+ });
395
+
396
+ describe('getCallLogData', () => {
397
+ const token = { token_type: 'bearer', access_token: 'token' };
398
+
399
+ test('should get call log data with pagination', async () => {
400
+ const page1 = {
401
+ records: [{ id: 'call-1' }, { id: 'call-2' }],
402
+ navigation: { nextPage: { uri: 'next-page' } }
403
+ };
404
+ const page2 = {
405
+ records: [{ id: 'call-3' }],
406
+ navigation: {}
407
+ };
408
+
409
+ fetch
410
+ .mockResolvedValueOnce({ status: 200, json: () => Promise.resolve(page1) })
411
+ .mockResolvedValueOnce({ status: 200, json: () => Promise.resolve(page2) });
412
+
413
+ const result = await rc.getCallLogData({
414
+ token,
415
+ timezone: 'UTC',
416
+ timeFrom: '2024-01-01',
417
+ timeTo: '2024-01-31'
418
+ });
419
+
420
+ expect(result.records).toHaveLength(3);
421
+ expect(fetch).toHaveBeenCalledTimes(2);
422
+ });
423
+
424
+ test('should handle single page of results', async () => {
425
+ const response = {
426
+ records: [{ id: 'call-1' }],
427
+ navigation: {}
428
+ };
429
+ fetch.mockResolvedValue({ status: 200, json: () => Promise.resolve(response) });
430
+
431
+ const result = await rc.getCallLogData({
432
+ extensionId: '12345',
433
+ token,
434
+ timezone: 'UTC',
435
+ timeFrom: '2024-01-01',
436
+ timeTo: '2024-01-31'
437
+ });
438
+
439
+ expect(result.records).toHaveLength(1);
440
+ expect(fetch).toHaveBeenCalledTimes(1);
441
+ });
442
+ });
443
+
444
+ describe('getSMSData', () => {
445
+ const token = { token_type: 'bearer', access_token: 'token' };
446
+
447
+ test('should get SMS data with pagination', async () => {
448
+ const page1 = {
449
+ records: [{ id: 'sms-1' }],
450
+ navigation: { nextPage: { uri: 'next' } }
451
+ };
452
+ const page2 = {
453
+ records: [{ id: 'sms-2' }],
454
+ navigation: {}
455
+ };
456
+
457
+ fetch
458
+ .mockResolvedValueOnce({ status: 200, json: () => Promise.resolve(page1) })
459
+ .mockResolvedValueOnce({ status: 200, json: () => Promise.resolve(page2) });
460
+
461
+ const result = await rc.getSMSData({
462
+ token,
463
+ timezone: 'UTC',
464
+ timeFrom: '2024-01-01',
465
+ timeTo: '2024-01-31'
466
+ });
467
+
468
+ expect(result.records).toHaveLength(2);
469
+ });
470
+ });
471
+ });
472
+ });
473
+