@sipgate/integration-bridge 0.30.4 → 1.0.0

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.
@@ -57,21 +57,6 @@ const contactsReadonlyMock = [
57
57
  readonly: true,
58
58
  },
59
59
  ];
60
- const calendarEventMock = {
61
- id: 'abc123',
62
- title: 'My Event',
63
- description: 'Awesome event',
64
- eventUrl: 'https://wwww.google.com',
65
- start: 123456789,
66
- end: 123456789,
67
- };
68
- const calendarWithMissingField = {
69
- title: 'My Event',
70
- description: 'Awesome event',
71
- eventUrl: 'https://wwww.google.com',
72
- start: 123456789,
73
- end: 123456789,
74
- };
75
60
  const contactsMinimumMock = [
76
61
  {
77
62
  id: '123',
@@ -113,473 +98,315 @@ const contactsWithMissingField = [
113
98
  const ERROR_MESSAGE = 'Error!';
114
99
  console.log = jest.fn();
115
100
  jest.useFakeTimers();
116
- describe('getContacts', () => {
117
- let request;
118
- let response;
119
- let next;
120
- beforeAll(() => {
121
- process.env.OAUTH2_REDIRECT_URL = 'http://example.com';
122
- process.env.OAUTH2_IDENTIFIER = 'TEST';
123
- });
124
- beforeEach(() => {
125
- request = (0, node_mocks_http_1.createRequest)({
126
- providerConfig: {
127
- apiKey: 'a1b2c3',
128
- apiUrl: 'http://example.com',
129
- locale: 'de_DE',
130
- },
101
+ describe('controller.model tests', () => {
102
+ describe('getContacts', () => {
103
+ let request;
104
+ let response;
105
+ let next;
106
+ beforeAll(() => {
107
+ process.env.OAUTH2_REDIRECT_URL = 'http://example.com';
108
+ process.env.OAUTH2_IDENTIFIER = 'TEST';
131
109
  });
132
- response = (0, node_mocks_http_1.createResponse)();
133
- next = jest.fn();
134
- });
135
- it('should handle contacts', () => __awaiter(void 0, void 0, void 0, function* () {
136
- const controller = new _1.Controller({
137
- getContacts: () => Promise.resolve(contactsMock),
138
- }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
139
- yield controller.getContacts(request, response, next);
140
- const data = response._getData();
141
- expect(next).not.toBeCalled();
142
- expect(data).toEqual(contactsMock);
143
- }));
144
- it('should handle readonly contacts', () => __awaiter(void 0, void 0, void 0, function* () {
145
- const controller = new _1.Controller({
146
- getContacts: () => Promise.resolve(contactsReadonlyMock),
147
- }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
148
- yield controller.getContacts(request, response, next);
149
- const data = response._getData();
150
- expect(next).not.toBeCalled();
151
- expect(data).toEqual(contactsReadonlyMock);
152
- }));
153
- it('should handle contacts with minimum fields', () => __awaiter(void 0, void 0, void 0, function* () {
154
- const controller = new _1.Controller({
155
- getContacts: () => Promise.resolve(contactsMinimumMock),
156
- }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
157
- yield controller.getContacts(request, response, next);
158
- const data = response._getData();
159
- expect(next).not.toBeCalled();
160
- expect(data).toEqual(contactsMinimumMock);
161
- }));
162
- it('should handle invalid contacts with missing fields', () => __awaiter(void 0, void 0, void 0, function* () {
163
- console.error = jest.fn();
164
- const contactsBrokenMock = [...contactsWithMissingField];
165
- const controller = new _1.Controller({
166
- getContacts: () => Promise.resolve(contactsBrokenMock),
167
- }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
168
- yield controller.getContacts(request, response, next);
169
- expect(next).toBeCalled();
170
- }));
171
- it('should handle an error when retrieving contacts', () => __awaiter(void 0, void 0, void 0, function* () {
172
- console.error = jest.fn();
173
- const controller = new _1.Controller({
174
- getContacts: () => Promise.reject(ERROR_MESSAGE),
175
- }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
176
- yield controller.getContacts(request, response, next);
177
- expect(next).toBeCalledWith(ERROR_MESSAGE);
178
- }));
179
- });
180
- describe('getCalendarEvents', () => {
181
- let request;
182
- let response;
183
- let next;
184
- beforeEach(() => {
185
- request = (0, node_mocks_http_1.createRequest)({
186
- providerConfig: {
187
- apiKey: 'a1b2c3',
188
- apiUrl: 'http://example.com',
189
- locale: 'de_DE',
190
- },
110
+ beforeEach(() => {
111
+ request = (0, node_mocks_http_1.createRequest)({
112
+ providerConfig: {
113
+ apiKey: 'a1b2c3',
114
+ apiUrl: 'http://example.com',
115
+ locale: 'de_DE',
116
+ },
117
+ });
118
+ response = (0, node_mocks_http_1.createResponse)();
119
+ next = jest.fn();
191
120
  });
192
- response = (0, node_mocks_http_1.createResponse)();
193
- next = jest.fn();
121
+ it('should handle contacts', () => __awaiter(void 0, void 0, void 0, function* () {
122
+ const controller = new _1.Controller({
123
+ getContacts: () => Promise.resolve(contactsMock),
124
+ }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
125
+ yield controller.getContacts(request, response, next);
126
+ const data = response._getData();
127
+ expect(next).not.toBeCalled();
128
+ expect(data).toEqual(contactsMock);
129
+ }));
130
+ it('should handle readonly contacts', () => __awaiter(void 0, void 0, void 0, function* () {
131
+ const controller = new _1.Controller({
132
+ getContacts: () => Promise.resolve(contactsReadonlyMock),
133
+ }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
134
+ yield controller.getContacts(request, response, next);
135
+ const data = response._getData();
136
+ expect(next).not.toBeCalled();
137
+ expect(data).toEqual(contactsReadonlyMock);
138
+ }));
139
+ it('should handle contacts with minimum fields', () => __awaiter(void 0, void 0, void 0, function* () {
140
+ const controller = new _1.Controller({
141
+ getContacts: () => Promise.resolve(contactsMinimumMock),
142
+ }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
143
+ yield controller.getContacts(request, response, next);
144
+ const data = response._getData();
145
+ expect(next).not.toBeCalled();
146
+ expect(data).toEqual(contactsMinimumMock);
147
+ }));
148
+ it('should handle invalid contacts with missing fields', () => __awaiter(void 0, void 0, void 0, function* () {
149
+ console.error = jest.fn();
150
+ const contactsBrokenMock = [...contactsWithMissingField];
151
+ const controller = new _1.Controller({
152
+ getContacts: () => Promise.resolve(contactsBrokenMock),
153
+ }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
154
+ yield controller.getContacts(request, response, next);
155
+ expect(next).toBeCalled();
156
+ }));
157
+ it('should handle an error when retrieving contacts', () => __awaiter(void 0, void 0, void 0, function* () {
158
+ console.error = jest.fn();
159
+ const controller = new _1.Controller({
160
+ getContacts: () => Promise.reject(ERROR_MESSAGE),
161
+ }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
162
+ yield controller.getContacts(request, response, next);
163
+ expect(next).toBeCalledWith(ERROR_MESSAGE);
164
+ }));
194
165
  });
195
- it('should handle calendar events', () => __awaiter(void 0, void 0, void 0, function* () {
196
- const controller = new _1.Controller({
197
- getCalendarEvents: () => Promise.resolve([calendarEventMock]),
198
- }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
199
- yield controller.getCalendarEvents(request, response, next);
200
- const data = response._getData();
201
- expect(next).not.toBeCalled();
202
- expect(data).toEqual([calendarEventMock]);
203
- }));
204
- it('should handle invalid calendar events', () => __awaiter(void 0, void 0, void 0, function* () {
205
- console.error = jest.fn();
206
- const calendarEventsBrokenMock = [
207
- Object.assign({}, calendarWithMissingField),
208
- ];
209
- const controller = new _1.Controller({
210
- getCalendarEvents: () => Promise.resolve(calendarEventsBrokenMock),
211
- }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
212
- yield controller.getCalendarEvents(request, response, next);
213
- expect(next).toBeCalled();
214
- }));
215
- it('should handle an error when retrieving calendar events', () => __awaiter(void 0, void 0, void 0, function* () {
216
- console.error = jest.fn();
217
- const controller = new _1.Controller({
218
- getCalendarEvents: () => Promise.reject(ERROR_MESSAGE),
219
- }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
220
- yield controller.getCalendarEvents(request, response, next);
221
- expect(next).toBeCalledWith(ERROR_MESSAGE);
222
- }));
223
- });
224
- describe('createCalendarEvent', () => {
225
- let request;
226
- let response;
227
- let next;
228
- beforeEach(() => {
229
- request = (0, node_mocks_http_1.createRequest)({
230
- providerConfig: {
231
- apiKey: 'a1b2c3',
232
- apiUrl: 'http://example.com',
233
- locale: 'de_DE',
234
- },
166
+ describe('getOAuth2RedirectUrl', () => {
167
+ let request;
168
+ let response;
169
+ let next;
170
+ beforeEach(() => {
171
+ request = (0, node_mocks_http_1.createRequest)();
172
+ response = (0, node_mocks_http_1.createResponse)();
173
+ next = jest.fn();
235
174
  });
236
- response = (0, node_mocks_http_1.createResponse)();
237
- next = jest.fn();
175
+ it('should handle OAuth2 callback', () => __awaiter(void 0, void 0, void 0, function* () {
176
+ const mockHandleOAuth2Callback = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () {
177
+ return Promise.resolve({
178
+ apiKey: 'key',
179
+ apiUrl: 'url',
180
+ });
181
+ }));
182
+ const controller = new _1.Controller({
183
+ handleOAuth2Callback: mockHandleOAuth2Callback,
184
+ }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
185
+ yield controller.oAuth2Callback(request, response);
186
+ expect(mockHandleOAuth2Callback).toBeCalled();
187
+ expect(next).not.toBeCalled();
188
+ }));
189
+ it('should handle a custom redirect url', () => __awaiter(void 0, void 0, void 0, function* () {
190
+ const mockRedirectUrl = 'http://example.com?name=TEST&key=key&url=url';
191
+ const mockRedirect = jest.fn();
192
+ const mockHandleOAuth2Callback = jest.fn((req) => __awaiter(void 0, void 0, void 0, function* () {
193
+ return Promise.resolve({
194
+ apiKey: 'key',
195
+ apiUrl: 'url',
196
+ });
197
+ }));
198
+ const controller = new _1.Controller({
199
+ handleOAuth2Callback: mockHandleOAuth2Callback,
200
+ }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
201
+ request = (0, node_mocks_http_1.createRequest)({
202
+ query: {
203
+ redirectUrl: mockRedirectUrl,
204
+ },
205
+ });
206
+ response = { redirect: mockRedirect };
207
+ yield controller.oAuth2Callback(request, response);
208
+ expect(mockHandleOAuth2Callback).toBeCalled();
209
+ expect(mockRedirect).toBeCalledWith(mockRedirectUrl);
210
+ expect(next).not.toBeCalled();
211
+ }));
238
212
  });
239
- it('should create calendar events', () => __awaiter(void 0, void 0, void 0, function* () {
240
- const controller = new _1.Controller({
241
- createCalendarEvent: () => Promise.resolve(calendarEventMock),
242
- }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
243
- yield controller.createCalendarEvent(request, response, next);
244
- const data = response._getData();
245
- expect(next).not.toBeCalled();
246
- expect(data).toEqual(calendarEventMock);
247
- }));
248
- it('should handle invalid calendar events', () => __awaiter(void 0, void 0, void 0, function* () {
249
- console.error = jest.fn();
250
- const calendarEventBrokenMock = Object.assign({}, calendarWithMissingField);
251
- const controller = new _1.Controller({
252
- createCalendarEvent: () => Promise.resolve(calendarEventBrokenMock),
253
- }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
254
- yield controller.createCalendarEvent(request, response, next);
255
- expect(next).toBeCalled();
256
- }));
257
- it('should handle an error when creating calendar events', () => __awaiter(void 0, void 0, void 0, function* () {
258
- console.error = jest.fn();
259
- const controller = new _1.Controller({
260
- createCalendarEvent: () => Promise.reject(ERROR_MESSAGE),
261
- }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
262
- yield controller.createCalendarEvent(request, response, next);
263
- expect(next).toBeCalledWith(ERROR_MESSAGE);
264
- }));
265
- });
266
- describe('updateCalendarEvent', () => {
267
- let request;
268
- let response;
269
- let next;
270
- beforeEach(() => {
271
- request = (0, node_mocks_http_1.createRequest)({
272
- providerConfig: {
273
- apiKey: 'a1b2c3',
274
- apiUrl: 'http://example.com',
275
- locale: 'de_DE',
276
- },
213
+ describe('getHealth', () => {
214
+ let request;
215
+ let response;
216
+ let next;
217
+ beforeEach(() => {
218
+ request = (0, node_mocks_http_1.createRequest)();
219
+ response = (0, node_mocks_http_1.createResponse)();
220
+ next = jest.fn();
277
221
  });
278
- response = (0, node_mocks_http_1.createResponse)();
279
- next = jest.fn();
222
+ it('should implement a default function', () => __awaiter(void 0, void 0, void 0, function* () {
223
+ const controller = new _1.Controller({
224
+ getContacts: () => Promise.resolve(contactsMock),
225
+ }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
226
+ yield controller.getHealth(request, response, next);
227
+ expect(next).not.toBeCalled();
228
+ expect(response.statusCode).toBe(200);
229
+ }));
230
+ it('should accept a custom function', () => __awaiter(void 0, void 0, void 0, function* () {
231
+ const getHealthMock = jest.fn();
232
+ const controller = new _1.Controller({
233
+ getContacts: () => Promise.resolve(contactsMock),
234
+ getHealth: getHealthMock,
235
+ }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
236
+ yield controller.getHealth(request, response, next);
237
+ expect(getHealthMock).toBeCalled();
238
+ expect(next).not.toBeCalled();
239
+ expect(response.statusCode).toBe(200);
240
+ }));
241
+ it('should handle an error', () => __awaiter(void 0, void 0, void 0, function* () {
242
+ console.error = jest.fn();
243
+ const controller = new _1.Controller({
244
+ getContacts: () => Promise.reject(),
245
+ getHealth: () => Promise.reject(new Error('Error')),
246
+ }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
247
+ yield controller.getHealth(request, response, next);
248
+ expect(next).toBeCalled();
249
+ }));
280
250
  });
281
- it('should update calendar events', () => __awaiter(void 0, void 0, void 0, function* () {
282
- const controller = new _1.Controller({
283
- updateCalendarEvent: () => Promise.resolve(calendarEventMock),
284
- }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
285
- yield controller.updateCalendarEvent(request, response, next);
286
- const data = response._getData();
287
- expect(next).not.toBeCalled();
288
- expect(data).toEqual(calendarEventMock);
289
- }));
290
- it('should handle invalid calendar events', () => __awaiter(void 0, void 0, void 0, function* () {
291
- console.error = jest.fn();
292
- const calendarEventBrokenMock = Object.assign({}, calendarWithMissingField);
293
- const controller = new _1.Controller({
294
- updateCalendarEvent: () => Promise.resolve(calendarEventBrokenMock),
295
- }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
296
- yield controller.updateCalendarEvent(request, response, next);
297
- expect(next).toBeCalled();
298
- }));
299
- it('should handle an error when updating calendar events', () => __awaiter(void 0, void 0, void 0, function* () {
300
- console.error = jest.fn();
301
- const controller = new _1.Controller({
302
- updateCalendarEvent: () => Promise.reject(ERROR_MESSAGE),
303
- }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
304
- yield controller.updateCalendarEvent(request, response, next);
305
- expect(next).toBeCalledWith(ERROR_MESSAGE);
306
- }));
307
- });
308
- describe('deleteCalendarEvent', () => {
309
- let request;
310
- let response;
311
- let next;
312
- beforeEach(() => {
313
- request = (0, node_mocks_http_1.createRequest)({
314
- providerConfig: {
315
- apiKey: 'a1b2c3',
316
- apiUrl: 'http://example.com',
317
- locale: 'de_DE',
318
- },
251
+ describe('handleCallEvent', () => {
252
+ let request;
253
+ let response;
254
+ let next;
255
+ beforeEach(() => {
256
+ response = (0, node_mocks_http_1.createResponse)();
257
+ next = jest.fn();
319
258
  });
320
- response = (0, node_mocks_http_1.createResponse)();
321
- next = jest.fn();
322
- });
323
- it('should delete calendar events', () => __awaiter(void 0, void 0, void 0, function* () {
324
- const controller = new _1.Controller({
325
- deleteCalendarEvent: () => Promise.resolve(),
326
- }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
327
- yield controller.deleteCalendarEvent(request, response, next);
328
- expect(next).not.toBeCalled();
329
- expect(response._getStatusCode()).toEqual(200);
330
- }));
331
- it('should handle an error when deleting calendar events', () => __awaiter(void 0, void 0, void 0, function* () {
332
- console.error = jest.fn();
333
- const controller = new _1.Controller({
334
- deleteCalendarEvent: () => Promise.reject(ERROR_MESSAGE),
335
- }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
336
- yield controller.deleteCalendarEvent(request, response, next);
337
- expect(next).toBeCalledWith(ERROR_MESSAGE);
338
- }));
339
- });
340
- describe('getOAuth2RedirectUrl', () => {
341
- let request;
342
- let response;
343
- let next;
344
- beforeEach(() => {
345
- request = (0, node_mocks_http_1.createRequest)();
346
- response = (0, node_mocks_http_1.createResponse)();
347
- next = jest.fn();
348
- });
349
- it('should handle OAuth2 callback', () => __awaiter(void 0, void 0, void 0, function* () {
350
- const mockHandleOAuth2Callback = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () {
351
- return Promise.resolve({
352
- apiKey: 'key',
353
- apiUrl: 'url',
259
+ it('should ignore a call event with a remote direct dial', () => __awaiter(void 0, void 0, void 0, function* () {
260
+ request = (0, node_mocks_http_1.createRequest)({
261
+ providerConfig: {
262
+ apiKey: 'a1b2c3',
263
+ apiUrl: 'http://example.com',
264
+ locale: 'de_DE',
265
+ },
266
+ body: {
267
+ participants: [
268
+ {
269
+ type: _1.CallParticipantType.LOCAL,
270
+ phoneNumber: '1234567890',
271
+ },
272
+ {
273
+ type: _1.CallParticipantType.REMOTE,
274
+ phoneNumber: '13',
275
+ },
276
+ ],
277
+ id: '',
278
+ startTime: 0,
279
+ endTime: 0,
280
+ direction: _1.CallDirection.IN,
281
+ note: '',
282
+ state: _1.CallState.BUSY,
283
+ },
354
284
  });
285
+ const controller = new _1.Controller({
286
+ handleCallEvent: (config, event) => Promise.resolve(''),
287
+ }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
288
+ yield controller.handleCallEvent(request, response, next);
289
+ const data = response._getData();
290
+ expect(next).not.toBeCalled();
291
+ expect(data).toEqual('Skipping call event');
355
292
  }));
356
- const controller = new _1.Controller({
357
- handleOAuth2Callback: mockHandleOAuth2Callback,
358
- }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
359
- yield controller.oAuth2Callback(request, response);
360
- expect(mockHandleOAuth2Callback).toBeCalled();
361
- expect(next).not.toBeCalled();
362
- }));
363
- it('should handle a custom redirect url', () => __awaiter(void 0, void 0, void 0, function* () {
364
- const mockRedirectUrl = 'http://example.com?name=TEST&key=key&url=url';
365
- const mockRedirect = jest.fn();
366
- const mockHandleOAuth2Callback = jest.fn((req) => __awaiter(void 0, void 0, void 0, function* () {
367
- return Promise.resolve({
368
- apiKey: 'key',
369
- apiUrl: 'url',
293
+ it('should handle a call event', () => __awaiter(void 0, void 0, void 0, function* () {
294
+ request = (0, node_mocks_http_1.createRequest)({
295
+ providerConfig: {
296
+ apiKey: 'a1b2c3',
297
+ apiUrl: 'http://example.com',
298
+ locale: 'de_DE',
299
+ },
300
+ body: {
301
+ participants: [
302
+ {
303
+ type: _1.CallParticipantType.LOCAL,
304
+ phoneNumber: '1234567890',
305
+ },
306
+ {
307
+ type: _1.CallParticipantType.REMOTE,
308
+ phoneNumber: '0123456789',
309
+ },
310
+ ],
311
+ id: '',
312
+ startTime: 0,
313
+ endTime: 0,
314
+ direction: _1.CallDirection.IN,
315
+ note: '',
316
+ state: _1.CallState.BUSY,
317
+ },
370
318
  });
319
+ const controller = new _1.Controller({
320
+ handleCallEvent: (config, event) => Promise.resolve('callRef'),
321
+ }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
322
+ yield controller.handleCallEvent(request, response, next);
323
+ const data = response._getData();
324
+ expect(next).not.toBeCalled();
325
+ expect(data).toEqual('callRef');
326
+ }));
327
+ it('should handle adapter not implementing the feature', () => __awaiter(void 0, void 0, void 0, function* () {
328
+ request = (0, node_mocks_http_1.createRequest)({
329
+ providerConfig: {
330
+ apiKey: 'a1b2c3',
331
+ apiUrl: 'http://example.com',
332
+ locale: 'de_DE',
333
+ },
334
+ });
335
+ const controller = new _1.Controller({}, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
336
+ yield controller.handleCallEvent(request, response, next);
337
+ expect(next).toBeCalled();
338
+ }));
339
+ it('should handle config being missing', () => __awaiter(void 0, void 0, void 0, function* () {
340
+ request = (0, node_mocks_http_1.createRequest)({});
341
+ const controller = new _1.Controller({
342
+ handleCallEvent: (config, event) => Promise.resolve('callRef'),
343
+ }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
344
+ yield controller.handleCallEvent(request, response, next);
345
+ expect(next).toBeCalled();
371
346
  }));
372
- const controller = new _1.Controller({
373
- handleOAuth2Callback: mockHandleOAuth2Callback,
374
- }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
375
- request = (0, node_mocks_http_1.createRequest)({
376
- query: {
377
- redirectUrl: mockRedirectUrl,
378
- },
379
- });
380
- response = { redirect: mockRedirect };
381
- yield controller.oAuth2Callback(request, response);
382
- expect(mockHandleOAuth2Callback).toBeCalled();
383
- expect(mockRedirect).toBeCalledWith(mockRedirectUrl);
384
- expect(next).not.toBeCalled();
385
- }));
386
- });
387
- describe('getHealth', () => {
388
- let request;
389
- let response;
390
- let next;
391
- beforeEach(() => {
392
- request = (0, node_mocks_http_1.createRequest)();
393
- response = (0, node_mocks_http_1.createResponse)();
394
- next = jest.fn();
395
- });
396
- it('should implement a default function', () => __awaiter(void 0, void 0, void 0, function* () {
397
- const controller = new _1.Controller({
398
- getContacts: () => Promise.resolve(contactsMock),
399
- }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
400
- yield controller.getHealth(request, response, next);
401
- expect(next).not.toBeCalled();
402
- expect(response.statusCode).toBe(200);
403
- }));
404
- it('should accept a custom function', () => __awaiter(void 0, void 0, void 0, function* () {
405
- const getHealthMock = jest.fn();
406
- const controller = new _1.Controller({
407
- getContacts: () => Promise.resolve(contactsMock),
408
- getHealth: getHealthMock,
409
- }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
410
- yield controller.getHealth(request, response, next);
411
- expect(getHealthMock).toBeCalled();
412
- expect(next).not.toBeCalled();
413
- expect(response.statusCode).toBe(200);
414
- }));
415
- it('should handle an error', () => __awaiter(void 0, void 0, void 0, function* () {
416
- console.error = jest.fn();
417
- const controller = new _1.Controller({
418
- getContacts: () => Promise.reject(),
419
- getHealth: () => Promise.reject(new Error('Error')),
420
- }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
421
- yield controller.getHealth(request, response, next);
422
- expect(next).toBeCalled();
423
- }));
424
- });
425
- describe('handleCallEvent', () => {
426
- let request;
427
- let response;
428
- let next;
429
- beforeEach(() => {
430
- response = (0, node_mocks_http_1.createResponse)();
431
- next = jest.fn();
432
347
  });
433
- it('should ignore a call event with a remote direct dial', () => __awaiter(void 0, void 0, void 0, function* () {
434
- request = (0, node_mocks_http_1.createRequest)({
435
- providerConfig: {
436
- apiKey: 'a1b2c3',
437
- apiUrl: 'http://example.com',
438
- locale: 'de_DE',
439
- },
440
- body: {
441
- participants: [
442
- {
443
- type: _1.CallParticipantType.LOCAL,
444
- phoneNumber: '1234567890',
445
- },
446
- {
447
- type: _1.CallParticipantType.REMOTE,
448
- phoneNumber: '13',
449
- },
450
- ],
451
- id: '',
452
- startTime: 0,
453
- endTime: 0,
454
- direction: _1.CallDirection.IN,
455
- note: '',
456
- state: _1.CallState.BUSY,
457
- },
458
- });
459
- const controller = new _1.Controller({
460
- handleCallEvent: (config, event) => Promise.resolve(''),
461
- }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
462
- yield controller.handleCallEvent(request, response, next);
463
- const data = response._getData();
464
- expect(next).not.toBeCalled();
465
- expect(data).toEqual('Skipping call event');
466
- }));
467
- it('should handle a call event', () => __awaiter(void 0, void 0, void 0, function* () {
468
- request = (0, node_mocks_http_1.createRequest)({
469
- providerConfig: {
470
- apiKey: 'a1b2c3',
471
- apiUrl: 'http://example.com',
472
- locale: 'de_DE',
473
- },
474
- body: {
475
- participants: [
476
- {
477
- type: _1.CallParticipantType.LOCAL,
478
- phoneNumber: '1234567890',
479
- },
480
- {
481
- type: _1.CallParticipantType.REMOTE,
482
- phoneNumber: '0123456789',
483
- },
484
- ],
485
- id: '',
486
- startTime: 0,
487
- endTime: 0,
488
- direction: _1.CallDirection.IN,
489
- note: '',
490
- state: _1.CallState.BUSY,
491
- },
348
+ describe('updateCallEvent', () => {
349
+ let request;
350
+ let response;
351
+ let next;
352
+ beforeEach(() => {
353
+ response = (0, node_mocks_http_1.createResponse)();
354
+ next = jest.fn();
492
355
  });
493
- const controller = new _1.Controller({
494
- handleCallEvent: (config, event) => Promise.resolve('callRef'),
495
- }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
496
- yield controller.handleCallEvent(request, response, next);
497
- const data = response._getData();
498
- expect(next).not.toBeCalled();
499
- expect(data).toEqual('callRef');
500
- }));
501
- it('should handle adapter not implementing the feature', () => __awaiter(void 0, void 0, void 0, function* () {
502
- request = (0, node_mocks_http_1.createRequest)({
503
- providerConfig: {
504
- apiKey: 'a1b2c3',
505
- apiUrl: 'http://example.com',
506
- locale: 'de_DE',
507
- },
508
- });
509
- const controller = new _1.Controller({}, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
510
- yield controller.handleCallEvent(request, response, next);
511
- expect(next).toBeCalled();
512
- }));
513
- it('should handle config being missing', () => __awaiter(void 0, void 0, void 0, function* () {
514
- request = (0, node_mocks_http_1.createRequest)({});
515
- const controller = new _1.Controller({
516
- handleCallEvent: (config, event) => Promise.resolve('callRef'),
517
- }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
518
- yield controller.handleCallEvent(request, response, next);
519
- expect(next).toBeCalled();
520
- }));
521
- });
522
- describe('updateCallEvent', () => {
523
- let request;
524
- let response;
525
- let next;
526
- beforeEach(() => {
527
- response = (0, node_mocks_http_1.createResponse)();
528
- next = jest.fn();
356
+ it('should handle a call event', () => __awaiter(void 0, void 0, void 0, function* () {
357
+ request = (0, node_mocks_http_1.createRequest)({
358
+ providerConfig: {
359
+ apiKey: 'a1b2c3',
360
+ apiUrl: 'http://example.com',
361
+ locale: 'de_DE',
362
+ },
363
+ body: {
364
+ participants: [
365
+ {
366
+ type: _1.CallParticipantType.LOCAL,
367
+ phoneNumber: '1234567890',
368
+ },
369
+ {
370
+ type: _1.CallParticipantType.REMOTE,
371
+ phoneNumber: '0123456789',
372
+ },
373
+ ],
374
+ id: '',
375
+ startTime: 0,
376
+ endTime: 0,
377
+ direction: _1.CallDirection.IN,
378
+ note: '',
379
+ state: _1.CallState.BUSY,
380
+ },
381
+ });
382
+ const controller = new _1.Controller({
383
+ updateCallEvent: (config, id, event) => Promise.resolve(),
384
+ }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
385
+ yield controller.updateCallEvent(request, response, next);
386
+ const data = response._getData();
387
+ expect(next).not.toBeCalled();
388
+ expect(data).toEqual('');
389
+ }));
390
+ it('should handle adapter not implementing the feature', () => __awaiter(void 0, void 0, void 0, function* () {
391
+ request = (0, node_mocks_http_1.createRequest)({
392
+ providerConfig: {
393
+ apiKey: 'a1b2c3',
394
+ apiUrl: 'http://example.com',
395
+ locale: 'de_DE',
396
+ },
397
+ });
398
+ const controller = new _1.Controller({}, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
399
+ yield controller.updateCallEvent(request, response, next);
400
+ expect(next).toBeCalled();
401
+ }));
402
+ it('should handle config being missing', () => __awaiter(void 0, void 0, void 0, function* () {
403
+ request = (0, node_mocks_http_1.createRequest)({});
404
+ const controller = new _1.Controller({
405
+ updateCallEvent: (config, id, event) => Promise.resolve(),
406
+ }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
407
+ yield controller.updateCallEvent(request, response, next);
408
+ expect(next).toBeCalled();
409
+ }));
529
410
  });
530
- it('should handle a call event', () => __awaiter(void 0, void 0, void 0, function* () {
531
- request = (0, node_mocks_http_1.createRequest)({
532
- providerConfig: {
533
- apiKey: 'a1b2c3',
534
- apiUrl: 'http://example.com',
535
- locale: 'de_DE',
536
- },
537
- body: {
538
- participants: [
539
- {
540
- type: _1.CallParticipantType.LOCAL,
541
- phoneNumber: '1234567890',
542
- },
543
- {
544
- type: _1.CallParticipantType.REMOTE,
545
- phoneNumber: '0123456789',
546
- },
547
- ],
548
- id: '',
549
- startTime: 0,
550
- endTime: 0,
551
- direction: _1.CallDirection.IN,
552
- note: '',
553
- state: _1.CallState.BUSY,
554
- },
555
- });
556
- const controller = new _1.Controller({
557
- updateCallEvent: (config, id, event) => Promise.resolve(),
558
- }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
559
- yield controller.updateCallEvent(request, response, next);
560
- const data = response._getData();
561
- expect(next).not.toBeCalled();
562
- expect(data).toEqual('');
563
- }));
564
- it('should handle adapter not implementing the feature', () => __awaiter(void 0, void 0, void 0, function* () {
565
- request = (0, node_mocks_http_1.createRequest)({
566
- providerConfig: {
567
- apiKey: 'a1b2c3',
568
- apiUrl: 'http://example.com',
569
- locale: 'de_DE',
570
- },
571
- });
572
- const controller = new _1.Controller({}, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
573
- yield controller.updateCallEvent(request, response, next);
574
- expect(next).toBeCalled();
575
- }));
576
- it('should handle config being missing', () => __awaiter(void 0, void 0, void 0, function* () {
577
- request = (0, node_mocks_http_1.createRequest)({});
578
- const controller = new _1.Controller({
579
- updateCallEvent: (config, id, event) => Promise.resolve(),
580
- }, new cache_1.ContactCacheStorage(new storage_1.MemoryStorageAdapter()));
581
- yield controller.updateCallEvent(request, response, next);
582
- expect(next).toBeCalled();
583
- }));
584
411
  });
585
412
  //# sourceMappingURL=controller.model.test.js.map