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(visual-review): reject a quarantine expiry in the past (#111760)

Julian Bez32866a4fd71b
diff --git a/products/visual_review/backend/presentation/serializers.py b/products/visual_review/backend/presentation/serializers.py
index 8f928744c9e7..b2b51f299818 100644
--- a/products/visual_review/backend/presentation/serializers.py
+++ b/products/visual_review/backend/presentation/serializers.py
@@ -4,6 +4,10 @@ DRF serializers for visual_review.
Converts DTOs to/from JSON using DataclassSerializer.
"""
+from datetime import datetime
+
+from django.utils import timezone
+
from rest_framework import serializers
from rest_framework_dataclasses.serializers import DataclassSerializer
@@ -408,6 +412,12 @@ class QuarantineInputSerializer(DataclassSerializer):
class Meta:
dataclass = QuarantineInput
+ def validate_expires_at(self, value: datetime | None) -> datetime | None:
+ # A past expiry would end the active quarantine and store one that is already over.
+ if value is not None and value <= timezone.now():
+ raise serializers.ValidationError("The expiry must be in the future.")
+ return value
+
class UnquarantineQuerySerializer(serializers.Serializer):
identifier = serializers.CharField(max_length=512, help_text="Snapshot identifier to unquarantine")
diff --git a/products/visual_review/backend/tests/test_presentation.py b/products/visual_review/backend/tests/test_presentation.py
index 8f835c381f91..d1a488c7a80f 100644
--- a/products/visual_review/backend/tests/test_presentation.py
+++ b/products/visual_review/backend/tests/test_presentation.py
@@ -149,16 +149,33 @@ class TestRepoViewSet(VisualReviewTeamScopedTestMixin, APIBaseTest):
assert entry.expires_at is not None
assert before + window <= entry.expires_at <= timezone.now() + window
- def test_opening_a_quarantine_still_needs_a_reason(self):
+ @parameterized.expand(
+ [
+ ("without_a_reason", {}),
+ ("with_a_past_expiry", {"reason": "flaky", "expires_at": "2020-01-01T00:00:00Z"}),
+ ]
+ )
+ def test_opening_a_quarantine_rejects_bad_input(self, _name, body):
repo = api.create_repo(team_id=self.team.id, repo_external_id=555, repo_full_name="org/open")
+ quarantine.quarantine_identifier(
+ repo_id=repo.id,
+ identifier="Button",
+ run_type=RunType.STORYBOOK,
+ reason="flaky",
+ user_id=self.user.id,
+ team_id=self.team.id,
+ )
response = self.client.post(
f"/api/projects/{self.team.id}/visual_review/repos/{repo.id}/quarantine/{RunType.STORYBOOK}",
- {"identifier": "Button"},
+ {"identifier": "Button", **body},
format="json",
)
assert response.status_code == status.HTTP_400_BAD_REQUEST
+ assert [
+ entry.identifier for entry in quarantine.list_quarantined_identifiers(repo.id, team_id=self.team.id)
+ ] == ["Button"]
class TestRunViewSet(VisualReviewTeamScopedTestMixin, APIBaseTest):