Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[STRATCONN-3468] | Introduce 'Amazon AMC' Audience Destination #2036

Merged
merged 23 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Amazon-Ads (actions) createAudience creates an audience 1`] = `
Object {
"externalId": "1234549079612618",
}
`;

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
import nock from 'nock'
import { createTestIntegration } from '@segment/actions-core'
import Definition from '../index'
import { HTTPError } from '@segment/actions-core/*'

const testDestination = createTestIntegration(Definition)

export const settings = {
region: 'https://advertising-api.amazon.com'
}

const validSettings = {
region: 'https://advertising-api.amazon.com',
oauth: {
access_token: 'valid token',
refresh_token: '123'
}
}
const audienceSettings = {
advertiserId: '1234567893456754321',
countryCode: 'US',
description: 'Test Audience Description',
externalAudienceId: 'external-audience-123456'
}
const createAudienceInputTemp = {
settings,
audienceSettings: audienceSettings,
audienceName: 'Test Audience'
}

const getAudienceInput = {
settings,
externalId: '1234549079612618'
}

describe('Amazon-Ads (actions)', () => {
describe('testAuthentication', () => {
it('should not throw an error if all the appropriate credentials are available', async () => {
nock(`${settings.region}`).get('/v2/profiles').reply(200, {})
Innovative-GauravKochar marked this conversation as resolved.
Show resolved Hide resolved
await expect(testDestination.testAuthentication(validSettings)).resolves.not.toThrowError()
})

it('should throw an error if the user has not completed the oauth flow', async () => {
await expect(testDestination.testAuthentication(settings)).rejects.toThrowError(
'Credentials are invalid: Please authenticate via Oauth before enabling the destination.'
)
})

it('should throw an error if the oauth token is invalid', async () => {
nock(`${settings.region}`).get('/v2/profiles').reply(401)
Innovative-GauravKochar marked this conversation as resolved.
Show resolved Hide resolved

await expect(testDestination.testAuthentication(validSettings)).rejects.toThrowError(
'Credentials are invalid: Invalid Amazon Oauth access token. Please reauthenticate to retrieve a valid access token before enabling the destination.'
)
})
})

describe('createAudience', () => {
it('should fail if no audience name is set', async () => {
const createAudienceInput = {
...createAudienceInputTemp,
audienceName: ''
}
await expect(testDestination.createAudience(createAudienceInput)).rejects.toThrowError(
'Missing audienceName Value'
)
})

it('should fail if advertiserId is missing in audienceSettings', async () => {
const createAudienceInput = {
...createAudienceInputTemp,
audienceSettings: {
...audienceSettings,
advertiserId: ''
}
}
await expect(testDestination.createAudience(createAudienceInput)).rejects.toThrowError(
'Missing advertiserId Value'
)
})
it('should fail if externalAudienceId is missing in audienceSettings', async () => {
const createAudienceInput = {
...createAudienceInputTemp,
audienceSettings: {
...audienceSettings,
externalAudienceId: ''
}
}
await expect(testDestination.createAudience(createAudienceInput)).rejects.toThrowError(
'Missing externalAudienceId Value'
)
})
it('should fail if countryCode is missing in audienceSettings', async () => {
const createAudienceInput = {
...createAudienceInputTemp,
audienceSettings: {
...audienceSettings,
countryCode: ''
}
}
await expect(testDestination.createAudience(createAudienceInput)).rejects.toThrowError(
'Missing countryCode Value'
)
})
it('should fail if description is missing in audienceSettings', async () => {
const createAudienceInput = {
...createAudienceInputTemp,
audienceSettings: {
...audienceSettings,
description: ''
}
}
await expect(testDestination.createAudience(createAudienceInput)).rejects.toThrowError(
'Missing description Value'
)
})
it('should fail if invalid currency is provided in audienceSettings', async () => {
const createAudienceInput = {
settings,
audienceName: 'Test Audience',
audienceSettings: {
...audienceSettings,
ttl: 12345678,
currency: 'INVALID',
cpmCents: 1234
}
}
await expect(testDestination.createAudience(createAudienceInput)).rejects.toThrowError('Invalid Currency Value')
})

it('should throw an HTTPError when the response is not ok', async () => {
nock(`${settings.region}`)
.post('/amc/audiences/metadata')
.matchHeader('content-type', 'application/vnd.amcaudiences.v1+json')
.reply(400)

await expect(testDestination.createAudience(createAudienceInputTemp)).rejects.toThrowError('Bad Request')
})

it('Should throw an error when invalid cpmCent is provided', async () => {
const createAudienceInput = {
settings,
audienceName: 'Test Audience',
audienceSettings: {
...audienceSettings,
ttl: 12345678,
currency: 'USD',
cpmCents: 'invalid cpm cents'
}
}

await expect(testDestination.createAudience(createAudienceInput)).rejects.toThrowError(
'CPM_CENTS:-String can not be converted to Number'
)
})

it('creates an audience', async () => {
nock(`${settings.region}`)
.post('/amc/audiences/metadata')
.matchHeader('content-type', 'application/vnd.amcaudiences.v1+json')
.reply(200, { audienceId: 1234549079612618, externalAudienceId: 'external-audience-123456' })

const createAudienceInput = {
settings,
audienceName: 'Test Audience',
audienceSettings: {
...audienceSettings,
ttl: 12345678,
currency: 'USD',
cpmCents: 1234
}
}

const r = await testDestination.createAudience(createAudienceInput)
expect(r).toMatchSnapshot()
expect(r).toEqual({
externalId: '1234549079612618'
})
})
})

describe('getAudience', () => {
const externalId = getAudienceInput.externalId
it('should succeed when with valid audienceId', async () => {
nock(`${settings.region}/amc/audiences/metadata`)
.get(`/${externalId}`)
.reply(200, {
advertiserId: 1234567893456754321,
audienceId: 1234549079612618,
countryCode: 'US',
description: 'Test Audience Description',
metadata: {
audienceFees: [],
audienceSize: { dspAudienceSize: -1, idResolutionCount: -1, receivedRecordSize: -1 },
externalAudienceId: 'external-audience-123456',
ttl: 34190000
},
name: 'Test Audience'
})
const r = await testDestination.getAudience(getAudienceInput)
expect(r).toEqual({
externalId: '1234549079612618'
})
})

it('should throw an HTTPError when the response is not ok', async () => {
nock(`${settings.region}/amc/audiences/metadata`)
.get(`/${externalId}`)
.reply(404, { message: 'audienceId not found' })

const audiencePromise = testDestination.getAudience(getAudienceInput)
await expect(audiencePromise).rejects.toThrow(HTTPError)
await expect(audiencePromise).rejects.toHaveProperty('response.statusText', 'Not Found')
await expect(audiencePromise).rejects.toHaveProperty('response.status', 404)
})

it('should throw an IntegrationError when the audienceId is not provided', async () => {
getAudienceInput.externalId = ''
const audiencePromise = testDestination.getAudience(getAudienceInput)
await expect(audiencePromise).rejects.toThrow('Missing audienceId value')
})
})
})

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading