From d276a519f6c0a489ac82006f263a38ca27ffed55 Mon Sep 17 00:00:00 2001 From: Antoine SEIN <142824551+asein-sinch@users.noreply.github.com> Date: Thu, 22 Aug 2024 11:14:32 +0200 Subject: [PATCH] DEVEXP-519: E2E SMS/Webhooks --- .../src/services/sms-event.service.ts | 6 ++ .../v1/callbacks/webhooks-events.steps.ts | 84 +++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 packages/sms/tests/rest/v1/callbacks/webhooks-events.steps.ts diff --git a/examples/webhooks/src/services/sms-event.service.ts b/examples/webhooks/src/services/sms-event.service.ts index 1da6338d..0e318582 100644 --- a/examples/webhooks/src/services/sms-event.service.ts +++ b/examples/webhooks/src/services/sms-event.service.ts @@ -8,6 +8,8 @@ export class SmsEventService { console.log(`:: INCOMING EVENT :: ${event.type}`); if (event.type === 'delivery_report_sms' || event.type === 'delivery_report_mms') { return this.handleDeliveryReportEvent(event as Sms.DeliveryReport); + } else if (event.type === 'recipient_delivery_report_sms' || event.type === 'recipient_delivery_report_mms') { + return this.handleRecipientDeliveryReportEvent(event as Sms.RecipientDeliveryReport); } else if (event.type === 'mo_text') { return this.handleSmsEvent(event as Sms.MOText); } else if (event.type === 'mo_binary') { @@ -21,6 +23,10 @@ export class SmsEventService { console.log(`The batch ${event.batch_id} has the following statuses:\n${event.statuses.map((status) => ' - \'' + status.status + '\' for the recipients: ' + status.recipients?.join(', ')).join('\n')} `); } + private handleRecipientDeliveryReportEvent(event: Sms.RecipientDeliveryReport): void { + console.log(`The batch ${event.batch_id} has the status "${event.status}" (code: ${event.code}) for the recipient ${event.recipient}`); + } + private handleSmsEvent(event: Sms.MOText): void { console.log(`A SMS sent by ${event.from} has been received by ${event.to} at ${event.received_at}. The content is:\n"${event.body}"`); } diff --git a/packages/sms/tests/rest/v1/callbacks/webhooks-events.steps.ts b/packages/sms/tests/rest/v1/callbacks/webhooks-events.steps.ts new file mode 100644 index 00000000..b1de8ac8 --- /dev/null +++ b/packages/sms/tests/rest/v1/callbacks/webhooks-events.steps.ts @@ -0,0 +1,84 @@ +import { Given, Then, When } from '@cucumber/cucumber'; +import { SmsCallbackWebhooks, SmsCallback, Sms } from '../../../../src'; +import assert from 'assert'; + +let smsCallbackWebhook: SmsCallbackWebhooks; +let rawEvent: any; +let event: SmsCallback; + +const processEvent = async (response: Response) => { + rawEvent = await response.text(); + event = smsCallbackWebhook.parseEvent(JSON.parse(rawEvent)); +}; + +Given('the SMS Webhooks handler is available', () => { + smsCallbackWebhook = new SmsCallbackWebhooks(); +}); + +When('I send a request to trigger an "incoming SMS" event', async () => { + const response = await fetch('http://localhost:3017/webhooks/sms/incoming-sms'); + await processEvent(response); +}); + +Then('the SMS event describes an "incoming SMS" event', () => { + const incomingSmsEvent = event as Sms.MOText; + assert.equal(incomingSmsEvent.id, '01W4FFL35P4NC4K35SMSBATCH8'); + assert.equal(incomingSmsEvent.from, '12015555555'); + assert.equal(incomingSmsEvent.to, '12017777777'); + assert.equal(incomingSmsEvent.body, 'Hello John! 👋'); + assert.equal(incomingSmsEvent.type, 'mo_text'); + assert.equal(incomingSmsEvent.operator_id, '311071'); + assert.deepEqual(incomingSmsEvent.received_at, new Date('2024-06-06T07:52:37.386Z')); +}); + +When('I send a request to trigger an "SMS delivery report" event', async () => { + const response = await fetch('http://localhost:3017/webhooks/sms/delivery-report-sms'); + await processEvent(response); +}); + +Then('the SMS event describes an "SMS delivery report" event', () => { + const deliveryReportEvent = event as Sms.DeliveryReport; + assert.equal(deliveryReportEvent.batch_id, '01W4FFL35P4NC4K35SMSBATCH8'); + assert.equal(deliveryReportEvent.client_reference, 'client-ref'); + assert.ok(deliveryReportEvent.statuses); + const status = deliveryReportEvent.statuses[0]; + assert.equal(status.code, 0); + assert.equal(status.count, 2); + const deliveryStatus: Sms.DeliveryReportStatusEnum = 'Delivered'; + assert.equal(status.status, deliveryStatus); + assert.ok(status.recipients); + assert.equal(status.recipients[0], '12017777777'); + assert.equal(status.recipients[1], '33612345678'); + assert.equal(deliveryReportEvent.type, 'delivery_report_sms'); +}); + +// eslint-disable-next-line max-len +When('I send a request to trigger an "SMS recipient delivery report" event with the status {string}', async (status: string) => { + const response = await fetch(`http://localhost:3017/webhooks/sms/recipient-delivery-report-sms-${status.toLowerCase()}`); + await processEvent(response); +}); + +Then('the SMS event describes an SMS recipient delivery report event with the status "Delivered"', () => { + const recipientDeliveryReportEvent = event as Sms.RecipientDeliveryReport; + assert.equal(recipientDeliveryReportEvent.batch_id, '01W4FFL35P4NC4K35SMSBATCH9'); + assert.equal(recipientDeliveryReportEvent.recipient, '12017777777'); + assert.equal(recipientDeliveryReportEvent.code, 0); + const deliveryStatus: Sms.DeliveryReportStatusEnum = 'Delivered'; + assert.equal(recipientDeliveryReportEvent.status, deliveryStatus); + assert.equal(recipientDeliveryReportEvent.type, 'recipient_delivery_report_sms'); + assert.equal(recipientDeliveryReportEvent.client_reference, 'client-ref'); + assert.deepEqual(recipientDeliveryReportEvent.at, new Date('2024-06-06T08:17:19.210Z')); + assert.deepEqual(recipientDeliveryReportEvent.operator_status_at, new Date('2024-06-06T08:17:00Z')); +}); + +Then('the SMS event describes an SMS recipient delivery report event with the status "Aborted"', () => { + const recipientDeliveryReportEvent = event as Sms.RecipientDeliveryReport; + assert.equal(recipientDeliveryReportEvent.batch_id, '01W4FFL35P4NC4K35SMSBATCH9'); + assert.equal(recipientDeliveryReportEvent.recipient, '12010000000'); + assert.equal(recipientDeliveryReportEvent.code, 412); + const deliveryStatus: Sms.DeliveryReportStatusEnum = 'Aborted'; + assert.equal(recipientDeliveryReportEvent.status, deliveryStatus); + assert.equal(recipientDeliveryReportEvent.type, 'recipient_delivery_report_sms'); + assert.equal(recipientDeliveryReportEvent.client_reference, 'client-ref'); + assert.deepEqual(recipientDeliveryReportEvent.at, new Date('2024-06-06T08:17:15.603Z')); +});