Skip to content

Commit b69ef6d

Browse files
flag tests
1 parent 8d5145c commit b69ef6d

File tree

2 files changed

+174
-15
lines changed

2 files changed

+174
-15
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import nock from 'nock'
2+
import { createTestEvent, createTestIntegration } from '@segment/actions-core'
3+
import Destination from '../../index'
4+
5+
const testDestination = createTestIntegration(Destination)
6+
7+
const event = createTestEvent({
8+
context: {
9+
personas: {
10+
audience_settings: {
11+
advertiserId: '585806696618602999',
12+
countryCode: 'US',
13+
description: 'Test Event',
14+
externalAudienceId: '65241452'
15+
},
16+
computation_class: 'audience',
17+
computation_id: 'aud_2g5VilffxpBYqelWk4K0yBzAuFl',
18+
computation_key: 'amazon_ads_audience_6_may_24',
19+
external_audience_id: '379909525712777677',
20+
namespace: 'spa_rHVbZsJXToWAwcmbgpfo36',
21+
space_id: 'spa_rHVbZsJXToWAwcmbgpfo36'
22+
},
23+
traits: {
24+
email: 'test@twilio.com'
25+
}
26+
},
27+
event: 'Audience Entered',
28+
messageId: 'personas_2g5WGNhZtTET4DhSeILpE6muHnH',
29+
properties: {
30+
amazon_ads_audience_6_may_24: true,
31+
audience_key: 'amazon_ads_audience_6_may_24',
32+
externalId: '379909525712777677'
33+
},
34+
receivedAt: '2024-05-06T09:30:38.650Z',
35+
timestamp: '2024-05-06T09:30:22.829Z',
36+
type: 'track',
37+
userId: 'test-kochar-01',
38+
writeKey: 'REDACTED'
39+
})
40+
41+
const settings = {
42+
region: 'https://advertising-api.amazon.com'
43+
}
44+
45+
const mapping = {
46+
email: { '@path': '$.properties.email' },
47+
event_name: { '@path': '$.event' },
48+
externalUserId: { '@path': '$.userId' },
49+
audienceId: { '@path': '$.context.personas.external_audience_id' },
50+
enable_batching: true
51+
}
52+
53+
// These tests cover behavior when the FLAG_CONSENT_REQUIRED feature flag is OFF (no `features` passed).
54+
// Delete this file when the flag is removed from the production code.
55+
describe('AmazonAds.syncAudiencesToDSP (flag off)', () => {
56+
beforeEach(() => {
57+
nock.cleanAll()
58+
jest.resetAllMocks()
59+
})
60+
afterEach(() => {
61+
jest.resetAllMocks()
62+
})
63+
64+
it('should not include userConsent in the record when flag is off', async () => {
65+
nock('https://advertising-api.amazon.com')
66+
.post('/amc/audiences/records')
67+
.matchHeader('content-type', 'application/vnd.amcaudiences.v1+json')
68+
.reply(202, { jobRequestId: '1155d3e3-b18c-4b2b-a3b2-26173cdaf770' })
69+
70+
const response = await testDestination.executeBatch('syncAudiencesToDSP', {
71+
events: [{ ...event, userId: 'test_kochar-02' }],
72+
settings,
73+
mapping
74+
})
75+
76+
expect(response.length).toBe(1)
77+
expect(response[0].status).toBe(202)
78+
expect(response[0]).not.toHaveProperty('sent.userConsent')
79+
})
80+
81+
it('should not throw a consent error for non-US country code when flag is off (single event)', async () => {
82+
nock('https://advertising-api.amazon.com')
83+
.post('/amc/audiences/records')
84+
.matchHeader('content-type', 'application/vnd.amcaudiences.v1+json')
85+
.reply(202, { jobRequestId: '1155d3e3-b18c-4b2b-a3b2-26173cdaf770' })
86+
87+
const deEvent = {
88+
...event,
89+
context: {
90+
...event.context,
91+
personas: {
92+
...event.context!.personas,
93+
audience_settings: {
94+
...event.context!.personas!.audience_settings,
95+
countryCode: 'DE'
96+
}
97+
}
98+
}
99+
}
100+
101+
const response = await testDestination.testAction('syncAudiencesToDSP', {
102+
event: deEvent,
103+
settings,
104+
useDefaultMappings: true
105+
})
106+
107+
expect(response[response.length - 1].status).toBe(202)
108+
})
109+
110+
it('should not produce a per-record consent error for non-US country code when flag is off (batch)', async () => {
111+
nock('https://advertising-api.amazon.com')
112+
.post('/amc/audiences/records')
113+
.matchHeader('content-type', 'application/vnd.amcaudiences.v1+json')
114+
.reply(202, { jobRequestId: '1155d3e3-b18c-4b2b-a3b2-26173cdaf770' })
115+
116+
const deEvent = {
117+
...event,
118+
userId: 'test_kochar-02',
119+
context: {
120+
...event.context,
121+
personas: {
122+
...event.context!.personas,
123+
audience_settings: {
124+
...event.context!.personas!.audience_settings,
125+
countryCode: 'DE'
126+
}
127+
}
128+
}
129+
}
130+
131+
const response = await testDestination.executeBatch('syncAudiencesToDSP', {
132+
events: [deEvent],
133+
settings,
134+
mapping
135+
})
136+
137+
expect(response.length).toBe(1)
138+
expect(response[0].status).toBe(202)
139+
expect(response[0]).not.toHaveProperty('errortype')
140+
})
141+
})

packages/destination-actions/src/destinations/amazon-amc/syncAudiencesToDSP/__tests__/index.test.ts

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { createTestEvent, createTestIntegration } from '@segment/actions-core'
33
import Destination from '../../index'
44
import { SegmentEvent } from '@segment/actions-core/*'
55
import { InvalidAuthenticationError } from '@segment/actions-core'
6+
import { FLAG_CONSENT_REQUIRED } from '../../utils'
67

78
const testDestination = createTestIntegration(Destination)
89
const event = createTestEvent({
@@ -111,6 +112,8 @@ const settings = {
111112
region: 'https://advertising-api.amazon.com'
112113
}
113114

115+
const features = { [FLAG_CONSENT_REQUIRED]: true }
116+
114117
describe('AmazonAds.syncAudiencesToDSP', () => {
115118
beforeEach(() => {
116119
nock.cleanAll()
@@ -128,7 +131,8 @@ describe('AmazonAds.syncAudiencesToDSP', () => {
128131
const response = await testDestination.testAction('syncAudiencesToDSP', {
129132
event,
130133
settings,
131-
useDefaultMappings: true
134+
useDefaultMappings: true,
135+
features
132136
})
133137

134138
expect(response.length).toBe(1)
@@ -159,7 +163,8 @@ describe('AmazonAds.syncAudiencesToDSP', () => {
159163
}
160164
},
161165
settings,
162-
useDefaultMappings: true
166+
useDefaultMappings: true,
167+
features
163168
})
164169

165170
expect(response.length).toBe(1)
@@ -190,7 +195,8 @@ describe('AmazonAds.syncAudiencesToDSP', () => {
190195
}
191196
},
192197
settings,
193-
useDefaultMappings: true
198+
useDefaultMappings: true,
199+
features
194200
})
195201

196202
expect(response.length).toBe(1)
@@ -226,7 +232,8 @@ describe('AmazonAds.syncAudiencesToDSP', () => {
226232
}
227233
},
228234
settings,
229-
useDefaultMappings: true
235+
useDefaultMappings: true,
236+
features
230237
})
231238

232239
expect(response.length).toBe(1)
@@ -250,7 +257,8 @@ describe('AmazonAds.syncAudiencesToDSP', () => {
250257
event: 'Audience Exited'
251258
},
252259
settings,
253-
useDefaultMappings: true
260+
useDefaultMappings: true,
261+
features
254262
})
255263

256264
expect(response.length).toBe(1)
@@ -269,7 +277,8 @@ describe('AmazonAds.syncAudiencesToDSP', () => {
269277
userId: 'invalid+1@user.com'
270278
},
271279
settings,
272-
useDefaultMappings: true
280+
useDefaultMappings: true,
281+
features
273282
})
274283
).rejects.toThrowError('externalUserId must satisfy regular expression pattern: [0-9a-zA-Z\\-\\_]{1,128}}')
275284
})
@@ -283,7 +292,8 @@ describe('AmazonAds.syncAudiencesToDSP', () => {
283292
const response = await testDestination.executeBatch('syncAudiencesToDSP', {
284293
events,
285294
settings,
286-
mapping
295+
mapping,
296+
features
287297
})
288298
expect(response.length).toBe(2)
289299
expect(response[0]).toEqual({
@@ -327,7 +337,8 @@ describe('AmazonAds.syncAudiencesToDSP', () => {
327337
const response = await testDestination.executeBatch('syncAudiencesToDSP', {
328338
events,
329339
settings,
330-
mapping
340+
mapping,
341+
features
331342
})
332343
expect(response.length).toBe(2)
333344
expect(response[0]).toEqual({
@@ -395,7 +406,8 @@ describe('AmazonAds.syncAudiencesToDSP', () => {
395406
const response = await testDestination.executeBatch('syncAudiencesToDSP', {
396407
events,
397408
settings,
398-
mapping
409+
mapping,
410+
features
399411
})
400412

401413
expect(response.length).toBe(2)
@@ -422,7 +434,8 @@ describe('AmazonAds.syncAudiencesToDSP', () => {
422434
testDestination.executeBatch('syncAudiencesToDSP', {
423435
events,
424436
settings,
425-
mapping
437+
mapping,
438+
features
426439
})
427440
).rejects.toThrowError(new InvalidAuthenticationError('Unauthorized'))
428441
})
@@ -458,7 +471,8 @@ describe('AmazonAds.syncAudiencesToDSP', () => {
458471
amznAdStorage: { '@path': '$.properties.amznAdStorage' },
459472
amznUserData: { '@path': '$.properties.amznUserData' }
460473
}
461-
}
474+
},
475+
features
462476
})
463477

464478
expect(response.length).toBe(1)
@@ -487,7 +501,8 @@ describe('AmazonAds.syncAudiencesToDSP', () => {
487501
const response = await testDestination.executeBatch('syncAudiencesToDSP', {
488502
events: [{ ...event, userId: 'test_kochar-02' }],
489503
settings,
490-
mapping
504+
mapping,
505+
features
491506
})
492507

493508
expect(response.length).toBe(1)
@@ -523,7 +538,8 @@ describe('AmazonAds.syncAudiencesToDSP', () => {
523538
testDestination.testAction('syncAudiencesToDSP', {
524539
event: deEvent,
525540
settings,
526-
useDefaultMappings: true
541+
useDefaultMappings: true,
542+
features
527543
})
528544
).rejects.toThrowError('Non US country codes must provide valid consent data.')
529545
})
@@ -546,7 +562,8 @@ describe('AmazonAds.syncAudiencesToDSP', () => {
546562
const response = await testDestination.executeBatch('syncAudiencesToDSP', {
547563
events: [deEvent],
548564
settings,
549-
mapping
565+
mapping,
566+
features
550567
})
551568

552569
expect(response.length).toBe(1)
@@ -589,7 +606,8 @@ describe('AmazonAds.syncAudiencesToDSP', () => {
589606
consent: {
590607
ipAddress: { '@path': '$.context.ip' }
591608
}
592-
}
609+
},
610+
features
593611
})
594612

595613
expect(response.length).toBe(1)

0 commit comments

Comments
 (0)