Skip to content

Commit

Permalink
adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joe-ayoub-segment committed Oct 22, 2024
1 parent b97454d commit fa6b3a3
Show file tree
Hide file tree
Showing 5 changed files with 290 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,281 @@
import nock from 'nock'
import { createTestEvent, createTestIntegration } from '@segment/actions-core'
import Destination from '../../index'
import { createTestEvent, createTestIntegration, SegmentEvent, PayloadValidationError } from '@segment/actions-core'
import Definition from '../../index'
import { Settings } from '../../generated-types'
import { RESERVED_HEADERS } from '../constants'

const testDestination = createTestIntegration(Destination)
let testDestination = createTestIntegration(Definition)

const timestamp = '2024-01-08T13:52:50.212Z'
const settings: Settings = {
sendGridApiKey: 'test-api-key'
}
const validPayload = {
timestamp: timestamp,
event: 'Send Email From Template',
messageId: 'aaa-bbb-ccc',
type: 'track',
userId: 'user_id_1',
context: {
campaign: {
source: 'source1',
medium: 'medium1',
term: 'term1',
content: 'content1',
name: 'name1'
}
},
properties: {
from: {
email: '[email protected]',
name: 'Billy Joe'
},
to: {
email: '[email protected]',
name: 'Mary Jane'
},
cc: [
{
email: '[email protected]',
name: 'CC 1'
},
{
email: '[email protected]',
name: 'CC 2'
}
],
bcc: [
{
email: '[email protected]',
name: 'BCC 1'
},
{
email: '[email protected]',
name: 'BCC 2'
}
],
headers: {
testHeader1: 'testHeaderValue1',
},
dynamic_template_data: {
stringVal: 'stringVal',
numVal: 123456,
boolVal: true,
objVal: {
key1: 'value1',
key2: 'value2'
},
arrayVal: ['value1', 'value2']
},
template_id: 'd-1234567890',
custom_args: {
custom_arg1: 'custom_arg_value1',
custom_arg2: 'custom_arg_value2'
},
reply_to: {
reply_to_equals_from: true,
},
subscription_tracking: {
enable: false
},
categories: ['category1', 'category2'],
google_analytics: {
enable: true
},
ip_pool_name: 'ip_pool_name1',
asm: {
group_id: 123
},
mail_settings: {
sandbox_mode: false
}
}
} as Partial<SegmentEvent>
const mapping = {
from: {'@path' : '$.properties.from'},
to: {'@path' : '$.properties.to'},
cc: {'@path' : '$.properties.cc'},
bcc: {'@path' : '$.properties.bcc'},
headers: {'@path' : '$.properties.headers'},
dynamic_template_data: {'@path' : '$.properties.dynamic_template_data'},
template_id: {'@path' : '$.properties.template_id'},
custom_args: {'@path' : '$.properties.custom_args'},
reply_to: {'@path' : '$.properties.reply_to'},
subscription_tracking: {'@path' : '$.properties.subscription_tracking'},
categories: {'@path' : '$.properties.categories'},
google_analytics: {
enable: {'@path': "$.properties.google_analytics.enable"},
utm_source: {'@path': "$.context.campaign.source"},
utm_medium: {'@path': "$.context.campaign.medium"},
utm_term: {'@path': "$.context.campaign.term"},
utm_content: {'@path': "$.context.campaign.content"},
utm_campaign: {'@path': "$.context.campaign.name"}
},
ip_pool_name: {'@path' : '$.properties.ip_pool_name'},
asm: {'@path' : '$.properties.asm'},
mail_settings: {'@path' : '$.properties.mail_settings'},
send_at: {'@path' : '$.properties.send_at'}
}
const expectedSendgridPayload = {
personalizations: [
{
from: {
email: "[email protected]",
name: "Billy Joe"
},
to: [
{
email: "[email protected]",
name: "Mary Jane"
}
],
cc: [
{
email: "[email protected]",
name: "CC 1"
},
{
email: "[email protected]",
name: "CC 2"
}
],
bcc: [
{
email: "[email protected]",
name: "BCC 1"
},
{
email: "[email protected]",
name: "BCC 2"
}
],
headers: {
testHeader1: "testHeaderValue1"
},
dynamic_template_data: {
stringVal: "stringVal",
numVal: 123456,
boolVal: true,
objVal: {
key1: "value1",
key2: "value2"
},
arrayVal: ["value1", "value2"]
},
custom_args: {
custom_arg1: 'custom_arg_value1',
custom_arg2: 'custom_arg_value2'
}
}
],
reply_to: {
email: "[email protected]",
name: "Billy Joe"
},
template_id: "d-1234567890",
categories: ["category1", "category2"],
ip_pool_name: "ip_pool_name1",
tracking_settings: {
subscription_tracking: {
enable: false
},
ganalytics: {
enable: true,
utm_source: "source1",
utm_medium: "medium1",
utm_term: "term1",
utm_content: "content1",
utm_campaign: "name1"
}
},
mail_settings: {
sandbox_mode: false
}
}

beforeEach((done) => {
testDestination = createTestIntegration(Definition)
nock.cleanAll()
done()
})

describe('Sendgrid.sendEmail', () => {
// TODO: Test your action
it('should send an email', async () => {
const event = createTestEvent(validPayload)
// send email via Sendgrid
nock('https://api.sendgrid.com').post('/v3/mail/send', expectedSendgridPayload).reply(200, {})
const responses = await testDestination.testAction('sendEmail', {
event,
settings,
useDefaultMappings: true,
mapping
})
expect(responses.length).toBe(1)
expect(responses[0].status).toBe(200)
})

it('should throw error if bad headers', async () => {
const badPayload = { ...validPayload, properties: {...validPayload.properties, headers: {
testHeader1: 'testHeaderValue1',
"dkim-signature": "baaaad illegal header"
}}}
const event = createTestEvent(badPayload)
await expect(
testDestination.testAction('sendEmail', {
event,
settings,
useDefaultMappings: true,
mapping
})
).rejects.toThrowError(
new PayloadValidationError(`Headers cannot contain any of the following reserved headers: ${RESERVED_HEADERS.join(', ')}`)
)
})

it('should throw error if bad template ID', async () => {
const badPayload = { ...validPayload, properties: {...validPayload.properties, template_id: '1234567890'}}
const event = createTestEvent(badPayload)
await expect(
testDestination.testAction('sendEmail', {
event,
settings,
useDefaultMappings: true,
mapping
})
).rejects.toThrowError(
new PayloadValidationError(`Template ID must refer to a Dynamic Template. Dynamic Template IDs start with "d-"`)
)
})

it('should throw error if send_at more than 72h in future', async () => {
const send_at = new Date(Date.now() + 73 * 60 * 60 * 1000)
const badPayload = { ...validPayload, properties: {...validPayload.properties, send_at: send_at.toISOString()}}
const event = createTestEvent(badPayload)
await expect(
testDestination.testAction('sendEmail', {
event,
settings,
useDefaultMappings: true,
mapping
})
).rejects.toThrowError(
new PayloadValidationError(`send_at should be less than 72 hours from now`)
)
})

it('should throw error if send_at in the past', async () => {
const send_at = new Date(Date.now() - 100 * 60 * 60 * 1000)
const badPayload = { ...validPayload, properties: {...validPayload.properties, send_at: send_at.toISOString()}}
const event = createTestEvent(badPayload)
await expect(
testDestination.testAction('sendEmail', {
event,
settings,
useDefaultMappings: true,
mapping
})
).rejects.toThrowError(
new PayloadValidationError(`send_at should be less than 72 hours from now`)
)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ export const MIN_IP_POOL_NAME_LENGTH = 2

export const MAX_IP_POOL_NAME_LENGTH = 64

export const SEND_EMAIL_URL = 'https://api.sendgrid.com/v3/mail/send'

Original file line number Diff line number Diff line change
Expand Up @@ -200,19 +200,9 @@ export const fields: Record<string, InputField> = {
categories: {
label: 'Categories',
description: 'Categories for the email.',
type: 'object',
type: 'string',
multiple: true,
required: false,
defaultObjectUI: 'keyvalue',
additionalProperties: false,
properties: {
category: {
label: 'Category Name',
description: 'Category Name.',
type: 'string',
required: true
}
}
},
google_analytics: {
label: 'Google Analytics',
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RequestClient, PayloadValidationError } from '@segment/actions-core'
import type { Payload } from './generated-types'
import { SendEmailReq } from './types'
import { RESERVED_HEADERS, MAX_CATEGORY_LENGTH, MIN_IP_POOL_NAME_LENGTH, MAX_IP_POOL_NAME_LENGTH } from './constants'
import { RESERVED_HEADERS, MAX_CATEGORY_LENGTH, MIN_IP_POOL_NAME_LENGTH, MAX_IP_POOL_NAME_LENGTH, SEND_EMAIL_URL } from './constants'

export async function send(request: RequestClient, payload: Payload) {
validate(payload)
Expand Down Expand Up @@ -32,7 +32,7 @@ export async function send(request: RequestClient, payload: Payload) {
name: payload.reply_to.reply_to_equals_from ? payload.from.name : payload.reply_to.name
},
template_id: payload.template_id,
categories: payload.categories?.map((category) => category.category),
categories: payload.categories,
asm: payload.ASM ? { group_id: payload.ASM.groupId as number } : undefined,
ip_pool_name: payload.ip_pool_name,
tracking_settings: {
Expand All @@ -42,7 +42,7 @@ export async function send(request: RequestClient, payload: Payload) {
mail_settings: payload.mail_settings ?? undefined
}

return await request('https://api.sendgrid.com/v3/mail/send', {
return await request(SEND_EMAIL_URL, {
method: 'post',
json
})
Expand All @@ -52,10 +52,15 @@ function toUnixTS(date: string | undefined): number | undefined {
if (typeof date === 'undefined' || date === null || date === '') {
return undefined
}

return new Date(date).getTime()
}

function validate(payload: Payload) {
if(!payload.template_id.startsWith('d-')) {
throw new PayloadValidationError('Template ID must refer to a Dynamic Template. Dynamic Template IDs start with "d-"')
}

if (!payload.reply_to.reply_to_equals_from && !payload.reply_to.email) {
throw new PayloadValidationError("'Reply To >> Email' must be provided if 'Reply To Equals From' is set to true")
}
Expand All @@ -67,9 +72,9 @@ function validate(payload: Payload) {
}

payload?.categories?.forEach((category) => {
if (category.category.length >= MAX_CATEGORY_LENGTH) {
if (category.length > MAX_CATEGORY_LENGTH) {
throw new PayloadValidationError(
`Category with name ${category.category} exceeds the max length of ${MAX_CATEGORY_LENGTH} characters`
`Category with name ${category} exceeds the max length of ${MAX_CATEGORY_LENGTH} characters`
)
}
})
Expand Down

0 comments on commit fa6b3a3

Please sign in to comment.