Skip to content

Commit

Permalink
Prevent WaterSchedule with Duration or Interval 0
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinmclean committed Dec 10, 2024
1 parent 61dfa11 commit 5d7e320
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions garden-app/pkg/water_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
12 changes: 12 additions & 0 deletions garden-app/server/water_schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}}}`,
Expand Down

0 comments on commit 5d7e320

Please sign in to comment.