:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { DateTime } from 'luxon'
import { TemplateTester } from '../../test/test-helpers'
import { template } from './incoming_webhook.template'
describe('incoming webhook template', () => {
const tester = new TemplateTester(template)
beforeEach(async () => {
await tester.beforeEach()
const fixedTime = DateTime.fromISO('2025-01-01T00:00:00Z').toJSDate()
jest.spyOn(Date, 'now').mockReturnValue(fixedTime.getTime())
})
it('should invoke the function', async () => {
const response = await tester.invoke(
{
event: '{request.body.eventName}',
distinct_id: 'hardcoded',
properties: {
root_level: '{request.body.rootLevel}',
nested_level: '{request.body.nested.nestedLevel}',
missing: '{request.body.missing?.missingvalue}',
},
},
{
request: {
method: 'POST',
body: {
eventName: 'the event',
rootLevel: 'rootLevelValue',
nested: {
nestedLevel: 'nestedLevelValue',
},
},
stringBody: '',
headers: {},
ip: '127.0.0.1',
query: {},
},
}
)
expect(response.error).toBeUndefined()
expect(response.finished).toEqual(true)
expect(response.capturedPostHogEvents).toMatchInlineSnapshot(`
[
{
"distinct_id": "hardcoded",
"event": "the event",
"properties": {
"$hog_function_execution_count": 1,
"missing": null,
"nested_level": "nestedLevelValue",
"root_level": "rootLevelValue",
},
"team_id": 1,
"timestamp": "2025-01-01T00:00:00.000Z",
},
]
`)
})
it('should return 401 if the auth header is incorrect', async () => {
const response = await tester.invoke(
{
event: '{request.body.eventName}',
distinct_id: 'hardcoded',
auth_header: 'Bearer my-secret-token',
},
{
request: {
method: 'POST',
body: {
eventName: 'the event',
},
stringBody: '',
headers: {
authorization: 'Bearer wrong-token',
},
ip: '127.0.0.1',
query: {},
},
}
)
expect(response.error).toBeUndefined()
expect(response.finished).toEqual(true)
expect(response.execResult).toEqual({
httpResponse: {
status: 401,
body: 'Unauthorized',
},
})
})
it('should pass if the auth header is correct', async () => {
const response = await tester.invoke(
{
event: '{request.body.eventName}',
distinct_id: 'hardcoded',
auth_header: 'Bearer my-secret-token',
},
{
request: {
method: 'POST',
body: {
eventName: 'the event',
},
stringBody: '',
headers: {
authorization: 'Bearer my-secret-token',
},
ip: '127.0.0.1',
query: {},
},
}
)
expect(response.capturedPostHogEvents).toHaveLength(1)
expect(response.error).toBeUndefined()
expect(response.finished).toEqual(true)
expect(response.execResult).toBeUndefined()
})
it('should print the request body if debug is true', async () => {
const response = await tester.invoke(
{
event: '{request.body.eventName}',
distinct_id: 'hardcoded',
debug: true,
},
{
request: {
method: 'POST',
body: {
eventName: 'the event',
},
stringBody: '',
headers: {},
query: {},
},
}
)
expect(response.logs.map((x) => x.message)).toEqual([
`Incoming request:, {"eventName":"the event"}`,
expect.stringContaining('Function completed'),
])
})
})