: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(analytics): stop get_client capturing to the real project in tests (#83133)
Matt Brooker4ea4dfe68b2f
diff --git a/posthog/ph_client.py b/posthog/ph_client.pyindex a80b85061e5c..5b2745a65b10 100644--- a/posthog/ph_client.py+++ b/posthog/ph_client.py@@ -1,3 +1,4 @@+import osimport atexitimport threadingfrom collections.abc import Mapping@@ -6,6 +7,8 @@ from numbers import Numberfrom typing import Anyfrom uuid import UUID+from django.conf import settings+import structlogimport posthoganalytics@@ -159,6 +162,11 @@ def get_client(region: str = "US", **kwargs: Any):else:return+ # A fresh client does not inherit the module-level `disabled` flag that apps.py sets+ # under TEST, so without this a test that runs in cloud mode captures to the real+ # project. Callers can still pass `disabled` explicitly to override.+ kwargs.setdefault("disabled", bool(settings.TEST or os.environ.get("OPT_OUT_CAPTURE", False)))+return Posthog(api_key,host=host,diff --git a/posthog/test/test_ph_client.py b/posthog/test/test_ph_client.pyindex cb597cf6437a..fa793c87ecbb 100644--- a/posthog/test/test_ph_client.py+++ b/posthog/test/test_ph_client.py@@ -1,6 +1,6 @@from unittest.mock import MagicMock-from django.test import SimpleTestCase+from django.test import SimpleTestCase, override_settingsimport posthoganalytics@@ -31,3 +31,29 @@ class TestScopedCaptureFlush(SimpleTestCase):client = MagicMock()ScopedCapture(client).flush()client.flush.assert_called_once_with(timeout_seconds=None)+++class TestGetClientTestGuard(SimpleTestCase):+ def test_client_is_disabled_under_test_settings(self) -> None:+ # apps.py disables the module-level client under TEST, but a client built here+ # is a fresh instance that never sees that flag. Without its own guard, any+ # test running in cloud mode captures to the real project.+ client = get_client()+ assert client is not None+ self.assertTrue(client.disabled)++ @override_settings(CLOUD_DEPLOYMENT="US")+ def test_client_stays_disabled_when_a_test_runs_in_cloud_mode(self) -> None:+ # is_cloud() is the only other guard on this path, so a test that overrides+ # CLOUD_DEPLOYMENT to exercise cloud behaviour would otherwise emit for real.+ client = get_client()+ assert client is not None+ self.assertTrue(client.disabled)++ def test_explicit_disabled_wins(self) -> None:+ client = get_client(disabled=False)+ assert client is not None+ self.assertFalse(client.disabled)++ def test_unknown_region_returns_nothing(self) -> None:+ self.assertIsNone(get_client(region="MARS"))