Skip to content

Commit

Permalink
guard for nil iteration_count when updating survey iterations
Browse files Browse the repository at this point in the history
  • Loading branch information
Phanatic committed Jul 23, 2024
1 parent 5dd0781 commit a6a6036
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
9 changes: 8 additions & 1 deletion posthog/api/survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,14 @@ def update(self, instance: Survey, validated_data):
instance.targeting_flag.save()

iteration_count = validated_data.get("iteration_count")
if instance.current_iteration is not None and instance.current_iteration > iteration_count > 0:
if iteration_count is None:
iteration_count = 0

if (
instance.current_iteration is not None
and iteration_count is not None
and instance.current_iteration > iteration_count > 0
):
raise serializers.ValidationError(
f"Cannot change survey recurrence to {iteration_count}, should be at least {instance.current_iteration}"
)
Expand Down
29 changes: 29 additions & 0 deletions posthog/api/test/test_survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -2346,6 +2346,35 @@ def test_cannot_reduce_iterations_lt_current_iteration(self):
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert response.json()["detail"] == "Cannot change survey recurrence to 1, should be at least 2"

def test_can_set_recurrence_if_previously_disabled(self):
survey = self._create_recurring_survey()
survey.current_iteration = 2
survey.save()
response = self.client.patch(
f"/api/projects/{self.team.id}/surveys/{survey.id}/",
data={
"start_date": datetime.now() - timedelta(days=1),
},
)
response_data = response.json()
survey.refresh_from_db()
survey.current_iteration = 2
survey.save()
response = self.client.patch(
f"/api/projects/{self.team.id}/surveys/{survey.id}/",
data={
"start_date": datetime.now() - timedelta(days=1),
"iteration_count": 2,
"iteration_frequency_days": 30,
},
)

assert response.status_code == status.HTTP_200_OK
response_data = response.json()
assert response_data["iteration_start_dates"] is not None
assert len(response_data["iteration_start_dates"]) == 2
assert response_data["current_iteration"] == 1

def test_can_turn_off_recurring_schedule(self):
survey = self._create_recurring_survey()
response = self.client.patch(
Expand Down

0 comments on commit a6a6036

Please sign in to comment.