Skip to content

Commit

Permalink
fix(batch-exports): Do not allow backfills in the future
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasfarias committed Aug 21, 2024
1 parent c802071 commit 197740f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
45 changes: 45 additions & 0 deletions posthog/api/test/batch_exports/test_backfill.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import datetime as dt

import pytest
from django.test.client import Client as HttpClient
from freezegun import freeze_time
from rest_framework import status

from posthog.api.test.batch_exports.conftest import start_test_worker
Expand Down Expand Up @@ -97,6 +100,48 @@ def test_batch_export_backfill_with_non_isoformatted_dates(client: HttpClient):
assert response.status_code == status.HTTP_400_BAD_REQUEST, response.json()


def test_batch_export_backfill_with_end_at_in_the_future(client: HttpClient):
"""Test a BatchExport backfill fails if we pass malformed dates."""
temporal = sync_connect()

destination_data = {
"type": "S3",
"config": {
"bucket_name": "my-production-s3-bucket",
"region": "us-east-1",
"prefix": "posthog-events/",
"aws_access_key_id": "abc123",
"aws_secret_access_key": "secret",
},
}
batch_export_data = {
"name": "my-production-s3-bucket-destination",
"destination": destination_data,
"interval": "hour",
}

organization = create_organization("Test Org")
team = create_team(organization)
user = create_user("[email protected]", "Test User", organization)
test_time = dt.datetime.now(dt.UTC)
client.force_login(user)

with start_test_worker(temporal):
batch_export = create_batch_export_ok(client, team.pk, batch_export_data)

batch_export_id = batch_export["id"]

with freeze_time(test_time):
response = backfill_batch_export(
client,
team.pk,
batch_export_id,
test_time.isoformat(),
(test_time + dt.timedelta(hours=1, seconds=1)).isoformat(),
)
assert response.status_code == status.HTTP_400_BAD_REQUEST, response.json()


def test_batch_export_backfill_with_naive_bounds(client: HttpClient):
"""Test a BatchExport backfill fails if we naive dates."""
temporal = sync_connect()
Expand Down
6 changes: 6 additions & 0 deletions posthog/batch_exports/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,12 @@ def backfill(self, request: request.Request, *args, **kwargs) -> response.Respon
raise ValidationError("The initial backfill datetime 'start_at' happens after 'end_at'")

batch_export = self.get_object()

if end_at > dt.datetime.now(dt.UTC) + batch_export.interval_time_delta:
raise ValidationError(
f"The provided 'end_at' ({end_at.isoformat()}) is too far into the future. Cannot backfill beyond 1 batch period into the future."
)

temporal = sync_connect()
try:
backfill_id = backfill_export(temporal, str(batch_export.pk), self.team_id, start_at, end_at)
Expand Down

0 comments on commit 197740f

Please sign in to comment.