@app-connect/core 1.7.5 → 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,282 @@
1
+ jest.mock('tz-lookup');
2
+ jest.mock('country-state-city');
3
+
4
+ const tzlookup = require('tz-lookup');
5
+ const { State } = require('country-state-city');
6
+ const {
7
+ getTimeZone,
8
+ getHashValue,
9
+ secondsToHoursMinutesSeconds,
10
+ getMostRecentDate,
11
+ getMediaReaderLinkByPlatformMediaLink
12
+ } = require('../../lib/util');
13
+
14
+ describe('Utility Functions', () => {
15
+ describe('getTimeZone', () => {
16
+ beforeEach(() => {
17
+ jest.clearAllMocks();
18
+ });
19
+
20
+ test('should return timezone for valid state and country', () => {
21
+ // Arrange
22
+ State.getStateByCodeAndCountry = jest.fn().mockReturnValue({
23
+ name: 'California',
24
+ latitude: '36.778259',
25
+ longitude: '-119.417931'
26
+ });
27
+ tzlookup.mockReturnValue('America/Los_Angeles');
28
+
29
+ // Act
30
+ const result = getTimeZone('US', 'CA');
31
+
32
+ // Assert
33
+ expect(State.getStateByCodeAndCountry).toHaveBeenCalledWith('CA', 'US');
34
+ expect(tzlookup).toHaveBeenCalledWith('36.778259', '-119.417931');
35
+ expect(result).toBe('America/Los_Angeles');
36
+ });
37
+
38
+ test('should return "Unknown timezone" when state not found', () => {
39
+ // Arrange
40
+ State.getStateByCodeAndCountry = jest.fn().mockReturnValue(null);
41
+
42
+ // Act
43
+ const result = getTimeZone('XX', 'YY');
44
+
45
+ // Assert
46
+ expect(result).toBe('Unknown timezone');
47
+ expect(tzlookup).not.toHaveBeenCalled();
48
+ });
49
+
50
+ test('should handle different country/state combinations', () => {
51
+ // Arrange
52
+ State.getStateByCodeAndCountry = jest.fn().mockReturnValue({
53
+ name: 'Tokyo',
54
+ latitude: '35.6762',
55
+ longitude: '139.6503'
56
+ });
57
+ tzlookup.mockReturnValue('Asia/Tokyo');
58
+
59
+ // Act
60
+ const result = getTimeZone('JP', 'TK');
61
+
62
+ // Assert
63
+ expect(result).toBe('Asia/Tokyo');
64
+ });
65
+
66
+ test('should handle European timezone', () => {
67
+ // Arrange
68
+ State.getStateByCodeAndCountry = jest.fn().mockReturnValue({
69
+ name: 'Bavaria',
70
+ latitude: '48.7904',
71
+ longitude: '11.4979'
72
+ });
73
+ tzlookup.mockReturnValue('Europe/Berlin');
74
+
75
+ // Act
76
+ const result = getTimeZone('DE', 'BY');
77
+
78
+ // Assert
79
+ expect(result).toBe('Europe/Berlin');
80
+ });
81
+ });
82
+
83
+ describe('getHashValue', () => {
84
+ test('should generate consistent hash for same input', () => {
85
+ const hash1 = getHashValue('test-string', 'secret-key');
86
+ const hash2 = getHashValue('test-string', 'secret-key');
87
+
88
+ expect(hash1).toBe(hash2);
89
+ });
90
+
91
+ test('should generate different hashes for different strings', () => {
92
+ const hash1 = getHashValue('string-1', 'secret-key');
93
+ const hash2 = getHashValue('string-2', 'secret-key');
94
+
95
+ expect(hash1).not.toBe(hash2);
96
+ });
97
+
98
+ test('should generate different hashes for different keys', () => {
99
+ const hash1 = getHashValue('test-string', 'key-1');
100
+ const hash2 = getHashValue('test-string', 'key-2');
101
+
102
+ expect(hash1).not.toBe(hash2);
103
+ });
104
+
105
+ test('should return a 64-character hex string (SHA-256)', () => {
106
+ const hash = getHashValue('test', 'key');
107
+
108
+ expect(hash).toHaveLength(64);
109
+ expect(hash).toMatch(/^[0-9a-f]{64}$/);
110
+ });
111
+
112
+ test('should handle empty string', () => {
113
+ const hash = getHashValue('', 'secret-key');
114
+
115
+ expect(hash).toHaveLength(64);
116
+ expect(hash).toMatch(/^[0-9a-f]{64}$/);
117
+ });
118
+
119
+ test('should handle special characters', () => {
120
+ const hash = getHashValue('test@#$%^&*()', 'key!@#');
121
+
122
+ expect(hash).toHaveLength(64);
123
+ expect(hash).toMatch(/^[0-9a-f]{64}$/);
124
+ });
125
+ });
126
+
127
+ describe('secondsToHoursMinutesSeconds', () => {
128
+ test('should return "0 seconds" for 0', () => {
129
+ expect(secondsToHoursMinutesSeconds(0)).toBe('0 seconds');
130
+ });
131
+
132
+ test('should return singular "second" for 1 second', () => {
133
+ expect(secondsToHoursMinutesSeconds(1)).toBe('1 second');
134
+ });
135
+
136
+ test('should return plural "seconds" for multiple seconds', () => {
137
+ expect(secondsToHoursMinutesSeconds(30)).toBe('30 seconds');
138
+ });
139
+
140
+ test('should return singular "minute" for 60 seconds', () => {
141
+ expect(secondsToHoursMinutesSeconds(60)).toBe('1 minute');
142
+ });
143
+
144
+ test('should return plural "minutes" for multiple minutes', () => {
145
+ expect(secondsToHoursMinutesSeconds(120)).toBe('2 minutes');
146
+ });
147
+
148
+ test('should return minutes and seconds combined', () => {
149
+ expect(secondsToHoursMinutesSeconds(90)).toBe('1 minute, 30 seconds');
150
+ });
151
+
152
+ test('should return singular "hour" for 3600 seconds', () => {
153
+ expect(secondsToHoursMinutesSeconds(3600)).toBe('1 hour');
154
+ });
155
+
156
+ test('should return plural "hours" for multiple hours', () => {
157
+ expect(secondsToHoursMinutesSeconds(7200)).toBe('2 hours');
158
+ });
159
+
160
+ test('should return hours, minutes, and seconds combined', () => {
161
+ expect(secondsToHoursMinutesSeconds(3661)).toBe('1 hour, 1 minute, 1 second');
162
+ });
163
+
164
+ test('should return hours and minutes combined (no seconds)', () => {
165
+ expect(secondsToHoursMinutesSeconds(3660)).toBe('1 hour, 1 minute');
166
+ });
167
+
168
+ test('should return hours and seconds combined (no minutes)', () => {
169
+ expect(secondsToHoursMinutesSeconds(3601)).toBe('1 hour, 1 second');
170
+ });
171
+
172
+ test('should handle large values', () => {
173
+ // 2 hours, 30 minutes, 45 seconds
174
+ expect(secondsToHoursMinutesSeconds(9045)).toBe('2 hours, 30 minutes, 45 seconds');
175
+ });
176
+
177
+ test('should return input directly if not a number', () => {
178
+ expect(secondsToHoursMinutesSeconds('not a number')).toBe('not a number');
179
+ expect(secondsToHoursMinutesSeconds(undefined)).toBe(undefined);
180
+ // Note: null coerces to 0 via isNaN(null) === false, so returns "0 seconds"
181
+ expect(secondsToHoursMinutesSeconds(null)).toBe('0 seconds');
182
+ });
183
+
184
+ test('should handle NaN', () => {
185
+ expect(secondsToHoursMinutesSeconds(NaN)).toBe(NaN);
186
+ });
187
+ });
188
+
189
+ describe('getMostRecentDate', () => {
190
+ test('should return 0 for empty array', () => {
191
+ expect(getMostRecentDate({ allDateValues: [] })).toBe(0);
192
+ });
193
+
194
+ test('should return the single date from single-element array', () => {
195
+ const date = new Date('2024-01-15').getTime();
196
+ expect(getMostRecentDate({ allDateValues: [date] })).toBe(date);
197
+ });
198
+
199
+ test('should return the most recent date from multiple dates', () => {
200
+ const date1 = new Date('2024-01-01').getTime();
201
+ const date2 = new Date('2024-06-15').getTime();
202
+ const date3 = new Date('2024-03-10').getTime();
203
+
204
+ expect(getMostRecentDate({ allDateValues: [date1, date2, date3] })).toBe(date2);
205
+ });
206
+
207
+ test('should handle numeric timestamps', () => {
208
+ const timestamps = [1000, 5000, 3000, 2000];
209
+ expect(getMostRecentDate({ allDateValues: timestamps })).toBe(5000);
210
+ });
211
+
212
+ test('should skip null values', () => {
213
+ const date1 = new Date('2024-01-01').getTime();
214
+ const date2 = new Date('2024-06-15').getTime();
215
+
216
+ expect(getMostRecentDate({ allDateValues: [date1, null, date2, null] })).toBe(date2);
217
+ });
218
+
219
+ test('should skip undefined values', () => {
220
+ const date1 = new Date('2024-01-01').getTime();
221
+ const date2 = new Date('2024-06-15').getTime();
222
+
223
+ expect(getMostRecentDate({ allDateValues: [date1, undefined, date2] })).toBe(date2);
224
+ });
225
+
226
+ test('should return 0 for array with only null/undefined values', () => {
227
+ expect(getMostRecentDate({ allDateValues: [null, undefined, null] })).toBe(0);
228
+ });
229
+
230
+ test('should handle all same dates', () => {
231
+ const sameDate = new Date('2024-05-01').getTime();
232
+ expect(getMostRecentDate({ allDateValues: [sameDate, sameDate, sameDate] })).toBe(sameDate);
233
+ });
234
+ });
235
+
236
+ describe('getMediaReaderLinkByPlatformMediaLink', () => {
237
+ test('should return null for null input', () => {
238
+ expect(getMediaReaderLinkByPlatformMediaLink(null)).toBeNull();
239
+ });
240
+
241
+ test('should return null for undefined input', () => {
242
+ expect(getMediaReaderLinkByPlatformMediaLink(undefined)).toBeNull();
243
+ });
244
+
245
+ test('should return null for empty string', () => {
246
+ expect(getMediaReaderLinkByPlatformMediaLink('')).toBeNull();
247
+ });
248
+
249
+ test('should return media reader link for valid platform media link', () => {
250
+ const platformLink = 'https://media.ringcentral.com/restapi/v1.0/account/123/extension/456/message-store/789/content/abc';
251
+ const result = getMediaReaderLinkByPlatformMediaLink(platformLink);
252
+
253
+ expect(result).toBe(`https://ringcentral.github.io/ringcentral-media-reader/?media=${encodeURIComponent(platformLink)}`);
254
+ });
255
+
256
+ test('should properly encode special characters in URL', () => {
257
+ const platformLink = 'https://media.ringcentral.com/test?param=value&other=123';
258
+ const result = getMediaReaderLinkByPlatformMediaLink(platformLink);
259
+
260
+ expect(result).toContain('https://ringcentral.github.io/ringcentral-media-reader/?media=');
261
+ expect(result).toContain(encodeURIComponent(platformLink));
262
+ });
263
+
264
+ test('should handle simple URL', () => {
265
+ const platformLink = 'https://example.com/media.mp3';
266
+ const result = getMediaReaderLinkByPlatformMediaLink(platformLink);
267
+
268
+ expect(result).toBe(`https://ringcentral.github.io/ringcentral-media-reader/?media=${encodeURIComponent(platformLink)}`);
269
+ });
270
+
271
+ test('should handle URL with query parameters', () => {
272
+ const platformLink = 'https://media.ringcentral.com/file?id=123&type=audio';
273
+ const result = getMediaReaderLinkByPlatformMediaLink(platformLink);
274
+
275
+ expect(result).toContain('ringcentral-media-reader');
276
+ // Verify the URL is properly encoded
277
+ expect(result).not.toContain('&type=');
278
+ expect(result).toContain('%26type%3D');
279
+ });
280
+ });
281
+ });
282
+
@@ -0,0 +1,98 @@
1
+ // Use in-memory SQLite for isolated model tests
2
+ jest.mock('../../models/sequelize', () => {
3
+ const { Sequelize } = require('sequelize');
4
+ return {
5
+ sequelize: new Sequelize({
6
+ dialect: 'sqlite',
7
+ storage: ':memory:',
8
+ logging: false,
9
+ }),
10
+ };
11
+ });
12
+
13
+ const { AccountDataModel, getOrRefreshAccountData } = require('../../models/accountDataModel');
14
+ const { sequelize } = require('../../models/sequelize');
15
+
16
+ describe('getOrRefreshAccountData', () => {
17
+ beforeAll(async () => {
18
+ await AccountDataModel.sync({ force: true });
19
+ });
20
+
21
+ afterEach(async () => {
22
+ await AccountDataModel.destroy({ where: {} });
23
+ });
24
+
25
+ afterAll(async () => {
26
+ await sequelize.close();
27
+ });
28
+
29
+ test('returns cached data when record exists and refresh not forced', async () => {
30
+ await AccountDataModel.create({
31
+ rcAccountId: 'acc-1',
32
+ platformName: 'test-platform',
33
+ dataKey: 'key-1',
34
+ data: { cached: true },
35
+ });
36
+
37
+ const fetchFn = jest.fn().mockResolvedValue({ cached: false });
38
+
39
+ const out = await getOrRefreshAccountData({
40
+ rcAccountId: 'acc-1',
41
+ platformName: 'test-platform',
42
+ dataKey: 'key-1',
43
+ forceRefresh: false,
44
+ fetchFn,
45
+ });
46
+
47
+ expect(out).toEqual({ cached: true });
48
+ expect(fetchFn).not.toHaveBeenCalled();
49
+ });
50
+
51
+ test('creates new record when none exists', async () => {
52
+ const fetchFn = jest.fn().mockResolvedValue({ fresh: 'data' });
53
+
54
+ const out = await getOrRefreshAccountData({
55
+ rcAccountId: 'acc-2',
56
+ platformName: 'test-platform',
57
+ dataKey: 'key-2',
58
+ fetchFn,
59
+ });
60
+
61
+ expect(out).toEqual({ fresh: 'data' });
62
+ expect(fetchFn).toHaveBeenCalledTimes(1);
63
+
64
+ const stored = await AccountDataModel.findOne({
65
+ where: { rcAccountId: 'acc-2', platformName: 'test-platform', dataKey: 'key-2' },
66
+ });
67
+ expect(stored).not.toBeNull();
68
+ expect(stored.data).toEqual({ fresh: 'data' });
69
+ });
70
+
71
+ test('refreshes existing record when forceRefresh is true', async () => {
72
+ await AccountDataModel.create({
73
+ rcAccountId: 'acc-3',
74
+ platformName: 'test-platform',
75
+ dataKey: 'key-3',
76
+ data: { cached: true },
77
+ });
78
+
79
+ const fetchFn = jest.fn().mockResolvedValue({ cached: false, updated: true });
80
+
81
+ const out = await getOrRefreshAccountData({
82
+ rcAccountId: 'acc-3',
83
+ platformName: 'test-platform',
84
+ dataKey: 'key-3',
85
+ forceRefresh: true,
86
+ fetchFn,
87
+ });
88
+
89
+ expect(fetchFn).toHaveBeenCalledTimes(1);
90
+ expect(out).toEqual({ cached: false, updated: true });
91
+
92
+ const refreshed = await AccountDataModel.findOne({
93
+ where: { rcAccountId: 'acc-3', platformName: 'test-platform', dataKey: 'key-3' },
94
+ });
95
+ expect(refreshed.data).toEqual({ cached: false, updated: true });
96
+ });
97
+ });
98
+
@@ -0,0 +1,189 @@
1
+ // Mock dynamoose before requiring the module
2
+ jest.mock('dynamoose', () => {
3
+ const mockModel = {
4
+ query: jest.fn().mockReturnThis(),
5
+ eq: jest.fn().mockReturnThis(),
6
+ using: jest.fn().mockReturnThis(),
7
+ exec: jest.fn()
8
+ };
9
+
10
+ return {
11
+ Schema: jest.fn().mockReturnValue({}),
12
+ model: jest.fn().mockReturnValue(mockModel)
13
+ };
14
+ });
15
+
16
+ describe('Connector Schema', () => {
17
+ const originalEnv = process.env.DEVELOPER_APP_SERVER_SECRET_KEY;
18
+
19
+ beforeEach(() => {
20
+ jest.clearAllMocks();
21
+ jest.resetModules();
22
+ process.env.DEVELOPER_APP_SERVER_SECRET_KEY = 'test-secret-key-12345678901234';
23
+ });
24
+
25
+ afterEach(() => {
26
+ if (originalEnv) {
27
+ process.env.DEVELOPER_APP_SERVER_SECRET_KEY = originalEnv;
28
+ } else {
29
+ delete process.env.DEVELOPER_APP_SERVER_SECRET_KEY;
30
+ }
31
+ });
32
+
33
+ describe('getProxyConfig', () => {
34
+ test('should return proxy config when connector found', async () => {
35
+ const mockProxyConfig = {
36
+ operations: {
37
+ createCallLog: { method: 'POST', url: '/api/logs' },
38
+ findContact: { method: 'GET', url: '/api/contacts' }
39
+ }
40
+ };
41
+
42
+ const mockConnector = {
43
+ proxyConfig: mockProxyConfig,
44
+ encodedSecretKey: null
45
+ };
46
+
47
+ // Import fresh module
48
+ const { Connector } = require('../../../models/dynamo/connectorSchema');
49
+
50
+ // Mock the query chain
51
+ Connector.query = jest.fn().mockReturnThis();
52
+ Connector.eq = jest.fn().mockReturnThis();
53
+ Connector.using = jest.fn().mockReturnThis();
54
+ Connector.exec = jest.fn().mockResolvedValue([mockConnector]);
55
+
56
+ const result = await Connector.getProxyConfig('proxy-123');
57
+
58
+ expect(Connector.query).toHaveBeenCalledWith('proxyId');
59
+ expect(Connector.eq).toHaveBeenCalledWith('proxy-123');
60
+ expect(Connector.using).toHaveBeenCalledWith('proxyIdIndex');
61
+ expect(result).toEqual(mockProxyConfig);
62
+ });
63
+
64
+ test('should return null when no connector found', async () => {
65
+ const { Connector } = require('../../../models/dynamo/connectorSchema');
66
+
67
+ Connector.query = jest.fn().mockReturnThis();
68
+ Connector.eq = jest.fn().mockReturnThis();
69
+ Connector.using = jest.fn().mockReturnThis();
70
+ Connector.exec = jest.fn().mockResolvedValue([]);
71
+
72
+ const result = await Connector.getProxyConfig('non-existent-proxy');
73
+
74
+ expect(result).toBeNull();
75
+ });
76
+
77
+ test('should handle proxy config without encoded secret key', async () => {
78
+ const mockProxyConfig = {
79
+ operations: {
80
+ findContact: { method: 'GET', url: '/api/contacts' }
81
+ }
82
+ };
83
+
84
+ const mockConnector = {
85
+ proxyConfig: mockProxyConfig,
86
+ encodedSecretKey: ''
87
+ };
88
+
89
+ const { Connector } = require('../../../models/dynamo/connectorSchema');
90
+
91
+ Connector.query = jest.fn().mockReturnThis();
92
+ Connector.eq = jest.fn().mockReturnThis();
93
+ Connector.using = jest.fn().mockReturnThis();
94
+ Connector.exec = jest.fn().mockResolvedValue([mockConnector]);
95
+
96
+ const result = await Connector.getProxyConfig('proxy-no-secret');
97
+
98
+ expect(result).toEqual(mockProxyConfig);
99
+ expect(result.secretKey).toBeUndefined();
100
+ });
101
+
102
+ test('should handle undefined encoded secret key', async () => {
103
+ const mockProxyConfig = {
104
+ operations: {
105
+ createCallLog: { method: 'POST', url: '/api/logs' }
106
+ }
107
+ };
108
+
109
+ const mockConnector = {
110
+ proxyConfig: mockProxyConfig,
111
+ encodedSecretKey: undefined
112
+ };
113
+
114
+ const { Connector } = require('../../../models/dynamo/connectorSchema');
115
+
116
+ Connector.query = jest.fn().mockReturnThis();
117
+ Connector.eq = jest.fn().mockReturnThis();
118
+ Connector.using = jest.fn().mockReturnThis();
119
+ Connector.exec = jest.fn().mockResolvedValue([mockConnector]);
120
+
121
+ const result = await Connector.getProxyConfig('proxy-undefined-secret');
122
+
123
+ expect(result).toEqual(mockProxyConfig);
124
+ expect(result.secretKey).toBeUndefined();
125
+ });
126
+ });
127
+
128
+ describe('CONNECTOR_STATUS constants', () => {
129
+ test('should have correct status values in schema enum', () => {
130
+ // The status values are defined in the schema
131
+ const expectedStatuses = ['private', 'under_review', 'approved', 'rejected'];
132
+
133
+ // Import the module to verify it loads without error
134
+ const { Connector } = require('../../../models/dynamo/connectorSchema');
135
+
136
+ // The module exports Connector which means schema was created successfully
137
+ expect(Connector).toBeDefined();
138
+ expect(typeof Connector.getProxyConfig).toBe('function');
139
+ });
140
+ });
141
+
142
+ describe('Module Structure', () => {
143
+ test('should export Connector model', () => {
144
+ const connectorModule = require('../../../models/dynamo/connectorSchema');
145
+
146
+ expect(connectorModule.Connector).toBeDefined();
147
+ });
148
+
149
+ test('should have getProxyConfig static method', () => {
150
+ const { Connector } = require('../../../models/dynamo/connectorSchema');
151
+
152
+ expect(typeof Connector.getProxyConfig).toBe('function');
153
+ });
154
+ });
155
+
156
+ describe('getDeveloperCipherKey behavior', () => {
157
+ // These tests verify the cipher key handling indirectly through the module
158
+
159
+ test('should handle secret key of exactly 32 characters', async () => {
160
+ process.env.DEVELOPER_APP_SERVER_SECRET_KEY = '12345678901234567890123456789012'; // exactly 32 chars
161
+
162
+ jest.resetModules();
163
+
164
+ const { Connector } = require('../../../models/dynamo/connectorSchema');
165
+
166
+ // If the key is exactly 32 chars, no padding/truncation needed
167
+ expect(Connector).toBeDefined();
168
+ });
169
+
170
+ test('should throw error when DEVELOPER_APP_SERVER_SECRET_KEY is not set and decode is called', async () => {
171
+ delete process.env.DEVELOPER_APP_SERVER_SECRET_KEY;
172
+
173
+ jest.resetModules();
174
+
175
+ const { Connector } = require('../../../models/dynamo/connectorSchema');
176
+
177
+ Connector.query = jest.fn().mockReturnThis();
178
+ Connector.eq = jest.fn().mockReturnThis();
179
+ Connector.using = jest.fn().mockReturnThis();
180
+ Connector.exec = jest.fn().mockResolvedValue([{
181
+ proxyConfig: { test: true },
182
+ encodedSecretKey: 'some-encrypted-value'
183
+ }]);
184
+
185
+ // The decode function will be called which requires the secret key
186
+ await expect(Connector.getProxyConfig('test')).rejects.toThrow('DEVELOPER_APP_SERVER_SECRET_KEY is not defined');
187
+ });
188
+ });
189
+ });