Skip to content

: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.

fix(api): Validate interval regardless of method (#70708)

Tomás Farías Santanabedecabfaf30
diff --git a/products/batch_exports/backend/api/batch_export.py b/products/batch_exports/backend/api/batch_export.py
index e09c07ebec22..7be85cfd7f1f 100644
--- a/products/batch_exports/backend/api/batch_export.py
+++ b/products/batch_exports/backend/api/batch_export.py
@@ -1070,6 +1070,28 @@ class BatchExportSerializer(serializers.ModelSerializer):
return attrs
+ def validate_interval(self, interval: str) -> str:
+ """Validate sub-hour frequency intervals are only available when feature flag is enabled."""
+ team_id = self.context["team_id"]
+
+ if interval not in ("hour", "day", "week"):
+ team = Team.objects.get(id=team_id)
+
+ if not posthoganalytics.feature_enabled(
+ "high-frequency-batch-exports",
+ str(team.uuid),
+ groups={"organization": str(team.organization.id)},
+ group_properties={
+ "organization": {
+ "id": str(team.organization.id),
+ "created_at": team.organization.created_at,
+ }
+ },
+ send_feature_flag_events=False,
+ ):
+ raise PermissionDenied("Higher frequency batch exports are not enabled for this team.")
+ return interval
+
def validate_timezone(self, timezone: str | None) -> str | None:
"""Validate timezone.
@@ -1400,23 +1422,6 @@ class BatchExportSerializer(serializers.ModelSerializer):
destination_data = validated_data.pop("destination")
team_id = self.context["team_id"]
- if validated_data["interval"] not in ("hour", "day", "week"):
- team = Team.objects.get(id=team_id)
-
- if not posthoganalytics.feature_enabled(
- "high-frequency-batch-exports",
- str(team.uuid),
- groups={"organization": str(team.organization.id)},
- group_properties={
- "organization": {
- "id": str(team.organization.id),
- "created_at": team.organization.created_at,
- }
- },
- send_feature_flag_events=False,
- ):
- raise PermissionDenied("Higher frequency batch exports are not enabled for this team.")
-
hogql_query = None
if hogql_query := validated_data.pop("hogql_query", None):
batch_export_schema = self.serialize_hogql_query_to_batch_export_schema(hogql_query)
diff --git a/products/batch_exports/backend/tests/api/test_update.py b/products/batch_exports/backend/tests/api/test_update.py
index 956423aab531..6a210bc68310 100644
--- a/products/batch_exports/backend/tests/api/test_update.py
+++ b/products/batch_exports/backend/tests/api/test_update.py
@@ -378,6 +378,28 @@ def test_can_patch_config(client: HttpClient, interval, temporal, encryption_cod
None,
"offset_hour is not applicable for non-daily/weekly intervals",
),
+ pytest.param(
+ {
+ "interval": "hour",
+ "timezone": "UTC",
+ "offset_day": None,
+ "offset_hour": None,
+ },
+ {
+ "interval": "every 5 minutes",
+ "timezone": "UTC",
+ "offset_day": None,
+ "offset_hour": None,
+ },
+ {
+ "interval": "hour",
+ "timezone": "UTC",
+ "offset_day": None,
+ "offset_hour": None,
+ },
+ "Higher frequency batch exports are not enabled for this team.",
+ id="Cannot update to high frequency batch exports without feature flag",
+ ),
],
)
def test_can_patch_schedule_configuration(
@@ -439,7 +461,7 @@ def test_can_patch_schedule_configuration(
response = patch_batch_export(client, team.pk, batch_export["id"], new_batch_export_data)
if expected_error:
- assert response.status_code == status.HTTP_400_BAD_REQUEST
+ assert response.status_code == status.HTTP_400_BAD_REQUEST or response.status_code == status.HTTP_403_FORBIDDEN
assert expected_error in response.json()["detail"]
return
assert response.status_code == status.HTTP_200_OK, response.json()