From 7780e140f611aa12bdb315b3a0eb13ebbec6fb49 Mon Sep 17 00:00:00 2001 From: Stephen Okpalaononuju Date: Thu, 25 Jul 2024 15:09:13 +0100 Subject: [PATCH 1/3] ch: add test to serviceConversationListener --- .../serviceConversationListener.test.ts | 150 ++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 tests/webhooks/serviceConversationListener.test.ts diff --git a/tests/webhooks/serviceConversationListener.test.ts b/tests/webhooks/serviceConversationListener.test.ts new file mode 100644 index 00000000..44ea13b6 --- /dev/null +++ b/tests/webhooks/serviceConversationListener.test.ts @@ -0,0 +1,150 @@ +/** + * 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'; + +// eslint-disable-next-line @typescript-eslint/no-unused-vars +const mockDocRemove = jest.fn((doc: string) => true); + +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(); + }); + afterEach(() => { + mockDocRemove.mockClear(); + }); + + 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); + expect(mockDocRemove).not.toHaveBeenCalled(); + }; + + 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(400); + expect(mockDocRemove).not.toHaveBeenCalled(); + }; + + 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); + expect(mockDocRemove).not.toHaveBeenCalled(); + }; + + 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); + expect(mockDocRemove).toHaveBeenCalled(); + }; + + await serviceConversationListener(baseContext, event1, callback1); + }); +}); From bb6c5aebe965e0fdf098056cf427890455825894 Mon Sep 17 00:00:00 2001 From: Stephen Okpalaononuju Date: Fri, 26 Jul 2024 10:05:39 +0100 Subject: [PATCH 2/3] ch: remove unwanted test mock --- tests/webhooks/serviceConversationListener.test.ts | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/tests/webhooks/serviceConversationListener.test.ts b/tests/webhooks/serviceConversationListener.test.ts index 44ea13b6..a0f91851 100644 --- a/tests/webhooks/serviceConversationListener.test.ts +++ b/tests/webhooks/serviceConversationListener.test.ts @@ -19,9 +19,6 @@ import { Event as Body } from '../../functions/helpers/sendErrorMessageForUnsupp import { handler as serviceConversationListener } from '../../functions/webhooks/serviceConversationListener.protected'; import helpers, { MockedResponse } from '../helpers'; -// eslint-disable-next-line @typescript-eslint/no-unused-vars -const mockDocRemove = jest.fn((doc: string) => true); - const baseContext = { getTwilioClient: (): any => ({ conversations: { @@ -51,9 +48,6 @@ describe('serviceConversationListener', () => { afterAll(() => { helpers.teardown(); }); - afterEach(() => { - mockDocRemove.mockClear(); - }); test('Should return status 400 if Body, Media or EventType are undefined', async () => { const event1: Body = { @@ -83,9 +77,7 @@ describe('serviceConversationListener', () => { const callback: ServerlessCallback = (err, result) => { expect(result).toBeDefined(); const response = result as MockedResponse; - expect(response.getStatus()).toBe(400); - expect(mockDocRemove).not.toHaveBeenCalled(); }; await serviceConversationListener(baseContext, event1, callback); @@ -106,7 +98,6 @@ describe('serviceConversationListener', () => { expect(result).toBeDefined(); const response = result as MockedResponse; expect(response.getStatus()).toBe(400); - expect(mockDocRemove).not.toHaveBeenCalled(); }; await serviceConversationListener({ ...baseContext }, event1, callback1); @@ -123,7 +114,6 @@ describe('serviceConversationListener', () => { expect(result).toBeDefined(); const response = result as MockedResponse; expect(response.getStatus()).toBe(500); - expect(mockDocRemove).not.toHaveBeenCalled(); }; await serviceConversationListener(baseContext, event2, callback2); @@ -142,7 +132,6 @@ describe('serviceConversationListener', () => { expect(result).toBeDefined(); const response = result as MockedResponse; expect(response.getStatus()).toBe(200); - expect(mockDocRemove).toHaveBeenCalled(); }; await serviceConversationListener(baseContext, event1, callback1); From 86a2c40da4d36d9baa5fafa3bd8ae9e3ec70f9b8 Mon Sep 17 00:00:00 2001 From: Stephen Okpalaononuju Date: Fri, 26 Jul 2024 10:08:26 +0100 Subject: [PATCH 3/3] ch: remove unwanted test mock --- tests/webhooks/serviceConversationListener.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/webhooks/serviceConversationListener.test.ts b/tests/webhooks/serviceConversationListener.test.ts index a0f91851..b7ee0c4f 100644 --- a/tests/webhooks/serviceConversationListener.test.ts +++ b/tests/webhooks/serviceConversationListener.test.ts @@ -97,7 +97,7 @@ describe('serviceConversationListener', () => { const callback1: ServerlessCallback = (err, result) => { expect(result).toBeDefined(); const response = result as MockedResponse; - expect(response.getStatus()).toBe(400); + expect(response.getStatus()).toBe(500); }; await serviceConversationListener({ ...baseContext }, event1, callback1);