-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #656 from techmatters/CHI-2790-native_channel_aler…
…t_service_user_unsupported_media-test CHI-2790: Alert service user for unsupported media test
- Loading branch information
Showing
1 changed file
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/** | ||
* Copyright (C) 2021-2023 Technology Matters | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see https://www.gnu.org/licenses/. | ||
*/ | ||
|
||
import { ServerlessCallback } from '@twilio-labs/serverless-runtime-types/types'; | ||
import { Event as Body } from '../../functions/helpers/sendErrorMessageForUnsupportedMedia.private'; | ||
import { handler as serviceConversationListener } from '../../functions/webhooks/serviceConversationListener.protected'; | ||
import helpers, { MockedResponse } from '../helpers'; | ||
|
||
const baseContext = { | ||
getTwilioClient: (): any => ({ | ||
conversations: { | ||
conversations: { | ||
get: () => {}, | ||
}, | ||
}, | ||
}), | ||
DOMAIN_NAME: 'serverless', | ||
CHAT_SERVICE_SID: 'chatService', | ||
SYNC_SERVICE_SID: 'SYNC_SERVICE_SID', | ||
PATH: 'PATH', | ||
SERVICE_SID: undefined, | ||
ENVIRONMENT_SID: undefined, | ||
}; | ||
|
||
describe('serviceConversationListener', () => { | ||
beforeAll(() => { | ||
const runtime = new helpers.MockRuntime(baseContext); | ||
// eslint-disable-next-line no-underscore-dangle | ||
runtime._addFunction( | ||
'helpers/sendErrorMessageForUnsupportedMedia', | ||
'functions/helpers/sendErrorMessageForUnsupportedMedia.private', | ||
); | ||
helpers.setup({}, runtime); | ||
}); | ||
afterAll(() => { | ||
helpers.teardown(); | ||
}); | ||
|
||
test('Should return status 400 if Body, Media or EventType are undefined', async () => { | ||
const event1: Body = { | ||
Body: undefined, | ||
ConversationSid: 'CHxxxxxxx34EWS', | ||
EventType: 'onMessageAdded', | ||
Media: {}, | ||
DateCreated: new Date(), | ||
}; | ||
|
||
const event2: Body = { | ||
Body: 'Test word', | ||
ConversationSid: 'CHxxxxxxx34EWS', | ||
EventType: 'onMessageAdded', | ||
Media: undefined, | ||
DateCreated: new Date(), | ||
}; | ||
|
||
const event3: Body = { | ||
Body: 'Test word', | ||
ConversationSid: 'CHxxxxxxx34EWS', | ||
EventType: undefined, | ||
Media: {}, | ||
DateCreated: new Date(), | ||
}; | ||
|
||
const callback: ServerlessCallback = (err, result) => { | ||
expect(result).toBeDefined(); | ||
const response = result as MockedResponse; | ||
expect(response.getStatus()).toBe(400); | ||
}; | ||
|
||
await serviceConversationListener(baseContext, event1, callback); | ||
await serviceConversationListener(baseContext, event2, callback); | ||
await serviceConversationListener(baseContext, event3, callback); | ||
}); | ||
|
||
test('Should return status 500 if object response is undefined', async () => { | ||
const event1: Body = { | ||
Body: 'Test word', | ||
ConversationSid: 'CHxxxxxxx34EWS', | ||
EventType: 'onMessageAdded', | ||
Media: {}, | ||
DateCreated: new Date(), | ||
}; | ||
|
||
const callback1: ServerlessCallback = (err, result) => { | ||
expect(result).toBeDefined(); | ||
const response = result as MockedResponse; | ||
expect(response.getStatus()).toBe(500); | ||
}; | ||
|
||
await serviceConversationListener({ ...baseContext }, event1, callback1); | ||
|
||
const event2: Body = { | ||
Body: 'Test word', | ||
ConversationSid: 'CHxxxxxxx34EWS', | ||
EventType: 'onMessageAdded', | ||
Media: {}, | ||
DateCreated: new Date(), | ||
}; | ||
|
||
const callback2: ServerlessCallback = (err, result) => { | ||
expect(result).toBeDefined(); | ||
const response = result as MockedResponse; | ||
expect(response.getStatus()).toBe(500); | ||
}; | ||
|
||
await serviceConversationListener(baseContext, event2, callback2); | ||
}); | ||
|
||
test('Should return status 200', async () => { | ||
const event1: Body = { | ||
Body: 'Test word', | ||
ConversationSid: 'CHxxxxxxx34EWS', | ||
EventType: 'onMessageAdded', | ||
Media: {}, | ||
DateCreated: new Date(), | ||
}; | ||
|
||
const callback1: ServerlessCallback = (err, result) => { | ||
expect(result).toBeDefined(); | ||
const response = result as MockedResponse; | ||
expect(response.getStatus()).toBe(200); | ||
}; | ||
|
||
await serviceConversationListener(baseContext, event1, callback1); | ||
}); | ||
}); |