Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/1900.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Blocked editing of start time for planned maintenance tasks that have already started
2 changes: 2 additions & 0 deletions src/argus/plannedmaintenance/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ def clean(self):
raise ValidationError(
f"This planned maintenance task is no longer modifiable as it ended more than {hours} hours ago."
)
if not old.future and old.start_time != self.start_time:
raise ValidationError("The start time cannot be modified after the task has already started.")

def save(self, *args, **kwargs):
self.full_clean()
Expand Down
28 changes: 28 additions & 0 deletions tests/plannedmaintenance/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,31 @@ def test_given_current_pm_cancel_sets_end_time(self):

self.assertNotEqual(self.current_pm.end_time, current_pm_end_time)
self.assertLess(self.current_pm.end_time, timezone.now())

def test_given_task_has_not_started_then_can_modify_start_time(self):
new_start = self.future_pm.start_time + timedelta(hours=1)
self.future_pm.start_time = new_start
self.future_pm.save()
self.future_pm.refresh_from_db()
self.assertEqual(self.future_pm.start_time, new_start)

def test_given_task_has_already_started_then_cannot_modify_start_time(self):
original_start = self.current_pm.start_time
self.current_pm.start_time = timezone.now() + timedelta(hours=1)
with self.assertRaises(ValidationError):
self.current_pm.save()
self.current_pm.refresh_from_db()
self.assertEqual(self.current_pm.start_time, original_start)
Comment thread
aleksfl marked this conversation as resolved.

def test_given_task_has_recently_ended_then_cannot_modify_start_time(self):
now = timezone.now()
recently_ended_pm = PlannedMaintenanceFactory(
start_time=now - timedelta(hours=2),
end_time=now - timedelta(minutes=5),
)
original_start = recently_ended_pm.start_time
recently_ended_pm.start_time = now + timedelta(hours=1)
with self.assertRaises(ValidationError):
recently_ended_pm.save()
recently_ended_pm.refresh_from_db()
self.assertEqual(recently_ended_pm.start_time, original_start)
Loading