Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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,29 @@
import nock from 'nock'
import { createTestIntegration } from '@segment/actions-core'
import Definition from '../index'

const testDestination = createTestIntegration(Definition)

describe('Survicate Cloud Mode', () => {
beforeEach(() => {
nock.cleanAll()
})

describe('testAuthentication', () => {
const authData = {
apiKey: 'test_api_key'
}

it('should validate authentication inputs', async () => {
nock('https://integrations.survicate.com').get('/endpoint/segment/check').reply(200, {})

await expect(testDestination.testAuthentication(authData)).resolves.not.toThrowError()
})

it('should fail on authentication failure', async () => {
nock('https://integrations.survicate.com').get('/endpoint/segment/check').reply(401, {})

await expect(testDestination.testAuthentication(authData)).rejects.toThrowError()
})
})
})

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import nock from 'nock'
import { createTestEvent, createTestIntegration, SegmentEvent } from '@segment/actions-core'
import Definition from '../../index'
import { Settings } from '../../generated-types'

let testDestination = createTestIntegration(Definition)
const settings: Settings = {
apiKey: 'test_api_key'
}

const payload = {
type: 'group' as const,
userId: 'user123',
anonymousId: 'anon123',
groupId: 'group123',
traits: {
name: 'Test Group',
industry: 'Technology',
size: 100
},
timestamp: '2023-10-01T00:00:00Z'
} as Partial<SegmentEvent>

const mapping = {
userId: { '@path': '$.userId' },
anonymousId: { '@path': '$.anonymousId' },
groupId: { '@path': '$.groupId' },
traits: { '@path': '$.traits' },
timestamp: { '@path': '$.timestamp' }
}

beforeEach(() => {
testDestination = createTestIntegration(Definition)
nock.cleanAll()
})

describe('Survicate Cloud Mode - identifyGroup', () => {
it('should send identify group payload to Survicate', async () => {
const event = createTestEvent(payload)

const expectedJson = {
userId: 'user123',
anonymousId: 'anon123',
groupId: 'group123',
traits: {
group_name: 'Test Group',
group_industry: 'Technology',
group_size: 100
},
timestamp: '2023-10-01T00:00:00Z'
}

nock('https://integrations.survicate.com').post('/endpoint/segment/group', expectedJson).reply(200, {})

const response = await testDestination.testAction('identifyGroup', {
event,
settings,
useDefaultMappings: true,
mapping
})

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

it('should throw error when userId is not provided', async () => {
const event = createTestEvent({
...payload,
userId: undefined
})

await expect(
testDestination.testAction('identifyGroup', {
event,
settings,
useDefaultMappings: true,
mapping
})
).rejects.toThrowError()
})

it('should throw error when groupId is missing', async () => {
const event = createTestEvent({
...payload,
groupId: undefined
})

await expect(
testDestination.testAction('identifyGroup', {
event,
settings,
useDefaultMappings: true,
mapping
})
).rejects.toThrowError()
})

it('should throw error when traits are missing', async () => {
const event = createTestEvent({
...payload,
traits: undefined
})

await expect(
testDestination.testAction('identifyGroup', {
event,
settings,
useDefaultMappings: true,
mapping
})
).rejects.toThrowError()
})
})

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { PayloadValidationError, ActionDefinition } from '@segment/actions-core'
import { Settings } from '../generated-types'
import { Payload } from './generated-types'

const action: ActionDefinition<Settings, Payload> = {
title: 'Identify Group',
description: 'Send group traits to Survicate',
defaultSubscription: 'type = "group"',
platform: 'cloud',
fields: {
userId: {
type: 'string',
required: true,
description: "The user's id",
label: 'User ID',
default: {
'@path': '$.userId'
}
},
anonymousId: {
type: 'string',
required: false,
description: 'An anonymous id',
label: 'Anonymous ID',
default: {
'@path': '$.anonymousId'
}
},
groupId: {
type: 'string',
required: true,
description: 'The group id',
label: 'Group ID',
default: { '@path': '$.groupId' }
},
traits: {
type: 'object',
required: true,
description: 'The Segment traits to be forwarded to Survicate',
label: 'Group traits',
default: { '@path': '$.traits' }
},
timestamp: {
type: 'string',
required: true,
format: 'date-time',
description: 'The timestamp of the event.',
label: 'Timestamp',
default: { '@path': '$.timestamp' }
}
},
perform: (request, { payload }) => {
const { userId, anonymousId, groupId, traits, timestamp } = payload

if (!userId && !anonymousId) {
throw new PayloadValidationError("'User ID' or 'Anonymous ID' is required")
}

return request(`https://integrations.survicate.com/endpoint/segment/group`, {
method: 'post',
json: {
...(userId ? { userId } : {}),
...(anonymousId ? { anonymousId } : {}),
groupId,
traits: Object.fromEntries(Object.entries(traits).map(([key, value]) => [`group_${key}`, value])),
timestamp
}
})
}
}

export default action
Loading
Loading