: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 { IntegrationType } from '~/cdp/types'
import { DependencyUnavailableError } from '~/common/utils/db/error'
import { closeHub, createHub } from '~/common/utils/db/hub'
import { PostgresUse } from '~/common/utils/db/postgres'
import { createTestTeamFixture } from '~/tests/helpers/sql'
import { Hub } from '~/types'
import { insertIntegration } from '../../_tests/fixtures'
import { IntegrationManagerService } from './integration-manager.service'
describe('IntegrationManager', () => {
jest.setTimeout(2000)
let hub: Hub
let manager: IntegrationManagerService
let integrations: IntegrationType[]
let teamId1: number
beforeEach(async () => {
hub = await createHub()
manager = new IntegrationManagerService(hub.pubSub, hub.postgres, hub.encryptedFields)
teamId1 = (await createTestTeamFixture(hub.postgres)).team.id
integrations = []
integrations.push(
await insertIntegration(hub.postgres, teamId1, {
id: teamId1,
kind: 'slack',
config: { team: 'foobar' },
sensitive_config: {
access_token: hub.encryptedFields.encrypt('token'),
not_encrypted: 'not-encrypted',
},
})
)
})
afterEach(async () => {
await closeHub(hub)
})
it('returns the integrations', async () => {
const items = await manager.getMany([integrations[0].id])
expect(items).toEqual({
[teamId1]: {
config: {
team: 'foobar',
},
id: teamId1,
kind: 'slack',
sensitive_config: {
access_token: 'token',
not_encrypted: 'not-encrypted',
},
team_id: teamId1,
},
})
})
it('retries the lookup when the read replica cancels it', async () => {
const cancelled = new Error('Canceling statement due to conflict with recovery.')
jest.spyOn(hub.postgres, 'query').mockRejectedValueOnce(
new DependencyUnavailableError(cancelled.message, 'Postgres', cancelled)
)
const item = await manager.get(integrations[0].id)
expect(item?.config).toEqual({ team: 'foobar' })
})
it('updates cached integration data when integration changes', async () => {
// First check - initial state
const item = await manager.get(integrations[0].id)
expect(item?.config).toEqual({ team: 'foobar' })
expect(item?.sensitive_config).toEqual({ access_token: 'token', not_encrypted: 'not-encrypted' })
// Update the integration in the database
await hub.postgres.query(
PostgresUse.COMMON_WRITE,
`UPDATE posthog_integration
SET config = jsonb_set(config, '{team}', '"updated-team"'::jsonb),
sensitive_config = jsonb_set(sensitive_config, '{access_token}', $1::jsonb)
WHERE id = $2`,
[JSON.stringify(hub.encryptedFields.encrypt('updated-token')), integrations[0].id],
'updateIntegration'
)
manager['onIntegrationsReloaded']([integrations[0].id])
// Verify the database update worked
const updatedIntegration = await hub.postgres.query(
PostgresUse.COMMON_READ,
`SELECT config, sensitive_config FROM posthog_integration WHERE id = $1`,
[integrations[0].id],
'fetchUpdatedIntegration'
)
// assert the integration was updated
expect(updatedIntegration.rows[0].config).toEqual({ team: 'updated-team' })
expect(hub.encryptedFields.decrypt(updatedIntegration.rows[0].sensitive_config.access_token)).toEqual(
'updated-token'
)
// Trigger integration reload
manager['onIntegrationsReloaded']([integrations[0].id])
// Check if the cached data was updated
const reloadedIntegrations = await manager.get(integrations[0].id)
expect(reloadedIntegrations?.config).toEqual({ team: 'updated-team' })
expect(reloadedIntegrations?.sensitive_config).toEqual({
access_token: 'updated-token',
not_encrypted: 'not-encrypted',
})
})
})