2022-11-30 13:49:17 +01:00
|
|
|
import { createServer } from 'http'
|
2023-05-04 13:25:56 +02:00
|
|
|
import { DateTime } from 'luxon'
|
2022-11-30 13:49:17 +01:00
|
|
|
|
|
|
|
import { UUIDT } from '../src/utils/utils'
|
2024-05-29 02:29:24 +02:00
|
|
|
import {
|
|
|
|
capture,
|
|
|
|
createAction,
|
2024-07-22 22:21:46 +02:00
|
|
|
createGroup,
|
|
|
|
createGroupType,
|
2024-05-29 02:29:24 +02:00
|
|
|
createHook,
|
|
|
|
createOrganizationRaw,
|
|
|
|
createTeam,
|
|
|
|
createUser,
|
|
|
|
reloadAction,
|
|
|
|
} from './api'
|
2022-11-30 13:49:17 +01:00
|
|
|
|
2023-05-04 13:25:56 +02:00
|
|
|
test.concurrent(`webhooks: fires slack webhook`, async () => {
|
|
|
|
// Create an action with post_to_slack enabled.
|
|
|
|
// NOTE: I'm not 100% sure how this works i.e. what all the step
|
|
|
|
// configuration means so there's probably a more succinct way to do
|
|
|
|
// this.
|
|
|
|
let webHookCalledWith: any
|
|
|
|
const server = createServer((req, res) => {
|
|
|
|
let body = ''
|
|
|
|
req.on('data', (chunk) => {
|
|
|
|
body += chunk
|
|
|
|
})
|
|
|
|
req.on('end', () => {
|
|
|
|
webHookCalledWith = JSON.parse(body)
|
|
|
|
res.writeHead(200, { 'Content-Type': 'text/plain' })
|
|
|
|
res.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
try {
|
|
|
|
await new Promise((resolve) => {
|
|
|
|
server.on('listening', resolve)
|
|
|
|
server.listen()
|
|
|
|
})
|
|
|
|
|
|
|
|
const distinctId = new UUIDT().toString()
|
2022-11-30 13:49:17 +01:00
|
|
|
|
2024-07-22 22:21:46 +02:00
|
|
|
const organizationId = await createOrganizationRaw({
|
|
|
|
available_product_features: `array ['{ "key": "group_analytics", "name": "group_analytics" }'::jsonb]`,
|
|
|
|
})
|
2023-05-04 13:25:56 +02:00
|
|
|
const teamId = await createTeam(organizationId, `http://localhost:${server.address()?.port}`)
|
|
|
|
const user = await createUser(teamId, new UUIDT().toString())
|
2024-11-01 00:12:28 +01:00
|
|
|
await createGroupType(teamId, teamId, 0, 'organization')
|
2024-07-22 22:21:46 +02:00
|
|
|
await createGroup(teamId, 0, 'TestWebhookOrg', { name: 'test-webhooks' })
|
2024-05-08 09:46:52 +02:00
|
|
|
const action = await createAction({
|
|
|
|
team_id: teamId,
|
|
|
|
name: 'slack',
|
|
|
|
description: 'slack',
|
|
|
|
created_at: new Date().toISOString(),
|
|
|
|
updated_at: new Date().toISOString(),
|
|
|
|
deleted: false,
|
|
|
|
post_to_slack: true,
|
|
|
|
slack_message_format:
|
2024-07-22 22:21:46 +02:00
|
|
|
'[event.name] with [event.properties.name] was triggered by [person.properties.email] of [groups.organization.properties.name]',
|
2024-05-08 09:46:52 +02:00
|
|
|
created_by_id: user.id,
|
|
|
|
is_calculating: false,
|
|
|
|
last_calculated_at: new Date().toISOString(),
|
|
|
|
bytecode: null,
|
|
|
|
bytecode_error: null,
|
|
|
|
steps_json: [
|
2023-05-04 13:25:56 +02:00
|
|
|
{
|
|
|
|
tag_name: 'div',
|
|
|
|
text: 'text',
|
|
|
|
href: null,
|
|
|
|
url: 'http://localhost:8000',
|
|
|
|
url_matching: null,
|
|
|
|
event: '$autocapture',
|
|
|
|
properties: null,
|
|
|
|
selector: null,
|
2024-05-08 09:46:52 +02:00
|
|
|
href_matching: null,
|
|
|
|
text_matching: null,
|
2023-05-04 13:25:56 +02:00
|
|
|
},
|
2024-05-08 09:46:52 +02:00
|
|
|
],
|
|
|
|
})
|
2023-05-04 13:25:56 +02:00
|
|
|
|
|
|
|
await reloadAction(teamId, action.id)
|
|
|
|
|
|
|
|
await capture({
|
|
|
|
teamId,
|
|
|
|
distinctId,
|
|
|
|
uuid: new UUIDT().toString(),
|
|
|
|
event: '$autocapture',
|
|
|
|
properties: {
|
|
|
|
name: 'hehe',
|
|
|
|
uuid: new UUIDT().toString(),
|
|
|
|
$current_url: 'http://localhost:8000',
|
|
|
|
$elements: [{ tag_name: 'div', nth_child: 1, nth_of_type: 2, $el_text: 'text' }],
|
|
|
|
$set: { email: 't@t.com' },
|
2024-07-22 22:21:46 +02:00
|
|
|
$groups: { organization: 'TestWebhookOrg' },
|
2023-05-04 13:25:56 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
for (const _ in Array.from(Array(20).keys())) {
|
|
|
|
if (webHookCalledWith) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 1000))
|
|
|
|
}
|
|
|
|
|
2024-07-22 22:21:46 +02:00
|
|
|
expect(webHookCalledWith).toEqual({ text: `$autocapture with hehe was triggered by t@t.com of test-webhooks` })
|
2023-05-04 13:25:56 +02:00
|
|
|
} finally {
|
|
|
|
server.close()
|
|
|
|
}
|
2022-11-30 13:49:17 +01:00
|
|
|
})
|
|
|
|
|
2023-05-04 13:25:56 +02:00
|
|
|
test.concurrent(`webhooks: fires zapier REST webhook`, async () => {
|
2022-11-30 13:49:17 +01:00
|
|
|
// Create an action with post_to_slack enabled.
|
|
|
|
// NOTE: I'm not 100% sure how this works i.e. what all the step
|
|
|
|
// configuration means so there's probably a more succinct way to do
|
|
|
|
// this.
|
|
|
|
let webHookCalledWith: any
|
|
|
|
const server = createServer((req, res) => {
|
|
|
|
let body = ''
|
|
|
|
req.on('data', (chunk) => {
|
|
|
|
body += chunk
|
|
|
|
})
|
|
|
|
req.on('end', () => {
|
|
|
|
webHookCalledWith = JSON.parse(body)
|
|
|
|
res.writeHead(200, { 'Content-Type': 'text/plain' })
|
|
|
|
res.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
try {
|
2023-01-05 20:55:46 +01:00
|
|
|
await new Promise((resolve) => {
|
|
|
|
server.on('listening', resolve)
|
|
|
|
server.listen()
|
|
|
|
})
|
2022-11-30 13:49:17 +01:00
|
|
|
|
|
|
|
const distinctId = new UUIDT().toString()
|
2023-05-04 13:25:56 +02:00
|
|
|
const ts = new Date()
|
2022-11-30 13:49:17 +01:00
|
|
|
|
2024-05-29 02:29:24 +02:00
|
|
|
const organizationId = await createOrganizationRaw({
|
|
|
|
available_product_features: `array ['{ "key": "zapier", "name": "zapier" }'::jsonb]`,
|
|
|
|
})
|
|
|
|
|
2023-03-01 13:03:54 +01:00
|
|
|
const teamId = await createTeam(organizationId, `http://localhost:${server.address()?.port}`)
|
|
|
|
const user = await createUser(teamId, new UUIDT().toString())
|
2024-05-08 09:46:52 +02:00
|
|
|
const action = await createAction({
|
|
|
|
team_id: teamId,
|
|
|
|
name: 'zapier',
|
|
|
|
description: 'zapier',
|
|
|
|
created_at: new Date().toISOString(),
|
|
|
|
updated_at: new Date().toISOString(),
|
|
|
|
deleted: false,
|
|
|
|
post_to_slack: false,
|
|
|
|
slack_message_format:
|
|
|
|
'[event.name] with [event.properties.name] was triggered by [person.properties.email]',
|
|
|
|
created_by_id: user.id,
|
|
|
|
is_calculating: false,
|
|
|
|
last_calculated_at: new Date().toISOString(),
|
|
|
|
bytecode: null,
|
|
|
|
bytecode_error: null,
|
|
|
|
steps_json: [
|
2022-11-30 13:49:17 +01:00
|
|
|
{
|
|
|
|
tag_name: 'div',
|
|
|
|
text: 'text',
|
|
|
|
href: null,
|
|
|
|
url: 'http://localhost:8000',
|
|
|
|
url_matching: null,
|
|
|
|
event: '$autocapture',
|
|
|
|
properties: null,
|
|
|
|
selector: null,
|
2024-05-08 09:46:52 +02:00
|
|
|
text_matching: null,
|
|
|
|
href_matching: null,
|
2022-11-30 13:49:17 +01:00
|
|
|
},
|
2024-05-08 09:46:52 +02:00
|
|
|
],
|
|
|
|
})
|
2023-05-04 13:25:56 +02:00
|
|
|
await createHook(teamId, user.id, action.id, `http://localhost:${server.address()?.port}`)
|
2022-11-30 13:49:17 +01:00
|
|
|
|
2023-03-01 13:03:54 +01:00
|
|
|
await reloadAction(teamId, action.id)
|
2022-11-30 13:49:17 +01:00
|
|
|
|
2023-05-04 13:25:56 +02:00
|
|
|
const eventUuid = new UUIDT().toString()
|
2023-03-01 12:03:13 +01:00
|
|
|
await capture({
|
|
|
|
teamId,
|
|
|
|
distinctId,
|
2023-05-04 13:25:56 +02:00
|
|
|
uuid: eventUuid,
|
2023-03-01 12:03:13 +01:00
|
|
|
event: '$autocapture',
|
|
|
|
properties: {
|
|
|
|
name: 'hehe',
|
|
|
|
uuid: new UUIDT().toString(),
|
|
|
|
$current_url: 'http://localhost:8000',
|
|
|
|
$elements: [{ tag_name: 'div', nth_child: 1, nth_of_type: 2, $el_text: 'text' }],
|
2023-05-04 13:25:56 +02:00
|
|
|
$set: { email: 't@t.com' },
|
2023-03-01 12:03:13 +01:00
|
|
|
},
|
2023-05-04 13:25:56 +02:00
|
|
|
eventTime: ts,
|
|
|
|
sentAt: undefined,
|
2022-11-30 13:49:17 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
for (const _ in Array.from(Array(20).keys())) {
|
|
|
|
if (webHookCalledWith) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 1000))
|
|
|
|
}
|
|
|
|
|
2023-05-04 13:25:56 +02:00
|
|
|
const expectedData = {
|
|
|
|
distinctId: distinctId,
|
|
|
|
elementsList: [
|
|
|
|
{
|
|
|
|
$el_text: 'text',
|
|
|
|
attributes: {},
|
|
|
|
nth_child: 1,
|
|
|
|
nth_of_type: 2,
|
|
|
|
order: 0,
|
|
|
|
tag_name: 'div',
|
|
|
|
text: 'text',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
event: '$autocapture',
|
|
|
|
eventUuid: eventUuid,
|
|
|
|
person: {
|
|
|
|
created_at: new Date(DateTime.fromJSDate(ts).toFormat('yyyy-MM-dd HH:mm:ss')).toISOString(),
|
|
|
|
properties: {
|
|
|
|
$creator_event_uuid: eventUuid,
|
|
|
|
$initial_current_url: 'http://localhost:8000',
|
2023-09-14 12:57:54 +02:00
|
|
|
$current_url: 'http://localhost:8000',
|
2023-05-04 13:25:56 +02:00
|
|
|
email: 't@t.com',
|
|
|
|
},
|
|
|
|
uuid: expect.any(String),
|
|
|
|
},
|
|
|
|
properties: {
|
|
|
|
$current_url: 'http://localhost:8000',
|
|
|
|
$sent_at: expect.any(String),
|
2024-05-06 10:47:24 +02:00
|
|
|
$elements_chain: 'div:nth-child="1"nth-of-type="2"text="text"',
|
2023-05-04 13:25:56 +02:00
|
|
|
$set: {
|
|
|
|
email: 't@t.com',
|
2023-09-14 12:57:54 +02:00
|
|
|
$current_url: 'http://localhost:8000',
|
2023-05-04 13:25:56 +02:00
|
|
|
},
|
|
|
|
$set_once: {
|
|
|
|
$initial_current_url: 'http://localhost:8000',
|
|
|
|
},
|
|
|
|
name: 'hehe',
|
|
|
|
uuid: eventUuid,
|
|
|
|
},
|
|
|
|
teamId: teamId,
|
|
|
|
timestamp: ts.toISOString(),
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(webHookCalledWith).toEqual(expect.objectContaining({ data: expectedData }))
|
2022-11-30 13:49:17 +01:00
|
|
|
} finally {
|
|
|
|
server.close()
|
|
|
|
}
|
|
|
|
})
|