Skip to content

: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.

5.5 KB
Raw Blame
import { HogFunctionInvocationGlobals } from '../../../types'
import { TemplateTester } from '../../test/test-helpers'
import { template } from './url-masking.template'

describe('url-masking.template', () => {
    const tester = new TemplateTester(template)
    let mockGlobals: HogFunctionInvocationGlobals

    beforeEach(async () => {
        await tester.beforeEach()
    })

    it('should mask sensitive parameters in URLs', async () => {
        mockGlobals = tester.createGlobals({
            event: {
                properties: {
                    $current_url: 'https://[email protected]&password=secret&name=john',
                    $referrer: 'https://other.com?token=12345&[email protected]',
                },
            },
        })

        const response = await tester.invoke(
            {
                urlProperties: {
                    $current_url: 'email, password',
                    $referrer: 'email, token',
                },
                maskWith: '[REDACTED]',
            },
            mockGlobals
        )

        expect(response.finished).toBe(true)
        expect(response.error).toBeUndefined()
        expect(response.execResult).toMatchObject({
            properties: {
                $current_url: 'https://example.com?email=[REDACTED]&password=[REDACTED]&name=john',
                $referrer: 'https://other.com?token=[REDACTED]&email=[REDACTED]',
            },
        })
    })

    it('should handle URLs without query parameters', async () => {
        mockGlobals = tester.createGlobals({
            event: {
                properties: {
                    $current_url: 'https://example.com',
                },
            },
        })

        const response = await tester.invoke(
            {
                urlProperties: {
                    $current_url: 'email, password',
                },
                maskWith: '[REDACTED]',
            },
            mockGlobals
        )

        expect(response.finished).toBe(true)
        expect(response.error).toBeUndefined()
        expect(response.execResult).toMatchObject({
            properties: {
                $current_url: 'https://example.com',
            },
        })
    })

    it('should handle malformed URLs starting with question mark', async () => {
        mockGlobals = tester.createGlobals({
            event: {
                properties: {
                    $current_url: '[email protected]',
                },
            },
        })

        const response = await tester.invoke(
            {
                urlProperties: {
                    $current_url: 'email',
                },
                maskWith: '[REDACTED]',
            },
            mockGlobals
        )

        expect(response.finished).toBe(true)
        expect(response.error).toBeUndefined()
        expect(response.execResult).toMatchObject({
            properties: {
                $current_url: '[email protected]',
            },
        })
    })

    it('should handle missing properties', async () => {
        mockGlobals = tester.createGlobals({
            event: {
                properties: {
                    $current_url: 'https://[email protected]',
                },
            },
        })

        const response = await tester.invoke(
            {
                urlProperties: {
                    $current_url: 'email',
                    $referrer: 'email', // $referrer doesn't exist in properties
                },
                maskWith: '[REDACTED]',
            },
            mockGlobals
        )

        expect(response.finished).toBe(true)
        expect(response.error).toBeUndefined()
        expect(response.execResult).toMatchObject({
            properties: {
                $current_url: 'https://example.com?email=[REDACTED]',
            },
        })
    })

    it('should handle parameters without values', async () => {
        mockGlobals = tester.createGlobals({
            event: {
                properties: {
                    $current_url: 'https://example.com?email&token=123&password',
                },
            },
        })

        const response = await tester.invoke(
            {
                urlProperties: {
                    $current_url: 'email, password, token',
                },
                maskWith: '[REDACTED]',
            },
            mockGlobals
        )

        expect(response.finished).toBe(true)
        expect(response.error).toBeUndefined()
        expect(response.execResult).toMatchObject({
            properties: {
                $current_url: 'https://example.com?email=[REDACTED]&token=[REDACTED]&password=[REDACTED]',
            },
        })
    })

    it('should handle empty parameter values', async () => {
        mockGlobals = tester.createGlobals({
            event: {
                properties: {
                    $current_url: 'https://example.com?email=&token=123',
                },
            },
        })

        const response = await tester.invoke(
            {
                urlProperties: {
                    $current_url: 'email, token',
                },
                maskWith: '[REDACTED]',
            },
            mockGlobals
        )

        expect(response.finished).toBe(true)
        expect(response.error).toBeUndefined()
        expect(response.execResult).toMatchObject({
            properties: {
                $current_url: 'https://example.com?email=[REDACTED]&token=[REDACTED]',
            },
        })
    })
})