0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-12-01 12:21:02 +01:00

Fix $active_feature_flags in capture.py (#7173)

* fix $active_feature_flags in capture.py

* add test for case

* remove unused import
This commit is contained in:
Rick Marron 2021-11-17 01:30:19 -08:00 committed by GitHub
parent 79acd298d4
commit 87744e863b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 4 deletions

View File

@ -18,7 +18,7 @@ from posthog.celery import app as celery_app
from posthog.exceptions import RequestParsingError, generate_exception_response
from posthog.helpers.session_recording import preprocess_session_recording_events
from posthog.models import Team
from posthog.models.feature_flag import get_active_feature_flags
from posthog.models.feature_flag import get_overridden_feature_flags
from posthog.models.utils import UUIDT
from posthog.utils import cors_response, get_ip_address, is_clickhouse_enabled
@ -136,7 +136,7 @@ def _get_distinct_id(data: Dict[str, Any]) -> str:
def _ensure_web_feature_flags_in_properties(event: Dict[str, Any], team: Team, distinct_id: str):
"""If the event comes from web, ensure that it contains property $active_feature_flags."""
if event["properties"].get("$lib") == "web" and "$active_feature_flags" not in event["properties"]:
flags = get_active_feature_flags(team, distinct_id)
flags = get_overridden_feature_flags(team, distinct_id)
event["properties"]["$active_feature_flags"] = list(flags.keys())
for k, v in flags.items():
event["properties"][f"$feature/{k}"] = v

View File

@ -13,8 +13,8 @@ from freezegun import freeze_time
from rest_framework import status
from posthog.api.test.mock_sentry import mock_sentry_context_for_tagging
from posthog.models import PersonalAPIKey
from posthog.models.feature_flag import FeatureFlag
from posthog.models import Person, PersonalAPIKey
from posthog.models.feature_flag import FeatureFlag, FeatureFlagOverride
from posthog.test.base import BaseTest
@ -771,6 +771,30 @@ class TestCapture(BaseTest):
arguments = self._to_arguments(patch_process_event_with_plugins)
self.assertEqual(arguments["data"]["properties"]["$active_feature_flags"], ["test-ff"])
@patch("posthog.api.capture.celery_app.send_task")
def test_add_feature_flags_with_overrides_if_missing(self, patch_process_event_with_plugins) -> None:
feature_flag_instance = FeatureFlag.objects.create(
team=self.team, created_by=self.user, key="test-ff", rollout_percentage=0
)
Person.objects.create(
team=self.team, distinct_ids=[self.user.distinct_id], properties={"email": self.user.email},
)
FeatureFlagOverride.objects.create(
team=self.team, user=self.user, feature_flag=feature_flag_instance, override_value=True
)
self.client.post(
"/track/",
data={
"data": json.dumps(
[{"event": "purchase", "properties": {"distinct_id": self.user.distinct_id, "$lib": "web"}}]
),
"api_key": self.team.api_token,
},
)
arguments = self._to_arguments(patch_process_event_with_plugins)
self.assertEqual(arguments["data"]["properties"]["$feature/test-ff"], True)
self.assertEqual(arguments["data"]["properties"]["$active_feature_flags"], ["test-ff"])
def test_handle_lacking_event_name_field(self):
response = self.client.post(
"/e/",