Skip to content

STRATCONN-5889 - [Batch] - New Alec / Batch Destination #2726

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
@@ -0,0 +1,69 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Testing snapshot for actions-batch destination: trackEvent action - all fields 1`] = `
Array [
Object {
"events": Array [
Object {
"attributes": Object {
"testType": "ek)J)lzyONWw(04s",
},
"name": "ek)J)lzyONWw(04s",
},
],
"identifiers": Object {
"custom_id": "ek)J)lzyONWw(04s",
},
},
]
`;

exports[`Testing snapshot for actions-batch destination: trackEvent action - required fields 1`] = `
Array [
Object {
"events": Array [],
"identifiers": Object {
"custom_id": "ek)J)lzyONWw(04s",
},
},
]
`;

exports[`Testing snapshot for actions-batch destination: updateProfile action - all fields 1`] = `
Array [
Object {
"attributes": Object {
"$email_address": "E$Fjd*VczxvyBpVFPB",
"$email_marketing": "E$Fjd*VczxvyBpVFPB",
"$language": "E$Fjd*VczxvyBpVFPB",
"$phone_number": "E$Fjd*VczxvyBpVFPB",
"$region": "E$Fjd*VczxvyBpVFPB",
"$sms_marketing": "E$Fjd*VczxvyBpVFPB",
"$timezone": "E$Fjd*VczxvyBpVFPB",
"testType": "E$Fjd*VczxvyBpVFPB",
},
"identifiers": Object {
"custom_id": "E$Fjd*VczxvyBpVFPB",
},
},
]
`;

exports[`Testing snapshot for actions-batch destination: updateProfile action - required fields 1`] = `
Array [
Object {
"attributes": Object {
"$email_address": null,
"$email_marketing": null,
"$language": null,
"$phone_number": null,
"$region": null,
"$sms_marketing": null,
"$timezone": null,
},
"identifiers": Object {
"custom_id": "E$Fjd*VczxvyBpVFPB",
},
},
]
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*import nock from 'nock'
import { createTestEvent, createTestIntegration } from '@segment/actions-core'
import Definition from '../index'
const testDestination = createTestIntegration(Definition)
describe('Batch', () => {
describe('testAuthentication', () => {
it('should validate authentication inputs', async () => {
nock('https://your.destination.endpoint').get('*').reply(200, {})
// This should match your authentication.fields
const authData = {}
await expect(testDestination.testAuthentication(authData)).resolves.not.toThrowError()
})
})
})
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { createTestEvent, createTestIntegration } from '@segment/actions-core'
import { generateTestData } from '../../../lib/test-data'
import destination from '../index'
import nock from 'nock'

const testDestination = createTestIntegration(destination)
const destinationSlug = 'actions-batch'

describe(`Testing snapshot for ${destinationSlug} destination:`, () => {
for (const actionSlug in destination.actions) {
it(`${actionSlug} action - required fields`, async () => {
const seedName = `${destinationSlug}#${actionSlug}`
const action = destination.actions[actionSlug]
const [eventData, settingsData] = generateTestData(seedName, destination, action, true)

nock(/.*/).persist().get(/.*/).reply(200)
nock(/.*/).persist().post(/.*/).reply(200)
nock(/.*/).persist().put(/.*/).reply(200)

const event = createTestEvent({
properties: eventData
})

const responses = await testDestination.testAction(actionSlug, {
event: event,
mapping: event.properties,
settings: settingsData,
auth: undefined
})

const request = responses[0].request
const rawBody = await request.text()

try {
const json = JSON.parse(rawBody)
expect(json).toMatchSnapshot()
return
} catch (err) {
expect(rawBody).toMatchSnapshot()
}

expect(request.headers).toMatchSnapshot()
})

it(`${actionSlug} action - all fields`, async () => {
const seedName = `${destinationSlug}#${actionSlug}`
const action = destination.actions[actionSlug]
const [eventData, settingsData] = generateTestData(seedName, destination, action, false)

nock(/.*/).persist().get(/.*/).reply(200)
nock(/.*/).persist().post(/.*/).reply(200)
nock(/.*/).persist().put(/.*/).reply(200)

const event = createTestEvent({
properties: eventData
})

const responses = await testDestination.testAction(actionSlug, {
event: event,
mapping: event.properties,
settings: settingsData,
auth: undefined
})

const request = responses[0].request
const rawBody = await request.text()

try {
const json = JSON.parse(rawBody)
expect(json).toMatchSnapshot()
return
} catch (err) {
expect(rawBody).toMatchSnapshot()
}
})
}
})

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

64 changes: 64 additions & 0 deletions packages/destination-actions/src/destinations/batch/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import type { DestinationDefinition } from '@segment/actions-core'
import type { Settings } from './generated-types'
import { DEFAULT_REQUEST_TIMEOUT } from '@segment/actions-core'

import updateProfile from './updateProfile'
import trackEvent from './trackEvent'

const destination: DestinationDefinition<Settings> = {
name: 'Batch',
slug: 'actions-batch',
mode: 'cloud',

authentication: {
scheme: 'custom',
fields: {
apiToken: {
label: 'REST API Key',
description: 'Token used to authorize sending data to the Destination platform',
type: 'password',
required: true
},
projectKey: {
label: 'Project Key',
description: 'The unique project key identifying your project in the Destination platform',
type: 'string',
required: true
}
},
testAuthentication: async (request, { settings }) => {
// Check the authentification
const response = await request('https://api.batch.com/2.2/profiles/update', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${settings.apiToken}`,
'X-Batch-Project': `${settings.projectKey}`
}
})

// If the response is a status code of 200, this means that authentication is valid
if (response.status !== 200) {
throw new Error('Invalid API token or project key')
}
}
},

extendRequest({ settings }) {
return {
headers: {
'Content-Type': 'application/json', // Content Type
Authorization: `Bearer ${settings.apiToken}`, // REST API Key
'X-Batch-Project': `${settings.projectKey}` // Project Key
},
timeout: Math.max(30_000, DEFAULT_REQUEST_TIMEOUT)
}
},

actions: {
updateProfile,
trackEvent
}
}

export default destination
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Testing snapshot for Batch's trackEvent destination action: all fields 1`] = `
Array [
Object {
"events": Array [
Object {
"attributes": Object {},
"name": "8HZCkO#V",
},
],
"identifiers": Object {
"custom_id": "8HZCkO#V",
},
},
]
`;

exports[`Testing snapshot for Batch's trackEvent destination action: required fields 1`] = `
Array [
Object {
"events": Array [],
"identifiers": Object {
"custom_id": "8HZCkO#V",
},
},
]
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { createTestEvent, createTestIntegration } from '@segment/actions-core'
import { generateTestData } from '../../../../lib/test-data'
import destination from '../../index'
import nock from 'nock'

const testDestination = createTestIntegration(destination)
const actionSlug = 'trackEvent'
const destinationSlug = 'Batch'
const seedName = `${destinationSlug}#${actionSlug}`

describe(`Testing snapshot for ${destinationSlug}'s ${actionSlug} destination action:`, () => {
it('required fields', async () => {
const action = destination.actions[actionSlug]
const [eventData, settingsData] = generateTestData(seedName, destination, action, true)

nock(/.*/).persist().get(/.*/).reply(200)
nock(/.*/).persist().post(/.*/).reply(200)
nock(/.*/).persist().put(/.*/).reply(200)

const event = createTestEvent({
properties: eventData
})

const responses = await testDestination.testAction(actionSlug, {
event: event,
mapping: event.properties,
settings: settingsData,
auth: undefined
})

const request = responses[0].request
const rawBody = await request.text()

try {
const json = JSON.parse(rawBody)
expect(json).toMatchSnapshot()
return
} catch (err) {
expect(rawBody).toMatchSnapshot()
}

expect(request.headers).toMatchSnapshot()
})

it('all fields', async () => {
const action = destination.actions[actionSlug]
const [eventData, settingsData] = generateTestData(seedName, destination, action, false)

nock(/.*/).persist().get(/.*/).reply(200)
nock(/.*/).persist().post(/.*/).reply(200)
nock(/.*/).persist().put(/.*/).reply(200)

const event = createTestEvent({
properties: eventData
})

const responses = await testDestination.testAction(actionSlug, {
event: event,
mapping: event.properties,
settings: settingsData,
auth: undefined
})

const request = responses[0].request
const rawBody = await request.text()

try {
const json = JSON.parse(rawBody)
expect(json).toMatchSnapshot()
return
} catch (err) {
expect(rawBody).toMatchSnapshot()
}
})
})

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

Loading