Skip to content

Commit

Permalink
chore: add sanitization test to heatmaps tests (#1256)
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra authored Jun 21, 2024
1 parent fd5ff2c commit 004351a
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/__tests__/heatmaps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { createPosthogInstance } from './helpers/posthog-instance'
import { uuidv7 } from '../uuidv7'
import { PostHog } from '../posthog-core'
import { DecideResponse } from '../types'
import { isObject } from '../utils/type-utils'

jest.mock('../utils/logger')

describe('heatmaps', () => {
Expand All @@ -18,7 +20,25 @@ describe('heatmaps', () => {

beforeEach(async () => {
onCapture = jest.fn()
posthog = await createPosthogInstance(uuidv7(), { _onCapture: onCapture })
posthog = await createPosthogInstance(uuidv7(), {
_onCapture: onCapture,
sanitize_properties: (props) => {
// what ever sanitization makes sense
const sanitizeUrl = (url: string) => url.replace(/https?:\/\/[^/]+/g, 'http://replaced')
if (props['$current_url']) {
props['$current_url'] = sanitizeUrl(props['$current_url'])
}
if (isObject(props['$heatmap_data'])) {
// the keys of the heatmap data are URLs, so we might need to sanitize them to
// this sanitized URL would need to be entered in the toolbar for the heatmap display to work
props['$heatmap_data'] = Object.entries(props['$heatmap_data']).reduce((acc, [url, data]) => {
acc[sanitizeUrl(url)] = data
return acc
}, {})
}
return props
},
})
})

it('should include generated heatmap data', async () => {
Expand All @@ -32,7 +52,7 @@ describe('heatmaps', () => {
event: 'test event',
properties: {
$heatmap_data: {
'http://localhost/': [
'http://replaced/': [
{
target_fixed: false,
type: 'click',
Expand All @@ -54,8 +74,8 @@ describe('heatmaps', () => {
posthog.capture('test event')

expect(onCapture).toBeCalledTimes(1)
expect(onCapture.mock.lastCall[1].properties.$heatmap_data['http://localhost/']).toHaveLength(4)
expect(onCapture.mock.lastCall[1].properties.$heatmap_data['http://localhost/'].map((x) => x.type)).toEqual([
expect(onCapture.mock.lastCall[1].properties.$heatmap_data['http://replaced/']).toHaveLength(4)
expect(onCapture.mock.lastCall[1].properties.$heatmap_data['http://replaced/'].map((x) => x.type)).toEqual([
'click',
'click',
'rageclick',
Expand All @@ -68,7 +88,7 @@ describe('heatmaps', () => {
posthog.heatmaps?.['_onClick']?.(createMockMouseEvent())
posthog.capture('test event')
expect(onCapture).toBeCalledTimes(1)
expect(onCapture.mock.lastCall[1].properties.$heatmap_data['http://localhost/']).toHaveLength(2)
expect(onCapture.mock.lastCall[1].properties.$heatmap_data['http://replaced/']).toHaveLength(2)

posthog.capture('test event 2')
expect(onCapture).toBeCalledTimes(2)
Expand Down

0 comments on commit 004351a

Please sign in to comment.