-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Braze Cloud] Adds sync mode to new createAlias V2 action (#2385)
* Braze syncmode for createAlias action * Fixes build; generates types * Splits off V2 action into it's own directory * Updates snapshot for v1 action unit tests * Removes unneeded git diffs * Updates unit test with better test names and a check for the expected error message when testing if syncMode != add * Updates generated snapshot with new unit test names
- Loading branch information
Showing
5 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
...actions/src/destinations/braze/createAlias2/__tests__/__snapshots__/snapshot.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Testing snapshot for Braze's createAlias2 destination V2 action: snapshot with all possible fields 1`] = ` | ||
Object { | ||
"user_aliases": Array [ | ||
Object { | ||
"alias_label": "gYiVmU)MW(1BFgh^@[", | ||
"alias_name": "gYiVmU)MW(1BFgh^@[", | ||
"external_id": "gYiVmU)MW(1BFgh^@[", | ||
}, | ||
], | ||
} | ||
`; | ||
|
||
exports[`Testing snapshot for Braze's createAlias2 destination V2 action: snapshot with only required fields 1`] = ` | ||
Object { | ||
"user_aliases": Array [ | ||
Object { | ||
"alias_label": "test_label", | ||
"alias_name": "test_name", | ||
}, | ||
], | ||
} | ||
`; |
93 changes: 93 additions & 0 deletions
93
packages/destination-actions/src/destinations/braze/createAlias2/__tests__/snapshot.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
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 = 'createAlias2' | ||
const destinationSlug = 'Braze' | ||
const seedName = `${destinationSlug}#${actionSlug}` | ||
|
||
describe(`Testing snapshot for ${destinationSlug}'s ${actionSlug} destination V2 action:`, () => { | ||
it('fails if sync mode is not add', async () => { | ||
const action = destination.actions[actionSlug] | ||
const [eventData, settingsData] = generateTestData(seedName, destination, action, false) | ||
|
||
const event = createTestEvent({ | ||
properties: eventData | ||
}) | ||
|
||
await expect( | ||
testDestination.testAction(actionSlug, { | ||
event: event, | ||
mapping: { ...event.properties, __segment_internal_sync_mode: 'upsert' }, | ||
settings: settingsData, | ||
auth: undefined | ||
}) | ||
).rejects.toThrowError('syncMode must be "add"') | ||
}) | ||
|
||
it('snapshot with only 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: { alias_name: 'test_name', alias_label: 'test_label', __segment_internal_sync_mode: 'add' }, | ||
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('snapshot with all possible 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, __segment_internal_sync_mode: 'add' }, | ||
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() | ||
} | ||
}) | ||
}) |
16 changes: 16 additions & 0 deletions
16
packages/destination-actions/src/destinations/braze/createAlias2/generated-types.ts
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
56 changes: 56 additions & 0 deletions
56
packages/destination-actions/src/destinations/braze/createAlias2/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import type { ActionDefinition } from '@segment/actions-core' | ||
import { IntegrationError } from '@segment/actions-core' | ||
import type { Settings } from '../generated-types' | ||
import type { Payload } from './generated-types' | ||
|
||
const action: ActionDefinition<Settings, Payload> = { | ||
title: 'Create Alias V2', | ||
description: 'Create new user aliases for existing identified users, or to create new unidentified users.', | ||
defaultSubscription: 'event = "Create Alias"', | ||
fields: { | ||
external_id: { | ||
label: 'External ID', | ||
description: 'The external ID of the user to create an alias for.', | ||
type: 'string', | ||
allowNull: true | ||
}, | ||
alias_name: { | ||
label: 'Alias Name', | ||
description: 'The alias identifier', | ||
type: 'string', | ||
required: true | ||
}, | ||
alias_label: { | ||
label: 'Alias Label', | ||
description: 'A label indicating the type of alias', | ||
type: 'string', | ||
required: true | ||
} | ||
}, | ||
syncMode: { | ||
description: 'Define how the records from your destination will be synced.', | ||
label: 'How to add Aliases', | ||
default: 'add', | ||
choices: [{ label: 'Create Alias', value: 'add' }] | ||
}, | ||
perform: (request, { settings, payload, syncMode }) => { | ||
if (syncMode === 'add') { | ||
return request(`${settings.endpoint}/users/alias/new`, { | ||
method: 'post', | ||
json: { | ||
user_aliases: [ | ||
{ | ||
external_id: payload.external_id ?? undefined, | ||
alias_name: payload.alias_name, | ||
alias_label: payload.alias_label | ||
} | ||
] | ||
} | ||
}) | ||
} | ||
|
||
throw new IntegrationError('syncMode must be "add"', 'Invalid syncMode', 400) | ||
} | ||
} | ||
|
||
export default action |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters