From 0d0414fdd6e2450effd7d11529028b9c0da7ece9 Mon Sep 17 00:00:00 2001 From: Tiina Turban Date: Thu, 18 Jul 2024 23:39:12 +0200 Subject: [PATCH] fix tests --- .../functional_tests/webhooks.test.ts | 7 +++-- .../batch-processing/each-batch-webhooks.ts | 8 ++++-- .../each-batch-webhooks.test.ts | 28 +++++++++++++++++-- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/plugin-server/functional_tests/webhooks.test.ts b/plugin-server/functional_tests/webhooks.test.ts index bc5cb93a8ca53..4e26055c8de15 100644 --- a/plugin-server/functional_tests/webhooks.test.ts +++ b/plugin-server/functional_tests/webhooks.test.ts @@ -8,7 +8,6 @@ import { createGroup, createGroupType, createHook, - createOrganization, createOrganizationRaw, createTeam, createUser, @@ -41,7 +40,9 @@ test.concurrent(`webhooks: fires slack webhook`, async () => { const distinctId = new UUIDT().toString() - const organizationId = await createOrganization() + const organizationId = await createOrganizationRaw({ + available_product_features: `array ['{ "key": "group_analytics", "name": "group_analytics" }'::jsonb]`, + }) const teamId = await createTeam(organizationId, `http://localhost:${server.address()?.port}`) const user = await createUser(teamId, new UUIDT().toString()) await createGroupType(teamId, 0, 'organization') @@ -90,7 +91,7 @@ test.concurrent(`webhooks: fires slack webhook`, async () => { $current_url: 'http://localhost:8000', $elements: [{ tag_name: 'div', nth_child: 1, nth_of_type: 2, $el_text: 'text' }], $set: { email: 't@t.com' }, - $group_0: 'TestWebhookOrg', + $groups: { organization: 'TestWebhookOrg' }, }, }) diff --git a/plugin-server/src/main/ingestion-queues/batch-processing/each-batch-webhooks.ts b/plugin-server/src/main/ingestion-queues/batch-processing/each-batch-webhooks.ts index c2fc20ba6c8da..51123f497363b 100644 --- a/plugin-server/src/main/ingestion-queues/batch-processing/each-batch-webhooks.ts +++ b/plugin-server/src/main/ingestion-queues/batch-processing/each-batch-webhooks.ts @@ -169,8 +169,11 @@ async function addGroupPropertiesToPostIngestionEvent( for (const [groupType, columnIndex] of Object.entries(groupTypes)) { const groupKey = (event.properties[`$groups`] || {})[groupType] + if (!groupKey) { + continue + } - const queryString = `SELECT properties FROM posthog_group WHERE team_id = $1 AND group_type_index = $2 AND group_key = $3` + const queryString = `SELECT group_properties FROM posthog_group WHERE team_id = $1 AND group_type_index = $2 AND group_key = $3` const selectResult: QueryResult = await postgres.query( PostgresUse.COMMON_READ, @@ -178,6 +181,7 @@ async function addGroupPropertiesToPostIngestionEvent( [event.teamId, columnIndex, groupKey], 'fetchGroup' ) + const groupProperties = selectResult.rows.length > 0 ? selectResult.rows[0].group_properties : {} if (groupKey && groupProperties) { @@ -185,7 +189,7 @@ async function addGroupPropertiesToPostIngestionEvent( index: columnIndex, key: groupKey, type: groupType, - properties: JSON.parse(groupProperties), + properties: groupProperties, } } } diff --git a/plugin-server/tests/main/ingestion-queues/each-batch-webhooks.test.ts b/plugin-server/tests/main/ingestion-queues/each-batch-webhooks.test.ts index 344bf200f766a..63092e94ede3f 100644 --- a/plugin-server/tests/main/ingestion-queues/each-batch-webhooks.test.ts +++ b/plugin-server/tests/main/ingestion-queues/each-batch-webhooks.test.ts @@ -28,7 +28,6 @@ const clickhouseEvent: RawClickHouseEvent = { person_id: 'F99FA0A1-E0C2-4CFE-A09A-4C3C4327A4CC', person_created_at: '2020-02-20 02:15:00' as ClickHouseTimestampSecondPrecision, // Match createEvent ts format person_properties: '{}', - group0_properties: JSON.stringify({ name: 'PostHog' }), person_mode: 'full', } @@ -47,6 +46,30 @@ describe('eachMessageWebhooksHandlers', () => { [], 'testTag' ) + await hub.db.postgres.query( + PostgresUse.COMMON_WRITE, + ` + INSERT INTO posthog_group (team_id, group_key, group_type_index, group_properties, created_at, properties_last_updated_at, properties_last_operation, version) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8) + `, + [ + 2, + 'org_posthog', + 0, + JSON.stringify({ name: 'PostHog' }), + new Date().toISOString(), + JSON.stringify({}), + JSON.stringify({}), + 1, + ], + 'upsertGroup' + ) + await hub.db.postgres.query( + PostgresUse.COMMON_WRITE, + `UPDATE posthog_team SET slack_incoming_webhook = 'https://webhook.example.com/'`, + [], + 'testTag' + ) }) afterEach(async () => { @@ -120,7 +143,8 @@ describe('eachMessageWebhooksHandlers', () => { actionMatcher, hookCannon, groupTypeManager, - organizationManager + organizationManager, + hub.postgres ) // NOTE: really it would be nice to verify that fire has been called