Skip to content

Commit

Permalink
Added condition for event name in hubspot (#1364)
Browse files Browse the repository at this point in the history
* Added condition for event name in hubspot

* Added portalId in settings and added unit test

* Added condition when portal Id is not configure in setting and added unit test

* Resolve comments for PR

---------

Co-authored-by: Harsh Vardhan <[email protected]>
  • Loading branch information
hvardhan-unth and Harsh Vardhan authored Jul 14, 2023
1 parent 1a60942 commit dee4caf
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Testing snapshot for actions-hubspot-cloud destination: sendCustomBehavioralEvent action - all fields 1`] = `
Object {
"email": "[email protected]",
"eventName": "NTxWtuPi(z!bOHmgT^*3",
"objectId": "NTxWtuPi(z!bOHmgT^*3",
"occurredAt": "2021-02-01T00:00:00.000Z",
"properties": Object {
"testType": "NTxWtuPi(z!bOHmgT^*3",
},
"utk": "NTxWtuPi(z!bOHmgT^*3",
}
`;
exports[`Testing snapshot for actions-hubspot-cloud destination: sendCustomBehavioralEvent action - all fields 1`] = `[PayloadValidationError: EventName should begin with peNTxWtuPi(z!bOHmgT^*3_]`;

exports[`Testing snapshot for actions-hubspot-cloud destination: sendCustomBehavioralEvent action - required fields 1`] = `[PayloadValidationError: One of the following parameters: email, user token, or objectId is required]`;

exports[`Testing snapshot for actions-hubspot-cloud destination: sendCustomBehavioralEvent action - required fields 1`] = `[PayloadValidationError: EventName should begin with peNTxWtuPi(z!bOHmgT^*3_]`;

exports[`Testing snapshot for actions-hubspot-cloud destination: upsertCompany action - all fields 1`] = `
Object {
"properties": Object {
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
Expand Up @@ -17,7 +17,13 @@ const destination: DestinationDefinition<Settings> = {

authentication: {
scheme: 'oauth2',
fields: {},
fields: {
portalId: {
description: 'The Hub ID of your HubSpot account.',
label: 'Hub ID',
type: 'string'
}
},
testAuthentication: (request) => {
// HubSpot doesn't have a test authentication endpoint, so we using a lightweight CRM API to validate access token
return request(`${HUBSPOT_BASE_URL}/crm/v3/objects/contacts?limit=1`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,16 @@ Object {
"utk": "abverazffa===1314122f",
}
`;

exports[`HubSpot.sendCustomBehavioralEvent should succeed when all fields are given 1`] = `
Object {
"email": "[email protected]",
"eventName": "pe22596207_test_event_http",
"objectId": "802",
"occurredAt": "2023-07-05T08:28:35.216Z",
"properties": Object {
"hs_city": "city",
},
"utk": "abverazffa===1314122f",
}
`;
Original file line number Diff line number Diff line change
@@ -1,37 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Testing snapshot for HubSpot's sendCustomBehavioralEvent destination action: all fields 1`] = `
Object {
"email": "[email protected]",
"eventName": "9B#[dv&Iy",
"objectId": "9B#[dv&Iy",
"occurredAt": "2021-02-01T00:00:00.000Z",
"properties": Object {
"testType": "9B#[dv&Iy",
},
"utk": "9B#[dv&Iy",
}
`;
exports[`Testing snapshot for HubSpot's sendCustomBehavioralEvent destination action: all fields 1`] = `[PayloadValidationError: EventName should begin with pe9B#[dv&Iy_]`;

exports[`Testing snapshot for HubSpot's sendCustomBehavioralEvent destination action: required fields 1`] = `
Object {
"email": "[email protected]",
"eventName": "9B#[dv&Iy",
}
`;

exports[`Testing snapshot for HubSpot's sendCustomBehavioralEvent destination action: required fields 2`] = `
Headers {
Symbol(map): Object {
"authorization": Array [
"Bearer undefined",
],
"content-type": Array [
"application/json",
],
"user-agent": Array [
"Segment (Actions)",
],
},
}
`;
exports[`Testing snapshot for HubSpot's sendCustomBehavioralEvent destination action: required fields 1`] = `[PayloadValidationError: EventName should begin with pe9B#[dv&Iy_]`;
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import Destination from '../../index'
import { HUBSPOT_BASE_URL } from '../../properties'

let testDestination = createTestIntegration(Destination)
const settings = {
portalId: '22596207'
}

beforeEach((done) => {
// Re-Initialize the destination before each test
Expand Down Expand Up @@ -166,6 +169,139 @@ describe('HubSpot.sendCustomBehavioralEvent', () => {
})
})

test('should fail when event name does not start with pe{hubId}_ and hubId is configured in settings', async () => {
const event = createTestEvent({
type: 'track',
event: 'test_event',
properties: {
email: '[email protected]',
utk: 'abverazffa===1314122f',
userId: '802',
city: 'city'
}
})

const mapping = {
eventName: {
'@path': '$.event'
},
utk: {
'@path': '$.properties.utk'
},
objectId: {
'@path': '$.properties.userId'
},
properties: {
hs_city: {
'@path': '$.properties.city'
}
}
}

return expect(
testDestination.testAction('sendCustomBehavioralEvent', {
event,
settings,
useDefaultMappings: true,
mapping: mapping
})
).rejects.toThrowError(`EventName should begin with pe${settings.portalId}_`)
})

test('should fail when event name does not start with pe{hubId}_ and hubId is not configured in settings', async () => {
const event = createTestEvent({
type: 'track',
event: 'test_event',
properties: {
email: '[email protected]',
utk: 'abverazffa===1314122f',
userId: '802',
city: 'city'
}
})

settings.portalId = ''

const mapping = {
eventName: {
'@path': '$.event'
},
utk: {
'@path': '$.properties.utk'
},
objectId: {
'@path': '$.properties.userId'
},
properties: {
hs_city: {
'@path': '$.properties.city'
}
}
}

return expect(
testDestination.testAction('sendCustomBehavioralEvent', {
event,
settings,
useDefaultMappings: true,
mapping: mapping
})
).rejects.toThrowError(`EventName should begin with pe<hubId>_`)
})

test('should succeed when all fields are given', async () => {
const event = createTestEvent({
type: 'track',
event: 'pe22596207_test_event_http',
timestamp: '2023-07-05T08:28:35.216Z',
properties: {
email: '[email protected]',
utk: 'abverazffa===1314122f',
userId: '802',
city: 'city'
}
})

const expectedPayload = {
eventName: event.event,
occurredAt: event.timestamp as string,
utk: event.properties?.utk,
email: event.properties?.email,
objectId: event.properties?.userId,
properties: {
hs_city: event.properties?.city
}
}

const mapping = {
eventName: {
'@path': '$.event'
},
utk: {
'@path': '$.properties.utk'
},
objectId: {
'@path': '$.properties.userId'
},
properties: {
hs_city: {
'@path': '$.properties.city'
}
}
}

nock(HUBSPOT_BASE_URL).post('/events/v3/send', expectedPayload).reply(204, {})

const responses = await testDestination.testAction('sendCustomBehavioralEvent', {
event,
useDefaultMappings: true,
mapping: mapping
})
expect(responses.length).toBe(1)
expect(responses[0].status).toBe(204)
expect(responses[0].options.json).toMatchSnapshot()
})

test('should fail when email, utk and objectId is not provided', async () => {
const event = createTestEvent({
type: 'track',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,29 @@ describe(`Testing snapshot for ${destinationSlug}'s ${actionSlug} destination ac
properties: { ...eventData, email: '[email protected]' }
})

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

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

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

expect(request.headers).toMatchSnapshot()
expect(request.headers).toMatchSnapshot()
} catch (e) {
expect(e).toMatchSnapshot()
}
})

it('all fields', async () => {
Expand All @@ -46,15 +56,28 @@ describe(`Testing snapshot for ${destinationSlug}'s ${actionSlug} destination ac
properties: eventData
})

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

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

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

const request = responses[0].request
const json = await request.json()
expect(json).toMatchSnapshot()
expect(request.headers).toMatchSnapshot()
} catch (e) {
expect(e).toMatchSnapshot()
}
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const action: ActionDefinition<Settings, Payload> = {
defaultObjectUI: 'keyvalue:only'
}
},
perform: (request, { payload }) => {
perform: (request, { payload, settings }) => {
const event: CustomBehavioralEvent = {
eventName: payload.eventName,
occurredAt: payload.occurredAt,
Expand All @@ -77,6 +77,16 @@ const action: ActionDefinition<Settings, Payload> = {
properties: flattenObject(payload.properties)
}

const hubId = settings?.portalId
const regExp = /^pe\d+_.*/

if (!hubId && !regExp.exec(payload?.eventName)) {
throw new PayloadValidationError(`EventName should begin with pe<hubId>_`)
}
if (hubId && !payload?.eventName.startsWith(`pe${hubId}_`)) {
throw new PayloadValidationError(`EventName should begin with pe${hubId}_`)
}

if (!payload.utk && !payload.email && !payload.objectId) {
throw new PayloadValidationError(`One of the following parameters: email, user token, or objectId is required`)
}
Expand Down

0 comments on commit dee4caf

Please sign in to comment.