From b5044c32f73625679671d05a150fb12622ebcf16 Mon Sep 17 00:00:00 2001 From: Aloento <11802769+Aloento@users.noreply.github.com> Date: Tue, 1 Dec 2026 12:39:51 +0100 Subject: [PATCH 1/3] [Fix] Validate the effective interval when patching an incident An incident patch compared the incoming end_date with the stored start_date only, so a closed incident moving both dates at once could still end up with end_date <= start_date. Validate the window the patch leaves behind, and keep update_date as the end of a resolved event. --- internal/api/v2/v2.go | 40 +++- internal/api/v2/v2_patch_interval_test.go | 272 ++++++++++++++++++++++ 2 files changed, 306 insertions(+), 6 deletions(-) create mode 100644 internal/api/v2/v2_patch_interval_test.go diff --git a/internal/api/v2/v2.go b/internal/api/v2/v2.go index 5b4a4a5..8e761f6 100644 --- a/internal/api/v2/v2.go +++ b/internal/api/v2/v2.go @@ -1337,6 +1337,14 @@ func checkPatchData(incoming *PatchIncidentData, stored *db.Incident) error { return err } + if stored.Type == event.TypeIncident { + if err := checkIncidentPatchInterval(incoming, stored); err != nil { + return err + } + + return checkPatchDataForIncident(incoming, stored) + } + if incoming.EndDate != nil && stored.StartDate != nil && incoming.EndDate.Before(*stored.StartDate) { return apiErrors.ErrIncidentPatchEndDateBeforeStart } @@ -1347,15 +1355,35 @@ func checkPatchData(incoming *PatchIncidentData, stored *db.Incident) error { return apiErrors.ErrIncidentPatchClosedMaintenanceStatus } - if stored.Type == event.TypeIncident { - // updateFields stores the resolution time as end_date, so an update_date - // before start_date would invert the window the availability uses. - if incoming.Status == event.IncidentResolved && stored.StartDate != nil && - incoming.UpdateDate.Before(*stored.StartDate) { + return nil +} + +// checkIncidentPatchInterval validates the window a patch leaves behind instead +// of the incoming fields alone: a closed incident can move both dates at once, +// so an end date after the stored start can still land before the new start. +func checkIncidentPatchInterval(incoming *PatchIncidentData, stored *db.Incident) error { + start := stored.StartDate + if incoming.StartDate != nil { + start = incoming.StartDate + } + + if incoming.Status == event.IncidentResolved { + // updateFields stores the resolution time as end_date, so a resolved + // event ends at update_date whatever end_date the patch carries. + if start != nil && incoming.UpdateDate.Before(*start) { return apiErrors.ErrIncidentPatchResolvedBeforeStart } - return checkPatchDataForIncident(incoming, stored) + return nil + } + + end := stored.EndDate + if incoming.EndDate != nil { + end = incoming.EndDate + } + + if start != nil && end != nil && !end.After(*start) { + return apiErrors.ErrIncidentPatchEndDateBeforeStart } return nil diff --git a/internal/api/v2/v2_patch_interval_test.go b/internal/api/v2/v2_patch_interval_test.go new file mode 100644 index 0000000..9b090ea --- /dev/null +++ b/internal/api/v2/v2_patch_interval_test.go @@ -0,0 +1,272 @@ +package v2 + +import ( + "fmt" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/stackmon/otc-status-dashboard/internal/api/errors" + "github.com/stackmon/otc-status-dashboard/internal/db" + "github.com/stackmon/otc-status-dashboard/internal/event" +) + +func patchTimePtr(value time.Time) *time.Time { + return &value +} + +func TestCheckPatchDataClosedIncidentInterval(t *testing.T) { + storedStart := time.Date(2026, 11, 10, 12, 0, 0, 0, time.UTC) + storedEnd := storedStart.Add(4 * time.Hour) + impact := 2 + + tests := []struct { + name string + startDate *time.Time + endDate *time.Time + expectedErr error + }{ + { + name: "both dates edited into an inverted interval", + startDate: patchTimePtr(storedEnd.Add(-time.Hour)), + endDate: patchTimePtr(storedStart.Add(time.Hour)), + expectedErr: errors.ErrIncidentPatchEndDateBeforeStart, + }, + { + name: "start edited after the stored end", + startDate: patchTimePtr(storedEnd.Add(time.Hour)), + expectedErr: errors.ErrIncidentPatchEndDateBeforeStart, + }, + { + name: "end edited before the stored start", + endDate: patchTimePtr(storedStart.Add(-time.Minute)), + expectedErr: errors.ErrIncidentPatchEndDateBeforeStart, + }, + { + name: "equal dates", + startDate: patchTimePtr(storedStart.Add(time.Hour)), + endDate: patchTimePtr(storedStart.Add(time.Hour)), + expectedErr: errors.ErrIncidentPatchEndDateBeforeStart, + }, + { + name: "both dates corrected to an earlier window", + startDate: patchTimePtr(storedStart.Add(-2 * time.Hour)), + endDate: patchTimePtr(storedStart.Add(-time.Hour)), + }, + { + name: "start edited inside the stored window", + startDate: patchTimePtr(storedStart.Add(time.Hour)), + }, + { + name: "end edited after the stored end", + endDate: patchTimePtr(storedEnd.Add(time.Hour)), + }, + { + name: "changed without dates keeps the stored window", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + stored := &db.Incident{ + Type: event.TypeIncident, + Impact: &impact, + Status: event.IncidentResolved, + StartDate: &storedStart, + EndDate: &storedEnd, + } + incoming := &PatchIncidentData{ + Status: event.IncidentChanged, + UpdateDate: storedEnd, + StartDate: tc.startDate, + EndDate: tc.endDate, + } + + err := checkPatchData(incoming, stored) + + if tc.expectedErr != nil { + require.ErrorIs(t, err, tc.expectedErr) + return + } + assert.NoError(t, err) + }) + } +} + +func TestCheckPatchDataOpenIncidentInterval(t *testing.T) { + storedStart := time.Date(2026, 11, 10, 12, 0, 0, 0, time.UTC) + impact := 2 + + tests := []struct { + name string + status event.Status + startDate *time.Time + endDate *time.Time + updateDate time.Time + expectedErr error + }{ + { + name: "end date before the stored start", + status: event.IncidentAnalysing, + endDate: patchTimePtr(storedStart.Add(-time.Hour)), + updateDate: storedStart.Add(time.Minute), + expectedErr: errors.ErrIncidentPatchEndDateBeforeStart, + }, + { + name: "resolution before the effective start", + status: event.IncidentResolved, + updateDate: storedStart.Add(-time.Minute), + expectedErr: errors.ErrIncidentPatchResolvedBeforeStart, + }, + { + name: "resolution before an edited start", + status: event.IncidentResolved, + startDate: patchTimePtr(storedStart.Add(time.Hour)), + updateDate: storedStart.Add(time.Minute), + expectedErr: errors.ErrIncidentPatchResolvedBeforeStart, + }, + { + name: "resolution at the stored start", + status: event.IncidentResolved, + updateDate: storedStart, + }, + { + name: "resolution after the stored start", + status: event.IncidentResolved, + updateDate: storedStart.Add(time.Minute), + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + stored := &db.Incident{ + Type: event.TypeIncident, + Impact: &impact, + Status: event.IncidentAnalysing, + StartDate: &storedStart, + } + incoming := &PatchIncidentData{ + Status: tc.status, + UpdateDate: tc.updateDate, + StartDate: tc.startDate, + EndDate: tc.endDate, + } + + err := checkPatchData(incoming, stored) + + if tc.expectedErr != nil { + require.ErrorIs(t, err, tc.expectedErr) + return + } + assert.NoError(t, err) + }) + } +} + +func TestPatchEventInvertedIntervalHandler(t *testing.T) { + start := time.Date(2026, 11, 10, 12, 0, 0, 0, time.UTC) + end := start.Add(4 * time.Hour) + impact := 2 + title := "Test Incident" + + storedIncident := &db.Incident{ + ID: 113, + Text: &title, + Impact: &impact, + Type: event.TypeIncident, + Status: event.IncidentResolved, + StartDate: &start, + EndDate: &end, + } + + r := initRouterWithStoredEvent(t, storedIncident) + + body := fmt.Sprintf( + `{"status":"changed","message":"test message","update_date":%q,"start_date":%q,"end_date":%q}`, + end.Format(time.RFC3339), + end.Add(-time.Hour).Format(time.RFC3339), + start.Add(time.Hour).Format(time.RFC3339), + ) + + w := httptest.NewRecorder() + req, _ := http.NewRequest(http.MethodPatch, "/v2/events/113", strings.NewReader(body)) + req.Header.Set("Content-Type", "application/json") + + r.ServeHTTP(w, req) + + require.Equal(t, http.StatusBadRequest, w.Code) + assert.JSONEq(t, `{"errMsg":"end_date must not be earlier than start_date"}`, w.Body.String()) +} + +func TestPatchEventEqualIntervalHandler(t *testing.T) { + start := time.Date(2026, 11, 10, 12, 0, 0, 0, time.UTC) + end := start.Add(4 * time.Hour) + impact := 2 + title := "Test Incident" + + storedIncident := &db.Incident{ + ID: 114, + Text: &title, + Impact: &impact, + Type: event.TypeIncident, + Status: event.IncidentResolved, + StartDate: &start, + EndDate: &end, + } + + r := initRouterWithStoredEvent(t, storedIncident) + + edited := start.Add(time.Hour).Format(time.RFC3339) + body := fmt.Sprintf( + `{"status":"changed","message":"test message","update_date":%q,"start_date":%q,"end_date":%q}`, + end.Format(time.RFC3339), edited, edited, + ) + + w := httptest.NewRecorder() + req, _ := http.NewRequest(http.MethodPatch, "/v2/events/114", strings.NewReader(body)) + req.Header.Set("Content-Type", "application/json") + + r.ServeHTTP(w, req) + + require.Equal(t, http.StatusBadRequest, w.Code) + assert.JSONEq(t, `{"errMsg":"end_date must not be earlier than start_date"}`, w.Body.String()) +} + +func TestPatchEventResolvedBeforeStartHandler(t *testing.T) { + start := time.Date(2026, 11, 10, 12, 0, 0, 0, time.UTC) + impact := 2 + title := "Test Incident" + + storedIncident := &db.Incident{ + ID: 115, + Text: &title, + Impact: &impact, + Type: event.TypeIncident, + Status: event.IncidentAnalysing, + StartDate: &start, + } + + r := initRouterWithStoredEvent(t, storedIncident) + + body := fmt.Sprintf( + `{"status":"resolved","message":"test message","update_date":%q}`, + start.Add(-time.Minute).Format(time.RFC3339), + ) + + w := httptest.NewRecorder() + req, _ := http.NewRequest(http.MethodPatch, "/v2/events/115", strings.NewReader(body)) + req.Header.Set("Content-Type", "application/json") + + r.ServeHTTP(w, req) + + require.Equal(t, http.StatusBadRequest, w.Code) + assert.JSONEq(t, + `{"errMsg":"update_date must not be earlier than start_date for a resolved event"}`, + w.Body.String(), + ) +} From ecdc3a29e4dc2169782904891361239ee67b8e87 Mon Sep 17 00:00:00 2001 From: Aloento <11802769+Aloento@users.noreply.github.com> Date: Tue, 1 Dec 2026 12:41:30 +0100 Subject: [PATCH 2/3] [Fix] Reject resolving an incident at its start date A resolution exactly at the effective start left a zero-length window that the availability query drops. Require update_date to be later than the start, and word both patch interval errors to say so. --- internal/api/errors/incident.go | 4 ++-- internal/api/v2/v2.go | 2 +- internal/api/v2/v2_patch_interval_test.go | 13 +++++++------ internal/api/v2/v2_test.go | 3 ++- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/internal/api/errors/incident.go b/internal/api/errors/incident.go index 10691b9..7f7b88a 100644 --- a/internal/api/errors/incident.go +++ b/internal/api/errors/incident.go @@ -41,10 +41,10 @@ var ErrIncidentPatchClosedMaintenanceStatus = errors.New("wrong status for close var ErrIncidentPatchOpenedStartDate = errors.New("can not change start date for open incident") var ErrIncidentPatchOpenedEndDateMissing = errors.New("wrong end date with resolved status") var ErrIncidentPatchResolvedBeforeStart = errors.New( - "update_date must not be earlier than start_date for a resolved event", + "update_date must be after start_date for a resolved event", ) var ErrIncidentPatchEndDateBeforeStart = errors.New( - "end_date must not be earlier than start_date", + "end_date must be after start_date", ) var ErrIncidentPatchImpactStatusWrong = errors.New("wrong status for changing impact") var ErrIncidentPatchTypeForbidden = errors.New("can not change event type") diff --git a/internal/api/v2/v2.go b/internal/api/v2/v2.go index 8e761f6..01b35cd 100644 --- a/internal/api/v2/v2.go +++ b/internal/api/v2/v2.go @@ -1370,7 +1370,7 @@ func checkIncidentPatchInterval(incoming *PatchIncidentData, stored *db.Incident if incoming.Status == event.IncidentResolved { // updateFields stores the resolution time as end_date, so a resolved // event ends at update_date whatever end_date the patch carries. - if start != nil && incoming.UpdateDate.Before(*start) { + if start != nil && !incoming.UpdateDate.After(*start) { return apiErrors.ErrIncidentPatchResolvedBeforeStart } diff --git a/internal/api/v2/v2_patch_interval_test.go b/internal/api/v2/v2_patch_interval_test.go index 9b090ea..b27548c 100644 --- a/internal/api/v2/v2_patch_interval_test.go +++ b/internal/api/v2/v2_patch_interval_test.go @@ -131,9 +131,10 @@ func TestCheckPatchDataOpenIncidentInterval(t *testing.T) { expectedErr: errors.ErrIncidentPatchResolvedBeforeStart, }, { - name: "resolution at the stored start", - status: event.IncidentResolved, - updateDate: storedStart, + name: "resolution at the stored start", + status: event.IncidentResolved, + updateDate: storedStart, + expectedErr: errors.ErrIncidentPatchResolvedBeforeStart, }, { name: "resolution after the stored start", @@ -200,7 +201,7 @@ func TestPatchEventInvertedIntervalHandler(t *testing.T) { r.ServeHTTP(w, req) require.Equal(t, http.StatusBadRequest, w.Code) - assert.JSONEq(t, `{"errMsg":"end_date must not be earlier than start_date"}`, w.Body.String()) + assert.JSONEq(t, `{"errMsg":"end_date must be after start_date"}`, w.Body.String()) } func TestPatchEventEqualIntervalHandler(t *testing.T) { @@ -234,7 +235,7 @@ func TestPatchEventEqualIntervalHandler(t *testing.T) { r.ServeHTTP(w, req) require.Equal(t, http.StatusBadRequest, w.Code) - assert.JSONEq(t, `{"errMsg":"end_date must not be earlier than start_date"}`, w.Body.String()) + assert.JSONEq(t, `{"errMsg":"end_date must be after start_date"}`, w.Body.String()) } func TestPatchEventResolvedBeforeStartHandler(t *testing.T) { @@ -266,7 +267,7 @@ func TestPatchEventResolvedBeforeStartHandler(t *testing.T) { require.Equal(t, http.StatusBadRequest, w.Code) assert.JSONEq(t, - `{"errMsg":"update_date must not be earlier than start_date for a resolved event"}`, + `{"errMsg":"update_date must be after start_date for a resolved event"}`, w.Body.String(), ) } diff --git a/internal/api/v2/v2_test.go b/internal/api/v2/v2_test.go index 4d4805c..2b77fb4 100644 --- a/internal/api/v2/v2_test.go +++ b/internal/api/v2/v2_test.go @@ -437,10 +437,11 @@ func TestCheckPatchDataResolvedAgainstStartDate(t *testing.T) { expected error }{ { - name: "resolution at the start date is accepted", + name: "resolution at the start date is rejected", storedDate: &start, status: event.IncidentResolved, updateDate: start, + expected: errors.ErrIncidentPatchResolvedBeforeStart, }, { name: "resolution after the start date is accepted", From adc0177cffd73e1159709922cff4f61a1436bdbf Mon Sep 17 00:00:00 2001 From: Aloento <11802769+Aloento@users.noreply.github.com> Date: Tue, 1 Dec 2026 12:54:12 +0100 Subject: [PATCH 3/3] [Fix] Use a distinct error for an inverted incident interval The patch interval error is shared with maintenance and info, which still accept end_date equal to start_date. Keep their message unchanged and give incidents the stricter wording. --- internal/api/errors/incident.go | 3 +++ internal/api/v2/v2.go | 2 +- internal/api/v2/v2_patch_interval_test.go | 10 +++++----- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/internal/api/errors/incident.go b/internal/api/errors/incident.go index 7f7b88a..b7ad5c3 100644 --- a/internal/api/errors/incident.go +++ b/internal/api/errors/incident.go @@ -44,6 +44,9 @@ var ErrIncidentPatchResolvedBeforeStart = errors.New( "update_date must be after start_date for a resolved event", ) var ErrIncidentPatchEndDateBeforeStart = errors.New( + "end_date must not be earlier than start_date", +) +var ErrIncidentPatchEndDateNotAfterStart = errors.New( "end_date must be after start_date", ) var ErrIncidentPatchImpactStatusWrong = errors.New("wrong status for changing impact") diff --git a/internal/api/v2/v2.go b/internal/api/v2/v2.go index 01b35cd..fe453a4 100644 --- a/internal/api/v2/v2.go +++ b/internal/api/v2/v2.go @@ -1383,7 +1383,7 @@ func checkIncidentPatchInterval(incoming *PatchIncidentData, stored *db.Incident } if start != nil && end != nil && !end.After(*start) { - return apiErrors.ErrIncidentPatchEndDateBeforeStart + return apiErrors.ErrIncidentPatchEndDateNotAfterStart } return nil diff --git a/internal/api/v2/v2_patch_interval_test.go b/internal/api/v2/v2_patch_interval_test.go index b27548c..19c2bef 100644 --- a/internal/api/v2/v2_patch_interval_test.go +++ b/internal/api/v2/v2_patch_interval_test.go @@ -35,23 +35,23 @@ func TestCheckPatchDataClosedIncidentInterval(t *testing.T) { name: "both dates edited into an inverted interval", startDate: patchTimePtr(storedEnd.Add(-time.Hour)), endDate: patchTimePtr(storedStart.Add(time.Hour)), - expectedErr: errors.ErrIncidentPatchEndDateBeforeStart, + expectedErr: errors.ErrIncidentPatchEndDateNotAfterStart, }, { name: "start edited after the stored end", startDate: patchTimePtr(storedEnd.Add(time.Hour)), - expectedErr: errors.ErrIncidentPatchEndDateBeforeStart, + expectedErr: errors.ErrIncidentPatchEndDateNotAfterStart, }, { name: "end edited before the stored start", endDate: patchTimePtr(storedStart.Add(-time.Minute)), - expectedErr: errors.ErrIncidentPatchEndDateBeforeStart, + expectedErr: errors.ErrIncidentPatchEndDateNotAfterStart, }, { name: "equal dates", startDate: patchTimePtr(storedStart.Add(time.Hour)), endDate: patchTimePtr(storedStart.Add(time.Hour)), - expectedErr: errors.ErrIncidentPatchEndDateBeforeStart, + expectedErr: errors.ErrIncidentPatchEndDateNotAfterStart, }, { name: "both dates corrected to an earlier window", @@ -115,7 +115,7 @@ func TestCheckPatchDataOpenIncidentInterval(t *testing.T) { status: event.IncidentAnalysing, endDate: patchTimePtr(storedStart.Add(-time.Hour)), updateDate: storedStart.Add(time.Minute), - expectedErr: errors.ErrIncidentPatchEndDateBeforeStart, + expectedErr: errors.ErrIncidentPatchEndDateNotAfterStart, }, { name: "resolution before the effective start",