-
Notifications
You must be signed in to change notification settings - Fork 254
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1da8912
commit f3d2d22
Showing
4 changed files
with
236 additions
and
81 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -2,58 +2,177 @@ import nock from 'nock' | |
import { createTestEvent, createTestIntegration } from '@segment/actions-core' | ||
import Destination from '../../index' | ||
|
||
const testDestination = createTestIntegration(Destination) | ||
let testDestination = createTestIntegration(Destination) | ||
|
||
describe('OptimizelyAdvancedAudienceTargeting.syncAudience', () => { | ||
const trackEvent = createTestEvent({ | ||
context: { | ||
personas: { | ||
computation_class: 'audience', | ||
computation_key: 'some_audience_name', | ||
computation_id: 'abc' | ||
beforeEach((done) => { | ||
testDestination = createTestIntegration(Destination) | ||
nock.cleanAll() | ||
done() | ||
}) | ||
|
||
describe('single request', () => { | ||
const trackEvent = createTestEvent({ | ||
context: { | ||
personas: { | ||
computation_class: 'audience', | ||
computation_key: 'some_audience_name', | ||
computation_id: 'abc' | ||
}, | ||
traits: { | ||
email: '[email protected]' | ||
} | ||
}, | ||
traits: { | ||
email: '[email protected]' | ||
email: '[email protected]', | ||
some_audience_name: true | ||
} | ||
}, | ||
traits: { | ||
email: '[email protected]', | ||
some_audience_name: true | ||
} | ||
}) | ||
const identifyEvent = createTestEvent({ | ||
context: { | ||
personas: { | ||
computation_class: 'audience', | ||
computation_key: 'some_audience_name', | ||
computation_id: 'abc' | ||
}) | ||
const identifyEvent = createTestEvent({ | ||
context: { | ||
personas: { | ||
computation_class: 'audience', | ||
computation_key: 'some_audience_name', | ||
computation_id: 'abc' | ||
} | ||
}, | ||
properties: { | ||
audience_key: 'some_audience_name', | ||
some_audience_name: true | ||
} | ||
} | ||
}) | ||
}) | ||
|
||
it('should handle props with track', async () => { | ||
nock('https://function.zaius.app/twilio_segment').post('/sync_audience').reply(201) | ||
it('should handle traits with track', async () => { | ||
nock('https://function.zaius.app/twilio_segment').post('/batch_sync_audience').reply(201) | ||
|
||
await expect( | ||
testDestination.testAction('syncAudience', { | ||
event: trackEvent, | ||
useDefaultMappings: true | ||
}) | ||
).resolves.not.toThrowError() | ||
}) | ||
await expect( | ||
testDestination.testAction('syncAudience', { | ||
event: trackEvent, | ||
useDefaultMappings: true | ||
}) | ||
).resolves.not.toThrowError() | ||
}) | ||
|
||
it('should handle props with track', async () => { | ||
nock('https://function.zaius.app/twilio_segment').post('/batch_sync_audience').reply(201) | ||
|
||
await expect( | ||
testDestination.testAction('syncAudience', { | ||
event: identifyEvent, | ||
useDefaultMappings: true | ||
}) | ||
).resolves.not.toThrowError() | ||
}) | ||
|
||
it('should handle errors response', async () => { | ||
nock('https://function.zaius.app/twilio_segment').post('/batch_sync_audience').reply(400) | ||
|
||
await expect( | ||
testDestination.testAction('syncAudience', { | ||
event: identifyEvent, | ||
useDefaultMappings: true | ||
}) | ||
).rejects.toThrowError() | ||
}) | ||
|
||
it('should handle traits with track', async () => { | ||
nock('https://function.zaius.app/twilio_segment').post('/sync_audience').reply(201) | ||
it('should handle 401 response', async () => { | ||
nock('https://function.zaius.app/twilio_segment').post('/batch_sync_audience').reply(401) | ||
|
||
await expect( | ||
testDestination.testAction('syncAudience', { | ||
event: identifyEvent, | ||
useDefaultMappings: true | ||
await expect( | ||
testDestination.testAction('syncAudience', { | ||
event: identifyEvent, | ||
useDefaultMappings: true | ||
}) | ||
).rejects.toThrowError() | ||
}) | ||
}) | ||
|
||
describe('batch request', () => { | ||
const trackEvents = [ | ||
createTestEvent({ | ||
traits: { | ||
email: '[email protected]', | ||
some_audience_name: true | ||
} | ||
}), | ||
createTestEvent({ | ||
traits: { | ||
email: '[email protected]', | ||
some_audience_name: true | ||
} | ||
}) | ||
] | ||
const identifyEvents = [ | ||
createTestEvent({ | ||
context: { | ||
personas: { | ||
computation_class: 'audience', | ||
computation_key: 'some_audience_name', | ||
computation_id: 'abc' | ||
} | ||
}, | ||
properties: { | ||
audience_key: 'some_audience_name', | ||
some_audience_name: true | ||
} | ||
}), | ||
createTestEvent({ | ||
context: { | ||
personas: { | ||
computation_class: 'audience', | ||
computation_key: 'some_audience_name1', | ||
computation_id: 'abc1' | ||
} | ||
}, | ||
properties: { | ||
audience_key: 'some_audience_name1', | ||
some_audience_name: true | ||
} | ||
}) | ||
).resolves.not.toThrowError() | ||
] | ||
|
||
it('should handle traits with track', async () => { | ||
nock('https://function.zaius.app/twilio_segment').post('/batch_sync_audience').reply(201) | ||
|
||
await expect( | ||
testDestination.testBatchAction('syncAudience', { | ||
events: trackEvents, | ||
useDefaultMappings: true | ||
}) | ||
).resolves.not.toThrowError() | ||
}) | ||
|
||
it('should handle props with track', async () => { | ||
nock('https://function.zaius.app/twilio_segment').post('/batch_sync_audience').reply(201) | ||
|
||
await expect( | ||
testDestination.testBatchAction('syncAudience', { | ||
events: identifyEvents, | ||
useDefaultMappings: true | ||
}) | ||
).resolves.not.toThrowError() | ||
}) | ||
|
||
it('should handle errors response', async () => { | ||
nock('https://function.zaius.app/twilio_segment').post('/batch_sync_audience').reply(400) | ||
|
||
await expect( | ||
testDestination.testBatchAction('syncAudience', { | ||
events: identifyEvents, | ||
useDefaultMappings: true | ||
}) | ||
).rejects.toThrowError() | ||
}) | ||
|
||
it('should handle 401 response', async () => { | ||
nock('https://function.zaius.app/twilio_segment').post('/batch_sync_audience').reply(401) | ||
|
||
await expect( | ||
testDestination.testBatchAction('syncAudience', { | ||
events: identifyEvents, | ||
useDefaultMappings: true | ||
}) | ||
).rejects.toThrowError() | ||
}) | ||
}) | ||
}) |
8 changes: 8 additions & 0 deletions
8
...s/src/destinations/optimizely-advanced-audience-targeting/syncAudience/generated-types.ts
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
50 changes: 50 additions & 0 deletions
50
...src/destinations/optimizely-advanced-audience-targeting/syncAudience/optimizely-client.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,50 @@ | ||
import type { Settings } from '../generated-types' | ||
import type { Payload } from './generated-types' | ||
import { getHost } from '../utils' | ||
import { RequestClient } from '@segment/actions-core' | ||
|
||
interface PropsOrTraits { | ||
properties?: Record<string, boolean> | ||
traits?: Record<string, boolean> | ||
} | ||
|
||
export interface Data { | ||
payload: Payload | Array<Payload> | ||
settings: Settings | ||
rawData: PropsOrTraits | Array<PropsOrTraits> | ||
} | ||
|
||
export class OptimizelyClient { | ||
request: RequestClient | ||
payloads: Payload[] | ||
settings: Settings | ||
propsOrTraits: PropsOrTraits[] | ||
|
||
constructor(request: RequestClient, data: Data) { | ||
this.request = request | ||
this.settings = data.settings | ||
this.payloads = Array.isArray(data.payload) ? data.payload : [data.payload] | ||
this.propsOrTraits = Array.isArray(data.rawData) ? data.rawData : [data.rawData] | ||
} | ||
|
||
async send() { | ||
const host = getHost(this.settings) | ||
|
||
const requestBody = this.payloads.map((payload, index) => { | ||
return { | ||
audienceId: payload.segment_computation_id, | ||
audienceName: payload.custom_audience_name, | ||
timestamp: payload.timestamp, | ||
subscription: | ||
this.propsOrTraits[index].properties?.[payload.custom_audience_name] ?? | ||
this.propsOrTraits[index].traits?.[payload.custom_audience_name], | ||
userId: payload.optimizelyUserId | ||
} | ||
}) | ||
|
||
return this.request(`${host}/batch_sync_audience`, { | ||
method: 'post', | ||
json: requestBody | ||
}) | ||
} | ||
} |