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.

4.4 KB
Raw Blame
import { MOCK_TEAM_ID } from 'lib/api.mock'

import { expectLogic } from 'kea-test-utils'

import api from 'lib/api'
import { delay } from 'lib/utils/async'

import { initKeaTests } from '~/test/init'

import { RestrictionType, eventIngestionRestrictionLogic } from './eventIngestionRestrictionLogic'

jest.mock('lib/api')

describe('eventIngestionRestrictionLogic', () => {
    let logic: ReturnType<typeof eventIngestionRestrictionLogic.build>

    beforeEach(() => {
        initKeaTests()
        logic = eventIngestionRestrictionLogic()
    })

    it('loads event ingestion restrictions', async () => {
        jest.spyOn(api, 'get').mockResolvedValue([
            {
                restriction_type: RestrictionType.DROP_EVENT_FROM_INGESTION,
                distinct_ids: ['user1', 'user2'],
            },
        ])

        logic.mount()
        logic.values.eventIngestionRestrictions
        await delay(1)
        const hasMatchingCall = (api.get as jest.Mock).mock.calls.some(
            (call) => call[0] === `api/projects/${MOCK_TEAM_ID}/event_ingestion_restrictions/`
        )
        expect(hasMatchingCall).toBe(true)

        await expectLogic(logic)
            .toDispatchActions(['loadEventIngestionRestrictions'])
            .toDispatchActionsInAnyOrder(['loadEventIngestionRestrictionsSuccess'])
            .toMatchValues({
                eventIngestionRestrictions: [
                    {
                        restriction_type: RestrictionType.DROP_EVENT_FROM_INGESTION,
                        distinct_ids: ['user1', 'user2'],
                    },
                ],
                hasProjectNoticeRestriction: true,
            })
    })

    it('handles SKIP_PERSON_PROCESSING restriction', async () => {
        jest.spyOn(api, 'get').mockResolvedValue([
            {
                restriction_type: RestrictionType.SKIP_PERSON_PROCESSING,
                distinct_ids: ['user3'],
            },
        ])

        logic.mount()
        logic.values.eventIngestionRestrictions

        await expectLogic(logic)
            .toDispatchActions(['loadEventIngestionRestrictions', 'loadEventIngestionRestrictionsSuccess'])
            .toMatchValues({
                eventIngestionRestrictions: [
                    {
                        restriction_type: RestrictionType.SKIP_PERSON_PROCESSING,
                        distinct_ids: ['user3'],
                    },
                ],
                hasProjectNoticeRestriction: true,
            })
    })

    it('handles FORCE_OVERFLOW_FROM_INGESTION restriction (should not trigger project notice)', async () => {
        jest.spyOn(api, 'get').mockResolvedValue([
            {
                restriction_type: RestrictionType.FORCE_OVERFLOW_FROM_INGESTION,
                distinct_ids: ['user4'],
            },
        ])

        logic.mount()
        logic.values.eventIngestionRestrictions

        await expectLogic(logic)
            .toDispatchActions(['loadEventIngestionRestrictions', 'loadEventIngestionRestrictionsSuccess'])
            .toMatchValues({
                eventIngestionRestrictions: [
                    {
                        restriction_type: RestrictionType.FORCE_OVERFLOW_FROM_INGESTION,
                        distinct_ids: ['user4'],
                    },
                ],
                hasProjectNoticeRestriction: false,
            })
    })

    it('handles empty restrictions', async () => {
        jest.spyOn(api, 'get').mockResolvedValue([])

        logic.mount()
        logic.values.eventIngestionRestrictions

        await expectLogic(logic)
            .toDispatchActions(['loadEventIngestionRestrictions', 'loadEventIngestionRestrictionsSuccess'])
            .toMatchValues({
                eventIngestionRestrictions: [],
                hasProjectNoticeRestriction: false,
            })
    })

    it('coerces null api response to empty array', async () => {
        // api.get resolves to null when the response body isn't valid JSON
        // (e.g. 204 No Content, CDN/proxy HTML error pages) — guard against it
        jest.spyOn(api, 'get').mockResolvedValue(null)

        logic.mount()
        logic.values.eventIngestionRestrictions

        await expectLogic(logic)
            .toDispatchActions(['loadEventIngestionRestrictions', 'loadEventIngestionRestrictionsSuccess'])
            .toMatchValues({
                eventIngestionRestrictions: [],
                hasProjectNoticeRestriction: false,
            })
    })
})