From 1901c4d435704a83143125facae4d712df5eb84e Mon Sep 17 00:00:00 2001 From: Quentin BEY Date: Tue, 28 Oct 2025 22:39:56 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=91=EF=B8=8F(posthog)=20pass=20str=20i?= =?UTF-8?q?nstead=20of=20UUID=20for=20user=20PK?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The serialization before sending the request to Posthog was failing because of UUID. --- CHANGELOG.md | 5 ++++ src/backend/chat/views.py | 2 +- src/backend/core/feature_flags/helpers.py | 2 +- .../core/tests/feature_flags/test_helpers.py | 28 ++++++++++++++----- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5433186..45f0570 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,11 @@ and this project adheres to ## [Unreleased] +### Fixed + +- 🚑️(posthog) pass str instead of UUID for user PK #134 + + ## [0.0.7] - 2025-10-28 ### Fixed diff --git a/src/backend/chat/views.py b/src/backend/chat/views.py index 383b780..0437237 100644 --- a/src/backend/chat/views.py +++ b/src/backend/chat/views.py @@ -425,7 +425,7 @@ class ChatConversationAttachmentViewSet( if settings.POSTHOG_KEY: posthog.capture( "item_uploaded", - distinct_id=request.user.pk, # same as set by the frontend + distinct_id=str(request.user.pk), # same as set by the frontend properties={ "id": attachment.pk, "file_name": attachment.file_name, diff --git a/src/backend/core/feature_flags/helpers.py b/src/backend/core/feature_flags/helpers.py index a1f537a..a4260fd 100644 --- a/src/backend/core/feature_flags/helpers.py +++ b/src/backend/core/feature_flags/helpers.py @@ -38,7 +38,7 @@ def is_feature_enabled( if posthog is not None: return posthog.feature_enabled( frontend_feature_name(feature_name), - user.pk, # same as set by the frontend + str(user.pk), # same as set by the frontend ) # No feature flag manager diff --git a/src/backend/core/tests/feature_flags/test_helpers.py b/src/backend/core/tests/feature_flags/test_helpers.py index 814a789..d61d7fc 100644 --- a/src/backend/core/tests/feature_flags/test_helpers.py +++ b/src/backend/core/tests/feature_flags/test_helpers.py @@ -1,9 +1,12 @@ """Tests for feature flag helpers.""" +import json import logging from unittest.mock import patch +import posthog import pytest +import responses from core.factories import UserFactory from core.feature_flags.flags import FeatureToggle @@ -42,18 +45,29 @@ def test_is_feature_enabled_always_disabled(feature_flags): assert is_feature_enabled(user, "document_upload") is False -@patch("core.feature_flags.helpers.posthog") -def test_is_feature_enabled_dynamic_posthog_true(mock_posthog, feature_flags): +@responses.activate +def test_is_feature_enabled_dynamic_posthog_true(feature_flags, settings): """Test that a dynamic feature returns the value from PostHog when PostHog is available.""" + settings.POSTHOG_KEY = {"id": "132456", "host": "https://eu.i.posthog-test.com"} + + posthog.api_key = settings.POSTHOG_KEY["id"] + posthog.host = settings.POSTHOG_KEY["host"] + + responses.post( + f"{posthog.host}/flags/?v=2", json={"flags": {"web-search": {"enabled": True}}}, status=200 + ) + feature_flags.web_search = FeatureToggle.DYNAMIC user = UserFactory() - mock_posthog.feature_enabled.return_value = True assert is_feature_enabled(user, "web_search") is True - mock_posthog.feature_enabled.assert_called_once_with( - "web-search", - user.pk, - ) + + request_body = json.loads(responses.calls[0].request.body) + assert request_body["distinct_id"] == str(user.pk) + assert request_body["flag_keys_to_evaluate"] == ["web-search"] + + posthog.api_key = None + posthog.host = None @patch("core.feature_flags.helpers.posthog")