From 5d7e320696017305a1802d25723406243723519b Mon Sep 17 00:00:00 2001 From: Calvin McLean Date: Mon, 9 Dec 2024 19:59:55 -0700 Subject: [PATCH] Prevent WaterSchedule with Duration or Interval 0 --- garden-app/pkg/water_schedule.go | 7 +++++++ garden-app/server/water_schedule_test.go | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/garden-app/pkg/water_schedule.go b/garden-app/pkg/water_schedule.go index df425261..cc759c3b 100644 --- a/garden-app/pkg/water_schedule.go +++ b/garden-app/pkg/water_schedule.go @@ -289,6 +289,13 @@ func (ws *WaterSchedule) Bind(r *http.Request) error { } } + if ws.Duration != nil && ws.Duration.Duration == 0 { + return errors.New("duration must not be 0") + } + if ws.Interval != nil && ws.Interval.Duration == 0 { + return errors.New("interval must not be 0") + } + return nil } diff --git a/garden-app/server/water_schedule_test.go b/garden-app/server/water_schedule_test.go index 3e778cf2..3e0ee04a 100644 --- a/garden-app/server/water_schedule_test.go +++ b/garden-app/server/water_schedule_test.go @@ -433,6 +433,18 @@ func TestCreateWaterSchedule(t *testing.T) { `{"id":"[0-9a-v]{20}","duration":"1s","interval":"24h0m0s","start_date":"\d{4}-\d{2}-\d\dT\d\d:\d\d:\d\d\.\d+(-07:00|Z)","start_time":"11:24:52-07:00","next_water":{"time":"\d\d\d\d-\d\d-\d\dT11:24:52-07:00","duration":"1s"},"links":\[{"rel":"self","href":"/water_schedules/[0-9a-v]{20}"}\]}`, http.StatusCreated, }, + { + "ErrorDurationZero", + `{"duration":"0s","interval":"24h0m0s","start_time":"11:24:52-07:00"}`, + `{"status":"Invalid request.","error":"duration must not be 0"}`, + http.StatusBadRequest, + }, + { + "ErrorIntervalZero", + `{"duration":"10s","interval":0,"start_time":"11:24:52-07:00"}`, + `{"status":"Invalid request.","error":"interval must not be 0"}`, + http.StatusBadRequest, + }, { "ErrorRainWeatherClientDNE", `{"duration":"1s","interval":"24h0m0s","start_time":"11:24:52-07:00", "weather_control":{"rain_control":{"baseline_value":0,"factor":0,"range":25.4,"client_id":"c5cvhpcbcv45e8bp16dg"}}}`,