: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
fix(dashboards): evaluate customization flag against posthog project (#90755)
posthog[bot]0b2dba175119
diff --git a/posthog/api/test/dashboards/test_dashboard.py b/posthog/api/test/dashboards/test_dashboard.pyindex c3746336051e..fab85c214f42 100644--- a/posthog/api/test/dashboards/test_dashboard.py+++ b/posthog/api/test/dashboards/test_dashboard.py@@ -4,9 +4,8 @@ import datetimefrom freezegun import freeze_timefrom posthog.test.base import APIBaseTest, FuzzyInt, QueryMatchingTest, snapshot_postgres_queriesfrom unittest import mock-from unittest.mock import ANY, MagicMock, call, patch+from unittest.mock import ANY, MagicMock, patch-from django.conf import settingsfrom django.core.cache import cachefrom django.test import override_settingsfrom django.utils.timezone import now@@ -674,11 +673,10 @@ class TestDashboard(APIBaseTest, QueryMatchingTest):Dashboard.objects.get(id=copied_id).customization, {"show_legend": False, "tile_spacing": "wide"})- @patch(- "products.dashboards.backend.feature_flags.get_flags_from_service",- return_value={"flags": {"dashboard-customization": {"enabled": True}}},- )- def test_dashboard_customization_uses_remote_flag_evaluation(self, mock_get_flags: MagicMock) -> None:+ @patch("products.dashboards.backend.feature_flags.posthoganalytics.feature_enabled", return_value=True)+ def test_dashboard_customization_evaluates_flag_against_posthog_project(+ self, mock_feature_enabled: MagicMock+ ) -> None:dashboard_id, _ = self.dashboard_api.create_dashboard({"name": "dashboard"})_, updated = self.dashboard_api.update_dashboard(@@ -690,31 +688,11 @@ class TestDashboard(APIBaseTest, QueryMatchingTest):updated["customization"],{"tile_spacing": "condensed", "layout_compaction": "horizontal"},)- expected_flag_call = call(- self.team.api_token,- self.user.distinct_id,- groups={"organization": str(self.team.organization_id), "project": str(self.team.id)},- flag_keys=["dashboard-customization"],- internal_request_token=settings.INTERNAL_REQUEST_TOKEN,- evaluation_runtime="all",+ # The flag must resolve through posthoganalytics (our project), not the customer team token.+ self.assertIn(+ "dashboard-customization",+ [flag_call.args[0] for flag_call in mock_feature_enabled.call_args_list],)- self.assertGreater(len(mock_get_flags.call_args_list), 0)- self.assertTrue(all(flag_call == expected_flag_call for flag_call in mock_get_flags.call_args_list))-- @patch("products.dashboards.backend.feature_flags.get_flags_from_service", side_effect=ConnectionError)- def test_dashboard_customization_fails_closed_when_remote_evaluation_fails(- self, _mock_get_flags: MagicMock- ) -> None:- dashboard_id, _ = self.dashboard_api.create_dashboard({"name": "dashboard"})-- _, response = self.dashboard_api.update_dashboard(- dashboard_id,- {"grid_spacing": "condensed"},- expected_status=status.HTTP_400_BAD_REQUEST,- )-- self.assertEqual(response["attr"], "grid_spacing")- self.assertEqual(response["detail"], "Tile density isn't available.")@parameterized.expand([("horizontal",), ("stable",)])@patch("products.dashboards.backend.api.dashboard.dashboard_customization_enabled", return_value=True)diff --git a/products/dashboards/backend/feature_flags.py b/products/dashboards/backend/feature_flags.pyindex 53ae99f043c1..ed087aaf87a5 100644--- a/products/dashboards/backend/feature_flags.py+++ b/products/dashboards/backend/feature_flags.py@@ -2,11 +2,8 @@ from __future__ import annotationsfrom typing import TYPE_CHECKING-from django.conf import settings-import posthoganalytics-from posthog.api.services.flags_service import get_flags_from_servicefrom posthog.permissions import _FORCE_ENABLED_FLAGSif TYPE_CHECKING:@@ -38,32 +35,9 @@ def widget_flag_enabled(flag: str, *, team: Team, user: User | None = None) -> b)-def _remote_flag_enabled(flag: str, *, team: Team, user: User | None = None) -> bool:- """Evaluate flags whose rollout depends on cohort membership."""- if flag in _FORCE_ENABLED_FLAGS:- return True-- distinct_id = (user.distinct_id or str(user.uuid)) if user is not None else str(team.uuid)- organization_id = str(team.organization_id)- project_id = str(team.id)-- try:- result = get_flags_from_service(- team.api_token,- distinct_id,- groups={"organization": organization_id, "project": project_id},- flag_keys=[flag],- internal_request_token=settings.INTERNAL_REQUEST_TOKEN,- evaluation_runtime="all",- )- except Exception:- return False- return bool(result.get("flags", {}).get(flag, {}).get("enabled"))--def dashboard_widgets_enabled(*, team: Team, user: User | None = None) -> bool:return widget_flag_enabled(DASHBOARD_WIDGETS_FLAG, team=team, user=user)def dashboard_customization_enabled(*, team: Team, user: User | None = None) -> bool:- return _remote_flag_enabled(DASHBOARD_CUSTOMIZATION_FLAG, team=team, user=user)+ return widget_flag_enabled(DASHBOARD_CUSTOMIZATION_FLAG, team=team, user=user)