From fbcbd03c92d4d4ff5ca14c3781268900296870cf Mon Sep 17 00:00:00 2001 From: ferzunzu Date: Fri, 26 Jul 2024 13:05:00 +0200 Subject: [PATCH] Validate that the start date is earlier than the maximum year of the library datetime. --- taiga/projects/milestones/models.py | 6 ++++++ .../test_milestones_resources.py | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/taiga/projects/milestones/models.py b/taiga/projects/milestones/models.py index da325d89..8a7b1887 100644 --- a/taiga/projects/milestones/models.py +++ b/taiga/projects/milestones/models.py @@ -72,6 +72,10 @@ def __repr__(self): return "".format(self.id) def clean(self): + + if self.estimated_start and self.estimated_start.year == datetime.MAXYEAR: + raise ValidationError(_("Invalid estimated start year, it must be less than 9999.")) + # Don't allow draft entries to have a pub_date. if self.estimated_start and self.estimated_finish and self.estimated_start >= self.estimated_finish: raise ValidationError(_('The estimated start must be previous to the estimated finish.')) @@ -107,6 +111,7 @@ def closed_points(self): ) def total_closed_points_by_date(self, date): + # Milestone instance will keep a cache of the total closed points by date if self._total_closed_points_by_date is None: self._total_closed_points_by_date = {} @@ -165,6 +170,7 @@ def total_closed_points_by_date(self, date): acumulated_date_points = 0 current_date = self.estimated_start + while current_date <= self.estimated_finish: acumulated_date_points += self._total_closed_points_by_date.get(current_date, 0) self._total_closed_points_by_date[current_date] = acumulated_date_points diff --git a/tests/integration/resources_permissions/test_milestones_resources.py b/tests/integration/resources_permissions/test_milestones_resources.py index b7a063a9..03790dc2 100644 --- a/tests/integration/resources_permissions/test_milestones_resources.py +++ b/tests/integration/resources_permissions/test_milestones_resources.py @@ -295,6 +295,23 @@ def test_milestone_create(client, data): assert results == [401, 403, 403, 451, 451] +def test_milestone_create_invalid_dates(client, data): + url = reverse('milestones-list') + + user = data.project_owner + create_data = json.dumps({ + "name": "test", + "estimated_start": "9999-12-10", + "estimated_finish": "9999-12-24", + "project": data.public_project.pk, + }) + + client.login(user) + + response = client.post(url, create_data, content_type="application/json") + assert response.status_code == 400 + assert response.data["__all__"][0] == "Invalid estimated start year, it must be less than 9999." + def test_milestone_patch(client, data): public_url = reverse('milestones-detail', kwargs={"pk": data.public_milestone.pk}) private_url1 = reverse('milestones-detail', kwargs={"pk": data.private_milestone1.pk})