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
5 changes: 4 additions & 1 deletion internal/api/errors/incident.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@ 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",
)
var ErrIncidentPatchEndDateNotAfterStart = errors.New(
"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")

Expand Down
40 changes: 34 additions & 6 deletions internal/api/v2/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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.After(*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.ErrIncidentPatchEndDateNotAfterStart
}

return nil
Expand Down
273 changes: 273 additions & 0 deletions internal/api/v2/v2_patch_interval_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,273 @@
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.ErrIncidentPatchEndDateNotAfterStart,
},
{
name: "start edited after the stored end",
startDate: patchTimePtr(storedEnd.Add(time.Hour)),
expectedErr: errors.ErrIncidentPatchEndDateNotAfterStart,
},
{
name: "end edited before the stored start",
endDate: patchTimePtr(storedStart.Add(-time.Minute)),
expectedErr: errors.ErrIncidentPatchEndDateNotAfterStart,
},
{
name: "equal dates",
startDate: patchTimePtr(storedStart.Add(time.Hour)),
endDate: patchTimePtr(storedStart.Add(time.Hour)),
expectedErr: errors.ErrIncidentPatchEndDateNotAfterStart,
},
{
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.ErrIncidentPatchEndDateNotAfterStart,
},
{
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,
expectedErr: errors.ErrIncidentPatchResolvedBeforeStart,
},
{
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 be after 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 be after 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 be after start_date for a resolved event"}`,
w.Body.String(),
)
}
3 changes: 2 additions & 1 deletion internal/api/v2/v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down