Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

test(backend): keep the feature-flag tests from sending events to PostHog (#15378)

Reinier van der Leerfddc3f218a70
diff --git a/autogpt_platform/backend/backend/util/feature_flag/backends_test.py b/autogpt_platform/backend/backend/util/feature_flag/backends_test.py
index b27af3ea87..e6953b08cf 100644
--- a/autogpt_platform/backend/backend/util/feature_flag/backends_test.py
+++ b/autogpt_platform/backend/backend/util/feature_flag/backends_test.py
@@ -210,6 +210,7 @@ class TestAnUnknownBackendValue:
assert Config().feature_flag_backend is FeatureFlagBackend.POSTHOG
[email protected]("posthog_events_dropped")
class TestPostHogBackend:
@pytest.mark.asyncio
async def test_an_enabled_flag_is_authoritative(self, mocker, user_context):
@@ -1089,6 +1090,7 @@ class TestSentryFlagContext:
assert await evaluate_feature_flag(Flag.HIRE_EXPERTS, "u-1") == (True, True)
[email protected]("posthog_events_dropped")
class TestTheCountryRuleInEveryBackend:
"""The trial's country targeting, through each vendor's real evaluator."""
diff --git a/autogpt_platform/backend/backend/util/feature_flag/conftest.py b/autogpt_platform/backend/backend/util/feature_flag/conftest.py
new file mode 100644
index 0000000000..197729a607
--- /dev/null
+++ b/autogpt_platform/backend/backend/util/feature_flag/conftest.py
@@ -0,0 +1,39 @@
+import pytest
+import urllib3.connectionpool
+from posthog import Posthog
+
+
[email protected](autouse=True)
+def no_real_http(request, monkeypatch):
+ """Fails a test that reaches for the network, naming what it would have sent.
+
+ A real PostHog client posts a `$feature_flag_called` event for every flag
+ read; refusing it at `capture` also stops the uploads its background thread
+ would make after the test ends. Yields the record, which the guard's own
+ tests clear.
+ """
+ sent: list[str] = []
+
+ def refuse_request(pool, method, url, *args, **kwargs):
+ sent.append(f"{method} {pool.scheme}://{pool.host}{url.split('?')[0]}")
+ raise urllib3.exceptions.ProtocolError("refused by the no_real_http fixture")
+
+ def refuse_event(client, event, **kwargs):
+ sent.append(f"a {event} event")
+
+ monkeypatch.setattr(
+ urllib3.connectionpool.HTTPConnectionPool, "urlopen", refuse_request
+ )
+ monkeypatch.setattr(Posthog, "capture", refuse_event)
+ yield sent
+ if sent:
+ pytest.fail(
+ f"{request.node.nodeid} would have sent {sent} to the network. Mock "
+ "the request, or use posthog_events_dropped for a flag read's events."
+ )
+
+
+def posthog_events_dropped(mocker):
+ """For tests whose real PostHog client reads flags, and so reports each read."""
+ mocker.patch.object(Posthog, "capture")
diff --git a/autogpt_platform/backend/backend/util/feature_flag/definition_cache_test.py b/autogpt_platform/backend/backend/util/feature_flag/definition_cache_test.py
index 6fe5ce483f..9a070f3dc8 100644
--- a/autogpt_platform/backend/backend/util/feature_flag/definition_cache_test.py
+++ b/autogpt_platform/backend/backend/util/feature_flag/definition_cache_test.py
@@ -435,6 +435,7 @@ class TestLaunchDarklyBackendIsUntouched:
build.assert_not_called()
[email protected]("posthog_events_dropped")
class TestAgainstTheRealSDK:
"""The SDK's own poll, driven through the provider."""
diff --git a/autogpt_platform/backend/backend/util/feature_flag/no_real_http_test.py b/autogpt_platform/backend/backend/util/feature_flag/no_real_http_test.py
new file mode 100644
index 0000000000..e8c1d1dc3b
--- /dev/null
+++ b/autogpt_platform/backend/backend/util/feature_flag/no_real_http_test.py
@@ -0,0 +1,26 @@
+"""The flag package's network guard refuses what it exists to refuse."""
+
+import pytest
+import urllib3
+from posthog import Posthog
+
+
+def test_a_request_off_the_machine_is_refused_and_recorded(no_real_http):
+ # A host that never resolves, so a guard that stopped applying fails here
+ # without reaching anything real.
+ with pytest.raises(urllib3.exceptions.ProtocolError):
+ urllib3.PoolManager().request("GET", "https://example.invalid/flags?token=x")
+
+ assert no_real_http == ["GET https://example.invalid/flags"]
+ no_real_http.clear()
+
+
+def test_a_captured_event_is_refused_and_recorded(no_real_http):
+ client = Posthog("phc_test", sync_mode=True)
+ try:
+ client.capture("$feature_flag_called", distinct_id="u-1")
+ finally:
+ client.shutdown()
+
+ assert no_real_http == ["a $feature_flag_called event"]
+ no_real_http.clear()
diff --git a/autogpt_platform/backend/scripts/feature_flag_sync_dual_test.py b/autogpt_platform/backend/scripts/feature_flag_sync_dual_test.py
index 1d1c09c4d3..adae608217 100644
--- a/autogpt_platform/backend/scripts/feature_flag_sync_dual_test.py
+++ b/autogpt_platform/backend/scripts/feature_flag_sync_dual_test.py
@@ -276,6 +276,8 @@ def _posthog() -> Posthog:
client.feature_flags = flags
client.cohorts = {str(ids[p["name"]]): p["filters"]["properties"] for p in payloads}
client._get_flags_decision = _no_remote_flags
+ # Each read reports a $feature_flag_called event, which would post to PostHog.
+ client.capture = _no_events
return client
@@ -283,6 +285,10 @@ def _no_remote_flags(*args, **kwargs):
raise ConnectionError("only an in-process answer counts here")
+def _no_events(*args, **kwargs) -> None:
+ return None
+
+
def _record_posthog_reads(monkeypatch) -> dict[str, tuple[Any, bool]]:
reads: dict[str, tuple[Any, bool]] = {}
evaluate = ph.evaluate_flag