Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fcm payload update #2073

Merged
merged 3 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const spaceId = 'spaceid'
const contentSid = 'HX1234'
const pushServiceSid = 'ISXXX'
const devices = ['android', 'ios']
const FcmPayload = '{}'

const defaultTemplate = {
types: {
Expand Down Expand Up @@ -49,15 +50,10 @@ const testAction = createTestAction({
})

const getDefaultExpectedNotifyApiReq = (extId: NonNullable<Payload['externalIds']>[number]) => {
return new URLSearchParams({
const params = new URLSearchParams({
Body: defaultTemplate.types['twilio/text'].body,
Title: customizationTitle,
FcmPayload: JSON.stringify({
mutable_content: true,
notification: {
badge: 1
}
}),
FcmPayload: JSON.stringify({}),
ApnPayload: JSON.stringify({
aps: {
'mutable-content': 1,
Expand All @@ -80,6 +76,8 @@ const getDefaultExpectedNotifyApiReq = (extId: NonNullable<Payload['externalIds'
__segment_internal_external_id_value__: extId.id
})
})
params.sort()
return params
}

describe('sendMobilePush action', () => {
Expand Down Expand Up @@ -114,6 +112,7 @@ describe('sendMobilePush action', () => {
'DeliveryCallbackUrl',
`http://localhost.com/?space_id=spaceid&__segment_internal_external_id_key__=${externalId.type}&__segment_internal_external_id_value__=${externalId.id}#rp=all&rc=600`
)
notifyReqBody.sort()

nock(`https://content.twilio.com`).get(`/v1/Content/${contentSid}`).reply(200, defaultTemplate)
nock(notifyReqUrl).post('', notifyReqBody.toString()).reply(201, externalId)
Expand Down Expand Up @@ -259,12 +258,7 @@ describe('sendMobilePush action', () => {
const notifyReqBody = new URLSearchParams({
Body: 'Welcome to Wadiya General Aladeen!',
Title: 'Aladeen',
FcmPayload: JSON.stringify({
mutable_content: true,
notification: {
badge: 1
}
}),
FcmPayload,
ApnPayload: JSON.stringify({
aps: {
'mutable-content': 1,
Expand All @@ -283,6 +277,7 @@ describe('sendMobilePush action', () => {
__segment_internal_external_id_value__: 'ios-token-1'
})
})
notifyReqBody.sort()

const notifyReqUrl = `https://push.ashburn.us1.twilio.com/v1/Services/${pushServiceSid}/Notifications`
nock(notifyReqUrl).post('', notifyReqBody.toString()).reply(201, externalIds[0])
Expand Down Expand Up @@ -310,12 +305,7 @@ describe('sendMobilePush action', () => {
const notifyReqBody = new URLSearchParams({
Body: 'I have Aladeeen news',
Title: 'General',
FcmPayload: JSON.stringify({
mutable_content: true,
notification: {
badge: 1
}
}),
FcmPayload,
ApnPayload: JSON.stringify({
aps: {
'mutable-content': 1,
Expand All @@ -334,6 +324,7 @@ describe('sendMobilePush action', () => {
__segment_internal_external_id_value__: 'ios-token-1'
})
})
notifyReqBody.sort()

const notifyReqUrl = `https://push.ashburn.us1.twilio.com/v1/Services/${pushServiceSid}/Notifications`
nock(notifyReqUrl).post('', notifyReqBody.toString()).reply(201, externalIds[0])
Expand Down Expand Up @@ -390,12 +381,7 @@ describe('sendMobilePush action', () => {
Sound: sound,
Priority: priority,
TimeToLive: ttl.toString(),
FcmPayload: JSON.stringify({
mutable_content: true,
notification: {
badge: customizations.badgeAmount
}
}),
FcmPayload,
ApnPayload: JSON.stringify({
aps: {
'mutable-content': 1,
Expand Down Expand Up @@ -428,6 +414,7 @@ describe('sendMobilePush action', () => {
__segment_internal_external_id_value__: 'ios-token-1'
})
})
notificationReq.sort()

const notifyReqUrl = `https://push.ashburn.us1.twilio.com/v1/Services/${pushServiceSid}/Notifications`
nock(notifyReqUrl).persist().post('', notificationReq.toString()).reply(201, externalIds[0])
Expand Down Expand Up @@ -488,12 +475,7 @@ describe('sendMobilePush action', () => {
Sound: sound,
Priority: priority,
TimeToLive: ttl.toString(),
FcmPayload: JSON.stringify({
mutable_content: true,
notification: {
badge: customizations.badgeAmount
}
}),
FcmPayload,
ApnPayload: JSON.stringify({
aps: {
'mutable-content': 1,
Expand All @@ -510,6 +492,7 @@ describe('sendMobilePush action', () => {
__segment_internal_external_id_value__: 'ios-token-1'
})
})
notificationReq.sort()

const notifyReqUrl = `https://push.ashburn.us1.twilio.com/v1/Services/${pushServiceSid}/Notifications`
nock(notifyReqUrl).post('', notificationReq.toString()).reply(201, externalIds[0])
Expand All @@ -533,12 +516,7 @@ describe('sendMobilePush action', () => {
Sound: sound,
Priority: priority,
TimeToLive: ttl.toString(),
FcmPayload: JSON.stringify({
mutable_content: true,
notification: {
badge: customizations.badgeAmount
}
}),
FcmPayload,
ApnPayload: JSON.stringify({
aps: {
'mutable-content': 1,
Expand All @@ -565,6 +543,7 @@ describe('sendMobilePush action', () => {
__segment_internal_external_id_value__: 'ios-token-1'
})
})
notificationReq.sort()

const notifyReqUrl = `https://push.ashburn.us1.twilio.com/v1/Services/${pushServiceSid}/Notifications`
nock(notifyReqUrl).post('', notificationReq.toString()).reply(201, externalIds[0])
Expand Down Expand Up @@ -606,12 +585,7 @@ describe('sendMobilePush action', () => {
Sound: sound,
Priority: priority,
TimeToLive: ttl.toString(),
FcmPayload: JSON.stringify({
mutable_content: true,
notification: {
badge: customizations.badgeAmount
}
}),
FcmPayload,
ApnPayload: JSON.stringify({
aps: {
'mutable-content': 1,
Expand All @@ -628,6 +602,7 @@ describe('sendMobilePush action', () => {
__segment_internal_external_id_value__: 'ios-token-1'
})
})
notificationReq.sort()

const notifyReqUrl = `https://push.ashburn.us1.twilio.com/v1/Services/${pushServiceSid}/Notifications`
nock(notifyReqUrl).post('', notificationReq.toString()).reply(201, externalIds[0])
Expand Down Expand Up @@ -660,12 +635,7 @@ describe('sendMobilePush action', () => {
Sound: sound,
Priority: priority,
TimeToLive: ttl.toString(),
FcmPayload: JSON.stringify({
mutable_content: true,
notification: {
badge: customizations.badgeAmount
}
}),
FcmPayload,
ApnPayload: JSON.stringify({
aps: {
'mutable-content': 1,
Expand All @@ -683,6 +653,7 @@ describe('sendMobilePush action', () => {
__segment_internal_external_id_value__: 'ios-token-1'
})
})
notificationReq.sort()

const notifyReqUrl = `https://push.ashburn.us1.twilio.com/v1/Services/${pushServiceSid}/Notifications`
nock(`https://content.twilio.com`).get(`/v1/Content/${contentSid}`).reply(200, template)
Expand Down Expand Up @@ -719,12 +690,7 @@ describe('sendMobilePush action', () => {
Sound: sound,
Priority: priority,
TimeToLive: ttl.toString(),
FcmPayload: JSON.stringify({
mutable_content: true,
notification: {
badge: customizations.badgeAmount
}
}),
FcmPayload,
ApnPayload: JSON.stringify({
aps: {
'mutable-content': 1,
Expand All @@ -742,6 +708,7 @@ describe('sendMobilePush action', () => {
__segment_internal_external_id_value__: 'ios-token-1'
})
})
notificationReq.sort()

const notifyReqUrl = `https://push.ashburn.us1.twilio.com/v1/Services/${pushServiceSid}/Notifications`
nock(`https://content.twilio.com`).get(`/v1/Content/${contentSid}`).reply(200, template)
Expand All @@ -758,128 +725,4 @@ describe('sendMobilePush action', () => {
expect(responses[1].data).toMatchObject(externalIds[0])
})
})

describe('Google Api Formatting', () => {
const externalId = {
type: 'android.push_token',
id: 'android-token-1',
channelType: 'ANDROID_PUSH',
subscriptionStatus: 'subscribed'
}

const androidLegacyReq = new URLSearchParams({
Body: defaultTemplate.types['twilio/text'].body,
Title: customizationTitle,
FcmPayload: JSON.stringify({
mutable_content: true,
notification: {
badge: 1
}
}),
ApnPayload: JSON.stringify({
aps: {
'mutable-content': 1,
badge: 1
}
}),
Recipients: JSON.stringify({
fcm: [{ addr: externalId.id }]
}),
CustomData: JSON.stringify({
space_id: spaceId,
badgeAmount: 1,
badgeStrategy: 'inc',
__segment_internal_external_id_key__: externalId.type,
__segment_internal_external_id_value__: externalId.id
})
})

const androidV1Req = new URLSearchParams({
Body: defaultTemplate.types['twilio/text'].body,
Title: customizationTitle,
FcmPayload: JSON.stringify({
android: {
mutable_content: true,
notification: {
badge: 1
}
}
}),
ApnPayload: JSON.stringify({
aps: {
'mutable-content': 1,
badge: 1
}
}),
Recipients: JSON.stringify({
fcm: [{ addr: externalId.id }]
}),
CustomData: JSON.stringify({
space_id: spaceId,
badgeAmount: 1,
badgeStrategy: 'inc',
__segment_internal_external_id_key__: externalId.type,
__segment_internal_external_id_value__: externalId.id
})
})

it('should format FCM overrides with legacy format when googleApiVersion field is not provided', async () => {
const notifyReqUrl = `https://push.ashburn.us1.twilio.com/v1/Services/${pushServiceSid}/Notifications`
const notifyReqBody = androidLegacyReq

nock(`https://content.twilio.com`).get(`/v1/Content/${contentSid}`).reply(200, defaultTemplate)
nock(notifyReqUrl).post('', notifyReqBody.toString()).reply(201, externalId)

const responses = await testAction({
mappingOverrides: {
externalIds: [externalId]
}
})
expect(responses[1].url).toStrictEqual(notifyReqUrl)
expect(responses[1].status).toEqual(201)
expect(responses[1].data).toMatchObject(externalId)
})

it('should format FCM overrides with legacy format when googleApiVersion field is set to legacy', async () => {
const notifyReqUrl = `https://push.ashburn.us1.twilio.com/v1/Services/${pushServiceSid}/Notifications`
const notifyReqBody = androidLegacyReq

nock(`https://content.twilio.com`).get(`/v1/Content/${contentSid}`).reply(200, defaultTemplate)
nock(notifyReqUrl).post('', notifyReqBody.toString()).reply(201, externalId)

const responses = await testAction({
mappingOverrides: {
googleApiVersion: 'legacy',
externalIds: [externalId]
}
})
expect(responses[1].url).toStrictEqual(notifyReqUrl)
expect(responses[1].status).toEqual(201)
expect(responses[1].data).toMatchObject(externalId)
})

it('should format FCM overrides with v1 format when googleApiVersion field is v1', async () => {
const externalId = {
type: 'android.push_token',
id: 'android-token-1',
channelType: 'ANDROID_PUSH',
subscriptionStatus: 'subscribed'
}
const notifyReqUrl = `https://push.ashburn.us1.twilio.com/v1/Services/${pushServiceSid}/Notifications`
const notifyReqBody = androidV1Req

nock(`https://content.twilio.com`).get(`/v1/Content/${contentSid}`).reply(200, defaultTemplate)
nock(notifyReqUrl).post('', notifyReqBody.toString()).reply(201, externalId)

const responses = await testAction({
mappingOverrides: {
googleApiVersion: 'v1',
externalIds: [externalId]
}
})
expect(responses[1].url).toStrictEqual(notifyReqUrl)
expect(responses[1].status).toEqual(201)
expect(responses[1].data).toMatchObject(externalId)
})
})
})
Loading
Loading