Skip to content

Commit 2c8e311

Browse files
committed
verify same timezone for start/end
1 parent 26e0f02 commit 2c8e311

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

coderd/insights.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,22 @@ func parseInsightsStartAndEndTime(ctx context.Context, rw http.ResponseWriter, s
389389
}
390390
*qp.dest = t
391391
}
392+
if startTime.Location().String() != endTime.Location().String() {
393+
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
394+
Message: "Query parameter has invalid value.",
395+
Validations: []codersdk.ValidationError{
396+
{
397+
Field: "start_time",
398+
Detail: fmt.Sprintf("Query param %q must have the same timezone as %q", "start_time", "end_time"),
399+
},
400+
{
401+
Field: "end_time",
402+
Detail: fmt.Sprintf("Query param %q must have the same timezone as %q", "end_time", "start_time"),
403+
},
404+
},
405+
})
406+
return time.Time{}, time.Time{}, false
407+
}
392408
if endTime.Before(startTime) {
393409
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
394410
Message: "Query parameter has invalid value.",

coderd/insights_internal_test.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
func Test_parseInsightsStartAndEndTime(t *testing.T) {
1616
t.Parallel()
1717

18-
format := codersdk.InsightsTimeLayout
18+
layout := codersdk.InsightsTimeLayout
1919
now := time.Now().UTC()
2020
y, m, d := now.Date()
2121
today := time.Date(y, m, d, 0, 0, 0, 0, time.UTC)
@@ -49,8 +49,8 @@ func Test_parseInsightsStartAndEndTime(t *testing.T) {
4949
{
5050
name: "Today",
5151
args: args{
52-
startTime: today.Format(format),
53-
endTime: thisHour.Format(format),
52+
startTime: today.Format(layout),
53+
endTime: thisHour.Format(layout),
5454
},
5555
wantStartTime: time.Date(2023, 7, today.Day(), 0, 0, 0, 0, time.UTC),
5656
wantEndTime: time.Date(2023, 7, today.Day(), thisHour.Hour(), 0, 0, 0, time.UTC),
@@ -59,16 +59,16 @@ func Test_parseInsightsStartAndEndTime(t *testing.T) {
5959
{
6060
name: "Today with minutes and seconds",
6161
args: args{
62-
startTime: today.Format(format),
63-
endTime: thisHour.Add(time.Minute + time.Second).Format(format),
62+
startTime: today.Format(layout),
63+
endTime: thisHour.Add(time.Minute + time.Second).Format(layout),
6464
},
6565
wantOk: false,
6666
},
6767
{
6868
name: "Today (hour round up)",
6969
args: args{
70-
startTime: today.Format(format),
71-
endTime: thisHourRoundUp.Format(format),
70+
startTime: today.Format(layout),
71+
endTime: thisHourRoundUp.Format(layout),
7272
},
7373
wantStartTime: time.Date(2023, 7, today.Day(), 0, 0, 0, 0, time.UTC),
7474
wantEndTime: time.Date(2023, 7, today.Day(), thisHourRoundUp.Hour(), 0, 0, 0, time.UTC),
@@ -84,6 +84,14 @@ func Test_parseInsightsStartAndEndTime(t *testing.T) {
8484
wantEndTime: time.Date(2023, 7, 17, 0, 0, 0, 0, helsinki),
8585
wantOk: true,
8686
},
87+
{
88+
name: "Mixed timezone week",
89+
args: args{
90+
startTime: "2023-07-10T00:00:00Z",
91+
endTime: "2023-07-17T00:00:00+03:00",
92+
},
93+
wantOk: false,
94+
},
8795
{
8896
name: "Bad format",
8997
args: args{
@@ -95,24 +103,24 @@ func Test_parseInsightsStartAndEndTime(t *testing.T) {
95103
{
96104
name: "Zero time",
97105
args: args{
98-
startTime: (time.Time{}).Format(format),
99-
endTime: (time.Time{}).Format(format),
106+
startTime: (time.Time{}).Format(layout),
107+
endTime: (time.Time{}).Format(layout),
100108
},
101109
wantOk: false,
102110
},
103111
{
104112
name: "Time in future",
105113
args: args{
106-
startTime: today.AddDate(0, 0, 1).Format(format),
107-
endTime: today.AddDate(0, 0, 2).Format(format),
114+
startTime: today.AddDate(0, 0, 1).Format(layout),
115+
endTime: today.AddDate(0, 0, 2).Format(layout),
108116
},
109117
wantOk: false,
110118
},
111119
{
112120
name: "End before start",
113121
args: args{
114-
startTime: today.Format(format),
115-
endTime: today.AddDate(0, 0, -1).Format(format),
122+
startTime: today.Format(layout),
123+
endTime: today.AddDate(0, 0, -1).Format(layout),
116124
},
117125
wantOk: false,
118126
},

0 commit comments

Comments
 (0)