Skip to content

Commit

Permalink
Added error in case email, utk and object is not provided (#1374)
Browse files Browse the repository at this point in the history
* Added error in case email, utk and object is not provided

* Added unit test case for the payload validation error condition

* Added snapshot for the condition when one of email, utk or objectId provided

* Updated the test case that was failing

---------

Co-authored-by: Harsh Vardhan <[email protected]>
  • Loading branch information
hvardhan-unth and Harsh Vardhan authored Jul 6, 2023
1 parent f980f43 commit e05e642
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ Object {
}
`;

exports[`Testing snapshot for actions-hubspot-cloud destination: sendCustomBehavioralEvent action - required fields 1`] = `
Object {
"eventName": "NTxWtuPi(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: upsertCompany action - all fields 1`] = `
Object {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`HubSpot.sendCustomBehavioralEvent One of email, utk or objectId is provided 1`] = `
Object {
"email": undefined,
"eventName": "pe22596207_test_event_http",
"objectId": undefined,
"occurredAt": "2023-07-04T10:25:44.778Z",
"properties": Object {
"hs_city": "city",
},
"utk": "abverazffa===1314122f",
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,104 @@ describe('HubSpot.sendCustomBehavioralEvent', () => {
}
})
})

test('should fail when email, utk and objectId is not provided', async () => {
const event = createTestEvent({
type: 'track',
event: 'pe22596207_test_event_http',
properties: {
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(400, {})

return expect(
testDestination.testAction('sendCustomBehavioralEvent', {
event,
useDefaultMappings: true,
mapping: mapping
})
).rejects.toThrowError('One of the following parameters: email, user token, or objectId is required')
})

it('One of email, utk or objectId is provided', async () => {
const event = createTestEvent({
type: 'track',
event: 'pe22596207_test_event_http',
properties: {
utk: 'abverazffa===1314122f',
city: 'city'
},
timestamp: '2023-07-04T10:25:44.778Z'
})

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()
})
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ActionDefinition } from '@segment/actions-core'
import { ActionDefinition, PayloadValidationError } from '@segment/actions-core'
import type { Settings } from '../generated-types'
import { HUBSPOT_BASE_URL } from '../properties'
import type { Payload } from './generated-types'
Expand Down Expand Up @@ -76,6 +76,11 @@ const action: ActionDefinition<Settings, Payload> = {
objectId: payload.objectId,
properties: flattenObject(payload.properties)
}

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

return request(`${HUBSPOT_BASE_URL}/events/v3/send`, {
method: 'post',
json: event
Expand Down

0 comments on commit e05e642

Please sign in to comment.